]> err.no Git - linux-2.6/blob - sound/pci/hda/patch_cmedia.c
Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / 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 get_defcfg_connect(cfg) ((cfg & AC_DEFCFG_PORT_CONN) >> AC_DEFCFG_PORT_CONN_SHIFT)
369 #define get_defcfg_association(cfg) ((cfg & AC_DEFCFG_DEF_ASSOC) >> AC_DEFCFG_ASSOC_SHIFT)
370 #define get_defcfg_sequence(cfg) (cfg & AC_DEFCFG_SEQUENCE)
371
372 /* get all pin default configuration in def_conf */
373 static int cmi9880_get_pin_def_config(struct hda_codec *codec)
374 {
375         struct cmi_spec *spec = codec->spec;
376         hda_nid_t nid, nid_start;
377         int i = 0, nodes;
378
379         nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
380         for (nid = nid_start; nid < nodes + nid_start; nid++) {
381                 unsigned int wid_caps = snd_hda_param_read(codec, nid,
382                                                    AC_PAR_AUDIO_WIDGET_CAP);
383                 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
384                 /* read all default configuration for pin complex */
385                 if (wid_type == AC_WID_PIN) {
386                         spec->pin_nid[i] = nid;
387                         spec->def_conf[i] = 
388                                 snd_hda_codec_read(codec, nid, 0,
389                                         AC_VERB_GET_CONFIG_DEFAULT, 0);
390                         i++;
391                 }
392         }
393         spec->pin_def_confs = i;
394         return 0;
395 }
396
397 /* get a pin default configuration of nid in def_conf */
398 static unsigned int cmi9880_get_def_config(struct hda_codec *codec, hda_nid_t nid)
399 {
400         struct cmi_spec *spec = codec->spec;
401         int i = 0;
402
403         while (spec->pin_nid[i] != nid && i < spec->pin_def_confs)
404                 i++;
405         if (i == spec->pin_def_confs)
406                 return (unsigned int) -1;
407         else
408                 return spec->def_conf[i];
409 }
410
411 /* decide what pins to use for multichannel playback */
412 static int cmi9880_get_multich_pins(struct hda_codec *codec)
413 {
414         struct cmi_spec *spec = codec->spec;
415         int i, j, pins, seq[4];
416         int max_channel = 0;
417         unsigned int def_conf, sequence;
418         hda_nid_t nid;
419
420         memset(spec->multich_pin, 0, sizeof(spec->multich_pin));
421         for (pins = 0, i = 0; i < spec->pin_def_confs && pins < 4; i++) {
422                 def_conf = spec->def_conf[i];
423                 /* skip pin not connected */
424                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
425                         continue;
426                 /* get the sequence if association == 1 */
427                 /* the other pins have association = 0, incorrect in spec 1.0 */
428                 if (get_defcfg_association(def_conf) == 1) {
429                         sequence = get_defcfg_sequence(def_conf);
430                         seq[pins] = sequence;
431                         spec->multich_pin[pins] = spec->pin_nid[i];
432                         pins++; // ready for next slot
433                         max_channel += 2;
434                 }
435         }
436         /* sort by sequence, data collected here will be for Windows */ 
437         for (i = 0; i < pins; i++) {
438                 for (j = i + 1; j < pins; j++) {
439                         if (seq[j] < seq[i]) {
440                                 sequence = seq[j];
441                                 nid = spec->multich_pin[j];
442                                 seq[j] = seq[i];
443                                 spec->multich_pin[j] = spec->multich_pin[i];
444                                 seq[i] = sequence;
445                                 spec->multich_pin[i] = nid;
446                         }
447                 }
448         }
449         /* the pin assignment is for front, C/LFE, surround and back */
450         if (max_channel >= 6) {
451                 hda_nid_t temp;
452                 /* exchange pin of C/LFE and surround */
453                 temp = spec->multich_pin[1];
454                 spec->multich_pin[1] = spec->multich_pin[2];
455                 spec->multich_pin[2] = temp;
456         }
457         return max_channel;
458 }
459
460 /* fill in the multi_dac_nids table, which will decide
461    which audio widget to use for each channel */
462 static int cmi9880_fill_multi_dac_nids(struct hda_codec *codec)
463 {
464         struct cmi_spec *spec = codec->spec;
465         hda_nid_t nid;
466         int assigned[4];
467         int i, j;
468
469         /* clear the table, only one c-media dac assumed here */
470         memset(spec->dac_nids, 0, sizeof(spec->dac_nids));
471         memset(assigned, 0, sizeof(assigned));
472         /* check the pins we found */
473         for (i = 0; i < spec->multiout.max_channels / 2; i++) {
474                 nid = spec->multich_pin[i];
475                 /* nid 0x0b~0x0e is hardwired to audio widget 0x3~0x6 */
476                 if (nid <= 0x0e && nid >= 0x0b) {
477                         spec->dac_nids[i] = nid - 0x08;
478                         assigned[nid - 0x0b] = 1;
479                 }
480         }
481         /* left pin can be connect to any audio widget */
482         for (i = 0; i < spec->multiout.max_channels / 2; i++) {
483                 if (!assigned[i]) {
484                         /* search for an empty channel */
485                         /* I should also check the pin type */
486                         for (j = 0; j < ARRAY_SIZE(spec->dac_nids); j++)
487                                 if (! spec->dac_nids[j]) {
488                                         spec->dac_nids[j] = i + 3;
489                                         assigned[i] = 1;
490                                         break;
491                                 }
492                 }
493         }
494         return 0;
495 }
496
497 /* create multi_init table, which is used for multichannel initialization */
498 static int cmi9880_fill_multi_init(struct hda_codec *codec)
499 {
500         struct cmi_spec *spec = codec->spec;
501         hda_nid_t nid;
502         int i, j, k, len;
503
504         /* clear the table, only one c-media dac assumed here */
505         memset(spec->multi_init, 0, sizeof(spec->multi_init));
506         for (j = 0, i = 0; i < spec->multiout.max_channels / 2; i++) {
507                 hda_nid_t conn[4];
508                 nid = spec->multich_pin[i];
509                 /* set as output */
510                 spec->multi_init[j].nid = nid;
511                 spec->multi_init[j].verb = AC_VERB_SET_PIN_WIDGET_CONTROL;
512                 spec->multi_init[j].param = 0xc0;
513                 j++;
514                 /* nid 0x0f,0x10,0x1f,0x20 are needed to set connection */
515                 switch (nid) {
516                 case 0x0f:
517                 case 0x10:
518                 case 0x1f:
519                 case 0x20:
520                         /* set connection */
521                         spec->multi_init[j].nid = nid;
522                         spec->multi_init[j].verb = AC_VERB_SET_CONNECT_SEL;
523                         /* find the index in connect list */
524                         len = snd_hda_get_connections(codec, nid, conn, 4);
525                         for (k = 0; k < len; k++)
526                                 if (conn[k] == spec->dac_nids[i])
527                                         break;
528                         spec->multi_init[j].param = k < len ? k : 0;
529                         j++;
530                         break;
531                 }
532         }
533         return 0;
534 }
535
536 static int cmi9880_init(struct hda_codec *codec)
537 {
538         struct cmi_spec *spec = codec->spec;
539         if (spec->board_config == CMI_ALLOUT)
540                 snd_hda_sequence_write(codec, cmi9880_allout_init);
541         else
542                 snd_hda_sequence_write(codec, cmi9880_basic_init);
543         if (spec->board_config == CMI_AUTO)
544                 snd_hda_sequence_write(codec, spec->multi_init);
545         return 0;
546 }
547
548 #ifdef CONFIG_PM
549 /*
550  * resume
551  */
552 static int cmi9880_resume(struct hda_codec *codec)
553 {
554         struct cmi_spec *spec = codec->spec;
555
556         cmi9880_init(codec);
557         snd_hda_resume_ctls(codec, cmi9880_basic_mixer);
558         if (spec->surr_switch)
559                 snd_hda_resume_ctls(codec, cmi9880_ch_mode_mixer);
560         if (spec->multiout.dig_out_nid)
561                 snd_hda_resume_spdif_out(codec);
562         if (spec->dig_in_nid)
563                 snd_hda_resume_spdif_in(codec);
564
565         return 0;
566 }
567 #endif
568
569 /*
570  * Analog playback callbacks
571  */
572 static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
573                                      struct hda_codec *codec,
574                                      snd_pcm_substream_t *substream)
575 {
576         struct cmi_spec *spec = codec->spec;
577         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
578 }
579
580 static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
581                                         struct hda_codec *codec,
582                                         unsigned int stream_tag,
583                                         unsigned int format,
584                                         snd_pcm_substream_t *substream)
585 {
586         struct cmi_spec *spec = codec->spec;
587         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
588                                                 format, substream);
589 }
590
591 static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
592                                        struct hda_codec *codec,
593                                        snd_pcm_substream_t *substream)
594 {
595         struct cmi_spec *spec = codec->spec;
596         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
597 }
598
599 /*
600  * Digital out
601  */
602 static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
603                                          struct hda_codec *codec,
604                                          snd_pcm_substream_t *substream)
605 {
606         struct cmi_spec *spec = codec->spec;
607         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
608 }
609
610 static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
611                                           struct hda_codec *codec,
612                                           snd_pcm_substream_t *substream)
613 {
614         struct cmi_spec *spec = codec->spec;
615         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
616 }
617
618 /*
619  * Analog capture
620  */
621 static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
622                                       struct hda_codec *codec,
623                                       unsigned int stream_tag,
624                                       unsigned int format,
625                                       snd_pcm_substream_t *substream)
626 {
627         struct cmi_spec *spec = codec->spec;
628
629         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
630                                    stream_tag, 0, format);
631         return 0;
632 }
633
634 static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
635                                       struct hda_codec *codec,
636                                       snd_pcm_substream_t *substream)
637 {
638         struct cmi_spec *spec = codec->spec;
639
640         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
641         return 0;
642 }
643
644
645 /*
646  */
647 static struct hda_pcm_stream cmi9880_pcm_analog_playback = {
648         .substreams = 1,
649         .channels_min = 2,
650         .channels_max = 8,
651         .nid = 0x03, /* NID to query formats and rates */
652         .ops = {
653                 .open = cmi9880_playback_pcm_open,
654                 .prepare = cmi9880_playback_pcm_prepare,
655                 .cleanup = cmi9880_playback_pcm_cleanup
656         },
657 };
658
659 static struct hda_pcm_stream cmi9880_pcm_analog_capture = {
660         .substreams = 2,
661         .channels_min = 2,
662         .channels_max = 2,
663         .nid = 0x08, /* NID to query formats and rates */
664         .ops = {
665                 .prepare = cmi9880_capture_pcm_prepare,
666                 .cleanup = cmi9880_capture_pcm_cleanup
667         },
668 };
669
670 static struct hda_pcm_stream cmi9880_pcm_digital_playback = {
671         .substreams = 1,
672         .channels_min = 2,
673         .channels_max = 2,
674         /* NID is set in cmi9880_build_pcms */
675         .ops = {
676                 .open = cmi9880_dig_playback_pcm_open,
677                 .close = cmi9880_dig_playback_pcm_close
678         },
679 };
680
681 static struct hda_pcm_stream cmi9880_pcm_digital_capture = {
682         .substreams = 1,
683         .channels_min = 2,
684         .channels_max = 2,
685         /* NID is set in cmi9880_build_pcms */
686 };
687
688 static int cmi9880_build_pcms(struct hda_codec *codec)
689 {
690         struct cmi_spec *spec = codec->spec;
691         struct hda_pcm *info = spec->pcm_rec;
692
693         codec->num_pcms = 1;
694         codec->pcm_info = info;
695
696         info->name = "CMI9880";
697         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
698         info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;
699
700         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
701                 codec->num_pcms++;
702                 info++;
703                 info->name = "CMI9880 Digital";
704                 if (spec->multiout.dig_out_nid) {
705                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
706                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
707                 }
708                 if (spec->dig_in_nid) {
709                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
710                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
711                 }
712         }
713
714         return 0;
715 }
716
717 static void cmi9880_free(struct hda_codec *codec)
718 {
719         kfree(codec->spec);
720 }
721
722 /*
723  */
724
725 static struct hda_board_config cmi9880_cfg_tbl[] = {
726         { .modelname = "minimal", .config = CMI_MINIMAL },
727         { .modelname = "min_fp", .config = CMI_MIN_FP },
728         { .modelname = "full", .config = CMI_FULL },
729         { .modelname = "full_dig", .config = CMI_FULL_DIG },
730         { .modelname = "allout", .config = CMI_ALLOUT },
731         { .modelname = "auto", .config = CMI_AUTO },
732         {} /* terminator */
733 };
734
735 static struct hda_codec_ops cmi9880_patch_ops = {
736         .build_controls = cmi9880_build_controls,
737         .build_pcms = cmi9880_build_pcms,
738         .init = cmi9880_init,
739         .free = cmi9880_free,
740 #ifdef CONFIG_PM
741         .resume = cmi9880_resume,
742 #endif
743 };
744
745 static int patch_cmi9880(struct hda_codec *codec)
746 {
747         struct cmi_spec *spec;
748
749         spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
750         if (spec == NULL)
751                 return -ENOMEM;
752
753         codec->spec = spec;
754         spec->board_config = snd_hda_check_board_config(codec, cmi9880_cfg_tbl);
755         if (spec->board_config < 0) {
756                 snd_printdd(KERN_INFO "hda_codec: Unknown model for CMI9880\n");
757                 spec->board_config = CMI_AUTO; /* try everything */
758         }
759
760         /* copy default DAC NIDs */
761         memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
762
763         switch (spec->board_config) {
764         case CMI_MINIMAL:
765         case CMI_MIN_FP:
766                 spec->surr_switch = 1;
767                 if (spec->board_config == CMI_MINIMAL)
768                         spec->num_ch_modes = 2;
769                 else {
770                         spec->front_panel = 1;
771                         spec->num_ch_modes = 3;
772                 }
773                 spec->channel_modes = cmi9880_channel_modes;
774                 spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
775                 spec->input_mux = &cmi9880_basic_mux;
776                 break;
777         case CMI_FULL:
778         case CMI_FULL_DIG:
779                 spec->front_panel = 1;
780                 spec->multiout.max_channels = 8;
781                 spec->input_mux = &cmi9880_basic_mux;
782                 if (spec->board_config == CMI_FULL_DIG) {
783                         spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
784                         spec->dig_in_nid = CMI_DIG_IN_NID;
785                 }
786                 break;
787         case CMI_ALLOUT:
788                 spec->front_panel = 1;
789                 spec->multiout.max_channels = 8;
790                 spec->no_line_in = 1;
791                 spec->input_mux = &cmi9880_no_line_mux;
792                 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
793                 break;
794         case CMI_AUTO:
795                 {
796                 unsigned int port_e, port_f, port_g, port_h;
797                 unsigned int port_spdifi, port_spdifo;
798                 int max_channels;
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                         }
822                         spec->channel_modes = cmi9880_channel_modes;
823                         spec->input_mux = &cmi9880_basic_mux;
824                         spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
825                 } else {
826                         spec->input_mux = &cmi9880_basic_mux;
827                         if (get_defcfg_connect(port_spdifo) != AC_JACK_PORT_NONE)
828                                 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
829                         if (get_defcfg_connect(port_spdifi) != AC_JACK_PORT_NONE)
830                                 spec->dig_in_nid = CMI_DIG_IN_NID;
831                         spec->multiout.max_channels = 8;
832                 }
833                 max_channels = cmi9880_get_multich_pins(codec);
834                 if (max_channels > 0) {
835                         spec->multiout.max_channels = max_channels;
836                         cmi9880_fill_multi_dac_nids(codec);
837                         cmi9880_fill_multi_init(codec);
838                 } else
839                         snd_printd("patch_cmedia: cannot detect association in defcfg\n");
840                 break;
841                 }
842         }
843
844         spec->multiout.num_dacs = 4;
845         spec->multiout.dac_nids = spec->dac_nids;
846
847         spec->adc_nids = cmi9880_adc_nids;
848
849         codec->patch_ops = cmi9880_patch_ops;
850
851         return 0;
852 }
853
854 /*
855  * patch entries
856  */
857 struct hda_codec_preset snd_hda_preset_cmedia[] = {
858         { .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
859         { .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
860         {} /* terminator */
861 };