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;
63 struct hda_pcm pcm_rec[2]; /* PCM information */
65 unsigned int hp_detect:1;
66 unsigned int mic_detect:1;
68 unsigned int spdif_detect:1;
69 unsigned int sense_b:1;
71 struct hda_input_mux input_mux;
72 unsigned int last_input;
75 /* available models with CS420x */
90 /* Vendor-specific processing widget */
91 #define CS420X_VENDOR_NID 0x11
92 #define CS_DIG_OUT1_PIN_NID 0x10
93 #define CS_DIG_OUT2_PIN_NID 0x15
94 #define CS_DMIC1_PIN_NID 0x12
95 #define CS_DMIC2_PIN_NID 0x0e
98 #define IDX_SPDIF_STAT 0x0000
99 #define IDX_SPDIF_CTL 0x0001
100 #define IDX_ADC_CFG 0x0002
101 /* SZC bitmask, 4 modes below:
103 * 1 = digital immediate, analog zero-cross
104 * 2 = digtail & analog soft-ramp
105 * 3 = digital soft-ramp, analog zero-cross
107 #define CS_COEF_ADC_SZC_MASK (3 << 0)
108 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
109 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
110 /* PGA mode: 0 = differential, 1 = signle-ended */
111 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
112 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
113 #define IDX_DAC_CFG 0x0003
114 /* SZC bitmask, 4 modes below:
118 * 3 = soft-ramp on zero-cross
120 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
121 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
122 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
124 #define IDX_BEEP_CFG 0x0004
125 /* 0x0008 - test reg key */
126 /* 0x0009 - 0x0014 -> 12 test regs */
127 /* 0x0015 - visibility reg */
130 * Cirrus Logic CS4210
132 * 1 DAC => HP(sense) / Speakers,
133 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
134 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
136 #define CS4210_DAC_NID 0x02
137 #define CS4210_ADC_NID 0x03
138 #define CS421X_VENDOR_NID 0x0B
139 #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
140 #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
142 #define CS421X_IDX_DEV_CFG 0x01
143 #define CS421X_IDX_ADC_CFG 0x02
144 #define CS421X_IDX_DAC_CFG 0x03
145 #define CS421X_IDX_SPK_CTL 0x04
147 #define SPDIF_EVENT 0x04
149 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
151 struct cs_spec *spec = codec->spec;
152 snd_hda_codec_write(codec, spec->vendor_nid, 0,
153 AC_VERB_SET_COEF_INDEX, idx);
154 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
155 AC_VERB_GET_PROC_COEF, 0);
158 static inline void cs_vendor_coef_set(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 snd_hda_codec_write(codec, spec->vendor_nid, 0,
165 AC_VERB_SET_PROC_COEF, coef);
175 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
176 struct hda_codec *codec,
177 struct snd_pcm_substream *substream)
179 struct cs_spec *spec = codec->spec;
180 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
184 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
185 struct hda_codec *codec,
186 unsigned int stream_tag,
188 struct snd_pcm_substream *substream)
190 struct cs_spec *spec = codec->spec;
191 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
192 stream_tag, format, substream);
195 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
196 struct hda_codec *codec,
197 struct snd_pcm_substream *substream)
199 struct cs_spec *spec = codec->spec;
200 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
206 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
207 struct hda_codec *codec,
208 struct snd_pcm_substream *substream)
210 struct cs_spec *spec = codec->spec;
211 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
214 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
215 struct hda_codec *codec,
216 struct snd_pcm_substream *substream)
218 struct cs_spec *spec = codec->spec;
219 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
222 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
223 struct hda_codec *codec,
224 unsigned int stream_tag,
226 struct snd_pcm_substream *substream)
228 struct cs_spec *spec = codec->spec;
229 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
233 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
234 struct hda_codec *codec,
235 struct snd_pcm_substream *substream)
237 struct cs_spec *spec = codec->spec;
238 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
244 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
245 struct hda_codec *codec,
246 unsigned int stream_tag,
248 struct snd_pcm_substream *substream)
250 struct cs_spec *spec = codec->spec;
251 spec->cur_adc = spec->adc_nid[spec->cur_input];
252 spec->cur_adc_stream_tag = stream_tag;
253 spec->cur_adc_format = format;
254 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
258 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
259 struct hda_codec *codec,
260 struct snd_pcm_substream *substream)
262 struct cs_spec *spec = codec->spec;
263 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
270 static const struct hda_pcm_stream cs_pcm_analog_playback = {
275 .open = cs_playback_pcm_open,
276 .prepare = cs_playback_pcm_prepare,
277 .cleanup = cs_playback_pcm_cleanup
281 static const struct hda_pcm_stream cs_pcm_analog_capture = {
286 .prepare = cs_capture_pcm_prepare,
287 .cleanup = cs_capture_pcm_cleanup
291 static const struct hda_pcm_stream cs_pcm_digital_playback = {
296 .open = cs_dig_playback_pcm_open,
297 .close = cs_dig_playback_pcm_close,
298 .prepare = cs_dig_playback_pcm_prepare,
299 .cleanup = cs_dig_playback_pcm_cleanup
303 static const struct hda_pcm_stream cs_pcm_digital_capture = {
309 static int cs_build_pcms(struct hda_codec *codec)
311 struct cs_spec *spec = codec->spec;
312 struct hda_pcm *info = spec->pcm_rec;
314 codec->pcm_info = info;
317 info->name = "Cirrus Analog";
318 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
319 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
320 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
321 spec->multiout.max_channels;
322 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
323 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
324 spec->adc_nid[spec->cur_input];
327 if (!spec->multiout.dig_out_nid && !spec->dig_in)
331 info->name = "Cirrus Digital";
332 info->pcm_type = spec->autocfg.dig_out_type[0];
334 info->pcm_type = HDA_PCM_TYPE_SPDIF;
335 if (spec->multiout.dig_out_nid) {
336 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
337 cs_pcm_digital_playback;
338 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
339 spec->multiout.dig_out_nid;
342 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
343 cs_pcm_digital_capture;
344 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
352 * parse codec topology
355 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
360 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
365 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
367 struct cs_spec *spec = codec->spec;
368 struct auto_pin_cfg *cfg = &spec->autocfg;
369 hda_nid_t pin = cfg->inputs[idx].pin;
371 if (!is_jack_detectable(codec, pin))
373 val = snd_hda_codec_get_pincfg(codec, pin);
374 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
377 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
383 nid = codec->start_nid;
384 for (i = 0; i < codec->num_nodes; i++, nid++) {
386 type = get_wcaps_type(get_wcaps(codec, nid));
387 if (type != AC_WID_AUD_IN)
389 idx = snd_hda_get_conn_index(codec, nid, pin, false);
398 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
401 val = snd_hda_codec_get_pincfg(codec, nid);
402 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
405 static int parse_output(struct hda_codec *codec)
407 struct cs_spec *spec = codec->spec;
408 struct auto_pin_cfg *cfg = &spec->autocfg;
412 for (i = 0; i < cfg->line_outs; i++) {
413 dac = get_dac(codec, cfg->line_out_pins[i]);
416 spec->dac_nid[i] = dac;
418 spec->multiout.num_dacs = i;
419 spec->multiout.dac_nids = spec->dac_nid;
420 spec->multiout.max_channels = i * 2;
422 /* add HP and speakers */
424 for (i = 0; i < cfg->hp_outs; i++) {
425 dac = get_dac(codec, cfg->hp_pins[i]);
429 spec->multiout.hp_nid = dac;
431 spec->multiout.extra_out_nid[extra_nids++] = dac;
433 for (i = 0; i < cfg->speaker_outs; i++) {
434 dac = get_dac(codec, cfg->speaker_pins[i]);
437 spec->multiout.extra_out_nid[extra_nids++] = dac;
440 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
441 cfg->speaker_outs = cfg->line_outs;
442 memcpy(cfg->speaker_pins, cfg->line_out_pins,
443 sizeof(cfg->speaker_pins));
450 static int parse_input(struct hda_codec *codec)
452 struct cs_spec *spec = codec->spec;
453 struct auto_pin_cfg *cfg = &spec->autocfg;
456 for (i = 0; i < cfg->num_inputs; i++) {
457 hda_nid_t pin = cfg->inputs[i].pin;
458 spec->input_idx[spec->num_inputs] = i;
459 spec->capsrc_idx[i] = spec->num_inputs++;
461 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
463 if (!spec->num_inputs)
466 /* check whether the automatic mic switch is available */
467 if (spec->num_inputs == 2 &&
468 cfg->inputs[0].type == AUTO_PIN_MIC &&
469 cfg->inputs[1].type == AUTO_PIN_MIC) {
470 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
471 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
472 spec->mic_detect = 1;
473 spec->automic_idx = 0;
476 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
477 spec->mic_detect = 1;
478 spec->automic_idx = 1;
486 static int parse_digital_output(struct hda_codec *codec)
488 struct cs_spec *spec = codec->spec;
489 struct auto_pin_cfg *cfg = &spec->autocfg;
494 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
496 spec->multiout.dig_out_nid = nid;
497 spec->multiout.share_spdif = 1;
498 if (cfg->dig_outs > 1 &&
499 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
500 spec->slave_dig_outs[0] = nid;
501 codec->slave_dig_outs = spec->slave_dig_outs;
506 static int parse_digital_input(struct hda_codec *codec)
508 struct cs_spec *spec = codec->spec;
509 struct auto_pin_cfg *cfg = &spec->autocfg;
513 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
518 * create mixer controls
521 static const char * const dir_sfx[2] = { "Playback", "Capture" };
523 static int add_mute(struct hda_codec *codec, const char *name, int index,
524 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
527 struct snd_kcontrol_new knew =
528 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
529 knew.private_value = pval;
530 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
531 *kctlp = snd_ctl_new1(&knew, codec);
532 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
533 return snd_hda_ctl_add(codec, 0, *kctlp);
536 static int add_volume(struct hda_codec *codec, const char *name,
537 int index, unsigned int pval, int dir,
538 struct snd_kcontrol **kctlp)
541 struct snd_kcontrol_new knew =
542 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
543 knew.private_value = pval;
544 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
545 *kctlp = snd_ctl_new1(&knew, codec);
546 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
547 return snd_hda_ctl_add(codec, 0, *kctlp);
550 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
554 /* set the upper-limit for mixer amp to 0dB */
555 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
556 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
557 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
558 << AC_AMPCAP_NUM_STEPS_SHIFT;
559 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
562 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
564 struct cs_spec *spec = codec->spec;
569 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
570 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
574 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
576 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
577 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
583 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
584 int num_ctls, int type)
586 struct cs_spec *spec = codec->spec;
589 struct snd_kcontrol *kctl;
590 static const char * const speakers[] = {
591 "Front Speaker", "Surround Speaker", "Bass Speaker"
593 static const char * const line_outs[] = {
594 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
597 fix_volume_caps(codec, dac);
598 if (!spec->vmaster_sw) {
599 err = add_vmaster(codec, dac);
606 case AUTO_PIN_HP_OUT:
610 case AUTO_PIN_SPEAKER_OUT:
612 name = speakers[idx];
618 name = line_outs[idx];
624 err = add_mute(codec, name, index,
625 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
628 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
632 err = add_volume(codec, name, index,
633 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
636 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
643 static int build_output(struct hda_codec *codec)
645 struct cs_spec *spec = codec->spec;
646 struct auto_pin_cfg *cfg = &spec->autocfg;
649 for (i = 0; i < cfg->line_outs; i++) {
650 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
651 i, cfg->line_outs, cfg->line_out_type);
655 for (i = 0; i < cfg->hp_outs; i++) {
656 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
657 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
661 for (i = 0; i < cfg->speaker_outs; i++) {
662 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
663 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
673 static const struct snd_kcontrol_new cs_capture_ctls[] = {
674 HDA_BIND_SW("Capture Switch", 0),
675 HDA_BIND_VOL("Capture Volume", 0),
678 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
681 struct cs_spec *spec = codec->spec;
683 if (spec->cur_input == idx && !force)
685 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
686 /* stream is running, let's swap the current ADC */
687 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
688 spec->cur_adc = spec->adc_nid[idx];
689 snd_hda_codec_setup_stream(codec, spec->cur_adc,
690 spec->cur_adc_stream_tag, 0,
691 spec->cur_adc_format);
693 snd_hda_codec_write(codec, spec->cur_adc, 0,
694 AC_VERB_SET_CONNECT_SEL,
696 spec->cur_input = idx;
700 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
701 struct snd_ctl_elem_info *uinfo)
703 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
704 struct cs_spec *spec = codec->spec;
705 struct auto_pin_cfg *cfg = &spec->autocfg;
708 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
710 uinfo->value.enumerated.items = spec->num_inputs;
711 if (uinfo->value.enumerated.item >= spec->num_inputs)
712 uinfo->value.enumerated.item = spec->num_inputs - 1;
713 idx = spec->input_idx[uinfo->value.enumerated.item];
714 strcpy(uinfo->value.enumerated.name,
715 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, NULL));
719 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
720 struct snd_ctl_elem_value *ucontrol)
722 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
723 struct cs_spec *spec = codec->spec;
724 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
728 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
729 struct snd_ctl_elem_value *ucontrol)
731 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
732 struct cs_spec *spec = codec->spec;
733 unsigned int idx = ucontrol->value.enumerated.item[0];
735 if (idx >= spec->num_inputs)
737 idx = spec->input_idx[idx];
738 return change_cur_input(codec, idx, 0);
741 static const struct snd_kcontrol_new cs_capture_source = {
742 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
743 .name = "Capture Source",
744 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
745 .info = cs_capture_source_info,
746 .get = cs_capture_source_get,
747 .put = cs_capture_source_put,
750 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
751 struct hda_ctl_ops *ops)
753 struct cs_spec *spec = codec->spec;
754 struct hda_bind_ctls *bind;
757 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
763 for (i = 0; i < AUTO_PIN_LAST; i++) {
764 if (!spec->adc_nid[i])
767 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
768 spec->adc_idx[i], HDA_INPUT);
773 /* add a (input-boost) volume control to the given input pin */
774 static int add_input_volume_control(struct hda_codec *codec,
775 struct auto_pin_cfg *cfg,
778 hda_nid_t pin = cfg->inputs[item].pin;
781 struct snd_kcontrol *kctl;
783 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
785 caps = query_amp_caps(codec, pin, HDA_INPUT);
786 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
789 label = hda_get_autocfg_input_label(codec, cfg, item);
790 return add_volume(codec, label, 0,
791 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
794 static int build_input(struct hda_codec *codec)
796 struct cs_spec *spec = codec->spec;
799 if (!spec->num_inputs)
802 /* make bind-capture */
803 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
804 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
805 for (i = 0; i < 2; i++) {
806 struct snd_kcontrol *kctl;
808 if (!spec->capture_bind[i])
810 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
813 kctl->private_value = (long)spec->capture_bind[i];
814 err = snd_hda_ctl_add(codec, 0, kctl);
817 for (n = 0; n < AUTO_PIN_LAST; n++) {
818 if (!spec->adc_nid[n])
820 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
826 if (spec->num_inputs > 1 && !spec->mic_detect) {
827 err = snd_hda_ctl_add(codec, 0,
828 snd_ctl_new1(&cs_capture_source, codec));
833 for (i = 0; i < spec->num_inputs; i++) {
834 err = add_input_volume_control(codec, &spec->autocfg, i);
845 static int build_digital_output(struct hda_codec *codec)
847 struct cs_spec *spec = codec->spec;
850 if (!spec->multiout.dig_out_nid)
853 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
854 spec->multiout.dig_out_nid);
857 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
863 static int build_digital_input(struct hda_codec *codec)
865 struct cs_spec *spec = codec->spec;
867 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
872 * auto-mute and auto-mic switching
873 * CS421x auto-output redirecting
877 static void cs_automute(struct hda_codec *codec)
879 struct cs_spec *spec = codec->spec;
880 struct auto_pin_cfg *cfg = &spec->autocfg;
881 unsigned int hp_present;
882 unsigned int spdif_present;
888 nid = cfg->dig_out_pins[0];
889 if (is_jack_detectable(codec, nid)) {
891 TODO: SPDIF output redirect when SENSE_B is enabled.
892 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
895 if (snd_hda_jack_detect(codec, nid)
896 /* && spec->sense_b */)
902 for (i = 0; i < cfg->hp_outs; i++) {
903 nid = cfg->hp_pins[i];
904 if (!is_jack_detectable(codec, nid))
906 hp_present = snd_hda_jack_detect(codec, nid);
911 /* mute speakers if spdif or hp jack is plugged in */
912 for (i = 0; i < cfg->speaker_outs; i++) {
913 nid = cfg->speaker_pins[i];
914 snd_hda_codec_write(codec, nid, 0,
915 AC_VERB_SET_PIN_WIDGET_CONTROL,
916 hp_present ? 0 : PIN_OUT);
917 /* detect on spdif is specific to CS421x */
918 if (spec->vendor_nid == CS421X_VENDOR_NID) {
919 snd_hda_codec_write(codec, nid, 0,
920 AC_VERB_SET_PIN_WIDGET_CONTROL,
921 spdif_present ? 0 : PIN_OUT);
924 if (spec->board_config == CS420X_MBP53 ||
925 spec->board_config == CS420X_MBP55 ||
926 spec->board_config == CS420X_IMAC27) {
927 unsigned int gpio = hp_present ? 0x02 : 0x08;
928 snd_hda_codec_write(codec, 0x01, 0,
929 AC_VERB_SET_GPIO_DATA, gpio);
932 /* specific to CS421x */
933 if (spec->vendor_nid == CS421X_VENDOR_NID) {
934 /* mute HPs if spdif jack (SENSE_B) is present */
935 for (i = 0; i < cfg->hp_outs; i++) {
936 nid = cfg->hp_pins[i];
937 snd_hda_codec_write(codec, nid, 0,
938 AC_VERB_SET_PIN_WIDGET_CONTROL,
939 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
942 /* SPDIF TX on/off */
944 nid = cfg->dig_out_pins[0];
945 snd_hda_codec_write(codec, nid, 0,
946 AC_VERB_SET_PIN_WIDGET_CONTROL,
947 spdif_present ? PIN_OUT : 0);
950 /* Update board GPIOs if neccessary ... */
955 * Auto-input redirect for CS421x
956 * Switch max 3 inputs of a single ADC (nid 3)
959 static void cs_automic(struct hda_codec *codec)
961 struct cs_spec *spec = codec->spec;
962 struct auto_pin_cfg *cfg = &spec->autocfg;
964 unsigned int present;
966 nid = cfg->inputs[spec->automic_idx].pin;
967 present = snd_hda_jack_detect(codec, nid);
969 /* specific to CS421x, single ADC */
970 if (spec->vendor_nid == CS421X_VENDOR_NID) {
972 spec->last_input = spec->cur_input;
973 spec->cur_input = spec->automic_idx;
975 spec->cur_input = spec->last_input;
978 snd_hda_codec_write_cache(codec, spec->cur_adc, 0,
979 AC_VERB_SET_CONNECT_SEL,
980 spec->adc_idx[spec->cur_input]);
983 change_cur_input(codec, spec->automic_idx, 0);
985 change_cur_input(codec, !spec->automic_idx, 0);
992 static void init_output(struct hda_codec *codec)
994 struct cs_spec *spec = codec->spec;
995 struct auto_pin_cfg *cfg = &spec->autocfg;
999 for (i = 0; i < spec->multiout.num_dacs; i++)
1000 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1001 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1002 if (spec->multiout.hp_nid)
1003 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1004 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1005 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1006 if (!spec->multiout.extra_out_nid[i])
1008 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1009 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1012 /* set appropriate pin controls */
1013 for (i = 0; i < cfg->line_outs; i++)
1014 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1015 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1017 for (i = 0; i < cfg->hp_outs; i++) {
1018 hda_nid_t nid = cfg->hp_pins[i];
1019 snd_hda_codec_write(codec, nid, 0,
1020 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1021 if (!cfg->speaker_outs)
1023 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1024 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
1025 spec->hp_detect = 1;
1030 for (i = 0; i < cfg->speaker_outs; i++)
1031 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1032 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1034 /* SPDIF is enabled on presence detect for CS421x */
1035 if (spec->hp_detect || spec->spdif_detect)
1039 static void init_input(struct hda_codec *codec)
1041 struct cs_spec *spec = codec->spec;
1042 struct auto_pin_cfg *cfg = &spec->autocfg;
1046 for (i = 0; i < cfg->num_inputs; i++) {
1048 hda_nid_t pin = cfg->inputs[i].pin;
1049 if (!spec->adc_nid[i])
1051 /* set appropriate pin control and mute first */
1053 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1054 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1055 caps >>= AC_PINCAP_VREF_SHIFT;
1056 if (caps & AC_PINCAP_VREF_80)
1059 snd_hda_codec_write(codec, pin, 0,
1060 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1061 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1062 AC_VERB_SET_AMP_GAIN_MUTE,
1063 AMP_IN_MUTE(spec->adc_idx[i]));
1064 if (spec->mic_detect && spec->automic_idx == i)
1065 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
1067 /* specific to CS421x */
1068 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1069 if (spec->mic_detect)
1072 spec->cur_adc = spec->adc_nid[spec->cur_input];
1073 snd_hda_codec_write(codec, spec->cur_adc, 0,
1074 AC_VERB_SET_CONNECT_SEL,
1075 spec->adc_idx[spec->cur_input]);
1078 change_cur_input(codec, spec->cur_input, 1);
1079 if (spec->mic_detect)
1082 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1083 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1084 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1085 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1086 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1087 * No effect if SPDIF_OUT2 is
1088 * selected in IDX_SPDIF_CTL.
1090 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1094 static const struct hda_verb cs_coef_init_verbs[] = {
1095 {0x11, AC_VERB_SET_PROC_STATE, 1},
1096 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1097 {0x11, AC_VERB_SET_PROC_COEF,
1098 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1099 | 0x0040 /* Mute DACs on FIFO error */
1100 | 0x1000 /* Enable DACs High Pass Filter */
1101 | 0x0400 /* Disable Coefficient Auto increment */
1104 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1105 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1110 /* Errata: CS4207 rev C0/C1/C2 Silicon
1112 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1114 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1115 * may be excessive (up to an additional 200 μA), which is most easily
1116 * observed while the part is being held in reset (RESET# active low).
1118 * Root Cause: At initial powerup of the device, the logic that drives
1119 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1121 * Certain random patterns will cause a steady leakage current in those
1122 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1124 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1125 * blocks, which will alleviate the issue.
1128 static const struct hda_verb cs_errata_init_verbs[] = {
1129 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1130 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1132 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1133 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1134 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1135 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1136 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1137 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1139 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1140 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1142 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1143 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1144 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1145 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1146 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1147 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1148 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1150 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1151 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1152 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1153 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1160 static void init_digital(struct hda_codec *codec)
1164 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1165 coef |= 0x0008; /* Replace with mute on error */
1166 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1167 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1168 * SPDIF_OUT2 is shared with GPIO1 and
1171 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1174 static int cs_init(struct hda_codec *codec)
1176 struct cs_spec *spec = codec->spec;
1178 /* init_verb sequence for C0/C1/C2 errata*/
1179 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1181 snd_hda_sequence_write(codec, cs_coef_init_verbs);
1183 if (spec->gpio_mask) {
1184 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1186 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1188 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1194 init_digital(codec);
1195 snd_hda_jack_report_sync(codec);
1200 static int cs_build_controls(struct hda_codec *codec)
1202 struct cs_spec *spec = codec->spec;
1205 err = build_output(codec);
1208 err = build_input(codec);
1211 err = build_digital_output(codec);
1214 err = build_digital_input(codec);
1217 err = cs_init(codec);
1221 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1228 static void cs_free(struct hda_codec *codec)
1230 struct cs_spec *spec = codec->spec;
1231 kfree(spec->capture_bind[0]);
1232 kfree(spec->capture_bind[1]);
1236 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1238 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1246 snd_hda_jack_report_sync(codec);
1249 static const struct hda_codec_ops cs_patch_ops = {
1250 .build_controls = cs_build_controls,
1251 .build_pcms = cs_build_pcms,
1254 .unsol_event = cs_unsol_event,
1257 static int cs_parse_auto_config(struct hda_codec *codec)
1259 struct cs_spec *spec = codec->spec;
1262 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1266 err = parse_output(codec);
1269 err = parse_input(codec);
1272 err = parse_digital_output(codec);
1275 err = parse_digital_input(codec);
1281 static const char * const cs420x_models[CS420X_MODELS] = {
1282 [CS420X_MBP53] = "mbp53",
1283 [CS420X_MBP55] = "mbp55",
1284 [CS420X_IMAC27] = "imac27",
1285 [CS420X_AUTO] = "auto",
1289 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1290 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1291 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1292 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1293 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1294 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
1303 static const struct cs_pincfg mbp53_pincfgs[] = {
1304 { 0x09, 0x012b4050 },
1305 { 0x0a, 0x90100141 },
1306 { 0x0b, 0x90100140 },
1307 { 0x0c, 0x018b3020 },
1308 { 0x0d, 0x90a00110 },
1309 { 0x0e, 0x400000f0 },
1310 { 0x0f, 0x01cbe030 },
1311 { 0x10, 0x014be060 },
1312 { 0x12, 0x400000f0 },
1313 { 0x15, 0x400000f0 },
1317 static const struct cs_pincfg mbp55_pincfgs[] = {
1318 { 0x09, 0x012b4030 },
1319 { 0x0a, 0x90100121 },
1320 { 0x0b, 0x90100120 },
1321 { 0x0c, 0x400000f0 },
1322 { 0x0d, 0x90a00110 },
1323 { 0x0e, 0x400000f0 },
1324 { 0x0f, 0x400000f0 },
1325 { 0x10, 0x014be040 },
1326 { 0x12, 0x400000f0 },
1327 { 0x15, 0x400000f0 },
1331 static const struct cs_pincfg imac27_pincfgs[] = {
1332 { 0x09, 0x012b4050 },
1333 { 0x0a, 0x90100140 },
1334 { 0x0b, 0x90100142 },
1335 { 0x0c, 0x018b3020 },
1336 { 0x0d, 0x90a00110 },
1337 { 0x0e, 0x400000f0 },
1338 { 0x0f, 0x01cbe030 },
1339 { 0x10, 0x014be060 },
1340 { 0x12, 0x01ab9070 },
1341 { 0x15, 0x400000f0 },
1345 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1346 [CS420X_MBP53] = mbp53_pincfgs,
1347 [CS420X_MBP55] = mbp55_pincfgs,
1348 [CS420X_IMAC27] = imac27_pincfgs,
1351 static void fix_pincfg(struct hda_codec *codec, int model,
1352 const struct cs_pincfg **pin_configs)
1354 const struct cs_pincfg *cfg = pin_configs[model];
1357 for (; cfg->nid; cfg++)
1358 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1361 static int patch_cs420x(struct hda_codec *codec)
1363 struct cs_spec *spec;
1366 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1371 spec->vendor_nid = CS420X_VENDOR_NID;
1373 spec->board_config =
1374 snd_hda_check_board_config(codec, CS420X_MODELS,
1375 cs420x_models, cs420x_cfg_tbl);
1376 if (spec->board_config >= 0)
1377 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1379 switch (spec->board_config) {
1383 /* GPIO1 = headphones */
1384 /* GPIO3 = speakers */
1385 spec->gpio_mask = 0x0a;
1386 spec->gpio_dir = 0x0a;
1390 err = cs_parse_auto_config(codec);
1394 codec->patch_ops = cs_patch_ops;
1405 * Cirrus Logic CS4210
1407 * 1 DAC => HP(sense) / Speakers,
1408 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1409 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1412 /* CS4210 board names */
1413 static const char *cs421x_models[CS421X_MODELS] = {
1414 [CS421X_CDB4210] = "cdb4210",
1417 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1418 /* Test Intel board + CDB2410 */
1419 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1423 /* CS4210 board pinconfigs */
1424 /* Default CS4210 (CDB4210)*/
1425 static const struct cs_pincfg cdb4210_pincfgs[] = {
1426 { 0x05, 0x0321401f },
1427 { 0x06, 0x90170010 },
1428 { 0x07, 0x03813031 },
1429 { 0x08, 0xb7a70037 },
1430 { 0x09, 0xb7a6003e },
1431 { 0x0a, 0x034510f0 },
1435 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1436 [CS421X_CDB4210] = cdb4210_pincfgs,
1439 static const struct hda_verb cs421x_coef_init_verbs[] = {
1440 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1441 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1443 Disable Coefficient Index Auto-Increment(DAI)=1,
1446 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1448 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1449 /* ADC SZCMode = Digital Soft Ramp */
1450 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1452 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1453 {0x0B, AC_VERB_SET_PROC_COEF,
1454 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1455 | 0x0004 /* Mute DAC on FIFO error */
1456 | 0x0008 /* Enable DAC High Pass Filter */
1461 /* Errata: CS4210 rev A1 Silicon
1463 * http://www.cirrus.com/en/pubs/errata/
1466 * 1. Performance degredation is present in the ADC.
1467 * 2. Speaker output is not completely muted upon HP detect.
1468 * 3. Noise is present when clipping occurs on the amplified
1472 * The following verb sequence written to the registers during
1473 * initialization will correct the issues listed above.
1476 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1477 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1479 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1480 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1482 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1483 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1485 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1486 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1488 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1489 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1491 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1492 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1497 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1498 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1500 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_info *uinfo)
1503 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1505 uinfo->value.integer.min = 0;
1506 uinfo->value.integer.max = 3;
1510 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1511 struct snd_ctl_elem_value *ucontrol)
1513 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1515 ucontrol->value.integer.value[0] =
1516 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1520 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1521 struct snd_ctl_elem_value *ucontrol)
1523 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1525 unsigned int vol = ucontrol->value.integer.value[0];
1527 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1528 unsigned int original_coef = coef;
1531 coef |= (vol & 0x0003);
1532 if (original_coef == coef)
1535 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1540 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1542 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1543 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1544 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1545 .name = "Speaker Boost Playback Volume",
1546 .info = cs421x_boost_vol_info,
1547 .get = cs421x_boost_vol_get,
1548 .put = cs421x_boost_vol_put,
1549 .tlv = { .p = cs421x_speaker_boost_db_scale },
1552 static void cs421x_pinmux_init(struct hda_codec *codec)
1554 struct cs_spec *spec = codec->spec;
1555 unsigned int def_conf, coef;
1557 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1558 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1560 if (spec->gpio_mask)
1561 coef |= 0x0008; /* B1,B2 are GPIOs */
1566 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1570 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1572 if ((spec->gpio_mask || spec->sense_b) &&
1573 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1576 GPIO or SENSE_B forced - disconnect the DMIC pin.
1578 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1579 def_conf &= ~AC_DEFCFG_PORT_CONN;
1580 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1581 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1585 static void init_cs421x_digital(struct hda_codec *codec)
1587 struct cs_spec *spec = codec->spec;
1588 struct auto_pin_cfg *cfg = &spec->autocfg;
1592 for (i = 0; i < cfg->dig_outs; i++) {
1593 hda_nid_t nid = cfg->dig_out_pins[i];
1594 if (!cfg->speaker_outs)
1596 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1597 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
1598 spec->spdif_detect = 1;
1603 static int cs421x_init(struct hda_codec *codec)
1605 struct cs_spec *spec = codec->spec;
1607 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1608 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1610 cs421x_pinmux_init(codec);
1612 if (spec->gpio_mask) {
1613 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1615 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1617 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1623 init_cs421x_digital(codec);
1624 snd_hda_jack_report_sync(codec);
1630 * CS4210 Input MUX (1 ADC)
1632 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1633 struct snd_ctl_elem_info *uinfo)
1635 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1636 struct cs_spec *spec = codec->spec;
1638 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1641 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_value *ucontrol)
1644 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1645 struct cs_spec *spec = codec->spec;
1647 ucontrol->value.enumerated.item[0] = spec->cur_input;
1651 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1652 struct snd_ctl_elem_value *ucontrol)
1654 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1655 struct cs_spec *spec = codec->spec;
1657 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1658 spec->adc_nid[0], &spec->cur_input);
1662 static struct snd_kcontrol_new cs421x_capture_source = {
1664 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1665 .name = "Capture Source",
1666 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1667 .info = cs421x_mux_enum_info,
1668 .get = cs421x_mux_enum_get,
1669 .put = cs421x_mux_enum_put,
1672 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1674 struct cs_spec *spec = codec->spec;
1675 struct auto_pin_cfg *cfg = &spec->autocfg;
1676 const struct hda_input_mux *imux = &spec->input_mux;
1677 hda_nid_t pin = cfg->inputs[item].pin;
1678 struct snd_kcontrol *kctl;
1681 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1684 caps = query_amp_caps(codec, pin, HDA_INPUT);
1685 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1689 return add_volume(codec, imux->items[item].label, 0,
1690 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1693 /* add a (input-boost) volume control to the given input pin */
1694 static int build_cs421x_input(struct hda_codec *codec)
1696 struct cs_spec *spec = codec->spec;
1697 struct auto_pin_cfg *cfg = &spec->autocfg;
1698 struct hda_input_mux *imux = &spec->input_mux;
1699 int i, err, type_idx;
1702 if (!spec->num_inputs)
1705 /* make bind-capture */
1706 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1707 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1708 for (i = 0; i < 2; i++) {
1709 struct snd_kcontrol *kctl;
1711 if (!spec->capture_bind[i])
1713 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1716 kctl->private_value = (long)spec->capture_bind[i];
1717 err = snd_hda_ctl_add(codec, 0, kctl);
1720 for (n = 0; n < AUTO_PIN_LAST; n++) {
1721 if (!spec->adc_nid[n])
1723 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1729 /* Add Input MUX Items + Capture Volume/Switch */
1730 for (i = 0; i < spec->num_inputs; i++) {
1731 label = hda_get_autocfg_input_label(codec, cfg, i);
1732 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1734 err = cs421x_add_input_volume_control(codec, i);
1740 Add 'Capture Source' Switch if
1741 * 2 inputs and no mic detec
1744 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1745 (spec->num_inputs == 3)) {
1747 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1748 snd_ctl_new1(&cs421x_capture_source, codec));
1756 /* Single DAC (Mute/Gain) */
1757 static int build_cs421x_output(struct hda_codec *codec)
1759 hda_nid_t dac = CS4210_DAC_NID;
1760 struct cs_spec *spec = codec->spec;
1761 struct auto_pin_cfg *cfg = &spec->autocfg;
1762 struct snd_kcontrol *kctl;
1764 char *name = "HP/Speakers";
1766 fix_volume_caps(codec, dac);
1767 if (!spec->vmaster_sw) {
1768 err = add_vmaster(codec, dac);
1773 err = add_mute(codec, name, 0,
1774 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1777 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1781 err = add_volume(codec, name, 0,
1782 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1785 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1789 if (cfg->speaker_outs) {
1790 err = snd_hda_ctl_add(codec, 0,
1791 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1798 static int cs421x_build_controls(struct hda_codec *codec)
1800 struct cs_spec *spec = codec->spec;
1803 err = build_cs421x_output(codec);
1806 err = build_cs421x_input(codec);
1809 err = build_digital_output(codec);
1812 err = cs421x_init(codec);
1816 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1823 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1825 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1835 snd_hda_jack_report_sync(codec);
1838 static int parse_cs421x_input(struct hda_codec *codec)
1840 struct cs_spec *spec = codec->spec;
1841 struct auto_pin_cfg *cfg = &spec->autocfg;
1844 for (i = 0; i < cfg->num_inputs; i++) {
1845 hda_nid_t pin = cfg->inputs[i].pin;
1846 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1847 spec->cur_input = spec->last_input = i;
1850 /* check whether the automatic mic switch is available */
1851 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1852 spec->mic_detect = 1;
1853 spec->automic_idx = i;
1859 static int cs421x_parse_auto_config(struct hda_codec *codec)
1861 struct cs_spec *spec = codec->spec;
1864 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1867 err = parse_output(codec);
1870 err = parse_cs421x_input(codec);
1873 err = parse_digital_output(codec);
1881 Manage PDREF, when transitioning to D3hot
1882 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1884 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1888 snd_hda_shutup_pins(codec);
1890 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1891 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1892 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1893 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1895 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1896 coef |= 0x0004; /* PDREF */
1897 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1903 static struct hda_codec_ops cs4210_patch_ops = {
1904 .build_controls = cs421x_build_controls,
1905 .build_pcms = cs_build_pcms,
1906 .init = cs421x_init,
1908 .unsol_event = cs421x_unsol_event,
1910 .suspend = cs421x_suspend,
1914 static int patch_cs421x(struct hda_codec *codec)
1916 struct cs_spec *spec;
1919 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1924 spec->vendor_nid = CS421X_VENDOR_NID;
1926 spec->board_config =
1927 snd_hda_check_board_config(codec, CS421X_MODELS,
1928 cs421x_models, cs421x_cfg_tbl);
1929 if (spec->board_config >= 0)
1930 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1932 Setup GPIO/SENSE for each board (if used)
1934 switch (spec->board_config) {
1935 case CS421X_CDB4210:
1936 snd_printd("CS4210 board: %s\n",
1937 cs421x_models[spec->board_config]);
1938 /* spec->gpio_mask = 3;
1940 spec->gpio_data = 3;
1948 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1949 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1952 cs421x_pinmux_init(codec);
1954 err = cs421x_parse_auto_config(codec);
1958 codec->patch_ops = cs4210_patch_ops;
1972 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
1973 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1974 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1975 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
1979 MODULE_ALIAS("snd-hda-codec-id:10134206");
1980 MODULE_ALIAS("snd-hda-codec-id:10134207");
1981 MODULE_ALIAS("snd-hda-codec-id:10134210");
1983 MODULE_LICENSE("GPL");
1984 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1986 static struct hda_codec_preset_list cirrus_list = {
1987 .preset = snd_hda_preset_cirrus,
1988 .owner = THIS_MODULE,
1991 static int __init patch_cirrus_init(void)
1993 return snd_hda_add_codec_preset(&cirrus_list);
1996 static void __exit patch_cirrus_exit(void)
1998 snd_hda_delete_codec_preset(&cirrus_list);
2001 module_init(patch_cirrus_init)
2002 module_exit(patch_cirrus_exit)