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 */
96 /* Vendor-specific processing widget */
97 #define CS420X_VENDOR_NID 0x11
98 #define CS_DIG_OUT1_PIN_NID 0x10
99 #define CS_DIG_OUT2_PIN_NID 0x15
100 #define CS_DMIC1_PIN_NID 0x12
101 #define CS_DMIC2_PIN_NID 0x0e
104 #define IDX_SPDIF_STAT 0x0000
105 #define IDX_SPDIF_CTL 0x0001
106 #define IDX_ADC_CFG 0x0002
107 /* SZC bitmask, 4 modes below:
109 * 1 = digital immediate, analog zero-cross
110 * 2 = digtail & analog soft-ramp
111 * 3 = digital soft-ramp, analog zero-cross
113 #define CS_COEF_ADC_SZC_MASK (3 << 0)
114 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
115 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
116 /* PGA mode: 0 = differential, 1 = signle-ended */
117 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
118 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
119 #define IDX_DAC_CFG 0x0003
120 /* SZC bitmask, 4 modes below:
124 * 3 = soft-ramp on zero-cross
126 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
127 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
128 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
130 #define IDX_BEEP_CFG 0x0004
131 /* 0x0008 - test reg key */
132 /* 0x0009 - 0x0014 -> 12 test regs */
133 /* 0x0015 - visibility reg */
136 * Cirrus Logic CS4210
138 * 1 DAC => HP(sense) / Speakers,
139 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
140 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
142 #define CS4210_DAC_NID 0x02
143 #define CS4210_ADC_NID 0x03
144 #define CS4210_VENDOR_NID 0x0B
145 #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
146 #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
148 #define CS421X_IDX_DEV_CFG 0x01
149 #define CS421X_IDX_ADC_CFG 0x02
150 #define CS421X_IDX_DAC_CFG 0x03
151 #define CS421X_IDX_SPK_CTL 0x04
153 #define SPDIF_EVENT 0x04
155 /* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
156 #define CS4213_VENDOR_NID 0x09
159 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
161 struct cs_spec *spec = codec->spec;
162 snd_hda_codec_write(codec, spec->vendor_nid, 0,
163 AC_VERB_SET_COEF_INDEX, idx);
164 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
165 AC_VERB_GET_PROC_COEF, 0);
168 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
171 struct cs_spec *spec = codec->spec;
172 snd_hda_codec_write(codec, spec->vendor_nid, 0,
173 AC_VERB_SET_COEF_INDEX, idx);
174 snd_hda_codec_write(codec, spec->vendor_nid, 0,
175 AC_VERB_SET_PROC_COEF, coef);
185 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
186 struct hda_codec *codec,
187 struct snd_pcm_substream *substream)
189 struct cs_spec *spec = codec->spec;
190 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
194 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
195 struct hda_codec *codec,
196 unsigned int stream_tag,
198 struct snd_pcm_substream *substream)
200 struct cs_spec *spec = codec->spec;
201 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
202 stream_tag, format, substream);
205 static int cs_playback_pcm_cleanup(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_analog_cleanup(codec, &spec->multiout);
216 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
217 struct hda_codec *codec,
218 struct snd_pcm_substream *substream)
220 struct cs_spec *spec = codec->spec;
221 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
224 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
225 struct hda_codec *codec,
226 struct snd_pcm_substream *substream)
228 struct cs_spec *spec = codec->spec;
229 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
232 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
233 struct hda_codec *codec,
234 unsigned int stream_tag,
236 struct snd_pcm_substream *substream)
238 struct cs_spec *spec = codec->spec;
239 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
243 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
244 struct hda_codec *codec,
245 struct snd_pcm_substream *substream)
247 struct cs_spec *spec = codec->spec;
248 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
251 static void cs_update_input_select(struct hda_codec *codec)
253 struct cs_spec *spec = codec->spec;
255 snd_hda_codec_write(codec, spec->cur_adc, 0,
256 AC_VERB_SET_CONNECT_SEL,
257 spec->adc_idx[spec->cur_input]);
263 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
264 struct hda_codec *codec,
265 unsigned int stream_tag,
267 struct snd_pcm_substream *substream)
269 struct cs_spec *spec = codec->spec;
270 spec->cur_adc = spec->adc_nid[spec->cur_input];
271 spec->cur_adc_stream_tag = stream_tag;
272 spec->cur_adc_format = format;
273 cs_update_input_select(codec);
274 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
278 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
279 struct hda_codec *codec,
280 struct snd_pcm_substream *substream)
282 struct cs_spec *spec = codec->spec;
283 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
290 static const struct hda_pcm_stream cs_pcm_analog_playback = {
295 .open = cs_playback_pcm_open,
296 .prepare = cs_playback_pcm_prepare,
297 .cleanup = cs_playback_pcm_cleanup
301 static const struct hda_pcm_stream cs_pcm_analog_capture = {
306 .prepare = cs_capture_pcm_prepare,
307 .cleanup = cs_capture_pcm_cleanup
311 static const struct hda_pcm_stream cs_pcm_digital_playback = {
316 .open = cs_dig_playback_pcm_open,
317 .close = cs_dig_playback_pcm_close,
318 .prepare = cs_dig_playback_pcm_prepare,
319 .cleanup = cs_dig_playback_pcm_cleanup
323 static const struct hda_pcm_stream cs_pcm_digital_capture = {
329 static int cs_build_pcms(struct hda_codec *codec)
331 struct cs_spec *spec = codec->spec;
332 struct hda_pcm *info = spec->pcm_rec;
334 codec->pcm_info = info;
337 info->name = "Cirrus Analog";
338 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
339 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
340 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
341 spec->multiout.max_channels;
342 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
343 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
344 spec->adc_nid[spec->cur_input];
347 if (!spec->multiout.dig_out_nid && !spec->dig_in)
351 info->name = "Cirrus Digital";
352 info->pcm_type = spec->autocfg.dig_out_type[0];
354 info->pcm_type = HDA_PCM_TYPE_SPDIF;
355 if (spec->multiout.dig_out_nid) {
356 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
357 cs_pcm_digital_playback;
358 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
359 spec->multiout.dig_out_nid;
362 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
363 cs_pcm_digital_capture;
364 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
372 * parse codec topology
375 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
380 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
385 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
387 struct cs_spec *spec = codec->spec;
388 struct auto_pin_cfg *cfg = &spec->autocfg;
389 hda_nid_t pin = cfg->inputs[idx].pin;
391 if (!is_jack_detectable(codec, pin))
393 val = snd_hda_codec_get_pincfg(codec, pin);
394 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
397 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
403 nid = codec->start_nid;
404 for (i = 0; i < codec->num_nodes; i++, nid++) {
406 type = get_wcaps_type(get_wcaps(codec, nid));
407 if (type != AC_WID_AUD_IN)
409 idx = snd_hda_get_conn_index(codec, nid, pin, false);
418 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
421 val = snd_hda_codec_get_pincfg(codec, nid);
422 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
425 static int parse_output(struct hda_codec *codec)
427 struct cs_spec *spec = codec->spec;
428 struct auto_pin_cfg *cfg = &spec->autocfg;
432 for (i = 0; i < cfg->line_outs; i++) {
433 dac = get_dac(codec, cfg->line_out_pins[i]);
436 spec->dac_nid[i] = dac;
438 spec->multiout.num_dacs = i;
439 spec->multiout.dac_nids = spec->dac_nid;
440 spec->multiout.max_channels = i * 2;
442 /* add HP and speakers */
444 for (i = 0; i < cfg->hp_outs; i++) {
445 dac = get_dac(codec, cfg->hp_pins[i]);
449 spec->multiout.hp_nid = dac;
451 spec->multiout.extra_out_nid[extra_nids++] = dac;
453 for (i = 0; i < cfg->speaker_outs; i++) {
454 dac = get_dac(codec, cfg->speaker_pins[i]);
457 spec->multiout.extra_out_nid[extra_nids++] = dac;
460 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
461 cfg->speaker_outs = cfg->line_outs;
462 memcpy(cfg->speaker_pins, cfg->line_out_pins,
463 sizeof(cfg->speaker_pins));
470 static int parse_input(struct hda_codec *codec)
472 struct cs_spec *spec = codec->spec;
473 struct auto_pin_cfg *cfg = &spec->autocfg;
476 for (i = 0; i < cfg->num_inputs; i++) {
477 hda_nid_t pin = cfg->inputs[i].pin;
478 spec->input_idx[spec->num_inputs] = i;
479 spec->capsrc_idx[i] = spec->num_inputs++;
481 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
483 if (!spec->num_inputs)
486 /* check whether the automatic mic switch is available */
487 if (spec->num_inputs == 2 &&
488 cfg->inputs[0].type == AUTO_PIN_MIC &&
489 cfg->inputs[1].type == AUTO_PIN_MIC) {
490 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
491 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
492 spec->mic_detect = 1;
493 spec->automic_idx = 0;
496 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
497 spec->mic_detect = 1;
498 spec->automic_idx = 1;
506 static int parse_digital_output(struct hda_codec *codec)
508 struct cs_spec *spec = codec->spec;
509 struct auto_pin_cfg *cfg = &spec->autocfg;
514 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
516 spec->multiout.dig_out_nid = nid;
517 spec->multiout.share_spdif = 1;
518 if (cfg->dig_outs > 1 &&
519 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
520 spec->slave_dig_outs[0] = nid;
521 codec->slave_dig_outs = spec->slave_dig_outs;
526 static int parse_digital_input(struct hda_codec *codec)
528 struct cs_spec *spec = codec->spec;
529 struct auto_pin_cfg *cfg = &spec->autocfg;
533 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
538 * create mixer controls
541 static const char * const dir_sfx[2] = { "Playback", "Capture" };
543 static int add_mute(struct hda_codec *codec, const char *name, int index,
544 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
547 struct snd_kcontrol_new knew =
548 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
549 knew.private_value = pval;
550 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
551 *kctlp = snd_ctl_new1(&knew, codec);
552 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
553 return snd_hda_ctl_add(codec, 0, *kctlp);
556 static int add_volume(struct hda_codec *codec, const char *name,
557 int index, unsigned int pval, int dir,
558 struct snd_kcontrol **kctlp)
561 struct snd_kcontrol_new knew =
562 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
563 knew.private_value = pval;
564 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
565 *kctlp = snd_ctl_new1(&knew, codec);
566 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
567 return snd_hda_ctl_add(codec, 0, *kctlp);
570 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
574 /* set the upper-limit for mixer amp to 0dB */
575 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
576 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
577 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
578 << AC_AMPCAP_NUM_STEPS_SHIFT;
579 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
582 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
584 struct cs_spec *spec = codec->spec;
589 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
590 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
594 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
596 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
597 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
603 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
604 int num_ctls, int type)
606 struct cs_spec *spec = codec->spec;
609 struct snd_kcontrol *kctl;
610 static const char * const speakers[] = {
611 "Front Speaker", "Surround Speaker", "Bass Speaker"
613 static const char * const line_outs[] = {
614 "Front Line Out", "Surround Line Out", "Bass Line Out"
617 fix_volume_caps(codec, dac);
618 if (!spec->vmaster_sw) {
619 err = add_vmaster(codec, dac);
626 case AUTO_PIN_HP_OUT:
630 case AUTO_PIN_SPEAKER_OUT:
632 name = speakers[idx];
638 name = line_outs[idx];
644 err = add_mute(codec, name, index,
645 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
648 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
652 err = add_volume(codec, name, index,
653 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
656 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
663 static int build_output(struct hda_codec *codec)
665 struct cs_spec *spec = codec->spec;
666 struct auto_pin_cfg *cfg = &spec->autocfg;
669 for (i = 0; i < cfg->line_outs; i++) {
670 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
671 i, cfg->line_outs, cfg->line_out_type);
675 for (i = 0; i < cfg->hp_outs; i++) {
676 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
677 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
681 for (i = 0; i < cfg->speaker_outs; i++) {
682 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
683 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
693 static const struct snd_kcontrol_new cs_capture_ctls[] = {
694 HDA_BIND_SW("Capture Switch", 0),
695 HDA_BIND_VOL("Capture Volume", 0),
698 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
701 struct cs_spec *spec = codec->spec;
703 if (spec->cur_input == idx && !force)
705 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
706 /* stream is running, let's swap the current ADC */
707 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
708 spec->cur_adc = spec->adc_nid[idx];
709 snd_hda_codec_setup_stream(codec, spec->cur_adc,
710 spec->cur_adc_stream_tag, 0,
711 spec->cur_adc_format);
713 spec->cur_input = idx;
714 cs_update_input_select(codec);
718 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
719 struct snd_ctl_elem_info *uinfo)
721 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
722 struct cs_spec *spec = codec->spec;
723 struct auto_pin_cfg *cfg = &spec->autocfg;
726 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
728 uinfo->value.enumerated.items = spec->num_inputs;
729 if (uinfo->value.enumerated.item >= spec->num_inputs)
730 uinfo->value.enumerated.item = spec->num_inputs - 1;
731 idx = spec->input_idx[uinfo->value.enumerated.item];
732 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
733 uinfo->value.enumerated.name,
734 sizeof(uinfo->value.enumerated.name), NULL);
738 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
739 struct snd_ctl_elem_value *ucontrol)
741 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
742 struct cs_spec *spec = codec->spec;
743 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
747 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
748 struct snd_ctl_elem_value *ucontrol)
750 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
751 struct cs_spec *spec = codec->spec;
752 unsigned int idx = ucontrol->value.enumerated.item[0];
754 if (idx >= spec->num_inputs)
756 idx = spec->input_idx[idx];
757 return change_cur_input(codec, idx, 0);
760 static const struct snd_kcontrol_new cs_capture_source = {
761 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
762 .name = "Capture Source",
763 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
764 .info = cs_capture_source_info,
765 .get = cs_capture_source_get,
766 .put = cs_capture_source_put,
769 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
770 struct hda_ctl_ops *ops)
772 struct cs_spec *spec = codec->spec;
773 struct hda_bind_ctls *bind;
776 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
782 for (i = 0; i < AUTO_PIN_LAST; i++) {
783 if (!spec->adc_nid[i])
786 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
787 spec->adc_idx[i], HDA_INPUT);
792 /* add a (input-boost) volume control to the given input pin */
793 static int add_input_volume_control(struct hda_codec *codec,
794 struct auto_pin_cfg *cfg,
797 hda_nid_t pin = cfg->inputs[item].pin;
800 struct snd_kcontrol *kctl;
802 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
804 caps = query_amp_caps(codec, pin, HDA_INPUT);
805 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
808 label = hda_get_autocfg_input_label(codec, cfg, item);
809 return add_volume(codec, label, 0,
810 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
813 static int build_input(struct hda_codec *codec)
815 struct cs_spec *spec = codec->spec;
818 if (!spec->num_inputs)
821 /* make bind-capture */
822 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
823 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
824 for (i = 0; i < 2; i++) {
825 struct snd_kcontrol *kctl;
827 if (!spec->capture_bind[i])
829 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
832 kctl->private_value = (long)spec->capture_bind[i];
833 err = snd_hda_ctl_add(codec, 0, kctl);
836 for (n = 0; n < AUTO_PIN_LAST; n++) {
837 if (!spec->adc_nid[n])
839 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
845 if (spec->num_inputs > 1 && !spec->mic_detect) {
846 err = snd_hda_ctl_add(codec, 0,
847 snd_ctl_new1(&cs_capture_source, codec));
852 for (i = 0; i < spec->num_inputs; i++) {
853 err = add_input_volume_control(codec, &spec->autocfg, i);
864 static int build_digital_output(struct hda_codec *codec)
866 struct cs_spec *spec = codec->spec;
869 if (!spec->multiout.dig_out_nid)
872 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
873 spec->multiout.dig_out_nid);
876 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
882 static int build_digital_input(struct hda_codec *codec)
884 struct cs_spec *spec = codec->spec;
886 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
891 * auto-mute and auto-mic switching
892 * CS421x auto-output redirecting
896 static void cs_automute(struct hda_codec *codec)
898 struct cs_spec *spec = codec->spec;
899 struct auto_pin_cfg *cfg = &spec->autocfg;
900 unsigned int hp_present;
901 unsigned int spdif_present;
907 nid = cfg->dig_out_pins[0];
908 if (is_jack_detectable(codec, nid)) {
910 TODO: SPDIF output redirect when SENSE_B is enabled.
911 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
914 if (snd_hda_jack_detect(codec, nid)
915 /* && spec->sense_b */)
921 for (i = 0; i < cfg->hp_outs; i++) {
922 nid = cfg->hp_pins[i];
923 if (!is_jack_detectable(codec, nid))
925 hp_present = snd_hda_jack_detect(codec, nid);
930 /* mute speakers if spdif or hp jack is plugged in */
931 for (i = 0; i < cfg->speaker_outs; i++) {
932 int pin_ctl = hp_present ? 0 : PIN_OUT;
933 /* detect on spdif is specific to CS4210 */
934 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
937 nid = cfg->speaker_pins[i];
938 snd_hda_codec_write(codec, nid, 0,
939 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_ctl);
941 if (spec->gpio_eapd_hp) {
942 unsigned int gpio = hp_present ?
943 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
944 snd_hda_codec_write(codec, 0x01, 0,
945 AC_VERB_SET_GPIO_DATA, gpio);
948 /* specific to CS4210 */
949 if (spec->vendor_nid == CS4210_VENDOR_NID) {
950 /* mute HPs if spdif jack (SENSE_B) is present */
951 for (i = 0; i < cfg->hp_outs; i++) {
952 nid = cfg->hp_pins[i];
953 snd_hda_codec_write(codec, nid, 0,
954 AC_VERB_SET_PIN_WIDGET_CONTROL,
955 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
958 /* SPDIF TX on/off */
960 nid = cfg->dig_out_pins[0];
961 snd_hda_codec_write(codec, nid, 0,
962 AC_VERB_SET_PIN_WIDGET_CONTROL,
963 spdif_present ? PIN_OUT : 0);
966 /* Update board GPIOs if neccessary ... */
971 * Auto-input redirect for CS421x
972 * Switch max 3 inputs of a single ADC (nid 3)
975 static void cs_automic(struct hda_codec *codec)
977 struct cs_spec *spec = codec->spec;
978 struct auto_pin_cfg *cfg = &spec->autocfg;
980 unsigned int present;
982 nid = cfg->inputs[spec->automic_idx].pin;
983 present = snd_hda_jack_detect(codec, nid);
985 /* specific to CS421x, single ADC */
986 if (spec->vendor_nid == CS420X_VENDOR_NID) {
988 change_cur_input(codec, spec->automic_idx, 0);
990 change_cur_input(codec, !spec->automic_idx, 0);
993 if (spec->cur_input != spec->automic_idx) {
994 spec->last_input = spec->cur_input;
995 spec->cur_input = spec->automic_idx;
998 spec->cur_input = spec->last_input;
1000 cs_update_input_select(codec);
1007 static void init_output(struct hda_codec *codec)
1009 struct cs_spec *spec = codec->spec;
1010 struct auto_pin_cfg *cfg = &spec->autocfg;
1014 for (i = 0; i < spec->multiout.num_dacs; i++)
1015 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1016 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1017 if (spec->multiout.hp_nid)
1018 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1019 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1020 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1021 if (!spec->multiout.extra_out_nid[i])
1023 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1024 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1027 /* set appropriate pin controls */
1028 for (i = 0; i < cfg->line_outs; i++)
1029 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1030 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1032 for (i = 0; i < cfg->hp_outs; i++) {
1033 hda_nid_t nid = cfg->hp_pins[i];
1034 snd_hda_codec_write(codec, nid, 0,
1035 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1036 if (!cfg->speaker_outs)
1038 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1039 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
1040 spec->hp_detect = 1;
1045 for (i = 0; i < cfg->speaker_outs; i++)
1046 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1047 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1049 /* SPDIF is enabled on presence detect for CS421x */
1050 if (spec->hp_detect || spec->spdif_detect)
1054 static void init_input(struct hda_codec *codec)
1056 struct cs_spec *spec = codec->spec;
1057 struct auto_pin_cfg *cfg = &spec->autocfg;
1061 for (i = 0; i < cfg->num_inputs; i++) {
1063 hda_nid_t pin = cfg->inputs[i].pin;
1064 if (!spec->adc_nid[i])
1066 /* set appropriate pin control and mute first */
1068 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1069 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1070 caps >>= AC_PINCAP_VREF_SHIFT;
1071 if (caps & AC_PINCAP_VREF_80)
1074 snd_hda_codec_write(codec, pin, 0,
1075 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1076 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1077 AC_VERB_SET_AMP_GAIN_MUTE,
1078 AMP_IN_MUTE(spec->adc_idx[i]));
1079 if (spec->mic_detect && spec->automic_idx == i)
1080 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
1082 /* CS420x has multiple ADC, CS421x has single ADC */
1083 if (spec->vendor_nid == CS420X_VENDOR_NID) {
1084 change_cur_input(codec, spec->cur_input, 1);
1085 if (spec->mic_detect)
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);
1098 if (spec->mic_detect)
1101 spec->cur_adc = spec->adc_nid[spec->cur_input];
1102 cs_update_input_select(codec);
1107 static const struct hda_verb cs_coef_init_verbs[] = {
1108 {0x11, AC_VERB_SET_PROC_STATE, 1},
1109 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1110 {0x11, AC_VERB_SET_PROC_COEF,
1111 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1112 | 0x0040 /* Mute DACs on FIFO error */
1113 | 0x1000 /* Enable DACs High Pass Filter */
1114 | 0x0400 /* Disable Coefficient Auto increment */
1117 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1118 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1123 /* Errata: CS4207 rev C0/C1/C2 Silicon
1125 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1127 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1128 * may be excessive (up to an additional 200 μA), which is most easily
1129 * observed while the part is being held in reset (RESET# active low).
1131 * Root Cause: At initial powerup of the device, the logic that drives
1132 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1134 * Certain random patterns will cause a steady leakage current in those
1135 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1137 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1138 * blocks, which will alleviate the issue.
1141 static const struct hda_verb cs_errata_init_verbs[] = {
1142 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1143 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1145 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1146 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1147 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1148 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1149 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1150 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1152 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1153 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1155 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1156 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1157 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1158 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1159 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1160 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1161 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1163 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1164 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1165 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1166 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1173 static void init_digital(struct hda_codec *codec)
1177 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1178 coef |= 0x0008; /* Replace with mute on error */
1179 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1180 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1181 * SPDIF_OUT2 is shared with GPIO1 and
1184 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1187 static int cs_init(struct hda_codec *codec)
1189 struct cs_spec *spec = codec->spec;
1191 /* init_verb sequence for C0/C1/C2 errata*/
1192 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1194 snd_hda_sequence_write(codec, cs_coef_init_verbs);
1196 if (spec->gpio_mask) {
1197 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1199 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1201 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1207 init_digital(codec);
1208 snd_hda_jack_report_sync(codec);
1213 static int cs_build_controls(struct hda_codec *codec)
1215 struct cs_spec *spec = codec->spec;
1218 err = build_output(codec);
1221 err = build_input(codec);
1224 err = build_digital_output(codec);
1227 err = build_digital_input(codec);
1230 err = cs_init(codec);
1234 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1241 static void cs_free(struct hda_codec *codec)
1243 struct cs_spec *spec = codec->spec;
1244 kfree(spec->capture_bind[0]);
1245 kfree(spec->capture_bind[1]);
1249 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1251 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1259 snd_hda_jack_report_sync(codec);
1262 static const struct hda_codec_ops cs_patch_ops = {
1263 .build_controls = cs_build_controls,
1264 .build_pcms = cs_build_pcms,
1267 .unsol_event = cs_unsol_event,
1270 static int cs_parse_auto_config(struct hda_codec *codec)
1272 struct cs_spec *spec = codec->spec;
1275 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1279 err = parse_output(codec);
1282 err = parse_input(codec);
1285 err = parse_digital_output(codec);
1288 err = parse_digital_input(codec);
1294 static const char * const cs420x_models[CS420X_MODELS] = {
1295 [CS420X_MBP53] = "mbp53",
1296 [CS420X_MBP55] = "mbp55",
1297 [CS420X_IMAC27] = "imac27",
1298 [CS420X_IMAC27_122] = "imac27_122",
1299 [CS420X_APPLE] = "apple",
1300 [CS420X_AUTO] = "auto",
1304 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1305 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1306 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1307 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1308 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1309 /* this conflicts with too many other models */
1310 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1314 static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
1315 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
1316 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
1325 static const struct cs_pincfg mbp53_pincfgs[] = {
1326 { 0x09, 0x012b4050 },
1327 { 0x0a, 0x90100141 },
1328 { 0x0b, 0x90100140 },
1329 { 0x0c, 0x018b3020 },
1330 { 0x0d, 0x90a00110 },
1331 { 0x0e, 0x400000f0 },
1332 { 0x0f, 0x01cbe030 },
1333 { 0x10, 0x014be060 },
1334 { 0x12, 0x400000f0 },
1335 { 0x15, 0x400000f0 },
1339 static const struct cs_pincfg mbp55_pincfgs[] = {
1340 { 0x09, 0x012b4030 },
1341 { 0x0a, 0x90100121 },
1342 { 0x0b, 0x90100120 },
1343 { 0x0c, 0x400000f0 },
1344 { 0x0d, 0x90a00110 },
1345 { 0x0e, 0x400000f0 },
1346 { 0x0f, 0x400000f0 },
1347 { 0x10, 0x014be040 },
1348 { 0x12, 0x400000f0 },
1349 { 0x15, 0x400000f0 },
1353 static const struct cs_pincfg imac27_pincfgs[] = {
1354 { 0x09, 0x012b4050 },
1355 { 0x0a, 0x90100140 },
1356 { 0x0b, 0x90100142 },
1357 { 0x0c, 0x018b3020 },
1358 { 0x0d, 0x90a00110 },
1359 { 0x0e, 0x400000f0 },
1360 { 0x0f, 0x01cbe030 },
1361 { 0x10, 0x014be060 },
1362 { 0x12, 0x01ab9070 },
1363 { 0x15, 0x400000f0 },
1367 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1368 [CS420X_MBP53] = mbp53_pincfgs,
1369 [CS420X_MBP55] = mbp55_pincfgs,
1370 [CS420X_IMAC27] = imac27_pincfgs,
1373 static void fix_pincfg(struct hda_codec *codec, int model,
1374 const struct cs_pincfg **pin_configs)
1376 const struct cs_pincfg *cfg = pin_configs[model];
1379 for (; cfg->nid; cfg++)
1380 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1383 static int patch_cs420x(struct hda_codec *codec)
1385 struct cs_spec *spec;
1388 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1393 spec->vendor_nid = CS420X_VENDOR_NID;
1395 spec->board_config =
1396 snd_hda_check_board_config(codec, CS420X_MODELS,
1397 cs420x_models, cs420x_cfg_tbl);
1398 if (spec->board_config < 0)
1399 spec->board_config =
1400 snd_hda_check_board_codec_sid_config(codec,
1401 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
1402 if (spec->board_config >= 0)
1403 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1405 switch (spec->board_config) {
1410 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1411 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1412 spec->gpio_mask = spec->gpio_dir =
1413 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1415 case CS420X_IMAC27_122:
1416 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1417 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1418 spec->gpio_mask = spec->gpio_dir =
1419 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1423 err = cs_parse_auto_config(codec);
1427 codec->patch_ops = cs_patch_ops;
1438 * Cirrus Logic CS4210
1440 * 1 DAC => HP(sense) / Speakers,
1441 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1442 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1445 /* CS4210 board names */
1446 static const char *cs421x_models[CS421X_MODELS] = {
1447 [CS421X_CDB4210] = "cdb4210",
1448 [STUMPY_CDB4210] = "stumpy",
1449 [LUMPY_CDB4210] = "lumpy",
1452 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1453 /* Test Intel board + CDB2410 */
1454 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1458 /* CS4210 board pinconfigs */
1459 /* Default CS4210 (CDB4210)*/
1460 static const struct cs_pincfg cdb4210_pincfgs[] = {
1461 { 0x05, 0x0321401f },
1462 { 0x06, 0x90170010 },
1463 { 0x07, 0x03813031 },
1464 { 0x08, 0xb7a70037 },
1465 { 0x09, 0xb7a6003e },
1466 { 0x0a, 0x034510f0 },
1471 static struct cs_pincfg stumpy_pincfgs[] = {
1472 { 0x05, 0x022120f0 },
1473 { 0x06, 0x901700f0 },
1474 { 0x07, 0x02a120f0 },
1475 { 0x08, 0x77a70037 },
1476 { 0x09, 0x77a6003e },
1477 { 0x0a, 0x434510f0 },
1482 static struct cs_pincfg lumpy_pincfgs[] = {
1483 { 0x05, 0x022110f0 },
1484 { 0x06, 0x901700f0 },
1485 { 0x07, 0x02a110f0 },
1486 { 0x08, 0x77a70037 },
1487 { 0x09, 0xb7a6003e },
1488 { 0x0a, 0x400000f0 },
1492 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1493 [CS421X_CDB4210] = cdb4210_pincfgs,
1494 [STUMPY_CDB4210] = stumpy_pincfgs,
1495 [LUMPY_CDB4210] = lumpy_pincfgs,
1498 static const struct hda_verb cs421x_coef_init_verbs[] = {
1499 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1500 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1502 Disable Coefficient Index Auto-Increment(DAI)=1,
1505 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1507 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1508 /* ADC SZCMode = Digital Soft Ramp */
1509 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1511 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1512 {0x0B, AC_VERB_SET_PROC_COEF,
1513 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1514 | 0x0004 /* Mute DAC on FIFO error */
1515 | 0x0008 /* Enable DAC High Pass Filter */
1520 /* Errata: CS4210 rev A1 Silicon
1522 * http://www.cirrus.com/en/pubs/errata/
1525 * 1. Performance degredation is present in the ADC.
1526 * 2. Speaker output is not completely muted upon HP detect.
1527 * 3. Noise is present when clipping occurs on the amplified
1531 * The following verb sequence written to the registers during
1532 * initialization will correct the issues listed above.
1535 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1536 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1538 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1539 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1541 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1542 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1544 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1545 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1547 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1548 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1550 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1551 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1556 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1557 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1559 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_info *uinfo)
1562 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1564 uinfo->value.integer.min = 0;
1565 uinfo->value.integer.max = 3;
1569 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1570 struct snd_ctl_elem_value *ucontrol)
1572 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1574 ucontrol->value.integer.value[0] =
1575 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1579 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1580 struct snd_ctl_elem_value *ucontrol)
1582 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1584 unsigned int vol = ucontrol->value.integer.value[0];
1586 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1587 unsigned int original_coef = coef;
1590 coef |= (vol & 0x0003);
1591 if (original_coef == coef)
1594 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1599 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1601 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1602 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1603 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1604 .name = "Speaker Boost Playback Volume",
1605 .info = cs421x_boost_vol_info,
1606 .get = cs421x_boost_vol_get,
1607 .put = cs421x_boost_vol_put,
1608 .tlv = { .p = cs421x_speaker_boost_db_scale },
1611 static void cs4210_pinmux_init(struct hda_codec *codec)
1613 struct cs_spec *spec = codec->spec;
1614 unsigned int def_conf, coef;
1616 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1617 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1619 if (spec->gpio_mask)
1620 coef |= 0x0008; /* B1,B2 are GPIOs */
1625 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1629 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1631 if ((spec->gpio_mask || spec->sense_b) &&
1632 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1635 GPIO or SENSE_B forced - disconnect the DMIC pin.
1637 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1638 def_conf &= ~AC_DEFCFG_PORT_CONN;
1639 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1640 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1644 static void init_cs421x_digital(struct hda_codec *codec)
1646 struct cs_spec *spec = codec->spec;
1647 struct auto_pin_cfg *cfg = &spec->autocfg;
1651 for (i = 0; i < cfg->dig_outs; i++) {
1652 hda_nid_t nid = cfg->dig_out_pins[i];
1653 if (!cfg->speaker_outs)
1655 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1656 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
1657 spec->spdif_detect = 1;
1662 static int cs421x_init(struct hda_codec *codec)
1664 struct cs_spec *spec = codec->spec;
1666 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1667 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1668 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1669 cs4210_pinmux_init(codec);
1672 if (spec->gpio_mask) {
1673 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1675 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1677 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1683 init_cs421x_digital(codec);
1684 snd_hda_jack_report_sync(codec);
1690 * CS4210 Input MUX (1 ADC)
1692 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_info *uinfo)
1695 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1696 struct cs_spec *spec = codec->spec;
1698 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1701 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1702 struct snd_ctl_elem_value *ucontrol)
1704 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1705 struct cs_spec *spec = codec->spec;
1707 ucontrol->value.enumerated.item[0] = spec->cur_input;
1711 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_value *ucontrol)
1714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1715 struct cs_spec *spec = codec->spec;
1717 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1718 spec->adc_nid[0], &spec->cur_input);
1722 static struct snd_kcontrol_new cs421x_capture_source = {
1724 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1725 .name = "Capture Source",
1726 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1727 .info = cs421x_mux_enum_info,
1728 .get = cs421x_mux_enum_get,
1729 .put = cs421x_mux_enum_put,
1732 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1734 struct cs_spec *spec = codec->spec;
1735 struct auto_pin_cfg *cfg = &spec->autocfg;
1736 const struct hda_input_mux *imux = &spec->input_mux;
1737 hda_nid_t pin = cfg->inputs[item].pin;
1738 struct snd_kcontrol *kctl;
1741 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1744 caps = query_amp_caps(codec, pin, HDA_INPUT);
1745 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1749 return add_volume(codec, imux->items[item].label, 0,
1750 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1753 /* add a (input-boost) volume control to the given input pin */
1754 static int build_cs421x_input(struct hda_codec *codec)
1756 struct cs_spec *spec = codec->spec;
1757 struct auto_pin_cfg *cfg = &spec->autocfg;
1758 struct hda_input_mux *imux = &spec->input_mux;
1759 int i, err, type_idx;
1762 if (!spec->num_inputs)
1765 /* make bind-capture */
1766 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1767 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1768 for (i = 0; i < 2; i++) {
1769 struct snd_kcontrol *kctl;
1771 if (!spec->capture_bind[i])
1773 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1776 kctl->private_value = (long)spec->capture_bind[i];
1777 err = snd_hda_ctl_add(codec, 0, kctl);
1780 for (n = 0; n < AUTO_PIN_LAST; n++) {
1781 if (!spec->adc_nid[n])
1783 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1789 /* Add Input MUX Items + Capture Volume/Switch */
1790 for (i = 0; i < spec->num_inputs; i++) {
1791 label = hda_get_autocfg_input_label(codec, cfg, i);
1792 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1794 err = cs421x_add_input_volume_control(codec, i);
1800 Add 'Capture Source' Switch if
1801 * 2 inputs and no mic detec
1804 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1805 (spec->num_inputs == 3)) {
1807 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1808 snd_ctl_new1(&cs421x_capture_source, codec));
1816 /* Single DAC (Mute/Gain) */
1817 static int build_cs421x_output(struct hda_codec *codec)
1819 hda_nid_t dac = CS4210_DAC_NID;
1820 struct cs_spec *spec = codec->spec;
1821 struct auto_pin_cfg *cfg = &spec->autocfg;
1822 struct snd_kcontrol *kctl;
1824 char *name = "Master";
1826 fix_volume_caps(codec, dac);
1828 err = add_mute(codec, name, 0,
1829 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1833 err = add_volume(codec, name, 0,
1834 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1838 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
1839 err = snd_hda_ctl_add(codec, 0,
1840 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1847 static int cs421x_build_controls(struct hda_codec *codec)
1849 struct cs_spec *spec = codec->spec;
1852 err = build_cs421x_output(codec);
1855 err = build_cs421x_input(codec);
1858 err = build_digital_output(codec);
1861 err = cs421x_init(codec);
1865 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1872 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1874 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1884 snd_hda_jack_report_sync(codec);
1887 static int parse_cs421x_input(struct hda_codec *codec)
1889 struct cs_spec *spec = codec->spec;
1890 struct auto_pin_cfg *cfg = &spec->autocfg;
1893 for (i = 0; i < cfg->num_inputs; i++) {
1894 hda_nid_t pin = cfg->inputs[i].pin;
1895 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1896 spec->cur_input = spec->last_input = i;
1899 /* check whether the automatic mic switch is available */
1900 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1901 spec->mic_detect = 1;
1902 spec->automic_idx = i;
1908 static int cs421x_parse_auto_config(struct hda_codec *codec)
1910 struct cs_spec *spec = codec->spec;
1913 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1916 err = parse_output(codec);
1919 err = parse_cs421x_input(codec);
1922 err = parse_digital_output(codec);
1930 Manage PDREF, when transitioning to D3hot
1931 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1933 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1935 struct cs_spec *spec = codec->spec;
1938 snd_hda_shutup_pins(codec);
1940 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1941 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1942 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1943 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1945 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1946 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1947 coef |= 0x0004; /* PDREF */
1948 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1955 static struct hda_codec_ops cs421x_patch_ops = {
1956 .build_controls = cs421x_build_controls,
1957 .build_pcms = cs_build_pcms,
1958 .init = cs421x_init,
1960 .unsol_event = cs421x_unsol_event,
1962 .suspend = cs421x_suspend,
1966 static int patch_cs4210(struct hda_codec *codec)
1968 struct cs_spec *spec;
1971 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1976 spec->vendor_nid = CS4210_VENDOR_NID;
1978 spec->board_config =
1979 snd_hda_check_board_config(codec, CS421X_MODELS,
1980 cs421x_models, cs421x_cfg_tbl);
1981 if (spec->board_config >= 0)
1982 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1984 Setup GPIO/SENSE for each board (if used)
1986 switch (spec->board_config) {
1987 case CS421X_CDB4210:
1988 snd_printd("CS4210 board: %s\n",
1989 cs421x_models[spec->board_config]);
1990 /* spec->gpio_mask = 3;
1992 spec->gpio_data = 3;
2000 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
2001 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
2004 cs4210_pinmux_init(codec);
2006 err = cs421x_parse_auto_config(codec);
2010 codec->patch_ops = cs421x_patch_ops;
2020 static int patch_cs4213(struct hda_codec *codec)
2022 struct cs_spec *spec;
2025 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2030 spec->vendor_nid = CS4213_VENDOR_NID;
2032 err = cs421x_parse_auto_config(codec);
2036 codec->patch_ops = cs421x_patch_ops;
2049 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
2050 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2051 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
2052 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2053 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
2057 MODULE_ALIAS("snd-hda-codec-id:10134206");
2058 MODULE_ALIAS("snd-hda-codec-id:10134207");
2059 MODULE_ALIAS("snd-hda-codec-id:10134210");
2060 MODULE_ALIAS("snd-hda-codec-id:10134213");
2062 MODULE_LICENSE("GPL");
2063 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2065 static struct hda_codec_preset_list cirrus_list = {
2066 .preset = snd_hda_preset_cirrus,
2067 .owner = THIS_MODULE,
2070 static int __init patch_cirrus_init(void)
2072 return snd_hda_add_codec_preset(&cirrus_list);
2075 static void __exit patch_cirrus_exit(void)
2077 snd_hda_delete_codec_preset(&cirrus_list);
2080 module_init(patch_cirrus_init)
2081 module_exit(patch_cirrus_exit)