ALSA: hda - MacBookPro 5,3 line-in support
[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         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 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 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 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 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 = snd_hda_query_pin_caps(codec, pin);
335         if (!(val & AC_PINCAP_PRES_DETECT))
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(codec, nid) & AC_WCAP_TYPE)
353                         >> AC_WCAP_TYPE_SHIFT;
354                 if (type != AC_WID_AUD_IN)
355                         continue;
356                 nums = snd_hda_get_connections(codec, nid, pins,
357                                                ARRAY_SIZE(pins));
358                 if (nums <= 0)
359                         continue;
360                 for (j = 0; j < nums; j++) {
361                         if (pins[j] == pin) {
362                                 *idxp = j;
363                                 return nid;
364                         }
365                 }
366         }
367         return 0;
368 }
369
370 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
371 {
372         unsigned int val;
373         val = snd_hda_codec_get_pincfg(codec, nid);
374         return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
375 }
376
377 static int parse_output(struct hda_codec *codec)
378 {
379         struct cs_spec *spec = codec->spec;
380         struct auto_pin_cfg *cfg = &spec->autocfg;
381         int i, extra_nids;
382         hda_nid_t dac;
383
384         for (i = 0; i < cfg->line_outs; i++) {
385                 dac = get_dac(codec, cfg->line_out_pins[i]);
386                 if (!dac)
387                         break;
388                 spec->dac_nid[i] = dac;
389         }
390         spec->multiout.num_dacs = i;
391         spec->multiout.dac_nids = spec->dac_nid;
392         spec->multiout.max_channels = i * 2;
393
394         /* add HP and speakers */
395         extra_nids = 0;
396         for (i = 0; i < cfg->hp_outs; i++) {
397                 dac = get_dac(codec, cfg->hp_pins[i]);
398                 if (!dac)
399                         break;
400                 if (!i)
401                         spec->multiout.hp_nid = dac;
402                 else
403                         spec->multiout.extra_out_nid[extra_nids++] = dac;
404         }
405         for (i = 0; i < cfg->speaker_outs; i++) {
406                 dac = get_dac(codec, cfg->speaker_pins[i]);
407                 if (!dac)
408                         break;
409                 spec->multiout.extra_out_nid[extra_nids++] = dac;
410         }
411
412         if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
413                 cfg->speaker_outs = cfg->line_outs;
414                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
415                        sizeof(cfg->speaker_pins));
416                 cfg->line_outs = 0;
417         }
418
419         return 0;
420 }
421
422 static int parse_input(struct hda_codec *codec)
423 {
424         struct cs_spec *spec = codec->spec;
425         struct auto_pin_cfg *cfg = &spec->autocfg;
426         int i;
427
428         for (i = 0; i < cfg->num_inputs; i++) {
429                 hda_nid_t pin = cfg->inputs[i].pin;
430                 spec->input_idx[spec->num_inputs] = i;
431                 spec->capsrc_idx[i] = spec->num_inputs++;
432                 spec->cur_input = i;
433                 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
434         }
435         if (!spec->num_inputs)
436                 return 0;
437
438         /* check whether the automatic mic switch is available */
439         if (spec->num_inputs == 2 &&
440             cfg->inputs[0].type == AUTO_PIN_MIC &&
441             cfg->inputs[1].type == AUTO_PIN_MIC) {
442                 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
443                         if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
444                                 spec->mic_detect = 1;
445                                 spec->automic_idx = 0;
446                         }
447                 } else {
448                         if (is_ext_mic(codec, cfg->inputs[1].pin)) {
449                                 spec->mic_detect = 1;
450                                 spec->automic_idx = 1;
451                         }
452                 }
453         }
454         return 0;
455 }
456
457
458 static int parse_digital_output(struct hda_codec *codec)
459 {
460         struct cs_spec *spec = codec->spec;
461         struct auto_pin_cfg *cfg = &spec->autocfg;
462         hda_nid_t nid;
463
464         if (!cfg->dig_outs)
465                 return 0;
466         if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
467                 return 0;
468         spec->multiout.dig_out_nid = nid;
469         spec->multiout.share_spdif = 1;
470         if (cfg->dig_outs > 1 &&
471             snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
472                 spec->slave_dig_outs[0] = nid;
473                 codec->slave_dig_outs = spec->slave_dig_outs;
474         }
475         return 0;
476 }
477
478 static int parse_digital_input(struct hda_codec *codec)
479 {
480         struct cs_spec *spec = codec->spec;
481         struct auto_pin_cfg *cfg = &spec->autocfg;
482         int idx;
483
484         if (cfg->dig_in_pin)
485                 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
486         return 0;
487 }
488
489 /*
490  * create mixer controls
491  */
492
493 static const char *dir_sfx[2] = { "Playback", "Capture" };
494
495 static int add_mute(struct hda_codec *codec, const char *name, int index,
496                     unsigned int pval, int dir, struct snd_kcontrol **kctlp)
497 {
498         char tmp[44];
499         struct snd_kcontrol_new knew =
500                 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
501         knew.private_value = pval;
502         snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
503         *kctlp = snd_ctl_new1(&knew, codec);
504         (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
505         return snd_hda_ctl_add(codec, 0, *kctlp);
506 }
507
508 static int add_volume(struct hda_codec *codec, const char *name,
509                       int index, unsigned int pval, int dir,
510                       struct snd_kcontrol **kctlp)
511 {
512         char tmp[32];
513         struct snd_kcontrol_new knew =
514                 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
515         knew.private_value = pval;
516         snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
517         *kctlp = snd_ctl_new1(&knew, codec);
518         (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
519         return snd_hda_ctl_add(codec, 0, *kctlp);
520 }
521
522 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
523 {
524         unsigned int caps;
525
526         /* set the upper-limit for mixer amp to 0dB */
527         caps = query_amp_caps(codec, dac, HDA_OUTPUT);
528         caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
529         caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
530                 << AC_AMPCAP_NUM_STEPS_SHIFT;
531         snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
532 }
533
534 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
535 {
536         struct cs_spec *spec = codec->spec;
537         unsigned int tlv[4];
538         int err;
539
540         spec->vmaster_sw =
541                 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
542         err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
543         if (err < 0)
544                 return err;
545
546         snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
547         spec->vmaster_vol =
548                 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
549         err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
550         if (err < 0)
551                 return err;
552         return 0;
553 }
554
555 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
556                       int num_ctls, int type)
557 {
558         struct cs_spec *spec = codec->spec;
559         const char *name;
560         int err, index;
561         struct snd_kcontrol *kctl;
562         static char *speakers[] = {
563                 "Front Speaker", "Surround Speaker", "Bass Speaker"
564         };
565         static char *line_outs[] = {
566                 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
567         };
568
569         fix_volume_caps(codec, dac);
570         if (!spec->vmaster_sw) {
571                 err = add_vmaster(codec, dac);
572                 if (err < 0)
573                         return err;
574         }
575
576         index = 0;
577         switch (type) {
578         case AUTO_PIN_HP_OUT:
579                 name = "Headphone";
580                 index = idx;
581                 break;
582         case AUTO_PIN_SPEAKER_OUT:
583                 if (num_ctls > 1)
584                         name = speakers[idx];
585                 else
586                         name = "Speaker";
587                 break;
588         default:
589                 if (num_ctls > 1)
590                         name = line_outs[idx];
591                 else
592                         name = "Line-Out";
593                 break;
594         }
595
596         err = add_mute(codec, name, index,
597                        HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
598         if (err < 0)
599                 return err;
600         err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
601         if (err < 0)
602                 return err;
603
604         err = add_volume(codec, name, index,
605                          HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
606         if (err < 0)
607                 return err;
608         err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
609         if (err < 0)
610                 return err;
611
612         return 0;
613 }               
614
615 static int build_output(struct hda_codec *codec)
616 {
617         struct cs_spec *spec = codec->spec;
618         struct auto_pin_cfg *cfg = &spec->autocfg;
619         int i, err;
620
621         for (i = 0; i < cfg->line_outs; i++) {
622                 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
623                                  i, cfg->line_outs, cfg->line_out_type);
624                 if (err < 0)
625                         return err;
626         }
627         for (i = 0; i < cfg->hp_outs; i++) {
628                 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
629                                  i, cfg->hp_outs, AUTO_PIN_HP_OUT);
630                 if (err < 0)
631                         return err;
632         }
633         for (i = 0; i < cfg->speaker_outs; i++) {
634                 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
635                                  i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
636                 if (err < 0)
637                         return err;
638         }
639         return 0;
640 }
641
642 /*
643  */
644
645 static struct snd_kcontrol_new cs_capture_ctls[] = {
646         HDA_BIND_SW("Capture Switch", 0),
647         HDA_BIND_VOL("Capture Volume", 0),
648 };
649
650 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
651                             int force)
652 {
653         struct cs_spec *spec = codec->spec;
654         
655         if (spec->cur_input == idx && !force)
656                 return 0;
657         if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
658                 /* stream is running, let's swap the current ADC */
659                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
660                 spec->cur_adc = spec->adc_nid[idx];
661                 snd_hda_codec_setup_stream(codec, spec->cur_adc,
662                                            spec->cur_adc_stream_tag, 0,
663                                            spec->cur_adc_format);
664         }
665         snd_hda_codec_write(codec, spec->cur_adc, 0,
666                             AC_VERB_SET_CONNECT_SEL,
667                             spec->adc_idx[idx]);
668         spec->cur_input = idx;
669         return 1;
670 }
671
672 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
673                                   struct snd_ctl_elem_info *uinfo)
674 {
675         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
676         struct cs_spec *spec = codec->spec;
677         struct auto_pin_cfg *cfg = &spec->autocfg;
678         unsigned int idx;
679
680         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
681         uinfo->count = 1;
682         uinfo->value.enumerated.items = spec->num_inputs;
683         if (uinfo->value.enumerated.item >= spec->num_inputs)
684                 uinfo->value.enumerated.item = spec->num_inputs - 1;
685         idx = spec->input_idx[uinfo->value.enumerated.item];
686         strcpy(uinfo->value.enumerated.name,
687                hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
688         return 0;
689 }
690
691 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
692                                  struct snd_ctl_elem_value *ucontrol)
693 {
694         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
695         struct cs_spec *spec = codec->spec;
696         ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
697         return 0;
698 }
699
700 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
701                                  struct snd_ctl_elem_value *ucontrol)
702 {
703         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
704         struct cs_spec *spec = codec->spec;
705         unsigned int idx = ucontrol->value.enumerated.item[0];
706
707         if (idx >= spec->num_inputs)
708                 return -EINVAL;
709         idx = spec->input_idx[idx];
710         return change_cur_input(codec, idx, 0);
711 }
712
713 static struct snd_kcontrol_new cs_capture_source = {
714         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
715         .name = "Capture Source",
716         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
717         .info = cs_capture_source_info,
718         .get = cs_capture_source_get,
719         .put = cs_capture_source_put,
720 };
721
722 static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
723                                                struct hda_ctl_ops *ops)
724 {
725         struct cs_spec *spec = codec->spec;
726         struct hda_bind_ctls *bind;
727         int i, n;
728
729         bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
730                        GFP_KERNEL);
731         if (!bind)
732                 return NULL;
733         bind->ops = ops;
734         n = 0;
735         for (i = 0; i < AUTO_PIN_LAST; i++) {
736                 if (!spec->adc_nid[i])
737                         continue;
738                 bind->values[n++] =
739                         HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
740                                             spec->adc_idx[i], HDA_INPUT);
741         }
742         return bind;
743 }
744
745 static int build_input(struct hda_codec *codec)
746 {
747         struct cs_spec *spec = codec->spec;
748         int i, err;
749
750         if (!spec->num_inputs)
751                 return 0;
752
753         /* make bind-capture */
754         spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
755         spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
756         for (i = 0; i < 2; i++) {
757                 struct snd_kcontrol *kctl;
758                 int n;
759                 if (!spec->capture_bind[i])
760                         return -ENOMEM;
761                 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
762                 if (!kctl)
763                         return -ENOMEM;
764                 kctl->private_value = (long)spec->capture_bind[i];
765                 err = snd_hda_ctl_add(codec, 0, kctl);
766                 if (err < 0)
767                         return err;
768                 for (n = 0; n < AUTO_PIN_LAST; n++) {
769                         if (!spec->adc_nid[n])
770                                 continue;
771                         err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
772                         if (err < 0)
773                                 return err;
774                 }
775         }
776         
777         if (spec->num_inputs > 1 && !spec->mic_detect) {
778                 err = snd_hda_ctl_add(codec, 0,
779                                       snd_ctl_new1(&cs_capture_source, codec));
780                 if (err < 0)
781                         return err;
782         }
783
784         return 0;
785 }
786
787 /*
788  */
789
790 static int build_digital_output(struct hda_codec *codec)
791 {
792         struct cs_spec *spec = codec->spec;
793         int err;
794
795         if (!spec->multiout.dig_out_nid)
796                 return 0;
797
798         err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
799         if (err < 0)
800                 return err;
801         err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
802         if (err < 0)
803                 return err;
804         return 0;
805 }
806
807 static int build_digital_input(struct hda_codec *codec)
808 {
809         struct cs_spec *spec = codec->spec;
810         if (spec->dig_in)
811                 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
812         return 0;
813 }
814
815 /*
816  * auto-mute and auto-mic switching
817  */
818
819 static void cs_automute(struct hda_codec *codec)
820 {
821         struct cs_spec *spec = codec->spec;
822         struct auto_pin_cfg *cfg = &spec->autocfg;
823         unsigned int caps, hp_present;
824         hda_nid_t nid;
825         int i;
826
827         hp_present = 0;
828         for (i = 0; i < cfg->hp_outs; i++) {
829                 nid = cfg->hp_pins[i];
830                 caps = snd_hda_query_pin_caps(codec, nid);
831                 if (!(caps & AC_PINCAP_PRES_DETECT))
832                         continue;
833                 hp_present = snd_hda_jack_detect(codec, nid);
834                 if (hp_present)
835                         break;
836         }
837         for (i = 0; i < cfg->speaker_outs; i++) {
838                 nid = cfg->speaker_pins[i];
839                 snd_hda_codec_write(codec, nid, 0,
840                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
841                                     hp_present ? 0 : PIN_OUT);
842         }
843         if (spec->board_config == CS420X_MBP53 ||
844             spec->board_config == CS420X_MBP55 ||
845             spec->board_config == CS420X_IMAC27) {
846                 unsigned int gpio = hp_present ? 0x02 : 0x08;
847                 snd_hda_codec_write(codec, 0x01, 0,
848                                     AC_VERB_SET_GPIO_DATA, gpio);
849         }
850 }
851
852 static void cs_automic(struct hda_codec *codec)
853 {
854         struct cs_spec *spec = codec->spec;
855         struct auto_pin_cfg *cfg = &spec->autocfg;
856         hda_nid_t nid;
857         unsigned int present;
858         
859         nid = cfg->inputs[spec->automic_idx].pin;
860         present = snd_hda_jack_detect(codec, nid);
861         if (present)
862                 change_cur_input(codec, spec->automic_idx, 0);
863         else
864                 change_cur_input(codec, !spec->automic_idx, 0);
865 }
866
867 /*
868  */
869
870 static void init_output(struct hda_codec *codec)
871 {
872         struct cs_spec *spec = codec->spec;
873         struct auto_pin_cfg *cfg = &spec->autocfg;
874         int i;
875
876         /* mute first */
877         for (i = 0; i < spec->multiout.num_dacs; i++)
878                 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
879                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
880         if (spec->multiout.hp_nid)
881                 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
882                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
883         for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
884                 if (!spec->multiout.extra_out_nid[i])
885                         break;
886                 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
887                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
888         }
889
890         /* set appropriate pin controls */
891         for (i = 0; i < cfg->line_outs; i++)
892                 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
893                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
894         for (i = 0; i < cfg->hp_outs; i++) {
895                 hda_nid_t nid = cfg->hp_pins[i];
896                 snd_hda_codec_write(codec, nid, 0,
897                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
898                 if (!cfg->speaker_outs)
899                         continue;
900                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
901                         snd_hda_codec_write(codec, nid, 0,
902                                             AC_VERB_SET_UNSOLICITED_ENABLE,
903                                             AC_USRSP_EN | HP_EVENT);
904                         spec->hp_detect = 1;
905                 }
906         }
907         for (i = 0; i < cfg->speaker_outs; i++)
908                 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
909                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
910         if (spec->hp_detect)
911                 cs_automute(codec);
912 }
913
914 static void init_input(struct hda_codec *codec)
915 {
916         struct cs_spec *spec = codec->spec;
917         struct auto_pin_cfg *cfg = &spec->autocfg;
918         unsigned int coef;
919         int i;
920
921         for (i = 0; i < cfg->num_inputs; i++) {
922                 unsigned int ctl;
923                 hda_nid_t pin = cfg->inputs[i].pin;
924                 if (!spec->adc_nid[i])
925                         continue;
926                 /* set appropriate pin control and mute first */
927                 ctl = PIN_IN;
928                 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
929                         unsigned int caps = snd_hda_query_pin_caps(codec, pin);
930                         caps >>= AC_PINCAP_VREF_SHIFT;
931                         if (caps & AC_PINCAP_VREF_80)
932                                 ctl = PIN_VREF80;
933                 }
934                 snd_hda_codec_write(codec, pin, 0,
935                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
936                 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
937                                     AC_VERB_SET_AMP_GAIN_MUTE,
938                                     AMP_IN_MUTE(spec->adc_idx[i]));
939                 if (spec->mic_detect && spec->automic_idx == i)
940                         snd_hda_codec_write(codec, pin, 0,
941                                             AC_VERB_SET_UNSOLICITED_ENABLE,
942                                             AC_USRSP_EN | MIC_EVENT);
943         }
944         change_cur_input(codec, spec->cur_input, 1);
945         if (spec->mic_detect)
946                 cs_automic(codec);
947
948         coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
949         if (is_active_pin(codec, CS_DMIC2_PIN_NID))
950                 coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
951         if (is_active_pin(codec, CS_DMIC1_PIN_NID))
952                 coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0 
953                                  * No effect if SPDIF_OUT2 is selected in 
954                                  * IDX_SPDIF_CTL.
955                                   */
956         cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
957 }
958
959 static struct hda_verb cs_coef_init_verbs[] = {
960         {0x11, AC_VERB_SET_PROC_STATE, 1},
961         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
962         {0x11, AC_VERB_SET_PROC_COEF,
963          (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
964           | 0x0040 /* Mute DACs on FIFO error */
965           | 0x1000 /* Enable DACs High Pass Filter */
966           | 0x0400 /* Disable Coefficient Auto increment */
967           )},
968         /* Beep */
969         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
970         {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
971
972         {} /* terminator */
973 };
974
975 /* Errata: CS4207 rev C0/C1/C2 Silicon
976  *
977  * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
978  *
979  * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
980  * may be excessive (up to an additional 200 μA), which is most easily
981  * observed while the part is being held in reset (RESET# active low).
982  *
983  * Root Cause: At initial powerup of the device, the logic that drives
984  * the clock and write enable to the S/PDIF SRC RAMs is not properly
985  * initialized.
986  * Certain random patterns will cause a steady leakage current in those
987  * RAM cells. The issue will resolve once the SRCs are used (turned on).
988  *
989  * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
990  * blocks, which will alleviate the issue.
991  */
992
993 static struct hda_verb cs_errata_init_verbs[] = {
994         {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
995         {0x11, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
996
997         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
998         {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
999         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1000         {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1001         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1002         {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1003
1004         {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1005         {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1006
1007         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1008         {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1009         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1010         {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1011         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1012         {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1013         {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1014
1015         {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1016         {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1017         /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1018
1019         {} /* terminator */
1020 };
1021
1022 /* SPDIF setup */
1023 static void init_digital(struct hda_codec *codec)
1024 {
1025         unsigned int coef;
1026
1027         coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1028         coef |= 0x0008; /* Replace with mute on error */
1029         if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1030                 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1031                                  * SPDIF_OUT2 is shared with GPIO1 and
1032                                  * DMIC_SDA2.
1033                                  */
1034         cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1035 }
1036
1037 static int cs_init(struct hda_codec *codec)
1038 {
1039         struct cs_spec *spec = codec->spec;
1040
1041         /* init_verb sequence for C0/C1/C2 errata*/
1042         snd_hda_sequence_write(codec, cs_errata_init_verbs);
1043
1044         snd_hda_sequence_write(codec, cs_coef_init_verbs);
1045
1046         if (spec->gpio_mask) {
1047                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1048                                     spec->gpio_mask);
1049                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1050                                     spec->gpio_dir);
1051                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1052                                     spec->gpio_data);
1053         }
1054
1055         init_output(codec);
1056         init_input(codec);
1057         init_digital(codec);
1058         return 0;
1059 }
1060
1061 static int cs_build_controls(struct hda_codec *codec)
1062 {
1063         int err;
1064
1065         err = build_output(codec);
1066         if (err < 0)
1067                 return err;
1068         err = build_input(codec);
1069         if (err < 0)
1070                 return err;
1071         err = build_digital_output(codec);
1072         if (err < 0)
1073                 return err;
1074         err = build_digital_input(codec);
1075         if (err < 0)
1076                 return err;
1077         return cs_init(codec);
1078 }
1079
1080 static void cs_free(struct hda_codec *codec)
1081 {
1082         struct cs_spec *spec = codec->spec;
1083         kfree(spec->capture_bind[0]);
1084         kfree(spec->capture_bind[1]);
1085         kfree(codec->spec);
1086 }
1087
1088 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1089 {
1090         switch ((res >> 26) & 0x7f) {
1091         case HP_EVENT:
1092                 cs_automute(codec);
1093                 break;
1094         case MIC_EVENT:
1095                 cs_automic(codec);
1096                 break;
1097         }
1098 }
1099
1100 static struct hda_codec_ops cs_patch_ops = {
1101         .build_controls = cs_build_controls,
1102         .build_pcms = cs_build_pcms,
1103         .init = cs_init,
1104         .free = cs_free,
1105         .unsol_event = cs_unsol_event,
1106 };
1107
1108 static int cs_parse_auto_config(struct hda_codec *codec)
1109 {
1110         struct cs_spec *spec = codec->spec;
1111         int err;
1112
1113         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1114         if (err < 0)
1115                 return err;
1116
1117         err = parse_output(codec);
1118         if (err < 0)
1119                 return err;
1120         err = parse_input(codec);
1121         if (err < 0)
1122                 return err;
1123         err = parse_digital_output(codec);
1124         if (err < 0)
1125                 return err;
1126         err = parse_digital_input(codec);
1127         if (err < 0)
1128                 return err;
1129         return 0;
1130 }
1131
1132 static const char *cs420x_models[CS420X_MODELS] = {
1133         [CS420X_MBP53] = "mbp53",
1134         [CS420X_MBP55] = "mbp55",
1135         [CS420X_IMAC27] = "imac27",
1136         [CS420X_AUTO] = "auto",
1137 };
1138
1139
1140 static struct snd_pci_quirk cs420x_cfg_tbl[] = {
1141         SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1142         SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1143         SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
1144         {} /* terminator */
1145 };
1146
1147 struct cs_pincfg {
1148         hda_nid_t nid;
1149         u32 val;
1150 };
1151
1152 static struct cs_pincfg mbp53_pincfgs[] = {
1153         { 0x09, 0x012b4050 },
1154         { 0x0a, 0x90100141 },
1155         { 0x0b, 0x90100140 },
1156         { 0x0c, 0x018b3020 },
1157         { 0x0d, 0x90a00110 },
1158         { 0x0e, 0x400000f0 },
1159         { 0x0f, 0x01cbe030 },
1160         { 0x10, 0x014be060 },
1161         { 0x12, 0x400000f0 },
1162         { 0x15, 0x400000f0 },
1163         {} /* terminator */
1164 };
1165
1166 static struct cs_pincfg mbp55_pincfgs[] = {
1167         { 0x09, 0x012b4030 },
1168         { 0x0a, 0x90100121 },
1169         { 0x0b, 0x90100120 },
1170         { 0x0c, 0x400000f0 },
1171         { 0x0d, 0x90a00110 },
1172         { 0x0e, 0x400000f0 },
1173         { 0x0f, 0x400000f0 },
1174         { 0x10, 0x014be040 },
1175         { 0x12, 0x400000f0 },
1176         { 0x15, 0x400000f0 },
1177         {} /* terminator */
1178 };
1179
1180 static struct cs_pincfg imac27_pincfgs[] = {
1181         { 0x09, 0x012b4050 },
1182         { 0x0a, 0x90100140 },
1183         { 0x0b, 0x90100142 },
1184         { 0x0c, 0x018b3020 },
1185         { 0x0d, 0x90a00110 },
1186         { 0x0e, 0x400000f0 },
1187         { 0x0f, 0x01cbe030 },
1188         { 0x10, 0x014be060 },
1189         { 0x12, 0x01ab9070 },
1190         { 0x15, 0x400000f0 },
1191         {} /* terminator */
1192 };
1193
1194 static struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1195         [CS420X_MBP53] = mbp53_pincfgs,
1196         [CS420X_MBP55] = mbp55_pincfgs,
1197         [CS420X_IMAC27] = imac27_pincfgs,
1198 };
1199
1200 static void fix_pincfg(struct hda_codec *codec, int model)
1201 {
1202         const struct cs_pincfg *cfg = cs_pincfgs[model];
1203         if (!cfg)
1204                 return;
1205         for (; cfg->nid; cfg++)
1206                 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1207 }
1208
1209
1210 static int patch_cs420x(struct hda_codec *codec)
1211 {
1212         struct cs_spec *spec;
1213         int err;
1214
1215         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1216         if (!spec)
1217                 return -ENOMEM;
1218         codec->spec = spec;
1219
1220         spec->board_config =
1221                 snd_hda_check_board_config(codec, CS420X_MODELS,
1222                                            cs420x_models, cs420x_cfg_tbl);
1223         if (spec->board_config >= 0)
1224                 fix_pincfg(codec, spec->board_config);
1225
1226         switch (spec->board_config) {
1227         case CS420X_IMAC27:
1228         case CS420X_MBP53:
1229         case CS420X_MBP55:
1230                 /* GPIO1 = headphones */
1231                 /* GPIO3 = speakers */
1232                 spec->gpio_mask = 0x0a;
1233                 spec->gpio_dir = 0x0a;
1234                 break;
1235         }
1236
1237         err = cs_parse_auto_config(codec);
1238         if (err < 0)
1239                 goto error;
1240
1241         codec->patch_ops = cs_patch_ops;
1242
1243         return 0;
1244
1245  error:
1246         kfree(codec->spec);
1247         codec->spec = NULL;
1248         return err;
1249 }
1250
1251
1252 /*
1253  * patch entries
1254  */
1255 static struct hda_codec_preset snd_hda_preset_cirrus[] = {
1256         { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1257         { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1258         {} /* terminator */
1259 };
1260
1261 MODULE_ALIAS("snd-hda-codec-id:10134206");
1262 MODULE_ALIAS("snd-hda-codec-id:10134207");
1263
1264 MODULE_LICENSE("GPL");
1265 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1266
1267 static struct hda_codec_preset_list cirrus_list = {
1268         .preset = snd_hda_preset_cirrus,
1269         .owner = THIS_MODULE,
1270 };
1271
1272 static int __init patch_cirrus_init(void)
1273 {
1274         return snd_hda_add_codec_preset(&cirrus_list);
1275 }
1276
1277 static void __exit patch_cirrus_exit(void)
1278 {
1279         snd_hda_delete_codec_preset(&cirrus_list);
1280 }
1281
1282 module_init(patch_cirrus_init)
1283 module_exit(patch_cirrus_exit)