ALSA: hda - Add CS420x-specific coef setup
[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                 return 0;
476         spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
477         if (!spec->dig_in)
478                 return 0;
479         return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
480 }
481
482 /*
483  * create mixer controls
484  */
485
486 static const char *dir_sfx[2] = { "Playback", "Capture" };
487
488 static int add_mute(struct hda_codec *codec, const char *name, int index,
489                     unsigned int pval, int dir, struct snd_kcontrol **kctlp)
490 {
491         char tmp[32];
492         struct snd_kcontrol_new knew =
493                 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
494         knew.private_value = pval;
495         snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
496         *kctlp = snd_ctl_new1(&knew, codec);
497         return snd_hda_ctl_add(codec, *kctlp);
498 }
499
500 static int add_volume(struct hda_codec *codec, const char *name,
501                       int index, unsigned int pval, int dir,
502                       struct snd_kcontrol **kctlp)
503 {
504         char tmp[32];
505         struct snd_kcontrol_new knew =
506                 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
507         knew.private_value = pval;
508         snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
509         *kctlp = snd_ctl_new1(&knew, codec);
510         return snd_hda_ctl_add(codec, *kctlp);
511 }
512
513 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
514 {
515         unsigned int caps;
516
517         /* set the upper-limit for mixer amp to 0dB */
518         caps = query_amp_caps(codec, dac, HDA_OUTPUT);
519         caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
520         caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
521                 << AC_AMPCAP_NUM_STEPS_SHIFT;
522         snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
523 }
524
525 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
526 {
527         struct cs_spec *spec = codec->spec;
528         unsigned int tlv[4];
529         int err;
530
531         spec->vmaster_sw =
532                 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
533         err = snd_hda_ctl_add(codec, spec->vmaster_sw);
534         if (err < 0)
535                 return err;
536
537         snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
538         spec->vmaster_vol =
539                 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
540         err = snd_hda_ctl_add(codec, spec->vmaster_vol);
541         if (err < 0)
542                 return err;
543         return 0;
544 }
545
546 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
547                       int num_ctls, int type)
548 {
549         struct cs_spec *spec = codec->spec;
550         const char *name;
551         int err, index;
552         struct snd_kcontrol *kctl;
553         static char *speakers[] = {
554                 "Front Speaker", "Surround Speaker", "Bass Speaker"
555         };
556         static char *line_outs[] = {
557                 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
558         };
559
560         fix_volume_caps(codec, dac);
561         if (!spec->vmaster_sw) {
562                 err = add_vmaster(codec, dac);
563                 if (err < 0)
564                         return err;
565         }
566
567         index = 0;
568         switch (type) {
569         case AUTO_PIN_HP_OUT:
570                 name = "Headphone";
571                 index = idx;
572                 break;
573         case AUTO_PIN_SPEAKER_OUT:
574                 if (num_ctls > 1)
575                         name = speakers[idx];
576                 else
577                         name = "Speaker";
578                 break;
579         default:
580                 if (num_ctls > 1)
581                         name = line_outs[idx];
582                 else
583                         name = "Line-Out";
584                 break;
585         }
586
587         err = add_mute(codec, name, index,
588                        HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
589         if (err < 0)
590                 return err;
591         err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
592         if (err < 0)
593                 return err;
594
595         err = add_volume(codec, name, index,
596                          HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
597         if (err < 0)
598                 return err;
599         err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
600         if (err < 0)
601                 return err;
602
603         return 0;
604 }               
605
606 static int build_output(struct hda_codec *codec)
607 {
608         struct cs_spec *spec = codec->spec;
609         struct auto_pin_cfg *cfg = &spec->autocfg;
610         int i, err;
611
612         for (i = 0; i < cfg->line_outs; i++) {
613                 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
614                                  i, cfg->line_outs, cfg->line_out_type);
615                 if (err < 0)
616                         return err;
617         }
618         for (i = 0; i < cfg->hp_outs; i++) {
619                 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
620                                  i, cfg->hp_outs, AUTO_PIN_HP_OUT);
621                 if (err < 0)
622                         return err;
623         }
624         for (i = 0; i < cfg->speaker_outs; i++) {
625                 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
626                                  i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
627                 if (err < 0)
628                         return err;
629         }
630         return 0;
631 }
632
633 /*
634  */
635
636 static struct snd_kcontrol_new cs_capture_ctls[] = {
637         HDA_BIND_SW("Capture Switch", 0),
638         HDA_BIND_VOL("Capture Volume", 0),
639 };
640
641 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
642                             int force)
643 {
644         struct cs_spec *spec = codec->spec;
645         struct auto_pin_cfg *cfg = &spec->autocfg;
646         
647         if (spec->cur_input == idx && !force)
648                 return 0;
649         if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
650                 /* stream is running, let's swap the current ADC */
651                 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
652                 spec->cur_adc = spec->adc_nid[idx];
653                 snd_hda_codec_setup_stream(codec, spec->cur_adc,
654                                            spec->cur_adc_stream_tag, 0,
655                                            spec->cur_adc_format);
656         }
657         snd_hda_codec_write(codec, spec->cur_adc, 0,
658                             AC_VERB_SET_CONNECT_SEL,
659                             spec->adc_idx[idx]);
660         spec->cur_input = idx;
661         return 1;
662 }
663
664 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
665                                   struct snd_ctl_elem_info *uinfo)
666 {
667         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
668         struct cs_spec *spec = codec->spec;
669         unsigned int idx;
670
671         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
672         uinfo->count = 1;
673         uinfo->value.enumerated.items = spec->num_inputs;
674         if (uinfo->value.enumerated.item >= spec->num_inputs)
675                 uinfo->value.enumerated.item = spec->num_inputs - 1;
676         idx = spec->input_idx[uinfo->value.enumerated.item];
677         strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
678         return 0;
679 }
680
681 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
682                                  struct snd_ctl_elem_value *ucontrol)
683 {
684         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
685         struct cs_spec *spec = codec->spec;
686         ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
687         return 0;
688 }
689
690 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
691                                  struct snd_ctl_elem_value *ucontrol)
692 {
693         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
694         struct cs_spec *spec = codec->spec;
695         unsigned int idx = ucontrol->value.enumerated.item[0];
696
697         if (idx >= spec->num_inputs)
698                 return -EINVAL;
699         idx = spec->input_idx[idx];
700         return change_cur_input(codec, idx, 0);
701 }
702
703 static struct snd_kcontrol_new cs_capture_source = {
704         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
705         .name = "Capture Source",
706         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
707         .info = cs_capture_source_info,
708         .get = cs_capture_source_get,
709         .put = cs_capture_source_put,
710 };
711
712 static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
713                                                struct hda_ctl_ops *ops)
714 {
715         struct cs_spec *spec = codec->spec;
716         struct hda_bind_ctls *bind;
717         int i, n;
718
719         bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
720                        GFP_KERNEL);
721         if (!bind)
722                 return NULL;
723         bind->ops = ops;
724         n = 0;
725         for (i = 0; i < AUTO_PIN_LAST; i++) {
726                 if (!spec->adc_nid[i])
727                         continue;
728                 bind->values[n++] =
729                         HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
730                                             spec->adc_idx[i], HDA_INPUT);
731         }
732         return bind;
733 }
734
735 static int build_input(struct hda_codec *codec)
736 {
737         struct cs_spec *spec = codec->spec;
738         int i, err;
739
740         if (!spec->num_inputs)
741                 return 0;
742
743         /* make bind-capture */
744         spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
745         spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
746         for (i = 0; i < 2; i++) {
747                 struct snd_kcontrol *kctl;
748                 if (!spec->capture_bind[i])
749                         return -ENOMEM;
750                 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
751                 if (!kctl)
752                         return -ENOMEM;
753                 kctl->private_value = (long)spec->capture_bind[i];
754                 err = snd_hda_ctl_add(codec, kctl);
755                 if (err < 0)
756                         return err;
757         }
758         
759         if (spec->num_inputs > 1 && !spec->mic_detect) {
760                 err = snd_hda_ctl_add(codec,
761                                       snd_ctl_new1(&cs_capture_source, codec));
762                 if (err < 0)
763                         return err;
764         }
765
766         return 0;
767 }
768
769 /*
770  */
771
772 static int build_digital_output(struct hda_codec *codec)
773 {
774         struct cs_spec *spec = codec->spec;
775         int err;
776
777         err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
778         if (err < 0)
779                 return err;
780         err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
781         if (err < 0)
782                 return err;
783         return 0;
784 }
785
786 static int build_digital_input(struct hda_codec *codec)
787 {
788         struct cs_spec *spec = codec->spec;
789         if (spec->dig_in)
790                 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
791         return 0;
792 }
793
794 /*
795  * auto-mute and auto-mic switching
796  */
797
798 static void cs_automute(struct hda_codec *codec)
799 {
800         struct cs_spec *spec = codec->spec;
801         struct auto_pin_cfg *cfg = &spec->autocfg;
802         unsigned int caps, present, hp_present;
803         hda_nid_t nid;
804         int i;
805
806         hp_present = 0;
807         for (i = 0; i < cfg->hp_outs; i++) {
808                 nid = cfg->hp_pins[i];
809                 caps = snd_hda_query_pin_caps(codec, nid);
810                 if (!(caps & AC_PINCAP_PRES_DETECT))
811                         continue;
812                 if (caps & AC_PINCAP_TRIG_REQ)
813                         snd_hda_codec_read(codec, nid, 0,
814                                            AC_VERB_SET_PIN_SENSE, 0);
815                 present = snd_hda_codec_read(codec, nid, 0,
816                                              AC_VERB_GET_PIN_SENSE, 0);
817                 hp_present |= (present & AC_PINSENSE_PRESENCE) != 0;
818                 if (hp_present)
819                         break;
820         }
821         for (i = 0; i < cfg->speaker_outs; i++) {
822                 nid = cfg->speaker_pins[i];
823                 snd_hda_codec_write(codec, nid, 0,
824                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
825                                     hp_present ? 0 : PIN_OUT);
826         }
827 }
828
829 static void cs_automic(struct hda_codec *codec)
830 {
831         struct cs_spec *spec = codec->spec;
832         struct auto_pin_cfg *cfg = &spec->autocfg;
833         hda_nid_t nid;
834         unsigned int caps, present;
835         
836         nid = cfg->input_pins[spec->automic_idx];
837         caps = snd_hda_query_pin_caps(codec, nid);
838         if (caps & AC_PINCAP_TRIG_REQ)
839                 snd_hda_codec_read(codec, nid, 0, AC_VERB_SET_PIN_SENSE, 0);
840         present = snd_hda_codec_read(codec, nid, 0,
841                                      AC_VERB_GET_PIN_SENSE, 0);
842         if (present & AC_PINSENSE_PRESENCE)
843                 change_cur_input(codec, spec->automic_idx, 0);
844         else {
845                 unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
846                         AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
847                 change_cur_input(codec, imic, 0);
848         }
849 }
850
851 /*
852  */
853
854 static void init_output(struct hda_codec *codec)
855 {
856         struct cs_spec *spec = codec->spec;
857         struct auto_pin_cfg *cfg = &spec->autocfg;
858         int i;
859
860         /* mute first */
861         for (i = 0; i < spec->multiout.num_dacs; i++)
862                 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
863                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
864         if (spec->multiout.hp_nid)
865                 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
866                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
867         for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
868                 if (!spec->multiout.extra_out_nid[i])
869                         break;
870                 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
871                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
872         }
873
874         /* set appropriate pin controls */
875         for (i = 0; i < cfg->line_outs; i++)
876                 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
877                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
878         for (i = 0; i < cfg->hp_outs; i++) {
879                 hda_nid_t nid = cfg->hp_pins[i];
880                 snd_hda_codec_write(codec, nid, 0,
881                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
882                 if (!cfg->speaker_outs)
883                         continue;
884                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
885                         snd_hda_codec_write(codec, nid, 0,
886                                             AC_VERB_SET_UNSOLICITED_ENABLE,
887                                             AC_USRSP_EN | HP_EVENT);
888                         spec->hp_detect = 1;
889                 }
890         }
891         for (i = 0; i < cfg->speaker_outs; i++)
892                 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
893                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
894         if (spec->hp_detect)
895                 cs_automute(codec);
896 }
897
898 static void init_input(struct hda_codec *codec)
899 {
900         struct cs_spec *spec = codec->spec;
901         struct auto_pin_cfg *cfg = &spec->autocfg;
902         unsigned int coef;
903         int i;
904
905         for (i = 0; i < AUTO_PIN_LAST; i++) {
906                 unsigned int ctl;
907                 hda_nid_t pin = cfg->input_pins[i];
908                 if (!pin || !spec->adc_nid[i])
909                         continue;
910                 /* set appropriate pin control and mute first */
911                 ctl = PIN_IN;
912                 if (i <= AUTO_PIN_FRONT_MIC) {
913                         unsigned int caps = snd_hda_query_pin_caps(codec, pin);
914                         caps >>= AC_PINCAP_VREF_SHIFT;
915                         if (caps & AC_PINCAP_VREF_80)
916                                 ctl = PIN_VREF80;
917                 }
918                 snd_hda_codec_write(codec, pin, 0,
919                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
920                 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
921                                     AC_VERB_SET_AMP_GAIN_MUTE,
922                                     AMP_IN_MUTE(spec->adc_idx[i]));
923                 if (spec->mic_detect && spec->automic_idx == i)
924                         snd_hda_codec_write(codec, pin, 0,
925                                             AC_VERB_SET_UNSOLICITED_ENABLE,
926                                             AC_USRSP_EN | MIC_EVENT);
927         }
928         change_cur_input(codec, spec->cur_input, 1);
929         if (spec->mic_detect)
930                 cs_automic(codec);
931
932         coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
933         if (is_active_pin(codec, CS_DMIC2_PIN_NID))
934                 coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
935         if (is_active_pin(codec, CS_DMIC1_PIN_NID))
936                 coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0 
937                                  * No effect if SPDIF_OUT2 is slected in 
938                                  * IDX_SPDIF_CTL.
939                                   */
940         cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
941 }
942
943 static struct hda_verb cs_coef_init_verbs[] = {
944         {0x11, AC_VERB_SET_PROC_STATE, 1},
945         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
946         {0x11, AC_VERB_SET_PROC_COEF,
947          (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
948           | 0x0040 /* Mute DACs on FIFO error */
949           | 0x1000 /* Enable DACs High Pass Filter */
950           | 0x0400 /* Disable Coefficient Auto increment */
951           )},
952         /* Beep */
953         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
954         {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
955
956         {} /* terminator */
957 };
958
959 /* SPDIF setup */
960 static void init_digital(struct hda_codec *codec)
961 {
962         unsigned int coef;
963
964         coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
965         coef |= 0x0008; /* Replace with mute on error */
966         if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
967                 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
968                                  * SPDIF_OUT2 is shared with GPIO1 and
969                                  * DMIC_SDA2.
970                                  */
971         cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
972 }
973
974 static int cs_init(struct hda_codec *codec)
975 {
976         struct cs_spec *spec = codec->spec;
977
978         snd_hda_sequence_write(codec, cs_coef_init_verbs);
979         init_output(codec);
980         init_input(codec);
981         init_digital(codec);
982         return 0;
983 }
984
985 static int cs_build_controls(struct hda_codec *codec)
986 {
987         struct cs_spec *spec = codec->spec;
988         int err;
989
990         err = build_output(codec);
991         if (err < 0)
992                 return err;
993         err = build_input(codec);
994         if (err < 0)
995                 return err;
996         err = build_digital_output(codec);
997         if (err < 0)
998                 return err;
999         err = build_digital_input(codec);
1000         if (err < 0)
1001                 return err;
1002         return cs_init(codec);
1003 }
1004
1005 static void cs_free(struct hda_codec *codec)
1006 {
1007         struct cs_spec *spec = codec->spec;
1008         kfree(spec->capture_bind[0]);
1009         kfree(spec->capture_bind[1]);
1010         kfree(codec->spec);
1011 }
1012
1013 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1014 {
1015         switch ((res >> 26) & 0x7f) {
1016         case HP_EVENT:
1017                 cs_automute(codec);
1018                 break;
1019         case MIC_EVENT:
1020                 cs_automic(codec);
1021                 break;
1022         }
1023 }
1024
1025 static struct hda_codec_ops cs_patch_ops = {
1026         .build_controls = cs_build_controls,
1027         .build_pcms = cs_build_pcms,
1028         .init = cs_init,
1029         .free = cs_free,
1030         .unsol_event = cs_unsol_event,
1031 };
1032
1033 static int cs_parse_auto_config(struct hda_codec *codec)
1034 {
1035         struct cs_spec *spec = codec->spec;
1036         int err;
1037
1038         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1039         if (err < 0)
1040                 return err;
1041         return 0;
1042 }
1043
1044
1045 static int patch_cs420x(struct hda_codec *codec)
1046 {
1047         struct cs_spec *spec;
1048         int err;
1049
1050         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1051         if (!spec)
1052                 return -ENOMEM;
1053         codec->spec = spec;
1054
1055         err = cs_parse_auto_config(codec);
1056         if (err < 0)
1057                 goto error;
1058
1059         err = parse_output(codec);
1060         if (err < 0)
1061                 goto error;
1062         err = parse_input(codec);
1063         if (err < 0)
1064                 goto error;
1065         err = parse_digital_output(codec);
1066         if (err < 0)
1067                 goto error;
1068         err = parse_digital_input(codec);
1069         if (err < 0)
1070                 goto error;
1071
1072         codec->patch_ops = cs_patch_ops;
1073
1074         return 0;
1075
1076  error:
1077         kfree(codec->spec);
1078         codec->spec = NULL;
1079         return err;
1080 }
1081
1082
1083 /*
1084  * patch entries
1085  */
1086 static struct hda_codec_preset snd_hda_preset_cirrus[] = {
1087         { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1088         { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1089         {} /* terminator */
1090 };
1091
1092 MODULE_ALIAS("snd-hda-codec-id:10134206");
1093 MODULE_ALIAS("snd-hda-codec-id:10134207");
1094
1095 MODULE_LICENSE("GPL");
1096 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1097
1098 static struct hda_codec_preset_list cirrus_list = {
1099         .preset = snd_hda_preset_cirrus,
1100         .owner = THIS_MODULE,
1101 };
1102
1103 static int __init patch_cirrus_init(void)
1104 {
1105         return snd_hda_add_codec_preset(&cirrus_list);
1106 }
1107
1108 static void __exit patch_cirrus_exit(void)
1109 {
1110         snd_hda_delete_codec_preset(&cirrus_list);
1111 }
1112
1113 module_init(patch_cirrus_init)
1114 module_exit(patch_cirrus_exit)