[ALSA] hda-codec - Add channel-mode helper
[linux-2.6.git] / sound / pci / hda / patch_cmedia.c
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 */
36 enum {
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 };
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[4];          /* 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         hda_nid_t multich_pin[4];       /* max 8-channel */
76         struct hda_verb multi_init[9];  /* 2 verbs for each pin + terminator */
77 };
78
79 /* amp values */
80 #define AMP_IN_MUTE(idx)        (0x7080 | ((idx)<<8))
81 #define AMP_IN_UNMUTE(idx)      (0x7000 | ((idx)<<8))
82 #define AMP_OUT_MUTE    0xb080
83 #define AMP_OUT_UNMUTE  0xb000
84 #define AMP_OUT_ZERO    0xb000
85 /* pinctl values */
86 #define PIN_IN          0x20
87 #define PIN_VREF80      0x24
88 #define PIN_VREF50      0x21
89 #define PIN_OUT         0x40
90 #define PIN_HP          0xc0
91
92 /*
93  * input MUX
94  */
95 static int cmi_mux_enum_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
96 {
97         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
98         struct cmi_spec *spec = codec->spec;
99         return snd_hda_input_mux_info(spec->input_mux, uinfo);
100 }
101
102 static int cmi_mux_enum_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
103 {
104         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
105         struct cmi_spec *spec = codec->spec;
106         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
107
108         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
109         return 0;
110 }
111
112 static int cmi_mux_enum_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
113 {
114         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
115         struct cmi_spec *spec = codec->spec;
116         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
117
118         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
119                                      spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
120 }
121
122 /*
123  * shared line-in, mic for surrounds
124  */
125
126 /* 3-stack / 2 channel */
127 static struct hda_verb cmi9880_ch2_init[] = {
128         /* set line-in PIN for input */
129         { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
130         /* set mic PIN for input, also enable vref */
131         { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
132         /* route front PCM (DAC1) to HP */
133         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
134         {}
135 };
136
137 /* 3-stack / 6 channel */
138 static struct hda_verb cmi9880_ch6_init[] = {
139         /* set line-in PIN for output */
140         { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
141         /* set mic PIN for output */
142         { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
143         /* route front PCM (DAC1) to HP */
144         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
145         {}
146 };
147
148 /* 3-stack+front / 8 channel */
149 static struct hda_verb cmi9880_ch8_init[] = {
150         /* set line-in PIN for output */
151         { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
152         /* set mic PIN for output */
153         { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
154         /* route rear-surround PCM (DAC4) to HP */
155         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x03 },
156         {}
157 };
158
159 static struct hda_channel_mode cmi9880_channel_modes[3] = {
160         { 2, cmi9880_ch2_init },
161         { 6, cmi9880_ch6_init },
162         { 8, cmi9880_ch8_init },
163 };
164
165 static int cmi_ch_mode_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
166 {
167         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
168         struct cmi_spec *spec = codec->spec;
169         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_modes,
170                                     spec->num_channel_modes);
171 }
172
173 static int cmi_ch_mode_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
174 {
175         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
176         struct cmi_spec *spec = codec->spec;
177         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_modes,
178                                    spec->num_channel_modes, spec->multiout.max_channels);
179 }
180
181 static int cmi_ch_mode_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
182 {
183         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
184         struct cmi_spec *spec = codec->spec;
185         return snd_hda_ch_mode_put(codec, ucontrol, spec->channel_modes,
186                                    spec->num_channel_modes, &spec->multiout.max_channels);
187 }
188
189 /*
190  */
191 static snd_kcontrol_new_t cmi9880_basic_mixer[] = {
192         /* CMI9880 has no playback volumes! */
193         HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT), /* front */
194         HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0x0, HDA_OUTPUT),
195         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
196         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
197         HDA_CODEC_MUTE("Side Playback Switch", 0x06, 0x0, HDA_OUTPUT),
198         {
199                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
200                 /* The multiple "Capture Source" controls confuse alsamixer
201                  * So call somewhat different..
202                  * FIXME: the controls appear in the "playback" view!
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("PC Speaker Playback Volume", 0x23, 0, HDA_OUTPUT),
216         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x23, 0, HDA_OUTPUT),
217         { } /* end */
218 };
219
220 /*
221  * shared I/O pins
222  */
223 static snd_kcontrol_new_t 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 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 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 hda_nid_t cmi9880_dac_nids[4] = {
258         0x03, 0x04, 0x05, 0x06
259 };
260 /* ADC0, ADC1 */
261 static 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 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 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         int err;
334
335         err = snd_hda_add_new_ctls(codec, cmi9880_basic_mixer);
336         if (err < 0)
337                 return err;
338         if (spec->channel_modes) {
339                 err = snd_hda_add_new_ctls(codec, cmi9880_ch_mode_mixer);
340                 if (err < 0)
341                         return err;
342         }
343         if (spec->multiout.dig_out_nid) {
344                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
345                 if (err < 0)
346                         return err;
347         }
348         if (spec->dig_in_nid) {
349                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
350                 if (err < 0)
351                         return err;
352         }
353         return 0;
354 }
355
356 /* fill in the multi_dac_nids table, which will decide
357    which audio widget to use for each channel */
358 static int cmi9880_fill_multi_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
359 {
360         struct cmi_spec *spec = codec->spec;
361         hda_nid_t nid;
362         int assigned[4];
363         int i, j;
364
365         /* clear the table, only one c-media dac assumed here */
366         memset(spec->dac_nids, 0, sizeof(spec->dac_nids));
367         memset(assigned, 0, sizeof(assigned));
368         /* check the pins we found */
369         for (i = 0; i < cfg->line_outs; i++) {
370                 nid = cfg->line_out_pins[i];
371                 /* nid 0x0b~0x0e is hardwired to audio widget 0x3~0x6 */
372                 if (nid >= 0x0b && nid <= 0x0e) {
373                         spec->dac_nids[i] = (nid - 0x0b) + 0x03;
374                         assigned[nid - 0x0b] = 1;
375                 }
376         }
377         /* left pin can be connect to any audio widget */
378         for (i = 0; i < cfg->line_outs; i++) {
379                 nid = cfg->line_out_pins[i];
380                 if (nid <= 0x0e)
381                         continue;
382                 /* search for an empty channel */
383                 for (j = 0; j < cfg->line_outs; j++) {
384                         if (! assigned[j]) {
385                                 spec->dac_nids[i] = j + 0x03;
386                                 assigned[j] = 1;
387                                 break;
388                         }
389                 }
390         }
391         spec->num_dacs = cfg->line_outs;
392         return 0;
393 }
394
395 /* create multi_init table, which is used for multichannel initialization */
396 static int cmi9880_fill_multi_init(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
397 {
398         struct cmi_spec *spec = codec->spec;
399         hda_nid_t nid;
400         int i, j, k, len;
401
402         /* clear the table, only one c-media dac assumed here */
403         memset(spec->multi_init, 0, sizeof(spec->multi_init));
404         for (j = 0, i = 0; i < cfg->line_outs; i++) {
405                 hda_nid_t conn[4];
406                 nid = cfg->line_out_pins[i];
407                 /* set as output */
408                 spec->multi_init[j].nid = nid;
409                 spec->multi_init[j].verb = AC_VERB_SET_PIN_WIDGET_CONTROL;
410                 spec->multi_init[j].param = PIN_OUT;
411                 j++;
412                 if (nid > 0x0e) {
413                         /* set connection */
414                         spec->multi_init[j].nid = nid;
415                         spec->multi_init[j].verb = AC_VERB_SET_CONNECT_SEL;
416                         spec->multi_init[j].param = 0;
417                         /* find the index in connect list */
418                         len = snd_hda_get_connections(codec, nid, conn, 4);
419                         for (k = 0; k < len; k++)
420                                 if (conn[k] == spec->dac_nids[i]) {
421                                         spec->multi_init[j].param = k;
422                                         break;
423                                 }
424                         j++;
425                 }
426         }
427         return 0;
428 }
429
430 static int cmi9880_init(struct hda_codec *codec)
431 {
432         struct cmi_spec *spec = codec->spec;
433         if (spec->board_config == CMI_ALLOUT)
434                 snd_hda_sequence_write(codec, cmi9880_allout_init);
435         else
436                 snd_hda_sequence_write(codec, cmi9880_basic_init);
437         if (spec->board_config == CMI_AUTO)
438                 snd_hda_sequence_write(codec, spec->multi_init);
439         return 0;
440 }
441
442 #ifdef CONFIG_PM
443 /*
444  * resume
445  */
446 static int cmi9880_resume(struct hda_codec *codec)
447 {
448         struct cmi_spec *spec = codec->spec;
449
450         cmi9880_init(codec);
451         snd_hda_resume_ctls(codec, cmi9880_basic_mixer);
452         if (spec->channel_modes)
453                 snd_hda_resume_ctls(codec, cmi9880_ch_mode_mixer);
454         if (spec->multiout.dig_out_nid)
455                 snd_hda_resume_spdif_out(codec);
456         if (spec->dig_in_nid)
457                 snd_hda_resume_spdif_in(codec);
458
459         return 0;
460 }
461 #endif
462
463 /*
464  * Analog playback callbacks
465  */
466 static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
467                                      struct hda_codec *codec,
468                                      snd_pcm_substream_t *substream)
469 {
470         struct cmi_spec *spec = codec->spec;
471         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
472 }
473
474 static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
475                                         struct hda_codec *codec,
476                                         unsigned int stream_tag,
477                                         unsigned int format,
478                                         snd_pcm_substream_t *substream)
479 {
480         struct cmi_spec *spec = codec->spec;
481         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
482                                                 format, substream);
483 }
484
485 static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
486                                        struct hda_codec *codec,
487                                        snd_pcm_substream_t *substream)
488 {
489         struct cmi_spec *spec = codec->spec;
490         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
491 }
492
493 /*
494  * Digital out
495  */
496 static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
497                                          struct hda_codec *codec,
498                                          snd_pcm_substream_t *substream)
499 {
500         struct cmi_spec *spec = codec->spec;
501         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
502 }
503
504 static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
505                                           struct hda_codec *codec,
506                                           snd_pcm_substream_t *substream)
507 {
508         struct cmi_spec *spec = codec->spec;
509         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
510 }
511
512 /*
513  * Analog capture
514  */
515 static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
516                                       struct hda_codec *codec,
517                                       unsigned int stream_tag,
518                                       unsigned int format,
519                                       snd_pcm_substream_t *substream)
520 {
521         struct cmi_spec *spec = codec->spec;
522
523         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
524                                    stream_tag, 0, format);
525         return 0;
526 }
527
528 static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
529                                       struct hda_codec *codec,
530                                       snd_pcm_substream_t *substream)
531 {
532         struct cmi_spec *spec = codec->spec;
533
534         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
535         return 0;
536 }
537
538
539 /*
540  */
541 static struct hda_pcm_stream cmi9880_pcm_analog_playback = {
542         .substreams = 1,
543         .channels_min = 2,
544         .channels_max = 8,
545         .nid = 0x03, /* NID to query formats and rates */
546         .ops = {
547                 .open = cmi9880_playback_pcm_open,
548                 .prepare = cmi9880_playback_pcm_prepare,
549                 .cleanup = cmi9880_playback_pcm_cleanup
550         },
551 };
552
553 static struct hda_pcm_stream cmi9880_pcm_analog_capture = {
554         .substreams = 2,
555         .channels_min = 2,
556         .channels_max = 2,
557         .nid = 0x08, /* NID to query formats and rates */
558         .ops = {
559                 .prepare = cmi9880_capture_pcm_prepare,
560                 .cleanup = cmi9880_capture_pcm_cleanup
561         },
562 };
563
564 static struct hda_pcm_stream cmi9880_pcm_digital_playback = {
565         .substreams = 1,
566         .channels_min = 2,
567         .channels_max = 2,
568         /* NID is set in cmi9880_build_pcms */
569         .ops = {
570                 .open = cmi9880_dig_playback_pcm_open,
571                 .close = cmi9880_dig_playback_pcm_close
572         },
573 };
574
575 static struct hda_pcm_stream cmi9880_pcm_digital_capture = {
576         .substreams = 1,
577         .channels_min = 2,
578         .channels_max = 2,
579         /* NID is set in cmi9880_build_pcms */
580 };
581
582 static int cmi9880_build_pcms(struct hda_codec *codec)
583 {
584         struct cmi_spec *spec = codec->spec;
585         struct hda_pcm *info = spec->pcm_rec;
586
587         codec->num_pcms = 1;
588         codec->pcm_info = info;
589
590         info->name = "CMI9880";
591         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
592         info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;
593
594         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
595                 codec->num_pcms++;
596                 info++;
597                 info->name = "CMI9880 Digital";
598                 if (spec->multiout.dig_out_nid) {
599                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
600                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
601                 }
602                 if (spec->dig_in_nid) {
603                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
604                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
605                 }
606         }
607
608         return 0;
609 }
610
611 static void cmi9880_free(struct hda_codec *codec)
612 {
613         kfree(codec->spec);
614 }
615
616 /*
617  */
618
619 static struct hda_board_config cmi9880_cfg_tbl[] = {
620         { .modelname = "minimal", .config = CMI_MINIMAL },
621         { .modelname = "min_fp", .config = CMI_MIN_FP },
622         { .modelname = "full", .config = CMI_FULL },
623         { .modelname = "full_dig", .config = CMI_FULL_DIG },
624         { .pci_subvendor = 0x1043, .pci_subdevice = 0x813d, .config = CMI_FULL_DIG }, /* ASUS P5AD2 */
625         { .modelname = "allout", .config = CMI_ALLOUT },
626         { .modelname = "auto", .config = CMI_AUTO },
627         {} /* terminator */
628 };
629
630 static struct hda_codec_ops cmi9880_patch_ops = {
631         .build_controls = cmi9880_build_controls,
632         .build_pcms = cmi9880_build_pcms,
633         .init = cmi9880_init,
634         .free = cmi9880_free,
635 #ifdef CONFIG_PM
636         .resume = cmi9880_resume,
637 #endif
638 };
639
640 static int patch_cmi9880(struct hda_codec *codec)
641 {
642         struct cmi_spec *spec;
643
644         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
645         if (spec == NULL)
646                 return -ENOMEM;
647
648         codec->spec = spec;
649         spec->board_config = snd_hda_check_board_config(codec, cmi9880_cfg_tbl);
650         if (spec->board_config < 0) {
651                 snd_printdd(KERN_INFO "hda_codec: Unknown model for CMI9880\n");
652                 spec->board_config = CMI_AUTO; /* try everything */
653         }
654
655         /* copy default DAC NIDs */
656         memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
657         spec->num_dacs = 4;
658
659         switch (spec->board_config) {
660         case CMI_MINIMAL:
661         case CMI_MIN_FP:
662                 spec->channel_modes = cmi9880_channel_modes;
663                 if (spec->board_config == CMI_MINIMAL)
664                         spec->num_channel_modes = 2;
665                 else {
666                         spec->front_panel = 1;
667                         spec->num_channel_modes = 3;
668                 }
669                 spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
670                 spec->input_mux = &cmi9880_basic_mux;
671                 break;
672         case CMI_FULL:
673         case CMI_FULL_DIG:
674                 spec->front_panel = 1;
675                 spec->multiout.max_channels = 8;
676                 spec->input_mux = &cmi9880_basic_mux;
677                 if (spec->board_config == CMI_FULL_DIG) {
678                         spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
679                         spec->dig_in_nid = CMI_DIG_IN_NID;
680                 }
681                 break;
682         case CMI_ALLOUT:
683                 spec->front_panel = 1;
684                 spec->multiout.max_channels = 8;
685                 spec->no_line_in = 1;
686                 spec->input_mux = &cmi9880_no_line_mux;
687                 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
688                 break;
689         case CMI_AUTO:
690                 {
691                 unsigned int port_e, port_f, port_g, port_h;
692                 unsigned int port_spdifi, port_spdifo;
693                 struct auto_pin_cfg cfg;
694
695                 /* collect pin default configuration */
696                 port_e = snd_hda_codec_read(codec, 0x0f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
697                 port_f = snd_hda_codec_read(codec, 0x10, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
698                 spec->front_panel = 1;
699                 if (get_defcfg_connect(port_e) == AC_JACK_PORT_NONE ||
700                     get_defcfg_connect(port_f) == AC_JACK_PORT_NONE) {
701                         port_g = snd_hda_codec_read(codec, 0x1f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
702                         port_h = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
703                         spec->channel_modes = cmi9880_channel_modes;
704                         /* no front panel */
705                         if (get_defcfg_connect(port_g) == AC_JACK_PORT_NONE ||
706                             get_defcfg_connect(port_h) == AC_JACK_PORT_NONE) {
707                                 /* no optional rear panel */
708                                 spec->board_config = CMI_MINIMAL;
709                                 spec->front_panel = 0;
710                                 spec->num_channel_modes = 2;
711                         } else {
712                                 spec->board_config = CMI_MIN_FP;
713                                 spec->num_channel_modes = 3;
714                         }
715                         spec->input_mux = &cmi9880_basic_mux;
716                         spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
717                 } else {
718                         spec->input_mux = &cmi9880_basic_mux;
719                         port_spdifi = snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
720                         port_spdifo = snd_hda_codec_read(codec, 0x12, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
721                         if (get_defcfg_connect(port_spdifo) != AC_JACK_PORT_NONE)
722                                 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
723                         if (get_defcfg_connect(port_spdifi) != AC_JACK_PORT_NONE)
724                                 spec->dig_in_nid = CMI_DIG_IN_NID;
725                         spec->multiout.max_channels = 8;
726                 }
727                 snd_hda_parse_pin_def_config(codec, &cfg);
728                 if (cfg.line_outs) {
729                         spec->multiout.max_channels = cfg.line_outs * 2;
730                         cmi9880_fill_multi_dac_nids(codec, &cfg);
731                         cmi9880_fill_multi_init(codec, &cfg);
732                 } else
733                         snd_printd("patch_cmedia: cannot detect association in defcfg\n");
734                 break;
735                 }
736         }
737
738         spec->multiout.num_dacs = spec->num_dacs;
739         spec->multiout.dac_nids = spec->dac_nids;
740
741         spec->adc_nids = cmi9880_adc_nids;
742
743         codec->patch_ops = cmi9880_patch_ops;
744
745         return 0;
746 }
747
748 /*
749  * patch entries
750  */
751 struct hda_codec_preset snd_hda_preset_cmedia[] = {
752         { .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
753         { .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
754         {} /* terminator */
755 };