ALSA: hda - Fix the connection selection of ADCs on Cirrus codecs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / pci / hda / patch_cirrus.c
blob2fbab8e29576bb236ce51fd5f4050bc0283843b6
1 /*
2 * HD audio interface patch for Cirrus Logic CS420x chip
4 * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
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.
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.
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
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/pci.h>
25 #include <linux/module.h>
26 #include <sound/core.h>
27 #include "hda_codec.h"
28 #include "hda_local.h"
29 #include <sound/tlv.h>
34 struct cs_spec {
35 int board_config;
36 struct auto_pin_cfg autocfg;
37 struct hda_multi_out multiout;
38 struct snd_kcontrol *vmaster_sw;
39 struct snd_kcontrol *vmaster_vol;
41 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
42 hda_nid_t slave_dig_outs[2];
44 unsigned int input_idx[AUTO_PIN_LAST];
45 unsigned int capsrc_idx[AUTO_PIN_LAST];
46 hda_nid_t adc_nid[AUTO_PIN_LAST];
47 unsigned int adc_idx[AUTO_PIN_LAST];
48 unsigned int num_inputs;
49 unsigned int cur_input;
50 unsigned int automic_idx;
51 hda_nid_t cur_adc;
52 unsigned int cur_adc_stream_tag;
53 unsigned int cur_adc_format;
54 hda_nid_t dig_in;
56 const struct hda_bind_ctls *capture_bind[2];
58 unsigned int gpio_mask;
59 unsigned int gpio_dir;
60 unsigned int gpio_data;
62 struct hda_pcm pcm_rec[2]; /* PCM information */
64 unsigned int hp_detect:1;
65 unsigned int mic_detect:1;
66 /* CS421x */
67 unsigned int spdif_detect:1;
68 unsigned int sense_b:1;
69 hda_nid_t vendor_nid;
70 struct hda_input_mux input_mux;
71 unsigned int last_input;
74 /* available models with CS420x */
75 enum {
76 CS420X_MBP53,
77 CS420X_MBP55,
78 CS420X_IMAC27,
79 CS420X_AUTO,
80 CS420X_MODELS
83 /* CS421x boards */
84 enum {
85 CS421X_CDB4210,
86 CS421X_MODELS
89 /* Vendor-specific processing widget */
90 #define CS420X_VENDOR_NID 0x11
91 #define CS_DIG_OUT1_PIN_NID 0x10
92 #define CS_DIG_OUT2_PIN_NID 0x15
93 #define CS_DMIC1_PIN_NID 0x12
94 #define CS_DMIC2_PIN_NID 0x0e
96 /* coef indices */
97 #define IDX_SPDIF_STAT 0x0000
98 #define IDX_SPDIF_CTL 0x0001
99 #define IDX_ADC_CFG 0x0002
100 /* SZC bitmask, 4 modes below:
101 * 0 = immediate,
102 * 1 = digital immediate, analog zero-cross
103 * 2 = digtail & analog soft-ramp
104 * 3 = digital soft-ramp, analog zero-cross
106 #define CS_COEF_ADC_SZC_MASK (3 << 0)
107 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
108 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
109 /* PGA mode: 0 = differential, 1 = signle-ended */
110 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
111 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
112 #define IDX_DAC_CFG 0x0003
113 /* SZC bitmask, 4 modes below:
114 * 0 = Immediate
115 * 1 = zero-cross
116 * 2 = soft-ramp
117 * 3 = soft-ramp on zero-cross
119 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
120 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
121 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
123 #define IDX_BEEP_CFG 0x0004
124 /* 0x0008 - test reg key */
125 /* 0x0009 - 0x0014 -> 12 test regs */
126 /* 0x0015 - visibility reg */
129 * Cirrus Logic CS4210
131 * 1 DAC => HP(sense) / Speakers,
132 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
133 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
135 #define CS4210_DAC_NID 0x02
136 #define CS4210_ADC_NID 0x03
137 #define CS421X_VENDOR_NID 0x0B
138 #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
139 #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
141 #define CS421X_IDX_DEV_CFG 0x01
142 #define CS421X_IDX_ADC_CFG 0x02
143 #define CS421X_IDX_DAC_CFG 0x03
144 #define CS421X_IDX_SPK_CTL 0x04
146 #define SPDIF_EVENT 0x04
148 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
150 struct cs_spec *spec = codec->spec;
151 snd_hda_codec_write(codec, spec->vendor_nid, 0,
152 AC_VERB_SET_COEF_INDEX, idx);
153 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
154 AC_VERB_GET_PROC_COEF, 0);
157 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
158 unsigned int coef)
160 struct cs_spec *spec = codec->spec;
161 snd_hda_codec_write(codec, spec->vendor_nid, 0,
162 AC_VERB_SET_COEF_INDEX, idx);
163 snd_hda_codec_write(codec, spec->vendor_nid, 0,
164 AC_VERB_SET_PROC_COEF, coef);
168 #define HP_EVENT 1
169 #define MIC_EVENT 2
172 * PCM callbacks
174 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
175 struct hda_codec *codec,
176 struct snd_pcm_substream *substream)
178 struct cs_spec *spec = codec->spec;
179 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
180 hinfo);
183 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
184 struct hda_codec *codec,
185 unsigned int stream_tag,
186 unsigned int format,
187 struct snd_pcm_substream *substream)
189 struct cs_spec *spec = codec->spec;
190 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
191 stream_tag, format, substream);
194 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
195 struct hda_codec *codec,
196 struct snd_pcm_substream *substream)
198 struct cs_spec *spec = codec->spec;
199 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
203 * Digital out
205 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
206 struct hda_codec *codec,
207 struct snd_pcm_substream *substream)
209 struct cs_spec *spec = codec->spec;
210 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
213 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
214 struct hda_codec *codec,
215 struct snd_pcm_substream *substream)
217 struct cs_spec *spec = codec->spec;
218 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
221 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
222 struct hda_codec *codec,
223 unsigned int stream_tag,
224 unsigned int format,
225 struct snd_pcm_substream *substream)
227 struct cs_spec *spec = codec->spec;
228 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
229 format, substream);
232 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
233 struct hda_codec *codec,
234 struct snd_pcm_substream *substream)
236 struct cs_spec *spec = codec->spec;
237 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
240 static void cs_update_input_select(struct hda_codec *codec)
242 struct cs_spec *spec = codec->spec;
243 if (spec->cur_adc)
244 snd_hda_codec_write(codec, spec->cur_adc, 0,
245 AC_VERB_SET_CONNECT_SEL,
246 spec->adc_idx[spec->cur_input]);
250 * Analog capture
252 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
253 struct hda_codec *codec,
254 unsigned int stream_tag,
255 unsigned int format,
256 struct snd_pcm_substream *substream)
258 struct cs_spec *spec = codec->spec;
259 spec->cur_adc = spec->adc_nid[spec->cur_input];
260 spec->cur_adc_stream_tag = stream_tag;
261 spec->cur_adc_format = format;
262 cs_update_input_select(codec);
263 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
264 return 0;
267 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
268 struct hda_codec *codec,
269 struct snd_pcm_substream *substream)
271 struct cs_spec *spec = codec->spec;
272 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
273 spec->cur_adc = 0;
274 return 0;
279 static const struct hda_pcm_stream cs_pcm_analog_playback = {
280 .substreams = 1,
281 .channels_min = 2,
282 .channels_max = 2,
283 .ops = {
284 .open = cs_playback_pcm_open,
285 .prepare = cs_playback_pcm_prepare,
286 .cleanup = cs_playback_pcm_cleanup
290 static const struct hda_pcm_stream cs_pcm_analog_capture = {
291 .substreams = 1,
292 .channels_min = 2,
293 .channels_max = 2,
294 .ops = {
295 .prepare = cs_capture_pcm_prepare,
296 .cleanup = cs_capture_pcm_cleanup
300 static const struct hda_pcm_stream cs_pcm_digital_playback = {
301 .substreams = 1,
302 .channels_min = 2,
303 .channels_max = 2,
304 .ops = {
305 .open = cs_dig_playback_pcm_open,
306 .close = cs_dig_playback_pcm_close,
307 .prepare = cs_dig_playback_pcm_prepare,
308 .cleanup = cs_dig_playback_pcm_cleanup
312 static const struct hda_pcm_stream cs_pcm_digital_capture = {
313 .substreams = 1,
314 .channels_min = 2,
315 .channels_max = 2,
318 static int cs_build_pcms(struct hda_codec *codec)
320 struct cs_spec *spec = codec->spec;
321 struct hda_pcm *info = spec->pcm_rec;
323 codec->pcm_info = info;
324 codec->num_pcms = 0;
326 info->name = "Cirrus Analog";
327 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
328 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
329 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
330 spec->multiout.max_channels;
331 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
332 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
333 spec->adc_nid[spec->cur_input];
334 codec->num_pcms++;
336 if (!spec->multiout.dig_out_nid && !spec->dig_in)
337 return 0;
339 info++;
340 info->name = "Cirrus Digital";
341 info->pcm_type = spec->autocfg.dig_out_type[0];
342 if (!info->pcm_type)
343 info->pcm_type = HDA_PCM_TYPE_SPDIF;
344 if (spec->multiout.dig_out_nid) {
345 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
346 cs_pcm_digital_playback;
347 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
348 spec->multiout.dig_out_nid;
350 if (spec->dig_in) {
351 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
352 cs_pcm_digital_capture;
353 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
355 codec->num_pcms++;
357 return 0;
361 * parse codec topology
364 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
366 hda_nid_t dac;
367 if (!pin)
368 return 0;
369 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
370 return 0;
371 return dac;
374 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
376 struct cs_spec *spec = codec->spec;
377 struct auto_pin_cfg *cfg = &spec->autocfg;
378 hda_nid_t pin = cfg->inputs[idx].pin;
379 unsigned int val;
380 if (!is_jack_detectable(codec, pin))
381 return 0;
382 val = snd_hda_codec_get_pincfg(codec, pin);
383 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
386 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
387 unsigned int *idxp)
389 int i, idx;
390 hda_nid_t nid;
392 nid = codec->start_nid;
393 for (i = 0; i < codec->num_nodes; i++, nid++) {
394 unsigned int type;
395 type = get_wcaps_type(get_wcaps(codec, nid));
396 if (type != AC_WID_AUD_IN)
397 continue;
398 idx = snd_hda_get_conn_index(codec, nid, pin, false);
399 if (idx >= 0) {
400 *idxp = idx;
401 return nid;
404 return 0;
407 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
409 unsigned int val;
410 val = snd_hda_codec_get_pincfg(codec, nid);
411 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
414 static int parse_output(struct hda_codec *codec)
416 struct cs_spec *spec = codec->spec;
417 struct auto_pin_cfg *cfg = &spec->autocfg;
418 int i, extra_nids;
419 hda_nid_t dac;
421 for (i = 0; i < cfg->line_outs; i++) {
422 dac = get_dac(codec, cfg->line_out_pins[i]);
423 if (!dac)
424 break;
425 spec->dac_nid[i] = dac;
427 spec->multiout.num_dacs = i;
428 spec->multiout.dac_nids = spec->dac_nid;
429 spec->multiout.max_channels = i * 2;
431 /* add HP and speakers */
432 extra_nids = 0;
433 for (i = 0; i < cfg->hp_outs; i++) {
434 dac = get_dac(codec, cfg->hp_pins[i]);
435 if (!dac)
436 break;
437 if (!i)
438 spec->multiout.hp_nid = dac;
439 else
440 spec->multiout.extra_out_nid[extra_nids++] = dac;
442 for (i = 0; i < cfg->speaker_outs; i++) {
443 dac = get_dac(codec, cfg->speaker_pins[i]);
444 if (!dac)
445 break;
446 spec->multiout.extra_out_nid[extra_nids++] = dac;
449 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
450 cfg->speaker_outs = cfg->line_outs;
451 memcpy(cfg->speaker_pins, cfg->line_out_pins,
452 sizeof(cfg->speaker_pins));
453 cfg->line_outs = 0;
456 return 0;
459 static int parse_input(struct hda_codec *codec)
461 struct cs_spec *spec = codec->spec;
462 struct auto_pin_cfg *cfg = &spec->autocfg;
463 int i;
465 for (i = 0; i < cfg->num_inputs; i++) {
466 hda_nid_t pin = cfg->inputs[i].pin;
467 spec->input_idx[spec->num_inputs] = i;
468 spec->capsrc_idx[i] = spec->num_inputs++;
469 spec->cur_input = i;
470 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
472 if (!spec->num_inputs)
473 return 0;
475 /* check whether the automatic mic switch is available */
476 if (spec->num_inputs == 2 &&
477 cfg->inputs[0].type == AUTO_PIN_MIC &&
478 cfg->inputs[1].type == AUTO_PIN_MIC) {
479 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
480 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
481 spec->mic_detect = 1;
482 spec->automic_idx = 0;
484 } else {
485 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
486 spec->mic_detect = 1;
487 spec->automic_idx = 1;
491 return 0;
495 static int parse_digital_output(struct hda_codec *codec)
497 struct cs_spec *spec = codec->spec;
498 struct auto_pin_cfg *cfg = &spec->autocfg;
499 hda_nid_t nid;
501 if (!cfg->dig_outs)
502 return 0;
503 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
504 return 0;
505 spec->multiout.dig_out_nid = nid;
506 spec->multiout.share_spdif = 1;
507 if (cfg->dig_outs > 1 &&
508 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
509 spec->slave_dig_outs[0] = nid;
510 codec->slave_dig_outs = spec->slave_dig_outs;
512 return 0;
515 static int parse_digital_input(struct hda_codec *codec)
517 struct cs_spec *spec = codec->spec;
518 struct auto_pin_cfg *cfg = &spec->autocfg;
519 int idx;
521 if (cfg->dig_in_pin)
522 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
523 return 0;
527 * create mixer controls
530 static const char * const dir_sfx[2] = { "Playback", "Capture" };
532 static int add_mute(struct hda_codec *codec, const char *name, int index,
533 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
535 char tmp[44];
536 struct snd_kcontrol_new knew =
537 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
538 knew.private_value = pval;
539 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
540 *kctlp = snd_ctl_new1(&knew, codec);
541 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
542 return snd_hda_ctl_add(codec, 0, *kctlp);
545 static int add_volume(struct hda_codec *codec, const char *name,
546 int index, unsigned int pval, int dir,
547 struct snd_kcontrol **kctlp)
549 char tmp[44];
550 struct snd_kcontrol_new knew =
551 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
552 knew.private_value = pval;
553 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
554 *kctlp = snd_ctl_new1(&knew, codec);
555 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
556 return snd_hda_ctl_add(codec, 0, *kctlp);
559 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
561 unsigned int caps;
563 /* set the upper-limit for mixer amp to 0dB */
564 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
565 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
566 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
567 << AC_AMPCAP_NUM_STEPS_SHIFT;
568 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
571 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
573 struct cs_spec *spec = codec->spec;
574 unsigned int tlv[4];
575 int err;
577 spec->vmaster_sw =
578 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
579 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
580 if (err < 0)
581 return err;
583 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
584 spec->vmaster_vol =
585 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
586 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
587 if (err < 0)
588 return err;
589 return 0;
592 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
593 int num_ctls, int type)
595 struct cs_spec *spec = codec->spec;
596 const char *name;
597 int err, index;
598 struct snd_kcontrol *kctl;
599 static const char * const speakers[] = {
600 "Front Speaker", "Surround Speaker", "Bass Speaker"
602 static const char * const line_outs[] = {
603 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
606 fix_volume_caps(codec, dac);
607 if (!spec->vmaster_sw) {
608 err = add_vmaster(codec, dac);
609 if (err < 0)
610 return err;
613 index = 0;
614 switch (type) {
615 case AUTO_PIN_HP_OUT:
616 name = "Headphone";
617 index = idx;
618 break;
619 case AUTO_PIN_SPEAKER_OUT:
620 if (num_ctls > 1)
621 name = speakers[idx];
622 else
623 name = "Speaker";
624 break;
625 default:
626 if (num_ctls > 1)
627 name = line_outs[idx];
628 else
629 name = "Line-Out";
630 break;
633 err = add_mute(codec, name, index,
634 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
635 if (err < 0)
636 return err;
637 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
638 if (err < 0)
639 return err;
641 err = add_volume(codec, name, index,
642 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
643 if (err < 0)
644 return err;
645 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
646 if (err < 0)
647 return err;
649 return 0;
652 static int build_output(struct hda_codec *codec)
654 struct cs_spec *spec = codec->spec;
655 struct auto_pin_cfg *cfg = &spec->autocfg;
656 int i, err;
658 for (i = 0; i < cfg->line_outs; i++) {
659 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
660 i, cfg->line_outs, cfg->line_out_type);
661 if (err < 0)
662 return err;
664 for (i = 0; i < cfg->hp_outs; i++) {
665 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
666 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
667 if (err < 0)
668 return err;
670 for (i = 0; i < cfg->speaker_outs; i++) {
671 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
672 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
673 if (err < 0)
674 return err;
676 return 0;
682 static const struct snd_kcontrol_new cs_capture_ctls[] = {
683 HDA_BIND_SW("Capture Switch", 0),
684 HDA_BIND_VOL("Capture Volume", 0),
687 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
688 int force)
690 struct cs_spec *spec = codec->spec;
692 if (spec->cur_input == idx && !force)
693 return 0;
694 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
695 /* stream is running, let's swap the current ADC */
696 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
697 spec->cur_adc = spec->adc_nid[idx];
698 snd_hda_codec_setup_stream(codec, spec->cur_adc,
699 spec->cur_adc_stream_tag, 0,
700 spec->cur_adc_format);
702 spec->cur_input = idx;
703 cs_update_input_select(codec);
704 return 1;
707 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
708 struct snd_ctl_elem_info *uinfo)
710 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
711 struct cs_spec *spec = codec->spec;
712 struct auto_pin_cfg *cfg = &spec->autocfg;
713 unsigned int idx;
715 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
716 uinfo->count = 1;
717 uinfo->value.enumerated.items = spec->num_inputs;
718 if (uinfo->value.enumerated.item >= spec->num_inputs)
719 uinfo->value.enumerated.item = spec->num_inputs - 1;
720 idx = spec->input_idx[uinfo->value.enumerated.item];
721 strcpy(uinfo->value.enumerated.name,
722 hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
723 return 0;
726 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
727 struct snd_ctl_elem_value *ucontrol)
729 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
730 struct cs_spec *spec = codec->spec;
731 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
732 return 0;
735 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
736 struct snd_ctl_elem_value *ucontrol)
738 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
739 struct cs_spec *spec = codec->spec;
740 unsigned int idx = ucontrol->value.enumerated.item[0];
742 if (idx >= spec->num_inputs)
743 return -EINVAL;
744 idx = spec->input_idx[idx];
745 return change_cur_input(codec, idx, 0);
748 static const struct snd_kcontrol_new cs_capture_source = {
749 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
750 .name = "Capture Source",
751 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
752 .info = cs_capture_source_info,
753 .get = cs_capture_source_get,
754 .put = cs_capture_source_put,
757 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
758 struct hda_ctl_ops *ops)
760 struct cs_spec *spec = codec->spec;
761 struct hda_bind_ctls *bind;
762 int i, n;
764 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
765 GFP_KERNEL);
766 if (!bind)
767 return NULL;
768 bind->ops = ops;
769 n = 0;
770 for (i = 0; i < AUTO_PIN_LAST; i++) {
771 if (!spec->adc_nid[i])
772 continue;
773 bind->values[n++] =
774 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
775 spec->adc_idx[i], HDA_INPUT);
777 return bind;
780 /* add a (input-boost) volume control to the given input pin */
781 static int add_input_volume_control(struct hda_codec *codec,
782 struct auto_pin_cfg *cfg,
783 int item)
785 hda_nid_t pin = cfg->inputs[item].pin;
786 u32 caps;
787 const char *label;
788 struct snd_kcontrol *kctl;
790 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
791 return 0;
792 caps = query_amp_caps(codec, pin, HDA_INPUT);
793 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
794 if (caps <= 1)
795 return 0;
796 label = hda_get_autocfg_input_label(codec, cfg, item);
797 return add_volume(codec, label, 0,
798 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
801 static int build_input(struct hda_codec *codec)
803 struct cs_spec *spec = codec->spec;
804 int i, err;
806 if (!spec->num_inputs)
807 return 0;
809 /* make bind-capture */
810 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
811 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
812 for (i = 0; i < 2; i++) {
813 struct snd_kcontrol *kctl;
814 int n;
815 if (!spec->capture_bind[i])
816 return -ENOMEM;
817 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
818 if (!kctl)
819 return -ENOMEM;
820 kctl->private_value = (long)spec->capture_bind[i];
821 err = snd_hda_ctl_add(codec, 0, kctl);
822 if (err < 0)
823 return err;
824 for (n = 0; n < AUTO_PIN_LAST; n++) {
825 if (!spec->adc_nid[n])
826 continue;
827 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
828 if (err < 0)
829 return err;
833 if (spec->num_inputs > 1 && !spec->mic_detect) {
834 err = snd_hda_ctl_add(codec, 0,
835 snd_ctl_new1(&cs_capture_source, codec));
836 if (err < 0)
837 return err;
840 for (i = 0; i < spec->num_inputs; i++) {
841 err = add_input_volume_control(codec, &spec->autocfg, i);
842 if (err < 0)
843 return err;
846 return 0;
852 static int build_digital_output(struct hda_codec *codec)
854 struct cs_spec *spec = codec->spec;
855 int err;
857 if (!spec->multiout.dig_out_nid)
858 return 0;
860 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
861 spec->multiout.dig_out_nid);
862 if (err < 0)
863 return err;
864 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
865 if (err < 0)
866 return err;
867 return 0;
870 static int build_digital_input(struct hda_codec *codec)
872 struct cs_spec *spec = codec->spec;
873 if (spec->dig_in)
874 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
875 return 0;
879 * auto-mute and auto-mic switching
880 * CS421x auto-output redirecting
881 * HP/SPK/SPDIF
884 static void cs_automute(struct hda_codec *codec)
886 struct cs_spec *spec = codec->spec;
887 struct auto_pin_cfg *cfg = &spec->autocfg;
888 unsigned int hp_present;
889 unsigned int spdif_present;
890 hda_nid_t nid;
891 int i;
893 spdif_present = 0;
894 if (cfg->dig_outs) {
895 nid = cfg->dig_out_pins[0];
896 if (is_jack_detectable(codec, nid)) {
898 TODO: SPDIF output redirect when SENSE_B is enabled.
899 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
900 assumed.
902 if (snd_hda_jack_detect(codec, nid)
903 /* && spec->sense_b */)
904 spdif_present = 1;
908 hp_present = 0;
909 for (i = 0; i < cfg->hp_outs; i++) {
910 nid = cfg->hp_pins[i];
911 if (!is_jack_detectable(codec, nid))
912 continue;
913 hp_present = snd_hda_jack_detect(codec, nid);
914 if (hp_present)
915 break;
918 /* mute speakers if spdif or hp jack is plugged in */
919 for (i = 0; i < cfg->speaker_outs; i++) {
920 nid = cfg->speaker_pins[i];
921 snd_hda_codec_write(codec, nid, 0,
922 AC_VERB_SET_PIN_WIDGET_CONTROL,
923 hp_present ? 0 : PIN_OUT);
924 /* detect on spdif is specific to CS421x */
925 if (spec->vendor_nid == CS421X_VENDOR_NID) {
926 snd_hda_codec_write(codec, nid, 0,
927 AC_VERB_SET_PIN_WIDGET_CONTROL,
928 spdif_present ? 0 : PIN_OUT);
931 if (spec->board_config == CS420X_MBP53 ||
932 spec->board_config == CS420X_MBP55 ||
933 spec->board_config == CS420X_IMAC27) {
934 unsigned int gpio = hp_present ? 0x02 : 0x08;
935 snd_hda_codec_write(codec, 0x01, 0,
936 AC_VERB_SET_GPIO_DATA, gpio);
939 /* specific to CS421x */
940 if (spec->vendor_nid == CS421X_VENDOR_NID) {
941 /* mute HPs if spdif jack (SENSE_B) is present */
942 for (i = 0; i < cfg->hp_outs; i++) {
943 nid = cfg->hp_pins[i];
944 snd_hda_codec_write(codec, nid, 0,
945 AC_VERB_SET_PIN_WIDGET_CONTROL,
946 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
949 /* SPDIF TX on/off */
950 if (cfg->dig_outs) {
951 nid = cfg->dig_out_pins[0];
952 snd_hda_codec_write(codec, nid, 0,
953 AC_VERB_SET_PIN_WIDGET_CONTROL,
954 spdif_present ? PIN_OUT : 0);
957 /* Update board GPIOs if neccessary ... */
962 * Auto-input redirect for CS421x
963 * Switch max 3 inputs of a single ADC (nid 3)
966 static void cs_automic(struct hda_codec *codec)
968 struct cs_spec *spec = codec->spec;
969 struct auto_pin_cfg *cfg = &spec->autocfg;
970 hda_nid_t nid;
971 unsigned int present;
973 nid = cfg->inputs[spec->automic_idx].pin;
974 present = snd_hda_jack_detect(codec, nid);
976 /* specific to CS421x, single ADC */
977 if (spec->vendor_nid == CS421X_VENDOR_NID) {
978 if (present) {
979 spec->last_input = spec->cur_input;
980 spec->cur_input = spec->automic_idx;
981 } else {
982 spec->cur_input = spec->last_input;
984 cs_update_input_select(codec);
985 } else {
986 if (present)
987 change_cur_input(codec, spec->automic_idx, 0);
988 else
989 change_cur_input(codec, !spec->automic_idx, 0);
996 static void init_output(struct hda_codec *codec)
998 struct cs_spec *spec = codec->spec;
999 struct auto_pin_cfg *cfg = &spec->autocfg;
1000 int i;
1002 /* mute first */
1003 for (i = 0; i < spec->multiout.num_dacs; i++)
1004 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1005 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1006 if (spec->multiout.hp_nid)
1007 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1008 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1009 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1010 if (!spec->multiout.extra_out_nid[i])
1011 break;
1012 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1013 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1016 /* set appropriate pin controls */
1017 for (i = 0; i < cfg->line_outs; i++)
1018 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1019 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1020 /* HP */
1021 for (i = 0; i < cfg->hp_outs; i++) {
1022 hda_nid_t nid = cfg->hp_pins[i];
1023 snd_hda_codec_write(codec, nid, 0,
1024 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1025 if (!cfg->speaker_outs)
1026 continue;
1027 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1028 snd_hda_codec_write(codec, nid, 0,
1029 AC_VERB_SET_UNSOLICITED_ENABLE,
1030 AC_USRSP_EN | HP_EVENT);
1031 spec->hp_detect = 1;
1035 /* Speaker */
1036 for (i = 0; i < cfg->speaker_outs; i++)
1037 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1038 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1040 /* SPDIF is enabled on presence detect for CS421x */
1041 if (spec->hp_detect || spec->spdif_detect)
1042 cs_automute(codec);
1045 static void init_input(struct hda_codec *codec)
1047 struct cs_spec *spec = codec->spec;
1048 struct auto_pin_cfg *cfg = &spec->autocfg;
1049 unsigned int coef;
1050 int i;
1052 for (i = 0; i < cfg->num_inputs; i++) {
1053 unsigned int ctl;
1054 hda_nid_t pin = cfg->inputs[i].pin;
1055 if (!spec->adc_nid[i])
1056 continue;
1057 /* set appropriate pin control and mute first */
1058 ctl = PIN_IN;
1059 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1060 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1061 caps >>= AC_PINCAP_VREF_SHIFT;
1062 if (caps & AC_PINCAP_VREF_80)
1063 ctl = PIN_VREF80;
1065 snd_hda_codec_write(codec, pin, 0,
1066 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1067 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1068 AC_VERB_SET_AMP_GAIN_MUTE,
1069 AMP_IN_MUTE(spec->adc_idx[i]));
1070 if (spec->mic_detect && spec->automic_idx == i)
1071 snd_hda_codec_write(codec, pin, 0,
1072 AC_VERB_SET_UNSOLICITED_ENABLE,
1073 AC_USRSP_EN | MIC_EVENT);
1075 /* specific to CS421x */
1076 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1077 if (spec->mic_detect)
1078 cs_automic(codec);
1079 else {
1080 spec->cur_adc = spec->adc_nid[spec->cur_input];
1081 cs_update_input_select(codec);
1083 } else {
1084 change_cur_input(codec, spec->cur_input, 1);
1085 if (spec->mic_detect)
1086 cs_automic(codec);
1088 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1089 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1090 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1091 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1092 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1093 * No effect if SPDIF_OUT2 is
1094 * selected in IDX_SPDIF_CTL.
1096 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1100 static const struct hda_verb cs_coef_init_verbs[] = {
1101 {0x11, AC_VERB_SET_PROC_STATE, 1},
1102 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1103 {0x11, AC_VERB_SET_PROC_COEF,
1104 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1105 | 0x0040 /* Mute DACs on FIFO error */
1106 | 0x1000 /* Enable DACs High Pass Filter */
1107 | 0x0400 /* Disable Coefficient Auto increment */
1109 /* Beep */
1110 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1111 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1113 {} /* terminator */
1116 /* Errata: CS4207 rev C0/C1/C2 Silicon
1118 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1120 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1121 * may be excessive (up to an additional 200 μA), which is most easily
1122 * observed while the part is being held in reset (RESET# active low).
1124 * Root Cause: At initial powerup of the device, the logic that drives
1125 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1126 * initialized.
1127 * Certain random patterns will cause a steady leakage current in those
1128 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1130 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1131 * blocks, which will alleviate the issue.
1134 static const struct hda_verb cs_errata_init_verbs[] = {
1135 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1136 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1138 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1139 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1140 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1141 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1142 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1143 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1145 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1146 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1148 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1149 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1150 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1151 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1152 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1153 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1154 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1156 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1157 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1158 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1159 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1160 #endif
1162 {} /* terminator */
1165 /* SPDIF setup */
1166 static void init_digital(struct hda_codec *codec)
1168 unsigned int coef;
1170 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1171 coef |= 0x0008; /* Replace with mute on error */
1172 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1173 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1174 * SPDIF_OUT2 is shared with GPIO1 and
1175 * DMIC_SDA2.
1177 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1180 static int cs_init(struct hda_codec *codec)
1182 struct cs_spec *spec = codec->spec;
1184 /* init_verb sequence for C0/C1/C2 errata*/
1185 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1187 snd_hda_sequence_write(codec, cs_coef_init_verbs);
1189 if (spec->gpio_mask) {
1190 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1191 spec->gpio_mask);
1192 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1193 spec->gpio_dir);
1194 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1195 spec->gpio_data);
1198 init_output(codec);
1199 init_input(codec);
1200 init_digital(codec);
1201 return 0;
1204 static int cs_build_controls(struct hda_codec *codec)
1206 int err;
1208 err = build_output(codec);
1209 if (err < 0)
1210 return err;
1211 err = build_input(codec);
1212 if (err < 0)
1213 return err;
1214 err = build_digital_output(codec);
1215 if (err < 0)
1216 return err;
1217 err = build_digital_input(codec);
1218 if (err < 0)
1219 return err;
1220 return cs_init(codec);
1223 static void cs_free(struct hda_codec *codec)
1225 struct cs_spec *spec = codec->spec;
1226 kfree(spec->capture_bind[0]);
1227 kfree(spec->capture_bind[1]);
1228 kfree(codec->spec);
1231 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1233 switch ((res >> 26) & 0x7f) {
1234 case HP_EVENT:
1235 cs_automute(codec);
1236 break;
1237 case MIC_EVENT:
1238 cs_automic(codec);
1239 break;
1243 static const struct hda_codec_ops cs_patch_ops = {
1244 .build_controls = cs_build_controls,
1245 .build_pcms = cs_build_pcms,
1246 .init = cs_init,
1247 .free = cs_free,
1248 .unsol_event = cs_unsol_event,
1251 static int cs_parse_auto_config(struct hda_codec *codec)
1253 struct cs_spec *spec = codec->spec;
1254 int err;
1256 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1257 if (err < 0)
1258 return err;
1260 err = parse_output(codec);
1261 if (err < 0)
1262 return err;
1263 err = parse_input(codec);
1264 if (err < 0)
1265 return err;
1266 err = parse_digital_output(codec);
1267 if (err < 0)
1268 return err;
1269 err = parse_digital_input(codec);
1270 if (err < 0)
1271 return err;
1272 return 0;
1275 static const char * const cs420x_models[CS420X_MODELS] = {
1276 [CS420X_MBP53] = "mbp53",
1277 [CS420X_MBP55] = "mbp55",
1278 [CS420X_IMAC27] = "imac27",
1279 [CS420X_AUTO] = "auto",
1283 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1284 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1285 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1286 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1287 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1288 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
1289 {} /* terminator */
1292 struct cs_pincfg {
1293 hda_nid_t nid;
1294 u32 val;
1297 static const struct cs_pincfg mbp53_pincfgs[] = {
1298 { 0x09, 0x012b4050 },
1299 { 0x0a, 0x90100141 },
1300 { 0x0b, 0x90100140 },
1301 { 0x0c, 0x018b3020 },
1302 { 0x0d, 0x90a00110 },
1303 { 0x0e, 0x400000f0 },
1304 { 0x0f, 0x01cbe030 },
1305 { 0x10, 0x014be060 },
1306 { 0x12, 0x400000f0 },
1307 { 0x15, 0x400000f0 },
1308 {} /* terminator */
1311 static const struct cs_pincfg mbp55_pincfgs[] = {
1312 { 0x09, 0x012b4030 },
1313 { 0x0a, 0x90100121 },
1314 { 0x0b, 0x90100120 },
1315 { 0x0c, 0x400000f0 },
1316 { 0x0d, 0x90a00110 },
1317 { 0x0e, 0x400000f0 },
1318 { 0x0f, 0x400000f0 },
1319 { 0x10, 0x014be040 },
1320 { 0x12, 0x400000f0 },
1321 { 0x15, 0x400000f0 },
1322 {} /* terminator */
1325 static const struct cs_pincfg imac27_pincfgs[] = {
1326 { 0x09, 0x012b4050 },
1327 { 0x0a, 0x90100140 },
1328 { 0x0b, 0x90100142 },
1329 { 0x0c, 0x018b3020 },
1330 { 0x0d, 0x90a00110 },
1331 { 0x0e, 0x400000f0 },
1332 { 0x0f, 0x01cbe030 },
1333 { 0x10, 0x014be060 },
1334 { 0x12, 0x01ab9070 },
1335 { 0x15, 0x400000f0 },
1336 {} /* terminator */
1339 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1340 [CS420X_MBP53] = mbp53_pincfgs,
1341 [CS420X_MBP55] = mbp55_pincfgs,
1342 [CS420X_IMAC27] = imac27_pincfgs,
1345 static void fix_pincfg(struct hda_codec *codec, int model,
1346 const struct cs_pincfg **pin_configs)
1348 const struct cs_pincfg *cfg = pin_configs[model];
1349 if (!cfg)
1350 return;
1351 for (; cfg->nid; cfg++)
1352 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1355 static int patch_cs420x(struct hda_codec *codec)
1357 struct cs_spec *spec;
1358 int err;
1360 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1361 if (!spec)
1362 return -ENOMEM;
1363 codec->spec = spec;
1365 spec->vendor_nid = CS420X_VENDOR_NID;
1367 spec->board_config =
1368 snd_hda_check_board_config(codec, CS420X_MODELS,
1369 cs420x_models, cs420x_cfg_tbl);
1370 if (spec->board_config >= 0)
1371 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1373 switch (spec->board_config) {
1374 case CS420X_IMAC27:
1375 case CS420X_MBP53:
1376 case CS420X_MBP55:
1377 /* GPIO1 = headphones */
1378 /* GPIO3 = speakers */
1379 spec->gpio_mask = 0x0a;
1380 spec->gpio_dir = 0x0a;
1381 break;
1384 err = cs_parse_auto_config(codec);
1385 if (err < 0)
1386 goto error;
1388 codec->patch_ops = cs_patch_ops;
1390 return 0;
1392 error:
1393 kfree(codec->spec);
1394 codec->spec = NULL;
1395 return err;
1399 * Cirrus Logic CS4210
1401 * 1 DAC => HP(sense) / Speakers,
1402 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1403 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1406 /* CS4210 board names */
1407 static const char *cs421x_models[CS421X_MODELS] = {
1408 [CS421X_CDB4210] = "cdb4210",
1411 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1412 /* Test Intel board + CDB2410 */
1413 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1414 {} /* terminator */
1417 /* CS4210 board pinconfigs */
1418 /* Default CS4210 (CDB4210)*/
1419 static const struct cs_pincfg cdb4210_pincfgs[] = {
1420 { 0x05, 0x0321401f },
1421 { 0x06, 0x90170010 },
1422 { 0x07, 0x03813031 },
1423 { 0x08, 0xb7a70037 },
1424 { 0x09, 0xb7a6003e },
1425 { 0x0a, 0x034510f0 },
1426 {} /* terminator */
1429 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1430 [CS421X_CDB4210] = cdb4210_pincfgs,
1433 static const struct hda_verb cs421x_coef_init_verbs[] = {
1434 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1435 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1437 Disable Coefficient Index Auto-Increment(DAI)=1,
1438 PDREF=0
1440 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1442 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1443 /* ADC SZCMode = Digital Soft Ramp */
1444 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1446 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1447 {0x0B, AC_VERB_SET_PROC_COEF,
1448 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1449 | 0x0004 /* Mute DAC on FIFO error */
1450 | 0x0008 /* Enable DAC High Pass Filter */
1452 {} /* terminator */
1455 /* Errata: CS4210 rev A1 Silicon
1457 * http://www.cirrus.com/en/pubs/errata/
1459 * Description:
1460 * 1. Performance degredation is present in the ADC.
1461 * 2. Speaker output is not completely muted upon HP detect.
1462 * 3. Noise is present when clipping occurs on the amplified
1463 * speaker outputs.
1465 * Workaround:
1466 * The following verb sequence written to the registers during
1467 * initialization will correct the issues listed above.
1470 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1471 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1473 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1474 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1476 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1477 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1479 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1480 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1482 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1483 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1485 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1486 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1488 {} /* terminator */
1491 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1492 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1494 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1495 struct snd_ctl_elem_info *uinfo)
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1498 uinfo->count = 1;
1499 uinfo->value.integer.min = 0;
1500 uinfo->value.integer.max = 3;
1501 return 0;
1504 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1505 struct snd_ctl_elem_value *ucontrol)
1507 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1509 ucontrol->value.integer.value[0] =
1510 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1511 return 0;
1514 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1515 struct snd_ctl_elem_value *ucontrol)
1517 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1519 unsigned int vol = ucontrol->value.integer.value[0];
1520 unsigned int coef =
1521 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1522 unsigned int original_coef = coef;
1524 coef &= ~0x0003;
1525 coef |= (vol & 0x0003);
1526 if (original_coef == coef)
1527 return 0;
1528 else {
1529 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1530 return 1;
1534 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1536 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1537 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1538 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1539 .name = "Speaker Boost Playback Volume",
1540 .info = cs421x_boost_vol_info,
1541 .get = cs421x_boost_vol_get,
1542 .put = cs421x_boost_vol_put,
1543 .tlv = { .p = cs421x_speaker_boost_db_scale },
1546 static void cs421x_pinmux_init(struct hda_codec *codec)
1548 struct cs_spec *spec = codec->spec;
1549 unsigned int def_conf, coef;
1551 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1552 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1554 if (spec->gpio_mask)
1555 coef |= 0x0008; /* B1,B2 are GPIOs */
1556 else
1557 coef &= ~0x0008;
1559 if (spec->sense_b)
1560 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1561 else
1562 coef &= ~0x0010;
1564 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1566 if ((spec->gpio_mask || spec->sense_b) &&
1567 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1570 GPIO or SENSE_B forced - disconnect the DMIC pin.
1572 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1573 def_conf &= ~AC_DEFCFG_PORT_CONN;
1574 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1575 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1579 static void init_cs421x_digital(struct hda_codec *codec)
1581 struct cs_spec *spec = codec->spec;
1582 struct auto_pin_cfg *cfg = &spec->autocfg;
1583 int i;
1586 for (i = 0; i < cfg->dig_outs; i++) {
1587 hda_nid_t nid = cfg->dig_out_pins[i];
1588 if (!cfg->speaker_outs)
1589 continue;
1590 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1592 snd_hda_codec_write(codec, nid, 0,
1593 AC_VERB_SET_UNSOLICITED_ENABLE,
1594 AC_USRSP_EN | SPDIF_EVENT);
1595 spec->spdif_detect = 1;
1600 static int cs421x_init(struct hda_codec *codec)
1602 struct cs_spec *spec = codec->spec;
1604 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1605 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1607 cs421x_pinmux_init(codec);
1609 if (spec->gpio_mask) {
1610 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1611 spec->gpio_mask);
1612 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1613 spec->gpio_dir);
1614 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1615 spec->gpio_data);
1618 init_output(codec);
1619 init_input(codec);
1620 init_cs421x_digital(codec);
1622 return 0;
1626 * CS4210 Input MUX (1 ADC)
1628 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1629 struct snd_ctl_elem_info *uinfo)
1631 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1632 struct cs_spec *spec = codec->spec;
1634 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1637 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1638 struct snd_ctl_elem_value *ucontrol)
1640 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1641 struct cs_spec *spec = codec->spec;
1643 ucontrol->value.enumerated.item[0] = spec->cur_input;
1644 return 0;
1647 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1648 struct snd_ctl_elem_value *ucontrol)
1650 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1651 struct cs_spec *spec = codec->spec;
1653 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1654 spec->adc_nid[0], &spec->cur_input);
1658 static struct snd_kcontrol_new cs421x_capture_source = {
1660 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1661 .name = "Capture Source",
1662 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1663 .info = cs421x_mux_enum_info,
1664 .get = cs421x_mux_enum_get,
1665 .put = cs421x_mux_enum_put,
1668 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1670 struct cs_spec *spec = codec->spec;
1671 struct auto_pin_cfg *cfg = &spec->autocfg;
1672 const struct hda_input_mux *imux = &spec->input_mux;
1673 hda_nid_t pin = cfg->inputs[item].pin;
1674 struct snd_kcontrol *kctl;
1675 u32 caps;
1677 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1678 return 0;
1680 caps = query_amp_caps(codec, pin, HDA_INPUT);
1681 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1682 if (caps <= 1)
1683 return 0;
1685 return add_volume(codec, imux->items[item].label, 0,
1686 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1689 /* add a (input-boost) volume control to the given input pin */
1690 static int build_cs421x_input(struct hda_codec *codec)
1692 struct cs_spec *spec = codec->spec;
1693 struct auto_pin_cfg *cfg = &spec->autocfg;
1694 struct hda_input_mux *imux = &spec->input_mux;
1695 int i, err, type_idx;
1696 const char *label;
1698 if (!spec->num_inputs)
1699 return 0;
1701 /* make bind-capture */
1702 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1703 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1704 for (i = 0; i < 2; i++) {
1705 struct snd_kcontrol *kctl;
1706 int n;
1707 if (!spec->capture_bind[i])
1708 return -ENOMEM;
1709 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1710 if (!kctl)
1711 return -ENOMEM;
1712 kctl->private_value = (long)spec->capture_bind[i];
1713 err = snd_hda_ctl_add(codec, 0, kctl);
1714 if (err < 0)
1715 return err;
1716 for (n = 0; n < AUTO_PIN_LAST; n++) {
1717 if (!spec->adc_nid[n])
1718 continue;
1719 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1720 if (err < 0)
1721 return err;
1725 /* Add Input MUX Items + Capture Volume/Switch */
1726 for (i = 0; i < spec->num_inputs; i++) {
1727 label = hda_get_autocfg_input_label(codec, cfg, i);
1728 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1730 err = cs421x_add_input_volume_control(codec, i);
1731 if (err < 0)
1732 return err;
1736 Add 'Capture Source' Switch if
1737 * 2 inputs and no mic detec
1738 * 3 inputs
1740 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1741 (spec->num_inputs == 3)) {
1743 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1744 snd_ctl_new1(&cs421x_capture_source, codec));
1745 if (err < 0)
1746 return err;
1749 return 0;
1752 /* Single DAC (Mute/Gain) */
1753 static int build_cs421x_output(struct hda_codec *codec)
1755 hda_nid_t dac = CS4210_DAC_NID;
1756 struct cs_spec *spec = codec->spec;
1757 struct auto_pin_cfg *cfg = &spec->autocfg;
1758 struct snd_kcontrol *kctl;
1759 int err;
1760 char *name = "HP/Speakers";
1762 fix_volume_caps(codec, dac);
1763 if (!spec->vmaster_sw) {
1764 err = add_vmaster(codec, dac);
1765 if (err < 0)
1766 return err;
1769 err = add_mute(codec, name, 0,
1770 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1771 if (err < 0)
1772 return err;
1773 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1774 if (err < 0)
1775 return err;
1777 err = add_volume(codec, name, 0,
1778 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1779 if (err < 0)
1780 return err;
1781 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1782 if (err < 0)
1783 return err;
1785 if (cfg->speaker_outs) {
1786 err = snd_hda_ctl_add(codec, 0,
1787 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1788 if (err < 0)
1789 return err;
1791 return err;
1794 static int cs421x_build_controls(struct hda_codec *codec)
1796 int err;
1798 err = build_cs421x_output(codec);
1799 if (err < 0)
1800 return err;
1801 err = build_cs421x_input(codec);
1802 if (err < 0)
1803 return err;
1804 err = build_digital_output(codec);
1805 if (err < 0)
1806 return err;
1807 return cs421x_init(codec);
1810 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1812 switch ((res >> 26) & 0x3f) {
1813 case HP_EVENT:
1814 case SPDIF_EVENT:
1815 cs_automute(codec);
1816 break;
1818 case MIC_EVENT:
1819 cs_automic(codec);
1820 break;
1824 static int parse_cs421x_input(struct hda_codec *codec)
1826 struct cs_spec *spec = codec->spec;
1827 struct auto_pin_cfg *cfg = &spec->autocfg;
1828 int i;
1830 for (i = 0; i < cfg->num_inputs; i++) {
1831 hda_nid_t pin = cfg->inputs[i].pin;
1832 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1833 spec->cur_input = spec->last_input = i;
1834 spec->num_inputs++;
1836 /* check whether the automatic mic switch is available */
1837 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1838 spec->mic_detect = 1;
1839 spec->automic_idx = i;
1842 return 0;
1845 static int cs421x_parse_auto_config(struct hda_codec *codec)
1847 struct cs_spec *spec = codec->spec;
1848 int err;
1850 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1851 if (err < 0)
1852 return err;
1853 err = parse_output(codec);
1854 if (err < 0)
1855 return err;
1856 err = parse_cs421x_input(codec);
1857 if (err < 0)
1858 return err;
1859 err = parse_digital_output(codec);
1860 if (err < 0)
1861 return err;
1862 return 0;
1865 #ifdef CONFIG_PM
1867 Manage PDREF, when transitioning to D3hot
1868 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1870 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1872 unsigned int coef;
1874 snd_hda_shutup_pins(codec);
1876 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1877 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1878 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1879 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1881 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1882 coef |= 0x0004; /* PDREF */
1883 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1885 return 0;
1887 #endif
1889 static struct hda_codec_ops cs4210_patch_ops = {
1890 .build_controls = cs421x_build_controls,
1891 .build_pcms = cs_build_pcms,
1892 .init = cs421x_init,
1893 .free = cs_free,
1894 .unsol_event = cs421x_unsol_event,
1895 #ifdef CONFIG_PM
1896 .suspend = cs421x_suspend,
1897 #endif
1900 static int patch_cs421x(struct hda_codec *codec)
1902 struct cs_spec *spec;
1903 int err;
1905 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1906 if (!spec)
1907 return -ENOMEM;
1908 codec->spec = spec;
1910 spec->vendor_nid = CS421X_VENDOR_NID;
1912 spec->board_config =
1913 snd_hda_check_board_config(codec, CS421X_MODELS,
1914 cs421x_models, cs421x_cfg_tbl);
1915 if (spec->board_config >= 0)
1916 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1918 Setup GPIO/SENSE for each board (if used)
1920 switch (spec->board_config) {
1921 case CS421X_CDB4210:
1922 snd_printd("CS4210 board: %s\n",
1923 cs421x_models[spec->board_config]);
1924 /* spec->gpio_mask = 3;
1925 spec->gpio_dir = 3;
1926 spec->gpio_data = 3;
1928 spec->sense_b = 1;
1930 break;
1934 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1935 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1936 is disabled.
1938 cs421x_pinmux_init(codec);
1940 err = cs421x_parse_auto_config(codec);
1941 if (err < 0)
1942 goto error;
1944 codec->patch_ops = cs4210_patch_ops;
1946 return 0;
1948 error:
1949 kfree(codec->spec);
1950 codec->spec = NULL;
1951 return err;
1956 * patch entries
1958 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
1959 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1960 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1961 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
1962 {} /* terminator */
1965 MODULE_ALIAS("snd-hda-codec-id:10134206");
1966 MODULE_ALIAS("snd-hda-codec-id:10134207");
1967 MODULE_ALIAS("snd-hda-codec-id:10134210");
1969 MODULE_LICENSE("GPL");
1970 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1972 static struct hda_codec_preset_list cirrus_list = {
1973 .preset = snd_hda_preset_cirrus,
1974 .owner = THIS_MODULE,
1977 static int __init patch_cirrus_init(void)
1979 return snd_hda_add_codec_preset(&cirrus_list);
1982 static void __exit patch_cirrus_exit(void)
1984 snd_hda_delete_codec_preset(&cirrus_list);
1987 module_init(patch_cirrus_init)
1988 module_exit(patch_cirrus_exit)