]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - sound/pci/hda/patch_cirrus.c
ALSA: hda - Fix cirrus codec parsing
[~shefty/rdma-dev.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 {
577         struct cs_spec *spec = codec->spec;
578         struct auto_pin_cfg *cfg = &spec->autocfg;
579         
580         if (spec->cur_input == idx)
581                 return 0;
582         if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
583                 /* stream is running, let's swap the current ADC */
584                 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
585                 spec->cur_adc = spec->adc_nid[idx];
586                 snd_hda_codec_setup_stream(codec, spec->cur_adc,
587                                            spec->cur_adc_stream_tag, 0,
588                                            spec->cur_adc_format);
589         }
590         snd_hda_codec_write(codec, spec->cur_adc, 0,
591                             AC_VERB_SET_CONNECT_SEL,
592                             spec->adc_idx[idx]);
593         spec->cur_input = idx;
594         return 1;
595 }
596
597 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
598                                   struct snd_ctl_elem_info *uinfo)
599 {
600         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
601         struct cs_spec *spec = codec->spec;
602         unsigned int idx;
603
604         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
605         uinfo->count = 1;
606         uinfo->value.enumerated.items = spec->num_inputs;
607         if (uinfo->value.enumerated.item >= spec->num_inputs)
608                 uinfo->value.enumerated.item = spec->num_inputs - 1;
609         idx = spec->input_idx[uinfo->value.enumerated.item];
610         strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
611         return 0;
612 }
613
614 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
615                                  struct snd_ctl_elem_value *ucontrol)
616 {
617         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
618         struct cs_spec *spec = codec->spec;
619         ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
620         return 0;
621 }
622
623 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
624                                  struct snd_ctl_elem_value *ucontrol)
625 {
626         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
627         struct cs_spec *spec = codec->spec;
628         unsigned int idx = ucontrol->value.enumerated.item[0];
629
630         if (idx >= spec->num_inputs)
631                 return -EINVAL;
632         idx = spec->input_idx[idx];
633         return change_cur_input(codec, idx);
634 }
635
636 static struct snd_kcontrol_new cs_capture_source = {
637         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
638         .name = "Capture Source",
639         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
640         .info = cs_capture_source_info,
641         .get = cs_capture_source_get,
642         .put = cs_capture_source_put,
643 };
644
645 static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
646                                                struct hda_ctl_ops *ops)
647 {
648         struct cs_spec *spec = codec->spec;
649         struct hda_bind_ctls *bind;
650         int i, n;
651
652         bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
653                        GFP_KERNEL);
654         if (!bind)
655                 return NULL;
656         bind->ops = ops;
657         n = 0;
658         for (i = 0; i < AUTO_PIN_LAST; i++) {
659                 if (!spec->adc_nid[i])
660                         continue;
661                 bind->values[n++] =
662                         HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
663                                             spec->adc_idx[i], HDA_INPUT);
664         }
665         return bind;
666 }
667
668 static int build_input(struct hda_codec *codec)
669 {
670         struct cs_spec *spec = codec->spec;
671         int i, err;
672
673         if (!spec->num_inputs)
674                 return 0;
675
676         /* make bind-capture */
677         spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
678         spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
679         for (i = 0; i < 2; i++) {
680                 struct snd_kcontrol *kctl;
681                 if (!spec->capture_bind[i])
682                         return -ENOMEM;
683                 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
684                 if (!kctl)
685                         return -ENOMEM;
686                 kctl->private_value = (long)spec->capture_bind[i];
687                 err = snd_hda_ctl_add(codec, kctl);
688                 if (err < 0)
689                         return err;
690         }
691         
692         if (spec->num_inputs > 1 && !spec->mic_detect) {
693                 err = snd_hda_ctl_add(codec,
694                                       snd_ctl_new1(&cs_capture_source, codec));
695                 if (err < 0)
696                         return err;
697         }
698
699         return 0;
700 }
701
702 /*
703  */
704
705 static int build_digital_output(struct hda_codec *codec)
706 {
707         struct cs_spec *spec = codec->spec;
708         int err;
709
710         err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
711         if (err < 0)
712                 return err;
713         err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
714         if (err < 0)
715                 return err;
716         return 0;
717 }
718
719 static int build_digital_input(struct hda_codec *codec)
720 {
721         struct cs_spec *spec = codec->spec;
722         if (spec->dig_in)
723                 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
724         return 0;
725 }
726
727 /*
728  * auto-mute and auto-mic switching
729  */
730
731 static void cs_automute(struct hda_codec *codec)
732 {
733         struct cs_spec *spec = codec->spec;
734         struct auto_pin_cfg *cfg = &spec->autocfg;
735         unsigned int caps, present, hp_present;
736         hda_nid_t nid;
737         int i;
738
739         hp_present = 0;
740         for (i = 0; i < cfg->hp_outs; i++) {
741                 nid = cfg->hp_pins[i];
742                 caps = snd_hda_query_pin_caps(codec, nid);
743                 if (!(caps & AC_PINCAP_PRES_DETECT))
744                         continue;
745                 if (caps & AC_PINCAP_TRIG_REQ)
746                         snd_hda_codec_read(codec, nid, 0,
747                                            AC_VERB_SET_PIN_SENSE, 0);
748                 present = snd_hda_codec_read(codec, nid, 0,
749                                              AC_VERB_GET_PIN_SENSE, 0);
750                 hp_present |= (present & AC_PINSENSE_PRESENCE) != 0;
751                 if (hp_present)
752                         break;
753         }
754         for (i = 0; i < cfg->speaker_outs; i++) {
755                 nid = cfg->speaker_pins[i];
756                 snd_hda_codec_write(codec, nid, 0,
757                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
758                                     hp_present ? 0 : PIN_OUT);
759         }
760 }
761
762 static void cs_automic(struct hda_codec *codec)
763 {
764         struct cs_spec *spec = codec->spec;
765         struct auto_pin_cfg *cfg = &spec->autocfg;
766         hda_nid_t nid;
767         unsigned int caps, present;
768         
769         nid = cfg->input_pins[spec->automic_idx];
770         caps = snd_hda_query_pin_caps(codec, nid);
771         if (caps & AC_PINCAP_TRIG_REQ)
772                 snd_hda_codec_read(codec, nid, 0, AC_VERB_SET_PIN_SENSE, 0);
773         present = snd_hda_codec_read(codec, nid, 0,
774                                      AC_VERB_GET_PIN_SENSE, 0);
775         if (present & AC_PINSENSE_PRESENCE)
776                 change_cur_input(codec, spec->automic_idx);
777         else {
778                 unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
779                         AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
780                 change_cur_input(codec, imic);
781         }
782 }
783
784 /*
785  */
786
787 static void init_output(struct hda_codec *codec)
788 {
789         struct cs_spec *spec = codec->spec;
790         struct auto_pin_cfg *cfg = &spec->autocfg;
791         int i;
792
793         /* mute first */
794         for (i = 0; i < spec->multiout.num_dacs; i++)
795                 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
796                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
797         if (spec->multiout.hp_nid)
798                 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
799                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
800         for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
801                 if (!spec->multiout.extra_out_nid[i])
802                         break;
803                 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
804                                     AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
805         }
806
807         /* set appropriate pin controls */
808         for (i = 0; i < cfg->line_outs; i++)
809                 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
810                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
811         for (i = 0; i < cfg->hp_outs; i++) {
812                 hda_nid_t nid = cfg->hp_pins[i];
813                 snd_hda_codec_write(codec, nid, 0,
814                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
815                 if (!cfg->speaker_outs)
816                         continue;
817                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
818                         snd_hda_codec_write(codec, nid, 0,
819                                             AC_VERB_SET_UNSOLICITED_ENABLE,
820                                             AC_USRSP_EN | HP_EVENT);
821                         spec->hp_detect = 1;
822                 }
823         }
824         for (i = 0; i < cfg->speaker_outs; i++)
825                 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
826                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
827         if (spec->hp_detect)
828                 cs_automute(codec);
829 }
830
831 static void init_input(struct hda_codec *codec)
832 {
833         struct cs_spec *spec = codec->spec;
834         struct auto_pin_cfg *cfg = &spec->autocfg;
835         int i;
836
837         for (i = 0; i < AUTO_PIN_LAST; i++) {
838                 unsigned int ctl;
839                 hda_nid_t pin = cfg->input_pins[i];
840                 if (!pin || !spec->adc_nid[i])
841                         continue;
842                 /* set appropriate pin control and mute first */
843                 ctl = PIN_IN;
844                 if (i <= AUTO_PIN_FRONT_MIC) {
845                         unsigned int caps = snd_hda_query_pin_caps(codec, pin);
846                         caps >>= AC_PINCAP_VREF_SHIFT;
847                         if (caps & AC_PINCAP_VREF_80)
848                                 ctl = PIN_VREF80;
849                 }
850                 snd_hda_codec_write(codec, pin, 0,
851                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
852                 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
853                                     AC_VERB_SET_AMP_GAIN_MUTE,
854                                     AMP_IN_MUTE(spec->adc_idx[i]));
855                 if (spec->mic_detect && spec->automic_idx == i)
856                         snd_hda_codec_write(codec, pin, 0,
857                                             AC_VERB_SET_UNSOLICITED_ENABLE,
858                                             AC_USRSP_EN | MIC_EVENT);
859         }
860         if (spec->mic_detect)
861                 cs_automic(codec);
862 }
863
864 static int cs_init(struct hda_codec *codec)
865 {
866         struct cs_spec *spec = codec->spec;
867
868         init_output(codec);
869         init_input(codec);
870         return 0;
871 }
872
873 static int cs_build_controls(struct hda_codec *codec)
874 {
875         struct cs_spec *spec = codec->spec;
876         int err;
877
878         err = build_output(codec);
879         if (err < 0)
880                 return err;
881         err = build_input(codec);
882         if (err < 0)
883                 return err;
884         err = build_digital_output(codec);
885         if (err < 0)
886                 return err;
887         err = build_digital_input(codec);
888         if (err < 0)
889                 return err;
890         return cs_init(codec);
891 }
892
893 static void cs_free(struct hda_codec *codec)
894 {
895         struct cs_spec *spec = codec->spec;
896         kfree(spec->capture_bind[0]);
897         kfree(spec->capture_bind[1]);
898         kfree(codec->spec);
899 }
900
901 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
902 {
903         switch ((res >> 26) & 0x7f) {
904         case HP_EVENT:
905                 cs_automute(codec);
906                 break;
907         case MIC_EVENT:
908                 cs_automic(codec);
909                 break;
910         }
911 }
912
913 static struct hda_codec_ops cs_patch_ops = {
914         .build_controls = cs_build_controls,
915         .build_pcms = cs_build_pcms,
916         .init = cs_init,
917         .free = cs_free,
918         .unsol_event = cs_unsol_event,
919 };
920
921 static int cs_parse_auto_config(struct hda_codec *codec)
922 {
923         struct cs_spec *spec = codec->spec;
924         int err;
925
926         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
927         if (err < 0)
928                 return err;
929         return 0;
930 }
931
932
933 static int patch_cs420x(struct hda_codec *codec)
934 {
935         struct cs_spec *spec;
936         int err;
937
938         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
939         if (!spec)
940                 return -ENOMEM;
941         codec->spec = spec;
942
943         err = cs_parse_auto_config(codec);
944         if (err < 0)
945                 goto error;
946
947         err = parse_output(codec);
948         if (err < 0)
949                 goto error;
950         err = parse_input(codec);
951         if (err < 0)
952                 goto error;
953         err = parse_digital_output(codec);
954         if (err < 0)
955                 goto error;
956         err = parse_digital_input(codec);
957         if (err < 0)
958                 goto error;
959
960         codec->patch_ops = cs_patch_ops;
961
962         return 0;
963
964  error:
965         kfree(codec->spec);
966         codec->spec = NULL;
967         return err;
968 }
969
970
971 /*
972  * patch entries
973  */
974 static struct hda_codec_preset snd_hda_preset_cirrus[] = {
975         { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
976         { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
977         {} /* terminator */
978 };
979
980 MODULE_ALIAS("snd-hda-codec-id:10134206");
981 MODULE_ALIAS("snd-hda-codec-id:10134207");
982
983 MODULE_LICENSE("GPL");
984 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
985
986 static struct hda_codec_preset_list cirrus_list = {
987         .preset = snd_hda_preset_cirrus,
988         .owner = THIS_MODULE,
989 };
990
991 static int __init patch_cirrus_init(void)
992 {
993         return snd_hda_add_codec_preset(&cirrus_list);
994 }
995
996 static void __exit patch_cirrus_exit(void)
997 {
998         snd_hda_delete_codec_preset(&cirrus_list);
999 }
1000
1001 module_init(patch_cirrus_init)
1002 module_exit(patch_cirrus_exit)