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