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