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