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