Merge git://git.infradead.org/mtd-2.6
[pandora-kernel.git] / sound / pci / hda / patch_cirrus.c
1 /*
2  * HD audio interface patch for Cirrus Logic CS420x chip
3  *
4  * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
5  *
6  *  This driver is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This driver is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/pci.h>
25 #include <linux/module.h>
26 #include <sound/core.h>
27 #include "hda_codec.h"
28 #include "hda_local.h"
29 #include <sound/tlv.h>
30
31 /*
32  */
33
34 struct cs_spec {
35         int board_config;
36         struct auto_pin_cfg autocfg;
37         struct hda_multi_out multiout;
38         struct snd_kcontrol *vmaster_sw;
39         struct snd_kcontrol *vmaster_vol;
40
41         hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
42         hda_nid_t slave_dig_outs[2];
43
44         unsigned int input_idx[AUTO_PIN_LAST];
45         unsigned int capsrc_idx[AUTO_PIN_LAST];
46         hda_nid_t adc_nid[AUTO_PIN_LAST];
47         unsigned int adc_idx[AUTO_PIN_LAST];
48         unsigned int num_inputs;
49         unsigned int cur_input;
50         unsigned int automic_idx;
51         hda_nid_t cur_adc;
52         unsigned int cur_adc_stream_tag;
53         unsigned int cur_adc_format;
54         hda_nid_t dig_in;
55
56         const struct hda_bind_ctls *capture_bind[2];
57
58         unsigned int gpio_mask;
59         unsigned int gpio_dir;
60         unsigned int gpio_data;
61
62         struct hda_pcm pcm_rec[2];      /* PCM information */
63
64         unsigned int hp_detect:1;
65         unsigned int mic_detect:1;
66         /* CS421x */
67         unsigned int spdif_detect:1;
68         unsigned int sense_b:1;
69         hda_nid_t vendor_nid;
70         struct hda_input_mux input_mux;
71         unsigned int last_input;
72 };
73
74 /* available models with CS420x */
75 enum {
76         CS420X_MBP53,
77         CS420X_MBP55,
78         CS420X_IMAC27,
79         CS420X_AUTO,
80         CS420X_MODELS
81 };
82
83 /* CS421x boards */
84 enum {
85         CS421X_CDB4210,
86         CS421X_MODELS
87 };
88
89 /* Vendor-specific processing widget */
90 #define CS420X_VENDOR_NID       0x11
91 #define CS_DIG_OUT1_PIN_NID     0x10
92 #define CS_DIG_OUT2_PIN_NID     0x15
93 #define CS_DMIC1_PIN_NID        0x12
94 #define CS_DMIC2_PIN_NID        0x0e
95
96 /* coef indices */
97 #define IDX_SPDIF_STAT          0x0000
98 #define IDX_SPDIF_CTL           0x0001
99 #define IDX_ADC_CFG             0x0002
100 /* SZC bitmask, 4 modes below:
101  * 0 = immediate,
102  * 1 = digital immediate, analog zero-cross
103  * 2 = digtail & analog soft-ramp
104  * 3 = digital soft-ramp, analog zero-cross
105  */
106 #define   CS_COEF_ADC_SZC_MASK          (3 << 0)
107 #define   CS_COEF_ADC_MIC_SZC_MODE      (3 << 0) /* SZC setup for mic */
108 #define   CS_COEF_ADC_LI_SZC_MODE       (3 << 0) /* SZC setup for line-in */
109 /* PGA mode: 0 = differential, 1 = signle-ended */
110 #define   CS_COEF_ADC_MIC_PGA_MODE      (1 << 5) /* PGA setup for mic */
111 #define   CS_COEF_ADC_LI_PGA_MODE       (1 << 6) /* PGA setup for line-in */
112 #define IDX_DAC_CFG             0x0003
113 /* SZC bitmask, 4 modes below:
114  * 0 = Immediate
115  * 1 = zero-cross
116  * 2 = soft-ramp
117  * 3 = soft-ramp on zero-cross
118  */
119 #define   CS_COEF_DAC_HP_SZC_MODE       (3 << 0) /* nid 0x02 */
120 #define   CS_COEF_DAC_LO_SZC_MODE       (3 << 2) /* nid 0x03 */
121 #define   CS_COEF_DAC_SPK_SZC_MODE      (3 << 4) /* nid 0x04 */
122
123 #define IDX_BEEP_CFG            0x0004
124 /* 0x0008 - test reg key */
125 /* 0x0009 - 0x0014 -> 12 test regs */
126 /* 0x0015 - visibility reg */
127
128 /*
129  * Cirrus Logic CS4210
130  *
131  * 1 DAC => HP(sense) / Speakers,
132  * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
133  * 1 SPDIF OUT => SPDIF Trasmitter(sense)
134 */
135 #define CS4210_DAC_NID          0x02
136 #define CS4210_ADC_NID          0x03
137 #define CS421X_VENDOR_NID       0x0B
138 #define CS421X_DMIC_PIN_NID     0x09 /* Port E */
139 #define CS421X_SPDIF_PIN_NID    0x0A /* Port H */
140
141 #define CS421X_IDX_DEV_CFG      0x01
142 #define CS421X_IDX_ADC_CFG      0x02
143 #define CS421X_IDX_DAC_CFG      0x03
144 #define CS421X_IDX_SPK_CTL      0x04
145
146 #define SPDIF_EVENT             0x04
147
148 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
149 {
150         struct cs_spec *spec = codec->spec;
151         snd_hda_codec_write(codec, spec->vendor_nid, 0,
152                             AC_VERB_SET_COEF_INDEX, idx);
153         return snd_hda_codec_read(codec, spec->vendor_nid, 0,
154                                   AC_VERB_GET_PROC_COEF, 0);
155 }
156
157 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
158                                       unsigned int coef)
159 {
160         struct cs_spec *spec = codec->spec;
161         snd_hda_codec_write(codec, spec->vendor_nid, 0,
162                             AC_VERB_SET_COEF_INDEX, idx);
163         snd_hda_codec_write(codec, spec->vendor_nid, 0,
164                             AC_VERB_SET_PROC_COEF, coef);
165 }
166
167
168 #define HP_EVENT        1
169 #define MIC_EVENT       2
170
171 /*
172  * PCM callbacks
173  */
174 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
175                                 struct hda_codec *codec,
176                                 struct snd_pcm_substream *substream)
177 {
178         struct cs_spec *spec = codec->spec;
179         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
180                                              hinfo);
181 }
182
183 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
184                                    struct hda_codec *codec,
185                                    unsigned int stream_tag,
186                                    unsigned int format,
187                                    struct snd_pcm_substream *substream)
188 {
189         struct cs_spec *spec = codec->spec;
190         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
191                                                 stream_tag, format, substream);
192 }
193
194 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
195                                    struct hda_codec *codec,
196                                    struct snd_pcm_substream *substream)
197 {
198         struct cs_spec *spec = codec->spec;
199         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
200 }
201
202 /*
203  * Digital out
204  */
205 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
206                                     struct hda_codec *codec,
207                                     struct snd_pcm_substream *substream)
208 {
209         struct cs_spec *spec = codec->spec;
210         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
211 }
212
213 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
214                                      struct hda_codec *codec,
215                                      struct snd_pcm_substream *substream)
216 {
217         struct cs_spec *spec = codec->spec;
218         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
219 }
220
221 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
222                                        struct hda_codec *codec,
223                                        unsigned int stream_tag,
224                                        unsigned int format,
225                                        struct snd_pcm_substream *substream)
226 {
227         struct cs_spec *spec = codec->spec;
228         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
229                                              format, substream);
230 }
231
232 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
233                                        struct hda_codec *codec,
234                                        struct snd_pcm_substream *substream)
235 {
236         struct cs_spec *spec = codec->spec;
237         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
238 }
239
240 /*
241  * Analog capture
242  */
243 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
244                                   struct hda_codec *codec,
245                                   unsigned int stream_tag,
246                                   unsigned int format,
247                                   struct snd_pcm_substream *substream)
248 {
249         struct cs_spec *spec = codec->spec;
250         spec->cur_adc = spec->adc_nid[spec->cur_input];
251         spec->cur_adc_stream_tag = stream_tag;
252         spec->cur_adc_format = format;
253         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
254         return 0;
255 }
256
257 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
258                                   struct hda_codec *codec,
259                                   struct snd_pcm_substream *substream)
260 {
261         struct cs_spec *spec = codec->spec;
262         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
263         spec->cur_adc = 0;
264         return 0;
265 }
266
267 /*
268  */
269 static const struct hda_pcm_stream cs_pcm_analog_playback = {
270         .substreams = 1,
271         .channels_min = 2,
272         .channels_max = 2,
273         .ops = {
274                 .open = cs_playback_pcm_open,
275                 .prepare = cs_playback_pcm_prepare,
276                 .cleanup = cs_playback_pcm_cleanup
277         },
278 };
279
280 static const struct hda_pcm_stream cs_pcm_analog_capture = {
281         .substreams = 1,
282         .channels_min = 2,
283         .channels_max = 2,
284         .ops = {
285                 .prepare = cs_capture_pcm_prepare,
286                 .cleanup = cs_capture_pcm_cleanup
287         },
288 };
289
290 static const struct hda_pcm_stream cs_pcm_digital_playback = {
291         .substreams = 1,
292         .channels_min = 2,
293         .channels_max = 2,
294         .ops = {
295                 .open = cs_dig_playback_pcm_open,
296                 .close = cs_dig_playback_pcm_close,
297                 .prepare = cs_dig_playback_pcm_prepare,
298                 .cleanup = cs_dig_playback_pcm_cleanup
299         },
300 };
301
302 static const struct hda_pcm_stream cs_pcm_digital_capture = {
303         .substreams = 1,
304         .channels_min = 2,
305         .channels_max = 2,
306 };
307
308 static int cs_build_pcms(struct hda_codec *codec)
309 {
310         struct cs_spec *spec = codec->spec;
311         struct hda_pcm *info = spec->pcm_rec;
312
313         codec->pcm_info = info;
314         codec->num_pcms = 0;
315
316         info->name = "Cirrus Analog";
317         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
318         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
319         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
320                 spec->multiout.max_channels;
321         info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
322         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
323                 spec->adc_nid[spec->cur_input];
324         codec->num_pcms++;
325
326         if (!spec->multiout.dig_out_nid && !spec->dig_in)
327                 return 0;
328
329         info++;
330         info->name = "Cirrus Digital";
331         info->pcm_type = spec->autocfg.dig_out_type[0];
332         if (!info->pcm_type)
333                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
334         if (spec->multiout.dig_out_nid) {
335                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
336                         cs_pcm_digital_playback;
337                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
338                         spec->multiout.dig_out_nid;
339         }
340         if (spec->dig_in) {
341                 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
342                         cs_pcm_digital_capture;
343                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
344         }
345         codec->num_pcms++;
346
347         return 0;
348 }
349
350 /*
351  * parse codec topology
352  */
353
354 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
355 {
356         hda_nid_t dac;
357         if (!pin)
358                 return 0;
359         if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
360                 return 0;
361         return dac;
362 }
363
364 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
365 {
366         struct cs_spec *spec = codec->spec;
367         struct auto_pin_cfg *cfg = &spec->autocfg;
368         hda_nid_t pin = cfg->inputs[idx].pin;
369         unsigned int val;
370         if (!is_jack_detectable(codec, pin))
371                 return 0;
372         val = snd_hda_codec_get_pincfg(codec, pin);
373         return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
374 }
375
376 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
377                          unsigned int *idxp)
378 {
379         int i, idx;
380         hda_nid_t nid;
381
382         nid = codec->start_nid;
383         for (i = 0; i < codec->num_nodes; i++, nid++) {
384                 unsigned int type;
385                 type = get_wcaps_type(get_wcaps(codec, nid));
386                 if (type != AC_WID_AUD_IN)
387                         continue;
388                 idx = snd_hda_get_conn_index(codec, nid, pin, false);
389                 if (idx >= 0) {
390                         *idxp = idx;
391                         return nid;
392                 }
393         }
394         return 0;
395 }
396
397 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
398 {
399         unsigned int val;
400         val = snd_hda_codec_get_pincfg(codec, nid);
401         return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
402 }
403
404 static int parse_output(struct hda_codec *codec)
405 {
406         struct cs_spec *spec = codec->spec;
407         struct auto_pin_cfg *cfg = &spec->autocfg;
408         int i, extra_nids;
409         hda_nid_t dac;
410
411         for (i = 0; i < cfg->line_outs; i++) {
412                 dac = get_dac(codec, cfg->line_out_pins[i]);
413                 if (!dac)
414                         break;
415                 spec->dac_nid[i] = dac;
416         }
417         spec->multiout.num_dacs = i;
418         spec->multiout.dac_nids = spec->dac_nid;
419         spec->multiout.max_channels = i * 2;
420
421         /* add HP and speakers */
422         extra_nids = 0;
423         for (i = 0; i < cfg->hp_outs; i++) {
424                 dac = get_dac(codec, cfg->hp_pins[i]);
425                 if (!dac)
426                         break;
427                 if (!i)
428                         spec->multiout.hp_nid = dac;
429                 else
430                         spec->multiout.extra_out_nid[extra_nids++] = dac;
431         }
432         for (i = 0; i < cfg->speaker_outs; i++) {
433                 dac = get_dac(codec, cfg->speaker_pins[i]);
434                 if (!dac)
435                         break;
436                 spec->multiout.extra_out_nid[extra_nids++] = dac;
437         }
438
439         if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
440                 cfg->speaker_outs = cfg->line_outs;
441                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
442                        sizeof(cfg->speaker_pins));
443                 cfg->line_outs = 0;
444         }
445
446         return 0;
447 }
448
449 static int parse_input(struct hda_codec *codec)
450 {
451         struct cs_spec *spec = codec->spec;
452         struct auto_pin_cfg *cfg = &spec->autocfg;
453         int i;
454
455         for (i = 0; i < cfg->num_inputs; i++) {
456                 hda_nid_t pin = cfg->inputs[i].pin;
457                 spec->input_idx[spec->num_inputs] = i;
458                 spec->capsrc_idx[i] = spec->num_inputs++;
459                 spec->cur_input = i;
460                 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
461         }
462         if (!spec->num_inputs)
463                 return 0;
464
465         /* check whether the automatic mic switch is available */
466         if (spec->num_inputs == 2 &&
467             cfg->inputs[0].type == AUTO_PIN_MIC &&
468             cfg->inputs[1].type == AUTO_PIN_MIC) {
469                 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
470                         if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
471                                 spec->mic_detect = 1;
472                                 spec->automic_idx = 0;
473                         }
474                 } else {
475                         if (is_ext_mic(codec, cfg->inputs[1].pin)) {
476                                 spec->mic_detect = 1;
477                                 spec->automic_idx = 1;
478                         }
479                 }
480         }
481         return 0;
482 }
483
484
485 static int parse_digital_output(struct hda_codec *codec)
486 {
487         struct cs_spec *spec = codec->spec;
488         struct auto_pin_cfg *cfg = &spec->autocfg;
489         hda_nid_t nid;
490
491         if (!cfg->dig_outs)
492                 return 0;
493         if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
494                 return 0;
495         spec->multiout.dig_out_nid = nid;
496         spec->multiout.share_spdif = 1;
497         if (cfg->dig_outs > 1 &&
498             snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
499                 spec->slave_dig_outs[0] = nid;
500                 codec->slave_dig_outs = spec->slave_dig_outs;
501         }
502         return 0;
503 }
504
505 static int parse_digital_input(struct hda_codec *codec)
506 {
507         struct cs_spec *spec = codec->spec;
508         struct auto_pin_cfg *cfg = &spec->autocfg;
509         int idx;
510
511         if (cfg->dig_in_pin)
512                 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
513         return 0;
514 }
515
516 /*
517  * create mixer controls
518  */
519
520 static const char * const dir_sfx[2] = { "Playback", "Capture" };
521
522 static int add_mute(struct hda_codec *codec, const char *name, int index,
523                     unsigned int pval, int dir, struct snd_kcontrol **kctlp)
524 {
525         char tmp[44];
526         struct snd_kcontrol_new knew =
527                 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
528         knew.private_value = pval;
529         snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
530         *kctlp = snd_ctl_new1(&knew, codec);
531         (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
532         return snd_hda_ctl_add(codec, 0, *kctlp);
533 }
534
535 static int add_volume(struct hda_codec *codec, const char *name,
536                       int index, unsigned int pval, int dir,
537                       struct snd_kcontrol **kctlp)
538 {
539         char tmp[44];
540         struct snd_kcontrol_new knew =
541                 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
542         knew.private_value = pval;
543         snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
544         *kctlp = snd_ctl_new1(&knew, codec);
545         (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
546         return snd_hda_ctl_add(codec, 0, *kctlp);
547 }
548
549 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
550 {
551         unsigned int caps;
552
553         /* set the upper-limit for mixer amp to 0dB */
554         caps = query_amp_caps(codec, dac, HDA_OUTPUT);
555         caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
556         caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
557                 << AC_AMPCAP_NUM_STEPS_SHIFT;
558         snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
559 }
560
561 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
562 {
563         struct cs_spec *spec = codec->spec;
564         unsigned int tlv[4];
565         int err;
566
567         spec->vmaster_sw =
568                 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
569         err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
570         if (err < 0)
571                 return err;
572
573         snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
574         spec->vmaster_vol =
575                 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
576         err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
577         if (err < 0)
578                 return err;
579         return 0;
580 }
581
582 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
583                       int num_ctls, int type)
584 {
585         struct cs_spec *spec = codec->spec;
586         const char *name;
587         int err, index;
588         struct snd_kcontrol *kctl;
589         static const char * const speakers[] = {
590                 "Front Speaker", "Surround Speaker", "Bass Speaker"
591         };
592         static const char * const line_outs[] = {
593                 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
594         };
595
596         fix_volume_caps(codec, dac);
597         if (!spec->vmaster_sw) {
598                 err = add_vmaster(codec, dac);
599                 if (err < 0)
600                         return err;
601         }
602
603         index = 0;
604         switch (type) {
605         case AUTO_PIN_HP_OUT:
606                 name = "Headphone";
607                 index = idx;
608                 break;
609         case AUTO_PIN_SPEAKER_OUT:
610                 if (num_ctls > 1)
611                         name = speakers[idx];
612                 else
613                         name = "Speaker";
614                 break;
615         default:
616                 if (num_ctls > 1)
617                         name = line_outs[idx];
618                 else
619                         name = "Line-Out";
620                 break;
621         }
622
623         err = add_mute(codec, name, index,
624                        HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
625         if (err < 0)
626                 return err;
627         err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
628         if (err < 0)
629                 return err;
630
631         err = add_volume(codec, name, index,
632                          HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
633         if (err < 0)
634                 return err;
635         err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
636         if (err < 0)
637                 return err;
638
639         return 0;
640 }               
641
642 static int build_output(struct hda_codec *codec)
643 {
644         struct cs_spec *spec = codec->spec;
645         struct auto_pin_cfg *cfg = &spec->autocfg;
646         int i, err;
647
648         for (i = 0; i < cfg->line_outs; i++) {
649                 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
650                                  i, cfg->line_outs, cfg->line_out_type);
651                 if (err < 0)
652                         return err;
653         }
654         for (i = 0; i < cfg->hp_outs; i++) {
655                 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
656                                  i, cfg->hp_outs, AUTO_PIN_HP_OUT);
657                 if (err < 0)
658                         return err;
659         }
660         for (i = 0; i < cfg->speaker_outs; i++) {
661                 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
662                                  i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
663                 if (err < 0)
664                         return err;
665         }
666         return 0;
667 }
668
669 /*
670  */
671
672 static const struct snd_kcontrol_new cs_capture_ctls[] = {
673         HDA_BIND_SW("Capture Switch", 0),
674         HDA_BIND_VOL("Capture Volume", 0),
675 };
676
677 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
678                             int force)
679 {
680         struct cs_spec *spec = codec->spec;
681         
682         if (spec->cur_input == idx && !force)
683                 return 0;
684         if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
685                 /* stream is running, let's swap the current ADC */
686                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
687                 spec->cur_adc = spec->adc_nid[idx];
688                 snd_hda_codec_setup_stream(codec, spec->cur_adc,
689                                            spec->cur_adc_stream_tag, 0,
690                                            spec->cur_adc_format);
691         }
692         snd_hda_codec_write(codec, spec->cur_adc, 0,
693                             AC_VERB_SET_CONNECT_SEL,
694                             spec->adc_idx[idx]);
695         spec->cur_input = idx;
696         return 1;
697 }
698
699 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
700                                   struct snd_ctl_elem_info *uinfo)
701 {
702         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
703         struct cs_spec *spec = codec->spec;
704         struct auto_pin_cfg *cfg = &spec->autocfg;
705         unsigned int idx;
706
707         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
708         uinfo->count = 1;
709         uinfo->value.enumerated.items = spec->num_inputs;
710         if (uinfo->value.enumerated.item >= spec->num_inputs)
711                 uinfo->value.enumerated.item = spec->num_inputs - 1;
712         idx = spec->input_idx[uinfo->value.enumerated.item];
713         strcpy(uinfo->value.enumerated.name,
714                hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
715         return 0;
716 }
717
718 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
719                                  struct snd_ctl_elem_value *ucontrol)
720 {
721         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
722         struct cs_spec *spec = codec->spec;
723         ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
724         return 0;
725 }
726
727 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
728                                  struct snd_ctl_elem_value *ucontrol)
729 {
730         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
731         struct cs_spec *spec = codec->spec;
732         unsigned int idx = ucontrol->value.enumerated.item[0];
733
734         if (idx >= spec->num_inputs)
735                 return -EINVAL;
736         idx = spec->input_idx[idx];
737         return change_cur_input(codec, idx, 0);
738 }
739
740 static const struct snd_kcontrol_new cs_capture_source = {
741         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
742         .name = "Capture Source",
743         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
744         .info = cs_capture_source_info,
745         .get = cs_capture_source_get,
746         .put = cs_capture_source_put,
747 };
748
749 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
750                                                struct hda_ctl_ops *ops)
751 {
752         struct cs_spec *spec = codec->spec;
753         struct hda_bind_ctls *bind;
754         int i, n;
755
756         bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
757                        GFP_KERNEL);
758         if (!bind)
759                 return NULL;
760         bind->ops = ops;
761         n = 0;
762         for (i = 0; i < AUTO_PIN_LAST; i++) {
763                 if (!spec->adc_nid[i])
764                         continue;
765                 bind->values[n++] =
766                         HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
767                                             spec->adc_idx[i], HDA_INPUT);
768         }
769         return bind;
770 }
771
772 /* add a (input-boost) volume control to the given input pin */
773 static int add_input_volume_control(struct hda_codec *codec,
774                                     struct auto_pin_cfg *cfg,
775                                     int item)
776 {
777         hda_nid_t pin = cfg->inputs[item].pin;
778         u32 caps;
779         const char *label;
780         struct snd_kcontrol *kctl;
781                 
782         if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
783                 return 0;
784         caps = query_amp_caps(codec, pin, HDA_INPUT);
785         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
786         if (caps <= 1)
787                 return 0;
788         label = hda_get_autocfg_input_label(codec, cfg, item);
789         return add_volume(codec, label, 0,
790                           HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
791 }
792
793 static int build_input(struct hda_codec *codec)
794 {
795         struct cs_spec *spec = codec->spec;
796         int i, err;
797
798         if (!spec->num_inputs)
799                 return 0;
800
801         /* make bind-capture */
802         spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
803         spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
804         for (i = 0; i < 2; i++) {
805                 struct snd_kcontrol *kctl;
806                 int n;
807                 if (!spec->capture_bind[i])
808                         return -ENOMEM;
809                 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
810                 if (!kctl)
811                         return -ENOMEM;
812                 kctl->private_value = (long)spec->capture_bind[i];
813                 err = snd_hda_ctl_add(codec, 0, kctl);
814                 if (err < 0)
815                         return err;
816                 for (n = 0; n < AUTO_PIN_LAST; n++) {
817                         if (!spec->adc_nid[n])
818                                 continue;
819                         err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
820                         if (err < 0)
821                                 return err;
822                 }
823         }
824         
825         if (spec->num_inputs > 1 && !spec->mic_detect) {
826                 err = snd_hda_ctl_add(codec, 0,
827                                       snd_ctl_new1(&cs_capture_source, codec));
828                 if (err < 0)
829                         return err;
830         }
831
832         for (i = 0; i < spec->num_inputs; i++) {
833                 err = add_input_volume_control(codec, &spec->autocfg, i);
834                 if (err < 0)
835                         return err;
836         }
837
838         return 0;
839 }
840
841 /*
842  */
843
844 static int build_digital_output(struct hda_codec *codec)
845 {
846         struct cs_spec *spec = codec->spec;
847         int err;
848
849         if (!spec->multiout.dig_out_nid)
850                 return 0;
851
852         err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
853                                             spec->multiout.dig_out_nid);
854         if (err < 0)
855                 return err;
856         err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
857         if (err < 0)
858                 return err;
859         return 0;
860 }
861
862 static int build_digital_input(struct hda_codec *codec)
863 {
864         struct cs_spec *spec = codec->spec;
865         if (spec->dig_in)
866                 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
867         return 0;
868 }
869
870 /*
871  * auto-mute and auto-mic switching
872  * CS421x auto-output redirecting
873  * HP/SPK/SPDIF
874  */
875
876 static void cs_automute(struct hda_codec *codec)
877 {
878         struct cs_spec *spec = codec->spec;
879         struct auto_pin_cfg *cfg = &spec->autocfg;
880         unsigned int hp_present;
881         unsigned int spdif_present;
882         hda_nid_t nid;
883         int i;
884
885         spdif_present = 0;
886         if (cfg->dig_outs) {
887                 nid = cfg->dig_out_pins[0];
888                 if (is_jack_detectable(codec, nid)) {
889                         /*
890                         TODO: SPDIF output redirect when SENSE_B is enabled.
891                         Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
892                         assumed.
893                         */
894                         if (snd_hda_jack_detect(codec, nid)
895                                 /* && spec->sense_b */)
896                                 spdif_present = 1;
897                 }
898         }
899
900         hp_present = 0;
901         for (i = 0; i < cfg->hp_outs; i++) {
902                 nid = cfg->hp_pins[i];
903                 if (!is_jack_detectable(codec, nid))
904                         continue;
905                 hp_present = snd_hda_jack_detect(codec, nid);
906                 if (hp_present)
907                         break;
908         }
909
910         /* mute speakers if spdif or hp jack is plugged in */
911         for (i = 0; i < cfg->speaker_outs; i++) {
912                 nid = cfg->speaker_pins[i];
913                 snd_hda_codec_write(codec, nid, 0,
914                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
915                                     hp_present ? 0 : PIN_OUT);
916                 /* detect on spdif is specific to CS421x */
917                 if (spec->vendor_nid == CS421X_VENDOR_NID) {
918                         snd_hda_codec_write(codec, nid, 0,
919                                         AC_VERB_SET_PIN_WIDGET_CONTROL,
920                                         spdif_present ? 0 : PIN_OUT);
921                 }
922         }
923         if (spec->board_config == CS420X_MBP53 ||
924             spec->board_config == CS420X_MBP55 ||
925             spec->board_config == CS420X_IMAC27) {
926                 unsigned int gpio = hp_present ? 0x02 : 0x08;
927                 snd_hda_codec_write(codec, 0x01, 0,
928                                     AC_VERB_SET_GPIO_DATA, gpio);
929         }
930
931         /* specific to CS421x */
932         if (spec->vendor_nid == CS421X_VENDOR_NID) {
933                 /* mute HPs if spdif jack (SENSE_B) is present */
934                 for (i = 0; i < cfg->hp_outs; i++) {
935                         nid = cfg->hp_pins[i];
936                         snd_hda_codec_write(codec, nid, 0,
937                                 AC_VERB_SET_PIN_WIDGET_CONTROL,
938                                 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
939                 }
940
941                 /* SPDIF TX on/off */
942                 if (cfg->dig_outs) {
943                         nid = cfg->dig_out_pins[0];
944                         snd_hda_codec_write(codec, nid, 0,
945                                 AC_VERB_SET_PIN_WIDGET_CONTROL,
946                                 spdif_present ? PIN_OUT : 0);
947
948                 }
949                 /* Update board GPIOs if neccessary ... */
950         }
951 }
952
953 /*
954  * Auto-input redirect for CS421x
955  * Switch max 3 inputs of a single ADC (nid 3)
956 */
957
958 static void cs_automic(struct hda_codec *codec)
959 {
960         struct cs_spec *spec = codec->spec;
961         struct auto_pin_cfg *cfg = &spec->autocfg;
962         hda_nid_t nid;
963         unsigned int present;
964
965         nid = cfg->inputs[spec->automic_idx].pin;
966         present = snd_hda_jack_detect(codec, nid);
967
968         /* specific to CS421x, single ADC */
969         if (spec->vendor_nid == CS421X_VENDOR_NID) {
970                 if (present) {
971                         spec->last_input = spec->cur_input;
972                         spec->cur_input = spec->automic_idx;
973                 } else  {
974                         spec->cur_input = spec->last_input;
975                 }
976
977                 snd_hda_codec_write_cache(codec, spec->cur_adc, 0,
978                                         AC_VERB_SET_CONNECT_SEL,
979                                         spec->adc_idx[spec->cur_input]);
980         } else {
981                 if (present)
982                         change_cur_input(codec, spec->automic_idx, 0);
983                 else
984                         change_cur_input(codec, !spec->automic_idx, 0);
985         }
986 }
987
988 /*
989  */
990
991 static void init_output(struct hda_codec *codec)
992 {
993         struct cs_spec *spec = codec->spec;
994         struct auto_pin_cfg *cfg = &spec->autocfg;
995         int i;
996
997         /* mute first */
998         for (i = 0; i < spec->multiout.num_dacs; i++)
999                 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1000                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1001         if (spec->multiout.hp_nid)
1002                 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1003                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1004         for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1005                 if (!spec->multiout.extra_out_nid[i])
1006                         break;
1007                 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1008                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1009         }
1010
1011         /* set appropriate pin controls */
1012         for (i = 0; i < cfg->line_outs; i++)
1013                 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1014                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1015         /* HP */
1016         for (i = 0; i < cfg->hp_outs; i++) {
1017                 hda_nid_t nid = cfg->hp_pins[i];
1018                 snd_hda_codec_write(codec, nid, 0,
1019                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1020                 if (!cfg->speaker_outs)
1021                         continue;
1022                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1023                         snd_hda_codec_write(codec, nid, 0,
1024                                             AC_VERB_SET_UNSOLICITED_ENABLE,
1025                                             AC_USRSP_EN | HP_EVENT);
1026                         spec->hp_detect = 1;
1027                 }
1028         }
1029
1030         /* Speaker */
1031         for (i = 0; i < cfg->speaker_outs; i++)
1032                 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1033                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1034
1035         /* SPDIF is enabled on presence detect for CS421x */
1036         if (spec->hp_detect || spec->spdif_detect)
1037                 cs_automute(codec);
1038 }
1039
1040 static void init_input(struct hda_codec *codec)
1041 {
1042         struct cs_spec *spec = codec->spec;
1043         struct auto_pin_cfg *cfg = &spec->autocfg;
1044         unsigned int coef;
1045         int i;
1046
1047         for (i = 0; i < cfg->num_inputs; i++) {
1048                 unsigned int ctl;
1049                 hda_nid_t pin = cfg->inputs[i].pin;
1050                 if (!spec->adc_nid[i])
1051                         continue;
1052                 /* set appropriate pin control and mute first */
1053                 ctl = PIN_IN;
1054                 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1055                         unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1056                         caps >>= AC_PINCAP_VREF_SHIFT;
1057                         if (caps & AC_PINCAP_VREF_80)
1058                                 ctl = PIN_VREF80;
1059                 }
1060                 snd_hda_codec_write(codec, pin, 0,
1061                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1062                 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1063                                     AC_VERB_SET_AMP_GAIN_MUTE,
1064                                     AMP_IN_MUTE(spec->adc_idx[i]));
1065                 if (spec->mic_detect && spec->automic_idx == i)
1066                         snd_hda_codec_write(codec, pin, 0,
1067                                             AC_VERB_SET_UNSOLICITED_ENABLE,
1068                                             AC_USRSP_EN | MIC_EVENT);
1069         }
1070         /* specific to CS421x */
1071         if (spec->vendor_nid == CS421X_VENDOR_NID) {
1072                 if (spec->mic_detect)
1073                         cs_automic(codec);
1074                 else  {
1075                         spec->cur_adc = spec->adc_nid[spec->cur_input];
1076                         snd_hda_codec_write(codec, spec->cur_adc, 0,
1077                                         AC_VERB_SET_CONNECT_SEL,
1078                                         spec->adc_idx[spec->cur_input]);
1079                 }
1080         } else {
1081                 change_cur_input(codec, spec->cur_input, 1);
1082                 if (spec->mic_detect)
1083                         cs_automic(codec);
1084
1085                 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1086                 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1087                         coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1088                 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1089                         coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1090                                          * No effect if SPDIF_OUT2 is
1091                                          * selected in IDX_SPDIF_CTL.
1092                                         */
1093                 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1094         }
1095 }
1096
1097 static const struct hda_verb cs_coef_init_verbs[] = {
1098         {0x11, AC_VERB_SET_PROC_STATE, 1},
1099         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1100         {0x11, AC_VERB_SET_PROC_COEF,
1101          (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1102           | 0x0040 /* Mute DACs on FIFO error */
1103           | 0x1000 /* Enable DACs High Pass Filter */
1104           | 0x0400 /* Disable Coefficient Auto increment */
1105           )},
1106         /* Beep */
1107         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1108         {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1109
1110         {} /* terminator */
1111 };
1112
1113 /* Errata: CS4207 rev C0/C1/C2 Silicon
1114  *
1115  * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1116  *
1117  * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1118  * may be excessive (up to an additional 200 Î¼A), which is most easily
1119  * observed while the part is being held in reset (RESET# active low).
1120  *
1121  * Root Cause: At initial powerup of the device, the logic that drives
1122  * the clock and write enable to the S/PDIF SRC RAMs is not properly
1123  * initialized.
1124  * Certain random patterns will cause a steady leakage current in those
1125  * RAM cells. The issue will resolve once the SRCs are used (turned on).
1126  *
1127  * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1128  * blocks, which will alleviate the issue.
1129  */
1130
1131 static const struct hda_verb cs_errata_init_verbs[] = {
1132         {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1133         {0x11, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
1134
1135         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1136         {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1137         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1138         {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1139         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1140         {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1141
1142         {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1143         {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1144
1145         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1146         {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1147         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1148         {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1149         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1150         {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1151         {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1152
1153 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1154         {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1155         {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1156         /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1157 #endif
1158
1159         {} /* terminator */
1160 };
1161
1162 /* SPDIF setup */
1163 static void init_digital(struct hda_codec *codec)
1164 {
1165         unsigned int coef;
1166
1167         coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1168         coef |= 0x0008; /* Replace with mute on error */
1169         if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1170                 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1171                                  * SPDIF_OUT2 is shared with GPIO1 and
1172                                  * DMIC_SDA2.
1173                                  */
1174         cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1175 }
1176
1177 static int cs_init(struct hda_codec *codec)
1178 {
1179         struct cs_spec *spec = codec->spec;
1180
1181         /* init_verb sequence for C0/C1/C2 errata*/
1182         snd_hda_sequence_write(codec, cs_errata_init_verbs);
1183
1184         snd_hda_sequence_write(codec, cs_coef_init_verbs);
1185
1186         if (spec->gpio_mask) {
1187                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1188                                     spec->gpio_mask);
1189                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1190                                     spec->gpio_dir);
1191                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1192                                     spec->gpio_data);
1193         }
1194
1195         init_output(codec);
1196         init_input(codec);
1197         init_digital(codec);
1198         return 0;
1199 }
1200
1201 static int cs_build_controls(struct hda_codec *codec)
1202 {
1203         int err;
1204
1205         err = build_output(codec);
1206         if (err < 0)
1207                 return err;
1208         err = build_input(codec);
1209         if (err < 0)
1210                 return err;
1211         err = build_digital_output(codec);
1212         if (err < 0)
1213                 return err;
1214         err = build_digital_input(codec);
1215         if (err < 0)
1216                 return err;
1217         return cs_init(codec);
1218 }
1219
1220 static void cs_free(struct hda_codec *codec)
1221 {
1222         struct cs_spec *spec = codec->spec;
1223         kfree(spec->capture_bind[0]);
1224         kfree(spec->capture_bind[1]);
1225         kfree(codec->spec);
1226 }
1227
1228 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1229 {
1230         switch ((res >> 26) & 0x7f) {
1231         case HP_EVENT:
1232                 cs_automute(codec);
1233                 break;
1234         case MIC_EVENT:
1235                 cs_automic(codec);
1236                 break;
1237         }
1238 }
1239
1240 static const struct hda_codec_ops cs_patch_ops = {
1241         .build_controls = cs_build_controls,
1242         .build_pcms = cs_build_pcms,
1243         .init = cs_init,
1244         .free = cs_free,
1245         .unsol_event = cs_unsol_event,
1246 };
1247
1248 static int cs_parse_auto_config(struct hda_codec *codec)
1249 {
1250         struct cs_spec *spec = codec->spec;
1251         int err;
1252
1253         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1254         if (err < 0)
1255                 return err;
1256
1257         err = parse_output(codec);
1258         if (err < 0)
1259                 return err;
1260         err = parse_input(codec);
1261         if (err < 0)
1262                 return err;
1263         err = parse_digital_output(codec);
1264         if (err < 0)
1265                 return err;
1266         err = parse_digital_input(codec);
1267         if (err < 0)
1268                 return err;
1269         return 0;
1270 }
1271
1272 static const char * const cs420x_models[CS420X_MODELS] = {
1273         [CS420X_MBP53] = "mbp53",
1274         [CS420X_MBP55] = "mbp55",
1275         [CS420X_IMAC27] = "imac27",
1276         [CS420X_AUTO] = "auto",
1277 };
1278
1279
1280 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1281         SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1282         SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1283         SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1284         SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1285         SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
1286         {} /* terminator */
1287 };
1288
1289 struct cs_pincfg {
1290         hda_nid_t nid;
1291         u32 val;
1292 };
1293
1294 static const struct cs_pincfg mbp53_pincfgs[] = {
1295         { 0x09, 0x012b4050 },
1296         { 0x0a, 0x90100141 },
1297         { 0x0b, 0x90100140 },
1298         { 0x0c, 0x018b3020 },
1299         { 0x0d, 0x90a00110 },
1300         { 0x0e, 0x400000f0 },
1301         { 0x0f, 0x01cbe030 },
1302         { 0x10, 0x014be060 },
1303         { 0x12, 0x400000f0 },
1304         { 0x15, 0x400000f0 },
1305         {} /* terminator */
1306 };
1307
1308 static const struct cs_pincfg mbp55_pincfgs[] = {
1309         { 0x09, 0x012b4030 },
1310         { 0x0a, 0x90100121 },
1311         { 0x0b, 0x90100120 },
1312         { 0x0c, 0x400000f0 },
1313         { 0x0d, 0x90a00110 },
1314         { 0x0e, 0x400000f0 },
1315         { 0x0f, 0x400000f0 },
1316         { 0x10, 0x014be040 },
1317         { 0x12, 0x400000f0 },
1318         { 0x15, 0x400000f0 },
1319         {} /* terminator */
1320 };
1321
1322 static const struct cs_pincfg imac27_pincfgs[] = {
1323         { 0x09, 0x012b4050 },
1324         { 0x0a, 0x90100140 },
1325         { 0x0b, 0x90100142 },
1326         { 0x0c, 0x018b3020 },
1327         { 0x0d, 0x90a00110 },
1328         { 0x0e, 0x400000f0 },
1329         { 0x0f, 0x01cbe030 },
1330         { 0x10, 0x014be060 },
1331         { 0x12, 0x01ab9070 },
1332         { 0x15, 0x400000f0 },
1333         {} /* terminator */
1334 };
1335
1336 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1337         [CS420X_MBP53] = mbp53_pincfgs,
1338         [CS420X_MBP55] = mbp55_pincfgs,
1339         [CS420X_IMAC27] = imac27_pincfgs,
1340 };
1341
1342 static void fix_pincfg(struct hda_codec *codec, int model,
1343                        const struct cs_pincfg **pin_configs)
1344 {
1345         const struct cs_pincfg *cfg = pin_configs[model];
1346         if (!cfg)
1347                 return;
1348         for (; cfg->nid; cfg++)
1349                 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1350 }
1351
1352 static int patch_cs420x(struct hda_codec *codec)
1353 {
1354         struct cs_spec *spec;
1355         int err;
1356
1357         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1358         if (!spec)
1359                 return -ENOMEM;
1360         codec->spec = spec;
1361
1362         spec->vendor_nid = CS420X_VENDOR_NID;
1363
1364         spec->board_config =
1365                 snd_hda_check_board_config(codec, CS420X_MODELS,
1366                                            cs420x_models, cs420x_cfg_tbl);
1367         if (spec->board_config >= 0)
1368                 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1369
1370         switch (spec->board_config) {
1371         case CS420X_IMAC27:
1372         case CS420X_MBP53:
1373         case CS420X_MBP55:
1374                 /* GPIO1 = headphones */
1375                 /* GPIO3 = speakers */
1376                 spec->gpio_mask = 0x0a;
1377                 spec->gpio_dir = 0x0a;
1378                 break;
1379         }
1380
1381         err = cs_parse_auto_config(codec);
1382         if (err < 0)
1383                 goto error;
1384
1385         codec->patch_ops = cs_patch_ops;
1386
1387         return 0;
1388
1389  error:
1390         kfree(codec->spec);
1391         codec->spec = NULL;
1392         return err;
1393 }
1394
1395 /*
1396  * Cirrus Logic CS4210
1397  *
1398  * 1 DAC => HP(sense) / Speakers,
1399  * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1400  * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1401 */
1402
1403 /* CS4210 board names */
1404 static const char *cs421x_models[CS421X_MODELS] = {
1405         [CS421X_CDB4210] = "cdb4210",
1406 };
1407
1408 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1409         /* Test Intel board + CDB2410  */
1410         SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1411         {} /* terminator */
1412 };
1413
1414 /* CS4210 board pinconfigs */
1415 /* Default CS4210 (CDB4210)*/
1416 static const struct cs_pincfg cdb4210_pincfgs[] = {
1417         { 0x05, 0x0321401f },
1418         { 0x06, 0x90170010 },
1419         { 0x07, 0x03813031 },
1420         { 0x08, 0xb7a70037 },
1421         { 0x09, 0xb7a6003e },
1422         { 0x0a, 0x034510f0 },
1423         {} /* terminator */
1424 };
1425
1426 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1427         [CS421X_CDB4210] = cdb4210_pincfgs,
1428 };
1429
1430 static const struct hda_verb cs421x_coef_init_verbs[] = {
1431         {0x0B, AC_VERB_SET_PROC_STATE, 1},
1432         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1433         /*
1434             Disable Coefficient Index Auto-Increment(DAI)=1,
1435             PDREF=0
1436         */
1437         {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1438
1439         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1440         /* ADC SZCMode = Digital Soft Ramp */
1441         {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1442
1443         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1444         {0x0B, AC_VERB_SET_PROC_COEF,
1445          (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1446           | 0x0004 /* Mute DAC on FIFO error */
1447           | 0x0008 /* Enable DAC High Pass Filter */
1448           )},
1449         {} /* terminator */
1450 };
1451
1452 /* Errata: CS4210 rev A1 Silicon
1453  *
1454  * http://www.cirrus.com/en/pubs/errata/
1455  *
1456  * Description:
1457  * 1. Performance degredation is present in the ADC.
1458  * 2. Speaker output is not completely muted upon HP detect.
1459  * 3. Noise is present when clipping occurs on the amplified
1460  *    speaker outputs.
1461  *
1462  * Workaround:
1463  * The following verb sequence written to the registers during
1464  * initialization will correct the issues listed above.
1465  */
1466
1467 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1468         {0x0B, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
1469
1470         {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1471         {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1472
1473         {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1474         {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1475
1476         {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1477         {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1478
1479         {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1480         {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1481
1482         {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1483         {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1484
1485         {} /* terminator */
1486 };
1487
1488 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1489 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1490
1491 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1492                                 struct snd_ctl_elem_info *uinfo)
1493 {
1494         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1495         uinfo->count = 1;
1496         uinfo->value.integer.min = 0;
1497         uinfo->value.integer.max = 3;
1498         return 0;
1499 }
1500
1501 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1502                                 struct snd_ctl_elem_value *ucontrol)
1503 {
1504         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1505
1506         ucontrol->value.integer.value[0] =
1507                 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1508         return 0;
1509 }
1510
1511 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1512                                 struct snd_ctl_elem_value *ucontrol)
1513 {
1514         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1515
1516         unsigned int vol = ucontrol->value.integer.value[0];
1517         unsigned int coef =
1518                 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1519         unsigned int original_coef = coef;
1520
1521         coef &= ~0x0003;
1522         coef |= (vol & 0x0003);
1523         if (original_coef == coef)
1524                 return 0;
1525         else {
1526                 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1527                 return 1;
1528         }
1529 }
1530
1531 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1532
1533         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1534         .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1535                         SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1536         .name = "Speaker Boost Playback Volume",
1537         .info = cs421x_boost_vol_info,
1538         .get = cs421x_boost_vol_get,
1539         .put = cs421x_boost_vol_put,
1540         .tlv = { .p = cs421x_speaker_boost_db_scale },
1541 };
1542
1543 static void cs421x_pinmux_init(struct hda_codec *codec)
1544 {
1545         struct cs_spec *spec = codec->spec;
1546         unsigned int def_conf, coef;
1547
1548         /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1549         coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1550
1551         if (spec->gpio_mask)
1552                 coef |= 0x0008; /* B1,B2 are GPIOs */
1553         else
1554                 coef &= ~0x0008;
1555
1556         if (spec->sense_b)
1557                 coef |= 0x0010; /* B2 is SENSE_B, not inverted  */
1558         else
1559                 coef &= ~0x0010;
1560
1561         cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1562
1563         if ((spec->gpio_mask || spec->sense_b) &&
1564             is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1565
1566                 /*
1567                     GPIO or SENSE_B forced - disconnect the DMIC pin.
1568                 */
1569                 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1570                 def_conf &= ~AC_DEFCFG_PORT_CONN;
1571                 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1572                 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1573         }
1574 }
1575
1576 static void init_cs421x_digital(struct hda_codec *codec)
1577 {
1578         struct cs_spec *spec = codec->spec;
1579         struct auto_pin_cfg *cfg = &spec->autocfg;
1580         int i;
1581
1582
1583         for (i = 0; i < cfg->dig_outs; i++) {
1584                 hda_nid_t nid = cfg->dig_out_pins[i];
1585                 if (!cfg->speaker_outs)
1586                         continue;
1587                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1588
1589                         snd_hda_codec_write(codec, nid, 0,
1590                                     AC_VERB_SET_UNSOLICITED_ENABLE,
1591                                     AC_USRSP_EN | SPDIF_EVENT);
1592                         spec->spdif_detect = 1;
1593                 }
1594         }
1595 }
1596
1597 static int cs421x_init(struct hda_codec *codec)
1598 {
1599         struct cs_spec *spec = codec->spec;
1600
1601         snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1602         snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1603
1604         cs421x_pinmux_init(codec);
1605
1606         if (spec->gpio_mask) {
1607                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1608                                     spec->gpio_mask);
1609                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1610                                     spec->gpio_dir);
1611                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1612                                     spec->gpio_data);
1613         }
1614
1615         init_output(codec);
1616         init_input(codec);
1617         init_cs421x_digital(codec);
1618
1619         return 0;
1620 }
1621
1622 /*
1623  * CS4210 Input MUX (1 ADC)
1624  */
1625 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1626                                         struct snd_ctl_elem_info *uinfo)
1627 {
1628         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1629         struct cs_spec *spec = codec->spec;
1630
1631         return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1632 }
1633
1634 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1635                                         struct snd_ctl_elem_value *ucontrol)
1636 {
1637         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1638         struct cs_spec *spec = codec->spec;
1639
1640         ucontrol->value.enumerated.item[0] = spec->cur_input;
1641         return 0;
1642 }
1643
1644 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1645                                         struct snd_ctl_elem_value *ucontrol)
1646 {
1647         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1648         struct cs_spec *spec = codec->spec;
1649
1650         return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1651                                 spec->adc_nid[0], &spec->cur_input);
1652
1653 }
1654
1655 static struct snd_kcontrol_new cs421x_capture_source = {
1656
1657         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1658         .name = "Capture Source",
1659         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1660         .info = cs421x_mux_enum_info,
1661         .get = cs421x_mux_enum_get,
1662         .put = cs421x_mux_enum_put,
1663 };
1664
1665 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1666 {
1667         struct cs_spec *spec = codec->spec;
1668         struct auto_pin_cfg *cfg = &spec->autocfg;
1669         const struct hda_input_mux *imux = &spec->input_mux;
1670         hda_nid_t pin = cfg->inputs[item].pin;
1671         struct snd_kcontrol *kctl;
1672         u32 caps;
1673
1674         if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1675                 return 0;
1676
1677         caps = query_amp_caps(codec, pin, HDA_INPUT);
1678         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1679         if (caps <= 1)
1680                 return 0;
1681
1682         return add_volume(codec,  imux->items[item].label, 0,
1683                           HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1684 }
1685
1686 /* add a (input-boost) volume control to the given input pin */
1687 static int build_cs421x_input(struct hda_codec *codec)
1688 {
1689         struct cs_spec *spec = codec->spec;
1690         struct auto_pin_cfg *cfg = &spec->autocfg;
1691         struct hda_input_mux *imux = &spec->input_mux;
1692         int i, err, type_idx;
1693         const char *label;
1694
1695         if (!spec->num_inputs)
1696                 return 0;
1697
1698         /* make bind-capture */
1699         spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1700         spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1701         for (i = 0; i < 2; i++) {
1702                 struct snd_kcontrol *kctl;
1703                 int n;
1704                 if (!spec->capture_bind[i])
1705                         return -ENOMEM;
1706                 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1707                 if (!kctl)
1708                         return -ENOMEM;
1709                 kctl->private_value = (long)spec->capture_bind[i];
1710                 err = snd_hda_ctl_add(codec, 0, kctl);
1711                 if (err < 0)
1712                         return err;
1713                 for (n = 0; n < AUTO_PIN_LAST; n++) {
1714                         if (!spec->adc_nid[n])
1715                                 continue;
1716                         err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1717                         if (err < 0)
1718                                 return err;
1719                 }
1720         }
1721
1722         /* Add Input MUX Items + Capture Volume/Switch */
1723         for (i = 0; i < spec->num_inputs; i++) {
1724                 label = hda_get_autocfg_input_label(codec, cfg, i);
1725                 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1726
1727                 err = cs421x_add_input_volume_control(codec, i);
1728                 if (err < 0)
1729                         return err;
1730         }
1731
1732         /*
1733             Add 'Capture Source' Switch if
1734                 * 2 inputs and no mic detec
1735                 * 3 inputs
1736         */
1737         if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1738             (spec->num_inputs == 3)) {
1739
1740                 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1741                               snd_ctl_new1(&cs421x_capture_source, codec));
1742                 if (err < 0)
1743                         return err;
1744         }
1745
1746         return 0;
1747 }
1748
1749 /* Single DAC (Mute/Gain) */
1750 static int build_cs421x_output(struct hda_codec *codec)
1751 {
1752         hda_nid_t dac = CS4210_DAC_NID;
1753         struct cs_spec *spec = codec->spec;
1754         struct auto_pin_cfg *cfg = &spec->autocfg;
1755         struct snd_kcontrol *kctl;
1756         int err;
1757         char *name = "HP/Speakers";
1758
1759         fix_volume_caps(codec, dac);
1760         if (!spec->vmaster_sw) {
1761                 err = add_vmaster(codec, dac);
1762                 if (err < 0)
1763                         return err;
1764         }
1765
1766         err = add_mute(codec, name, 0,
1767                         HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1768         if (err < 0)
1769                 return err;
1770         err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1771         if (err < 0)
1772                 return err;
1773
1774         err = add_volume(codec, name, 0,
1775                         HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1776         if (err < 0)
1777                 return err;
1778         err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1779         if (err < 0)
1780                 return err;
1781
1782         if (cfg->speaker_outs) {
1783                 err = snd_hda_ctl_add(codec, 0,
1784                         snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1785                 if (err < 0)
1786                         return err;
1787         }
1788         return err;
1789 }
1790
1791 static int cs421x_build_controls(struct hda_codec *codec)
1792 {
1793         int err;
1794
1795         err = build_cs421x_output(codec);
1796         if (err < 0)
1797                 return err;
1798         err = build_cs421x_input(codec);
1799         if (err < 0)
1800                 return err;
1801         err = build_digital_output(codec);
1802         if (err < 0)
1803                 return err;
1804         return cs421x_init(codec);
1805 }
1806
1807 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1808 {
1809         switch ((res >> 26) & 0x3f) {
1810         case HP_EVENT:
1811         case SPDIF_EVENT:
1812                 cs_automute(codec);
1813                 break;
1814
1815         case MIC_EVENT:
1816                 cs_automic(codec);
1817                 break;
1818         }
1819 }
1820
1821 static int parse_cs421x_input(struct hda_codec *codec)
1822 {
1823         struct cs_spec *spec = codec->spec;
1824         struct auto_pin_cfg *cfg = &spec->autocfg;
1825         int i;
1826
1827         for (i = 0; i < cfg->num_inputs; i++) {
1828                 hda_nid_t pin = cfg->inputs[i].pin;
1829                 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1830                 spec->cur_input = spec->last_input = i;
1831                 spec->num_inputs++;
1832
1833                 /* check whether the automatic mic switch is available */
1834                 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1835                         spec->mic_detect = 1;
1836                         spec->automic_idx = i;
1837                 }
1838         }
1839         return 0;
1840 }
1841
1842 static int cs421x_parse_auto_config(struct hda_codec *codec)
1843 {
1844         struct cs_spec *spec = codec->spec;
1845         int err;
1846
1847         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1848         if (err < 0)
1849                 return err;
1850         err = parse_output(codec);
1851         if (err < 0)
1852                 return err;
1853         err = parse_cs421x_input(codec);
1854         if (err < 0)
1855                 return err;
1856         err = parse_digital_output(codec);
1857         if (err < 0)
1858                 return err;
1859         return 0;
1860 }
1861
1862 #ifdef CONFIG_PM
1863 /*
1864         Manage PDREF, when transitioning to D3hot
1865         (DAC,ADC) -> D3, PDREF=1, AFG->D3
1866 */
1867 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1868 {
1869         unsigned int coef;
1870
1871         snd_hda_shutup_pins(codec);
1872
1873         snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1874                             AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
1875         snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1876                             AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
1877
1878         coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1879         coef |= 0x0004; /* PDREF */
1880         cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1881
1882         return 0;
1883 }
1884 #endif
1885
1886 static struct hda_codec_ops cs4210_patch_ops = {
1887         .build_controls = cs421x_build_controls,
1888         .build_pcms = cs_build_pcms,
1889         .init = cs421x_init,
1890         .free = cs_free,
1891         .unsol_event = cs421x_unsol_event,
1892 #ifdef CONFIG_PM
1893         .suspend = cs421x_suspend,
1894 #endif
1895 };
1896
1897 static int patch_cs421x(struct hda_codec *codec)
1898 {
1899         struct cs_spec *spec;
1900         int err;
1901
1902         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1903         if (!spec)
1904                 return -ENOMEM;
1905         codec->spec = spec;
1906
1907         spec->vendor_nid = CS421X_VENDOR_NID;
1908
1909         spec->board_config =
1910                 snd_hda_check_board_config(codec, CS421X_MODELS,
1911                                            cs421x_models, cs421x_cfg_tbl);
1912         if (spec->board_config >= 0)
1913                 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1914         /*
1915             Setup GPIO/SENSE for each board (if used)
1916         */
1917         switch (spec->board_config) {
1918         case CS421X_CDB4210:
1919                 snd_printd("CS4210 board: %s\n",
1920                         cs421x_models[spec->board_config]);
1921 /*              spec->gpio_mask = 3;
1922                 spec->gpio_dir = 3;
1923                 spec->gpio_data = 3;
1924 */
1925                 spec->sense_b = 1;
1926
1927                 break;
1928         }
1929
1930         /*
1931             Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1932             is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1933             is disabled.
1934         */
1935         cs421x_pinmux_init(codec);
1936
1937         err = cs421x_parse_auto_config(codec);
1938         if (err < 0)
1939                 goto error;
1940
1941         codec->patch_ops = cs4210_patch_ops;
1942
1943         return 0;
1944
1945  error:
1946         kfree(codec->spec);
1947         codec->spec = NULL;
1948         return err;
1949 }
1950
1951
1952 /*
1953  * patch entries
1954  */
1955 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
1956         { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1957         { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1958         { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
1959         {} /* terminator */
1960 };
1961
1962 MODULE_ALIAS("snd-hda-codec-id:10134206");
1963 MODULE_ALIAS("snd-hda-codec-id:10134207");
1964 MODULE_ALIAS("snd-hda-codec-id:10134210");
1965
1966 MODULE_LICENSE("GPL");
1967 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1968
1969 static struct hda_codec_preset_list cirrus_list = {
1970         .preset = snd_hda_preset_cirrus,
1971         .owner = THIS_MODULE,
1972 };
1973
1974 static int __init patch_cirrus_init(void)
1975 {
1976         return snd_hda_add_codec_preset(&cirrus_list);
1977 }
1978
1979 static void __exit patch_cirrus_exit(void)
1980 {
1981         snd_hda_delete_codec_preset(&cirrus_list);
1982 }
1983
1984 module_init(patch_cirrus_init)
1985 module_exit(patch_cirrus_exit)