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