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