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