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