64362433b98114409c6b5fe05054a9b264cb92ac
[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         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_codec_write(codec, nid, 0,
938                                     AC_VERB_SET_PIN_WIDGET_CONTROL, pin_ctl);
939         }
940         if (spec->gpio_eapd_hp) {
941                 unsigned int gpio = hp_present ?
942                         spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
943                 snd_hda_codec_write(codec, 0x01, 0,
944                                     AC_VERB_SET_GPIO_DATA, gpio);
945         }
946
947         /* specific to CS4210 */
948         if (spec->vendor_nid == CS4210_VENDOR_NID) {
949                 /* mute HPs if spdif jack (SENSE_B) is present */
950                 for (i = 0; i < cfg->hp_outs; i++) {
951                         nid = cfg->hp_pins[i];
952                         snd_hda_codec_write(codec, nid, 0,
953                                 AC_VERB_SET_PIN_WIDGET_CONTROL,
954                                 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
955                 }
956
957                 /* SPDIF TX on/off */
958                 if (cfg->dig_outs) {
959                         nid = cfg->dig_out_pins[0];
960                         snd_hda_codec_write(codec, nid, 0,
961                                 AC_VERB_SET_PIN_WIDGET_CONTROL,
962                                 spdif_present ? PIN_OUT : 0);
963
964                 }
965                 /* Update board GPIOs if neccessary ... */
966         }
967 }
968
969 /*
970  * Auto-input redirect for CS421x
971  * Switch max 3 inputs of a single ADC (nid 3)
972 */
973
974 static void cs_automic(struct hda_codec *codec)
975 {
976         struct cs_spec *spec = codec->spec;
977         struct auto_pin_cfg *cfg = &spec->autocfg;
978         hda_nid_t nid;
979         unsigned int present;
980
981         nid = cfg->inputs[spec->automic_idx].pin;
982         present = snd_hda_jack_detect(codec, nid);
983
984         /* specific to CS421x, single ADC */
985         if (spec->vendor_nid == CS420X_VENDOR_NID) {
986                 if (present)
987                         change_cur_input(codec, spec->automic_idx, 0);
988                 else
989                         change_cur_input(codec, !spec->automic_idx, 0);
990         } else {
991                 if (present) {
992                         if (spec->cur_input != spec->automic_idx) {
993                                 spec->last_input = spec->cur_input;
994                                 spec->cur_input = spec->automic_idx;
995                         }
996                 } else  {
997                         spec->cur_input = spec->last_input;
998                 }
999                 cs_update_input_select(codec);
1000         }
1001 }
1002
1003 /*
1004  */
1005
1006 static void init_output(struct hda_codec *codec)
1007 {
1008         struct cs_spec *spec = codec->spec;
1009         struct auto_pin_cfg *cfg = &spec->autocfg;
1010         int i;
1011
1012         /* mute first */
1013         for (i = 0; i < spec->multiout.num_dacs; i++)
1014                 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1015                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1016         if (spec->multiout.hp_nid)
1017                 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1018                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1019         for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1020                 if (!spec->multiout.extra_out_nid[i])
1021                         break;
1022                 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1023                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1024         }
1025
1026         /* set appropriate pin controls */
1027         for (i = 0; i < cfg->line_outs; i++)
1028                 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1029                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1030         /* HP */
1031         for (i = 0; i < cfg->hp_outs; i++) {
1032                 hda_nid_t nid = cfg->hp_pins[i];
1033                 snd_hda_codec_write(codec, nid, 0,
1034                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1035                 if (!cfg->speaker_outs)
1036                         continue;
1037                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1038                         snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
1039                         spec->hp_detect = 1;
1040                 }
1041         }
1042
1043         /* Speaker */
1044         for (i = 0; i < cfg->speaker_outs; i++)
1045                 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1046                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1047
1048         /* SPDIF is enabled on presence detect for CS421x */
1049         if (spec->hp_detect || spec->spdif_detect)
1050                 cs_automute(codec);
1051 }
1052
1053 static void init_input(struct hda_codec *codec)
1054 {
1055         struct cs_spec *spec = codec->spec;
1056         struct auto_pin_cfg *cfg = &spec->autocfg;
1057         unsigned int coef;
1058         int i;
1059
1060         for (i = 0; i < cfg->num_inputs; i++) {
1061                 unsigned int ctl;
1062                 hda_nid_t pin = cfg->inputs[i].pin;
1063                 if (!spec->adc_nid[i])
1064                         continue;
1065                 /* set appropriate pin control and mute first */
1066                 ctl = PIN_IN;
1067                 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1068                         unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1069                         caps >>= AC_PINCAP_VREF_SHIFT;
1070                         if (caps & AC_PINCAP_VREF_80)
1071                                 ctl = PIN_VREF80;
1072                 }
1073                 snd_hda_codec_write(codec, pin, 0,
1074                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1075                 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1076                                     AC_VERB_SET_AMP_GAIN_MUTE,
1077                                     AMP_IN_MUTE(spec->adc_idx[i]));
1078                 if (spec->mic_detect && spec->automic_idx == i)
1079                         snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
1080         }
1081         /* CS420x has multiple ADC, CS421x has single ADC */
1082         if (spec->vendor_nid == CS420X_VENDOR_NID) {
1083                 change_cur_input(codec, spec->cur_input, 1);
1084                 if (spec->mic_detect)
1085                         cs_automic(codec);
1086
1087                 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1088                 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1089                         coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1090                 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1091                         coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1092                                          * No effect if SPDIF_OUT2 is
1093                                          * selected in IDX_SPDIF_CTL.
1094                                         */
1095                 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1096         } else {
1097                 if (spec->mic_detect)
1098                         cs_automic(codec);
1099                 else  {
1100                         spec->cur_adc = spec->adc_nid[spec->cur_input];
1101                         cs_update_input_select(codec);
1102                 }
1103         }
1104 }
1105
1106 static const struct hda_verb cs_coef_init_verbs[] = {
1107         {0x11, AC_VERB_SET_PROC_STATE, 1},
1108         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1109         {0x11, AC_VERB_SET_PROC_COEF,
1110          (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1111           | 0x0040 /* Mute DACs on FIFO error */
1112           | 0x1000 /* Enable DACs High Pass Filter */
1113           | 0x0400 /* Disable Coefficient Auto increment */
1114           )},
1115         /* Beep */
1116         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1117         {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1118
1119         {} /* terminator */
1120 };
1121
1122 /* Errata: CS4207 rev C0/C1/C2 Silicon
1123  *
1124  * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1125  *
1126  * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1127  * may be excessive (up to an additional 200 Î¼A), which is most easily
1128  * observed while the part is being held in reset (RESET# active low).
1129  *
1130  * Root Cause: At initial powerup of the device, the logic that drives
1131  * the clock and write enable to the S/PDIF SRC RAMs is not properly
1132  * initialized.
1133  * Certain random patterns will cause a steady leakage current in those
1134  * RAM cells. The issue will resolve once the SRCs are used (turned on).
1135  *
1136  * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1137  * blocks, which will alleviate the issue.
1138  */
1139
1140 static const struct hda_verb cs_errata_init_verbs[] = {
1141         {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1142         {0x11, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
1143
1144         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1145         {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1146         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1147         {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1148         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1149         {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1150
1151         {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1152         {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1153
1154         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1155         {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1156         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1157         {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1158         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1159         {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1160         {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1161
1162 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1163         {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1164         {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1165         /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1166 #endif
1167
1168         {} /* terminator */
1169 };
1170
1171 /* SPDIF setup */
1172 static void init_digital(struct hda_codec *codec)
1173 {
1174         unsigned int coef;
1175
1176         coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1177         coef |= 0x0008; /* Replace with mute on error */
1178         if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1179                 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1180                                  * SPDIF_OUT2 is shared with GPIO1 and
1181                                  * DMIC_SDA2.
1182                                  */
1183         cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1184 }
1185
1186 static int cs_init(struct hda_codec *codec)
1187 {
1188         struct cs_spec *spec = codec->spec;
1189
1190         /* init_verb sequence for C0/C1/C2 errata*/
1191         snd_hda_sequence_write(codec, cs_errata_init_verbs);
1192
1193         snd_hda_sequence_write(codec, cs_coef_init_verbs);
1194
1195         if (spec->gpio_mask) {
1196                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1197                                     spec->gpio_mask);
1198                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1199                                     spec->gpio_dir);
1200                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1201                                     spec->gpio_data);
1202         }
1203
1204         init_output(codec);
1205         init_input(codec);
1206         init_digital(codec);
1207         snd_hda_jack_report_sync(codec);
1208
1209         return 0;
1210 }
1211
1212 static int cs_build_controls(struct hda_codec *codec)
1213 {
1214         struct cs_spec *spec = codec->spec;
1215         int err;
1216
1217         err = build_output(codec);
1218         if (err < 0)
1219                 return err;
1220         err = build_input(codec);
1221         if (err < 0)
1222                 return err;
1223         err = build_digital_output(codec);
1224         if (err < 0)
1225                 return err;
1226         err = build_digital_input(codec);
1227         if (err < 0)
1228                 return err;
1229         err = cs_init(codec);
1230         if (err < 0)
1231                 return err;
1232
1233         err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1234         if (err < 0)
1235                 return err;
1236
1237         return 0;
1238 }
1239
1240 static void cs_free(struct hda_codec *codec)
1241 {
1242         struct cs_spec *spec = codec->spec;
1243         kfree(spec->capture_bind[0]);
1244         kfree(spec->capture_bind[1]);
1245         kfree(codec->spec);
1246 }
1247
1248 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1249 {
1250         switch (snd_hda_jack_get_action(codec, res >> 26)) {
1251         case HP_EVENT:
1252                 cs_automute(codec);
1253                 break;
1254         case MIC_EVENT:
1255                 cs_automic(codec);
1256                 break;
1257         }
1258         snd_hda_jack_report_sync(codec);
1259 }
1260
1261 static const struct hda_codec_ops cs_patch_ops = {
1262         .build_controls = cs_build_controls,
1263         .build_pcms = cs_build_pcms,
1264         .init = cs_init,
1265         .free = cs_free,
1266         .unsol_event = cs_unsol_event,
1267 };
1268
1269 static int cs_parse_auto_config(struct hda_codec *codec)
1270 {
1271         struct cs_spec *spec = codec->spec;
1272         int err;
1273
1274         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1275         if (err < 0)
1276                 return err;
1277
1278         err = parse_output(codec);
1279         if (err < 0)
1280                 return err;
1281         err = parse_input(codec);
1282         if (err < 0)
1283                 return err;
1284         err = parse_digital_output(codec);
1285         if (err < 0)
1286                 return err;
1287         err = parse_digital_input(codec);
1288         if (err < 0)
1289                 return err;
1290         return 0;
1291 }
1292
1293 static const char * const cs420x_models[CS420X_MODELS] = {
1294         [CS420X_MBP53] = "mbp53",
1295         [CS420X_MBP55] = "mbp55",
1296         [CS420X_IMAC27] = "imac27",
1297         [CS420X_IMAC27_122] = "imac27_122",
1298         [CS420X_APPLE] = "apple",
1299         [CS420X_AUTO] = "auto",
1300 };
1301
1302
1303 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1304         SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1305         SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1306         SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1307         SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1308         /* this conflicts with too many other models */
1309         /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1310         {} /* terminator */
1311 };
1312
1313 static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
1314         SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
1315         SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
1316         {} /* terminator */
1317 };
1318
1319 struct cs_pincfg {
1320         hda_nid_t nid;
1321         u32 val;
1322 };
1323
1324 static const struct cs_pincfg mbp53_pincfgs[] = {
1325         { 0x09, 0x012b4050 },
1326         { 0x0a, 0x90100141 },
1327         { 0x0b, 0x90100140 },
1328         { 0x0c, 0x018b3020 },
1329         { 0x0d, 0x90a00110 },
1330         { 0x0e, 0x400000f0 },
1331         { 0x0f, 0x01cbe030 },
1332         { 0x10, 0x014be060 },
1333         { 0x12, 0x400000f0 },
1334         { 0x15, 0x400000f0 },
1335         {} /* terminator */
1336 };
1337
1338 static const struct cs_pincfg mbp55_pincfgs[] = {
1339         { 0x09, 0x012b4030 },
1340         { 0x0a, 0x90100121 },
1341         { 0x0b, 0x90100120 },
1342         { 0x0c, 0x400000f0 },
1343         { 0x0d, 0x90a00110 },
1344         { 0x0e, 0x400000f0 },
1345         { 0x0f, 0x400000f0 },
1346         { 0x10, 0x014be040 },
1347         { 0x12, 0x400000f0 },
1348         { 0x15, 0x400000f0 },
1349         {} /* terminator */
1350 };
1351
1352 static const struct cs_pincfg imac27_pincfgs[] = {
1353         { 0x09, 0x012b4050 },
1354         { 0x0a, 0x90100140 },
1355         { 0x0b, 0x90100142 },
1356         { 0x0c, 0x018b3020 },
1357         { 0x0d, 0x90a00110 },
1358         { 0x0e, 0x400000f0 },
1359         { 0x0f, 0x01cbe030 },
1360         { 0x10, 0x014be060 },
1361         { 0x12, 0x01ab9070 },
1362         { 0x15, 0x400000f0 },
1363         {} /* terminator */
1364 };
1365
1366 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1367         [CS420X_MBP53] = mbp53_pincfgs,
1368         [CS420X_MBP55] = mbp55_pincfgs,
1369         [CS420X_IMAC27] = imac27_pincfgs,
1370 };
1371
1372 static void fix_pincfg(struct hda_codec *codec, int model,
1373                        const struct cs_pincfg **pin_configs)
1374 {
1375         const struct cs_pincfg *cfg = pin_configs[model];
1376         if (!cfg)
1377                 return;
1378         for (; cfg->nid; cfg++)
1379                 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1380 }
1381
1382 static int patch_cs420x(struct hda_codec *codec)
1383 {
1384         struct cs_spec *spec;
1385         int err;
1386
1387         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1388         if (!spec)
1389                 return -ENOMEM;
1390         codec->spec = spec;
1391
1392         spec->vendor_nid = CS420X_VENDOR_NID;
1393
1394         spec->board_config =
1395                 snd_hda_check_board_config(codec, CS420X_MODELS,
1396                                            cs420x_models, cs420x_cfg_tbl);
1397         if (spec->board_config < 0)
1398                 spec->board_config =
1399                         snd_hda_check_board_codec_sid_config(codec,
1400                                 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
1401         if (spec->board_config >= 0)
1402                 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1403
1404         switch (spec->board_config) {
1405         case CS420X_IMAC27:
1406         case CS420X_MBP53:
1407         case CS420X_MBP55:
1408         case CS420X_APPLE:
1409                 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1410                 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1411                 spec->gpio_mask = spec->gpio_dir =
1412                         spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1413                 break;
1414         case CS420X_IMAC27_122:
1415                 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1416                 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1417                 spec->gpio_mask = spec->gpio_dir =
1418                         spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1419                 break;
1420         }
1421
1422         err = cs_parse_auto_config(codec);
1423         if (err < 0)
1424                 goto error;
1425
1426         codec->patch_ops = cs_patch_ops;
1427
1428         return 0;
1429
1430  error:
1431         kfree(codec->spec);
1432         codec->spec = NULL;
1433         return err;
1434 }
1435
1436 /*
1437  * Cirrus Logic CS4210
1438  *
1439  * 1 DAC => HP(sense) / Speakers,
1440  * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1441  * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1442 */
1443
1444 /* CS4210 board names */
1445 static const char *cs421x_models[CS421X_MODELS] = {
1446         [CS421X_CDB4210] = "cdb4210",
1447         [STUMPY_CDB4210] = "stumpy",
1448 };
1449
1450 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1451         /* Test Intel board + CDB2410  */
1452         SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1453         {} /* terminator */
1454 };
1455
1456 /* CS4210 board pinconfigs */
1457 /* Default CS4210 (CDB4210)*/
1458 static const struct cs_pincfg cdb4210_pincfgs[] = {
1459         { 0x05, 0x0321401f },
1460         { 0x06, 0x90170010 },
1461         { 0x07, 0x03813031 },
1462         { 0x08, 0xb7a70037 },
1463         { 0x09, 0xb7a6003e },
1464         { 0x0a, 0x034510f0 },
1465         {} /* terminator */
1466 };
1467
1468 /* Stumpy */
1469 static struct cs_pincfg stumpy_pincfgs[] = {
1470         { 0x05, 0x022120f0 },
1471         { 0x06, 0x901700f0 },
1472         { 0x07, 0x02a120f0 },
1473         { 0x08, 0x77a70037 },
1474         { 0x09, 0x77a6003e },
1475         { 0x0a, 0x434510f0 },
1476         {} /* terminator */
1477 };
1478
1479 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1480         [CS421X_CDB4210] = cdb4210_pincfgs,
1481         [STUMPY_CDB4210] = stumpy_pincfgs,
1482 };
1483
1484 static const struct hda_verb cs421x_coef_init_verbs[] = {
1485         {0x0B, AC_VERB_SET_PROC_STATE, 1},
1486         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1487         /*
1488             Disable Coefficient Index Auto-Increment(DAI)=1,
1489             PDREF=0
1490         */
1491         {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1492
1493         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1494         /* ADC SZCMode = Digital Soft Ramp */
1495         {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1496
1497         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1498         {0x0B, AC_VERB_SET_PROC_COEF,
1499          (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1500           | 0x0004 /* Mute DAC on FIFO error */
1501           | 0x0008 /* Enable DAC High Pass Filter */
1502           )},
1503         {} /* terminator */
1504 };
1505
1506 /* Errata: CS4210 rev A1 Silicon
1507  *
1508  * http://www.cirrus.com/en/pubs/errata/
1509  *
1510  * Description:
1511  * 1. Performance degredation is present in the ADC.
1512  * 2. Speaker output is not completely muted upon HP detect.
1513  * 3. Noise is present when clipping occurs on the amplified
1514  *    speaker outputs.
1515  *
1516  * Workaround:
1517  * The following verb sequence written to the registers during
1518  * initialization will correct the issues listed above.
1519  */
1520
1521 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1522         {0x0B, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
1523
1524         {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1525         {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1526
1527         {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1528         {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1529
1530         {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1531         {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1532
1533         {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1534         {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1535
1536         {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1537         {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1538
1539         {} /* terminator */
1540 };
1541
1542 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1543 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1544
1545 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1546                                 struct snd_ctl_elem_info *uinfo)
1547 {
1548         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1549         uinfo->count = 1;
1550         uinfo->value.integer.min = 0;
1551         uinfo->value.integer.max = 3;
1552         return 0;
1553 }
1554
1555 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1556                                 struct snd_ctl_elem_value *ucontrol)
1557 {
1558         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1559
1560         ucontrol->value.integer.value[0] =
1561                 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1562         return 0;
1563 }
1564
1565 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1566                                 struct snd_ctl_elem_value *ucontrol)
1567 {
1568         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1569
1570         unsigned int vol = ucontrol->value.integer.value[0];
1571         unsigned int coef =
1572                 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1573         unsigned int original_coef = coef;
1574
1575         coef &= ~0x0003;
1576         coef |= (vol & 0x0003);
1577         if (original_coef == coef)
1578                 return 0;
1579         else {
1580                 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1581                 return 1;
1582         }
1583 }
1584
1585 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1586
1587         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1588         .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1589                         SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1590         .name = "Speaker Boost Playback Volume",
1591         .info = cs421x_boost_vol_info,
1592         .get = cs421x_boost_vol_get,
1593         .put = cs421x_boost_vol_put,
1594         .tlv = { .p = cs421x_speaker_boost_db_scale },
1595 };
1596
1597 static void cs4210_pinmux_init(struct hda_codec *codec)
1598 {
1599         struct cs_spec *spec = codec->spec;
1600         unsigned int def_conf, coef;
1601
1602         /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1603         coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1604
1605         if (spec->gpio_mask)
1606                 coef |= 0x0008; /* B1,B2 are GPIOs */
1607         else
1608                 coef &= ~0x0008;
1609
1610         if (spec->sense_b)
1611                 coef |= 0x0010; /* B2 is SENSE_B, not inverted  */
1612         else
1613                 coef &= ~0x0010;
1614
1615         cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1616
1617         if ((spec->gpio_mask || spec->sense_b) &&
1618             is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1619
1620                 /*
1621                     GPIO or SENSE_B forced - disconnect the DMIC pin.
1622                 */
1623                 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1624                 def_conf &= ~AC_DEFCFG_PORT_CONN;
1625                 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1626                 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1627         }
1628 }
1629
1630 static void init_cs421x_digital(struct hda_codec *codec)
1631 {
1632         struct cs_spec *spec = codec->spec;
1633         struct auto_pin_cfg *cfg = &spec->autocfg;
1634         int i;
1635
1636
1637         for (i = 0; i < cfg->dig_outs; i++) {
1638                 hda_nid_t nid = cfg->dig_out_pins[i];
1639                 if (!cfg->speaker_outs)
1640                         continue;
1641                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1642                         snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
1643                         spec->spdif_detect = 1;
1644                 }
1645         }
1646 }
1647
1648 static int cs421x_init(struct hda_codec *codec)
1649 {
1650         struct cs_spec *spec = codec->spec;
1651
1652         if (spec->vendor_nid == CS4210_VENDOR_NID) {
1653                 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1654                 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1655                 cs4210_pinmux_init(codec);
1656         }
1657
1658         if (spec->gpio_mask) {
1659                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1660                                     spec->gpio_mask);
1661                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1662                                     spec->gpio_dir);
1663                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1664                                     spec->gpio_data);
1665         }
1666
1667         init_output(codec);
1668         init_input(codec);
1669         init_cs421x_digital(codec);
1670         snd_hda_jack_report_sync(codec);
1671
1672         return 0;
1673 }
1674
1675 /*
1676  * CS4210 Input MUX (1 ADC)
1677  */
1678 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1679                                         struct snd_ctl_elem_info *uinfo)
1680 {
1681         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1682         struct cs_spec *spec = codec->spec;
1683
1684         return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1685 }
1686
1687 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1688                                         struct snd_ctl_elem_value *ucontrol)
1689 {
1690         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1691         struct cs_spec *spec = codec->spec;
1692
1693         ucontrol->value.enumerated.item[0] = spec->cur_input;
1694         return 0;
1695 }
1696
1697 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1698                                         struct snd_ctl_elem_value *ucontrol)
1699 {
1700         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1701         struct cs_spec *spec = codec->spec;
1702
1703         return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1704                                 spec->adc_nid[0], &spec->cur_input);
1705
1706 }
1707
1708 static struct snd_kcontrol_new cs421x_capture_source = {
1709
1710         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1711         .name = "Capture Source",
1712         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1713         .info = cs421x_mux_enum_info,
1714         .get = cs421x_mux_enum_get,
1715         .put = cs421x_mux_enum_put,
1716 };
1717
1718 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1719 {
1720         struct cs_spec *spec = codec->spec;
1721         struct auto_pin_cfg *cfg = &spec->autocfg;
1722         const struct hda_input_mux *imux = &spec->input_mux;
1723         hda_nid_t pin = cfg->inputs[item].pin;
1724         struct snd_kcontrol *kctl;
1725         u32 caps;
1726
1727         if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1728                 return 0;
1729
1730         caps = query_amp_caps(codec, pin, HDA_INPUT);
1731         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1732         if (caps <= 1)
1733                 return 0;
1734
1735         return add_volume(codec,  imux->items[item].label, 0,
1736                           HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1737 }
1738
1739 /* add a (input-boost) volume control to the given input pin */
1740 static int build_cs421x_input(struct hda_codec *codec)
1741 {
1742         struct cs_spec *spec = codec->spec;
1743         struct auto_pin_cfg *cfg = &spec->autocfg;
1744         struct hda_input_mux *imux = &spec->input_mux;
1745         int i, err, type_idx;
1746         const char *label;
1747
1748         if (!spec->num_inputs)
1749                 return 0;
1750
1751         /* make bind-capture */
1752         spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1753         spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1754         for (i = 0; i < 2; i++) {
1755                 struct snd_kcontrol *kctl;
1756                 int n;
1757                 if (!spec->capture_bind[i])
1758                         return -ENOMEM;
1759                 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1760                 if (!kctl)
1761                         return -ENOMEM;
1762                 kctl->private_value = (long)spec->capture_bind[i];
1763                 err = snd_hda_ctl_add(codec, 0, kctl);
1764                 if (err < 0)
1765                         return err;
1766                 for (n = 0; n < AUTO_PIN_LAST; n++) {
1767                         if (!spec->adc_nid[n])
1768                                 continue;
1769                         err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1770                         if (err < 0)
1771                                 return err;
1772                 }
1773         }
1774
1775         /* Add Input MUX Items + Capture Volume/Switch */
1776         for (i = 0; i < spec->num_inputs; i++) {
1777                 label = hda_get_autocfg_input_label(codec, cfg, i);
1778                 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1779
1780                 err = cs421x_add_input_volume_control(codec, i);
1781                 if (err < 0)
1782                         return err;
1783         }
1784
1785         /*
1786             Add 'Capture Source' Switch if
1787                 * 2 inputs and no mic detec
1788                 * 3 inputs
1789         */
1790         if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1791             (spec->num_inputs == 3)) {
1792
1793                 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1794                               snd_ctl_new1(&cs421x_capture_source, codec));
1795                 if (err < 0)
1796                         return err;
1797         }
1798
1799         return 0;
1800 }
1801
1802 /* Single DAC (Mute/Gain) */
1803 static int build_cs421x_output(struct hda_codec *codec)
1804 {
1805         hda_nid_t dac = CS4210_DAC_NID;
1806         struct cs_spec *spec = codec->spec;
1807         struct auto_pin_cfg *cfg = &spec->autocfg;
1808         struct snd_kcontrol *kctl;
1809         int err;
1810         char *name = "Master";
1811
1812         fix_volume_caps(codec, dac);
1813
1814         err = add_mute(codec, name, 0,
1815                         HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1816         if (err < 0)
1817                 return err;
1818
1819         err = add_volume(codec, name, 0,
1820                         HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1821         if (err < 0)
1822                 return err;
1823
1824         if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
1825                 err = snd_hda_ctl_add(codec, 0,
1826                         snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1827                 if (err < 0)
1828                         return err;
1829         }
1830         return err;
1831 }
1832
1833 static int cs421x_build_controls(struct hda_codec *codec)
1834 {
1835         struct cs_spec *spec = codec->spec;
1836         int err;
1837
1838         err = build_cs421x_output(codec);
1839         if (err < 0)
1840                 return err;
1841         err = build_cs421x_input(codec);
1842         if (err < 0)
1843                 return err;
1844         err = build_digital_output(codec);
1845         if (err < 0)
1846                 return err;
1847         err =  cs421x_init(codec);
1848         if (err < 0)
1849                 return err;
1850
1851         err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1852         if (err < 0)
1853                 return err;
1854
1855         return 0;
1856 }
1857
1858 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1859 {
1860         switch (snd_hda_jack_get_action(codec, res >> 26)) {
1861         case HP_EVENT:
1862         case SPDIF_EVENT:
1863                 cs_automute(codec);
1864                 break;
1865
1866         case MIC_EVENT:
1867                 cs_automic(codec);
1868                 break;
1869         }
1870         snd_hda_jack_report_sync(codec);
1871 }
1872
1873 static int parse_cs421x_input(struct hda_codec *codec)
1874 {
1875         struct cs_spec *spec = codec->spec;
1876         struct auto_pin_cfg *cfg = &spec->autocfg;
1877         int i;
1878
1879         for (i = 0; i < cfg->num_inputs; i++) {
1880                 hda_nid_t pin = cfg->inputs[i].pin;
1881                 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1882                 spec->cur_input = spec->last_input = i;
1883                 spec->num_inputs++;
1884
1885                 /* check whether the automatic mic switch is available */
1886                 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1887                         spec->mic_detect = 1;
1888                         spec->automic_idx = i;
1889                 }
1890         }
1891         return 0;
1892 }
1893
1894 static int cs421x_parse_auto_config(struct hda_codec *codec)
1895 {
1896         struct cs_spec *spec = codec->spec;
1897         int err;
1898
1899         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1900         if (err < 0)
1901                 return err;
1902         err = parse_output(codec);
1903         if (err < 0)
1904                 return err;
1905         err = parse_cs421x_input(codec);
1906         if (err < 0)
1907                 return err;
1908         err = parse_digital_output(codec);
1909         if (err < 0)
1910                 return err;
1911         return 0;
1912 }
1913
1914 #ifdef CONFIG_PM
1915 /*
1916         Manage PDREF, when transitioning to D3hot
1917         (DAC,ADC) -> D3, PDREF=1, AFG->D3
1918 */
1919 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1920 {
1921         struct cs_spec *spec = codec->spec;
1922         unsigned int coef;
1923
1924         snd_hda_shutup_pins(codec);
1925
1926         snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1927                             AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
1928         snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1929                             AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
1930
1931         if (spec->vendor_nid == CS4210_VENDOR_NID) {
1932                 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1933                 coef |= 0x0004; /* PDREF */
1934                 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1935         }
1936
1937         return 0;
1938 }
1939 #endif
1940
1941 static struct hda_codec_ops cs421x_patch_ops = {
1942         .build_controls = cs421x_build_controls,
1943         .build_pcms = cs_build_pcms,
1944         .init = cs421x_init,
1945         .free = cs_free,
1946         .unsol_event = cs421x_unsol_event,
1947 #ifdef CONFIG_PM
1948         .suspend = cs421x_suspend,
1949 #endif
1950 };
1951
1952 static int patch_cs4210(struct hda_codec *codec)
1953 {
1954         struct cs_spec *spec;
1955         int err;
1956
1957         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1958         if (!spec)
1959                 return -ENOMEM;
1960         codec->spec = spec;
1961
1962         spec->vendor_nid = CS4210_VENDOR_NID;
1963
1964         spec->board_config =
1965                 snd_hda_check_board_config(codec, CS421X_MODELS,
1966                                            cs421x_models, cs421x_cfg_tbl);
1967         if (spec->board_config >= 0)
1968                 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1969         /*
1970             Setup GPIO/SENSE for each board (if used)
1971         */
1972         switch (spec->board_config) {
1973         case CS421X_CDB4210:
1974                 snd_printd("CS4210 board: %s\n",
1975                         cs421x_models[spec->board_config]);
1976 /*              spec->gpio_mask = 3;
1977                 spec->gpio_dir = 3;
1978                 spec->gpio_data = 3;
1979 */
1980                 spec->sense_b = 1;
1981
1982                 break;
1983         }
1984
1985         /*
1986             Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1987             is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1988             is disabled.
1989         */
1990         cs4210_pinmux_init(codec);
1991
1992         err = cs421x_parse_auto_config(codec);
1993         if (err < 0)
1994                 goto error;
1995
1996         codec->patch_ops = cs421x_patch_ops;
1997
1998         return 0;
1999
2000  error:
2001         kfree(codec->spec);
2002         codec->spec = NULL;
2003         return err;
2004 }
2005
2006 static int patch_cs4213(struct hda_codec *codec)
2007 {
2008         struct cs_spec *spec;
2009         int err;
2010
2011         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2012         if (!spec)
2013                 return -ENOMEM;
2014         codec->spec = spec;
2015
2016         spec->vendor_nid = CS4213_VENDOR_NID;
2017
2018         err = cs421x_parse_auto_config(codec);
2019         if (err < 0)
2020                 goto error;
2021
2022         codec->patch_ops = cs421x_patch_ops;
2023         return 0;
2024
2025  error:
2026         kfree(codec->spec);
2027         codec->spec = NULL;
2028         return err;
2029 }
2030
2031
2032 /*
2033  * patch entries
2034  */
2035 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
2036         { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2037         { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
2038         { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2039         { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
2040         {} /* terminator */
2041 };
2042
2043 MODULE_ALIAS("snd-hda-codec-id:10134206");
2044 MODULE_ALIAS("snd-hda-codec-id:10134207");
2045 MODULE_ALIAS("snd-hda-codec-id:10134210");
2046 MODULE_ALIAS("snd-hda-codec-id:10134213");
2047
2048 MODULE_LICENSE("GPL");
2049 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2050
2051 static struct hda_codec_preset_list cirrus_list = {
2052         .preset = snd_hda_preset_cirrus,
2053         .owner = THIS_MODULE,
2054 };
2055
2056 static int __init patch_cirrus_init(void)
2057 {
2058         return snd_hda_add_codec_preset(&cirrus_list);
2059 }
2060
2061 static void __exit patch_cirrus_exit(void)
2062 {
2063         snd_hda_delete_codec_preset(&cirrus_list);
2064 }
2065
2066 module_init(patch_cirrus_init)
2067 module_exit(patch_cirrus_exit)