blob: 9c6ce73b03c5585994ee4099106a70dbe7cca51c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * HD audio interface patch for C-Media CMI9880
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 *
9 * This driver is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This driver is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/pci.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include "hda_codec.h"
30#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020031#include "hda_auto_parser.h"
Takashi Iwaib060fb02012-12-19 17:35:47 +010032#include "hda_jack.h"
33#include "hda_generic.h"
34
Takashi Iwaif953eff2005-04-08 15:05:13 +020035#define NUM_PINS 11
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37
38/* board config type */
39enum {
40 CMI_MINIMAL, /* back 3-jack */
41 CMI_MIN_FP, /* back 3-jack + front-panel 2-jack */
42 CMI_FULL, /* back 6-jack + front-panel 2-jack */
43 CMI_FULL_DIG, /* back 6-jack + front-panel 2-jack + digital I/O */
44 CMI_ALLOUT, /* back 5-jack + front-panel 2-jack + digital out */
Takashi Iwaif953eff2005-04-08 15:05:13 +020045 CMI_AUTO, /* let driver guess it */
Takashi Iwaif5fcc132006-11-24 17:07:44 +010046 CMI_MODELS
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
49struct cmi_spec {
Takashi Iwaib060fb02012-12-19 17:35:47 +010050 struct hda_gen_spec gen;
51
52 /* below are only for static models */
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int board_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 unsigned int no_line_in: 1; /* no line-in (5-jack) */
56 unsigned int front_panel: 1; /* has front-panel 2-jack */
57
58 /* playback */
59 struct hda_multi_out multiout;
Takashi Iwai41923e42007-10-22 17:20:10 +020060 hda_nid_t dac_nids[AUTO_CFG_MAX_OUTS]; /* NID for each DAC */
Takashi Iwaie9edcee2005-06-13 14:16:38 +020061 int num_dacs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /* capture */
Takashi Iwai779d0652011-05-02 11:34:20 +020064 const hda_nid_t *adc_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 hda_nid_t dig_in_nid;
66
67 /* capture source */
68 const struct hda_input_mux *input_mux;
69 unsigned int cur_mux[2];
70
71 /* channel mode */
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +010072 int num_channel_modes;
73 const struct hda_channel_mode *channel_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 struct hda_pcm pcm_rec[2]; /* PCM information */
Takashi Iwaif953eff2005-04-08 15:05:13 +020076
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020077 /* pin default configuration */
Takashi Iwaif953eff2005-04-08 15:05:13 +020078 hda_nid_t pin_nid[NUM_PINS];
79 unsigned int def_conf[NUM_PINS];
80 unsigned int pin_def_confs;
81
82 /* multichannel pins */
Takashi Iwaif953eff2005-04-08 15:05:13 +020083 struct hda_verb multi_init[9]; /* 2 verbs for each pin + terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
86/*
87 * input MUX
88 */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +010089static int cmi_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
92 struct cmi_spec *spec = codec->spec;
93 return snd_hda_input_mux_info(spec->input_mux, uinfo);
94}
95
Takashi Iwaic8b6bf92005-11-17 14:57:47 +010096static int cmi_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
99 struct cmi_spec *spec = codec->spec;
100 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
101
102 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
103 return 0;
104}
105
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100106static int cmi_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
109 struct cmi_spec *spec = codec->spec;
110 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
111
112 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
113 spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
114}
115
116/*
117 * shared line-in, mic for surrounds
118 */
119
120/* 3-stack / 2 channel */
Takashi Iwai779d0652011-05-02 11:34:20 +0200121static const struct hda_verb cmi9880_ch2_init[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* set line-in PIN for input */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200123 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* set mic PIN for input, also enable vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200125 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /* route front PCM (DAC1) to HP */
127 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
128 {}
129};
130
131/* 3-stack / 6 channel */
Takashi Iwai779d0652011-05-02 11:34:20 +0200132static const struct hda_verb cmi9880_ch6_init[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* set line-in PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200134 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* set mic PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200136 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* route front PCM (DAC1) to HP */
138 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
139 {}
140};
141
142/* 3-stack+front / 8 channel */
Takashi Iwai779d0652011-05-02 11:34:20 +0200143static const struct hda_verb cmi9880_ch8_init[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* set line-in PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200145 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /* set mic PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200147 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* route rear-surround PCM (DAC4) to HP */
149 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x03 },
150 {}
151};
152
Takashi Iwai779d0652011-05-02 11:34:20 +0200153static const struct hda_channel_mode cmi9880_channel_modes[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 { 2, cmi9880_ch2_init },
155 { 6, cmi9880_ch6_init },
156 { 8, cmi9880_ch8_init },
157};
158
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100159static int cmi_ch_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
162 struct cmi_spec *spec = codec->spec;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100163 return snd_hda_ch_mode_info(codec, uinfo, spec->channel_modes,
164 spec->num_channel_modes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100167static int cmi_ch_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
170 struct cmi_spec *spec = codec->spec;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100171 return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_modes,
172 spec->num_channel_modes, spec->multiout.max_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100175static int cmi_ch_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
178 struct cmi_spec *spec = codec->spec;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100179 return snd_hda_ch_mode_put(codec, ucontrol, spec->channel_modes,
180 spec->num_channel_modes, &spec->multiout.max_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/*
184 */
Takashi Iwai779d0652011-05-02 11:34:20 +0200185static const struct snd_kcontrol_new cmi9880_basic_mixer[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* CMI9880 has no playback volumes! */
187 HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT), /* front */
188 HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0x0, HDA_OUTPUT),
189 HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
190 HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
191 HDA_CODEC_MUTE("Side Playback Switch", 0x06, 0x0, HDA_OUTPUT),
192 {
193 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
194 /* The multiple "Capture Source" controls confuse alsamixer
195 * So call somewhat different..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
197 /* .name = "Capture Source", */
198 .name = "Input Source",
199 .count = 2,
200 .info = cmi_mux_enum_info,
201 .get = cmi_mux_enum_get,
202 .put = cmi_mux_enum_put,
203 },
204 HDA_CODEC_VOLUME("Capture Volume", 0x08, 0, HDA_INPUT),
205 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0, HDA_INPUT),
206 HDA_CODEC_MUTE("Capture Switch", 0x08, 0, HDA_INPUT),
207 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0, HDA_INPUT),
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100208 HDA_CODEC_VOLUME("Beep Playback Volume", 0x23, 0, HDA_OUTPUT),
209 HDA_CODEC_MUTE("Beep Playback Switch", 0x23, 0, HDA_OUTPUT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 { } /* end */
211};
212
213/*
214 * shared I/O pins
215 */
Takashi Iwai779d0652011-05-02 11:34:20 +0200216static const struct snd_kcontrol_new cmi9880_ch_mode_mixer[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 {
218 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
219 .name = "Channel Mode",
220 .info = cmi_ch_mode_info,
221 .get = cmi_ch_mode_get,
222 .put = cmi_ch_mode_put,
223 },
224 { } /* end */
225};
226
227/* AUD-in selections:
228 * 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x1f 0x20
229 */
Takashi Iwai779d0652011-05-02 11:34:20 +0200230static const struct hda_input_mux cmi9880_basic_mux = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .num_items = 4,
232 .items = {
233 { "Front Mic", 0x5 },
234 { "Rear Mic", 0x2 },
235 { "Line", 0x1 },
236 { "CD", 0x7 },
237 }
238};
239
Takashi Iwai779d0652011-05-02 11:34:20 +0200240static const struct hda_input_mux cmi9880_no_line_mux = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .num_items = 3,
242 .items = {
243 { "Front Mic", 0x5 },
244 { "Rear Mic", 0x2 },
245 { "CD", 0x7 },
246 }
247};
248
249/* front, rear, clfe, rear_surr */
Takashi Iwai779d0652011-05-02 11:34:20 +0200250static const hda_nid_t cmi9880_dac_nids[4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 0x03, 0x04, 0x05, 0x06
252};
253/* ADC0, ADC1 */
Takashi Iwai779d0652011-05-02 11:34:20 +0200254static const hda_nid_t cmi9880_adc_nids[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 0x08, 0x09
256};
257
258#define CMI_DIG_OUT_NID 0x07
259#define CMI_DIG_IN_NID 0x0a
260
261/*
262 */
Takashi Iwai779d0652011-05-02 11:34:20 +0200263static const struct hda_verb cmi9880_basic_init[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* port-D for line out (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200265 { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* port-E for HP out (front panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200267 { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* route front PCM to HP */
269 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
270 /* port-A for surround (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200271 { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* port-G for CLFE (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200273 { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100274 { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* port-H for side (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200276 { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100277 { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* port-C for line-in (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200279 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* port-B for mic-in (rear panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200281 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* port-F for mic-in (front panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200283 { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* CD-in */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200285 { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* route front mic to ADC1/2 */
287 { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
288 { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
289 {} /* terminator */
290};
291
Takashi Iwai779d0652011-05-02 11:34:20 +0200292static const struct hda_verb cmi9880_allout_init[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* port-D for line out (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200294 { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* port-E for HP out (front panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200296 { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* route front PCM to HP */
298 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
299 /* port-A for side (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200300 { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* port-G for CLFE (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200302 { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100303 { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
304 /* port-H for side (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200305 { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100306 { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* port-C for surround (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200308 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* port-B for mic-in (rear panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200310 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* port-F for mic-in (front panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200312 { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* CD-in */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200314 { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* route front mic to ADC1/2 */
316 { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
317 { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
318 {} /* terminator */
319};
320
321/*
322 */
323static int cmi9880_build_controls(struct hda_codec *codec)
324{
325 struct cmi_spec *spec = codec->spec;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100326 struct snd_kcontrol *kctl;
327 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 err = snd_hda_add_new_ctls(codec, cmi9880_basic_mixer);
330 if (err < 0)
331 return err;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100332 if (spec->channel_modes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 err = snd_hda_add_new_ctls(codec, cmi9880_ch_mode_mixer);
334 if (err < 0)
335 return err;
336 }
337 if (spec->multiout.dig_out_nid) {
Stephen Warren74b654c2011-06-01 11:14:18 -0600338 err = snd_hda_create_spdif_out_ctls(codec,
339 spec->multiout.dig_out_nid,
340 spec->multiout.dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (err < 0)
342 return err;
Takashi Iwai9a081602008-02-12 18:37:26 +0100343 err = snd_hda_create_spdif_share_sw(codec,
344 &spec->multiout);
345 if (err < 0)
346 return err;
347 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 if (spec->dig_in_nid) {
350 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
351 if (err < 0)
352 return err;
353 }
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100354
355 /* assign Capture Source enums to NID */
356 kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
357 for (i = 0; kctl && i < kctl->count; i++) {
Takashi Iwai21949f02009-12-23 08:31:59 +0100358 err = snd_hda_add_nid(codec, kctl, i, spec->adc_nids[i]);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100359 if (err < 0)
360 return err;
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363}
364
365static int cmi9880_init(struct hda_codec *codec)
366{
367 struct cmi_spec *spec = codec->spec;
368 if (spec->board_config == CMI_ALLOUT)
369 snd_hda_sequence_write(codec, cmi9880_allout_init);
370 else
371 snd_hda_sequence_write(codec, cmi9880_basic_init);
Takashi Iwaif953eff2005-04-08 15:05:13 +0200372 if (spec->board_config == CMI_AUTO)
373 snd_hda_sequence_write(codec, spec->multi_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/*
378 * Analog playback callbacks
379 */
380static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
381 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100382 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct cmi_spec *spec = codec->spec;
Takashi Iwai9a081602008-02-12 18:37:26 +0100385 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
386 hinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
390 struct hda_codec *codec,
391 unsigned int stream_tag,
392 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100393 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct cmi_spec *spec = codec->spec;
396 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
397 format, substream);
398}
399
400static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
401 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100402 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct cmi_spec *spec = codec->spec;
405 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
406}
407
408/*
409 * Digital out
410 */
411static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
412 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100413 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct cmi_spec *spec = codec->spec;
416 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
417}
418
419static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
420 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100421 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct cmi_spec *spec = codec->spec;
424 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
425}
426
Takashi Iwai6b97eb42007-04-05 14:51:48 +0200427static int cmi9880_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
428 struct hda_codec *codec,
429 unsigned int stream_tag,
430 unsigned int format,
431 struct snd_pcm_substream *substream)
432{
433 struct cmi_spec *spec = codec->spec;
434 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
435 format, substream);
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/*
439 * Analog capture
440 */
441static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
442 struct hda_codec *codec,
443 unsigned int stream_tag,
444 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100445 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct cmi_spec *spec = codec->spec;
448
449 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
450 stream_tag, 0, format);
451 return 0;
452}
453
454static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
455 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100456 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct cmi_spec *spec = codec->spec;
459
Takashi Iwai888afa12008-03-18 09:57:50 +0100460 snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462}
463
464
465/*
466 */
Takashi Iwai779d0652011-05-02 11:34:20 +0200467static const struct hda_pcm_stream cmi9880_pcm_analog_playback = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 .substreams = 1,
469 .channels_min = 2,
470 .channels_max = 8,
471 .nid = 0x03, /* NID to query formats and rates */
472 .ops = {
473 .open = cmi9880_playback_pcm_open,
474 .prepare = cmi9880_playback_pcm_prepare,
475 .cleanup = cmi9880_playback_pcm_cleanup
476 },
477};
478
Takashi Iwai779d0652011-05-02 11:34:20 +0200479static const struct hda_pcm_stream cmi9880_pcm_analog_capture = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 .substreams = 2,
481 .channels_min = 2,
482 .channels_max = 2,
483 .nid = 0x08, /* NID to query formats and rates */
484 .ops = {
485 .prepare = cmi9880_capture_pcm_prepare,
486 .cleanup = cmi9880_capture_pcm_cleanup
487 },
488};
489
Takashi Iwai779d0652011-05-02 11:34:20 +0200490static const struct hda_pcm_stream cmi9880_pcm_digital_playback = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 .substreams = 1,
492 .channels_min = 2,
493 .channels_max = 2,
494 /* NID is set in cmi9880_build_pcms */
495 .ops = {
496 .open = cmi9880_dig_playback_pcm_open,
Takashi Iwai6b97eb42007-04-05 14:51:48 +0200497 .close = cmi9880_dig_playback_pcm_close,
498 .prepare = cmi9880_dig_playback_pcm_prepare
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 },
500};
501
Takashi Iwai779d0652011-05-02 11:34:20 +0200502static const struct hda_pcm_stream cmi9880_pcm_digital_capture = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 .substreams = 1,
504 .channels_min = 2,
505 .channels_max = 2,
506 /* NID is set in cmi9880_build_pcms */
507};
508
509static int cmi9880_build_pcms(struct hda_codec *codec)
510{
511 struct cmi_spec *spec = codec->spec;
512 struct hda_pcm *info = spec->pcm_rec;
513
514 codec->num_pcms = 1;
515 codec->pcm_info = info;
516
517 info->name = "CMI9880";
518 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
519 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;
520
521 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
522 codec->num_pcms++;
523 info++;
524 info->name = "CMI9880 Digital";
Takashi Iwai7ba72ba2008-02-06 14:03:20 +0100525 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (spec->multiout.dig_out_nid) {
527 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
528 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
529 }
530 if (spec->dig_in_nid) {
531 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
532 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
533 }
534 }
535
536 return 0;
537}
538
539static void cmi9880_free(struct hda_codec *codec)
540{
541 kfree(codec->spec);
542}
543
544/*
545 */
546
Takashi Iwaiea734962011-01-17 11:29:34 +0100547static const char * const cmi9880_models[CMI_MODELS] = {
Takashi Iwaif5fcc132006-11-24 17:07:44 +0100548 [CMI_MINIMAL] = "minimal",
549 [CMI_MIN_FP] = "min_fp",
550 [CMI_FULL] = "full",
551 [CMI_FULL_DIG] = "full_dig",
552 [CMI_ALLOUT] = "allout",
553 [CMI_AUTO] = "auto",
554};
555
Takashi Iwai779d0652011-05-02 11:34:20 +0200556static const struct snd_pci_quirk cmi9880_cfg_tbl[] = {
Takashi Iwaif5fcc132006-11-24 17:07:44 +0100557 SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", CMI_FULL_DIG),
Takashi Iwai79d06432008-05-30 16:54:49 +0200558 SND_PCI_QUIRK(0x1854, 0x002b, "LG LS75", CMI_MINIMAL),
Jiang zhe5b030382008-04-14 13:26:53 +0200559 SND_PCI_QUIRK(0x1854, 0x0032, "LG", CMI_FULL_DIG),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 {} /* terminator */
561};
562
Takashi Iwai779d0652011-05-02 11:34:20 +0200563static const struct hda_codec_ops cmi9880_patch_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .build_controls = cmi9880_build_controls,
565 .build_pcms = cmi9880_build_pcms,
566 .init = cmi9880_init,
567 .free = cmi9880_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568};
569
Takashi Iwaib060fb02012-12-19 17:35:47 +0100570/*
571 * stuff for auto-parser
572 */
573static const struct hda_codec_ops cmi_auto_patch_ops = {
574 .build_controls = snd_hda_gen_build_controls,
575 .build_pcms = snd_hda_gen_build_pcms,
576 .init = snd_hda_gen_init,
577 .free = snd_hda_gen_free,
Takashi Iwai8a6c21a2013-01-18 07:51:17 +0100578 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwaib060fb02012-12-19 17:35:47 +0100579};
580
581static int cmi_parse_auto_config(struct hda_codec *codec)
582{
583 struct cmi_spec *spec = codec->spec;
584 struct auto_pin_cfg *cfg = &spec->gen.autocfg;
585 int err;
586
587 snd_hda_gen_spec_init(&spec->gen);
588
589 err = snd_hda_parse_pin_defcfg(codec, cfg, NULL, 0);
590 if (err < 0)
591 return err;
592 err = snd_hda_gen_parse_auto_config(codec, cfg);
593 if (err < 0)
594 return err;
595
596 codec->patch_ops = cmi_auto_patch_ops;
597 return 0;
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600static int patch_cmi9880(struct hda_codec *codec)
601{
602 struct cmi_spec *spec;
603
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200604 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (spec == NULL)
606 return -ENOMEM;
607
608 codec->spec = spec;
Takashi Iwaif5fcc132006-11-24 17:07:44 +0100609 spec->board_config = snd_hda_check_board_config(codec, CMI_MODELS,
610 cmi9880_models,
611 cmi9880_cfg_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (spec->board_config < 0) {
Takashi Iwai9a11f1a2009-07-28 16:01:20 +0200613 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
614 codec->chip_name);
Takashi Iwaif953eff2005-04-08 15:05:13 +0200615 spec->board_config = CMI_AUTO; /* try everything */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Takashi Iwaib060fb02012-12-19 17:35:47 +0100618 if (spec->board_config == CMI_AUTO) {
619 int err = cmi_parse_auto_config(codec);
620 if (err < 0) {
621 snd_hda_gen_free(codec);
622 return err;
623 }
624 return 0;
625 }
626
Takashi Iwaif953eff2005-04-08 15:05:13 +0200627 /* copy default DAC NIDs */
628 memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200629 spec->num_dacs = 4;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 switch (spec->board_config) {
632 case CMI_MINIMAL:
633 case CMI_MIN_FP:
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100634 spec->channel_modes = cmi9880_channel_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (spec->board_config == CMI_MINIMAL)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100636 spec->num_channel_modes = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 else {
638 spec->front_panel = 1;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100639 spec->num_channel_modes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
642 spec->input_mux = &cmi9880_basic_mux;
643 break;
644 case CMI_FULL:
645 case CMI_FULL_DIG:
646 spec->front_panel = 1;
647 spec->multiout.max_channels = 8;
648 spec->input_mux = &cmi9880_basic_mux;
649 if (spec->board_config == CMI_FULL_DIG) {
650 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
651 spec->dig_in_nid = CMI_DIG_IN_NID;
652 }
653 break;
654 case CMI_ALLOUT:
Takashi Iwaib060fb02012-12-19 17:35:47 +0100655 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 spec->front_panel = 1;
657 spec->multiout.max_channels = 8;
658 spec->no_line_in = 1;
659 spec->input_mux = &cmi9880_no_line_mux;
660 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
661 break;
662 }
663
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200664 spec->multiout.num_dacs = spec->num_dacs;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200665 spec->multiout.dac_nids = spec->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 spec->adc_nids = cmi9880_adc_nids;
668
669 codec->patch_ops = cmi9880_patch_ops;
670
671 return 0;
672}
673
674/*
675 * patch entries
676 */
Takashi Iwai779d0652011-05-02 11:34:20 +0200677static const struct hda_codec_preset snd_hda_preset_cmedia[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 { .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
679 { .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
680 {} /* terminator */
681};
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100682
683MODULE_ALIAS("snd-hda-codec-id:13f69880");
684MODULE_ALIAS("snd-hda-codec-id:434d4980");
685
686MODULE_LICENSE("GPL");
687MODULE_DESCRIPTION("C-Media HD-audio codec");
688
689static struct hda_codec_preset_list cmedia_list = {
690 .preset = snd_hda_preset_cmedia,
691 .owner = THIS_MODULE,
692};
693
694static int __init patch_cmedia_init(void)
695{
696 return snd_hda_add_codec_preset(&cmedia_list);
697}
698
699static void __exit patch_cmedia_exit(void)
700{
701 snd_hda_delete_codec_preset(&cmedia_list);
702}
703
704module_init(patch_cmedia_init)
705module_exit(patch_cmedia_exit)