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