xref: /openbmc/linux/sound/pci/hda/patch_cmedia.c (revision f42b3800)
1 /*
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 
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pci.h>
28 #include <sound/core.h>
29 #include "hda_codec.h"
30 #include "hda_local.h"
31 #define NUM_PINS	11
32 
33 
34 /* board config type */
35 enum {
36 	CMI_MINIMAL,	/* back 3-jack */
37 	CMI_MIN_FP,	/* back 3-jack + front-panel 2-jack */
38 	CMI_FULL,	/* back 6-jack + front-panel 2-jack */
39 	CMI_FULL_DIG,	/* back 6-jack + front-panel 2-jack + digital I/O */
40 	CMI_ALLOUT,	/* back 5-jack + front-panel 2-jack + digital out */
41 	CMI_AUTO,	/* let driver guess it */
42 	CMI_MODELS
43 };
44 
45 struct cmi_spec {
46 	int board_config;
47 	unsigned int no_line_in: 1;	/* no line-in (5-jack) */
48 	unsigned int front_panel: 1;	/* has front-panel 2-jack */
49 
50 	/* playback */
51 	struct hda_multi_out multiout;
52 	hda_nid_t dac_nids[AUTO_CFG_MAX_OUTS];	/* NID for each DAC */
53 	int num_dacs;
54 
55 	/* capture */
56 	hda_nid_t *adc_nids;
57 	hda_nid_t dig_in_nid;
58 
59 	/* capture source */
60 	const struct hda_input_mux *input_mux;
61 	unsigned int cur_mux[2];
62 
63 	/* channel mode */
64 	int num_channel_modes;
65 	const struct hda_channel_mode *channel_modes;
66 
67 	struct hda_pcm pcm_rec[2];	/* PCM information */
68 
69 	/* pin deafault configuration */
70 	hda_nid_t pin_nid[NUM_PINS];
71 	unsigned int def_conf[NUM_PINS];
72 	unsigned int pin_def_confs;
73 
74 	/* multichannel pins */
75 	struct hda_verb multi_init[9];	/* 2 verbs for each pin + terminator */
76 };
77 
78 /*
79  * input MUX
80  */
81 static int cmi_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
82 {
83 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
84 	struct cmi_spec *spec = codec->spec;
85 	return snd_hda_input_mux_info(spec->input_mux, uinfo);
86 }
87 
88 static int cmi_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
89 {
90 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
91 	struct cmi_spec *spec = codec->spec;
92 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
93 
94 	ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
95 	return 0;
96 }
97 
98 static int cmi_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
99 {
100 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
101 	struct cmi_spec *spec = codec->spec;
102 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
103 
104 	return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
105 				     spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
106 }
107 
108 /*
109  * shared line-in, mic for surrounds
110  */
111 
112 /* 3-stack / 2 channel */
113 static struct hda_verb cmi9880_ch2_init[] = {
114 	/* set line-in PIN for input */
115 	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
116 	/* set mic PIN for input, also enable vref */
117 	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
118 	/* route front PCM (DAC1) to HP */
119 	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
120 	{}
121 };
122 
123 /* 3-stack / 6 channel */
124 static struct hda_verb cmi9880_ch6_init[] = {
125 	/* set line-in PIN for output */
126 	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
127 	/* set mic PIN for output */
128 	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
129 	/* route front PCM (DAC1) to HP */
130 	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
131 	{}
132 };
133 
134 /* 3-stack+front / 8 channel */
135 static struct hda_verb cmi9880_ch8_init[] = {
136 	/* set line-in PIN for output */
137 	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
138 	/* set mic PIN for output */
139 	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
140 	/* route rear-surround PCM (DAC4) to HP */
141 	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x03 },
142 	{}
143 };
144 
145 static struct hda_channel_mode cmi9880_channel_modes[3] = {
146 	{ 2, cmi9880_ch2_init },
147 	{ 6, cmi9880_ch6_init },
148 	{ 8, cmi9880_ch8_init },
149 };
150 
151 static int cmi_ch_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
152 {
153 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
154 	struct cmi_spec *spec = codec->spec;
155 	return snd_hda_ch_mode_info(codec, uinfo, spec->channel_modes,
156 				    spec->num_channel_modes);
157 }
158 
159 static int cmi_ch_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
160 {
161 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
162 	struct cmi_spec *spec = codec->spec;
163 	return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_modes,
164 				   spec->num_channel_modes, spec->multiout.max_channels);
165 }
166 
167 static int cmi_ch_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
168 {
169 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
170 	struct cmi_spec *spec = codec->spec;
171 	return snd_hda_ch_mode_put(codec, ucontrol, spec->channel_modes,
172 				   spec->num_channel_modes, &spec->multiout.max_channels);
173 }
174 
175 /*
176  */
177 static struct snd_kcontrol_new cmi9880_basic_mixer[] = {
178 	/* CMI9880 has no playback volumes! */
179 	HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT), /* front */
180 	HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0x0, HDA_OUTPUT),
181 	HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
182 	HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
183 	HDA_CODEC_MUTE("Side Playback Switch", 0x06, 0x0, HDA_OUTPUT),
184 	{
185 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
186 		/* The multiple "Capture Source" controls confuse alsamixer
187 		 * So call somewhat different..
188 		 */
189 		/* .name = "Capture Source", */
190 		.name = "Input Source",
191 		.count = 2,
192 		.info = cmi_mux_enum_info,
193 		.get = cmi_mux_enum_get,
194 		.put = cmi_mux_enum_put,
195 	},
196 	HDA_CODEC_VOLUME("Capture Volume", 0x08, 0, HDA_INPUT),
197 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0, HDA_INPUT),
198 	HDA_CODEC_MUTE("Capture Switch", 0x08, 0, HDA_INPUT),
199 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0, HDA_INPUT),
200 	HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x23, 0, HDA_OUTPUT),
201 	HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x23, 0, HDA_OUTPUT),
202 	{ } /* end */
203 };
204 
205 /*
206  * shared I/O pins
207  */
208 static struct snd_kcontrol_new cmi9880_ch_mode_mixer[] = {
209 	{
210 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
211 		.name = "Channel Mode",
212 		.info = cmi_ch_mode_info,
213 		.get = cmi_ch_mode_get,
214 		.put = cmi_ch_mode_put,
215 	},
216 	{ } /* end */
217 };
218 
219 /* AUD-in selections:
220  * 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x1f 0x20
221  */
222 static struct hda_input_mux cmi9880_basic_mux = {
223 	.num_items = 4,
224 	.items = {
225 		{ "Front Mic", 0x5 },
226 		{ "Rear Mic", 0x2 },
227 		{ "Line", 0x1 },
228 		{ "CD", 0x7 },
229 	}
230 };
231 
232 static struct hda_input_mux cmi9880_no_line_mux = {
233 	.num_items = 3,
234 	.items = {
235 		{ "Front Mic", 0x5 },
236 		{ "Rear Mic", 0x2 },
237 		{ "CD", 0x7 },
238 	}
239 };
240 
241 /* front, rear, clfe, rear_surr */
242 static hda_nid_t cmi9880_dac_nids[4] = {
243 	0x03, 0x04, 0x05, 0x06
244 };
245 /* ADC0, ADC1 */
246 static hda_nid_t cmi9880_adc_nids[2] = {
247 	0x08, 0x09
248 };
249 
250 #define CMI_DIG_OUT_NID	0x07
251 #define CMI_DIG_IN_NID	0x0a
252 
253 /*
254  */
255 static struct hda_verb cmi9880_basic_init[] = {
256 	/* port-D for line out (rear panel) */
257 	{ 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
258 	/* port-E for HP out (front panel) */
259 	{ 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
260 	/* route front PCM to HP */
261 	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
262 	/* port-A for surround (rear panel) */
263 	{ 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
264 	/* port-G for CLFE (rear panel) */
265 	{ 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
266 	{ 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
267 	/* port-H for side (rear panel) */
268 	{ 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
269 	{ 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
270 	/* port-C for line-in (rear panel) */
271 	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
272 	/* port-B for mic-in (rear panel) with vref */
273 	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
274 	/* port-F for mic-in (front panel) with vref */
275 	{ 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
276 	/* CD-in */
277 	{ 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
278 	/* route front mic to ADC1/2 */
279 	{ 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
280 	{ 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
281 	{} /* terminator */
282 };
283 
284 static struct hda_verb cmi9880_allout_init[] = {
285 	/* port-D for line out (rear panel) */
286 	{ 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
287 	/* port-E for HP out (front panel) */
288 	{ 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
289 	/* route front PCM to HP */
290 	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
291 	/* port-A for side (rear panel) */
292 	{ 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
293 	/* port-G for CLFE (rear panel) */
294 	{ 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
295 	{ 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
296 	/* port-H for side (rear panel) */
297 	{ 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
298 	{ 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
299 	/* port-C for surround (rear panel) */
300 	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
301 	/* port-B for mic-in (rear panel) with vref */
302 	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
303 	/* port-F for mic-in (front panel) with vref */
304 	{ 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
305 	/* CD-in */
306 	{ 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
307 	/* route front mic to ADC1/2 */
308 	{ 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
309 	{ 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
310 	{} /* terminator */
311 };
312 
313 /*
314  */
315 static int cmi9880_build_controls(struct hda_codec *codec)
316 {
317 	struct cmi_spec *spec = codec->spec;
318 	int err;
319 
320 	err = snd_hda_add_new_ctls(codec, cmi9880_basic_mixer);
321 	if (err < 0)
322 		return err;
323 	if (spec->channel_modes) {
324 		err = snd_hda_add_new_ctls(codec, cmi9880_ch_mode_mixer);
325 		if (err < 0)
326 			return err;
327 	}
328 	if (spec->multiout.dig_out_nid) {
329 		err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
330 		if (err < 0)
331 			return err;
332 	}
333 	if (spec->dig_in_nid) {
334 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
335 		if (err < 0)
336 			return err;
337 	}
338 	return 0;
339 }
340 
341 /* fill in the multi_dac_nids table, which will decide
342    which audio widget to use for each channel */
343 static int cmi9880_fill_multi_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
344 {
345 	struct cmi_spec *spec = codec->spec;
346 	hda_nid_t nid;
347 	int assigned[4];
348 	int i, j;
349 
350 	/* clear the table, only one c-media dac assumed here */
351 	memset(spec->dac_nids, 0, sizeof(spec->dac_nids));
352 	memset(assigned, 0, sizeof(assigned));
353 	/* check the pins we found */
354 	for (i = 0; i < cfg->line_outs; i++) {
355 		nid = cfg->line_out_pins[i];
356 		/* nid 0x0b~0x0e is hardwired to audio widget 0x3~0x6 */
357 		if (nid >= 0x0b && nid <= 0x0e) {
358 			spec->dac_nids[i] = (nid - 0x0b) + 0x03;
359 			assigned[nid - 0x0b] = 1;
360 		}
361 	}
362 	/* left pin can be connect to any audio widget */
363 	for (i = 0; i < cfg->line_outs; i++) {
364 		nid = cfg->line_out_pins[i];
365 		if (nid <= 0x0e)
366 			continue;
367 		/* search for an empty channel */
368 		for (j = 0; j < cfg->line_outs; j++) {
369 			if (! assigned[j]) {
370 				spec->dac_nids[i] = j + 0x03;
371 				assigned[j] = 1;
372 				break;
373 			}
374 		}
375 	}
376 	spec->num_dacs = cfg->line_outs;
377 	return 0;
378 }
379 
380 /* create multi_init table, which is used for multichannel initialization */
381 static int cmi9880_fill_multi_init(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
382 {
383 	struct cmi_spec *spec = codec->spec;
384 	hda_nid_t nid;
385 	int i, j, k, len;
386 
387 	/* clear the table, only one c-media dac assumed here */
388 	memset(spec->multi_init, 0, sizeof(spec->multi_init));
389 	for (j = 0, i = 0; i < cfg->line_outs; i++) {
390 		hda_nid_t conn[4];
391 		nid = cfg->line_out_pins[i];
392 		/* set as output */
393 		spec->multi_init[j].nid = nid;
394 		spec->multi_init[j].verb = AC_VERB_SET_PIN_WIDGET_CONTROL;
395 		spec->multi_init[j].param = PIN_OUT;
396 		j++;
397 		if (nid > 0x0e) {
398 			/* set connection */
399 			spec->multi_init[j].nid = nid;
400 			spec->multi_init[j].verb = AC_VERB_SET_CONNECT_SEL;
401 			spec->multi_init[j].param = 0;
402 			/* find the index in connect list */
403 			len = snd_hda_get_connections(codec, nid, conn, 4);
404 			for (k = 0; k < len; k++)
405 				if (conn[k] == spec->dac_nids[i]) {
406 					spec->multi_init[j].param = k;
407 					break;
408 				}
409 			j++;
410 		}
411 	}
412 	return 0;
413 }
414 
415 static int cmi9880_init(struct hda_codec *codec)
416 {
417 	struct cmi_spec *spec = codec->spec;
418 	if (spec->board_config == CMI_ALLOUT)
419 		snd_hda_sequence_write(codec, cmi9880_allout_init);
420 	else
421 		snd_hda_sequence_write(codec, cmi9880_basic_init);
422 	if (spec->board_config == CMI_AUTO)
423 		snd_hda_sequence_write(codec, spec->multi_init);
424 	return 0;
425 }
426 
427 /*
428  * Analog playback callbacks
429  */
430 static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
431 				     struct hda_codec *codec,
432 				     struct snd_pcm_substream *substream)
433 {
434 	struct cmi_spec *spec = codec->spec;
435 	return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
436 }
437 
438 static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
439 					struct hda_codec *codec,
440 					unsigned int stream_tag,
441 					unsigned int format,
442 					struct snd_pcm_substream *substream)
443 {
444 	struct cmi_spec *spec = codec->spec;
445 	return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
446 						format, substream);
447 }
448 
449 static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
450 				       struct hda_codec *codec,
451 				       struct snd_pcm_substream *substream)
452 {
453 	struct cmi_spec *spec = codec->spec;
454 	return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
455 }
456 
457 /*
458  * Digital out
459  */
460 static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
461 					 struct hda_codec *codec,
462 					 struct snd_pcm_substream *substream)
463 {
464 	struct cmi_spec *spec = codec->spec;
465 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
466 }
467 
468 static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
469 					  struct hda_codec *codec,
470 					  struct snd_pcm_substream *substream)
471 {
472 	struct cmi_spec *spec = codec->spec;
473 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
474 }
475 
476 static int cmi9880_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
477 					    struct hda_codec *codec,
478 					    unsigned int stream_tag,
479 					    unsigned int format,
480 					    struct snd_pcm_substream *substream)
481 {
482 	struct cmi_spec *spec = codec->spec;
483 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
484 					     format, substream);
485 }
486 
487 /*
488  * Analog capture
489  */
490 static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
491 				      struct hda_codec *codec,
492 				      unsigned int stream_tag,
493 				      unsigned int format,
494 				      struct snd_pcm_substream *substream)
495 {
496 	struct cmi_spec *spec = codec->spec;
497 
498 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
499 				   stream_tag, 0, format);
500 	return 0;
501 }
502 
503 static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
504 				      struct hda_codec *codec,
505 				      struct snd_pcm_substream *substream)
506 {
507 	struct cmi_spec *spec = codec->spec;
508 
509 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
510 	return 0;
511 }
512 
513 
514 /*
515  */
516 static struct hda_pcm_stream cmi9880_pcm_analog_playback = {
517 	.substreams = 1,
518 	.channels_min = 2,
519 	.channels_max = 8,
520 	.nid = 0x03, /* NID to query formats and rates */
521 	.ops = {
522 		.open = cmi9880_playback_pcm_open,
523 		.prepare = cmi9880_playback_pcm_prepare,
524 		.cleanup = cmi9880_playback_pcm_cleanup
525 	},
526 };
527 
528 static struct hda_pcm_stream cmi9880_pcm_analog_capture = {
529 	.substreams = 2,
530 	.channels_min = 2,
531 	.channels_max = 2,
532 	.nid = 0x08, /* NID to query formats and rates */
533 	.ops = {
534 		.prepare = cmi9880_capture_pcm_prepare,
535 		.cleanup = cmi9880_capture_pcm_cleanup
536 	},
537 };
538 
539 static struct hda_pcm_stream cmi9880_pcm_digital_playback = {
540 	.substreams = 1,
541 	.channels_min = 2,
542 	.channels_max = 2,
543 	/* NID is set in cmi9880_build_pcms */
544 	.ops = {
545 		.open = cmi9880_dig_playback_pcm_open,
546 		.close = cmi9880_dig_playback_pcm_close,
547 		.prepare = cmi9880_dig_playback_pcm_prepare
548 	},
549 };
550 
551 static struct hda_pcm_stream cmi9880_pcm_digital_capture = {
552 	.substreams = 1,
553 	.channels_min = 2,
554 	.channels_max = 2,
555 	/* NID is set in cmi9880_build_pcms */
556 };
557 
558 static int cmi9880_build_pcms(struct hda_codec *codec)
559 {
560 	struct cmi_spec *spec = codec->spec;
561 	struct hda_pcm *info = spec->pcm_rec;
562 
563 	codec->num_pcms = 1;
564 	codec->pcm_info = info;
565 
566 	info->name = "CMI9880";
567 	info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
568 	info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;
569 
570 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
571 		codec->num_pcms++;
572 		info++;
573 		info->name = "CMI9880 Digital";
574 		if (spec->multiout.dig_out_nid) {
575 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
576 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
577 		}
578 		if (spec->dig_in_nid) {
579 			info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
580 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
581 		}
582 	}
583 
584 	return 0;
585 }
586 
587 static void cmi9880_free(struct hda_codec *codec)
588 {
589 	kfree(codec->spec);
590 }
591 
592 /*
593  */
594 
595 static const char *cmi9880_models[CMI_MODELS] = {
596 	[CMI_MINIMAL]	= "minimal",
597 	[CMI_MIN_FP]	= "min_fp",
598 	[CMI_FULL]	= "full",
599 	[CMI_FULL_DIG]	= "full_dig",
600 	[CMI_ALLOUT]	= "allout",
601 	[CMI_AUTO]	= "auto",
602 };
603 
604 static struct snd_pci_quirk cmi9880_cfg_tbl[] = {
605 	SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", CMI_FULL_DIG),
606 	{} /* terminator */
607 };
608 
609 static struct hda_codec_ops cmi9880_patch_ops = {
610 	.build_controls = cmi9880_build_controls,
611 	.build_pcms = cmi9880_build_pcms,
612 	.init = cmi9880_init,
613 	.free = cmi9880_free,
614 };
615 
616 static int patch_cmi9880(struct hda_codec *codec)
617 {
618 	struct cmi_spec *spec;
619 
620 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
621 	if (spec == NULL)
622 		return -ENOMEM;
623 
624 	codec->spec = spec;
625 	spec->board_config = snd_hda_check_board_config(codec, CMI_MODELS,
626 							cmi9880_models,
627 							cmi9880_cfg_tbl);
628 	if (spec->board_config < 0) {
629 		snd_printdd(KERN_INFO "hda_codec: Unknown model for CMI9880\n");
630 		spec->board_config = CMI_AUTO; /* try everything */
631 	}
632 
633 	/* copy default DAC NIDs */
634 	memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
635 	spec->num_dacs = 4;
636 
637 	switch (spec->board_config) {
638 	case CMI_MINIMAL:
639 	case CMI_MIN_FP:
640 		spec->channel_modes = cmi9880_channel_modes;
641 		if (spec->board_config == CMI_MINIMAL)
642 			spec->num_channel_modes = 2;
643 		else {
644 			spec->front_panel = 1;
645 			spec->num_channel_modes = 3;
646 		}
647 		spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
648 		spec->input_mux = &cmi9880_basic_mux;
649 		break;
650 	case CMI_FULL:
651 	case CMI_FULL_DIG:
652 		spec->front_panel = 1;
653 		spec->multiout.max_channels = 8;
654 		spec->input_mux = &cmi9880_basic_mux;
655 		if (spec->board_config == CMI_FULL_DIG) {
656 			spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
657 			spec->dig_in_nid = CMI_DIG_IN_NID;
658 		}
659 		break;
660 	case CMI_ALLOUT:
661 		spec->front_panel = 1;
662 		spec->multiout.max_channels = 8;
663 		spec->no_line_in = 1;
664 		spec->input_mux = &cmi9880_no_line_mux;
665 		spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
666 		break;
667 	case CMI_AUTO:
668 		{
669 		unsigned int port_e, port_f, port_g, port_h;
670 		unsigned int port_spdifi, port_spdifo;
671 		struct auto_pin_cfg cfg;
672 
673 		/* collect pin default configuration */
674 		port_e = snd_hda_codec_read(codec, 0x0f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
675 		port_f = snd_hda_codec_read(codec, 0x10, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
676 		spec->front_panel = 1;
677 		if (get_defcfg_connect(port_e) == AC_JACK_PORT_NONE ||
678 		    get_defcfg_connect(port_f) == AC_JACK_PORT_NONE) {
679 			port_g = snd_hda_codec_read(codec, 0x1f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
680 			port_h = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
681 			spec->channel_modes = cmi9880_channel_modes;
682 			/* no front panel */
683 			if (get_defcfg_connect(port_g) == AC_JACK_PORT_NONE ||
684 			    get_defcfg_connect(port_h) == AC_JACK_PORT_NONE) {
685 				/* no optional rear panel */
686 				spec->board_config = CMI_MINIMAL;
687 				spec->front_panel = 0;
688 				spec->num_channel_modes = 2;
689 			} else {
690 				spec->board_config = CMI_MIN_FP;
691 				spec->num_channel_modes = 3;
692 			}
693 			spec->input_mux = &cmi9880_basic_mux;
694 			spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
695 		} else {
696 			spec->input_mux = &cmi9880_basic_mux;
697 			port_spdifi = snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
698 			port_spdifo = snd_hda_codec_read(codec, 0x12, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
699 			if (get_defcfg_connect(port_spdifo) != AC_JACK_PORT_NONE)
700 				spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
701 			if (get_defcfg_connect(port_spdifi) != AC_JACK_PORT_NONE)
702 				spec->dig_in_nid = CMI_DIG_IN_NID;
703 			spec->multiout.max_channels = 8;
704 		}
705 		snd_hda_parse_pin_def_config(codec, &cfg, NULL);
706 		if (cfg.line_outs) {
707 			spec->multiout.max_channels = cfg.line_outs * 2;
708 			cmi9880_fill_multi_dac_nids(codec, &cfg);
709 			cmi9880_fill_multi_init(codec, &cfg);
710 		} else
711 			snd_printd("patch_cmedia: cannot detect association in defcfg\n");
712 		break;
713 		}
714 	}
715 
716 	spec->multiout.num_dacs = spec->num_dacs;
717 	spec->multiout.dac_nids = spec->dac_nids;
718 
719 	spec->adc_nids = cmi9880_adc_nids;
720 
721 	codec->patch_ops = cmi9880_patch_ops;
722 
723 	return 0;
724 }
725 
726 /*
727  * patch entries
728  */
729 struct hda_codec_preset snd_hda_preset_cmedia[] = {
730 	{ .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
731  	{ .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
732 	{} /* terminator */
733 };
734