ALSA: hda: Virtualize SPDIF out controls
[linux-2.6.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
29 /*
30  */
31
32 struct cs_spec {
33         int board_config;
34         struct auto_pin_cfg autocfg;
35         struct hda_multi_out multiout;
36         struct snd_kcontrol *vmaster_sw;
37         struct snd_kcontrol *vmaster_vol;
38
39         hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
40         hda_nid_t slave_dig_outs[2];
41
42         unsigned int input_idx[AUTO_PIN_LAST];
43         unsigned int capsrc_idx[AUTO_PIN_LAST];
44         hda_nid_t adc_nid[AUTO_PIN_LAST];
45         unsigned int adc_idx[AUTO_PIN_LAST];
46         unsigned int num_inputs;
47         unsigned int cur_input;
48         unsigned int automic_idx;
49         hda_nid_t cur_adc;
50         unsigned int cur_adc_stream_tag;
51         unsigned int cur_adc_format;
52         hda_nid_t dig_in;
53
54         const struct hda_bind_ctls *capture_bind[2];
55
56         unsigned int gpio_mask;
57         unsigned int gpio_dir;
58         unsigned int gpio_data;
59
60         struct hda_pcm pcm_rec[2];      /* PCM information */
61
62         unsigned int hp_detect:1;
63         unsigned int mic_detect:1;
64 };
65
66 /* available models */
67 enum {
68         CS420X_MBP53,
69         CS420X_MBP55,
70         CS420X_IMAC27,
71         CS420X_AUTO,
72         CS420X_MODELS
73 };
74
75 /* Vendor-specific processing widget */
76 #define CS420X_VENDOR_NID       0x11
77 #define CS_DIG_OUT1_PIN_NID     0x10
78 #define CS_DIG_OUT2_PIN_NID     0x15
79 #define CS_DMIC1_PIN_NID        0x12
80 #define CS_DMIC2_PIN_NID        0x0e
81
82 /* coef indices */
83 #define IDX_SPDIF_STAT          0x0000
84 #define IDX_SPDIF_CTL           0x0001
85 #define IDX_ADC_CFG             0x0002
86 /* SZC bitmask, 4 modes below:
87  * 0 = immediate,
88  * 1 = digital immediate, analog zero-cross
89  * 2 = digtail & analog soft-ramp
90  * 3 = digital soft-ramp, analog zero-cross
91  */
92 #define   CS_COEF_ADC_SZC_MASK          (3 << 0)
93 #define   CS_COEF_ADC_MIC_SZC_MODE      (3 << 0) /* SZC setup for mic */
94 #define   CS_COEF_ADC_LI_SZC_MODE       (3 << 0) /* SZC setup for line-in */
95 /* PGA mode: 0 = differential, 1 = signle-ended */
96 #define   CS_COEF_ADC_MIC_PGA_MODE      (1 << 5) /* PGA setup for mic */
97 #define   CS_COEF_ADC_LI_PGA_MODE       (1 << 6) /* PGA setup for line-in */
98 #define IDX_DAC_CFG             0x0003
99 /* SZC bitmask, 4 modes below:
100  * 0 = Immediate
101  * 1 = zero-cross
102  * 2 = soft-ramp
103  * 3 = soft-ramp on zero-cross
104  */
105 #define   CS_COEF_DAC_HP_SZC_MODE       (3 << 0) /* nid 0x02 */
106 #define   CS_COEF_DAC_LO_SZC_MODE       (3 << 2) /* nid 0x03 */
107 #define   CS_COEF_DAC_SPK_SZC_MODE      (3 << 4) /* nid 0x04 */
108
109 #define IDX_BEEP_CFG            0x0004
110 /* 0x0008 - test reg key */
111 /* 0x0009 - 0x0014 -> 12 test regs */
112 /* 0x0015 - visibility reg */
113
114
115 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
116 {
117         snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
118                             AC_VERB_SET_COEF_INDEX, idx);
119         return snd_hda_codec_read(codec, CS420X_VENDOR_NID, 0,
120                                   AC_VERB_GET_PROC_COEF, 0);
121 }
122
123 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
124                                       unsigned int coef)
125 {
126         snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
127                             AC_VERB_SET_COEF_INDEX, idx);
128         snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
129                             AC_VERB_SET_PROC_COEF, coef);
130 }
131
132
133 #define HP_EVENT        1
134 #define MIC_EVENT       2
135
136 /*
137  * PCM callbacks
138  */
139 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
140                                 struct hda_codec *codec,
141                                 struct snd_pcm_substream *substream)
142 {
143         struct cs_spec *spec = codec->spec;
144         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
145                                              hinfo);
146 }
147
148 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
149                                    struct hda_codec *codec,
150                                    unsigned int stream_tag,
151                                    unsigned int format,
152                                    struct snd_pcm_substream *substream)
153 {
154         struct cs_spec *spec = codec->spec;
155         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
156                                                 stream_tag, format, substream);
157 }
158
159 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
160                                    struct hda_codec *codec,
161                                    struct snd_pcm_substream *substream)
162 {
163         struct cs_spec *spec = codec->spec;
164         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
165 }
166
167 /*
168  * Digital out
169  */
170 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
171                                     struct hda_codec *codec,
172                                     struct snd_pcm_substream *substream)
173 {
174         struct cs_spec *spec = codec->spec;
175         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
176 }
177
178 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
179                                      struct hda_codec *codec,
180                                      struct snd_pcm_substream *substream)
181 {
182         struct cs_spec *spec = codec->spec;
183         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
184 }
185
186 static int cs_dig_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_dig_prepare(codec, &spec->multiout, stream_tag,
194                                              format, substream);
195 }
196
197 static int cs_dig_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_dig_cleanup(codec, &spec->multiout);
203 }
204
205 /*
206  * Analog capture
207  */
208 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
209                                   struct hda_codec *codec,
210                                   unsigned int stream_tag,
211                                   unsigned int format,
212                                   struct snd_pcm_substream *substream)
213 {
214         struct cs_spec *spec = codec->spec;
215         spec->cur_adc = spec->adc_nid[spec->cur_input];
216         spec->cur_adc_stream_tag = stream_tag;
217         spec->cur_adc_format = format;
218         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
219         return 0;
220 }
221
222 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
223                                   struct hda_codec *codec,
224                                   struct snd_pcm_substream *substream)
225 {
226         struct cs_spec *spec = codec->spec;
227         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
228         spec->cur_adc = 0;
229         return 0;
230 }
231
232 /*
233  */
234 static const struct hda_pcm_stream cs_pcm_analog_playback = {
235         .substreams = 1,
236         .channels_min = 2,
237         .channels_max = 2,
238         .ops = {
239                 .open = cs_playback_pcm_open,
240                 .prepare = cs_playback_pcm_prepare,
241                 .cleanup = cs_playback_pcm_cleanup
242         },
243 };
244
245 static const struct hda_pcm_stream cs_pcm_analog_capture = {
246         .substreams = 1,
247         .channels_min = 2,
248         .channels_max = 2,
249         .ops = {
250                 .prepare = cs_capture_pcm_prepare,
251                 .cleanup = cs_capture_pcm_cleanup
252         },
253 };
254
255 static const struct hda_pcm_stream cs_pcm_digital_playback = {
256         .substreams = 1,
257         .channels_min = 2,
258         .channels_max = 2,
259         .ops = {
260                 .open = cs_dig_playback_pcm_open,
261                 .close = cs_dig_playback_pcm_close,
262                 .prepare = cs_dig_playback_pcm_prepare,
263                 .cleanup = cs_dig_playback_pcm_cleanup
264         },
265 };
266
267 static const struct hda_pcm_stream cs_pcm_digital_capture = {
268         .substreams = 1,
269         .channels_min = 2,
270         .channels_max = 2,
271 };
272
273 static int cs_build_pcms(struct hda_codec *codec)
274 {
275         struct cs_spec *spec = codec->spec;
276         struct hda_pcm *info = spec->pcm_rec;
277
278         codec->pcm_info = info;
279         codec->num_pcms = 0;
280
281         info->name = "Cirrus Analog";
282         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
283         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
284         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
285                 spec->multiout.max_channels;
286         info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
287         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
288                 spec->adc_nid[spec->cur_input];
289         codec->num_pcms++;
290
291         if (!spec->multiout.dig_out_nid && !spec->dig_in)
292                 return 0;
293
294         info++;
295         info->name = "Cirrus Digital";
296         info->pcm_type = spec->autocfg.dig_out_type[0];
297         if (!info->pcm_type)
298                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
299         if (spec->multiout.dig_out_nid) {
300                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
301                         cs_pcm_digital_playback;
302                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
303                         spec->multiout.dig_out_nid;
304         }
305         if (spec->dig_in) {
306                 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
307                         cs_pcm_digital_capture;
308                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
309         }
310         codec->num_pcms++;
311
312         return 0;
313 }
314
315 /*
316  * parse codec topology
317  */
318
319 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
320 {
321         hda_nid_t dac;
322         if (!pin)
323                 return 0;
324         if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
325                 return 0;
326         return dac;
327 }
328
329 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
330 {
331         struct cs_spec *spec = codec->spec;
332         struct auto_pin_cfg *cfg = &spec->autocfg;
333         hda_nid_t pin = cfg->inputs[idx].pin;
334         unsigned int val;
335         if (!is_jack_detectable(codec, pin))
336                 return 0;
337         val = snd_hda_codec_get_pincfg(codec, pin);
338         return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
339 }
340
341 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
342                          unsigned int *idxp)
343 {
344         int i;
345         hda_nid_t nid;
346
347         nid = codec->start_nid;
348         for (i = 0; i < codec->num_nodes; i++, nid++) {
349                 hda_nid_t pins[2];
350                 unsigned int type;
351                 int j, nums;
352                 type = get_wcaps_type(get_wcaps(codec, nid));
353                 if (type != AC_WID_AUD_IN)
354                         continue;
355                 nums = snd_hda_get_connections(codec, nid, pins,
356                                                ARRAY_SIZE(pins));
357                 if (nums <= 0)
358                         continue;
359                 for (j = 0; j < nums; j++) {
360                         if (pins[j] == pin) {
361                                 *idxp = j;
362                                 return nid;
363                         }
364                 }
365         }
366         return 0;
367 }
368
369 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
370 {
371         unsigned int val;
372         val = snd_hda_codec_get_pincfg(codec, nid);
373         return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
374 }
375
376 static int parse_output(struct hda_codec *codec)
377 {
378         struct cs_spec *spec = codec->spec;
379         struct auto_pin_cfg *cfg = &spec->autocfg;
380         int i, extra_nids;
381         hda_nid_t dac;
382
383         for (i = 0; i < cfg->line_outs; i++) {
384                 dac = get_dac(codec, cfg->line_out_pins[i]);
385                 if (!dac)
386                         break;
387                 spec->dac_nid[i] = dac;
388         }
389         spec->multiout.num_dacs = i;
390         spec->multiout.dac_nids = spec->dac_nid;
391         spec->multiout.max_channels = i * 2;
392
393         /* add HP and speakers */
394         extra_nids = 0;
395         for (i = 0; i < cfg->hp_outs; i++) {
396                 dac = get_dac(codec, cfg->hp_pins[i]);
397                 if (!dac)
398                         break;
399                 if (!i)
400                         spec->multiout.hp_nid = dac;
401                 else
402                         spec->multiout.extra_out_nid[extra_nids++] = dac;
403         }
404         for (i = 0; i < cfg->speaker_outs; i++) {
405                 dac = get_dac(codec, cfg->speaker_pins[i]);
406                 if (!dac)
407                         break;
408                 spec->multiout.extra_out_nid[extra_nids++] = dac;
409         }
410
411         if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
412                 cfg->speaker_outs = cfg->line_outs;
413                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
414                        sizeof(cfg->speaker_pins));
415                 cfg->line_outs = 0;
416         }
417
418         return 0;
419 }
420
421 static int parse_input(struct hda_codec *codec)
422 {
423         struct cs_spec *spec = codec->spec;
424         struct auto_pin_cfg *cfg = &spec->autocfg;
425         int i;
426
427         for (i = 0; i < cfg->num_inputs; i++) {
428                 hda_nid_t pin = cfg->inputs[i].pin;
429                 spec->input_idx[spec->num_inputs] = i;
430                 spec->capsrc_idx[i] = spec->num_inputs++;
431                 spec->cur_input = i;
432                 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
433         }
434         if (!spec->num_inputs)
435                 return 0;
436
437         /* check whether the automatic mic switch is available */
438         if (spec->num_inputs == 2 &&
439             cfg->inputs[0].type == AUTO_PIN_MIC &&
440             cfg->inputs[1].type == AUTO_PIN_MIC) {
441                 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
442                         if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
443                                 spec->mic_detect = 1;
444                                 spec->automic_idx = 0;
445                         }
446                 } else {
447                         if (is_ext_mic(codec, cfg->inputs[1].pin)) {
448                                 spec->mic_detect = 1;
449                                 spec->automic_idx = 1;
450                         }
451                 }
452         }
453         return 0;
454 }
455
456
457 static int parse_digital_output(struct hda_codec *codec)
458 {
459         struct cs_spec *spec = codec->spec;
460         struct auto_pin_cfg *cfg = &spec->autocfg;
461         hda_nid_t nid;
462
463         if (!cfg->dig_outs)
464                 return 0;
465         if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
466                 return 0;
467         spec->multiout.dig_out_nid = nid;
468         spec->multiout.share_spdif = 1;
469         if (cfg->dig_outs > 1 &&
470             snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
471                 spec->slave_dig_outs[0] = nid;
472                 codec->slave_dig_outs = spec->slave_dig_outs;
473         }
474         return 0;
475 }
476
477 static int parse_digital_input(struct hda_codec *codec)
478 {
479         struct cs_spec *spec = codec->spec;
480         struct auto_pin_cfg *cfg = &spec->autocfg;
481         int idx;
482
483         if (cfg->dig_in_pin)
484                 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
485         return 0;
486 }
487
488 /*
489  * create mixer controls
490  */
491
492 static const char * const dir_sfx[2] = { "Playback", "Capture" };
493
494 static int add_mute(struct hda_codec *codec, const char *name, int index,
495                     unsigned int pval, int dir, struct snd_kcontrol **kctlp)
496 {
497         char tmp[44];
498         struct snd_kcontrol_new knew =
499                 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
500         knew.private_value = pval;
501         snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
502         *kctlp = snd_ctl_new1(&knew, codec);
503         (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
504         return snd_hda_ctl_add(codec, 0, *kctlp);
505 }
506
507 static int add_volume(struct hda_codec *codec, const char *name,
508                       int index, unsigned int pval, int dir,
509                       struct snd_kcontrol **kctlp)
510 {
511         char tmp[32];
512         struct snd_kcontrol_new knew =
513                 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
514         knew.private_value = pval;
515         snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
516         *kctlp = snd_ctl_new1(&knew, codec);
517         (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
518         return snd_hda_ctl_add(codec, 0, *kctlp);
519 }
520
521 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
522 {
523         unsigned int caps;
524
525         /* set the upper-limit for mixer amp to 0dB */
526         caps = query_amp_caps(codec, dac, HDA_OUTPUT);
527         caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
528         caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
529                 << AC_AMPCAP_NUM_STEPS_SHIFT;
530         snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
531 }
532
533 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
534 {
535         struct cs_spec *spec = codec->spec;
536         unsigned int tlv[4];
537         int err;
538
539         spec->vmaster_sw =
540                 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
541         err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
542         if (err < 0)
543                 return err;
544
545         snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
546         spec->vmaster_vol =
547                 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
548         err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
549         if (err < 0)
550                 return err;
551         return 0;
552 }
553
554 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
555                       int num_ctls, int type)
556 {
557         struct cs_spec *spec = codec->spec;
558         const char *name;
559         int err, index;
560         struct snd_kcontrol *kctl;
561         static const char * const speakers[] = {
562                 "Front Speaker", "Surround Speaker", "Bass Speaker"
563         };
564         static const char * const line_outs[] = {
565                 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
566         };
567
568         fix_volume_caps(codec, dac);
569         if (!spec->vmaster_sw) {
570                 err = add_vmaster(codec, dac);
571                 if (err < 0)
572                         return err;
573         }
574
575         index = 0;
576         switch (type) {
577         case AUTO_PIN_HP_OUT:
578                 name = "Headphone";
579                 index = idx;
580                 break;
581         case AUTO_PIN_SPEAKER_OUT:
582                 if (num_ctls > 1)
583                         name = speakers[idx];
584                 else
585                         name = "Speaker";
586                 break;
587         default:
588                 if (num_ctls > 1)
589                         name = line_outs[idx];
590                 else
591                         name = "Line-Out";
592                 break;
593         }
594
595         err = add_mute(codec, name, index,
596                        HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
597         if (err < 0)
598                 return err;
599         err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
600         if (err < 0)
601                 return err;
602
603         err = add_volume(codec, name, index,
604                          HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
605         if (err < 0)
606                 return err;
607         err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
608         if (err < 0)
609                 return err;
610
611         return 0;
612 }               
613
614 static int build_output(struct hda_codec *codec)
615 {
616         struct cs_spec *spec = codec->spec;
617         struct auto_pin_cfg *cfg = &spec->autocfg;
618         int i, err;
619
620         for (i = 0; i < cfg->line_outs; i++) {
621                 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
622                                  i, cfg->line_outs, cfg->line_out_type);
623                 if (err < 0)
624                         return err;
625         }
626         for (i = 0; i < cfg->hp_outs; i++) {
627                 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
628                                  i, cfg->hp_outs, AUTO_PIN_HP_OUT);
629                 if (err < 0)
630                         return err;
631         }
632         for (i = 0; i < cfg->speaker_outs; i++) {
633                 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
634                                  i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
635                 if (err < 0)
636                         return err;
637         }
638         return 0;
639 }
640
641 /*
642  */
643
644 static const struct snd_kcontrol_new cs_capture_ctls[] = {
645         HDA_BIND_SW("Capture Switch", 0),
646         HDA_BIND_VOL("Capture Volume", 0),
647 };
648
649 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
650                             int force)
651 {
652         struct cs_spec *spec = codec->spec;
653         
654         if (spec->cur_input == idx && !force)
655                 return 0;
656         if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
657                 /* stream is running, let's swap the current ADC */
658                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
659                 spec->cur_adc = spec->adc_nid[idx];
660                 snd_hda_codec_setup_stream(codec, spec->cur_adc,
661                                            spec->cur_adc_stream_tag, 0,
662                                            spec->cur_adc_format);
663         }
664         snd_hda_codec_write(codec, spec->cur_adc, 0,
665                             AC_VERB_SET_CONNECT_SEL,
666                             spec->adc_idx[idx]);
667         spec->cur_input = idx;
668         return 1;
669 }
670
671 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
672                                   struct snd_ctl_elem_info *uinfo)
673 {
674         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
675         struct cs_spec *spec = codec->spec;
676         struct auto_pin_cfg *cfg = &spec->autocfg;
677         unsigned int idx;
678
679         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
680         uinfo->count = 1;
681         uinfo->value.enumerated.items = spec->num_inputs;
682         if (uinfo->value.enumerated.item >= spec->num_inputs)
683                 uinfo->value.enumerated.item = spec->num_inputs - 1;
684         idx = spec->input_idx[uinfo->value.enumerated.item];
685         strcpy(uinfo->value.enumerated.name,
686                hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
687         return 0;
688 }
689
690 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
691                                  struct snd_ctl_elem_value *ucontrol)
692 {
693         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
694         struct cs_spec *spec = codec->spec;
695         ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
696         return 0;
697 }
698
699 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
700                                  struct snd_ctl_elem_value *ucontrol)
701 {
702         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
703         struct cs_spec *spec = codec->spec;
704         unsigned int idx = ucontrol->value.enumerated.item[0];
705
706         if (idx >= spec->num_inputs)
707                 return -EINVAL;
708         idx = spec->input_idx[idx];
709         return change_cur_input(codec, idx, 0);
710 }
711
712 static const struct snd_kcontrol_new cs_capture_source = {
713         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
714         .name = "Capture Source",
715         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
716         .info = cs_capture_source_info,
717         .get = cs_capture_source_get,
718         .put = cs_capture_source_put,
719 };
720
721 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
722                                                struct hda_ctl_ops *ops)
723 {
724         struct cs_spec *spec = codec->spec;
725         struct hda_bind_ctls *bind;
726         int i, n;
727
728         bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
729                        GFP_KERNEL);
730         if (!bind)
731                 return NULL;
732         bind->ops = ops;
733         n = 0;
734         for (i = 0; i < AUTO_PIN_LAST; i++) {
735                 if (!spec->adc_nid[i])
736                         continue;
737                 bind->values[n++] =
738                         HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
739                                             spec->adc_idx[i], HDA_INPUT);
740         }
741         return bind;
742 }
743
744 /* add a (input-boost) volume control to the given input pin */
745 static int add_input_volume_control(struct hda_codec *codec,
746                                     struct auto_pin_cfg *cfg,
747                                     int item)
748 {
749         hda_nid_t pin = cfg->inputs[item].pin;
750         u32 caps;
751         const char *label;
752         struct snd_kcontrol *kctl;
753                 
754         if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
755                 return 0;
756         caps = query_amp_caps(codec, pin, HDA_INPUT);
757         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
758         if (caps <= 1)
759                 return 0;
760         label = hda_get_autocfg_input_label(codec, cfg, item);
761         return add_volume(codec, label, 0,
762                           HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
763 }
764
765 static int build_input(struct hda_codec *codec)
766 {
767         struct cs_spec *spec = codec->spec;
768         int i, err;
769
770         if (!spec->num_inputs)
771                 return 0;
772
773         /* make bind-capture */
774         spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
775         spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
776         for (i = 0; i < 2; i++) {
777                 struct snd_kcontrol *kctl;
778                 int n;
779                 if (!spec->capture_bind[i])
780                         return -ENOMEM;
781                 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
782                 if (!kctl)
783                         return -ENOMEM;
784                 kctl->private_value = (long)spec->capture_bind[i];
785                 err = snd_hda_ctl_add(codec, 0, kctl);
786                 if (err < 0)
787                         return err;
788                 for (n = 0; n < AUTO_PIN_LAST; n++) {
789                         if (!spec->adc_nid[n])
790                                 continue;
791                         err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
792                         if (err < 0)
793                                 return err;
794                 }
795         }
796         
797         if (spec->num_inputs > 1 && !spec->mic_detect) {
798                 err = snd_hda_ctl_add(codec, 0,
799                                       snd_ctl_new1(&cs_capture_source, codec));
800                 if (err < 0)
801                         return err;
802         }
803
804         for (i = 0; i < spec->num_inputs; i++) {
805                 err = add_input_volume_control(codec, &spec->autocfg, i);
806                 if (err < 0)
807                         return err;
808         }
809
810         return 0;
811 }
812
813 /*
814  */
815
816 static int build_digital_output(struct hda_codec *codec)
817 {
818         struct cs_spec *spec = codec->spec;
819         int err;
820
821         if (!spec->multiout.dig_out_nid)
822                 return 0;
823
824         err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
825                                             spec->multiout.dig_out_nid);
826         if (err < 0)
827                 return err;
828         err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
829         if (err < 0)
830                 return err;
831         return 0;
832 }
833
834 static int build_digital_input(struct hda_codec *codec)
835 {
836         struct cs_spec *spec = codec->spec;
837         if (spec->dig_in)
838                 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
839         return 0;
840 }
841
842 /*
843  * auto-mute and auto-mic switching
844  */
845
846 static void cs_automute(struct hda_codec *codec)
847 {
848         struct cs_spec *spec = codec->spec;
849         struct auto_pin_cfg *cfg = &spec->autocfg;
850         unsigned int hp_present;
851         hda_nid_t nid;
852         int i;
853
854         hp_present = 0;
855         for (i = 0; i < cfg->hp_outs; i++) {
856                 nid = cfg->hp_pins[i];
857                 if (!is_jack_detectable(codec, nid))
858                         continue;
859                 hp_present = snd_hda_jack_detect(codec, nid);
860                 if (hp_present)
861                         break;
862         }
863         for (i = 0; i < cfg->speaker_outs; i++) {
864                 nid = cfg->speaker_pins[i];
865                 snd_hda_codec_write(codec, nid, 0,
866                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
867                                     hp_present ? 0 : PIN_OUT);
868         }
869         if (spec->board_config == CS420X_MBP53 ||
870             spec->board_config == CS420X_MBP55 ||
871             spec->board_config == CS420X_IMAC27) {
872                 unsigned int gpio = hp_present ? 0x02 : 0x08;
873                 snd_hda_codec_write(codec, 0x01, 0,
874                                     AC_VERB_SET_GPIO_DATA, gpio);
875         }
876 }
877
878 static void cs_automic(struct hda_codec *codec)
879 {
880         struct cs_spec *spec = codec->spec;
881         struct auto_pin_cfg *cfg = &spec->autocfg;
882         hda_nid_t nid;
883         unsigned int present;
884         
885         nid = cfg->inputs[spec->automic_idx].pin;
886         present = snd_hda_jack_detect(codec, nid);
887         if (present)
888                 change_cur_input(codec, spec->automic_idx, 0);
889         else
890                 change_cur_input(codec, !spec->automic_idx, 0);
891 }
892
893 /*
894  */
895
896 static void init_output(struct hda_codec *codec)
897 {
898         struct cs_spec *spec = codec->spec;
899         struct auto_pin_cfg *cfg = &spec->autocfg;
900         int i;
901
902         /* mute first */
903         for (i = 0; i < spec->multiout.num_dacs; i++)
904                 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
905                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
906         if (spec->multiout.hp_nid)
907                 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
908                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
909         for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
910                 if (!spec->multiout.extra_out_nid[i])
911                         break;
912                 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
913                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
914         }
915
916         /* set appropriate pin controls */
917         for (i = 0; i < cfg->line_outs; i++)
918                 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
919                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
920         for (i = 0; i < cfg->hp_outs; i++) {
921                 hda_nid_t nid = cfg->hp_pins[i];
922                 snd_hda_codec_write(codec, nid, 0,
923                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
924                 if (!cfg->speaker_outs)
925                         continue;
926                 if (is_jack_detectable(codec, nid)) {
927                         snd_hda_codec_write(codec, nid, 0,
928                                             AC_VERB_SET_UNSOLICITED_ENABLE,
929                                             AC_USRSP_EN | HP_EVENT);
930                         spec->hp_detect = 1;
931                 }
932         }
933         for (i = 0; i < cfg->speaker_outs; i++)
934                 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
935                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
936         if (spec->hp_detect)
937                 cs_automute(codec);
938 }
939
940 static void init_input(struct hda_codec *codec)
941 {
942         struct cs_spec *spec = codec->spec;
943         struct auto_pin_cfg *cfg = &spec->autocfg;
944         unsigned int coef;
945         int i;
946
947         for (i = 0; i < cfg->num_inputs; i++) {
948                 unsigned int ctl;
949                 hda_nid_t pin = cfg->inputs[i].pin;
950                 if (!spec->adc_nid[i])
951                         continue;
952                 /* set appropriate pin control and mute first */
953                 ctl = PIN_IN;
954                 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
955                         unsigned int caps = snd_hda_query_pin_caps(codec, pin);
956                         caps >>= AC_PINCAP_VREF_SHIFT;
957                         if (caps & AC_PINCAP_VREF_80)
958                                 ctl = PIN_VREF80;
959                 }
960                 snd_hda_codec_write(codec, pin, 0,
961                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
962                 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
963                                     AC_VERB_SET_AMP_GAIN_MUTE,
964                                     AMP_IN_MUTE(spec->adc_idx[i]));
965                 if (spec->mic_detect && spec->automic_idx == i)
966                         snd_hda_codec_write(codec, pin, 0,
967                                             AC_VERB_SET_UNSOLICITED_ENABLE,
968                                             AC_USRSP_EN | MIC_EVENT);
969         }
970         change_cur_input(codec, spec->cur_input, 1);
971         if (spec->mic_detect)
972                 cs_automic(codec);
973
974         coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
975         if (is_active_pin(codec, CS_DMIC2_PIN_NID))
976                 coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
977         if (is_active_pin(codec, CS_DMIC1_PIN_NID))
978                 coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0 
979                                  * No effect if SPDIF_OUT2 is selected in 
980                                  * IDX_SPDIF_CTL.
981                                   */
982         cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
983 }
984
985 static const struct hda_verb cs_coef_init_verbs[] = {
986         {0x11, AC_VERB_SET_PROC_STATE, 1},
987         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
988         {0x11, AC_VERB_SET_PROC_COEF,
989          (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
990           | 0x0040 /* Mute DACs on FIFO error */
991           | 0x1000 /* Enable DACs High Pass Filter */
992           | 0x0400 /* Disable Coefficient Auto increment */
993           )},
994         /* Beep */
995         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
996         {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
997
998         {} /* terminator */
999 };
1000
1001 /* Errata: CS4207 rev C0/C1/C2 Silicon
1002  *
1003  * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1004  *
1005  * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1006  * may be excessive (up to an additional 200 μA), which is most easily
1007  * observed while the part is being held in reset (RESET# active low).
1008  *
1009  * Root Cause: At initial powerup of the device, the logic that drives
1010  * the clock and write enable to the S/PDIF SRC RAMs is not properly
1011  * initialized.
1012  * Certain random patterns will cause a steady leakage current in those
1013  * RAM cells. The issue will resolve once the SRCs are used (turned on).
1014  *
1015  * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1016  * blocks, which will alleviate the issue.
1017  */
1018
1019 static const struct hda_verb cs_errata_init_verbs[] = {
1020         {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1021         {0x11, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
1022
1023         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1024         {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1025         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1026         {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1027         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1028         {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1029
1030         {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1031         {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1032
1033         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1034         {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1035         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1036         {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1037         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1038         {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1039         {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1040
1041 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1042         {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1043         {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1044         /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1045 #endif
1046
1047         {} /* terminator */
1048 };
1049
1050 /* SPDIF setup */
1051 static void init_digital(struct hda_codec *codec)
1052 {
1053         unsigned int coef;
1054
1055         coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1056         coef |= 0x0008; /* Replace with mute on error */
1057         if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1058                 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1059                                  * SPDIF_OUT2 is shared with GPIO1 and
1060                                  * DMIC_SDA2.
1061                                  */
1062         cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1063 }
1064
1065 static int cs_init(struct hda_codec *codec)
1066 {
1067         struct cs_spec *spec = codec->spec;
1068
1069         /* init_verb sequence for C0/C1/C2 errata*/
1070         snd_hda_sequence_write(codec, cs_errata_init_verbs);
1071
1072         snd_hda_sequence_write(codec, cs_coef_init_verbs);
1073
1074         if (spec->gpio_mask) {
1075                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1076                                     spec->gpio_mask);
1077                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1078                                     spec->gpio_dir);
1079                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1080                                     spec->gpio_data);
1081         }
1082
1083         init_output(codec);
1084         init_input(codec);
1085         init_digital(codec);
1086         return 0;
1087 }
1088
1089 static int cs_build_controls(struct hda_codec *codec)
1090 {
1091         int err;
1092
1093         err = build_output(codec);
1094         if (err < 0)
1095                 return err;
1096         err = build_input(codec);
1097         if (err < 0)
1098                 return err;
1099         err = build_digital_output(codec);
1100         if (err < 0)
1101                 return err;
1102         err = build_digital_input(codec);
1103         if (err < 0)
1104                 return err;
1105         return cs_init(codec);
1106 }
1107
1108 static void cs_free(struct hda_codec *codec)
1109 {
1110         struct cs_spec *spec = codec->spec;
1111         kfree(spec->capture_bind[0]);
1112         kfree(spec->capture_bind[1]);
1113         kfree(codec->spec);
1114 }
1115
1116 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1117 {
1118         switch ((res >> 26) & 0x7f) {
1119         case HP_EVENT:
1120                 cs_automute(codec);
1121                 break;
1122         case MIC_EVENT:
1123                 cs_automic(codec);
1124                 break;
1125         }
1126 }
1127
1128 static const struct hda_codec_ops cs_patch_ops = {
1129         .build_controls = cs_build_controls,
1130         .build_pcms = cs_build_pcms,
1131         .init = cs_init,
1132         .free = cs_free,
1133         .unsol_event = cs_unsol_event,
1134 };
1135
1136 static int cs_parse_auto_config(struct hda_codec *codec)
1137 {
1138         struct cs_spec *spec = codec->spec;
1139         int err;
1140
1141         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1142         if (err < 0)
1143                 return err;
1144
1145         err = parse_output(codec);
1146         if (err < 0)
1147                 return err;
1148         err = parse_input(codec);
1149         if (err < 0)
1150                 return err;
1151         err = parse_digital_output(codec);
1152         if (err < 0)
1153                 return err;
1154         err = parse_digital_input(codec);
1155         if (err < 0)
1156                 return err;
1157         return 0;
1158 }
1159
1160 static const char * const cs420x_models[CS420X_MODELS] = {
1161         [CS420X_MBP53] = "mbp53",
1162         [CS420X_MBP55] = "mbp55",
1163         [CS420X_IMAC27] = "imac27",
1164         [CS420X_AUTO] = "auto",
1165 };
1166
1167
1168 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1169         SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1170         SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1171         SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1172         SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1173         SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
1174         {} /* terminator */
1175 };
1176
1177 struct cs_pincfg {
1178         hda_nid_t nid;
1179         u32 val;
1180 };
1181
1182 static const struct cs_pincfg mbp53_pincfgs[] = {
1183         { 0x09, 0x012b4050 },
1184         { 0x0a, 0x90100141 },
1185         { 0x0b, 0x90100140 },
1186         { 0x0c, 0x018b3020 },
1187         { 0x0d, 0x90a00110 },
1188         { 0x0e, 0x400000f0 },
1189         { 0x0f, 0x01cbe030 },
1190         { 0x10, 0x014be060 },
1191         { 0x12, 0x400000f0 },
1192         { 0x15, 0x400000f0 },
1193         {} /* terminator */
1194 };
1195
1196 static const struct cs_pincfg mbp55_pincfgs[] = {
1197         { 0x09, 0x012b4030 },
1198         { 0x0a, 0x90100121 },
1199         { 0x0b, 0x90100120 },
1200         { 0x0c, 0x400000f0 },
1201         { 0x0d, 0x90a00110 },
1202         { 0x0e, 0x400000f0 },
1203         { 0x0f, 0x400000f0 },
1204         { 0x10, 0x014be040 },
1205         { 0x12, 0x400000f0 },
1206         { 0x15, 0x400000f0 },
1207         {} /* terminator */
1208 };
1209
1210 static const struct cs_pincfg imac27_pincfgs[] = {
1211         { 0x09, 0x012b4050 },
1212         { 0x0a, 0x90100140 },
1213         { 0x0b, 0x90100142 },
1214         { 0x0c, 0x018b3020 },
1215         { 0x0d, 0x90a00110 },
1216         { 0x0e, 0x400000f0 },
1217         { 0x0f, 0x01cbe030 },
1218         { 0x10, 0x014be060 },
1219         { 0x12, 0x01ab9070 },
1220         { 0x15, 0x400000f0 },
1221         {} /* terminator */
1222 };
1223
1224 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1225         [CS420X_MBP53] = mbp53_pincfgs,
1226         [CS420X_MBP55] = mbp55_pincfgs,
1227         [CS420X_IMAC27] = imac27_pincfgs,
1228 };
1229
1230 static void fix_pincfg(struct hda_codec *codec, int model)
1231 {
1232         const struct cs_pincfg *cfg = cs_pincfgs[model];
1233         if (!cfg)
1234                 return;
1235         for (; cfg->nid; cfg++)
1236                 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1237 }
1238
1239
1240 static int patch_cs420x(struct hda_codec *codec)
1241 {
1242         struct cs_spec *spec;
1243         int err;
1244
1245         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1246         if (!spec)
1247                 return -ENOMEM;
1248         codec->spec = spec;
1249
1250         spec->board_config =
1251                 snd_hda_check_board_config(codec, CS420X_MODELS,
1252                                            cs420x_models, cs420x_cfg_tbl);
1253         if (spec->board_config >= 0)
1254                 fix_pincfg(codec, spec->board_config);
1255
1256         switch (spec->board_config) {
1257         case CS420X_IMAC27:
1258         case CS420X_MBP53:
1259         case CS420X_MBP55:
1260                 /* GPIO1 = headphones */
1261                 /* GPIO3 = speakers */
1262                 spec->gpio_mask = 0x0a;
1263                 spec->gpio_dir = 0x0a;
1264                 break;
1265         }
1266
1267         err = cs_parse_auto_config(codec);
1268         if (err < 0)
1269                 goto error;
1270
1271         codec->patch_ops = cs_patch_ops;
1272
1273         return 0;
1274
1275  error:
1276         kfree(codec->spec);
1277         codec->spec = NULL;
1278         return err;
1279 }
1280
1281
1282 /*
1283  * patch entries
1284  */
1285 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
1286         { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1287         { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1288         {} /* terminator */
1289 };
1290
1291 MODULE_ALIAS("snd-hda-codec-id:10134206");
1292 MODULE_ALIAS("snd-hda-codec-id:10134207");
1293
1294 MODULE_LICENSE("GPL");
1295 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1296
1297 static struct hda_codec_preset_list cirrus_list = {
1298         .preset = snd_hda_preset_cirrus,
1299         .owner = THIS_MODULE,
1300 };
1301
1302 static int __init patch_cirrus_init(void)
1303 {
1304         return snd_hda_add_codec_preset(&cirrus_list);
1305 }
1306
1307 static void __exit patch_cirrus_exit(void)
1308 {
1309         snd_hda_delete_codec_preset(&cirrus_list);
1310 }
1311
1312 module_init(patch_cirrus_init)
1313 module_exit(patch_cirrus_exit)