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