ALSA: hda - Add quirk for MacBook Pro 5,5 with CS4206
[pandora-kernel.git] / sound / pci / hda / patch_cirrus.c
1 /*
2  * HD audio interface patch for Cirrus Logic CS420x chip
3  *
4  * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
5  *
6  *  This driver is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This driver is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/pci.h>
25 #include <sound/core.h>
26 #include "hda_codec.h"
27 #include "hda_local.h"
28
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         struct hda_pcm pcm_rec[2];      /* PCM information */
57
58         unsigned int hp_detect:1;
59         unsigned int mic_detect:1;
60 };
61
62 /* available models */
63 enum {
64         CS420X_MBP55,
65         CS420X_AUTO,
66         CS420X_MODELS
67 };
68
69 /* Vendor-specific processing widget */
70 #define CS420X_VENDOR_NID       0x11
71 #define CS_DIG_OUT1_PIN_NID     0x10
72 #define CS_DIG_OUT2_PIN_NID     0x15
73 #define CS_DMIC1_PIN_NID        0x12
74 #define CS_DMIC2_PIN_NID        0x0e
75
76 /* coef indices */
77 #define IDX_SPDIF_STAT          0x0000
78 #define IDX_SPDIF_CTL           0x0001
79 #define IDX_ADC_CFG             0x0002
80 /* SZC bitmask, 4 modes below:
81  * 0 = immediate,
82  * 1 = digital immediate, analog zero-cross
83  * 2 = digtail & analog soft-ramp
84  * 3 = digital soft-ramp, analog zero-cross
85  */
86 #define   CS_COEF_ADC_SZC_MASK          (3 << 0)
87 #define   CS_COEF_ADC_MIC_SZC_MODE      (3 << 0) /* SZC setup for mic */
88 #define   CS_COEF_ADC_LI_SZC_MODE       (3 << 0) /* SZC setup for line-in */
89 /* PGA mode: 0 = differential, 1 = signle-ended */
90 #define   CS_COEF_ADC_MIC_PGA_MODE      (1 << 5) /* PGA setup for mic */
91 #define   CS_COEF_ADC_LI_PGA_MODE       (1 << 6) /* PGA setup for line-in */
92 #define IDX_DAC_CFG             0x0003
93 /* SZC bitmask, 4 modes below:
94  * 0 = Immediate
95  * 1 = zero-cross
96  * 2 = soft-ramp
97  * 3 = soft-ramp on zero-cross
98  */
99 #define   CS_COEF_DAC_HP_SZC_MODE       (3 << 0) /* nid 0x02 */
100 #define   CS_COEF_DAC_LO_SZC_MODE       (3 << 2) /* nid 0x03 */
101 #define   CS_COEF_DAC_SPK_SZC_MODE      (3 << 4) /* nid 0x04 */
102
103 #define IDX_BEEP_CFG            0x0004
104 /* 0x0008 - test reg key */
105 /* 0x0009 - 0x0014 -> 12 test regs */
106 /* 0x0015 - visibility reg */
107
108
109 static int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
110 {
111         snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
112                             AC_VERB_SET_COEF_INDEX, idx);
113         return snd_hda_codec_read(codec, CS420X_VENDOR_NID, 0,
114                                   AC_VERB_GET_PROC_COEF, 0);
115 }
116
117 static void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
118                                unsigned int coef)
119 {
120         snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
121                             AC_VERB_SET_COEF_INDEX, idx);
122         snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
123                             AC_VERB_SET_PROC_COEF, coef);
124 }
125
126
127 #define HP_EVENT        1
128 #define MIC_EVENT       2
129
130 /*
131  * PCM callbacks
132  */
133 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
134                                 struct hda_codec *codec,
135                                 struct snd_pcm_substream *substream)
136 {
137         struct cs_spec *spec = codec->spec;
138         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
139                                              hinfo);
140 }
141
142 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
143                                    struct hda_codec *codec,
144                                    unsigned int stream_tag,
145                                    unsigned int format,
146                                    struct snd_pcm_substream *substream)
147 {
148         struct cs_spec *spec = codec->spec;
149         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
150                                                 stream_tag, format, substream);
151 }
152
153 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
154                                    struct hda_codec *codec,
155                                    struct snd_pcm_substream *substream)
156 {
157         struct cs_spec *spec = codec->spec;
158         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
159 }
160
161 /*
162  * Digital out
163  */
164 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
165                                     struct hda_codec *codec,
166                                     struct snd_pcm_substream *substream)
167 {
168         struct cs_spec *spec = codec->spec;
169         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
170 }
171
172 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
173                                      struct hda_codec *codec,
174                                      struct snd_pcm_substream *substream)
175 {
176         struct cs_spec *spec = codec->spec;
177         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
178 }
179
180 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
181                                        struct hda_codec *codec,
182                                        unsigned int stream_tag,
183                                        unsigned int format,
184                                        struct snd_pcm_substream *substream)
185 {
186         struct cs_spec *spec = codec->spec;
187         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
188                                              format, substream);
189 }
190
191 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
192                                        struct hda_codec *codec,
193                                        struct snd_pcm_substream *substream)
194 {
195         struct cs_spec *spec = codec->spec;
196         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
197 }
198
199 /*
200  * Analog capture
201  */
202 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
203                                   struct hda_codec *codec,
204                                   unsigned int stream_tag,
205                                   unsigned int format,
206                                   struct snd_pcm_substream *substream)
207 {
208         struct cs_spec *spec = codec->spec;
209         spec->cur_adc = spec->adc_nid[spec->cur_input];
210         spec->cur_adc_stream_tag = stream_tag;
211         spec->cur_adc_format = format;
212         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
213         return 0;
214 }
215
216 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
217                                   struct hda_codec *codec,
218                                   struct snd_pcm_substream *substream)
219 {
220         struct cs_spec *spec = codec->spec;
221         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
222         spec->cur_adc = 0;
223         return 0;
224 }
225
226 /*
227  */
228 static struct hda_pcm_stream cs_pcm_analog_playback = {
229         .substreams = 1,
230         .channels_min = 2,
231         .channels_max = 2,
232         .ops = {
233                 .open = cs_playback_pcm_open,
234                 .prepare = cs_playback_pcm_prepare,
235                 .cleanup = cs_playback_pcm_cleanup
236         },
237 };
238
239 static struct hda_pcm_stream cs_pcm_analog_capture = {
240         .substreams = 1,
241         .channels_min = 2,
242         .channels_max = 2,
243         .ops = {
244                 .prepare = cs_capture_pcm_prepare,
245                 .cleanup = cs_capture_pcm_cleanup
246         },
247 };
248
249 static struct hda_pcm_stream cs_pcm_digital_playback = {
250         .substreams = 1,
251         .channels_min = 2,
252         .channels_max = 2,
253         .ops = {
254                 .open = cs_dig_playback_pcm_open,
255                 .close = cs_dig_playback_pcm_close,
256                 .prepare = cs_dig_playback_pcm_prepare,
257                 .cleanup = cs_dig_playback_pcm_cleanup
258         },
259 };
260
261 static struct hda_pcm_stream cs_pcm_digital_capture = {
262         .substreams = 1,
263         .channels_min = 2,
264         .channels_max = 2,
265 };
266
267 static int cs_build_pcms(struct hda_codec *codec)
268 {
269         struct cs_spec *spec = codec->spec;
270         struct hda_pcm *info = spec->pcm_rec;
271
272         codec->pcm_info = info;
273         codec->num_pcms = 0;
274
275         info->name = "Cirrus Analog";
276         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
277         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
278         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
279                 spec->multiout.max_channels;
280         info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
281         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
282                 spec->adc_nid[spec->cur_input];
283         codec->num_pcms++;
284
285         if (!spec->multiout.dig_out_nid && !spec->dig_in)
286                 return 0;
287
288         info++;
289         info->name = "Cirrus Digital";
290         info->pcm_type = spec->autocfg.dig_out_type[0];
291         if (!info->pcm_type)
292                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
293         if (spec->multiout.dig_out_nid) {
294                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
295                         cs_pcm_digital_playback;
296                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
297                         spec->multiout.dig_out_nid;
298         }
299         if (spec->dig_in) {
300                 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
301                         cs_pcm_digital_capture;
302                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
303         }
304         codec->num_pcms++;
305
306         return 0;
307 }
308
309 /*
310  * parse codec topology
311  */
312
313 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
314 {
315         hda_nid_t dac;
316         if (!pin)
317                 return 0;
318         if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
319                 return 0;
320         return dac;
321 }
322
323 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
324 {
325         struct cs_spec *spec = codec->spec;
326         struct auto_pin_cfg *cfg = &spec->autocfg;
327         hda_nid_t pin = cfg->input_pins[idx];
328         unsigned int val = snd_hda_query_pin_caps(codec, pin);
329         if (!(val & AC_PINCAP_PRES_DETECT))
330                 return 0;
331         val = snd_hda_codec_get_pincfg(codec, pin);
332         return (get_defcfg_connect(val) == AC_JACK_PORT_COMPLEX);
333 }
334
335 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
336                          unsigned int *idxp)
337 {
338         int i;
339         hda_nid_t nid;
340
341         nid = codec->start_nid;
342         for (i = 0; i < codec->num_nodes; i++, nid++) {
343                 hda_nid_t pins[2];
344                 unsigned int type;
345                 int j, nums;
346                 type = (get_wcaps(codec, nid) & AC_WCAP_TYPE)
347                         >> AC_WCAP_TYPE_SHIFT;
348                 if (type != AC_WID_AUD_IN)
349                         continue;
350                 nums = snd_hda_get_connections(codec, nid, pins,
351                                                ARRAY_SIZE(pins));
352                 if (nums <= 0)
353                         continue;
354                 for (j = 0; j < nums; j++) {
355                         if (pins[j] == pin) {
356                                 *idxp = j;
357                                 return nid;
358                         }
359                 }
360         }
361         return 0;
362 }
363
364 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
365 {
366         struct cs_spec *spec = codec->spec;
367         unsigned int val;
368         val = snd_hda_codec_get_pincfg(codec, nid);
369         return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
370 }
371
372 static int parse_output(struct hda_codec *codec)
373 {
374         struct cs_spec *spec = codec->spec;
375         struct auto_pin_cfg *cfg = &spec->autocfg;
376         int i, err, extra_nids;
377         hda_nid_t dac;
378
379         for (i = 0; i < cfg->line_outs; i++) {
380                 dac = get_dac(codec, cfg->line_out_pins[i]);
381                 if (!dac)
382                         break;
383                 spec->dac_nid[i] = dac;
384         }
385         spec->multiout.num_dacs = i;
386         spec->multiout.dac_nids = spec->dac_nid;
387         spec->multiout.max_channels = i * 2;
388
389         /* add HP and speakers */
390         extra_nids = 0;
391         for (i = 0; i < cfg->hp_outs; i++) {
392                 dac = get_dac(codec, cfg->hp_pins[i]);
393                 if (!dac)
394                         break;
395                 if (!i)
396                         spec->multiout.hp_nid = dac;
397                 else
398                         spec->multiout.extra_out_nid[extra_nids++] = dac;
399         }
400         for (i = 0; i < cfg->speaker_outs; i++) {
401                 dac = get_dac(codec, cfg->speaker_pins[i]);
402                 if (!dac)
403                         break;
404                 spec->multiout.extra_out_nid[extra_nids++] = dac;
405         }
406
407         if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
408                 cfg->speaker_outs = cfg->line_outs;
409                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
410                        sizeof(cfg->speaker_pins));
411                 cfg->line_outs = 0;
412         }
413
414         return 0;
415 }
416
417 static int parse_input(struct hda_codec *codec)
418 {
419         struct cs_spec *spec = codec->spec;
420         struct auto_pin_cfg *cfg = &spec->autocfg;
421         int i, n, err;
422
423         for (i = 0; i < AUTO_PIN_LAST; i++) {
424                 hda_nid_t pin = cfg->input_pins[i];
425                 struct snd_kcontrol *kctl;
426                 if (!pin)
427                         continue;
428                 spec->input_idx[spec->num_inputs] = i;
429                 spec->capsrc_idx[i] = spec->num_inputs++;
430                 spec->cur_input = i;
431                 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
432         }
433         if (!spec->num_inputs)
434                 return 0;
435
436         /* check whether the automatic mic switch is available */
437         if (spec->num_inputs == 2 &&
438             spec->adc_nid[AUTO_PIN_MIC] && spec->adc_nid[AUTO_PIN_FRONT_MIC]) {
439                 if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_FRONT_MIC])) {
440                         if (!is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
441                                 spec->mic_detect = 1;
442                                 spec->automic_idx = AUTO_PIN_FRONT_MIC;
443                         }
444                 } else {
445                         if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
446                                 spec->mic_detect = 1;
447                                 spec->automic_idx = AUTO_PIN_MIC;
448                         }
449                 }
450         }
451         return 0;
452 }
453
454
455 static int parse_digital_output(struct hda_codec *codec)
456 {
457         struct cs_spec *spec = codec->spec;
458         struct auto_pin_cfg *cfg = &spec->autocfg;
459         hda_nid_t nid;
460         int err;
461
462         if (!cfg->dig_outs)
463                 return 0;
464         if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
465                 return 0;
466         spec->multiout.dig_out_nid = nid;
467         spec->multiout.share_spdif = 1;
468         if (cfg->dig_outs > 1 &&
469             snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
470                 spec->slave_dig_outs[0] = nid;
471                 codec->slave_dig_outs = spec->slave_dig_outs;
472         }
473         return 0;
474 }
475
476 static int parse_digital_input(struct hda_codec *codec)
477 {
478         struct cs_spec *spec = codec->spec;
479         struct auto_pin_cfg *cfg = &spec->autocfg;
480         int idx;
481
482         if (cfg->dig_in_pin)
483                 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
484         return 0;
485 }
486
487 /*
488  * create mixer controls
489  */
490
491 static const char *dir_sfx[2] = { "Playback", "Capture" };
492
493 static int add_mute(struct hda_codec *codec, const char *name, int index,
494                     unsigned int pval, int dir, struct snd_kcontrol **kctlp)
495 {
496         char tmp[32];
497         struct snd_kcontrol_new knew =
498                 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
499         knew.private_value = pval;
500         snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
501         *kctlp = snd_ctl_new1(&knew, codec);
502         return snd_hda_ctl_add(codec, *kctlp);
503 }
504
505 static int add_volume(struct hda_codec *codec, const char *name,
506                       int index, unsigned int pval, int dir,
507                       struct snd_kcontrol **kctlp)
508 {
509         char tmp[32];
510         struct snd_kcontrol_new knew =
511                 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
512         knew.private_value = pval;
513         snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
514         *kctlp = snd_ctl_new1(&knew, codec);
515         return snd_hda_ctl_add(codec, *kctlp);
516 }
517
518 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
519 {
520         unsigned int caps;
521
522         /* set the upper-limit for mixer amp to 0dB */
523         caps = query_amp_caps(codec, dac, HDA_OUTPUT);
524         caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
525         caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
526                 << AC_AMPCAP_NUM_STEPS_SHIFT;
527         snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
528 }
529
530 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
531 {
532         struct cs_spec *spec = codec->spec;
533         unsigned int tlv[4];
534         int err;
535
536         spec->vmaster_sw =
537                 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
538         err = snd_hda_ctl_add(codec, spec->vmaster_sw);
539         if (err < 0)
540                 return err;
541
542         snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
543         spec->vmaster_vol =
544                 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
545         err = snd_hda_ctl_add(codec, spec->vmaster_vol);
546         if (err < 0)
547                 return err;
548         return 0;
549 }
550
551 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
552                       int num_ctls, int type)
553 {
554         struct cs_spec *spec = codec->spec;
555         const char *name;
556         int err, index;
557         struct snd_kcontrol *kctl;
558         static char *speakers[] = {
559                 "Front Speaker", "Surround Speaker", "Bass Speaker"
560         };
561         static char *line_outs[] = {
562                 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
563         };
564
565         fix_volume_caps(codec, dac);
566         if (!spec->vmaster_sw) {
567                 err = add_vmaster(codec, dac);
568                 if (err < 0)
569                         return err;
570         }
571
572         index = 0;
573         switch (type) {
574         case AUTO_PIN_HP_OUT:
575                 name = "Headphone";
576                 index = idx;
577                 break;
578         case AUTO_PIN_SPEAKER_OUT:
579                 if (num_ctls > 1)
580                         name = speakers[idx];
581                 else
582                         name = "Speaker";
583                 break;
584         default:
585                 if (num_ctls > 1)
586                         name = line_outs[idx];
587                 else
588                         name = "Line-Out";
589                 break;
590         }
591
592         err = add_mute(codec, name, index,
593                        HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
594         if (err < 0)
595                 return err;
596         err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
597         if (err < 0)
598                 return err;
599
600         err = add_volume(codec, name, index,
601                          HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
602         if (err < 0)
603                 return err;
604         err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
605         if (err < 0)
606                 return err;
607
608         return 0;
609 }               
610
611 static int build_output(struct hda_codec *codec)
612 {
613         struct cs_spec *spec = codec->spec;
614         struct auto_pin_cfg *cfg = &spec->autocfg;
615         int i, err;
616
617         for (i = 0; i < cfg->line_outs; i++) {
618                 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
619                                  i, cfg->line_outs, cfg->line_out_type);
620                 if (err < 0)
621                         return err;
622         }
623         for (i = 0; i < cfg->hp_outs; i++) {
624                 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
625                                  i, cfg->hp_outs, AUTO_PIN_HP_OUT);
626                 if (err < 0)
627                         return err;
628         }
629         for (i = 0; i < cfg->speaker_outs; i++) {
630                 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
631                                  i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
632                 if (err < 0)
633                         return err;
634         }
635         return 0;
636 }
637
638 /*
639  */
640
641 static struct snd_kcontrol_new cs_capture_ctls[] = {
642         HDA_BIND_SW("Capture Switch", 0),
643         HDA_BIND_VOL("Capture Volume", 0),
644 };
645
646 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
647                             int force)
648 {
649         struct cs_spec *spec = codec->spec;
650         struct auto_pin_cfg *cfg = &spec->autocfg;
651         
652         if (spec->cur_input == idx && !force)
653                 return 0;
654         if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
655                 /* stream is running, let's swap the current ADC */
656                 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
657                 spec->cur_adc = spec->adc_nid[idx];
658                 snd_hda_codec_setup_stream(codec, spec->cur_adc,
659                                            spec->cur_adc_stream_tag, 0,
660                                            spec->cur_adc_format);
661         }
662         snd_hda_codec_write(codec, spec->cur_adc, 0,
663                             AC_VERB_SET_CONNECT_SEL,
664                             spec->adc_idx[idx]);
665         spec->cur_input = idx;
666         return 1;
667 }
668
669 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
670                                   struct snd_ctl_elem_info *uinfo)
671 {
672         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
673         struct cs_spec *spec = codec->spec;
674         unsigned int idx;
675
676         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
677         uinfo->count = 1;
678         uinfo->value.enumerated.items = spec->num_inputs;
679         if (uinfo->value.enumerated.item >= spec->num_inputs)
680                 uinfo->value.enumerated.item = spec->num_inputs - 1;
681         idx = spec->input_idx[uinfo->value.enumerated.item];
682         strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
683         return 0;
684 }
685
686 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
687                                  struct snd_ctl_elem_value *ucontrol)
688 {
689         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
690         struct cs_spec *spec = codec->spec;
691         ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
692         return 0;
693 }
694
695 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
696                                  struct snd_ctl_elem_value *ucontrol)
697 {
698         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
699         struct cs_spec *spec = codec->spec;
700         unsigned int idx = ucontrol->value.enumerated.item[0];
701
702         if (idx >= spec->num_inputs)
703                 return -EINVAL;
704         idx = spec->input_idx[idx];
705         return change_cur_input(codec, idx, 0);
706 }
707
708 static struct snd_kcontrol_new cs_capture_source = {
709         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
710         .name = "Capture Source",
711         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
712         .info = cs_capture_source_info,
713         .get = cs_capture_source_get,
714         .put = cs_capture_source_put,
715 };
716
717 static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
718                                                struct hda_ctl_ops *ops)
719 {
720         struct cs_spec *spec = codec->spec;
721         struct hda_bind_ctls *bind;
722         int i, n;
723
724         bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
725                        GFP_KERNEL);
726         if (!bind)
727                 return NULL;
728         bind->ops = ops;
729         n = 0;
730         for (i = 0; i < AUTO_PIN_LAST; i++) {
731                 if (!spec->adc_nid[i])
732                         continue;
733                 bind->values[n++] =
734                         HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
735                                             spec->adc_idx[i], HDA_INPUT);
736         }
737         return bind;
738 }
739
740 static int build_input(struct hda_codec *codec)
741 {
742         struct cs_spec *spec = codec->spec;
743         int i, err;
744
745         if (!spec->num_inputs)
746                 return 0;
747
748         /* make bind-capture */
749         spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
750         spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
751         for (i = 0; i < 2; i++) {
752                 struct snd_kcontrol *kctl;
753                 if (!spec->capture_bind[i])
754                         return -ENOMEM;
755                 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
756                 if (!kctl)
757                         return -ENOMEM;
758                 kctl->private_value = (long)spec->capture_bind[i];
759                 err = snd_hda_ctl_add(codec, kctl);
760                 if (err < 0)
761                         return err;
762         }
763         
764         if (spec->num_inputs > 1 && !spec->mic_detect) {
765                 err = snd_hda_ctl_add(codec,
766                                       snd_ctl_new1(&cs_capture_source, codec));
767                 if (err < 0)
768                         return err;
769         }
770
771         return 0;
772 }
773
774 /*
775  */
776
777 static int build_digital_output(struct hda_codec *codec)
778 {
779         struct cs_spec *spec = codec->spec;
780         int err;
781
782         err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
783         if (err < 0)
784                 return err;
785         err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
786         if (err < 0)
787                 return err;
788         return 0;
789 }
790
791 static int build_digital_input(struct hda_codec *codec)
792 {
793         struct cs_spec *spec = codec->spec;
794         if (spec->dig_in)
795                 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
796         return 0;
797 }
798
799 /*
800  * auto-mute and auto-mic switching
801  */
802
803 static void cs_automute(struct hda_codec *codec)
804 {
805         struct cs_spec *spec = codec->spec;
806         struct auto_pin_cfg *cfg = &spec->autocfg;
807         unsigned int caps, present, hp_present;
808         hda_nid_t nid;
809         int i;
810
811         hp_present = 0;
812         for (i = 0; i < cfg->hp_outs; i++) {
813                 nid = cfg->hp_pins[i];
814                 caps = snd_hda_query_pin_caps(codec, nid);
815                 if (!(caps & AC_PINCAP_PRES_DETECT))
816                         continue;
817                 if (caps & AC_PINCAP_TRIG_REQ)
818                         snd_hda_codec_read(codec, nid, 0,
819                                            AC_VERB_SET_PIN_SENSE, 0);
820                 present = snd_hda_codec_read(codec, nid, 0,
821                                              AC_VERB_GET_PIN_SENSE, 0);
822                 hp_present |= (present & AC_PINSENSE_PRESENCE) != 0;
823                 if (hp_present)
824                         break;
825         }
826         for (i = 0; i < cfg->speaker_outs; i++) {
827                 nid = cfg->speaker_pins[i];
828                 snd_hda_codec_write(codec, nid, 0,
829                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
830                                     hp_present ? 0 : PIN_OUT);
831         }
832 }
833
834 static void cs_automic(struct hda_codec *codec)
835 {
836         struct cs_spec *spec = codec->spec;
837         struct auto_pin_cfg *cfg = &spec->autocfg;
838         hda_nid_t nid;
839         unsigned int caps, present;
840         
841         nid = cfg->input_pins[spec->automic_idx];
842         caps = snd_hda_query_pin_caps(codec, nid);
843         if (caps & AC_PINCAP_TRIG_REQ)
844                 snd_hda_codec_read(codec, nid, 0, AC_VERB_SET_PIN_SENSE, 0);
845         present = snd_hda_codec_read(codec, nid, 0,
846                                      AC_VERB_GET_PIN_SENSE, 0);
847         if (present & AC_PINSENSE_PRESENCE)
848                 change_cur_input(codec, spec->automic_idx, 0);
849         else {
850                 unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
851                         AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
852                 change_cur_input(codec, imic, 0);
853         }
854 }
855
856 /*
857  */
858
859 static void init_output(struct hda_codec *codec)
860 {
861         struct cs_spec *spec = codec->spec;
862         struct auto_pin_cfg *cfg = &spec->autocfg;
863         int i;
864
865         /* mute first */
866         for (i = 0; i < spec->multiout.num_dacs; i++)
867                 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
868                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
869         if (spec->multiout.hp_nid)
870                 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
871                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
872         for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
873                 if (!spec->multiout.extra_out_nid[i])
874                         break;
875                 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
876                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
877         }
878
879         /* set appropriate pin controls */
880         for (i = 0; i < cfg->line_outs; i++)
881                 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
882                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
883         for (i = 0; i < cfg->hp_outs; i++) {
884                 hda_nid_t nid = cfg->hp_pins[i];
885                 snd_hda_codec_write(codec, nid, 0,
886                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
887                 if (!cfg->speaker_outs)
888                         continue;
889                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
890                         snd_hda_codec_write(codec, nid, 0,
891                                             AC_VERB_SET_UNSOLICITED_ENABLE,
892                                             AC_USRSP_EN | HP_EVENT);
893                         spec->hp_detect = 1;
894                 }
895         }
896         for (i = 0; i < cfg->speaker_outs; i++)
897                 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
898                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
899         if (spec->hp_detect)
900                 cs_automute(codec);
901 }
902
903 static void init_input(struct hda_codec *codec)
904 {
905         struct cs_spec *spec = codec->spec;
906         struct auto_pin_cfg *cfg = &spec->autocfg;
907         unsigned int coef;
908         int i;
909
910         for (i = 0; i < AUTO_PIN_LAST; i++) {
911                 unsigned int ctl;
912                 hda_nid_t pin = cfg->input_pins[i];
913                 if (!pin || !spec->adc_nid[i])
914                         continue;
915                 /* set appropriate pin control and mute first */
916                 ctl = PIN_IN;
917                 if (i <= AUTO_PIN_FRONT_MIC) {
918                         unsigned int caps = snd_hda_query_pin_caps(codec, pin);
919                         caps >>= AC_PINCAP_VREF_SHIFT;
920                         if (caps & AC_PINCAP_VREF_80)
921                                 ctl = PIN_VREF80;
922                 }
923                 snd_hda_codec_write(codec, pin, 0,
924                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
925                 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
926                                     AC_VERB_SET_AMP_GAIN_MUTE,
927                                     AMP_IN_MUTE(spec->adc_idx[i]));
928                 if (spec->mic_detect && spec->automic_idx == i)
929                         snd_hda_codec_write(codec, pin, 0,
930                                             AC_VERB_SET_UNSOLICITED_ENABLE,
931                                             AC_USRSP_EN | MIC_EVENT);
932         }
933         change_cur_input(codec, spec->cur_input, 1);
934         if (spec->mic_detect)
935                 cs_automic(codec);
936
937         coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
938         if (is_active_pin(codec, CS_DMIC2_PIN_NID))
939                 coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
940         if (is_active_pin(codec, CS_DMIC1_PIN_NID))
941                 coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0 
942                                  * No effect if SPDIF_OUT2 is slected in 
943                                  * IDX_SPDIF_CTL.
944                                   */
945         cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
946 }
947
948 static struct hda_verb cs_coef_init_verbs[] = {
949         {0x11, AC_VERB_SET_PROC_STATE, 1},
950         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
951         {0x11, AC_VERB_SET_PROC_COEF,
952          (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
953           | 0x0040 /* Mute DACs on FIFO error */
954           | 0x1000 /* Enable DACs High Pass Filter */
955           | 0x0400 /* Disable Coefficient Auto increment */
956           )},
957         /* Beep */
958         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
959         {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
960
961         {} /* terminator */
962 };
963
964 /* SPDIF setup */
965 static void init_digital(struct hda_codec *codec)
966 {
967         unsigned int coef;
968
969         coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
970         coef |= 0x0008; /* Replace with mute on error */
971         if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
972                 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
973                                  * SPDIF_OUT2 is shared with GPIO1 and
974                                  * DMIC_SDA2.
975                                  */
976         cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
977 }
978
979 static int cs_init(struct hda_codec *codec)
980 {
981         struct cs_spec *spec = codec->spec;
982
983         snd_hda_sequence_write(codec, cs_coef_init_verbs);
984         init_output(codec);
985         init_input(codec);
986         init_digital(codec);
987         return 0;
988 }
989
990 static int cs_build_controls(struct hda_codec *codec)
991 {
992         struct cs_spec *spec = codec->spec;
993         int err;
994
995         err = build_output(codec);
996         if (err < 0)
997                 return err;
998         err = build_input(codec);
999         if (err < 0)
1000                 return err;
1001         err = build_digital_output(codec);
1002         if (err < 0)
1003                 return err;
1004         err = build_digital_input(codec);
1005         if (err < 0)
1006                 return err;
1007         return cs_init(codec);
1008 }
1009
1010 static void cs_free(struct hda_codec *codec)
1011 {
1012         struct cs_spec *spec = codec->spec;
1013         kfree(spec->capture_bind[0]);
1014         kfree(spec->capture_bind[1]);
1015         kfree(codec->spec);
1016 }
1017
1018 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1019 {
1020         switch ((res >> 26) & 0x7f) {
1021         case HP_EVENT:
1022                 cs_automute(codec);
1023                 break;
1024         case MIC_EVENT:
1025                 cs_automic(codec);
1026                 break;
1027         }
1028 }
1029
1030 static struct hda_codec_ops cs_patch_ops = {
1031         .build_controls = cs_build_controls,
1032         .build_pcms = cs_build_pcms,
1033         .init = cs_init,
1034         .free = cs_free,
1035         .unsol_event = cs_unsol_event,
1036 };
1037
1038 static int cs_parse_auto_config(struct hda_codec *codec)
1039 {
1040         struct cs_spec *spec = codec->spec;
1041         int err;
1042
1043         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1044         if (err < 0)
1045                 return err;
1046         return 0;
1047 }
1048
1049 static const char *cs420x_models[CS420X_MODELS] = {
1050         [CS420X_MBP55] = "mbp55",
1051         [CS420X_AUTO] = "auto",
1052 };
1053
1054
1055 static struct snd_pci_quirk cs420x_cfg_tbl[] = {
1056         SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1057         {} /* terminator */
1058 };
1059
1060 struct cs_pincfg {
1061         hda_nid_t nid;
1062         u32 val;
1063 };
1064
1065 static struct cs_pincfg mbp55_pincfgs[] = {
1066         { 0x09, 0x012b4030 },
1067         { 0x0a, 0x90100121 },
1068         { 0x0b, 0x90100120 },
1069         { 0x0c, 0x400000f0 },
1070         { 0x0d, 0x90a00110 },
1071         { 0x0e, 0x400000f0 },
1072         { 0x0f, 0x400000f0 },
1073         { 0x10, 0x014be040 },
1074         { 0x12, 0x400000f0 },
1075         { 0x15, 0x400000f0 },
1076         {} /* terminator */
1077 };
1078
1079 static struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1080         [CS420X_MBP55] = mbp55_pincfgs,
1081 };
1082
1083 static void fix_pincfg(struct hda_codec *codec, int model)
1084 {
1085         const struct cs_pincfg *cfg = cs_pincfgs[model];
1086         if (!cfg)
1087                 return;
1088         for (; cfg->nid; cfg++)
1089                 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1090 }
1091
1092
1093 static int patch_cs420x(struct hda_codec *codec)
1094 {
1095         struct cs_spec *spec;
1096         int err;
1097
1098         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1099         if (!spec)
1100                 return -ENOMEM;
1101         codec->spec = spec;
1102
1103         spec->board_config =
1104                 snd_hda_check_board_config(codec, CS420X_MODELS,
1105                                            cs420x_models, cs420x_cfg_tbl);
1106         if (spec->board_config >= 0)
1107                 fix_pincfg(codec, spec->board_config);
1108
1109         err = cs_parse_auto_config(codec);
1110         if (err < 0)
1111                 goto error;
1112
1113         err = parse_output(codec);
1114         if (err < 0)
1115                 goto error;
1116         err = parse_input(codec);
1117         if (err < 0)
1118                 goto error;
1119         err = parse_digital_output(codec);
1120         if (err < 0)
1121                 goto error;
1122         err = parse_digital_input(codec);
1123         if (err < 0)
1124                 goto error;
1125
1126         codec->patch_ops = cs_patch_ops;
1127
1128         return 0;
1129
1130  error:
1131         kfree(codec->spec);
1132         codec->spec = NULL;
1133         return err;
1134 }
1135
1136
1137 /*
1138  * patch entries
1139  */
1140 static struct hda_codec_preset snd_hda_preset_cirrus[] = {
1141         { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1142         { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1143         {} /* terminator */
1144 };
1145
1146 MODULE_ALIAS("snd-hda-codec-id:10134206");
1147 MODULE_ALIAS("snd-hda-codec-id:10134207");
1148
1149 MODULE_LICENSE("GPL");
1150 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1151
1152 static struct hda_codec_preset_list cirrus_list = {
1153         .preset = snd_hda_preset_cirrus,
1154         .owner = THIS_MODULE,
1155 };
1156
1157 static int __init patch_cirrus_init(void)
1158 {
1159         return snd_hda_add_codec_preset(&cirrus_list);
1160 }
1161
1162 static void __exit patch_cirrus_exit(void)
1163 {
1164         snd_hda_delete_codec_preset(&cirrus_list);
1165 }
1166
1167 module_init(patch_cirrus_init)
1168 module_exit(patch_cirrus_exit)