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