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"
30 #include <sound/tlv.h>
37 struct auto_pin_cfg autocfg;
38 struct hda_multi_out multiout;
39 struct snd_kcontrol *vmaster_sw;
40 struct snd_kcontrol *vmaster_vol;
42 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
43 hda_nid_t slave_dig_outs[2];
45 unsigned int input_idx[AUTO_PIN_LAST];
46 unsigned int capsrc_idx[AUTO_PIN_LAST];
47 hda_nid_t adc_nid[AUTO_PIN_LAST];
48 unsigned int adc_idx[AUTO_PIN_LAST];
49 unsigned int num_inputs;
50 unsigned int cur_input;
51 unsigned int automic_idx;
53 unsigned int cur_adc_stream_tag;
54 unsigned int cur_adc_format;
57 const struct hda_bind_ctls *capture_bind[2];
59 unsigned int gpio_mask;
60 unsigned int gpio_dir;
61 unsigned int gpio_data;
62 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
63 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
65 struct hda_pcm pcm_rec[2]; /* PCM information */
67 unsigned int hp_detect:1;
68 unsigned int mic_detect:1;
70 unsigned int spdif_detect:1;
71 unsigned int sense_b:1;
73 struct hda_input_mux input_mux;
74 unsigned int last_input;
77 /* available models with CS420x */
95 /* Vendor-specific processing widget */
96 #define CS420X_VENDOR_NID 0x11
97 #define CS_DIG_OUT1_PIN_NID 0x10
98 #define CS_DIG_OUT2_PIN_NID 0x15
99 #define CS_DMIC1_PIN_NID 0x12
100 #define CS_DMIC2_PIN_NID 0x0e
103 #define IDX_SPDIF_STAT 0x0000
104 #define IDX_SPDIF_CTL 0x0001
105 #define IDX_ADC_CFG 0x0002
106 /* SZC bitmask, 4 modes below:
108 * 1 = digital immediate, analog zero-cross
109 * 2 = digtail & analog soft-ramp
110 * 3 = digital soft-ramp, analog zero-cross
112 #define CS_COEF_ADC_SZC_MASK (3 << 0)
113 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
114 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
115 /* PGA mode: 0 = differential, 1 = signle-ended */
116 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
117 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
118 #define IDX_DAC_CFG 0x0003
119 /* SZC bitmask, 4 modes below:
123 * 3 = soft-ramp on zero-cross
125 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
126 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
127 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
129 #define IDX_BEEP_CFG 0x0004
130 /* 0x0008 - test reg key */
131 /* 0x0009 - 0x0014 -> 12 test regs */
132 /* 0x0015 - visibility reg */
135 * Cirrus Logic CS4210
137 * 1 DAC => HP(sense) / Speakers,
138 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
139 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
141 #define CS4210_DAC_NID 0x02
142 #define CS4210_ADC_NID 0x03
143 #define CS4210_VENDOR_NID 0x0B
144 #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
145 #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
147 #define CS421X_IDX_DEV_CFG 0x01
148 #define CS421X_IDX_ADC_CFG 0x02
149 #define CS421X_IDX_DAC_CFG 0x03
150 #define CS421X_IDX_SPK_CTL 0x04
152 #define SPDIF_EVENT 0x04
154 /* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
155 #define CS4213_VENDOR_NID 0x09
158 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
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 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
164 AC_VERB_GET_PROC_COEF, 0);
167 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
170 struct cs_spec *spec = codec->spec;
171 snd_hda_codec_write(codec, spec->vendor_nid, 0,
172 AC_VERB_SET_COEF_INDEX, idx);
173 snd_hda_codec_write(codec, spec->vendor_nid, 0,
174 AC_VERB_SET_PROC_COEF, coef);
184 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
185 struct hda_codec *codec,
186 struct snd_pcm_substream *substream)
188 struct cs_spec *spec = codec->spec;
189 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
193 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
194 struct hda_codec *codec,
195 unsigned int stream_tag,
197 struct snd_pcm_substream *substream)
199 struct cs_spec *spec = codec->spec;
200 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
201 stream_tag, format, substream);
204 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
205 struct hda_codec *codec,
206 struct snd_pcm_substream *substream)
208 struct cs_spec *spec = codec->spec;
209 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
215 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
216 struct hda_codec *codec,
217 struct snd_pcm_substream *substream)
219 struct cs_spec *spec = codec->spec;
220 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
223 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
224 struct hda_codec *codec,
225 struct snd_pcm_substream *substream)
227 struct cs_spec *spec = codec->spec;
228 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
231 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
232 struct hda_codec *codec,
233 unsigned int stream_tag,
235 struct snd_pcm_substream *substream)
237 struct cs_spec *spec = codec->spec;
238 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
242 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
243 struct hda_codec *codec,
244 struct snd_pcm_substream *substream)
246 struct cs_spec *spec = codec->spec;
247 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
250 static void cs_update_input_select(struct hda_codec *codec)
252 struct cs_spec *spec = codec->spec;
254 snd_hda_codec_write(codec, spec->cur_adc, 0,
255 AC_VERB_SET_CONNECT_SEL,
256 spec->adc_idx[spec->cur_input]);
262 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
263 struct hda_codec *codec,
264 unsigned int stream_tag,
266 struct snd_pcm_substream *substream)
268 struct cs_spec *spec = codec->spec;
269 spec->cur_adc = spec->adc_nid[spec->cur_input];
270 spec->cur_adc_stream_tag = stream_tag;
271 spec->cur_adc_format = format;
272 cs_update_input_select(codec);
273 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
277 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
278 struct hda_codec *codec,
279 struct snd_pcm_substream *substream)
281 struct cs_spec *spec = codec->spec;
282 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
289 static const struct hda_pcm_stream cs_pcm_analog_playback = {
294 .open = cs_playback_pcm_open,
295 .prepare = cs_playback_pcm_prepare,
296 .cleanup = cs_playback_pcm_cleanup
300 static const struct hda_pcm_stream cs_pcm_analog_capture = {
305 .prepare = cs_capture_pcm_prepare,
306 .cleanup = cs_capture_pcm_cleanup
310 static const struct hda_pcm_stream cs_pcm_digital_playback = {
315 .open = cs_dig_playback_pcm_open,
316 .close = cs_dig_playback_pcm_close,
317 .prepare = cs_dig_playback_pcm_prepare,
318 .cleanup = cs_dig_playback_pcm_cleanup
322 static const struct hda_pcm_stream cs_pcm_digital_capture = {
328 static int cs_build_pcms(struct hda_codec *codec)
330 struct cs_spec *spec = codec->spec;
331 struct hda_pcm *info = spec->pcm_rec;
333 codec->pcm_info = info;
336 info->name = "Cirrus Analog";
337 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
338 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
339 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
340 spec->multiout.max_channels;
341 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
342 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
343 spec->adc_nid[spec->cur_input];
346 if (!spec->multiout.dig_out_nid && !spec->dig_in)
350 info->name = "Cirrus Digital";
351 info->pcm_type = spec->autocfg.dig_out_type[0];
353 info->pcm_type = HDA_PCM_TYPE_SPDIF;
354 if (spec->multiout.dig_out_nid) {
355 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
356 cs_pcm_digital_playback;
357 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
358 spec->multiout.dig_out_nid;
361 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
362 cs_pcm_digital_capture;
363 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
371 * parse codec topology
374 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
379 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
384 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
386 struct cs_spec *spec = codec->spec;
387 struct auto_pin_cfg *cfg = &spec->autocfg;
388 hda_nid_t pin = cfg->inputs[idx].pin;
390 if (!is_jack_detectable(codec, pin))
392 val = snd_hda_codec_get_pincfg(codec, pin);
393 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
396 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
402 nid = codec->start_nid;
403 for (i = 0; i < codec->num_nodes; i++, nid++) {
405 type = get_wcaps_type(get_wcaps(codec, nid));
406 if (type != AC_WID_AUD_IN)
408 idx = snd_hda_get_conn_index(codec, nid, pin, false);
417 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
420 val = snd_hda_codec_get_pincfg(codec, nid);
421 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
424 static int parse_output(struct hda_codec *codec)
426 struct cs_spec *spec = codec->spec;
427 struct auto_pin_cfg *cfg = &spec->autocfg;
431 for (i = 0; i < cfg->line_outs; i++) {
432 dac = get_dac(codec, cfg->line_out_pins[i]);
435 spec->dac_nid[i] = dac;
437 spec->multiout.num_dacs = i;
438 spec->multiout.dac_nids = spec->dac_nid;
439 spec->multiout.max_channels = i * 2;
441 /* add HP and speakers */
443 for (i = 0; i < cfg->hp_outs; i++) {
444 dac = get_dac(codec, cfg->hp_pins[i]);
448 spec->multiout.hp_nid = dac;
450 spec->multiout.extra_out_nid[extra_nids++] = dac;
452 for (i = 0; i < cfg->speaker_outs; i++) {
453 dac = get_dac(codec, cfg->speaker_pins[i]);
456 spec->multiout.extra_out_nid[extra_nids++] = dac;
459 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
460 cfg->speaker_outs = cfg->line_outs;
461 memcpy(cfg->speaker_pins, cfg->line_out_pins,
462 sizeof(cfg->speaker_pins));
469 static int parse_input(struct hda_codec *codec)
471 struct cs_spec *spec = codec->spec;
472 struct auto_pin_cfg *cfg = &spec->autocfg;
475 for (i = 0; i < cfg->num_inputs; i++) {
476 hda_nid_t pin = cfg->inputs[i].pin;
477 spec->input_idx[spec->num_inputs] = i;
478 spec->capsrc_idx[i] = spec->num_inputs++;
480 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
482 if (!spec->num_inputs)
485 /* check whether the automatic mic switch is available */
486 if (spec->num_inputs == 2 &&
487 cfg->inputs[0].type == AUTO_PIN_MIC &&
488 cfg->inputs[1].type == AUTO_PIN_MIC) {
489 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
490 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
491 spec->mic_detect = 1;
492 spec->automic_idx = 0;
495 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
496 spec->mic_detect = 1;
497 spec->automic_idx = 1;
505 static int parse_digital_output(struct hda_codec *codec)
507 struct cs_spec *spec = codec->spec;
508 struct auto_pin_cfg *cfg = &spec->autocfg;
513 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
515 spec->multiout.dig_out_nid = nid;
516 spec->multiout.share_spdif = 1;
517 if (cfg->dig_outs > 1 &&
518 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
519 spec->slave_dig_outs[0] = nid;
520 codec->slave_dig_outs = spec->slave_dig_outs;
525 static int parse_digital_input(struct hda_codec *codec)
527 struct cs_spec *spec = codec->spec;
528 struct auto_pin_cfg *cfg = &spec->autocfg;
532 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
537 * create mixer controls
540 static const char * const dir_sfx[2] = { "Playback", "Capture" };
542 static int add_mute(struct hda_codec *codec, const char *name, int index,
543 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
546 struct snd_kcontrol_new knew =
547 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
548 knew.private_value = pval;
549 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
550 *kctlp = snd_ctl_new1(&knew, codec);
551 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
552 return snd_hda_ctl_add(codec, 0, *kctlp);
555 static int add_volume(struct hda_codec *codec, const char *name,
556 int index, unsigned int pval, int dir,
557 struct snd_kcontrol **kctlp)
560 struct snd_kcontrol_new knew =
561 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
562 knew.private_value = pval;
563 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
564 *kctlp = snd_ctl_new1(&knew, codec);
565 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
566 return snd_hda_ctl_add(codec, 0, *kctlp);
569 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
573 /* set the upper-limit for mixer amp to 0dB */
574 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
575 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
576 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
577 << AC_AMPCAP_NUM_STEPS_SHIFT;
578 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
581 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
583 struct cs_spec *spec = codec->spec;
588 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
589 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
593 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
595 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
596 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
602 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
603 int num_ctls, int type)
605 struct cs_spec *spec = codec->spec;
608 struct snd_kcontrol *kctl;
609 static const char * const speakers[] = {
610 "Front Speaker", "Surround Speaker", "Bass Speaker"
612 static const char * const line_outs[] = {
613 "Front Line Out", "Surround Line Out", "Bass Line Out"
616 fix_volume_caps(codec, dac);
617 if (!spec->vmaster_sw) {
618 err = add_vmaster(codec, dac);
625 case AUTO_PIN_HP_OUT:
629 case AUTO_PIN_SPEAKER_OUT:
631 name = speakers[idx];
637 name = line_outs[idx];
643 err = add_mute(codec, name, index,
644 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
647 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
651 err = add_volume(codec, name, index,
652 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
655 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
662 static int build_output(struct hda_codec *codec)
664 struct cs_spec *spec = codec->spec;
665 struct auto_pin_cfg *cfg = &spec->autocfg;
668 for (i = 0; i < cfg->line_outs; i++) {
669 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
670 i, cfg->line_outs, cfg->line_out_type);
674 for (i = 0; i < cfg->hp_outs; i++) {
675 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
676 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
680 for (i = 0; i < cfg->speaker_outs; i++) {
681 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
682 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
692 static const struct snd_kcontrol_new cs_capture_ctls[] = {
693 HDA_BIND_SW("Capture Switch", 0),
694 HDA_BIND_VOL("Capture Volume", 0),
697 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
700 struct cs_spec *spec = codec->spec;
702 if (spec->cur_input == idx && !force)
704 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
705 /* stream is running, let's swap the current ADC */
706 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
707 spec->cur_adc = spec->adc_nid[idx];
708 snd_hda_codec_setup_stream(codec, spec->cur_adc,
709 spec->cur_adc_stream_tag, 0,
710 spec->cur_adc_format);
712 spec->cur_input = idx;
713 cs_update_input_select(codec);
717 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
718 struct snd_ctl_elem_info *uinfo)
720 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
721 struct cs_spec *spec = codec->spec;
722 struct auto_pin_cfg *cfg = &spec->autocfg;
725 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
727 uinfo->value.enumerated.items = spec->num_inputs;
728 if (uinfo->value.enumerated.item >= spec->num_inputs)
729 uinfo->value.enumerated.item = spec->num_inputs - 1;
730 idx = spec->input_idx[uinfo->value.enumerated.item];
731 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
732 uinfo->value.enumerated.name,
733 sizeof(uinfo->value.enumerated.name), NULL);
737 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
738 struct snd_ctl_elem_value *ucontrol)
740 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
741 struct cs_spec *spec = codec->spec;
742 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
746 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
747 struct snd_ctl_elem_value *ucontrol)
749 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
750 struct cs_spec *spec = codec->spec;
751 unsigned int idx = ucontrol->value.enumerated.item[0];
753 if (idx >= spec->num_inputs)
755 idx = spec->input_idx[idx];
756 return change_cur_input(codec, idx, 0);
759 static const struct snd_kcontrol_new cs_capture_source = {
760 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
761 .name = "Capture Source",
762 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
763 .info = cs_capture_source_info,
764 .get = cs_capture_source_get,
765 .put = cs_capture_source_put,
768 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
769 struct hda_ctl_ops *ops)
771 struct cs_spec *spec = codec->spec;
772 struct hda_bind_ctls *bind;
775 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
781 for (i = 0; i < AUTO_PIN_LAST; i++) {
782 if (!spec->adc_nid[i])
785 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
786 spec->adc_idx[i], HDA_INPUT);
791 /* add a (input-boost) volume control to the given input pin */
792 static int add_input_volume_control(struct hda_codec *codec,
793 struct auto_pin_cfg *cfg,
796 hda_nid_t pin = cfg->inputs[item].pin;
799 struct snd_kcontrol *kctl;
801 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
803 caps = query_amp_caps(codec, pin, HDA_INPUT);
804 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
807 label = hda_get_autocfg_input_label(codec, cfg, item);
808 return add_volume(codec, label, 0,
809 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
812 static int build_input(struct hda_codec *codec)
814 struct cs_spec *spec = codec->spec;
817 if (!spec->num_inputs)
820 /* make bind-capture */
821 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
822 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
823 for (i = 0; i < 2; i++) {
824 struct snd_kcontrol *kctl;
826 if (!spec->capture_bind[i])
828 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
831 kctl->private_value = (long)spec->capture_bind[i];
832 err = snd_hda_ctl_add(codec, 0, kctl);
835 for (n = 0; n < AUTO_PIN_LAST; n++) {
836 if (!spec->adc_nid[n])
838 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
844 if (spec->num_inputs > 1 && !spec->mic_detect) {
845 err = snd_hda_ctl_add(codec, 0,
846 snd_ctl_new1(&cs_capture_source, codec));
851 for (i = 0; i < spec->num_inputs; i++) {
852 err = add_input_volume_control(codec, &spec->autocfg, i);
863 static int build_digital_output(struct hda_codec *codec)
865 struct cs_spec *spec = codec->spec;
868 if (!spec->multiout.dig_out_nid)
871 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
872 spec->multiout.dig_out_nid);
875 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
881 static int build_digital_input(struct hda_codec *codec)
883 struct cs_spec *spec = codec->spec;
885 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
890 * auto-mute and auto-mic switching
891 * CS421x auto-output redirecting
895 static void cs_automute(struct hda_codec *codec)
897 struct cs_spec *spec = codec->spec;
898 struct auto_pin_cfg *cfg = &spec->autocfg;
899 unsigned int hp_present;
900 unsigned int spdif_present;
906 nid = cfg->dig_out_pins[0];
907 if (is_jack_detectable(codec, nid)) {
909 TODO: SPDIF output redirect when SENSE_B is enabled.
910 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
913 if (snd_hda_jack_detect(codec, nid)
914 /* && spec->sense_b */)
920 for (i = 0; i < cfg->hp_outs; i++) {
921 nid = cfg->hp_pins[i];
922 if (!is_jack_detectable(codec, nid))
924 hp_present = snd_hda_jack_detect(codec, nid);
929 /* mute speakers if spdif or hp jack is plugged in */
930 for (i = 0; i < cfg->speaker_outs; i++) {
931 int pin_ctl = hp_present ? 0 : PIN_OUT;
932 /* detect on spdif is specific to CS4210 */
933 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
936 nid = cfg->speaker_pins[i];
937 snd_hda_codec_write(codec, nid, 0,
938 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_ctl);
940 if (spec->gpio_eapd_hp) {
941 unsigned int gpio = hp_present ?
942 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
943 snd_hda_codec_write(codec, 0x01, 0,
944 AC_VERB_SET_GPIO_DATA, gpio);
947 /* specific to CS4210 */
948 if (spec->vendor_nid == CS4210_VENDOR_NID) {
949 /* mute HPs if spdif jack (SENSE_B) is present */
950 for (i = 0; i < cfg->hp_outs; i++) {
951 nid = cfg->hp_pins[i];
952 snd_hda_codec_write(codec, nid, 0,
953 AC_VERB_SET_PIN_WIDGET_CONTROL,
954 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
957 /* SPDIF TX on/off */
959 nid = cfg->dig_out_pins[0];
960 snd_hda_codec_write(codec, nid, 0,
961 AC_VERB_SET_PIN_WIDGET_CONTROL,
962 spdif_present ? PIN_OUT : 0);
965 /* Update board GPIOs if neccessary ... */
970 * Auto-input redirect for CS421x
971 * Switch max 3 inputs of a single ADC (nid 3)
974 static void cs_automic(struct hda_codec *codec)
976 struct cs_spec *spec = codec->spec;
977 struct auto_pin_cfg *cfg = &spec->autocfg;
979 unsigned int present;
981 nid = cfg->inputs[spec->automic_idx].pin;
982 present = snd_hda_jack_detect(codec, nid);
984 /* specific to CS421x, single ADC */
985 if (spec->vendor_nid == CS420X_VENDOR_NID) {
987 change_cur_input(codec, spec->automic_idx, 0);
989 change_cur_input(codec, !spec->automic_idx, 0);
992 if (spec->cur_input != spec->automic_idx) {
993 spec->last_input = spec->cur_input;
994 spec->cur_input = spec->automic_idx;
997 spec->cur_input = spec->last_input;
999 cs_update_input_select(codec);
1006 static void init_output(struct hda_codec *codec)
1008 struct cs_spec *spec = codec->spec;
1009 struct auto_pin_cfg *cfg = &spec->autocfg;
1013 for (i = 0; i < spec->multiout.num_dacs; i++)
1014 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1015 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1016 if (spec->multiout.hp_nid)
1017 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1018 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1019 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1020 if (!spec->multiout.extra_out_nid[i])
1022 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1023 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1026 /* set appropriate pin controls */
1027 for (i = 0; i < cfg->line_outs; i++)
1028 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1029 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1031 for (i = 0; i < cfg->hp_outs; i++) {
1032 hda_nid_t nid = cfg->hp_pins[i];
1033 snd_hda_codec_write(codec, nid, 0,
1034 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1035 if (!cfg->speaker_outs)
1037 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1038 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
1039 spec->hp_detect = 1;
1044 for (i = 0; i < cfg->speaker_outs; i++)
1045 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1046 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1048 /* SPDIF is enabled on presence detect for CS421x */
1049 if (spec->hp_detect || spec->spdif_detect)
1053 static void init_input(struct hda_codec *codec)
1055 struct cs_spec *spec = codec->spec;
1056 struct auto_pin_cfg *cfg = &spec->autocfg;
1060 for (i = 0; i < cfg->num_inputs; i++) {
1062 hda_nid_t pin = cfg->inputs[i].pin;
1063 if (!spec->adc_nid[i])
1065 /* set appropriate pin control and mute first */
1067 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1068 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1069 caps >>= AC_PINCAP_VREF_SHIFT;
1070 if (caps & AC_PINCAP_VREF_80)
1073 snd_hda_codec_write(codec, pin, 0,
1074 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1075 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1076 AC_VERB_SET_AMP_GAIN_MUTE,
1077 AMP_IN_MUTE(spec->adc_idx[i]));
1078 if (spec->mic_detect && spec->automic_idx == i)
1079 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
1081 /* CS420x has multiple ADC, CS421x has single ADC */
1082 if (spec->vendor_nid == CS420X_VENDOR_NID) {
1083 change_cur_input(codec, spec->cur_input, 1);
1084 if (spec->mic_detect)
1087 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1088 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1089 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1090 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1091 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1092 * No effect if SPDIF_OUT2 is
1093 * selected in IDX_SPDIF_CTL.
1095 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1097 if (spec->mic_detect)
1100 spec->cur_adc = spec->adc_nid[spec->cur_input];
1101 cs_update_input_select(codec);
1106 static const struct hda_verb cs_coef_init_verbs[] = {
1107 {0x11, AC_VERB_SET_PROC_STATE, 1},
1108 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1109 {0x11, AC_VERB_SET_PROC_COEF,
1110 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1111 | 0x0040 /* Mute DACs on FIFO error */
1112 | 0x1000 /* Enable DACs High Pass Filter */
1113 | 0x0400 /* Disable Coefficient Auto increment */
1116 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1117 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1122 /* Errata: CS4207 rev C0/C1/C2 Silicon
1124 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1126 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1127 * may be excessive (up to an additional 200 μA), which is most easily
1128 * observed while the part is being held in reset (RESET# active low).
1130 * Root Cause: At initial powerup of the device, the logic that drives
1131 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1133 * Certain random patterns will cause a steady leakage current in those
1134 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1136 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1137 * blocks, which will alleviate the issue.
1140 static const struct hda_verb cs_errata_init_verbs[] = {
1141 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1142 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1144 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1145 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1146 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1147 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1148 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1149 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1151 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1152 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1154 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1155 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1156 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1157 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1158 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1159 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1160 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1162 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1163 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1164 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1165 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1172 static void init_digital(struct hda_codec *codec)
1176 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1177 coef |= 0x0008; /* Replace with mute on error */
1178 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1179 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1180 * SPDIF_OUT2 is shared with GPIO1 and
1183 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1186 static int cs_init(struct hda_codec *codec)
1188 struct cs_spec *spec = codec->spec;
1190 /* init_verb sequence for C0/C1/C2 errata*/
1191 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1193 snd_hda_sequence_write(codec, cs_coef_init_verbs);
1195 if (spec->gpio_mask) {
1196 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1198 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1200 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1206 init_digital(codec);
1207 snd_hda_jack_report_sync(codec);
1212 static int cs_build_controls(struct hda_codec *codec)
1214 struct cs_spec *spec = codec->spec;
1217 err = build_output(codec);
1220 err = build_input(codec);
1223 err = build_digital_output(codec);
1226 err = build_digital_input(codec);
1229 err = cs_init(codec);
1233 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1240 static void cs_free(struct hda_codec *codec)
1242 struct cs_spec *spec = codec->spec;
1243 kfree(spec->capture_bind[0]);
1244 kfree(spec->capture_bind[1]);
1248 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1250 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1258 snd_hda_jack_report_sync(codec);
1261 static const struct hda_codec_ops cs_patch_ops = {
1262 .build_controls = cs_build_controls,
1263 .build_pcms = cs_build_pcms,
1266 .unsol_event = cs_unsol_event,
1269 static int cs_parse_auto_config(struct hda_codec *codec)
1271 struct cs_spec *spec = codec->spec;
1274 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1278 err = parse_output(codec);
1281 err = parse_input(codec);
1284 err = parse_digital_output(codec);
1287 err = parse_digital_input(codec);
1293 static const char * const cs420x_models[CS420X_MODELS] = {
1294 [CS420X_MBP53] = "mbp53",
1295 [CS420X_MBP55] = "mbp55",
1296 [CS420X_IMAC27] = "imac27",
1297 [CS420X_IMAC27_122] = "imac27_122",
1298 [CS420X_APPLE] = "apple",
1299 [CS420X_AUTO] = "auto",
1303 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1304 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1305 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1306 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1307 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1308 /* this conflicts with too many other models */
1309 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1313 static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
1314 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
1315 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
1324 static const struct cs_pincfg mbp53_pincfgs[] = {
1325 { 0x09, 0x012b4050 },
1326 { 0x0a, 0x90100141 },
1327 { 0x0b, 0x90100140 },
1328 { 0x0c, 0x018b3020 },
1329 { 0x0d, 0x90a00110 },
1330 { 0x0e, 0x400000f0 },
1331 { 0x0f, 0x01cbe030 },
1332 { 0x10, 0x014be060 },
1333 { 0x12, 0x400000f0 },
1334 { 0x15, 0x400000f0 },
1338 static const struct cs_pincfg mbp55_pincfgs[] = {
1339 { 0x09, 0x012b4030 },
1340 { 0x0a, 0x90100121 },
1341 { 0x0b, 0x90100120 },
1342 { 0x0c, 0x400000f0 },
1343 { 0x0d, 0x90a00110 },
1344 { 0x0e, 0x400000f0 },
1345 { 0x0f, 0x400000f0 },
1346 { 0x10, 0x014be040 },
1347 { 0x12, 0x400000f0 },
1348 { 0x15, 0x400000f0 },
1352 static const struct cs_pincfg imac27_pincfgs[] = {
1353 { 0x09, 0x012b4050 },
1354 { 0x0a, 0x90100140 },
1355 { 0x0b, 0x90100142 },
1356 { 0x0c, 0x018b3020 },
1357 { 0x0d, 0x90a00110 },
1358 { 0x0e, 0x400000f0 },
1359 { 0x0f, 0x01cbe030 },
1360 { 0x10, 0x014be060 },
1361 { 0x12, 0x01ab9070 },
1362 { 0x15, 0x400000f0 },
1366 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1367 [CS420X_MBP53] = mbp53_pincfgs,
1368 [CS420X_MBP55] = mbp55_pincfgs,
1369 [CS420X_IMAC27] = imac27_pincfgs,
1372 static void fix_pincfg(struct hda_codec *codec, int model,
1373 const struct cs_pincfg **pin_configs)
1375 const struct cs_pincfg *cfg = pin_configs[model];
1378 for (; cfg->nid; cfg++)
1379 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1382 static int patch_cs420x(struct hda_codec *codec)
1384 struct cs_spec *spec;
1387 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1392 spec->vendor_nid = CS420X_VENDOR_NID;
1394 spec->board_config =
1395 snd_hda_check_board_config(codec, CS420X_MODELS,
1396 cs420x_models, cs420x_cfg_tbl);
1397 if (spec->board_config < 0)
1398 spec->board_config =
1399 snd_hda_check_board_codec_sid_config(codec,
1400 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
1401 if (spec->board_config >= 0)
1402 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1404 switch (spec->board_config) {
1409 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1410 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1411 spec->gpio_mask = spec->gpio_dir =
1412 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1414 case CS420X_IMAC27_122:
1415 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1416 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1417 spec->gpio_mask = spec->gpio_dir =
1418 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1422 err = cs_parse_auto_config(codec);
1426 codec->patch_ops = cs_patch_ops;
1437 * Cirrus Logic CS4210
1439 * 1 DAC => HP(sense) / Speakers,
1440 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1441 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1444 /* CS4210 board names */
1445 static const char *cs421x_models[CS421X_MODELS] = {
1446 [CS421X_CDB4210] = "cdb4210",
1447 [STUMPY_CDB4210] = "stumpy",
1450 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1451 /* Test Intel board + CDB2410 */
1452 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1456 /* CS4210 board pinconfigs */
1457 /* Default CS4210 (CDB4210)*/
1458 static const struct cs_pincfg cdb4210_pincfgs[] = {
1459 { 0x05, 0x0321401f },
1460 { 0x06, 0x90170010 },
1461 { 0x07, 0x03813031 },
1462 { 0x08, 0xb7a70037 },
1463 { 0x09, 0xb7a6003e },
1464 { 0x0a, 0x034510f0 },
1469 static struct cs_pincfg stumpy_pincfgs[] = {
1470 { 0x05, 0x022120f0 },
1471 { 0x06, 0x901700f0 },
1472 { 0x07, 0x02a120f0 },
1473 { 0x08, 0x77a70037 },
1474 { 0x09, 0x77a6003e },
1475 { 0x0a, 0x434510f0 },
1479 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1480 [CS421X_CDB4210] = cdb4210_pincfgs,
1481 [STUMPY_CDB4210] = stumpy_pincfgs,
1484 static const struct hda_verb cs421x_coef_init_verbs[] = {
1485 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1486 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1488 Disable Coefficient Index Auto-Increment(DAI)=1,
1491 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1493 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1494 /* ADC SZCMode = Digital Soft Ramp */
1495 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1497 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1498 {0x0B, AC_VERB_SET_PROC_COEF,
1499 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1500 | 0x0004 /* Mute DAC on FIFO error */
1501 | 0x0008 /* Enable DAC High Pass Filter */
1506 /* Errata: CS4210 rev A1 Silicon
1508 * http://www.cirrus.com/en/pubs/errata/
1511 * 1. Performance degredation is present in the ADC.
1512 * 2. Speaker output is not completely muted upon HP detect.
1513 * 3. Noise is present when clipping occurs on the amplified
1517 * The following verb sequence written to the registers during
1518 * initialization will correct the issues listed above.
1521 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1522 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1524 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1525 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1527 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1528 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1530 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1531 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1533 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1534 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1536 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1537 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1542 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1543 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1545 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1546 struct snd_ctl_elem_info *uinfo)
1548 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1550 uinfo->value.integer.min = 0;
1551 uinfo->value.integer.max = 3;
1555 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1556 struct snd_ctl_elem_value *ucontrol)
1558 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1560 ucontrol->value.integer.value[0] =
1561 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1565 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1568 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1570 unsigned int vol = ucontrol->value.integer.value[0];
1572 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1573 unsigned int original_coef = coef;
1576 coef |= (vol & 0x0003);
1577 if (original_coef == coef)
1580 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1585 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1587 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1588 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1589 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1590 .name = "Speaker Boost Playback Volume",
1591 .info = cs421x_boost_vol_info,
1592 .get = cs421x_boost_vol_get,
1593 .put = cs421x_boost_vol_put,
1594 .tlv = { .p = cs421x_speaker_boost_db_scale },
1597 static void cs4210_pinmux_init(struct hda_codec *codec)
1599 struct cs_spec *spec = codec->spec;
1600 unsigned int def_conf, coef;
1602 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1603 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1605 if (spec->gpio_mask)
1606 coef |= 0x0008; /* B1,B2 are GPIOs */
1611 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1615 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1617 if ((spec->gpio_mask || spec->sense_b) &&
1618 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1621 GPIO or SENSE_B forced - disconnect the DMIC pin.
1623 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1624 def_conf &= ~AC_DEFCFG_PORT_CONN;
1625 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1626 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1630 static void init_cs421x_digital(struct hda_codec *codec)
1632 struct cs_spec *spec = codec->spec;
1633 struct auto_pin_cfg *cfg = &spec->autocfg;
1637 for (i = 0; i < cfg->dig_outs; i++) {
1638 hda_nid_t nid = cfg->dig_out_pins[i];
1639 if (!cfg->speaker_outs)
1641 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1642 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
1643 spec->spdif_detect = 1;
1648 static int cs421x_init(struct hda_codec *codec)
1650 struct cs_spec *spec = codec->spec;
1652 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1653 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1654 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1655 cs4210_pinmux_init(codec);
1658 if (spec->gpio_mask) {
1659 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1661 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1663 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1669 init_cs421x_digital(codec);
1670 snd_hda_jack_report_sync(codec);
1676 * CS4210 Input MUX (1 ADC)
1678 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1679 struct snd_ctl_elem_info *uinfo)
1681 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1682 struct cs_spec *spec = codec->spec;
1684 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1687 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1688 struct snd_ctl_elem_value *ucontrol)
1690 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1691 struct cs_spec *spec = codec->spec;
1693 ucontrol->value.enumerated.item[0] = spec->cur_input;
1697 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1698 struct snd_ctl_elem_value *ucontrol)
1700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1701 struct cs_spec *spec = codec->spec;
1703 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1704 spec->adc_nid[0], &spec->cur_input);
1708 static struct snd_kcontrol_new cs421x_capture_source = {
1710 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1711 .name = "Capture Source",
1712 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1713 .info = cs421x_mux_enum_info,
1714 .get = cs421x_mux_enum_get,
1715 .put = cs421x_mux_enum_put,
1718 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1720 struct cs_spec *spec = codec->spec;
1721 struct auto_pin_cfg *cfg = &spec->autocfg;
1722 const struct hda_input_mux *imux = &spec->input_mux;
1723 hda_nid_t pin = cfg->inputs[item].pin;
1724 struct snd_kcontrol *kctl;
1727 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1730 caps = query_amp_caps(codec, pin, HDA_INPUT);
1731 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1735 return add_volume(codec, imux->items[item].label, 0,
1736 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1739 /* add a (input-boost) volume control to the given input pin */
1740 static int build_cs421x_input(struct hda_codec *codec)
1742 struct cs_spec *spec = codec->spec;
1743 struct auto_pin_cfg *cfg = &spec->autocfg;
1744 struct hda_input_mux *imux = &spec->input_mux;
1745 int i, err, type_idx;
1748 if (!spec->num_inputs)
1751 /* make bind-capture */
1752 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1753 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1754 for (i = 0; i < 2; i++) {
1755 struct snd_kcontrol *kctl;
1757 if (!spec->capture_bind[i])
1759 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1762 kctl->private_value = (long)spec->capture_bind[i];
1763 err = snd_hda_ctl_add(codec, 0, kctl);
1766 for (n = 0; n < AUTO_PIN_LAST; n++) {
1767 if (!spec->adc_nid[n])
1769 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1775 /* Add Input MUX Items + Capture Volume/Switch */
1776 for (i = 0; i < spec->num_inputs; i++) {
1777 label = hda_get_autocfg_input_label(codec, cfg, i);
1778 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1780 err = cs421x_add_input_volume_control(codec, i);
1786 Add 'Capture Source' Switch if
1787 * 2 inputs and no mic detec
1790 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1791 (spec->num_inputs == 3)) {
1793 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1794 snd_ctl_new1(&cs421x_capture_source, codec));
1802 /* Single DAC (Mute/Gain) */
1803 static int build_cs421x_output(struct hda_codec *codec)
1805 hda_nid_t dac = CS4210_DAC_NID;
1806 struct cs_spec *spec = codec->spec;
1807 struct auto_pin_cfg *cfg = &spec->autocfg;
1808 struct snd_kcontrol *kctl;
1810 char *name = "Master";
1812 fix_volume_caps(codec, dac);
1814 err = add_mute(codec, name, 0,
1815 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1819 err = add_volume(codec, name, 0,
1820 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1824 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
1825 err = snd_hda_ctl_add(codec, 0,
1826 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1833 static int cs421x_build_controls(struct hda_codec *codec)
1835 struct cs_spec *spec = codec->spec;
1838 err = build_cs421x_output(codec);
1841 err = build_cs421x_input(codec);
1844 err = build_digital_output(codec);
1847 err = cs421x_init(codec);
1851 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1858 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1860 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1870 snd_hda_jack_report_sync(codec);
1873 static int parse_cs421x_input(struct hda_codec *codec)
1875 struct cs_spec *spec = codec->spec;
1876 struct auto_pin_cfg *cfg = &spec->autocfg;
1879 for (i = 0; i < cfg->num_inputs; i++) {
1880 hda_nid_t pin = cfg->inputs[i].pin;
1881 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1882 spec->cur_input = spec->last_input = i;
1885 /* check whether the automatic mic switch is available */
1886 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1887 spec->mic_detect = 1;
1888 spec->automic_idx = i;
1894 static int cs421x_parse_auto_config(struct hda_codec *codec)
1896 struct cs_spec *spec = codec->spec;
1899 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1902 err = parse_output(codec);
1905 err = parse_cs421x_input(codec);
1908 err = parse_digital_output(codec);
1916 Manage PDREF, when transitioning to D3hot
1917 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1919 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1921 struct cs_spec *spec = codec->spec;
1924 snd_hda_shutup_pins(codec);
1926 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1927 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1928 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1929 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1931 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1932 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1933 coef |= 0x0004; /* PDREF */
1934 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1941 static struct hda_codec_ops cs421x_patch_ops = {
1942 .build_controls = cs421x_build_controls,
1943 .build_pcms = cs_build_pcms,
1944 .init = cs421x_init,
1946 .unsol_event = cs421x_unsol_event,
1948 .suspend = cs421x_suspend,
1952 static int patch_cs4210(struct hda_codec *codec)
1954 struct cs_spec *spec;
1957 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1962 spec->vendor_nid = CS4210_VENDOR_NID;
1964 spec->board_config =
1965 snd_hda_check_board_config(codec, CS421X_MODELS,
1966 cs421x_models, cs421x_cfg_tbl);
1967 if (spec->board_config >= 0)
1968 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1970 Setup GPIO/SENSE for each board (if used)
1972 switch (spec->board_config) {
1973 case CS421X_CDB4210:
1974 snd_printd("CS4210 board: %s\n",
1975 cs421x_models[spec->board_config]);
1976 /* spec->gpio_mask = 3;
1978 spec->gpio_data = 3;
1986 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1987 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1990 cs4210_pinmux_init(codec);
1992 err = cs421x_parse_auto_config(codec);
1996 codec->patch_ops = cs421x_patch_ops;
2006 static int patch_cs4213(struct hda_codec *codec)
2008 struct cs_spec *spec;
2011 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2016 spec->vendor_nid = CS4213_VENDOR_NID;
2018 err = cs421x_parse_auto_config(codec);
2022 codec->patch_ops = cs421x_patch_ops;
2035 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
2036 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2037 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
2038 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2039 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
2043 MODULE_ALIAS("snd-hda-codec-id:10134206");
2044 MODULE_ALIAS("snd-hda-codec-id:10134207");
2045 MODULE_ALIAS("snd-hda-codec-id:10134210");
2046 MODULE_ALIAS("snd-hda-codec-id:10134213");
2048 MODULE_LICENSE("GPL");
2049 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2051 static struct hda_codec_preset_list cirrus_list = {
2052 .preset = snd_hda_preset_cirrus,
2053 .owner = THIS_MODULE,
2056 static int __init patch_cirrus_init(void)
2058 return snd_hda_add_codec_preset(&cirrus_list);
2061 static void __exit patch_cirrus_exit(void)
2063 snd_hda_delete_codec_preset(&cirrus_list);
2066 module_init(patch_cirrus_init)
2067 module_exit(patch_cirrus_exit)