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 "hda_auto_parser.h"
31 #include <sound/tlv.h>
37 struct hda_gen_spec gen;
39 struct auto_pin_cfg autocfg;
40 struct hda_multi_out multiout;
41 struct snd_kcontrol *vmaster_sw;
42 struct snd_kcontrol *vmaster_vol;
44 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
45 hda_nid_t slave_dig_outs[2];
47 unsigned int input_idx[AUTO_PIN_LAST];
48 unsigned int capsrc_idx[AUTO_PIN_LAST];
49 hda_nid_t adc_nid[AUTO_PIN_LAST];
50 unsigned int adc_idx[AUTO_PIN_LAST];
51 unsigned int num_inputs;
52 unsigned int cur_input;
53 unsigned int automic_idx;
55 unsigned int cur_adc_stream_tag;
56 unsigned int cur_adc_format;
59 const struct hda_bind_ctls *capture_bind[2];
61 unsigned int gpio_mask;
62 unsigned int gpio_dir;
63 unsigned int gpio_data;
64 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
65 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
67 struct hda_pcm pcm_rec[2]; /* PCM information */
69 unsigned int hp_detect:1;
70 unsigned int mic_detect:1;
72 unsigned int spdif_detect:1;
73 unsigned int sense_b:1;
75 struct hda_input_mux input_mux;
76 unsigned int last_input;
79 /* available models with CS420x */
86 CS420X_IMAC27_122 = CS420X_GPIO_23,
87 CS420X_APPLE = CS420X_GPIO_13,
97 /* Vendor-specific processing widget */
98 #define CS420X_VENDOR_NID 0x11
99 #define CS_DIG_OUT1_PIN_NID 0x10
100 #define CS_DIG_OUT2_PIN_NID 0x15
101 #define CS_DMIC1_PIN_NID 0x12
102 #define CS_DMIC2_PIN_NID 0x0e
105 #define IDX_SPDIF_STAT 0x0000
106 #define IDX_SPDIF_CTL 0x0001
107 #define IDX_ADC_CFG 0x0002
108 /* SZC bitmask, 4 modes below:
110 * 1 = digital immediate, analog zero-cross
111 * 2 = digtail & analog soft-ramp
112 * 3 = digital soft-ramp, analog zero-cross
114 #define CS_COEF_ADC_SZC_MASK (3 << 0)
115 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
116 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
117 /* PGA mode: 0 = differential, 1 = signle-ended */
118 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
119 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
120 #define IDX_DAC_CFG 0x0003
121 /* SZC bitmask, 4 modes below:
125 * 3 = soft-ramp on zero-cross
127 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
128 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
129 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
131 #define IDX_BEEP_CFG 0x0004
132 /* 0x0008 - test reg key */
133 /* 0x0009 - 0x0014 -> 12 test regs */
134 /* 0x0015 - visibility reg */
137 * Cirrus Logic CS4210
139 * 1 DAC => HP(sense) / Speakers,
140 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
141 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
143 #define CS4210_DAC_NID 0x02
144 #define CS4210_ADC_NID 0x03
145 #define CS4210_VENDOR_NID 0x0B
146 #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
147 #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
149 #define CS421X_IDX_DEV_CFG 0x01
150 #define CS421X_IDX_ADC_CFG 0x02
151 #define CS421X_IDX_DAC_CFG 0x03
152 #define CS421X_IDX_SPK_CTL 0x04
154 #define SPDIF_EVENT 0x04
156 /* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
157 #define CS4213_VENDOR_NID 0x09
160 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
162 struct cs_spec *spec = codec->spec;
163 snd_hda_codec_write(codec, spec->vendor_nid, 0,
164 AC_VERB_SET_COEF_INDEX, idx);
165 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
166 AC_VERB_GET_PROC_COEF, 0);
169 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
172 struct cs_spec *spec = codec->spec;
173 snd_hda_codec_write(codec, spec->vendor_nid, 0,
174 AC_VERB_SET_COEF_INDEX, idx);
175 snd_hda_codec_write(codec, spec->vendor_nid, 0,
176 AC_VERB_SET_PROC_COEF, coef);
186 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
187 struct hda_codec *codec,
188 struct snd_pcm_substream *substream)
190 struct cs_spec *spec = codec->spec;
191 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
195 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
196 struct hda_codec *codec,
197 unsigned int stream_tag,
199 struct snd_pcm_substream *substream)
201 struct cs_spec *spec = codec->spec;
202 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
203 stream_tag, format, substream);
206 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
207 struct hda_codec *codec,
208 struct snd_pcm_substream *substream)
210 struct cs_spec *spec = codec->spec;
211 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
217 static int cs_dig_playback_pcm_open(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_open(codec, &spec->multiout);
225 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
226 struct hda_codec *codec,
227 struct snd_pcm_substream *substream)
229 struct cs_spec *spec = codec->spec;
230 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
233 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
234 struct hda_codec *codec,
235 unsigned int stream_tag,
237 struct snd_pcm_substream *substream)
239 struct cs_spec *spec = codec->spec;
240 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
244 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
245 struct hda_codec *codec,
246 struct snd_pcm_substream *substream)
248 struct cs_spec *spec = codec->spec;
249 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
252 static void cs_update_input_select(struct hda_codec *codec)
254 struct cs_spec *spec = codec->spec;
256 snd_hda_codec_write(codec, spec->cur_adc, 0,
257 AC_VERB_SET_CONNECT_SEL,
258 spec->adc_idx[spec->cur_input]);
264 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
265 struct hda_codec *codec,
266 unsigned int stream_tag,
268 struct snd_pcm_substream *substream)
270 struct cs_spec *spec = codec->spec;
271 spec->cur_adc = spec->adc_nid[spec->cur_input];
272 spec->cur_adc_stream_tag = stream_tag;
273 spec->cur_adc_format = format;
274 cs_update_input_select(codec);
275 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
279 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
280 struct hda_codec *codec,
281 struct snd_pcm_substream *substream)
283 struct cs_spec *spec = codec->spec;
284 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
291 static const struct hda_pcm_stream cs_pcm_analog_playback = {
296 .open = cs_playback_pcm_open,
297 .prepare = cs_playback_pcm_prepare,
298 .cleanup = cs_playback_pcm_cleanup
302 static const struct hda_pcm_stream cs_pcm_analog_capture = {
307 .prepare = cs_capture_pcm_prepare,
308 .cleanup = cs_capture_pcm_cleanup
312 static const struct hda_pcm_stream cs_pcm_digital_playback = {
317 .open = cs_dig_playback_pcm_open,
318 .close = cs_dig_playback_pcm_close,
319 .prepare = cs_dig_playback_pcm_prepare,
320 .cleanup = cs_dig_playback_pcm_cleanup
324 static const struct hda_pcm_stream cs_pcm_digital_capture = {
330 static int cs_build_pcms(struct hda_codec *codec)
332 struct cs_spec *spec = codec->spec;
333 struct hda_pcm *info = spec->pcm_rec;
335 codec->pcm_info = info;
338 info->name = "Cirrus Analog";
339 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
340 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
341 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
342 spec->multiout.max_channels;
343 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
344 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
345 spec->adc_nid[spec->cur_input];
348 if (!spec->multiout.dig_out_nid && !spec->dig_in)
352 info->name = "Cirrus Digital";
353 info->pcm_type = spec->autocfg.dig_out_type[0];
355 info->pcm_type = HDA_PCM_TYPE_SPDIF;
356 if (spec->multiout.dig_out_nid) {
357 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
358 cs_pcm_digital_playback;
359 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
360 spec->multiout.dig_out_nid;
363 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
364 cs_pcm_digital_capture;
365 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
373 * parse codec topology
376 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
381 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
386 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
388 struct cs_spec *spec = codec->spec;
389 struct auto_pin_cfg *cfg = &spec->autocfg;
390 hda_nid_t pin = cfg->inputs[idx].pin;
392 if (!is_jack_detectable(codec, pin))
394 val = snd_hda_codec_get_pincfg(codec, pin);
395 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
398 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
404 nid = codec->start_nid;
405 for (i = 0; i < codec->num_nodes; i++, nid++) {
407 type = get_wcaps_type(get_wcaps(codec, nid));
408 if (type != AC_WID_AUD_IN)
410 idx = snd_hda_get_conn_index(codec, nid, pin, false);
419 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
422 val = snd_hda_codec_get_pincfg(codec, nid);
423 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
426 static int parse_output(struct hda_codec *codec)
428 struct cs_spec *spec = codec->spec;
429 struct auto_pin_cfg *cfg = &spec->autocfg;
433 for (i = 0; i < cfg->line_outs; i++) {
434 dac = get_dac(codec, cfg->line_out_pins[i]);
437 spec->dac_nid[i] = dac;
439 spec->multiout.num_dacs = i;
440 spec->multiout.dac_nids = spec->dac_nid;
441 spec->multiout.max_channels = i * 2;
443 /* add HP and speakers */
445 for (i = 0; i < cfg->hp_outs; i++) {
446 dac = get_dac(codec, cfg->hp_pins[i]);
450 spec->multiout.hp_nid = dac;
452 spec->multiout.extra_out_nid[extra_nids++] = dac;
454 for (i = 0; i < cfg->speaker_outs; i++) {
455 dac = get_dac(codec, cfg->speaker_pins[i]);
458 spec->multiout.extra_out_nid[extra_nids++] = dac;
461 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
462 cfg->speaker_outs = cfg->line_outs;
463 memcpy(cfg->speaker_pins, cfg->line_out_pins,
464 sizeof(cfg->speaker_pins));
471 static int parse_input(struct hda_codec *codec)
473 struct cs_spec *spec = codec->spec;
474 struct auto_pin_cfg *cfg = &spec->autocfg;
477 for (i = 0; i < cfg->num_inputs; i++) {
478 hda_nid_t pin = cfg->inputs[i].pin;
479 spec->input_idx[spec->num_inputs] = i;
480 spec->capsrc_idx[i] = spec->num_inputs++;
482 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
484 if (!spec->num_inputs)
487 /* check whether the automatic mic switch is available */
488 if (spec->num_inputs == 2 &&
489 cfg->inputs[0].type == AUTO_PIN_MIC &&
490 cfg->inputs[1].type == AUTO_PIN_MIC) {
491 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
492 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
493 spec->mic_detect = 1;
494 spec->automic_idx = 0;
497 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
498 spec->mic_detect = 1;
499 spec->automic_idx = 1;
507 static int parse_digital_output(struct hda_codec *codec)
509 struct cs_spec *spec = codec->spec;
510 struct auto_pin_cfg *cfg = &spec->autocfg;
515 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
517 spec->multiout.dig_out_nid = nid;
518 spec->multiout.share_spdif = 1;
519 if (cfg->dig_outs > 1 &&
520 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
521 spec->slave_dig_outs[0] = nid;
522 codec->slave_dig_outs = spec->slave_dig_outs;
527 static int parse_digital_input(struct hda_codec *codec)
529 struct cs_spec *spec = codec->spec;
530 struct auto_pin_cfg *cfg = &spec->autocfg;
534 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
539 * create mixer controls
542 static const char * const dir_sfx[2] = { "Playback", "Capture" };
544 static int add_mute(struct hda_codec *codec, const char *name, int index,
545 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
548 struct snd_kcontrol_new knew =
549 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
550 knew.private_value = pval;
551 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
552 *kctlp = snd_ctl_new1(&knew, codec);
553 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
554 return snd_hda_ctl_add(codec, 0, *kctlp);
557 static int add_volume(struct hda_codec *codec, const char *name,
558 int index, unsigned int pval, int dir,
559 struct snd_kcontrol **kctlp)
562 struct snd_kcontrol_new knew =
563 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
564 knew.private_value = pval;
565 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
566 *kctlp = snd_ctl_new1(&knew, codec);
567 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
568 return snd_hda_ctl_add(codec, 0, *kctlp);
571 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
575 /* set the upper-limit for mixer amp to 0dB */
576 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
577 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
578 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
579 << AC_AMPCAP_NUM_STEPS_SHIFT;
580 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
583 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
585 struct cs_spec *spec = codec->spec;
590 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
591 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
595 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
597 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
598 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
604 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
605 int num_ctls, int type)
607 struct cs_spec *spec = codec->spec;
610 struct snd_kcontrol *kctl;
611 static const char * const speakers[] = {
612 "Front Speaker", "Surround Speaker", "Bass Speaker"
614 static const char * const line_outs[] = {
615 "Front Line Out", "Surround Line Out", "Bass Line Out"
618 fix_volume_caps(codec, dac);
619 if (!spec->vmaster_sw) {
620 err = add_vmaster(codec, dac);
627 case AUTO_PIN_HP_OUT:
631 case AUTO_PIN_SPEAKER_OUT:
633 name = speakers[idx];
639 name = line_outs[idx];
645 err = add_mute(codec, name, index,
646 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
649 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
653 err = add_volume(codec, name, index,
654 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
657 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
664 static int build_output(struct hda_codec *codec)
666 struct cs_spec *spec = codec->spec;
667 struct auto_pin_cfg *cfg = &spec->autocfg;
670 for (i = 0; i < cfg->line_outs; i++) {
671 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
672 i, cfg->line_outs, cfg->line_out_type);
676 for (i = 0; i < cfg->hp_outs; i++) {
677 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
678 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
682 for (i = 0; i < cfg->speaker_outs; i++) {
683 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
684 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
694 static const struct snd_kcontrol_new cs_capture_ctls[] = {
695 HDA_BIND_SW("Capture Switch", 0),
696 HDA_BIND_VOL("Capture Volume", 0),
699 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
702 struct cs_spec *spec = codec->spec;
704 if (spec->cur_input == idx && !force)
706 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
707 /* stream is running, let's swap the current ADC */
708 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
709 spec->cur_adc = spec->adc_nid[idx];
710 snd_hda_codec_setup_stream(codec, spec->cur_adc,
711 spec->cur_adc_stream_tag, 0,
712 spec->cur_adc_format);
714 spec->cur_input = idx;
715 cs_update_input_select(codec);
719 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
720 struct snd_ctl_elem_info *uinfo)
722 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
723 struct cs_spec *spec = codec->spec;
724 struct auto_pin_cfg *cfg = &spec->autocfg;
727 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
729 uinfo->value.enumerated.items = spec->num_inputs;
730 if (uinfo->value.enumerated.item >= spec->num_inputs)
731 uinfo->value.enumerated.item = spec->num_inputs - 1;
732 idx = spec->input_idx[uinfo->value.enumerated.item];
733 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
734 uinfo->value.enumerated.name,
735 sizeof(uinfo->value.enumerated.name), NULL);
739 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
740 struct snd_ctl_elem_value *ucontrol)
742 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
743 struct cs_spec *spec = codec->spec;
744 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
748 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
749 struct snd_ctl_elem_value *ucontrol)
751 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
752 struct cs_spec *spec = codec->spec;
753 unsigned int idx = ucontrol->value.enumerated.item[0];
755 if (idx >= spec->num_inputs)
757 idx = spec->input_idx[idx];
758 return change_cur_input(codec, idx, 0);
761 static const struct snd_kcontrol_new cs_capture_source = {
762 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
763 .name = "Capture Source",
764 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
765 .info = cs_capture_source_info,
766 .get = cs_capture_source_get,
767 .put = cs_capture_source_put,
770 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
771 struct hda_ctl_ops *ops)
773 struct cs_spec *spec = codec->spec;
774 struct hda_bind_ctls *bind;
777 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
783 for (i = 0; i < AUTO_PIN_LAST; i++) {
784 if (!spec->adc_nid[i])
787 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
788 spec->adc_idx[i], HDA_INPUT);
793 /* add a (input-boost) volume control to the given input pin */
794 static int add_input_volume_control(struct hda_codec *codec,
795 struct auto_pin_cfg *cfg,
798 hda_nid_t pin = cfg->inputs[item].pin;
801 struct snd_kcontrol *kctl;
803 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
805 caps = query_amp_caps(codec, pin, HDA_INPUT);
806 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
809 label = hda_get_autocfg_input_label(codec, cfg, item);
810 return add_volume(codec, label, 0,
811 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
814 static int build_input(struct hda_codec *codec)
816 struct cs_spec *spec = codec->spec;
819 if (!spec->num_inputs)
822 /* make bind-capture */
823 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
824 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
825 for (i = 0; i < 2; i++) {
826 struct snd_kcontrol *kctl;
828 if (!spec->capture_bind[i])
830 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
833 kctl->private_value = (long)spec->capture_bind[i];
834 err = snd_hda_ctl_add(codec, 0, kctl);
837 for (n = 0; n < AUTO_PIN_LAST; n++) {
838 if (!spec->adc_nid[n])
840 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
846 if (spec->num_inputs > 1 && !spec->mic_detect) {
847 err = snd_hda_ctl_add(codec, 0,
848 snd_ctl_new1(&cs_capture_source, codec));
853 for (i = 0; i < spec->num_inputs; i++) {
854 err = add_input_volume_control(codec, &spec->autocfg, i);
865 static int build_digital_output(struct hda_codec *codec)
867 struct cs_spec *spec = codec->spec;
870 if (!spec->multiout.dig_out_nid)
873 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
874 spec->multiout.dig_out_nid);
877 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
883 static int build_digital_input(struct hda_codec *codec)
885 struct cs_spec *spec = codec->spec;
887 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
892 * auto-mute and auto-mic switching
893 * CS421x auto-output redirecting
897 static void cs_automute(struct hda_codec *codec)
899 struct cs_spec *spec = codec->spec;
900 struct auto_pin_cfg *cfg = &spec->autocfg;
901 unsigned int hp_present;
902 unsigned int spdif_present;
908 nid = cfg->dig_out_pins[0];
909 if (is_jack_detectable(codec, nid)) {
911 TODO: SPDIF output redirect when SENSE_B is enabled.
912 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
915 if (snd_hda_jack_detect(codec, nid)
916 /* && spec->sense_b */)
922 for (i = 0; i < cfg->hp_outs; i++) {
923 nid = cfg->hp_pins[i];
924 if (!is_jack_detectable(codec, nid))
926 hp_present = snd_hda_jack_detect(codec, nid);
931 /* mute speakers if spdif or hp jack is plugged in */
932 for (i = 0; i < cfg->speaker_outs; i++) {
933 int pin_ctl = hp_present ? 0 : PIN_OUT;
934 /* detect on spdif is specific to CS4210 */
935 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
938 nid = cfg->speaker_pins[i];
939 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
941 if (spec->gpio_eapd_hp) {
942 unsigned int gpio = hp_present ?
943 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
944 snd_hda_codec_write(codec, 0x01, 0,
945 AC_VERB_SET_GPIO_DATA, gpio);
948 /* specific to CS4210 */
949 if (spec->vendor_nid == CS4210_VENDOR_NID) {
950 /* mute HPs if spdif jack (SENSE_B) is present */
951 for (i = 0; i < cfg->hp_outs; i++) {
952 nid = cfg->hp_pins[i];
953 snd_hda_set_pin_ctl(codec, nid,
954 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
957 /* SPDIF TX on/off */
959 nid = cfg->dig_out_pins[0];
960 snd_hda_set_pin_ctl(codec, nid,
961 spdif_present ? PIN_OUT : 0);
964 /* Update board GPIOs if neccessary ... */
969 * Auto-input redirect for CS421x
970 * Switch max 3 inputs of a single ADC (nid 3)
973 static void cs_automic(struct hda_codec *codec)
975 struct cs_spec *spec = codec->spec;
976 struct auto_pin_cfg *cfg = &spec->autocfg;
978 unsigned int present;
980 nid = cfg->inputs[spec->automic_idx].pin;
981 present = snd_hda_jack_detect(codec, nid);
983 /* specific to CS421x, single ADC */
984 if (spec->vendor_nid == CS420X_VENDOR_NID) {
986 change_cur_input(codec, spec->automic_idx, 0);
988 change_cur_input(codec, !spec->automic_idx, 0);
991 if (spec->cur_input != spec->automic_idx) {
992 spec->last_input = spec->cur_input;
993 spec->cur_input = spec->automic_idx;
996 spec->cur_input = spec->last_input;
998 cs_update_input_select(codec);
1005 static void init_output(struct hda_codec *codec)
1007 struct cs_spec *spec = codec->spec;
1008 struct auto_pin_cfg *cfg = &spec->autocfg;
1012 for (i = 0; i < spec->multiout.num_dacs; i++)
1013 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1014 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1015 if (spec->multiout.hp_nid)
1016 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1017 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1018 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1019 if (!spec->multiout.extra_out_nid[i])
1021 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1022 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1025 /* set appropriate pin controls */
1026 for (i = 0; i < cfg->line_outs; i++)
1027 snd_hda_set_pin_ctl(codec, cfg->line_out_pins[i], PIN_OUT);
1029 for (i = 0; i < cfg->hp_outs; i++) {
1030 hda_nid_t nid = cfg->hp_pins[i];
1031 snd_hda_set_pin_ctl(codec, nid, PIN_HP);
1032 if (!cfg->speaker_outs)
1034 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1035 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
1036 spec->hp_detect = 1;
1041 for (i = 0; i < cfg->speaker_outs; i++)
1042 snd_hda_set_pin_ctl(codec, cfg->speaker_pins[i], PIN_OUT);
1044 /* SPDIF is enabled on presence detect for CS421x */
1045 if (spec->hp_detect || spec->spdif_detect)
1049 static void init_input(struct hda_codec *codec)
1051 struct cs_spec *spec = codec->spec;
1052 struct auto_pin_cfg *cfg = &spec->autocfg;
1056 for (i = 0; i < cfg->num_inputs; i++) {
1058 hda_nid_t pin = cfg->inputs[i].pin;
1059 if (!spec->adc_nid[i])
1061 /* set appropriate pin control and mute first */
1063 if (cfg->inputs[i].type == AUTO_PIN_MIC)
1064 ctl |= snd_hda_get_default_vref(codec, pin);
1065 snd_hda_set_pin_ctl(codec, pin, ctl);
1066 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1067 AC_VERB_SET_AMP_GAIN_MUTE,
1068 AMP_IN_MUTE(spec->adc_idx[i]));
1069 if (spec->mic_detect && spec->automic_idx == i)
1070 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
1072 /* CS420x has multiple ADC, CS421x has single ADC */
1073 if (spec->vendor_nid == CS420X_VENDOR_NID) {
1074 change_cur_input(codec, spec->cur_input, 1);
1075 if (spec->mic_detect)
1078 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1079 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1080 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1081 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1082 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1083 * No effect if SPDIF_OUT2 is
1084 * selected in IDX_SPDIF_CTL.
1086 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1088 if (spec->mic_detect)
1091 spec->cur_adc = spec->adc_nid[spec->cur_input];
1092 cs_update_input_select(codec);
1097 static const struct hda_verb cs_coef_init_verbs[] = {
1098 {0x11, AC_VERB_SET_PROC_STATE, 1},
1099 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1100 {0x11, AC_VERB_SET_PROC_COEF,
1101 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1102 | 0x0040 /* Mute DACs on FIFO error */
1103 | 0x1000 /* Enable DACs High Pass Filter */
1104 | 0x0400 /* Disable Coefficient Auto increment */
1107 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1108 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1113 /* Errata: CS4207 rev C0/C1/C2 Silicon
1115 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1117 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1118 * may be excessive (up to an additional 200 μA), which is most easily
1119 * observed while the part is being held in reset (RESET# active low).
1121 * Root Cause: At initial powerup of the device, the logic that drives
1122 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1124 * Certain random patterns will cause a steady leakage current in those
1125 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1127 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1128 * blocks, which will alleviate the issue.
1131 static const struct hda_verb cs_errata_init_verbs[] = {
1132 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1133 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1135 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1136 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1137 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1138 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1139 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1140 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1142 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1143 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1145 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1146 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1147 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1148 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1149 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1150 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1151 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1153 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1154 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1155 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1156 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1163 static void init_digital(struct hda_codec *codec)
1167 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1168 coef |= 0x0008; /* Replace with mute on error */
1169 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1170 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1171 * SPDIF_OUT2 is shared with GPIO1 and
1174 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1177 static int cs_init(struct hda_codec *codec)
1179 struct cs_spec *spec = codec->spec;
1181 /* init_verb sequence for C0/C1/C2 errata*/
1182 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1184 snd_hda_sequence_write(codec, cs_coef_init_verbs);
1186 if (spec->gpio_mask) {
1187 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1189 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1191 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1197 init_digital(codec);
1202 static int cs_build_controls(struct hda_codec *codec)
1204 struct cs_spec *spec = codec->spec;
1207 err = build_output(codec);
1210 err = build_input(codec);
1213 err = build_digital_output(codec);
1216 err = build_digital_input(codec);
1219 err = cs_init(codec);
1223 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1230 static void cs_free(struct hda_codec *codec)
1232 struct cs_spec *spec = codec->spec;
1233 kfree(spec->capture_bind[0]);
1234 kfree(spec->capture_bind[1]);
1238 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1240 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1248 snd_hda_jack_report_sync(codec);
1251 static const struct hda_codec_ops cs_patch_ops = {
1252 .build_controls = cs_build_controls,
1253 .build_pcms = cs_build_pcms,
1256 .unsol_event = cs_unsol_event,
1259 static int cs_parse_auto_config(struct hda_codec *codec)
1261 struct cs_spec *spec = codec->spec;
1264 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1268 err = parse_output(codec);
1271 err = parse_input(codec);
1274 err = parse_digital_output(codec);
1277 err = parse_digital_input(codec);
1283 static const struct hda_model_fixup cs420x_models[] = {
1284 { .id = CS420X_MBP53, .name = "mbp53" },
1285 { .id = CS420X_MBP55, .name = "mbp55" },
1286 { .id = CS420X_IMAC27, .name = "imac27" },
1287 { .id = CS420X_IMAC27_122, .name = "imac27_122" },
1288 { .id = CS420X_APPLE, .name = "apple" },
1292 static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
1293 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1294 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1295 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1296 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1297 /* this conflicts with too many other models */
1298 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1301 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
1302 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
1306 static const struct hda_pintbl mbp53_pincfgs[] = {
1307 { 0x09, 0x012b4050 },
1308 { 0x0a, 0x90100141 },
1309 { 0x0b, 0x90100140 },
1310 { 0x0c, 0x018b3020 },
1311 { 0x0d, 0x90a00110 },
1312 { 0x0e, 0x400000f0 },
1313 { 0x0f, 0x01cbe030 },
1314 { 0x10, 0x014be060 },
1315 { 0x12, 0x400000f0 },
1316 { 0x15, 0x400000f0 },
1320 static const struct hda_pintbl mbp55_pincfgs[] = {
1321 { 0x09, 0x012b4030 },
1322 { 0x0a, 0x90100121 },
1323 { 0x0b, 0x90100120 },
1324 { 0x0c, 0x400000f0 },
1325 { 0x0d, 0x90a00110 },
1326 { 0x0e, 0x400000f0 },
1327 { 0x0f, 0x400000f0 },
1328 { 0x10, 0x014be040 },
1329 { 0x12, 0x400000f0 },
1330 { 0x15, 0x400000f0 },
1334 static const struct hda_pintbl imac27_pincfgs[] = {
1335 { 0x09, 0x012b4050 },
1336 { 0x0a, 0x90100140 },
1337 { 0x0b, 0x90100142 },
1338 { 0x0c, 0x018b3020 },
1339 { 0x0d, 0x90a00110 },
1340 { 0x0e, 0x400000f0 },
1341 { 0x0f, 0x01cbe030 },
1342 { 0x10, 0x014be060 },
1343 { 0x12, 0x01ab9070 },
1344 { 0x15, 0x400000f0 },
1348 static void cs420x_fixup_gpio_13(struct hda_codec *codec,
1349 const struct hda_fixup *fix, int action)
1351 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1352 struct cs_spec *spec = codec->spec;
1353 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1354 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1355 spec->gpio_mask = spec->gpio_dir =
1356 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1360 static void cs420x_fixup_gpio_23(struct hda_codec *codec,
1361 const struct hda_fixup *fix, int action)
1363 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1364 struct cs_spec *spec = codec->spec;
1365 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1366 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1367 spec->gpio_mask = spec->gpio_dir =
1368 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1372 static const struct hda_fixup cs420x_fixups[] = {
1374 .type = HDA_FIXUP_PINS,
1375 .v.pins = mbp53_pincfgs,
1377 .chain_id = CS420X_APPLE,
1380 .type = HDA_FIXUP_PINS,
1381 .v.pins = mbp55_pincfgs,
1383 .chain_id = CS420X_GPIO_13,
1386 .type = HDA_FIXUP_PINS,
1387 .v.pins = imac27_pincfgs,
1389 .chain_id = CS420X_GPIO_13,
1391 [CS420X_GPIO_13] = {
1392 .type = HDA_FIXUP_FUNC,
1393 .v.func = cs420x_fixup_gpio_13,
1395 [CS420X_GPIO_23] = {
1396 .type = HDA_FIXUP_FUNC,
1397 .v.func = cs420x_fixup_gpio_23,
1401 static int patch_cs420x(struct hda_codec *codec)
1403 struct cs_spec *spec;
1406 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1411 spec->vendor_nid = CS420X_VENDOR_NID;
1413 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
1415 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1417 err = cs_parse_auto_config(codec);
1421 codec->patch_ops = cs_patch_ops;
1423 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1434 * Cirrus Logic CS4210
1436 * 1 DAC => HP(sense) / Speakers,
1437 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1438 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1441 /* CS4210 board names */
1442 static const struct hda_model_fixup cs421x_models[] = {
1443 { .id = CS421X_CDB4210, .name = "cdb4210" },
1447 static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
1448 /* Test Intel board + CDB2410 */
1449 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1453 /* CS4210 board pinconfigs */
1454 /* Default CS4210 (CDB4210)*/
1455 static const struct hda_pintbl cdb4210_pincfgs[] = {
1456 { 0x05, 0x0321401f },
1457 { 0x06, 0x90170010 },
1458 { 0x07, 0x03813031 },
1459 { 0x08, 0xb7a70037 },
1460 { 0x09, 0xb7a6003e },
1461 { 0x0a, 0x034510f0 },
1465 /* Setup GPIO/SENSE for each board (if used) */
1466 static void cs421x_fixup_sense_b(struct hda_codec *codec,
1467 const struct hda_fixup *fix, int action)
1469 struct cs_spec *spec = codec->spec;
1470 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1474 static const struct hda_fixup cs421x_fixups[] = {
1475 [CS421X_CDB4210] = {
1476 .type = HDA_FIXUP_PINS,
1477 .v.pins = cdb4210_pincfgs,
1479 .chain_id = CS421X_SENSE_B,
1481 [CS421X_SENSE_B] = {
1482 .type = HDA_FIXUP_FUNC,
1483 .v.func = cs421x_fixup_sense_b,
1487 static const struct hda_verb cs421x_coef_init_verbs[] = {
1488 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1489 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1491 Disable Coefficient Index Auto-Increment(DAI)=1,
1494 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1496 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1497 /* ADC SZCMode = Digital Soft Ramp */
1498 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1500 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1501 {0x0B, AC_VERB_SET_PROC_COEF,
1502 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1503 | 0x0004 /* Mute DAC on FIFO error */
1504 | 0x0008 /* Enable DAC High Pass Filter */
1509 /* Errata: CS4210 rev A1 Silicon
1511 * http://www.cirrus.com/en/pubs/errata/
1514 * 1. Performance degredation is present in the ADC.
1515 * 2. Speaker output is not completely muted upon HP detect.
1516 * 3. Noise is present when clipping occurs on the amplified
1520 * The following verb sequence written to the registers during
1521 * initialization will correct the issues listed above.
1524 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1525 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1527 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1528 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1530 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1531 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1533 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1534 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1536 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1537 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1539 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1540 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1545 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1546 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1548 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1549 struct snd_ctl_elem_info *uinfo)
1551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1553 uinfo->value.integer.min = 0;
1554 uinfo->value.integer.max = 3;
1558 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1559 struct snd_ctl_elem_value *ucontrol)
1561 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1563 ucontrol->value.integer.value[0] =
1564 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1568 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1569 struct snd_ctl_elem_value *ucontrol)
1571 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1573 unsigned int vol = ucontrol->value.integer.value[0];
1575 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1576 unsigned int original_coef = coef;
1579 coef |= (vol & 0x0003);
1580 if (original_coef == coef)
1583 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1588 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1590 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1591 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1592 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1593 .name = "Speaker Boost Playback Volume",
1594 .info = cs421x_boost_vol_info,
1595 .get = cs421x_boost_vol_get,
1596 .put = cs421x_boost_vol_put,
1597 .tlv = { .p = cs421x_speaker_boost_db_scale },
1600 static void cs4210_pinmux_init(struct hda_codec *codec)
1602 struct cs_spec *spec = codec->spec;
1603 unsigned int def_conf, coef;
1605 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1606 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1608 if (spec->gpio_mask)
1609 coef |= 0x0008; /* B1,B2 are GPIOs */
1614 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1618 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1620 if ((spec->gpio_mask || spec->sense_b) &&
1621 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1624 GPIO or SENSE_B forced - disconnect the DMIC pin.
1626 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1627 def_conf &= ~AC_DEFCFG_PORT_CONN;
1628 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1629 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1633 static void init_cs421x_digital(struct hda_codec *codec)
1635 struct cs_spec *spec = codec->spec;
1636 struct auto_pin_cfg *cfg = &spec->autocfg;
1640 for (i = 0; i < cfg->dig_outs; i++) {
1641 hda_nid_t nid = cfg->dig_out_pins[i];
1642 if (!cfg->speaker_outs)
1644 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1645 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
1646 spec->spdif_detect = 1;
1651 static int cs421x_init(struct hda_codec *codec)
1653 struct cs_spec *spec = codec->spec;
1655 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1656 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1657 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1658 cs4210_pinmux_init(codec);
1661 if (spec->gpio_mask) {
1662 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1664 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1666 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1672 init_cs421x_digital(codec);
1678 * CS4210 Input MUX (1 ADC)
1680 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_info *uinfo)
1683 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1684 struct cs_spec *spec = codec->spec;
1686 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1689 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1690 struct snd_ctl_elem_value *ucontrol)
1692 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1693 struct cs_spec *spec = codec->spec;
1695 ucontrol->value.enumerated.item[0] = spec->cur_input;
1699 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1700 struct snd_ctl_elem_value *ucontrol)
1702 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1703 struct cs_spec *spec = codec->spec;
1705 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1706 spec->adc_nid[0], &spec->cur_input);
1710 static struct snd_kcontrol_new cs421x_capture_source = {
1712 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1713 .name = "Capture Source",
1714 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1715 .info = cs421x_mux_enum_info,
1716 .get = cs421x_mux_enum_get,
1717 .put = cs421x_mux_enum_put,
1720 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1722 struct cs_spec *spec = codec->spec;
1723 struct auto_pin_cfg *cfg = &spec->autocfg;
1724 const struct hda_input_mux *imux = &spec->input_mux;
1725 hda_nid_t pin = cfg->inputs[item].pin;
1726 struct snd_kcontrol *kctl;
1729 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1732 caps = query_amp_caps(codec, pin, HDA_INPUT);
1733 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1737 return add_volume(codec, imux->items[item].label, 0,
1738 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1741 /* add a (input-boost) volume control to the given input pin */
1742 static int build_cs421x_input(struct hda_codec *codec)
1744 struct cs_spec *spec = codec->spec;
1745 struct auto_pin_cfg *cfg = &spec->autocfg;
1746 struct hda_input_mux *imux = &spec->input_mux;
1747 int i, err, type_idx;
1750 if (!spec->num_inputs)
1753 /* make bind-capture */
1754 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1755 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1756 for (i = 0; i < 2; i++) {
1757 struct snd_kcontrol *kctl;
1759 if (!spec->capture_bind[i])
1761 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1764 kctl->private_value = (long)spec->capture_bind[i];
1765 err = snd_hda_ctl_add(codec, 0, kctl);
1768 for (n = 0; n < AUTO_PIN_LAST; n++) {
1769 if (!spec->adc_nid[n])
1771 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1777 /* Add Input MUX Items + Capture Volume/Switch */
1778 for (i = 0; i < spec->num_inputs; i++) {
1779 label = hda_get_autocfg_input_label(codec, cfg, i);
1780 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1782 err = cs421x_add_input_volume_control(codec, i);
1788 Add 'Capture Source' Switch if
1789 * 2 inputs and no mic detec
1792 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1793 (spec->num_inputs == 3)) {
1795 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1796 snd_ctl_new1(&cs421x_capture_source, codec));
1804 /* Single DAC (Mute/Gain) */
1805 static int build_cs421x_output(struct hda_codec *codec)
1807 hda_nid_t dac = CS4210_DAC_NID;
1808 struct cs_spec *spec = codec->spec;
1809 struct auto_pin_cfg *cfg = &spec->autocfg;
1810 struct snd_kcontrol *kctl;
1812 char *name = "Master";
1814 fix_volume_caps(codec, dac);
1816 err = add_mute(codec, name, 0,
1817 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1821 err = add_volume(codec, name, 0,
1822 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1826 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
1827 err = snd_hda_ctl_add(codec, 0,
1828 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1835 static int cs421x_build_controls(struct hda_codec *codec)
1837 struct cs_spec *spec = codec->spec;
1840 err = build_cs421x_output(codec);
1843 err = build_cs421x_input(codec);
1846 err = build_digital_output(codec);
1849 err = cs421x_init(codec);
1853 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1860 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1862 switch (snd_hda_jack_get_action(codec, res >> 26)) {
1872 snd_hda_jack_report_sync(codec);
1875 static int parse_cs421x_input(struct hda_codec *codec)
1877 struct cs_spec *spec = codec->spec;
1878 struct auto_pin_cfg *cfg = &spec->autocfg;
1881 for (i = 0; i < cfg->num_inputs; i++) {
1882 hda_nid_t pin = cfg->inputs[i].pin;
1883 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1884 spec->cur_input = spec->last_input = i;
1887 /* check whether the automatic mic switch is available */
1888 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1889 spec->mic_detect = 1;
1890 spec->automic_idx = i;
1896 static int cs421x_parse_auto_config(struct hda_codec *codec)
1898 struct cs_spec *spec = codec->spec;
1901 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1904 err = parse_output(codec);
1907 err = parse_cs421x_input(codec);
1910 err = parse_digital_output(codec);
1918 Manage PDREF, when transitioning to D3hot
1919 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1921 static int cs421x_suspend(struct hda_codec *codec)
1923 struct cs_spec *spec = codec->spec;
1926 snd_hda_shutup_pins(codec);
1928 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1929 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1930 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1931 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1933 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1934 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1935 coef |= 0x0004; /* PDREF */
1936 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1943 static struct hda_codec_ops cs421x_patch_ops = {
1944 .build_controls = cs421x_build_controls,
1945 .build_pcms = cs_build_pcms,
1946 .init = cs421x_init,
1948 .unsol_event = cs421x_unsol_event,
1950 .suspend = cs421x_suspend,
1954 static int patch_cs4210(struct hda_codec *codec)
1956 struct cs_spec *spec;
1959 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1964 spec->vendor_nid = CS4210_VENDOR_NID;
1966 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1968 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1971 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1972 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1975 cs4210_pinmux_init(codec);
1977 err = cs421x_parse_auto_config(codec);
1981 codec->patch_ops = cs421x_patch_ops;
1983 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1993 static int patch_cs4213(struct hda_codec *codec)
1995 struct cs_spec *spec;
1998 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2003 spec->vendor_nid = CS4213_VENDOR_NID;
2005 err = cs421x_parse_auto_config(codec);
2009 codec->patch_ops = cs421x_patch_ops;
2022 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
2023 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2024 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
2025 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2026 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
2030 MODULE_ALIAS("snd-hda-codec-id:10134206");
2031 MODULE_ALIAS("snd-hda-codec-id:10134207");
2032 MODULE_ALIAS("snd-hda-codec-id:10134210");
2033 MODULE_ALIAS("snd-hda-codec-id:10134213");
2035 MODULE_LICENSE("GPL");
2036 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2038 static struct hda_codec_preset_list cirrus_list = {
2039 .preset = snd_hda_preset_cirrus,
2040 .owner = THIS_MODULE,
2043 static int __init patch_cirrus_init(void)
2045 return snd_hda_add_codec_preset(&cirrus_list);
2048 static void __exit patch_cirrus_exit(void)
2050 snd_hda_delete_codec_preset(&cirrus_list);
2053 module_init(patch_cirrus_init)
2054 module_exit(patch_cirrus_exit)