]> git.karo-electronics.de Git - linux-beck.git/blob - sound/pci/hda/patch_cirrus.c
ALSA: hda - Create jack-detection kcontrols
[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         snd_hda_jack_set_dirty_all(codec); /* FIXME: to be more fine-grained */
1239
1240         switch ((res >> 26) & 0x7f) {
1241         case HP_EVENT:
1242                 cs_automute(codec);
1243                 break;
1244         case MIC_EVENT:
1245                 cs_automic(codec);
1246                 break;
1247         }
1248         snd_hda_jack_report_sync(codec);
1249 }
1250
1251 static const struct hda_codec_ops cs_patch_ops = {
1252         .build_controls = cs_build_controls,
1253         .build_pcms = cs_build_pcms,
1254         .init = cs_init,
1255         .free = cs_free,
1256         .unsol_event = cs_unsol_event,
1257 };
1258
1259 static int cs_parse_auto_config(struct hda_codec *codec)
1260 {
1261         struct cs_spec *spec = codec->spec;
1262         int err;
1263
1264         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1265         if (err < 0)
1266                 return err;
1267
1268         err = parse_output(codec);
1269         if (err < 0)
1270                 return err;
1271         err = parse_input(codec);
1272         if (err < 0)
1273                 return err;
1274         err = parse_digital_output(codec);
1275         if (err < 0)
1276                 return err;
1277         err = parse_digital_input(codec);
1278         if (err < 0)
1279                 return err;
1280         return 0;
1281 }
1282
1283 static const char * const cs420x_models[CS420X_MODELS] = {
1284         [CS420X_MBP53] = "mbp53",
1285         [CS420X_MBP55] = "mbp55",
1286         [CS420X_IMAC27] = "imac27",
1287         [CS420X_AUTO] = "auto",
1288 };
1289
1290
1291 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1292         SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1293         SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1294         SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1295         SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1296         SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
1297         {} /* terminator */
1298 };
1299
1300 struct cs_pincfg {
1301         hda_nid_t nid;
1302         u32 val;
1303 };
1304
1305 static const struct cs_pincfg mbp53_pincfgs[] = {
1306         { 0x09, 0x012b4050 },
1307         { 0x0a, 0x90100141 },
1308         { 0x0b, 0x90100140 },
1309         { 0x0c, 0x018b3020 },
1310         { 0x0d, 0x90a00110 },
1311         { 0x0e, 0x400000f0 },
1312         { 0x0f, 0x01cbe030 },
1313         { 0x10, 0x014be060 },
1314         { 0x12, 0x400000f0 },
1315         { 0x15, 0x400000f0 },
1316         {} /* terminator */
1317 };
1318
1319 static const struct cs_pincfg mbp55_pincfgs[] = {
1320         { 0x09, 0x012b4030 },
1321         { 0x0a, 0x90100121 },
1322         { 0x0b, 0x90100120 },
1323         { 0x0c, 0x400000f0 },
1324         { 0x0d, 0x90a00110 },
1325         { 0x0e, 0x400000f0 },
1326         { 0x0f, 0x400000f0 },
1327         { 0x10, 0x014be040 },
1328         { 0x12, 0x400000f0 },
1329         { 0x15, 0x400000f0 },
1330         {} /* terminator */
1331 };
1332
1333 static const struct cs_pincfg imac27_pincfgs[] = {
1334         { 0x09, 0x012b4050 },
1335         { 0x0a, 0x90100140 },
1336         { 0x0b, 0x90100142 },
1337         { 0x0c, 0x018b3020 },
1338         { 0x0d, 0x90a00110 },
1339         { 0x0e, 0x400000f0 },
1340         { 0x0f, 0x01cbe030 },
1341         { 0x10, 0x014be060 },
1342         { 0x12, 0x01ab9070 },
1343         { 0x15, 0x400000f0 },
1344         {} /* terminator */
1345 };
1346
1347 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1348         [CS420X_MBP53] = mbp53_pincfgs,
1349         [CS420X_MBP55] = mbp55_pincfgs,
1350         [CS420X_IMAC27] = imac27_pincfgs,
1351 };
1352
1353 static void fix_pincfg(struct hda_codec *codec, int model,
1354                        const struct cs_pincfg **pin_configs)
1355 {
1356         const struct cs_pincfg *cfg = pin_configs[model];
1357         if (!cfg)
1358                 return;
1359         for (; cfg->nid; cfg++)
1360                 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1361 }
1362
1363 static int patch_cs420x(struct hda_codec *codec)
1364 {
1365         struct cs_spec *spec;
1366         int err;
1367
1368         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1369         if (!spec)
1370                 return -ENOMEM;
1371         codec->spec = spec;
1372
1373         spec->vendor_nid = CS420X_VENDOR_NID;
1374
1375         spec->board_config =
1376                 snd_hda_check_board_config(codec, CS420X_MODELS,
1377                                            cs420x_models, cs420x_cfg_tbl);
1378         if (spec->board_config >= 0)
1379                 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1380
1381         switch (spec->board_config) {
1382         case CS420X_IMAC27:
1383         case CS420X_MBP53:
1384         case CS420X_MBP55:
1385                 /* GPIO1 = headphones */
1386                 /* GPIO3 = speakers */
1387                 spec->gpio_mask = 0x0a;
1388                 spec->gpio_dir = 0x0a;
1389                 break;
1390         }
1391
1392         err = cs_parse_auto_config(codec);
1393         if (err < 0)
1394                 goto error;
1395
1396         codec->patch_ops = cs_patch_ops;
1397
1398         return 0;
1399
1400  error:
1401         kfree(codec->spec);
1402         codec->spec = NULL;
1403         return err;
1404 }
1405
1406 /*
1407  * Cirrus Logic CS4210
1408  *
1409  * 1 DAC => HP(sense) / Speakers,
1410  * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1411  * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1412 */
1413
1414 /* CS4210 board names */
1415 static const char *cs421x_models[CS421X_MODELS] = {
1416         [CS421X_CDB4210] = "cdb4210",
1417 };
1418
1419 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1420         /* Test Intel board + CDB2410  */
1421         SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1422         {} /* terminator */
1423 };
1424
1425 /* CS4210 board pinconfigs */
1426 /* Default CS4210 (CDB4210)*/
1427 static const struct cs_pincfg cdb4210_pincfgs[] = {
1428         { 0x05, 0x0321401f },
1429         { 0x06, 0x90170010 },
1430         { 0x07, 0x03813031 },
1431         { 0x08, 0xb7a70037 },
1432         { 0x09, 0xb7a6003e },
1433         { 0x0a, 0x034510f0 },
1434         {} /* terminator */
1435 };
1436
1437 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1438         [CS421X_CDB4210] = cdb4210_pincfgs,
1439 };
1440
1441 static const struct hda_verb cs421x_coef_init_verbs[] = {
1442         {0x0B, AC_VERB_SET_PROC_STATE, 1},
1443         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1444         /*
1445             Disable Coefficient Index Auto-Increment(DAI)=1,
1446             PDREF=0
1447         */
1448         {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1449
1450         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1451         /* ADC SZCMode = Digital Soft Ramp */
1452         {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1453
1454         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1455         {0x0B, AC_VERB_SET_PROC_COEF,
1456          (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1457           | 0x0004 /* Mute DAC on FIFO error */
1458           | 0x0008 /* Enable DAC High Pass Filter */
1459           )},
1460         {} /* terminator */
1461 };
1462
1463 /* Errata: CS4210 rev A1 Silicon
1464  *
1465  * http://www.cirrus.com/en/pubs/errata/
1466  *
1467  * Description:
1468  * 1. Performance degredation is present in the ADC.
1469  * 2. Speaker output is not completely muted upon HP detect.
1470  * 3. Noise is present when clipping occurs on the amplified
1471  *    speaker outputs.
1472  *
1473  * Workaround:
1474  * The following verb sequence written to the registers during
1475  * initialization will correct the issues listed above.
1476  */
1477
1478 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1479         {0x0B, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
1480
1481         {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1482         {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1483
1484         {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1485         {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1486
1487         {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1488         {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1489
1490         {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1491         {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1492
1493         {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1494         {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1495
1496         {} /* terminator */
1497 };
1498
1499 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1500 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1501
1502 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1503                                 struct snd_ctl_elem_info *uinfo)
1504 {
1505         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1506         uinfo->count = 1;
1507         uinfo->value.integer.min = 0;
1508         uinfo->value.integer.max = 3;
1509         return 0;
1510 }
1511
1512 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1513                                 struct snd_ctl_elem_value *ucontrol)
1514 {
1515         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1516
1517         ucontrol->value.integer.value[0] =
1518                 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1519         return 0;
1520 }
1521
1522 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1523                                 struct snd_ctl_elem_value *ucontrol)
1524 {
1525         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1526
1527         unsigned int vol = ucontrol->value.integer.value[0];
1528         unsigned int coef =
1529                 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1530         unsigned int original_coef = coef;
1531
1532         coef &= ~0x0003;
1533         coef |= (vol & 0x0003);
1534         if (original_coef == coef)
1535                 return 0;
1536         else {
1537                 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1538                 return 1;
1539         }
1540 }
1541
1542 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1543
1544         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1545         .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1546                         SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1547         .name = "Speaker Boost Playback Volume",
1548         .info = cs421x_boost_vol_info,
1549         .get = cs421x_boost_vol_get,
1550         .put = cs421x_boost_vol_put,
1551         .tlv = { .p = cs421x_speaker_boost_db_scale },
1552 };
1553
1554 static void cs421x_pinmux_init(struct hda_codec *codec)
1555 {
1556         struct cs_spec *spec = codec->spec;
1557         unsigned int def_conf, coef;
1558
1559         /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1560         coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1561
1562         if (spec->gpio_mask)
1563                 coef |= 0x0008; /* B1,B2 are GPIOs */
1564         else
1565                 coef &= ~0x0008;
1566
1567         if (spec->sense_b)
1568                 coef |= 0x0010; /* B2 is SENSE_B, not inverted  */
1569         else
1570                 coef &= ~0x0010;
1571
1572         cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1573
1574         if ((spec->gpio_mask || spec->sense_b) &&
1575             is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1576
1577                 /*
1578                     GPIO or SENSE_B forced - disconnect the DMIC pin.
1579                 */
1580                 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1581                 def_conf &= ~AC_DEFCFG_PORT_CONN;
1582                 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1583                 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1584         }
1585 }
1586
1587 static void init_cs421x_digital(struct hda_codec *codec)
1588 {
1589         struct cs_spec *spec = codec->spec;
1590         struct auto_pin_cfg *cfg = &spec->autocfg;
1591         int i;
1592
1593
1594         for (i = 0; i < cfg->dig_outs; i++) {
1595                 hda_nid_t nid = cfg->dig_out_pins[i];
1596                 if (!cfg->speaker_outs)
1597                         continue;
1598                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1599                         snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
1600                         spec->spdif_detect = 1;
1601                 }
1602         }
1603 }
1604
1605 static int cs421x_init(struct hda_codec *codec)
1606 {
1607         struct cs_spec *spec = codec->spec;
1608
1609         snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1610         snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1611
1612         cs421x_pinmux_init(codec);
1613
1614         if (spec->gpio_mask) {
1615                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1616                                     spec->gpio_mask);
1617                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1618                                     spec->gpio_dir);
1619                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1620                                     spec->gpio_data);
1621         }
1622
1623         init_output(codec);
1624         init_input(codec);
1625         init_cs421x_digital(codec);
1626         snd_hda_jack_report_sync(codec);
1627
1628         return 0;
1629 }
1630
1631 /*
1632  * CS4210 Input MUX (1 ADC)
1633  */
1634 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1635                                         struct snd_ctl_elem_info *uinfo)
1636 {
1637         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1638         struct cs_spec *spec = codec->spec;
1639
1640         return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1641 }
1642
1643 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1644                                         struct snd_ctl_elem_value *ucontrol)
1645 {
1646         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1647         struct cs_spec *spec = codec->spec;
1648
1649         ucontrol->value.enumerated.item[0] = spec->cur_input;
1650         return 0;
1651 }
1652
1653 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1654                                         struct snd_ctl_elem_value *ucontrol)
1655 {
1656         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1657         struct cs_spec *spec = codec->spec;
1658
1659         return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1660                                 spec->adc_nid[0], &spec->cur_input);
1661
1662 }
1663
1664 static struct snd_kcontrol_new cs421x_capture_source = {
1665
1666         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1667         .name = "Capture Source",
1668         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1669         .info = cs421x_mux_enum_info,
1670         .get = cs421x_mux_enum_get,
1671         .put = cs421x_mux_enum_put,
1672 };
1673
1674 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1675 {
1676         struct cs_spec *spec = codec->spec;
1677         struct auto_pin_cfg *cfg = &spec->autocfg;
1678         const struct hda_input_mux *imux = &spec->input_mux;
1679         hda_nid_t pin = cfg->inputs[item].pin;
1680         struct snd_kcontrol *kctl;
1681         u32 caps;
1682
1683         if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1684                 return 0;
1685
1686         caps = query_amp_caps(codec, pin, HDA_INPUT);
1687         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1688         if (caps <= 1)
1689                 return 0;
1690
1691         return add_volume(codec,  imux->items[item].label, 0,
1692                           HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1693 }
1694
1695 /* add a (input-boost) volume control to the given input pin */
1696 static int build_cs421x_input(struct hda_codec *codec)
1697 {
1698         struct cs_spec *spec = codec->spec;
1699         struct auto_pin_cfg *cfg = &spec->autocfg;
1700         struct hda_input_mux *imux = &spec->input_mux;
1701         int i, err, type_idx;
1702         const char *label;
1703
1704         if (!spec->num_inputs)
1705                 return 0;
1706
1707         /* make bind-capture */
1708         spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1709         spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1710         for (i = 0; i < 2; i++) {
1711                 struct snd_kcontrol *kctl;
1712                 int n;
1713                 if (!spec->capture_bind[i])
1714                         return -ENOMEM;
1715                 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1716                 if (!kctl)
1717                         return -ENOMEM;
1718                 kctl->private_value = (long)spec->capture_bind[i];
1719                 err = snd_hda_ctl_add(codec, 0, kctl);
1720                 if (err < 0)
1721                         return err;
1722                 for (n = 0; n < AUTO_PIN_LAST; n++) {
1723                         if (!spec->adc_nid[n])
1724                                 continue;
1725                         err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1726                         if (err < 0)
1727                                 return err;
1728                 }
1729         }
1730
1731         /* Add Input MUX Items + Capture Volume/Switch */
1732         for (i = 0; i < spec->num_inputs; i++) {
1733                 label = hda_get_autocfg_input_label(codec, cfg, i);
1734                 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1735
1736                 err = cs421x_add_input_volume_control(codec, i);
1737                 if (err < 0)
1738                         return err;
1739         }
1740
1741         /*
1742             Add 'Capture Source' Switch if
1743                 * 2 inputs and no mic detec
1744                 * 3 inputs
1745         */
1746         if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1747             (spec->num_inputs == 3)) {
1748
1749                 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1750                               snd_ctl_new1(&cs421x_capture_source, codec));
1751                 if (err < 0)
1752                         return err;
1753         }
1754
1755         return 0;
1756 }
1757
1758 /* Single DAC (Mute/Gain) */
1759 static int build_cs421x_output(struct hda_codec *codec)
1760 {
1761         hda_nid_t dac = CS4210_DAC_NID;
1762         struct cs_spec *spec = codec->spec;
1763         struct auto_pin_cfg *cfg = &spec->autocfg;
1764         struct snd_kcontrol *kctl;
1765         int err;
1766         char *name = "HP/Speakers";
1767
1768         fix_volume_caps(codec, dac);
1769         if (!spec->vmaster_sw) {
1770                 err = add_vmaster(codec, dac);
1771                 if (err < 0)
1772                         return err;
1773         }
1774
1775         err = add_mute(codec, name, 0,
1776                         HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1777         if (err < 0)
1778                 return err;
1779         err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1780         if (err < 0)
1781                 return err;
1782
1783         err = add_volume(codec, name, 0,
1784                         HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1785         if (err < 0)
1786                 return err;
1787         err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1788         if (err < 0)
1789                 return err;
1790
1791         if (cfg->speaker_outs) {
1792                 err = snd_hda_ctl_add(codec, 0,
1793                         snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1794                 if (err < 0)
1795                         return err;
1796         }
1797         return err;
1798 }
1799
1800 static int cs421x_build_controls(struct hda_codec *codec)
1801 {
1802         struct cs_spec *spec = codec->spec;
1803         int err;
1804
1805         err = build_cs421x_output(codec);
1806         if (err < 0)
1807                 return err;
1808         err = build_cs421x_input(codec);
1809         if (err < 0)
1810                 return err;
1811         err = build_digital_output(codec);
1812         if (err < 0)
1813                 return err;
1814         err =  cs421x_init(codec);
1815         if (err < 0)
1816                 return err;
1817
1818         err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1819         if (err < 0)
1820                 return err;
1821
1822         return 0;
1823 }
1824
1825 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1826 {
1827         snd_hda_jack_set_dirty_all(codec); /* FIXME: to be more fine-grained */
1828
1829         switch ((res >> 26) & 0x3f) {
1830         case HP_EVENT:
1831         case SPDIF_EVENT:
1832                 cs_automute(codec);
1833                 break;
1834
1835         case MIC_EVENT:
1836                 cs_automic(codec);
1837                 break;
1838         }
1839         snd_hda_jack_report_sync(codec);
1840 }
1841
1842 static int parse_cs421x_input(struct hda_codec *codec)
1843 {
1844         struct cs_spec *spec = codec->spec;
1845         struct auto_pin_cfg *cfg = &spec->autocfg;
1846         int i;
1847
1848         for (i = 0; i < cfg->num_inputs; i++) {
1849                 hda_nid_t pin = cfg->inputs[i].pin;
1850                 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1851                 spec->cur_input = spec->last_input = i;
1852                 spec->num_inputs++;
1853
1854                 /* check whether the automatic mic switch is available */
1855                 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1856                         spec->mic_detect = 1;
1857                         spec->automic_idx = i;
1858                 }
1859         }
1860         return 0;
1861 }
1862
1863 static int cs421x_parse_auto_config(struct hda_codec *codec)
1864 {
1865         struct cs_spec *spec = codec->spec;
1866         int err;
1867
1868         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1869         if (err < 0)
1870                 return err;
1871         err = parse_output(codec);
1872         if (err < 0)
1873                 return err;
1874         err = parse_cs421x_input(codec);
1875         if (err < 0)
1876                 return err;
1877         err = parse_digital_output(codec);
1878         if (err < 0)
1879                 return err;
1880         return 0;
1881 }
1882
1883 #ifdef CONFIG_PM
1884 /*
1885         Manage PDREF, when transitioning to D3hot
1886         (DAC,ADC) -> D3, PDREF=1, AFG->D3
1887 */
1888 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1889 {
1890         unsigned int coef;
1891
1892         snd_hda_shutup_pins(codec);
1893
1894         snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1895                             AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
1896         snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1897                             AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
1898
1899         coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1900         coef |= 0x0004; /* PDREF */
1901         cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1902
1903         return 0;
1904 }
1905 #endif
1906
1907 static struct hda_codec_ops cs4210_patch_ops = {
1908         .build_controls = cs421x_build_controls,
1909         .build_pcms = cs_build_pcms,
1910         .init = cs421x_init,
1911         .free = cs_free,
1912         .unsol_event = cs421x_unsol_event,
1913 #ifdef CONFIG_PM
1914         .suspend = cs421x_suspend,
1915 #endif
1916 };
1917
1918 static int patch_cs421x(struct hda_codec *codec)
1919 {
1920         struct cs_spec *spec;
1921         int err;
1922
1923         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1924         if (!spec)
1925                 return -ENOMEM;
1926         codec->spec = spec;
1927
1928         spec->vendor_nid = CS421X_VENDOR_NID;
1929
1930         spec->board_config =
1931                 snd_hda_check_board_config(codec, CS421X_MODELS,
1932                                            cs421x_models, cs421x_cfg_tbl);
1933         if (spec->board_config >= 0)
1934                 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1935         /*
1936             Setup GPIO/SENSE for each board (if used)
1937         */
1938         switch (spec->board_config) {
1939         case CS421X_CDB4210:
1940                 snd_printd("CS4210 board: %s\n",
1941                         cs421x_models[spec->board_config]);
1942 /*              spec->gpio_mask = 3;
1943                 spec->gpio_dir = 3;
1944                 spec->gpio_data = 3;
1945 */
1946                 spec->sense_b = 1;
1947
1948                 break;
1949         }
1950
1951         /*
1952             Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1953             is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1954             is disabled.
1955         */
1956         cs421x_pinmux_init(codec);
1957
1958         err = cs421x_parse_auto_config(codec);
1959         if (err < 0)
1960                 goto error;
1961
1962         codec->patch_ops = cs4210_patch_ops;
1963
1964         return 0;
1965
1966  error:
1967         kfree(codec->spec);
1968         codec->spec = NULL;
1969         return err;
1970 }
1971
1972
1973 /*
1974  * patch entries
1975  */
1976 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
1977         { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1978         { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1979         { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
1980         {} /* terminator */
1981 };
1982
1983 MODULE_ALIAS("snd-hda-codec-id:10134206");
1984 MODULE_ALIAS("snd-hda-codec-id:10134207");
1985 MODULE_ALIAS("snd-hda-codec-id:10134210");
1986
1987 MODULE_LICENSE("GPL");
1988 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1989
1990 static struct hda_codec_preset_list cirrus_list = {
1991         .preset = snd_hda_preset_cirrus,
1992         .owner = THIS_MODULE,
1993 };
1994
1995 static int __init patch_cirrus_init(void)
1996 {
1997         return snd_hda_add_codec_preset(&cirrus_list);
1998 }
1999
2000 static void __exit patch_cirrus_exit(void)
2001 {
2002         snd_hda_delete_codec_preset(&cirrus_list);
2003 }
2004
2005 module_init(patch_cirrus_init)
2006 module_exit(patch_cirrus_exit)