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 */
93 /* Vendor-specific processing widget */
94 #define CS420X_VENDOR_NID 0x11
95 #define CS_DIG_OUT1_PIN_NID 0x10
96 #define CS_DIG_OUT2_PIN_NID 0x15
97 #define CS_DMIC1_PIN_NID 0x12
98 #define CS_DMIC2_PIN_NID 0x0e
101 #define IDX_SPDIF_STAT 0x0000
102 #define IDX_SPDIF_CTL 0x0001
103 #define IDX_ADC_CFG 0x0002
104 /* SZC bitmask, 4 modes below:
106 * 1 = digital immediate, analog zero-cross
107 * 2 = digtail & analog soft-ramp
108 * 3 = digital soft-ramp, analog zero-cross
110 #define CS_COEF_ADC_SZC_MASK (3 << 0)
111 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
112 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
113 /* PGA mode: 0 = differential, 1 = signle-ended */
114 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
115 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
116 #define IDX_DAC_CFG 0x0003
117 /* SZC bitmask, 4 modes below:
121 * 3 = soft-ramp on zero-cross
123 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
124 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
125 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
127 #define IDX_BEEP_CFG 0x0004
128 /* 0x0008 - test reg key */
129 /* 0x0009 - 0x0014 -> 12 test regs */
130 /* 0x0015 - visibility reg */
133 * Cirrus Logic CS4210
135 * 1 DAC => HP(sense) / Speakers,
136 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
137 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
139 #define CS4210_DAC_NID 0x02
140 #define CS4210_ADC_NID 0x03
141 #define CS421X_VENDOR_NID 0x0B
142 #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
143 #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
145 #define CS421X_IDX_DEV_CFG 0x01
146 #define CS421X_IDX_ADC_CFG 0x02
147 #define CS421X_IDX_DAC_CFG 0x03
148 #define CS421X_IDX_SPK_CTL 0x04
150 #define SPDIF_EVENT 0x04
152 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
154 struct cs_spec *spec = codec->spec;
155 snd_hda_codec_write(codec, spec->vendor_nid, 0,
156 AC_VERB_SET_COEF_INDEX, idx);
157 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
158 AC_VERB_GET_PROC_COEF, 0);
161 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
164 struct cs_spec *spec = codec->spec;
165 snd_hda_codec_write(codec, spec->vendor_nid, 0,
166 AC_VERB_SET_COEF_INDEX, idx);
167 snd_hda_codec_write(codec, spec->vendor_nid, 0,
168 AC_VERB_SET_PROC_COEF, coef);
178 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
179 struct hda_codec *codec,
180 struct snd_pcm_substream *substream)
182 struct cs_spec *spec = codec->spec;
183 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
187 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
188 struct hda_codec *codec,
189 unsigned int stream_tag,
191 struct snd_pcm_substream *substream)
193 struct cs_spec *spec = codec->spec;
194 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
195 stream_tag, format, substream);
198 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
199 struct hda_codec *codec,
200 struct snd_pcm_substream *substream)
202 struct cs_spec *spec = codec->spec;
203 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
209 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
210 struct hda_codec *codec,
211 struct snd_pcm_substream *substream)
213 struct cs_spec *spec = codec->spec;
214 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
217 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
218 struct hda_codec *codec,
219 struct snd_pcm_substream *substream)
221 struct cs_spec *spec = codec->spec;
222 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
225 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
226 struct hda_codec *codec,
227 unsigned int stream_tag,
229 struct snd_pcm_substream *substream)
231 struct cs_spec *spec = codec->spec;
232 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
236 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
237 struct hda_codec *codec,
238 struct snd_pcm_substream *substream)
240 struct cs_spec *spec = codec->spec;
241 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
244 static void cs_update_input_select(struct hda_codec *codec)
246 struct cs_spec *spec = codec->spec;
248 snd_hda_codec_write(codec, spec->cur_adc, 0,
249 AC_VERB_SET_CONNECT_SEL,
250 spec->adc_idx[spec->cur_input]);
256 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
257 struct hda_codec *codec,
258 unsigned int stream_tag,
260 struct snd_pcm_substream *substream)
262 struct cs_spec *spec = codec->spec;
263 spec->cur_adc = spec->adc_nid[spec->cur_input];
264 spec->cur_adc_stream_tag = stream_tag;
265 spec->cur_adc_format = format;
266 cs_update_input_select(codec);
267 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
271 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
272 struct hda_codec *codec,
273 struct snd_pcm_substream *substream)
275 struct cs_spec *spec = codec->spec;
276 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
283 static const struct hda_pcm_stream cs_pcm_analog_playback = {
288 .open = cs_playback_pcm_open,
289 .prepare = cs_playback_pcm_prepare,
290 .cleanup = cs_playback_pcm_cleanup
294 static const struct hda_pcm_stream cs_pcm_analog_capture = {
299 .prepare = cs_capture_pcm_prepare,
300 .cleanup = cs_capture_pcm_cleanup
304 static const struct hda_pcm_stream cs_pcm_digital_playback = {
309 .open = cs_dig_playback_pcm_open,
310 .close = cs_dig_playback_pcm_close,
311 .prepare = cs_dig_playback_pcm_prepare,
312 .cleanup = cs_dig_playback_pcm_cleanup
316 static const struct hda_pcm_stream cs_pcm_digital_capture = {
322 static int cs_build_pcms(struct hda_codec *codec)
324 struct cs_spec *spec = codec->spec;
325 struct hda_pcm *info = spec->pcm_rec;
327 codec->pcm_info = info;
330 info->name = "Cirrus Analog";
331 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
332 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
333 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
334 spec->multiout.max_channels;
335 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
336 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
337 spec->adc_nid[spec->cur_input];
340 if (!spec->multiout.dig_out_nid && !spec->dig_in)
344 info->name = "Cirrus Digital";
345 info->pcm_type = spec->autocfg.dig_out_type[0];
347 info->pcm_type = HDA_PCM_TYPE_SPDIF;
348 if (spec->multiout.dig_out_nid) {
349 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
350 cs_pcm_digital_playback;
351 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
352 spec->multiout.dig_out_nid;
355 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
356 cs_pcm_digital_capture;
357 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
365 * parse codec topology
368 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
373 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
378 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
380 struct cs_spec *spec = codec->spec;
381 struct auto_pin_cfg *cfg = &spec->autocfg;
382 hda_nid_t pin = cfg->inputs[idx].pin;
384 if (!is_jack_detectable(codec, pin))
386 val = snd_hda_codec_get_pincfg(codec, pin);
387 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
390 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
396 nid = codec->start_nid;
397 for (i = 0; i < codec->num_nodes; i++, nid++) {
399 type = get_wcaps_type(get_wcaps(codec, nid));
400 if (type != AC_WID_AUD_IN)
402 idx = snd_hda_get_conn_index(codec, nid, pin, false);
411 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
414 val = snd_hda_codec_get_pincfg(codec, nid);
415 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
418 static int parse_output(struct hda_codec *codec)
420 struct cs_spec *spec = codec->spec;
421 struct auto_pin_cfg *cfg = &spec->autocfg;
425 for (i = 0; i < cfg->line_outs; i++) {
426 dac = get_dac(codec, cfg->line_out_pins[i]);
429 spec->dac_nid[i] = dac;
431 spec->multiout.num_dacs = i;
432 spec->multiout.dac_nids = spec->dac_nid;
433 spec->multiout.max_channels = i * 2;
435 /* add HP and speakers */
437 for (i = 0; i < cfg->hp_outs; i++) {
438 dac = get_dac(codec, cfg->hp_pins[i]);
442 spec->multiout.hp_nid = dac;
444 spec->multiout.extra_out_nid[extra_nids++] = dac;
446 for (i = 0; i < cfg->speaker_outs; i++) {
447 dac = get_dac(codec, cfg->speaker_pins[i]);
450 spec->multiout.extra_out_nid[extra_nids++] = dac;
453 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
454 cfg->speaker_outs = cfg->line_outs;
455 memcpy(cfg->speaker_pins, cfg->line_out_pins,
456 sizeof(cfg->speaker_pins));
463 static int parse_input(struct hda_codec *codec)
465 struct cs_spec *spec = codec->spec;
466 struct auto_pin_cfg *cfg = &spec->autocfg;
469 for (i = 0; i < cfg->num_inputs; i++) {
470 hda_nid_t pin = cfg->inputs[i].pin;
471 spec->input_idx[spec->num_inputs] = i;
472 spec->capsrc_idx[i] = spec->num_inputs++;
474 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
476 if (!spec->num_inputs)
479 /* check whether the automatic mic switch is available */
480 if (spec->num_inputs == 2 &&
481 cfg->inputs[0].type == AUTO_PIN_MIC &&
482 cfg->inputs[1].type == AUTO_PIN_MIC) {
483 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
484 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
485 spec->mic_detect = 1;
486 spec->automic_idx = 0;
489 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
490 spec->mic_detect = 1;
491 spec->automic_idx = 1;
499 static int parse_digital_output(struct hda_codec *codec)
501 struct cs_spec *spec = codec->spec;
502 struct auto_pin_cfg *cfg = &spec->autocfg;
507 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
509 spec->multiout.dig_out_nid = nid;
510 spec->multiout.share_spdif = 1;
511 if (cfg->dig_outs > 1 &&
512 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
513 spec->slave_dig_outs[0] = nid;
514 codec->slave_dig_outs = spec->slave_dig_outs;
519 static int parse_digital_input(struct hda_codec *codec)
521 struct cs_spec *spec = codec->spec;
522 struct auto_pin_cfg *cfg = &spec->autocfg;
526 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
531 * create mixer controls
534 static const char * const dir_sfx[2] = { "Playback", "Capture" };
536 static int add_mute(struct hda_codec *codec, const char *name, int index,
537 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
540 struct snd_kcontrol_new knew =
541 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
542 knew.private_value = pval;
543 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
544 *kctlp = snd_ctl_new1(&knew, codec);
545 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
546 return snd_hda_ctl_add(codec, 0, *kctlp);
549 static int add_volume(struct hda_codec *codec, const char *name,
550 int index, unsigned int pval, int dir,
551 struct snd_kcontrol **kctlp)
554 struct snd_kcontrol_new knew =
555 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
556 knew.private_value = pval;
557 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
558 *kctlp = snd_ctl_new1(&knew, codec);
559 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
560 return snd_hda_ctl_add(codec, 0, *kctlp);
563 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
567 /* set the upper-limit for mixer amp to 0dB */
568 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
569 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
570 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
571 << AC_AMPCAP_NUM_STEPS_SHIFT;
572 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
575 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
577 struct cs_spec *spec = codec->spec;
582 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
583 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
587 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
589 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
590 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
596 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
597 int num_ctls, int type)
599 struct cs_spec *spec = codec->spec;
602 struct snd_kcontrol *kctl;
603 static const char * const speakers[] = {
604 "Front Speaker", "Surround Speaker", "Bass Speaker"
606 static const char * const line_outs[] = {
607 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
610 fix_volume_caps(codec, dac);
611 if (!spec->vmaster_sw) {
612 err = add_vmaster(codec, dac);
619 case AUTO_PIN_HP_OUT:
623 case AUTO_PIN_SPEAKER_OUT:
625 name = speakers[idx];
631 name = line_outs[idx];
637 err = add_mute(codec, name, index,
638 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
641 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
645 err = add_volume(codec, name, index,
646 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
649 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
656 static int build_output(struct hda_codec *codec)
658 struct cs_spec *spec = codec->spec;
659 struct auto_pin_cfg *cfg = &spec->autocfg;
662 for (i = 0; i < cfg->line_outs; i++) {
663 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
664 i, cfg->line_outs, cfg->line_out_type);
668 for (i = 0; i < cfg->hp_outs; i++) {
669 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
670 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
674 for (i = 0; i < cfg->speaker_outs; i++) {
675 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
676 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
686 static const struct snd_kcontrol_new cs_capture_ctls[] = {
687 HDA_BIND_SW("Capture Switch", 0),
688 HDA_BIND_VOL("Capture Volume", 0),
691 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
694 struct cs_spec *spec = codec->spec;
696 if (spec->cur_input == idx && !force)
698 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
699 /* stream is running, let's swap the current ADC */
700 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
701 spec->cur_adc = spec->adc_nid[idx];
702 snd_hda_codec_setup_stream(codec, spec->cur_adc,
703 spec->cur_adc_stream_tag, 0,
704 spec->cur_adc_format);
706 spec->cur_input = idx;
707 cs_update_input_select(codec);
711 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
712 struct snd_ctl_elem_info *uinfo)
714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
715 struct cs_spec *spec = codec->spec;
716 struct auto_pin_cfg *cfg = &spec->autocfg;
719 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
721 uinfo->value.enumerated.items = spec->num_inputs;
722 if (uinfo->value.enumerated.item >= spec->num_inputs)
723 uinfo->value.enumerated.item = spec->num_inputs - 1;
724 idx = spec->input_idx[uinfo->value.enumerated.item];
725 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
726 uinfo->value.enumerated.name,
727 sizeof(uinfo->value.enumerated.name), NULL);
731 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
732 struct snd_ctl_elem_value *ucontrol)
734 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
735 struct cs_spec *spec = codec->spec;
736 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
740 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
741 struct snd_ctl_elem_value *ucontrol)
743 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
744 struct cs_spec *spec = codec->spec;
745 unsigned int idx = ucontrol->value.enumerated.item[0];
747 if (idx >= spec->num_inputs)
749 idx = spec->input_idx[idx];
750 return change_cur_input(codec, idx, 0);
753 static const struct snd_kcontrol_new cs_capture_source = {
754 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
755 .name = "Capture Source",
756 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
757 .info = cs_capture_source_info,
758 .get = cs_capture_source_get,
759 .put = cs_capture_source_put,
762 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
763 struct hda_ctl_ops *ops)
765 struct cs_spec *spec = codec->spec;
766 struct hda_bind_ctls *bind;
769 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
775 for (i = 0; i < AUTO_PIN_LAST; i++) {
776 if (!spec->adc_nid[i])
779 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
780 spec->adc_idx[i], HDA_INPUT);
785 /* add a (input-boost) volume control to the given input pin */
786 static int add_input_volume_control(struct hda_codec *codec,
787 struct auto_pin_cfg *cfg,
790 hda_nid_t pin = cfg->inputs[item].pin;
793 struct snd_kcontrol *kctl;
795 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
797 caps = query_amp_caps(codec, pin, HDA_INPUT);
798 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
801 label = hda_get_autocfg_input_label(codec, cfg, item);
802 return add_volume(codec, label, 0,
803 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
806 static int build_input(struct hda_codec *codec)
808 struct cs_spec *spec = codec->spec;
811 if (!spec->num_inputs)
814 /* make bind-capture */
815 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
816 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
817 for (i = 0; i < 2; i++) {
818 struct snd_kcontrol *kctl;
820 if (!spec->capture_bind[i])
822 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
825 kctl->private_value = (long)spec->capture_bind[i];
826 err = snd_hda_ctl_add(codec, 0, kctl);
829 for (n = 0; n < AUTO_PIN_LAST; n++) {
830 if (!spec->adc_nid[n])
832 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
838 if (spec->num_inputs > 1 && !spec->mic_detect) {
839 err = snd_hda_ctl_add(codec, 0,
840 snd_ctl_new1(&cs_capture_source, codec));
845 for (i = 0; i < spec->num_inputs; i++) {
846 err = add_input_volume_control(codec, &spec->autocfg, i);
857 static int build_digital_output(struct hda_codec *codec)
859 struct cs_spec *spec = codec->spec;
862 if (!spec->multiout.dig_out_nid)
865 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
866 spec->multiout.dig_out_nid);
869 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
875 static int build_digital_input(struct hda_codec *codec)
877 struct cs_spec *spec = codec->spec;
879 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
884 * auto-mute and auto-mic switching
885 * CS421x auto-output redirecting
889 static void cs_automute(struct hda_codec *codec)
891 struct cs_spec *spec = codec->spec;
892 struct auto_pin_cfg *cfg = &spec->autocfg;
893 unsigned int hp_present;
894 unsigned int spdif_present;
900 nid = cfg->dig_out_pins[0];
901 if (is_jack_detectable(codec, nid)) {
903 TODO: SPDIF output redirect when SENSE_B is enabled.
904 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
907 if (snd_hda_jack_detect(codec, nid)
908 /* && spec->sense_b */)
914 for (i = 0; i < cfg->hp_outs; i++) {
915 nid = cfg->hp_pins[i];
916 if (!is_jack_detectable(codec, nid))
918 hp_present = snd_hda_jack_detect(codec, nid);
923 /* mute speakers if spdif or hp jack is plugged in */
924 for (i = 0; i < cfg->speaker_outs; i++) {
925 int pin_ctl = hp_present ? 0 : PIN_OUT;
926 /* detect on spdif is specific to CS421x */
927 if (spdif_present && (spec->vendor_nid == CS421X_VENDOR_NID))
930 nid = cfg->speaker_pins[i];
931 snd_hda_codec_write(codec, nid, 0,
932 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_ctl);
934 if (spec->gpio_eapd_hp) {
935 unsigned int gpio = hp_present ?
936 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
937 snd_hda_codec_write(codec, 0x01, 0,
938 AC_VERB_SET_GPIO_DATA, gpio);
941 /* specific to CS421x */
942 if (spec->vendor_nid == CS421X_VENDOR_NID) {
943 /* mute HPs if spdif jack (SENSE_B) is present */
944 for (i = 0; i < cfg->hp_outs; i++) {
945 nid = cfg->hp_pins[i];
946 snd_hda_codec_write(codec, nid, 0,
947 AC_VERB_SET_PIN_WIDGET_CONTROL,
948 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
951 /* SPDIF TX on/off */
953 nid = cfg->dig_out_pins[0];
954 snd_hda_codec_write(codec, nid, 0,
955 AC_VERB_SET_PIN_WIDGET_CONTROL,
956 spdif_present ? PIN_OUT : 0);
959 /* Update board GPIOs if neccessary ... */
964 * Auto-input redirect for CS421x
965 * Switch max 3 inputs of a single ADC (nid 3)
968 static void cs_automic(struct hda_codec *codec)
970 struct cs_spec *spec = codec->spec;
971 struct auto_pin_cfg *cfg = &spec->autocfg;
973 unsigned int present;
975 nid = cfg->inputs[spec->automic_idx].pin;
976 present = snd_hda_jack_detect(codec, nid);
978 /* specific to CS421x, single ADC */
979 if (spec->vendor_nid == CS421X_VENDOR_NID) {
981 spec->last_input = spec->cur_input;
982 spec->cur_input = spec->automic_idx;
984 spec->cur_input = spec->last_input;
986 cs_update_input_select(codec);
989 change_cur_input(codec, spec->automic_idx, 0);
991 change_cur_input(codec, !spec->automic_idx, 0);
998 static void init_output(struct hda_codec *codec)
1000 struct cs_spec *spec = codec->spec;
1001 struct auto_pin_cfg *cfg = &spec->autocfg;
1005 for (i = 0; i < spec->multiout.num_dacs; i++)
1006 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1007 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1008 if (spec->multiout.hp_nid)
1009 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1010 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1011 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1012 if (!spec->multiout.extra_out_nid[i])
1014 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1015 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1018 /* set appropriate pin controls */
1019 for (i = 0; i < cfg->line_outs; i++)
1020 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1021 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1023 for (i = 0; i < cfg->hp_outs; i++) {
1024 hda_nid_t nid = cfg->hp_pins[i];
1025 snd_hda_codec_write(codec, nid, 0,
1026 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1027 if (!cfg->speaker_outs)
1029 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1030 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
1031 spec->hp_detect = 1;
1036 for (i = 0; i < cfg->speaker_outs; i++)
1037 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1038 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1040 /* SPDIF is enabled on presence detect for CS421x */
1041 if (spec->hp_detect || spec->spdif_detect)
1045 static void init_input(struct hda_codec *codec)
1047 struct cs_spec *spec = codec->spec;
1048 struct auto_pin_cfg *cfg = &spec->autocfg;
1052 for (i = 0; i < cfg->num_inputs; i++) {
1054 hda_nid_t pin = cfg->inputs[i].pin;
1055 if (!spec->adc_nid[i])
1057 /* set appropriate pin control and mute first */
1059 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1060 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1061 caps >>= AC_PINCAP_VREF_SHIFT;
1062 if (caps & AC_PINCAP_VREF_80)
1065 snd_hda_codec_write(codec, pin, 0,
1066 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1067 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1068 AC_VERB_SET_AMP_GAIN_MUTE,
1069 AMP_IN_MUTE(spec->adc_idx[i]));
1070 if (spec->mic_detect && spec->automic_idx == i)
1071 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
1073 /* specific to CS421x */
1074 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1075 if (spec->mic_detect)
1078 spec->cur_adc = spec->adc_nid[spec->cur_input];
1079 cs_update_input_select(codec);
1082 change_cur_input(codec, spec->cur_input, 1);
1083 if (spec->mic_detect)
1086 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1087 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1088 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1089 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1090 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1091 * No effect if SPDIF_OUT2 is
1092 * selected in IDX_SPDIF_CTL.
1094 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1098 static const struct hda_verb cs_coef_init_verbs[] = {
1099 {0x11, AC_VERB_SET_PROC_STATE, 1},
1100 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1101 {0x11, AC_VERB_SET_PROC_COEF,
1102 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1103 | 0x0040 /* Mute DACs on FIFO error */
1104 | 0x1000 /* Enable DACs High Pass Filter */
1105 | 0x0400 /* Disable Coefficient Auto increment */
1108 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1109 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1114 /* Errata: CS4207 rev C0/C1/C2 Silicon
1116 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1118 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1119 * may be excessive (up to an additional 200 μA), which is most easily
1120 * observed while the part is being held in reset (RESET# active low).
1122 * Root Cause: At initial powerup of the device, the logic that drives
1123 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1125 * Certain random patterns will cause a steady leakage current in those
1126 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1128 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1129 * blocks, which will alleviate the issue.
1132 static const struct hda_verb cs_errata_init_verbs[] = {
1133 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1134 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1136 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1137 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1138 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1139 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1140 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1141 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1143 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1144 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1146 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1147 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1148 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1149 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1150 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1151 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1152 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1154 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1155 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1156 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1157 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1164 static void init_digital(struct hda_codec *codec)
1168 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1169 coef |= 0x0008; /* Replace with mute on error */
1170 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1171 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1172 * SPDIF_OUT2 is shared with GPIO1 and
1175 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1178 static int cs_init(struct hda_codec *codec)
1180 struct cs_spec *spec = codec->spec;
1182 /* init_verb sequence for C0/C1/C2 errata*/
1183 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1185 snd_hda_sequence_write(codec, cs_coef_init_verbs);
1187 if (spec->gpio_mask) {
1188 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1190 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1192 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1198 init_digital(codec);
1199 snd_hda_jack_report_sync(codec);
1204 static int cs_build_controls(struct hda_codec *codec)
1206 struct cs_spec *spec = codec->spec;
1209 err = build_output(codec);
1212 err = build_input(codec);
1215 err = build_digital_output(codec);
1218 err = build_digital_input(codec);
1221 err = cs_init(codec);
1225 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1232 static void cs_free(struct hda_codec *codec)
1234 struct cs_spec *spec = codec->spec;
1235 kfree(spec->capture_bind[0]);
1236 kfree(spec->capture_bind[1]);
1240 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1242 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1250 snd_hda_jack_report_sync(codec);
1253 static const struct hda_codec_ops cs_patch_ops = {
1254 .build_controls = cs_build_controls,
1255 .build_pcms = cs_build_pcms,
1258 .unsol_event = cs_unsol_event,
1261 static int cs_parse_auto_config(struct hda_codec *codec)
1263 struct cs_spec *spec = codec->spec;
1266 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1270 err = parse_output(codec);
1273 err = parse_input(codec);
1276 err = parse_digital_output(codec);
1279 err = parse_digital_input(codec);
1285 static const char * const cs420x_models[CS420X_MODELS] = {
1286 [CS420X_MBP53] = "mbp53",
1287 [CS420X_MBP55] = "mbp55",
1288 [CS420X_IMAC27] = "imac27",
1289 [CS420X_APPLE] = "apple",
1290 [CS420X_AUTO] = "auto",
1294 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1295 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1296 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1297 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1298 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1299 /* this conflicts with too many other models */
1300 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1304 static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
1305 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
1314 static const struct cs_pincfg mbp53_pincfgs[] = {
1315 { 0x09, 0x012b4050 },
1316 { 0x0a, 0x90100141 },
1317 { 0x0b, 0x90100140 },
1318 { 0x0c, 0x018b3020 },
1319 { 0x0d, 0x90a00110 },
1320 { 0x0e, 0x400000f0 },
1321 { 0x0f, 0x01cbe030 },
1322 { 0x10, 0x014be060 },
1323 { 0x12, 0x400000f0 },
1324 { 0x15, 0x400000f0 },
1328 static const struct cs_pincfg mbp55_pincfgs[] = {
1329 { 0x09, 0x012b4030 },
1330 { 0x0a, 0x90100121 },
1331 { 0x0b, 0x90100120 },
1332 { 0x0c, 0x400000f0 },
1333 { 0x0d, 0x90a00110 },
1334 { 0x0e, 0x400000f0 },
1335 { 0x0f, 0x400000f0 },
1336 { 0x10, 0x014be040 },
1337 { 0x12, 0x400000f0 },
1338 { 0x15, 0x400000f0 },
1342 static const struct cs_pincfg imac27_pincfgs[] = {
1343 { 0x09, 0x012b4050 },
1344 { 0x0a, 0x90100140 },
1345 { 0x0b, 0x90100142 },
1346 { 0x0c, 0x018b3020 },
1347 { 0x0d, 0x90a00110 },
1348 { 0x0e, 0x400000f0 },
1349 { 0x0f, 0x01cbe030 },
1350 { 0x10, 0x014be060 },
1351 { 0x12, 0x01ab9070 },
1352 { 0x15, 0x400000f0 },
1356 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1357 [CS420X_MBP53] = mbp53_pincfgs,
1358 [CS420X_MBP55] = mbp55_pincfgs,
1359 [CS420X_IMAC27] = imac27_pincfgs,
1362 static void fix_pincfg(struct hda_codec *codec, int model,
1363 const struct cs_pincfg **pin_configs)
1365 const struct cs_pincfg *cfg = pin_configs[model];
1368 for (; cfg->nid; cfg++)
1369 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1372 static int patch_cs420x(struct hda_codec *codec)
1374 struct cs_spec *spec;
1377 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1382 spec->vendor_nid = CS420X_VENDOR_NID;
1384 spec->board_config =
1385 snd_hda_check_board_config(codec, CS420X_MODELS,
1386 cs420x_models, cs420x_cfg_tbl);
1387 if (spec->board_config < 0)
1388 spec->board_config =
1389 snd_hda_check_board_codec_sid_config(codec,
1390 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
1391 if (spec->board_config >= 0)
1392 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1394 switch (spec->board_config) {
1399 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1400 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1401 spec->gpio_mask = spec->gpio_dir =
1402 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1406 err = cs_parse_auto_config(codec);
1410 codec->patch_ops = cs_patch_ops;
1421 * Cirrus Logic CS4210
1423 * 1 DAC => HP(sense) / Speakers,
1424 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1425 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1428 /* CS4210 board names */
1429 static const char *cs421x_models[CS421X_MODELS] = {
1430 [CS421X_CDB4210] = "cdb4210",
1433 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1434 /* Test Intel board + CDB2410 */
1435 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1439 /* CS4210 board pinconfigs */
1440 /* Default CS4210 (CDB4210)*/
1441 static const struct cs_pincfg cdb4210_pincfgs[] = {
1442 { 0x05, 0x0321401f },
1443 { 0x06, 0x90170010 },
1444 { 0x07, 0x03813031 },
1445 { 0x08, 0xb7a70037 },
1446 { 0x09, 0xb7a6003e },
1447 { 0x0a, 0x034510f0 },
1451 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1452 [CS421X_CDB4210] = cdb4210_pincfgs,
1455 static const struct hda_verb cs421x_coef_init_verbs[] = {
1456 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1457 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1459 Disable Coefficient Index Auto-Increment(DAI)=1,
1462 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1464 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1465 /* ADC SZCMode = Digital Soft Ramp */
1466 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1468 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1469 {0x0B, AC_VERB_SET_PROC_COEF,
1470 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1471 | 0x0004 /* Mute DAC on FIFO error */
1472 | 0x0008 /* Enable DAC High Pass Filter */
1477 /* Errata: CS4210 rev A1 Silicon
1479 * http://www.cirrus.com/en/pubs/errata/
1482 * 1. Performance degredation is present in the ADC.
1483 * 2. Speaker output is not completely muted upon HP detect.
1484 * 3. Noise is present when clipping occurs on the amplified
1488 * The following verb sequence written to the registers during
1489 * initialization will correct the issues listed above.
1492 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1493 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1495 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1496 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1498 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1499 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1501 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1502 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1504 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1505 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1507 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1508 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1513 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1514 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1516 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1517 struct snd_ctl_elem_info *uinfo)
1519 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1521 uinfo->value.integer.min = 0;
1522 uinfo->value.integer.max = 3;
1526 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_value *ucontrol)
1529 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1531 ucontrol->value.integer.value[0] =
1532 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1536 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1537 struct snd_ctl_elem_value *ucontrol)
1539 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1541 unsigned int vol = ucontrol->value.integer.value[0];
1543 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1544 unsigned int original_coef = coef;
1547 coef |= (vol & 0x0003);
1548 if (original_coef == coef)
1551 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1556 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1559 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1560 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1561 .name = "Speaker Boost Playback Volume",
1562 .info = cs421x_boost_vol_info,
1563 .get = cs421x_boost_vol_get,
1564 .put = cs421x_boost_vol_put,
1565 .tlv = { .p = cs421x_speaker_boost_db_scale },
1568 static void cs421x_pinmux_init(struct hda_codec *codec)
1570 struct cs_spec *spec = codec->spec;
1571 unsigned int def_conf, coef;
1573 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1574 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1576 if (spec->gpio_mask)
1577 coef |= 0x0008; /* B1,B2 are GPIOs */
1582 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1586 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1588 if ((spec->gpio_mask || spec->sense_b) &&
1589 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1592 GPIO or SENSE_B forced - disconnect the DMIC pin.
1594 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1595 def_conf &= ~AC_DEFCFG_PORT_CONN;
1596 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1597 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1601 static void init_cs421x_digital(struct hda_codec *codec)
1603 struct cs_spec *spec = codec->spec;
1604 struct auto_pin_cfg *cfg = &spec->autocfg;
1608 for (i = 0; i < cfg->dig_outs; i++) {
1609 hda_nid_t nid = cfg->dig_out_pins[i];
1610 if (!cfg->speaker_outs)
1612 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1613 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
1614 spec->spdif_detect = 1;
1619 static int cs421x_init(struct hda_codec *codec)
1621 struct cs_spec *spec = codec->spec;
1623 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1624 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1626 cs421x_pinmux_init(codec);
1628 if (spec->gpio_mask) {
1629 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1631 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1633 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1639 init_cs421x_digital(codec);
1640 snd_hda_jack_report_sync(codec);
1646 * CS4210 Input MUX (1 ADC)
1648 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1649 struct snd_ctl_elem_info *uinfo)
1651 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1652 struct cs_spec *spec = codec->spec;
1654 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1657 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1658 struct snd_ctl_elem_value *ucontrol)
1660 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1661 struct cs_spec *spec = codec->spec;
1663 ucontrol->value.enumerated.item[0] = spec->cur_input;
1667 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1668 struct snd_ctl_elem_value *ucontrol)
1670 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1671 struct cs_spec *spec = codec->spec;
1673 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1674 spec->adc_nid[0], &spec->cur_input);
1678 static struct snd_kcontrol_new cs421x_capture_source = {
1680 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1681 .name = "Capture Source",
1682 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1683 .info = cs421x_mux_enum_info,
1684 .get = cs421x_mux_enum_get,
1685 .put = cs421x_mux_enum_put,
1688 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1690 struct cs_spec *spec = codec->spec;
1691 struct auto_pin_cfg *cfg = &spec->autocfg;
1692 const struct hda_input_mux *imux = &spec->input_mux;
1693 hda_nid_t pin = cfg->inputs[item].pin;
1694 struct snd_kcontrol *kctl;
1697 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1700 caps = query_amp_caps(codec, pin, HDA_INPUT);
1701 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1705 return add_volume(codec, imux->items[item].label, 0,
1706 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1709 /* add a (input-boost) volume control to the given input pin */
1710 static int build_cs421x_input(struct hda_codec *codec)
1712 struct cs_spec *spec = codec->spec;
1713 struct auto_pin_cfg *cfg = &spec->autocfg;
1714 struct hda_input_mux *imux = &spec->input_mux;
1715 int i, err, type_idx;
1718 if (!spec->num_inputs)
1721 /* make bind-capture */
1722 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1723 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1724 for (i = 0; i < 2; i++) {
1725 struct snd_kcontrol *kctl;
1727 if (!spec->capture_bind[i])
1729 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1732 kctl->private_value = (long)spec->capture_bind[i];
1733 err = snd_hda_ctl_add(codec, 0, kctl);
1736 for (n = 0; n < AUTO_PIN_LAST; n++) {
1737 if (!spec->adc_nid[n])
1739 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1745 /* Add Input MUX Items + Capture Volume/Switch */
1746 for (i = 0; i < spec->num_inputs; i++) {
1747 label = hda_get_autocfg_input_label(codec, cfg, i);
1748 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1750 err = cs421x_add_input_volume_control(codec, i);
1756 Add 'Capture Source' Switch if
1757 * 2 inputs and no mic detec
1760 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1761 (spec->num_inputs == 3)) {
1763 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1764 snd_ctl_new1(&cs421x_capture_source, codec));
1772 /* Single DAC (Mute/Gain) */
1773 static int build_cs421x_output(struct hda_codec *codec)
1775 hda_nid_t dac = CS4210_DAC_NID;
1776 struct cs_spec *spec = codec->spec;
1777 struct auto_pin_cfg *cfg = &spec->autocfg;
1778 struct snd_kcontrol *kctl;
1780 char *name = "Master";
1782 fix_volume_caps(codec, dac);
1784 err = add_mute(codec, name, 0,
1785 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1789 err = add_volume(codec, name, 0,
1790 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1794 if (cfg->speaker_outs) {
1795 err = snd_hda_ctl_add(codec, 0,
1796 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1803 static int cs421x_build_controls(struct hda_codec *codec)
1805 struct cs_spec *spec = codec->spec;
1808 err = build_cs421x_output(codec);
1811 err = build_cs421x_input(codec);
1814 err = build_digital_output(codec);
1817 err = cs421x_init(codec);
1821 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1828 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1830 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1840 snd_hda_jack_report_sync(codec);
1843 static int parse_cs421x_input(struct hda_codec *codec)
1845 struct cs_spec *spec = codec->spec;
1846 struct auto_pin_cfg *cfg = &spec->autocfg;
1849 for (i = 0; i < cfg->num_inputs; i++) {
1850 hda_nid_t pin = cfg->inputs[i].pin;
1851 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1852 spec->cur_input = spec->last_input = i;
1855 /* check whether the automatic mic switch is available */
1856 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1857 spec->mic_detect = 1;
1858 spec->automic_idx = i;
1864 static int cs421x_parse_auto_config(struct hda_codec *codec)
1866 struct cs_spec *spec = codec->spec;
1869 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1872 err = parse_output(codec);
1875 err = parse_cs421x_input(codec);
1878 err = parse_digital_output(codec);
1886 Manage PDREF, when transitioning to D3hot
1887 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1889 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1893 snd_hda_shutup_pins(codec);
1895 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1896 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1897 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1898 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1900 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1901 coef |= 0x0004; /* PDREF */
1902 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1908 static struct hda_codec_ops cs4210_patch_ops = {
1909 .build_controls = cs421x_build_controls,
1910 .build_pcms = cs_build_pcms,
1911 .init = cs421x_init,
1913 .unsol_event = cs421x_unsol_event,
1915 .suspend = cs421x_suspend,
1919 static int patch_cs421x(struct hda_codec *codec)
1921 struct cs_spec *spec;
1924 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1929 spec->vendor_nid = CS421X_VENDOR_NID;
1931 spec->board_config =
1932 snd_hda_check_board_config(codec, CS421X_MODELS,
1933 cs421x_models, cs421x_cfg_tbl);
1934 if (spec->board_config >= 0)
1935 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1937 Setup GPIO/SENSE for each board (if used)
1939 switch (spec->board_config) {
1940 case CS421X_CDB4210:
1941 snd_printd("CS4210 board: %s\n",
1942 cs421x_models[spec->board_config]);
1943 /* spec->gpio_mask = 3;
1945 spec->gpio_data = 3;
1953 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1954 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1957 cs421x_pinmux_init(codec);
1959 err = cs421x_parse_auto_config(codec);
1963 codec->patch_ops = cs4210_patch_ops;
1977 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
1978 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1979 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1980 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
1984 MODULE_ALIAS("snd-hda-codec-id:10134206");
1985 MODULE_ALIAS("snd-hda-codec-id:10134207");
1986 MODULE_ALIAS("snd-hda-codec-id:10134210");
1988 MODULE_LICENSE("GPL");
1989 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1991 static struct hda_codec_preset_list cirrus_list = {
1992 .preset = snd_hda_preset_cirrus,
1993 .owner = THIS_MODULE,
1996 static int __init patch_cirrus_init(void)
1998 return snd_hda_add_codec_preset(&cirrus_list);
2001 static void __exit patch_cirrus_exit(void)
2003 snd_hda_delete_codec_preset(&cirrus_list);
2006 module_init(patch_cirrus_init)
2007 module_exit(patch_cirrus_exit)