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