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