2 * HD audio interface patch for Cirrus Logic CS420x chip
4 * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
6 * This driver is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This driver is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/pci.h>
25 #include <linux/module.h>
26 #include <sound/core.h>
27 #include "hda_codec.h"
28 #include "hda_local.h"
29 #include <sound/tlv.h>
36 struct auto_pin_cfg autocfg;
37 struct hda_multi_out multiout;
38 struct snd_kcontrol *vmaster_sw;
39 struct snd_kcontrol *vmaster_vol;
41 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
42 hda_nid_t slave_dig_outs[2];
44 unsigned int input_idx[AUTO_PIN_LAST];
45 unsigned int capsrc_idx[AUTO_PIN_LAST];
46 hda_nid_t adc_nid[AUTO_PIN_LAST];
47 unsigned int adc_idx[AUTO_PIN_LAST];
48 unsigned int num_inputs;
49 unsigned int cur_input;
50 unsigned int automic_idx;
52 unsigned int cur_adc_stream_tag;
53 unsigned int cur_adc_format;
56 const struct hda_bind_ctls *capture_bind[2];
58 unsigned int gpio_mask;
59 unsigned int gpio_dir;
60 unsigned int gpio_data;
61 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
62 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
64 struct hda_pcm pcm_rec[2]; /* PCM information */
66 unsigned int hp_detect:1;
67 unsigned int mic_detect:1;
69 unsigned int spdif_detect:1;
70 unsigned int sense_b:1;
72 struct hda_input_mux input_mux;
73 unsigned int last_input;
76 /* available models with CS420x */
92 /* Vendor-specific processing widget */
93 #define CS420X_VENDOR_NID 0x11
94 #define CS_DIG_OUT1_PIN_NID 0x10
95 #define CS_DIG_OUT2_PIN_NID 0x15
96 #define CS_DMIC1_PIN_NID 0x12
97 #define CS_DMIC2_PIN_NID 0x0e
100 #define IDX_SPDIF_STAT 0x0000
101 #define IDX_SPDIF_CTL 0x0001
102 #define IDX_ADC_CFG 0x0002
103 /* SZC bitmask, 4 modes below:
105 * 1 = digital immediate, analog zero-cross
106 * 2 = digtail & analog soft-ramp
107 * 3 = digital soft-ramp, analog zero-cross
109 #define CS_COEF_ADC_SZC_MASK (3 << 0)
110 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
111 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
112 /* PGA mode: 0 = differential, 1 = signle-ended */
113 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
114 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
115 #define IDX_DAC_CFG 0x0003
116 /* SZC bitmask, 4 modes below:
120 * 3 = soft-ramp on zero-cross
122 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
123 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
124 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
126 #define IDX_BEEP_CFG 0x0004
127 /* 0x0008 - test reg key */
128 /* 0x0009 - 0x0014 -> 12 test regs */
129 /* 0x0015 - visibility reg */
132 * Cirrus Logic CS4210
134 * 1 DAC => HP(sense) / Speakers,
135 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
136 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
138 #define CS4210_DAC_NID 0x02
139 #define CS4210_ADC_NID 0x03
140 #define CS421X_VENDOR_NID 0x0B
141 #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
142 #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
144 #define CS421X_IDX_DEV_CFG 0x01
145 #define CS421X_IDX_ADC_CFG 0x02
146 #define CS421X_IDX_DAC_CFG 0x03
147 #define CS421X_IDX_SPK_CTL 0x04
149 #define SPDIF_EVENT 0x04
151 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
153 struct cs_spec *spec = codec->spec;
154 snd_hda_codec_write(codec, spec->vendor_nid, 0,
155 AC_VERB_SET_COEF_INDEX, idx);
156 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
157 AC_VERB_GET_PROC_COEF, 0);
160 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
163 struct cs_spec *spec = codec->spec;
164 snd_hda_codec_write(codec, spec->vendor_nid, 0,
165 AC_VERB_SET_COEF_INDEX, idx);
166 snd_hda_codec_write(codec, spec->vendor_nid, 0,
167 AC_VERB_SET_PROC_COEF, coef);
177 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
178 struct hda_codec *codec,
179 struct snd_pcm_substream *substream)
181 struct cs_spec *spec = codec->spec;
182 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
186 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
187 struct hda_codec *codec,
188 unsigned int stream_tag,
190 struct snd_pcm_substream *substream)
192 struct cs_spec *spec = codec->spec;
193 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
194 stream_tag, format, substream);
197 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
198 struct hda_codec *codec,
199 struct snd_pcm_substream *substream)
201 struct cs_spec *spec = codec->spec;
202 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
208 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
209 struct hda_codec *codec,
210 struct snd_pcm_substream *substream)
212 struct cs_spec *spec = codec->spec;
213 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
216 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
217 struct hda_codec *codec,
218 struct snd_pcm_substream *substream)
220 struct cs_spec *spec = codec->spec;
221 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
224 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
225 struct hda_codec *codec,
226 unsigned int stream_tag,
228 struct snd_pcm_substream *substream)
230 struct cs_spec *spec = codec->spec;
231 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
235 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
236 struct hda_codec *codec,
237 struct snd_pcm_substream *substream)
239 struct cs_spec *spec = codec->spec;
240 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
243 static void cs_update_input_select(struct hda_codec *codec)
245 struct cs_spec *spec = codec->spec;
247 snd_hda_codec_write(codec, spec->cur_adc, 0,
248 AC_VERB_SET_CONNECT_SEL,
249 spec->adc_idx[spec->cur_input]);
255 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
256 struct hda_codec *codec,
257 unsigned int stream_tag,
259 struct snd_pcm_substream *substream)
261 struct cs_spec *spec = codec->spec;
262 spec->cur_adc = spec->adc_nid[spec->cur_input];
263 spec->cur_adc_stream_tag = stream_tag;
264 spec->cur_adc_format = format;
265 cs_update_input_select(codec);
266 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
270 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
271 struct hda_codec *codec,
272 struct snd_pcm_substream *substream)
274 struct cs_spec *spec = codec->spec;
275 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
282 static const struct hda_pcm_stream cs_pcm_analog_playback = {
287 .open = cs_playback_pcm_open,
288 .prepare = cs_playback_pcm_prepare,
289 .cleanup = cs_playback_pcm_cleanup
293 static const struct hda_pcm_stream cs_pcm_analog_capture = {
298 .prepare = cs_capture_pcm_prepare,
299 .cleanup = cs_capture_pcm_cleanup
303 static const struct hda_pcm_stream cs_pcm_digital_playback = {
308 .open = cs_dig_playback_pcm_open,
309 .close = cs_dig_playback_pcm_close,
310 .prepare = cs_dig_playback_pcm_prepare,
311 .cleanup = cs_dig_playback_pcm_cleanup
315 static const struct hda_pcm_stream cs_pcm_digital_capture = {
321 static int cs_build_pcms(struct hda_codec *codec)
323 struct cs_spec *spec = codec->spec;
324 struct hda_pcm *info = spec->pcm_rec;
326 codec->pcm_info = info;
329 info->name = "Cirrus Analog";
330 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
331 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
332 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
333 spec->multiout.max_channels;
334 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
335 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
336 spec->adc_nid[spec->cur_input];
339 if (!spec->multiout.dig_out_nid && !spec->dig_in)
343 info->name = "Cirrus Digital";
344 info->pcm_type = spec->autocfg.dig_out_type[0];
346 info->pcm_type = HDA_PCM_TYPE_SPDIF;
347 if (spec->multiout.dig_out_nid) {
348 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
349 cs_pcm_digital_playback;
350 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
351 spec->multiout.dig_out_nid;
354 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
355 cs_pcm_digital_capture;
356 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
364 * parse codec topology
367 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
372 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
377 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
379 struct cs_spec *spec = codec->spec;
380 struct auto_pin_cfg *cfg = &spec->autocfg;
381 hda_nid_t pin = cfg->inputs[idx].pin;
383 if (!is_jack_detectable(codec, pin))
385 val = snd_hda_codec_get_pincfg(codec, pin);
386 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
389 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
395 nid = codec->start_nid;
396 for (i = 0; i < codec->num_nodes; i++, nid++) {
398 type = get_wcaps_type(get_wcaps(codec, nid));
399 if (type != AC_WID_AUD_IN)
401 idx = snd_hda_get_conn_index(codec, nid, pin, false);
410 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
413 val = snd_hda_codec_get_pincfg(codec, nid);
414 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
417 static int parse_output(struct hda_codec *codec)
419 struct cs_spec *spec = codec->spec;
420 struct auto_pin_cfg *cfg = &spec->autocfg;
424 for (i = 0; i < cfg->line_outs; i++) {
425 dac = get_dac(codec, cfg->line_out_pins[i]);
428 spec->dac_nid[i] = dac;
430 spec->multiout.num_dacs = i;
431 spec->multiout.dac_nids = spec->dac_nid;
432 spec->multiout.max_channels = i * 2;
434 /* add HP and speakers */
436 for (i = 0; i < cfg->hp_outs; i++) {
437 dac = get_dac(codec, cfg->hp_pins[i]);
441 spec->multiout.hp_nid = dac;
443 spec->multiout.extra_out_nid[extra_nids++] = dac;
445 for (i = 0; i < cfg->speaker_outs; i++) {
446 dac = get_dac(codec, cfg->speaker_pins[i]);
449 spec->multiout.extra_out_nid[extra_nids++] = dac;
452 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
453 cfg->speaker_outs = cfg->line_outs;
454 memcpy(cfg->speaker_pins, cfg->line_out_pins,
455 sizeof(cfg->speaker_pins));
462 static int parse_input(struct hda_codec *codec)
464 struct cs_spec *spec = codec->spec;
465 struct auto_pin_cfg *cfg = &spec->autocfg;
468 for (i = 0; i < cfg->num_inputs; i++) {
469 hda_nid_t pin = cfg->inputs[i].pin;
470 spec->input_idx[spec->num_inputs] = i;
471 spec->capsrc_idx[i] = spec->num_inputs++;
473 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
475 if (!spec->num_inputs)
478 /* check whether the automatic mic switch is available */
479 if (spec->num_inputs == 2 &&
480 cfg->inputs[0].type == AUTO_PIN_MIC &&
481 cfg->inputs[1].type == AUTO_PIN_MIC) {
482 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
483 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
484 spec->mic_detect = 1;
485 spec->automic_idx = 0;
488 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
489 spec->mic_detect = 1;
490 spec->automic_idx = 1;
498 static int parse_digital_output(struct hda_codec *codec)
500 struct cs_spec *spec = codec->spec;
501 struct auto_pin_cfg *cfg = &spec->autocfg;
506 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
508 spec->multiout.dig_out_nid = nid;
509 spec->multiout.share_spdif = 1;
510 if (cfg->dig_outs > 1 &&
511 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
512 spec->slave_dig_outs[0] = nid;
513 codec->slave_dig_outs = spec->slave_dig_outs;
518 static int parse_digital_input(struct hda_codec *codec)
520 struct cs_spec *spec = codec->spec;
521 struct auto_pin_cfg *cfg = &spec->autocfg;
525 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
530 * create mixer controls
533 static const char * const dir_sfx[2] = { "Playback", "Capture" };
535 static int add_mute(struct hda_codec *codec, const char *name, int index,
536 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
539 struct snd_kcontrol_new knew =
540 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
541 knew.private_value = pval;
542 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
543 *kctlp = snd_ctl_new1(&knew, codec);
544 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
545 return snd_hda_ctl_add(codec, 0, *kctlp);
548 static int add_volume(struct hda_codec *codec, const char *name,
549 int index, unsigned int pval, int dir,
550 struct snd_kcontrol **kctlp)
553 struct snd_kcontrol_new knew =
554 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
555 knew.private_value = pval;
556 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
557 *kctlp = snd_ctl_new1(&knew, codec);
558 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
559 return snd_hda_ctl_add(codec, 0, *kctlp);
562 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
566 /* set the upper-limit for mixer amp to 0dB */
567 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
568 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
569 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
570 << AC_AMPCAP_NUM_STEPS_SHIFT;
571 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
574 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
576 struct cs_spec *spec = codec->spec;
581 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
582 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
586 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
588 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
589 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
595 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
596 int num_ctls, int type)
598 struct cs_spec *spec = codec->spec;
601 struct snd_kcontrol *kctl;
602 static const char * const speakers[] = {
603 "Front Speaker", "Surround Speaker", "Bass Speaker"
605 static const char * const line_outs[] = {
606 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
609 fix_volume_caps(codec, dac);
610 if (!spec->vmaster_sw) {
611 err = add_vmaster(codec, dac);
618 case AUTO_PIN_HP_OUT:
622 case AUTO_PIN_SPEAKER_OUT:
624 name = speakers[idx];
630 name = line_outs[idx];
636 err = add_mute(codec, name, index,
637 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
640 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
644 err = add_volume(codec, name, index,
645 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
648 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
655 static int build_output(struct hda_codec *codec)
657 struct cs_spec *spec = codec->spec;
658 struct auto_pin_cfg *cfg = &spec->autocfg;
661 for (i = 0; i < cfg->line_outs; i++) {
662 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
663 i, cfg->line_outs, cfg->line_out_type);
667 for (i = 0; i < cfg->hp_outs; i++) {
668 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
669 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
673 for (i = 0; i < cfg->speaker_outs; i++) {
674 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
675 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
685 static const struct snd_kcontrol_new cs_capture_ctls[] = {
686 HDA_BIND_SW("Capture Switch", 0),
687 HDA_BIND_VOL("Capture Volume", 0),
690 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
693 struct cs_spec *spec = codec->spec;
695 if (spec->cur_input == idx && !force)
697 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
698 /* stream is running, let's swap the current ADC */
699 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
700 spec->cur_adc = spec->adc_nid[idx];
701 snd_hda_codec_setup_stream(codec, spec->cur_adc,
702 spec->cur_adc_stream_tag, 0,
703 spec->cur_adc_format);
705 spec->cur_input = idx;
706 cs_update_input_select(codec);
710 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
711 struct snd_ctl_elem_info *uinfo)
713 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
714 struct cs_spec *spec = codec->spec;
715 struct auto_pin_cfg *cfg = &spec->autocfg;
718 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
720 uinfo->value.enumerated.items = spec->num_inputs;
721 if (uinfo->value.enumerated.item >= spec->num_inputs)
722 uinfo->value.enumerated.item = spec->num_inputs - 1;
723 idx = spec->input_idx[uinfo->value.enumerated.item];
724 strcpy(uinfo->value.enumerated.name,
725 hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
729 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
730 struct snd_ctl_elem_value *ucontrol)
732 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
733 struct cs_spec *spec = codec->spec;
734 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
738 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
739 struct snd_ctl_elem_value *ucontrol)
741 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
742 struct cs_spec *spec = codec->spec;
743 unsigned int idx = ucontrol->value.enumerated.item[0];
745 if (idx >= spec->num_inputs)
747 idx = spec->input_idx[idx];
748 return change_cur_input(codec, idx, 0);
751 static const struct snd_kcontrol_new cs_capture_source = {
752 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
753 .name = "Capture Source",
754 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
755 .info = cs_capture_source_info,
756 .get = cs_capture_source_get,
757 .put = cs_capture_source_put,
760 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
761 struct hda_ctl_ops *ops)
763 struct cs_spec *spec = codec->spec;
764 struct hda_bind_ctls *bind;
767 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
773 for (i = 0; i < AUTO_PIN_LAST; i++) {
774 if (!spec->adc_nid[i])
777 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
778 spec->adc_idx[i], HDA_INPUT);
783 /* add a (input-boost) volume control to the given input pin */
784 static int add_input_volume_control(struct hda_codec *codec,
785 struct auto_pin_cfg *cfg,
788 hda_nid_t pin = cfg->inputs[item].pin;
791 struct snd_kcontrol *kctl;
793 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
795 caps = query_amp_caps(codec, pin, HDA_INPUT);
796 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
799 label = hda_get_autocfg_input_label(codec, cfg, item);
800 return add_volume(codec, label, 0,
801 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
804 static int build_input(struct hda_codec *codec)
806 struct cs_spec *spec = codec->spec;
809 if (!spec->num_inputs)
812 /* make bind-capture */
813 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
814 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
815 for (i = 0; i < 2; i++) {
816 struct snd_kcontrol *kctl;
818 if (!spec->capture_bind[i])
820 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
823 kctl->private_value = (long)spec->capture_bind[i];
824 err = snd_hda_ctl_add(codec, 0, kctl);
827 for (n = 0; n < AUTO_PIN_LAST; n++) {
828 if (!spec->adc_nid[n])
830 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
836 if (spec->num_inputs > 1 && !spec->mic_detect) {
837 err = snd_hda_ctl_add(codec, 0,
838 snd_ctl_new1(&cs_capture_source, codec));
843 for (i = 0; i < spec->num_inputs; i++) {
844 err = add_input_volume_control(codec, &spec->autocfg, i);
855 static int build_digital_output(struct hda_codec *codec)
857 struct cs_spec *spec = codec->spec;
860 if (!spec->multiout.dig_out_nid)
863 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
864 spec->multiout.dig_out_nid);
867 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
873 static int build_digital_input(struct hda_codec *codec)
875 struct cs_spec *spec = codec->spec;
877 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
882 * auto-mute and auto-mic switching
883 * CS421x auto-output redirecting
887 static void cs_automute(struct hda_codec *codec)
889 struct cs_spec *spec = codec->spec;
890 struct auto_pin_cfg *cfg = &spec->autocfg;
891 unsigned int hp_present;
892 unsigned int spdif_present;
898 nid = cfg->dig_out_pins[0];
899 if (is_jack_detectable(codec, nid)) {
901 TODO: SPDIF output redirect when SENSE_B is enabled.
902 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
905 if (snd_hda_jack_detect(codec, nid)
906 /* && spec->sense_b */)
912 for (i = 0; i < cfg->hp_outs; i++) {
913 nid = cfg->hp_pins[i];
914 if (!is_jack_detectable(codec, nid))
916 hp_present = snd_hda_jack_detect(codec, nid);
921 /* mute speakers if spdif or hp jack is plugged in */
922 for (i = 0; i < cfg->speaker_outs; i++) {
923 int pin_ctl = hp_present ? 0 : PIN_OUT;
924 /* detect on spdif is specific to CS421x */
925 if (spdif_present && (spec->vendor_nid == CS421X_VENDOR_NID))
928 nid = cfg->speaker_pins[i];
929 snd_hda_codec_write(codec, nid, 0,
930 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_ctl);
932 if (spec->gpio_eapd_hp) {
933 unsigned int gpio = hp_present ?
934 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
935 snd_hda_codec_write(codec, 0x01, 0,
936 AC_VERB_SET_GPIO_DATA, gpio);
939 /* specific to CS421x */
940 if (spec->vendor_nid == CS421X_VENDOR_NID) {
941 /* mute HPs if spdif jack (SENSE_B) is present */
942 for (i = 0; i < cfg->hp_outs; i++) {
943 nid = cfg->hp_pins[i];
944 snd_hda_codec_write(codec, nid, 0,
945 AC_VERB_SET_PIN_WIDGET_CONTROL,
946 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
949 /* SPDIF TX on/off */
951 nid = cfg->dig_out_pins[0];
952 snd_hda_codec_write(codec, nid, 0,
953 AC_VERB_SET_PIN_WIDGET_CONTROL,
954 spdif_present ? PIN_OUT : 0);
957 /* Update board GPIOs if neccessary ... */
962 * Auto-input redirect for CS421x
963 * Switch max 3 inputs of a single ADC (nid 3)
966 static void cs_automic(struct hda_codec *codec)
968 struct cs_spec *spec = codec->spec;
969 struct auto_pin_cfg *cfg = &spec->autocfg;
971 unsigned int present;
973 nid = cfg->inputs[spec->automic_idx].pin;
974 present = snd_hda_jack_detect(codec, nid);
976 /* specific to CS421x, single ADC */
977 if (spec->vendor_nid == CS421X_VENDOR_NID) {
979 spec->last_input = spec->cur_input;
980 spec->cur_input = spec->automic_idx;
982 spec->cur_input = spec->last_input;
984 cs_update_input_select(codec);
987 change_cur_input(codec, spec->automic_idx, 0);
989 change_cur_input(codec, !spec->automic_idx, 0);
996 static void init_output(struct hda_codec *codec)
998 struct cs_spec *spec = codec->spec;
999 struct auto_pin_cfg *cfg = &spec->autocfg;
1003 for (i = 0; i < spec->multiout.num_dacs; i++)
1004 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1005 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1006 if (spec->multiout.hp_nid)
1007 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1008 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1009 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1010 if (!spec->multiout.extra_out_nid[i])
1012 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1013 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1016 /* set appropriate pin controls */
1017 for (i = 0; i < cfg->line_outs; i++)
1018 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1019 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1021 for (i = 0; i < cfg->hp_outs; i++) {
1022 hda_nid_t nid = cfg->hp_pins[i];
1023 snd_hda_codec_write(codec, nid, 0,
1024 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1025 if (!cfg->speaker_outs)
1027 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1028 snd_hda_codec_write(codec, nid, 0,
1029 AC_VERB_SET_UNSOLICITED_ENABLE,
1030 AC_USRSP_EN | HP_EVENT);
1031 spec->hp_detect = 1;
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_codec_write(codec, pin, 0,
1072 AC_VERB_SET_UNSOLICITED_ENABLE,
1073 AC_USRSP_EN | MIC_EVENT);
1075 /* specific to CS421x */
1076 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1077 if (spec->mic_detect)
1080 spec->cur_adc = spec->adc_nid[spec->cur_input];
1081 cs_update_input_select(codec);
1084 change_cur_input(codec, spec->cur_input, 1);
1085 if (spec->mic_detect)
1088 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1089 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1090 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1091 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1092 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1093 * No effect if SPDIF_OUT2 is
1094 * selected in IDX_SPDIF_CTL.
1096 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1100 static const struct hda_verb cs_coef_init_verbs[] = {
1101 {0x11, AC_VERB_SET_PROC_STATE, 1},
1102 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1103 {0x11, AC_VERB_SET_PROC_COEF,
1104 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1105 | 0x0040 /* Mute DACs on FIFO error */
1106 | 0x1000 /* Enable DACs High Pass Filter */
1107 | 0x0400 /* Disable Coefficient Auto increment */
1110 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1111 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1116 /* Errata: CS4207 rev C0/C1/C2 Silicon
1118 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1120 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1121 * may be excessive (up to an additional 200 μA), which is most easily
1122 * observed while the part is being held in reset (RESET# active low).
1124 * Root Cause: At initial powerup of the device, the logic that drives
1125 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1127 * Certain random patterns will cause a steady leakage current in those
1128 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1130 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1131 * blocks, which will alleviate the issue.
1134 static const struct hda_verb cs_errata_init_verbs[] = {
1135 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1136 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1138 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1139 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1140 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1141 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1142 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1143 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1145 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1146 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1148 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1149 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1150 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1151 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1152 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1153 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1154 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1156 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1157 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1158 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1159 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1166 static void init_digital(struct hda_codec *codec)
1170 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1171 coef |= 0x0008; /* Replace with mute on error */
1172 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1173 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1174 * SPDIF_OUT2 is shared with GPIO1 and
1177 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1180 static int cs_init(struct hda_codec *codec)
1182 struct cs_spec *spec = codec->spec;
1184 /* init_verb sequence for C0/C1/C2 errata*/
1185 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1187 snd_hda_sequence_write(codec, cs_coef_init_verbs);
1189 if (spec->gpio_mask) {
1190 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1192 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1194 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1200 init_digital(codec);
1204 static int cs_build_controls(struct hda_codec *codec)
1208 err = build_output(codec);
1211 err = build_input(codec);
1214 err = build_digital_output(codec);
1217 err = build_digital_input(codec);
1220 return cs_init(codec);
1223 static void cs_free(struct hda_codec *codec)
1225 struct cs_spec *spec = codec->spec;
1226 kfree(spec->capture_bind[0]);
1227 kfree(spec->capture_bind[1]);
1231 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1233 switch ((res >> 26) & 0x7f) {
1243 static const struct hda_codec_ops cs_patch_ops = {
1244 .build_controls = cs_build_controls,
1245 .build_pcms = cs_build_pcms,
1248 .unsol_event = cs_unsol_event,
1251 static int cs_parse_auto_config(struct hda_codec *codec)
1253 struct cs_spec *spec = codec->spec;
1256 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1260 err = parse_output(codec);
1263 err = parse_input(codec);
1266 err = parse_digital_output(codec);
1269 err = parse_digital_input(codec);
1275 static const char * const cs420x_models[CS420X_MODELS] = {
1276 [CS420X_MBP53] = "mbp53",
1277 [CS420X_MBP55] = "mbp55",
1278 [CS420X_IMAC27] = "imac27",
1279 [CS420X_APPLE] = "apple",
1280 [CS420X_AUTO] = "auto",
1284 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1285 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1286 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1287 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1288 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1289 /* this conflicts with too many other models */
1290 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1294 static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
1295 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
1304 static const struct cs_pincfg mbp53_pincfgs[] = {
1305 { 0x09, 0x012b4050 },
1306 { 0x0a, 0x90100141 },
1307 { 0x0b, 0x90100140 },
1308 { 0x0c, 0x018b3020 },
1309 { 0x0d, 0x90a00110 },
1310 { 0x0e, 0x400000f0 },
1311 { 0x0f, 0x01cbe030 },
1312 { 0x10, 0x014be060 },
1313 { 0x12, 0x400000f0 },
1314 { 0x15, 0x400000f0 },
1318 static const struct cs_pincfg mbp55_pincfgs[] = {
1319 { 0x09, 0x012b4030 },
1320 { 0x0a, 0x90100121 },
1321 { 0x0b, 0x90100120 },
1322 { 0x0c, 0x400000f0 },
1323 { 0x0d, 0x90a00110 },
1324 { 0x0e, 0x400000f0 },
1325 { 0x0f, 0x400000f0 },
1326 { 0x10, 0x014be040 },
1327 { 0x12, 0x400000f0 },
1328 { 0x15, 0x400000f0 },
1332 static const struct cs_pincfg imac27_pincfgs[] = {
1333 { 0x09, 0x012b4050 },
1334 { 0x0a, 0x90100140 },
1335 { 0x0b, 0x90100142 },
1336 { 0x0c, 0x018b3020 },
1337 { 0x0d, 0x90a00110 },
1338 { 0x0e, 0x400000f0 },
1339 { 0x0f, 0x01cbe030 },
1340 { 0x10, 0x014be060 },
1341 { 0x12, 0x01ab9070 },
1342 { 0x15, 0x400000f0 },
1346 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1347 [CS420X_MBP53] = mbp53_pincfgs,
1348 [CS420X_MBP55] = mbp55_pincfgs,
1349 [CS420X_IMAC27] = imac27_pincfgs,
1352 static void fix_pincfg(struct hda_codec *codec, int model,
1353 const struct cs_pincfg **pin_configs)
1355 const struct cs_pincfg *cfg = pin_configs[model];
1358 for (; cfg->nid; cfg++)
1359 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1362 static int patch_cs420x(struct hda_codec *codec)
1364 struct cs_spec *spec;
1367 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1372 spec->vendor_nid = CS420X_VENDOR_NID;
1374 spec->board_config =
1375 snd_hda_check_board_config(codec, CS420X_MODELS,
1376 cs420x_models, cs420x_cfg_tbl);
1377 if (spec->board_config < 0)
1378 spec->board_config =
1379 snd_hda_check_board_codec_sid_config(codec,
1380 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
1381 if (spec->board_config >= 0)
1382 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1384 switch (spec->board_config) {
1389 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1390 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1391 spec->gpio_mask = spec->gpio_dir =
1392 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1396 err = cs_parse_auto_config(codec);
1400 codec->patch_ops = cs_patch_ops;
1411 * Cirrus Logic CS4210
1413 * 1 DAC => HP(sense) / Speakers,
1414 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1415 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1418 /* CS4210 board names */
1419 static const char *cs421x_models[CS421X_MODELS] = {
1420 [CS421X_CDB4210] = "cdb4210",
1423 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1424 /* Test Intel board + CDB2410 */
1425 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1429 /* CS4210 board pinconfigs */
1430 /* Default CS4210 (CDB4210)*/
1431 static const struct cs_pincfg cdb4210_pincfgs[] = {
1432 { 0x05, 0x0321401f },
1433 { 0x06, 0x90170010 },
1434 { 0x07, 0x03813031 },
1435 { 0x08, 0xb7a70037 },
1436 { 0x09, 0xb7a6003e },
1437 { 0x0a, 0x034510f0 },
1441 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1442 [CS421X_CDB4210] = cdb4210_pincfgs,
1445 static const struct hda_verb cs421x_coef_init_verbs[] = {
1446 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1447 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1449 Disable Coefficient Index Auto-Increment(DAI)=1,
1452 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1454 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1455 /* ADC SZCMode = Digital Soft Ramp */
1456 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1458 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1459 {0x0B, AC_VERB_SET_PROC_COEF,
1460 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1461 | 0x0004 /* Mute DAC on FIFO error */
1462 | 0x0008 /* Enable DAC High Pass Filter */
1467 /* Errata: CS4210 rev A1 Silicon
1469 * http://www.cirrus.com/en/pubs/errata/
1472 * 1. Performance degredation is present in the ADC.
1473 * 2. Speaker output is not completely muted upon HP detect.
1474 * 3. Noise is present when clipping occurs on the amplified
1478 * The following verb sequence written to the registers during
1479 * initialization will correct the issues listed above.
1482 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1483 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1485 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1486 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1488 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1489 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1491 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1492 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1494 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1495 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1497 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1498 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1503 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1504 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1506 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1507 struct snd_ctl_elem_info *uinfo)
1509 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1511 uinfo->value.integer.min = 0;
1512 uinfo->value.integer.max = 3;
1516 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1517 struct snd_ctl_elem_value *ucontrol)
1519 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1521 ucontrol->value.integer.value[0] =
1522 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1526 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_value *ucontrol)
1529 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1531 unsigned int vol = ucontrol->value.integer.value[0];
1533 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1534 unsigned int original_coef = coef;
1537 coef |= (vol & 0x0003);
1538 if (original_coef == coef)
1541 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1546 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1548 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1549 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1550 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1551 .name = "Speaker Boost Playback Volume",
1552 .info = cs421x_boost_vol_info,
1553 .get = cs421x_boost_vol_get,
1554 .put = cs421x_boost_vol_put,
1555 .tlv = { .p = cs421x_speaker_boost_db_scale },
1558 static void cs421x_pinmux_init(struct hda_codec *codec)
1560 struct cs_spec *spec = codec->spec;
1561 unsigned int def_conf, coef;
1563 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1564 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1566 if (spec->gpio_mask)
1567 coef |= 0x0008; /* B1,B2 are GPIOs */
1572 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1576 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1578 if ((spec->gpio_mask || spec->sense_b) &&
1579 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1582 GPIO or SENSE_B forced - disconnect the DMIC pin.
1584 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1585 def_conf &= ~AC_DEFCFG_PORT_CONN;
1586 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1587 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1591 static void init_cs421x_digital(struct hda_codec *codec)
1593 struct cs_spec *spec = codec->spec;
1594 struct auto_pin_cfg *cfg = &spec->autocfg;
1598 for (i = 0; i < cfg->dig_outs; i++) {
1599 hda_nid_t nid = cfg->dig_out_pins[i];
1600 if (!cfg->speaker_outs)
1602 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1604 snd_hda_codec_write(codec, nid, 0,
1605 AC_VERB_SET_UNSOLICITED_ENABLE,
1606 AC_USRSP_EN | SPDIF_EVENT);
1607 spec->spdif_detect = 1;
1612 static int cs421x_init(struct hda_codec *codec)
1614 struct cs_spec *spec = codec->spec;
1616 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1617 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1619 cs421x_pinmux_init(codec);
1621 if (spec->gpio_mask) {
1622 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1624 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1626 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1632 init_cs421x_digital(codec);
1638 * CS4210 Input MUX (1 ADC)
1640 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1641 struct snd_ctl_elem_info *uinfo)
1643 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1644 struct cs_spec *spec = codec->spec;
1646 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1649 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1650 struct snd_ctl_elem_value *ucontrol)
1652 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1653 struct cs_spec *spec = codec->spec;
1655 ucontrol->value.enumerated.item[0] = spec->cur_input;
1659 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1660 struct snd_ctl_elem_value *ucontrol)
1662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1663 struct cs_spec *spec = codec->spec;
1665 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1666 spec->adc_nid[0], &spec->cur_input);
1670 static struct snd_kcontrol_new cs421x_capture_source = {
1672 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1673 .name = "Capture Source",
1674 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1675 .info = cs421x_mux_enum_info,
1676 .get = cs421x_mux_enum_get,
1677 .put = cs421x_mux_enum_put,
1680 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1682 struct cs_spec *spec = codec->spec;
1683 struct auto_pin_cfg *cfg = &spec->autocfg;
1684 const struct hda_input_mux *imux = &spec->input_mux;
1685 hda_nid_t pin = cfg->inputs[item].pin;
1686 struct snd_kcontrol *kctl;
1689 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1692 caps = query_amp_caps(codec, pin, HDA_INPUT);
1693 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1697 return add_volume(codec, imux->items[item].label, 0,
1698 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1701 /* add a (input-boost) volume control to the given input pin */
1702 static int build_cs421x_input(struct hda_codec *codec)
1704 struct cs_spec *spec = codec->spec;
1705 struct auto_pin_cfg *cfg = &spec->autocfg;
1706 struct hda_input_mux *imux = &spec->input_mux;
1707 int i, err, type_idx;
1710 if (!spec->num_inputs)
1713 /* make bind-capture */
1714 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1715 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1716 for (i = 0; i < 2; i++) {
1717 struct snd_kcontrol *kctl;
1719 if (!spec->capture_bind[i])
1721 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1724 kctl->private_value = (long)spec->capture_bind[i];
1725 err = snd_hda_ctl_add(codec, 0, kctl);
1728 for (n = 0; n < AUTO_PIN_LAST; n++) {
1729 if (!spec->adc_nid[n])
1731 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1737 /* Add Input MUX Items + Capture Volume/Switch */
1738 for (i = 0; i < spec->num_inputs; i++) {
1739 label = hda_get_autocfg_input_label(codec, cfg, i);
1740 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1742 err = cs421x_add_input_volume_control(codec, i);
1748 Add 'Capture Source' Switch if
1749 * 2 inputs and no mic detec
1752 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1753 (spec->num_inputs == 3)) {
1755 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1756 snd_ctl_new1(&cs421x_capture_source, codec));
1764 /* Single DAC (Mute/Gain) */
1765 static int build_cs421x_output(struct hda_codec *codec)
1767 hda_nid_t dac = CS4210_DAC_NID;
1768 struct cs_spec *spec = codec->spec;
1769 struct auto_pin_cfg *cfg = &spec->autocfg;
1770 struct snd_kcontrol *kctl;
1772 char *name = "Master";
1774 fix_volume_caps(codec, dac);
1776 err = add_mute(codec, name, 0,
1777 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1781 err = add_volume(codec, name, 0,
1782 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1786 if (cfg->speaker_outs) {
1787 err = snd_hda_ctl_add(codec, 0,
1788 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1795 static int cs421x_build_controls(struct hda_codec *codec)
1799 err = build_cs421x_output(codec);
1802 err = build_cs421x_input(codec);
1805 err = build_digital_output(codec);
1808 return cs421x_init(codec);
1811 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1813 switch ((res >> 26) & 0x3f) {
1825 static int parse_cs421x_input(struct hda_codec *codec)
1827 struct cs_spec *spec = codec->spec;
1828 struct auto_pin_cfg *cfg = &spec->autocfg;
1831 for (i = 0; i < cfg->num_inputs; i++) {
1832 hda_nid_t pin = cfg->inputs[i].pin;
1833 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1834 spec->cur_input = spec->last_input = i;
1837 /* check whether the automatic mic switch is available */
1838 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1839 spec->mic_detect = 1;
1840 spec->automic_idx = i;
1846 static int cs421x_parse_auto_config(struct hda_codec *codec)
1848 struct cs_spec *spec = codec->spec;
1851 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1854 err = parse_output(codec);
1857 err = parse_cs421x_input(codec);
1860 err = parse_digital_output(codec);
1868 Manage PDREF, when transitioning to D3hot
1869 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1871 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1875 snd_hda_shutup_pins(codec);
1877 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1878 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1879 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1880 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1882 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1883 coef |= 0x0004; /* PDREF */
1884 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1890 static struct hda_codec_ops cs4210_patch_ops = {
1891 .build_controls = cs421x_build_controls,
1892 .build_pcms = cs_build_pcms,
1893 .init = cs421x_init,
1895 .unsol_event = cs421x_unsol_event,
1897 .suspend = cs421x_suspend,
1901 static int patch_cs421x(struct hda_codec *codec)
1903 struct cs_spec *spec;
1906 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1911 spec->vendor_nid = CS421X_VENDOR_NID;
1913 spec->board_config =
1914 snd_hda_check_board_config(codec, CS421X_MODELS,
1915 cs421x_models, cs421x_cfg_tbl);
1916 if (spec->board_config >= 0)
1917 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1919 Setup GPIO/SENSE for each board (if used)
1921 switch (spec->board_config) {
1922 case CS421X_CDB4210:
1923 snd_printd("CS4210 board: %s\n",
1924 cs421x_models[spec->board_config]);
1925 /* spec->gpio_mask = 3;
1927 spec->gpio_data = 3;
1935 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1936 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1939 cs421x_pinmux_init(codec);
1941 err = cs421x_parse_auto_config(codec);
1945 codec->patch_ops = cs4210_patch_ops;
1959 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
1960 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1961 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1962 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
1966 MODULE_ALIAS("snd-hda-codec-id:10134206");
1967 MODULE_ALIAS("snd-hda-codec-id:10134207");
1968 MODULE_ALIAS("snd-hda-codec-id:10134210");
1970 MODULE_LICENSE("GPL");
1971 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1973 static struct hda_codec_preset_list cirrus_list = {
1974 .preset = snd_hda_preset_cirrus,
1975 .owner = THIS_MODULE,
1978 static int __init patch_cirrus_init(void)
1980 return snd_hda_add_codec_preset(&cirrus_list);
1983 static void __exit patch_cirrus_exit(void)
1985 snd_hda_delete_codec_preset(&cirrus_list);
1988 module_init(patch_cirrus_init)
1989 module_exit(patch_cirrus_exit)