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
;
62 struct hda_pcm pcm_rec
[2]; /* PCM information */
64 unsigned int hp_detect
:1;
65 unsigned int mic_detect
:1;
67 unsigned int spdif_detect
:1;
68 unsigned int sense_b
:1;
70 struct hda_input_mux input_mux
;
71 unsigned int last_input
;
74 /* available models with CS420x */
89 /* Vendor-specific processing widget */
90 #define CS420X_VENDOR_NID 0x11
91 #define CS_DIG_OUT1_PIN_NID 0x10
92 #define CS_DIG_OUT2_PIN_NID 0x15
93 #define CS_DMIC1_PIN_NID 0x12
94 #define CS_DMIC2_PIN_NID 0x0e
97 #define IDX_SPDIF_STAT 0x0000
98 #define IDX_SPDIF_CTL 0x0001
99 #define IDX_ADC_CFG 0x0002
100 /* SZC bitmask, 4 modes below:
102 * 1 = digital immediate, analog zero-cross
103 * 2 = digtail & analog soft-ramp
104 * 3 = digital soft-ramp, analog zero-cross
106 #define CS_COEF_ADC_SZC_MASK (3 << 0)
107 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
108 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
109 /* PGA mode: 0 = differential, 1 = signle-ended */
110 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
111 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
112 #define IDX_DAC_CFG 0x0003
113 /* SZC bitmask, 4 modes below:
117 * 3 = soft-ramp on zero-cross
119 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
120 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
121 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
123 #define IDX_BEEP_CFG 0x0004
124 /* 0x0008 - test reg key */
125 /* 0x0009 - 0x0014 -> 12 test regs */
126 /* 0x0015 - visibility reg */
129 * Cirrus Logic CS4210
131 * 1 DAC => HP(sense) / Speakers,
132 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
133 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
135 #define CS4210_DAC_NID 0x02
136 #define CS4210_ADC_NID 0x03
137 #define CS421X_VENDOR_NID 0x0B
138 #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
139 #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
141 #define CS421X_IDX_DEV_CFG 0x01
142 #define CS421X_IDX_ADC_CFG 0x02
143 #define CS421X_IDX_DAC_CFG 0x03
144 #define CS421X_IDX_SPK_CTL 0x04
146 #define SPDIF_EVENT 0x04
148 static inline int cs_vendor_coef_get(struct hda_codec
*codec
, unsigned int idx
)
150 struct cs_spec
*spec
= codec
->spec
;
151 snd_hda_codec_write(codec
, spec
->vendor_nid
, 0,
152 AC_VERB_SET_COEF_INDEX
, idx
);
153 return snd_hda_codec_read(codec
, spec
->vendor_nid
, 0,
154 AC_VERB_GET_PROC_COEF
, 0);
157 static inline void cs_vendor_coef_set(struct hda_codec
*codec
, unsigned int idx
,
160 struct cs_spec
*spec
= codec
->spec
;
161 snd_hda_codec_write(codec
, spec
->vendor_nid
, 0,
162 AC_VERB_SET_COEF_INDEX
, idx
);
163 snd_hda_codec_write(codec
, spec
->vendor_nid
, 0,
164 AC_VERB_SET_PROC_COEF
, coef
);
174 static int cs_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
175 struct hda_codec
*codec
,
176 struct snd_pcm_substream
*substream
)
178 struct cs_spec
*spec
= codec
->spec
;
179 return snd_hda_multi_out_analog_open(codec
, &spec
->multiout
, substream
,
183 static int cs_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
184 struct hda_codec
*codec
,
185 unsigned int stream_tag
,
187 struct snd_pcm_substream
*substream
)
189 struct cs_spec
*spec
= codec
->spec
;
190 return snd_hda_multi_out_analog_prepare(codec
, &spec
->multiout
,
191 stream_tag
, format
, substream
);
194 static int cs_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
195 struct hda_codec
*codec
,
196 struct snd_pcm_substream
*substream
)
198 struct cs_spec
*spec
= codec
->spec
;
199 return snd_hda_multi_out_analog_cleanup(codec
, &spec
->multiout
);
205 static int cs_dig_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
206 struct hda_codec
*codec
,
207 struct snd_pcm_substream
*substream
)
209 struct cs_spec
*spec
= codec
->spec
;
210 return snd_hda_multi_out_dig_open(codec
, &spec
->multiout
);
213 static int cs_dig_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
214 struct hda_codec
*codec
,
215 struct snd_pcm_substream
*substream
)
217 struct cs_spec
*spec
= codec
->spec
;
218 return snd_hda_multi_out_dig_close(codec
, &spec
->multiout
);
221 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
222 struct hda_codec
*codec
,
223 unsigned int stream_tag
,
225 struct snd_pcm_substream
*substream
)
227 struct cs_spec
*spec
= codec
->spec
;
228 return snd_hda_multi_out_dig_prepare(codec
, &spec
->multiout
, stream_tag
,
232 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
233 struct hda_codec
*codec
,
234 struct snd_pcm_substream
*substream
)
236 struct cs_spec
*spec
= codec
->spec
;
237 return snd_hda_multi_out_dig_cleanup(codec
, &spec
->multiout
);
243 static int cs_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
244 struct hda_codec
*codec
,
245 unsigned int stream_tag
,
247 struct snd_pcm_substream
*substream
)
249 struct cs_spec
*spec
= codec
->spec
;
250 spec
->cur_adc
= spec
->adc_nid
[spec
->cur_input
];
251 spec
->cur_adc_stream_tag
= stream_tag
;
252 spec
->cur_adc_format
= format
;
253 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
, stream_tag
, 0, format
);
257 static int cs_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
258 struct hda_codec
*codec
,
259 struct snd_pcm_substream
*substream
)
261 struct cs_spec
*spec
= codec
->spec
;
262 snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
);
269 static const struct hda_pcm_stream cs_pcm_analog_playback
= {
274 .open
= cs_playback_pcm_open
,
275 .prepare
= cs_playback_pcm_prepare
,
276 .cleanup
= cs_playback_pcm_cleanup
280 static const struct hda_pcm_stream cs_pcm_analog_capture
= {
285 .prepare
= cs_capture_pcm_prepare
,
286 .cleanup
= cs_capture_pcm_cleanup
290 static const struct hda_pcm_stream cs_pcm_digital_playback
= {
295 .open
= cs_dig_playback_pcm_open
,
296 .close
= cs_dig_playback_pcm_close
,
297 .prepare
= cs_dig_playback_pcm_prepare
,
298 .cleanup
= cs_dig_playback_pcm_cleanup
302 static const struct hda_pcm_stream cs_pcm_digital_capture
= {
308 static int cs_build_pcms(struct hda_codec
*codec
)
310 struct cs_spec
*spec
= codec
->spec
;
311 struct hda_pcm
*info
= spec
->pcm_rec
;
313 codec
->pcm_info
= info
;
316 info
->name
= "Cirrus Analog";
317 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] = cs_pcm_analog_playback
;
318 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
= spec
->dac_nid
[0];
319 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].channels_max
=
320 spec
->multiout
.max_channels
;
321 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] = cs_pcm_analog_capture
;
322 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
=
323 spec
->adc_nid
[spec
->cur_input
];
326 if (!spec
->multiout
.dig_out_nid
&& !spec
->dig_in
)
330 info
->name
= "Cirrus Digital";
331 info
->pcm_type
= spec
->autocfg
.dig_out_type
[0];
333 info
->pcm_type
= HDA_PCM_TYPE_SPDIF
;
334 if (spec
->multiout
.dig_out_nid
) {
335 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] =
336 cs_pcm_digital_playback
;
337 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
=
338 spec
->multiout
.dig_out_nid
;
341 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] =
342 cs_pcm_digital_capture
;
343 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
= spec
->dig_in
;
351 * parse codec topology
354 static hda_nid_t
get_dac(struct hda_codec
*codec
, hda_nid_t pin
)
359 if (snd_hda_get_connections(codec
, pin
, &dac
, 1) != 1)
364 static int is_ext_mic(struct hda_codec
*codec
, unsigned int idx
)
366 struct cs_spec
*spec
= codec
->spec
;
367 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
368 hda_nid_t pin
= cfg
->inputs
[idx
].pin
;
370 if (!is_jack_detectable(codec
, pin
))
372 val
= snd_hda_codec_get_pincfg(codec
, pin
);
373 return (snd_hda_get_input_pin_attr(val
) != INPUT_PIN_ATTR_INT
);
376 static hda_nid_t
get_adc(struct hda_codec
*codec
, hda_nid_t pin
,
382 nid
= codec
->start_nid
;
383 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
385 type
= get_wcaps_type(get_wcaps(codec
, nid
));
386 if (type
!= AC_WID_AUD_IN
)
388 idx
= snd_hda_get_conn_index(codec
, nid
, pin
, false);
397 static int is_active_pin(struct hda_codec
*codec
, hda_nid_t nid
)
400 val
= snd_hda_codec_get_pincfg(codec
, nid
);
401 return (get_defcfg_connect(val
) != AC_JACK_PORT_NONE
);
404 static int parse_output(struct hda_codec
*codec
)
406 struct cs_spec
*spec
= codec
->spec
;
407 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
411 for (i
= 0; i
< cfg
->line_outs
; i
++) {
412 dac
= get_dac(codec
, cfg
->line_out_pins
[i
]);
415 spec
->dac_nid
[i
] = dac
;
417 spec
->multiout
.num_dacs
= i
;
418 spec
->multiout
.dac_nids
= spec
->dac_nid
;
419 spec
->multiout
.max_channels
= i
* 2;
421 /* add HP and speakers */
423 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
424 dac
= get_dac(codec
, cfg
->hp_pins
[i
]);
428 spec
->multiout
.hp_nid
= dac
;
430 spec
->multiout
.extra_out_nid
[extra_nids
++] = dac
;
432 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
433 dac
= get_dac(codec
, cfg
->speaker_pins
[i
]);
436 spec
->multiout
.extra_out_nid
[extra_nids
++] = dac
;
439 if (cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
440 cfg
->speaker_outs
= cfg
->line_outs
;
441 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
442 sizeof(cfg
->speaker_pins
));
449 static int parse_input(struct hda_codec
*codec
)
451 struct cs_spec
*spec
= codec
->spec
;
452 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
455 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
456 hda_nid_t pin
= cfg
->inputs
[i
].pin
;
457 spec
->input_idx
[spec
->num_inputs
] = i
;
458 spec
->capsrc_idx
[i
] = spec
->num_inputs
++;
460 spec
->adc_nid
[i
] = get_adc(codec
, pin
, &spec
->adc_idx
[i
]);
462 if (!spec
->num_inputs
)
465 /* check whether the automatic mic switch is available */
466 if (spec
->num_inputs
== 2 &&
467 cfg
->inputs
[0].type
== AUTO_PIN_MIC
&&
468 cfg
->inputs
[1].type
== AUTO_PIN_MIC
) {
469 if (is_ext_mic(codec
, cfg
->inputs
[0].pin
)) {
470 if (!is_ext_mic(codec
, cfg
->inputs
[1].pin
)) {
471 spec
->mic_detect
= 1;
472 spec
->automic_idx
= 0;
475 if (is_ext_mic(codec
, cfg
->inputs
[1].pin
)) {
476 spec
->mic_detect
= 1;
477 spec
->automic_idx
= 1;
485 static int parse_digital_output(struct hda_codec
*codec
)
487 struct cs_spec
*spec
= codec
->spec
;
488 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
493 if (snd_hda_get_connections(codec
, cfg
->dig_out_pins
[0], &nid
, 1) < 1)
495 spec
->multiout
.dig_out_nid
= nid
;
496 spec
->multiout
.share_spdif
= 1;
497 if (cfg
->dig_outs
> 1 &&
498 snd_hda_get_connections(codec
, cfg
->dig_out_pins
[1], &nid
, 1) > 0) {
499 spec
->slave_dig_outs
[0] = nid
;
500 codec
->slave_dig_outs
= spec
->slave_dig_outs
;
505 static int parse_digital_input(struct hda_codec
*codec
)
507 struct cs_spec
*spec
= codec
->spec
;
508 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
512 spec
->dig_in
= get_adc(codec
, cfg
->dig_in_pin
, &idx
);
517 * create mixer controls
520 static const char * const dir_sfx
[2] = { "Playback", "Capture" };
522 static int add_mute(struct hda_codec
*codec
, const char *name
, int index
,
523 unsigned int pval
, int dir
, struct snd_kcontrol
**kctlp
)
526 struct snd_kcontrol_new knew
=
527 HDA_CODEC_MUTE_IDX(tmp
, index
, 0, 0, HDA_OUTPUT
);
528 knew
.private_value
= pval
;
529 snprintf(tmp
, sizeof(tmp
), "%s %s Switch", name
, dir_sfx
[dir
]);
530 *kctlp
= snd_ctl_new1(&knew
, codec
);
531 (*kctlp
)->id
.subdevice
= HDA_SUBDEV_AMP_FLAG
;
532 return snd_hda_ctl_add(codec
, 0, *kctlp
);
535 static int add_volume(struct hda_codec
*codec
, const char *name
,
536 int index
, unsigned int pval
, int dir
,
537 struct snd_kcontrol
**kctlp
)
540 struct snd_kcontrol_new knew
=
541 HDA_CODEC_VOLUME_IDX(tmp
, index
, 0, 0, HDA_OUTPUT
);
542 knew
.private_value
= pval
;
543 snprintf(tmp
, sizeof(tmp
), "%s %s Volume", name
, dir_sfx
[dir
]);
544 *kctlp
= snd_ctl_new1(&knew
, codec
);
545 (*kctlp
)->id
.subdevice
= HDA_SUBDEV_AMP_FLAG
;
546 return snd_hda_ctl_add(codec
, 0, *kctlp
);
549 static void fix_volume_caps(struct hda_codec
*codec
, hda_nid_t dac
)
553 /* set the upper-limit for mixer amp to 0dB */
554 caps
= query_amp_caps(codec
, dac
, HDA_OUTPUT
);
555 caps
&= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT
);
556 caps
|= ((caps
>> AC_AMPCAP_OFFSET_SHIFT
) & 0x7f)
557 << AC_AMPCAP_NUM_STEPS_SHIFT
;
558 snd_hda_override_amp_caps(codec
, dac
, HDA_OUTPUT
, caps
);
561 static int add_vmaster(struct hda_codec
*codec
, hda_nid_t dac
)
563 struct cs_spec
*spec
= codec
->spec
;
568 snd_ctl_make_virtual_master("Master Playback Switch", NULL
);
569 err
= snd_hda_ctl_add(codec
, dac
, spec
->vmaster_sw
);
573 snd_hda_set_vmaster_tlv(codec
, dac
, HDA_OUTPUT
, tlv
);
575 snd_ctl_make_virtual_master("Master Playback Volume", tlv
);
576 err
= snd_hda_ctl_add(codec
, dac
, spec
->vmaster_vol
);
582 static int add_output(struct hda_codec
*codec
, hda_nid_t dac
, int idx
,
583 int num_ctls
, int type
)
585 struct cs_spec
*spec
= codec
->spec
;
588 struct snd_kcontrol
*kctl
;
589 static const char * const speakers
[] = {
590 "Front Speaker", "Surround Speaker", "Bass Speaker"
592 static const char * const line_outs
[] = {
593 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
596 fix_volume_caps(codec
, dac
);
597 if (!spec
->vmaster_sw
) {
598 err
= add_vmaster(codec
, dac
);
605 case AUTO_PIN_HP_OUT
:
609 case AUTO_PIN_SPEAKER_OUT
:
611 name
= speakers
[idx
];
617 name
= line_outs
[idx
];
623 err
= add_mute(codec
, name
, index
,
624 HDA_COMPOSE_AMP_VAL(dac
, 3, 0, HDA_OUTPUT
), 0, &kctl
);
627 err
= snd_ctl_add_slave(spec
->vmaster_sw
, kctl
);
631 err
= add_volume(codec
, name
, index
,
632 HDA_COMPOSE_AMP_VAL(dac
, 3, 0, HDA_OUTPUT
), 0, &kctl
);
635 err
= snd_ctl_add_slave(spec
->vmaster_vol
, kctl
);
642 static int build_output(struct hda_codec
*codec
)
644 struct cs_spec
*spec
= codec
->spec
;
645 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
648 for (i
= 0; i
< cfg
->line_outs
; i
++) {
649 err
= add_output(codec
, get_dac(codec
, cfg
->line_out_pins
[i
]),
650 i
, cfg
->line_outs
, cfg
->line_out_type
);
654 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
655 err
= add_output(codec
, get_dac(codec
, cfg
->hp_pins
[i
]),
656 i
, cfg
->hp_outs
, AUTO_PIN_HP_OUT
);
660 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
661 err
= add_output(codec
, get_dac(codec
, cfg
->speaker_pins
[i
]),
662 i
, cfg
->speaker_outs
, AUTO_PIN_SPEAKER_OUT
);
672 static const struct snd_kcontrol_new cs_capture_ctls
[] = {
673 HDA_BIND_SW("Capture Switch", 0),
674 HDA_BIND_VOL("Capture Volume", 0),
677 static int change_cur_input(struct hda_codec
*codec
, unsigned int idx
,
680 struct cs_spec
*spec
= codec
->spec
;
682 if (spec
->cur_input
== idx
&& !force
)
684 if (spec
->cur_adc
&& spec
->cur_adc
!= spec
->adc_nid
[idx
]) {
685 /* stream is running, let's swap the current ADC */
686 __snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
, 1);
687 spec
->cur_adc
= spec
->adc_nid
[idx
];
688 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
,
689 spec
->cur_adc_stream_tag
, 0,
690 spec
->cur_adc_format
);
692 snd_hda_codec_write(codec
, spec
->cur_adc
, 0,
693 AC_VERB_SET_CONNECT_SEL
,
695 spec
->cur_input
= idx
;
699 static int cs_capture_source_info(struct snd_kcontrol
*kcontrol
,
700 struct snd_ctl_elem_info
*uinfo
)
702 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
703 struct cs_spec
*spec
= codec
->spec
;
704 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
707 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
709 uinfo
->value
.enumerated
.items
= spec
->num_inputs
;
710 if (uinfo
->value
.enumerated
.item
>= spec
->num_inputs
)
711 uinfo
->value
.enumerated
.item
= spec
->num_inputs
- 1;
712 idx
= spec
->input_idx
[uinfo
->value
.enumerated
.item
];
713 strcpy(uinfo
->value
.enumerated
.name
,
714 hda_get_input_pin_label(codec
, cfg
->inputs
[idx
].pin
, 1));
718 static int cs_capture_source_get(struct snd_kcontrol
*kcontrol
,
719 struct snd_ctl_elem_value
*ucontrol
)
721 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
722 struct cs_spec
*spec
= codec
->spec
;
723 ucontrol
->value
.enumerated
.item
[0] = spec
->capsrc_idx
[spec
->cur_input
];
727 static int cs_capture_source_put(struct snd_kcontrol
*kcontrol
,
728 struct snd_ctl_elem_value
*ucontrol
)
730 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
731 struct cs_spec
*spec
= codec
->spec
;
732 unsigned int idx
= ucontrol
->value
.enumerated
.item
[0];
734 if (idx
>= spec
->num_inputs
)
736 idx
= spec
->input_idx
[idx
];
737 return change_cur_input(codec
, idx
, 0);
740 static const struct snd_kcontrol_new cs_capture_source
= {
741 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
742 .name
= "Capture Source",
743 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
744 .info
= cs_capture_source_info
,
745 .get
= cs_capture_source_get
,
746 .put
= cs_capture_source_put
,
749 static const struct hda_bind_ctls
*make_bind_capture(struct hda_codec
*codec
,
750 struct hda_ctl_ops
*ops
)
752 struct cs_spec
*spec
= codec
->spec
;
753 struct hda_bind_ctls
*bind
;
756 bind
= kzalloc(sizeof(*bind
) + sizeof(long) * (spec
->num_inputs
+ 1),
762 for (i
= 0; i
< AUTO_PIN_LAST
; i
++) {
763 if (!spec
->adc_nid
[i
])
766 HDA_COMPOSE_AMP_VAL(spec
->adc_nid
[i
], 3,
767 spec
->adc_idx
[i
], HDA_INPUT
);
772 /* add a (input-boost) volume control to the given input pin */
773 static int add_input_volume_control(struct hda_codec
*codec
,
774 struct auto_pin_cfg
*cfg
,
777 hda_nid_t pin
= cfg
->inputs
[item
].pin
;
780 struct snd_kcontrol
*kctl
;
782 if (!(get_wcaps(codec
, pin
) & AC_WCAP_IN_AMP
))
784 caps
= query_amp_caps(codec
, pin
, HDA_INPUT
);
785 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
788 label
= hda_get_autocfg_input_label(codec
, cfg
, item
);
789 return add_volume(codec
, label
, 0,
790 HDA_COMPOSE_AMP_VAL(pin
, 3, 0, HDA_INPUT
), 1, &kctl
);
793 static int build_input(struct hda_codec
*codec
)
795 struct cs_spec
*spec
= codec
->spec
;
798 if (!spec
->num_inputs
)
801 /* make bind-capture */
802 spec
->capture_bind
[0] = make_bind_capture(codec
, &snd_hda_bind_sw
);
803 spec
->capture_bind
[1] = make_bind_capture(codec
, &snd_hda_bind_vol
);
804 for (i
= 0; i
< 2; i
++) {
805 struct snd_kcontrol
*kctl
;
807 if (!spec
->capture_bind
[i
])
809 kctl
= snd_ctl_new1(&cs_capture_ctls
[i
], codec
);
812 kctl
->private_value
= (long)spec
->capture_bind
[i
];
813 err
= snd_hda_ctl_add(codec
, 0, kctl
);
816 for (n
= 0; n
< AUTO_PIN_LAST
; n
++) {
817 if (!spec
->adc_nid
[n
])
819 err
= snd_hda_add_nid(codec
, kctl
, 0, spec
->adc_nid
[n
]);
825 if (spec
->num_inputs
> 1 && !spec
->mic_detect
) {
826 err
= snd_hda_ctl_add(codec
, 0,
827 snd_ctl_new1(&cs_capture_source
, codec
));
832 for (i
= 0; i
< spec
->num_inputs
; i
++) {
833 err
= add_input_volume_control(codec
, &spec
->autocfg
, i
);
844 static int build_digital_output(struct hda_codec
*codec
)
846 struct cs_spec
*spec
= codec
->spec
;
849 if (!spec
->multiout
.dig_out_nid
)
852 err
= snd_hda_create_spdif_out_ctls(codec
, spec
->multiout
.dig_out_nid
,
853 spec
->multiout
.dig_out_nid
);
856 err
= snd_hda_create_spdif_share_sw(codec
, &spec
->multiout
);
862 static int build_digital_input(struct hda_codec
*codec
)
864 struct cs_spec
*spec
= codec
->spec
;
866 return snd_hda_create_spdif_in_ctls(codec
, spec
->dig_in
);
871 * auto-mute and auto-mic switching
872 * CS421x auto-output redirecting
876 static void cs_automute(struct hda_codec
*codec
)
878 struct cs_spec
*spec
= codec
->spec
;
879 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
880 unsigned int hp_present
;
881 unsigned int spdif_present
;
887 nid
= cfg
->dig_out_pins
[0];
888 if (is_jack_detectable(codec
, nid
)) {
890 TODO: SPDIF output redirect when SENSE_B is enabled.
891 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
894 if (snd_hda_jack_detect(codec
, nid
)
895 /* && spec->sense_b */)
901 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
902 nid
= cfg
->hp_pins
[i
];
903 if (!is_jack_detectable(codec
, nid
))
905 hp_present
= snd_hda_jack_detect(codec
, nid
);
910 /* mute speakers if spdif or hp jack is plugged in */
911 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
912 nid
= cfg
->speaker_pins
[i
];
913 snd_hda_codec_write(codec
, nid
, 0,
914 AC_VERB_SET_PIN_WIDGET_CONTROL
,
915 hp_present
? 0 : PIN_OUT
);
916 /* detect on spdif is specific to CS421x */
917 if (spec
->vendor_nid
== CS421X_VENDOR_NID
) {
918 snd_hda_codec_write(codec
, nid
, 0,
919 AC_VERB_SET_PIN_WIDGET_CONTROL
,
920 spdif_present
? 0 : PIN_OUT
);
923 if (spec
->board_config
== CS420X_MBP53
||
924 spec
->board_config
== CS420X_MBP55
||
925 spec
->board_config
== CS420X_IMAC27
) {
926 unsigned int gpio
= hp_present
? 0x02 : 0x08;
927 snd_hda_codec_write(codec
, 0x01, 0,
928 AC_VERB_SET_GPIO_DATA
, gpio
);
931 /* specific to CS421x */
932 if (spec
->vendor_nid
== CS421X_VENDOR_NID
) {
933 /* mute HPs if spdif jack (SENSE_B) is present */
934 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
935 nid
= cfg
->hp_pins
[i
];
936 snd_hda_codec_write(codec
, nid
, 0,
937 AC_VERB_SET_PIN_WIDGET_CONTROL
,
938 (spdif_present
&& spec
->sense_b
) ? 0 : PIN_HP
);
941 /* SPDIF TX on/off */
943 nid
= cfg
->dig_out_pins
[0];
944 snd_hda_codec_write(codec
, nid
, 0,
945 AC_VERB_SET_PIN_WIDGET_CONTROL
,
946 spdif_present
? PIN_OUT
: 0);
949 /* Update board GPIOs if neccessary ... */
954 * Auto-input redirect for CS421x
955 * Switch max 3 inputs of a single ADC (nid 3)
958 static void cs_automic(struct hda_codec
*codec
)
960 struct cs_spec
*spec
= codec
->spec
;
961 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
963 unsigned int present
;
965 nid
= cfg
->inputs
[spec
->automic_idx
].pin
;
966 present
= snd_hda_jack_detect(codec
, nid
);
968 /* specific to CS421x, single ADC */
969 if (spec
->vendor_nid
== CS421X_VENDOR_NID
) {
971 spec
->last_input
= spec
->cur_input
;
972 spec
->cur_input
= spec
->automic_idx
;
974 spec
->cur_input
= spec
->last_input
;
977 snd_hda_codec_write_cache(codec
, spec
->cur_adc
, 0,
978 AC_VERB_SET_CONNECT_SEL
,
979 spec
->adc_idx
[spec
->cur_input
]);
982 change_cur_input(codec
, spec
->automic_idx
, 0);
984 change_cur_input(codec
, !spec
->automic_idx
, 0);
991 static void init_output(struct hda_codec
*codec
)
993 struct cs_spec
*spec
= codec
->spec
;
994 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
998 for (i
= 0; i
< spec
->multiout
.num_dacs
; i
++)
999 snd_hda_codec_write(codec
, spec
->multiout
.dac_nids
[i
], 0,
1000 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_MUTE
);
1001 if (spec
->multiout
.hp_nid
)
1002 snd_hda_codec_write(codec
, spec
->multiout
.hp_nid
, 0,
1003 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_MUTE
);
1004 for (i
= 0; i
< ARRAY_SIZE(spec
->multiout
.extra_out_nid
); i
++) {
1005 if (!spec
->multiout
.extra_out_nid
[i
])
1007 snd_hda_codec_write(codec
, spec
->multiout
.extra_out_nid
[i
], 0,
1008 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_MUTE
);
1011 /* set appropriate pin controls */
1012 for (i
= 0; i
< cfg
->line_outs
; i
++)
1013 snd_hda_codec_write(codec
, cfg
->line_out_pins
[i
], 0,
1014 AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_OUT
);
1016 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
1017 hda_nid_t nid
= cfg
->hp_pins
[i
];
1018 snd_hda_codec_write(codec
, nid
, 0,
1019 AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_HP
);
1020 if (!cfg
->speaker_outs
)
1022 if (get_wcaps(codec
, nid
) & AC_WCAP_UNSOL_CAP
) {
1023 snd_hda_codec_write(codec
, nid
, 0,
1024 AC_VERB_SET_UNSOLICITED_ENABLE
,
1025 AC_USRSP_EN
| HP_EVENT
);
1026 spec
->hp_detect
= 1;
1031 for (i
= 0; i
< cfg
->speaker_outs
; i
++)
1032 snd_hda_codec_write(codec
, cfg
->speaker_pins
[i
], 0,
1033 AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_OUT
);
1035 /* SPDIF is enabled on presence detect for CS421x */
1036 if (spec
->hp_detect
|| spec
->spdif_detect
)
1040 static void init_input(struct hda_codec
*codec
)
1042 struct cs_spec
*spec
= codec
->spec
;
1043 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1047 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1049 hda_nid_t pin
= cfg
->inputs
[i
].pin
;
1050 if (!spec
->adc_nid
[i
])
1052 /* set appropriate pin control and mute first */
1054 if (cfg
->inputs
[i
].type
== AUTO_PIN_MIC
) {
1055 unsigned int caps
= snd_hda_query_pin_caps(codec
, pin
);
1056 caps
>>= AC_PINCAP_VREF_SHIFT
;
1057 if (caps
& AC_PINCAP_VREF_80
)
1060 snd_hda_codec_write(codec
, pin
, 0,
1061 AC_VERB_SET_PIN_WIDGET_CONTROL
, ctl
);
1062 snd_hda_codec_write(codec
, spec
->adc_nid
[i
], 0,
1063 AC_VERB_SET_AMP_GAIN_MUTE
,
1064 AMP_IN_MUTE(spec
->adc_idx
[i
]));
1065 if (spec
->mic_detect
&& spec
->automic_idx
== i
)
1066 snd_hda_codec_write(codec
, pin
, 0,
1067 AC_VERB_SET_UNSOLICITED_ENABLE
,
1068 AC_USRSP_EN
| MIC_EVENT
);
1070 /* specific to CS421x */
1071 if (spec
->vendor_nid
== CS421X_VENDOR_NID
) {
1072 if (spec
->mic_detect
)
1075 spec
->cur_adc
= spec
->adc_nid
[spec
->cur_input
];
1076 snd_hda_codec_write(codec
, spec
->cur_adc
, 0,
1077 AC_VERB_SET_CONNECT_SEL
,
1078 spec
->adc_idx
[spec
->cur_input
]);
1081 change_cur_input(codec
, spec
->cur_input
, 1);
1082 if (spec
->mic_detect
)
1085 coef
= 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1086 if (is_active_pin(codec
, CS_DMIC2_PIN_NID
))
1087 coef
|= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1088 if (is_active_pin(codec
, CS_DMIC1_PIN_NID
))
1089 coef
|= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1090 * No effect if SPDIF_OUT2 is
1091 * selected in IDX_SPDIF_CTL.
1093 cs_vendor_coef_set(codec
, IDX_ADC_CFG
, coef
);
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
);
1201 static int cs_build_controls(struct hda_codec
*codec
)
1205 err
= build_output(codec
);
1208 err
= build_input(codec
);
1211 err
= build_digital_output(codec
);
1214 err
= build_digital_input(codec
);
1217 return cs_init(codec
);
1220 static void cs_free(struct hda_codec
*codec
)
1222 struct cs_spec
*spec
= codec
->spec
;
1223 kfree(spec
->capture_bind
[0]);
1224 kfree(spec
->capture_bind
[1]);
1228 static void cs_unsol_event(struct hda_codec
*codec
, unsigned int res
)
1230 switch ((res
>> 26) & 0x7f) {
1240 static const struct hda_codec_ops cs_patch_ops
= {
1241 .build_controls
= cs_build_controls
,
1242 .build_pcms
= cs_build_pcms
,
1245 .unsol_event
= cs_unsol_event
,
1248 static int cs_parse_auto_config(struct hda_codec
*codec
)
1250 struct cs_spec
*spec
= codec
->spec
;
1253 err
= snd_hda_parse_pin_def_config(codec
, &spec
->autocfg
, NULL
);
1257 err
= parse_output(codec
);
1260 err
= parse_input(codec
);
1263 err
= parse_digital_output(codec
);
1266 err
= parse_digital_input(codec
);
1272 static const char * const cs420x_models
[CS420X_MODELS
] = {
1273 [CS420X_MBP53
] = "mbp53",
1274 [CS420X_MBP55
] = "mbp55",
1275 [CS420X_IMAC27
] = "imac27",
1276 [CS420X_AUTO
] = "auto",
1280 static const struct snd_pci_quirk cs420x_cfg_tbl
[] = {
1281 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53
),
1282 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55
),
1283 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55
),
1284 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55
),
1285 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27
),
1294 static const struct cs_pincfg mbp53_pincfgs
[] = {
1295 { 0x09, 0x012b4050 },
1296 { 0x0a, 0x90100141 },
1297 { 0x0b, 0x90100140 },
1298 { 0x0c, 0x018b3020 },
1299 { 0x0d, 0x90a00110 },
1300 { 0x0e, 0x400000f0 },
1301 { 0x0f, 0x01cbe030 },
1302 { 0x10, 0x014be060 },
1303 { 0x12, 0x400000f0 },
1304 { 0x15, 0x400000f0 },
1308 static const struct cs_pincfg mbp55_pincfgs
[] = {
1309 { 0x09, 0x012b4030 },
1310 { 0x0a, 0x90100121 },
1311 { 0x0b, 0x90100120 },
1312 { 0x0c, 0x400000f0 },
1313 { 0x0d, 0x90a00110 },
1314 { 0x0e, 0x400000f0 },
1315 { 0x0f, 0x400000f0 },
1316 { 0x10, 0x014be040 },
1317 { 0x12, 0x400000f0 },
1318 { 0x15, 0x400000f0 },
1322 static const struct cs_pincfg imac27_pincfgs
[] = {
1323 { 0x09, 0x012b4050 },
1324 { 0x0a, 0x90100140 },
1325 { 0x0b, 0x90100142 },
1326 { 0x0c, 0x018b3020 },
1327 { 0x0d, 0x90a00110 },
1328 { 0x0e, 0x400000f0 },
1329 { 0x0f, 0x01cbe030 },
1330 { 0x10, 0x014be060 },
1331 { 0x12, 0x01ab9070 },
1332 { 0x15, 0x400000f0 },
1336 static const struct cs_pincfg
*cs_pincfgs
[CS420X_MODELS
] = {
1337 [CS420X_MBP53
] = mbp53_pincfgs
,
1338 [CS420X_MBP55
] = mbp55_pincfgs
,
1339 [CS420X_IMAC27
] = imac27_pincfgs
,
1342 static void fix_pincfg(struct hda_codec
*codec
, int model
,
1343 const struct cs_pincfg
**pin_configs
)
1345 const struct cs_pincfg
*cfg
= pin_configs
[model
];
1348 for (; cfg
->nid
; cfg
++)
1349 snd_hda_codec_set_pincfg(codec
, cfg
->nid
, cfg
->val
);
1352 static int patch_cs420x(struct hda_codec
*codec
)
1354 struct cs_spec
*spec
;
1357 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
1362 spec
->vendor_nid
= CS420X_VENDOR_NID
;
1364 spec
->board_config
=
1365 snd_hda_check_board_config(codec
, CS420X_MODELS
,
1366 cs420x_models
, cs420x_cfg_tbl
);
1367 if (spec
->board_config
>= 0)
1368 fix_pincfg(codec
, spec
->board_config
, cs_pincfgs
);
1370 switch (spec
->board_config
) {
1374 /* GPIO1 = headphones */
1375 /* GPIO3 = speakers */
1376 spec
->gpio_mask
= 0x0a;
1377 spec
->gpio_dir
= 0x0a;
1381 err
= cs_parse_auto_config(codec
);
1385 codec
->patch_ops
= cs_patch_ops
;
1396 * Cirrus Logic CS4210
1398 * 1 DAC => HP(sense) / Speakers,
1399 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1400 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1403 /* CS4210 board names */
1404 static const char *cs421x_models
[CS421X_MODELS
] = {
1405 [CS421X_CDB4210
] = "cdb4210",
1408 static const struct snd_pci_quirk cs421x_cfg_tbl
[] = {
1409 /* Test Intel board + CDB2410 */
1410 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210
),
1414 /* CS4210 board pinconfigs */
1415 /* Default CS4210 (CDB4210)*/
1416 static const struct cs_pincfg cdb4210_pincfgs
[] = {
1417 { 0x05, 0x0321401f },
1418 { 0x06, 0x90170010 },
1419 { 0x07, 0x03813031 },
1420 { 0x08, 0xb7a70037 },
1421 { 0x09, 0xb7a6003e },
1422 { 0x0a, 0x034510f0 },
1426 static const struct cs_pincfg
*cs421x_pincfgs
[CS421X_MODELS
] = {
1427 [CS421X_CDB4210
] = cdb4210_pincfgs
,
1430 static const struct hda_verb cs421x_coef_init_verbs
[] = {
1431 {0x0B, AC_VERB_SET_PROC_STATE
, 1},
1432 {0x0B, AC_VERB_SET_COEF_INDEX
, CS421X_IDX_DEV_CFG
},
1434 Disable Coefficient Index Auto-Increment(DAI)=1,
1437 {0x0B, AC_VERB_SET_PROC_COEF
, 0x0001 },
1439 {0x0B, AC_VERB_SET_COEF_INDEX
, CS421X_IDX_ADC_CFG
},
1440 /* ADC SZCMode = Digital Soft Ramp */
1441 {0x0B, AC_VERB_SET_PROC_COEF
, 0x0002 },
1443 {0x0B, AC_VERB_SET_COEF_INDEX
, CS421X_IDX_DAC_CFG
},
1444 {0x0B, AC_VERB_SET_PROC_COEF
,
1445 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1446 | 0x0004 /* Mute DAC on FIFO error */
1447 | 0x0008 /* Enable DAC High Pass Filter */
1452 /* Errata: CS4210 rev A1 Silicon
1454 * http://www.cirrus.com/en/pubs/errata/
1457 * 1. Performance degredation is present in the ADC.
1458 * 2. Speaker output is not completely muted upon HP detect.
1459 * 3. Noise is present when clipping occurs on the amplified
1463 * The following verb sequence written to the registers during
1464 * initialization will correct the issues listed above.
1467 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes
[] = {
1468 {0x0B, AC_VERB_SET_PROC_STATE
, 0x01}, /* VPW: processing on */
1470 {0x0B, AC_VERB_SET_COEF_INDEX
, 0x0006},
1471 {0x0B, AC_VERB_SET_PROC_COEF
, 0x9999}, /* Test mode: on */
1473 {0x0B, AC_VERB_SET_COEF_INDEX
, 0x000A},
1474 {0x0B, AC_VERB_SET_PROC_COEF
, 0x14CB}, /* Chop double */
1476 {0x0B, AC_VERB_SET_COEF_INDEX
, 0x0011},
1477 {0x0B, AC_VERB_SET_PROC_COEF
, 0xA2D0}, /* Increase ADC current */
1479 {0x0B, AC_VERB_SET_COEF_INDEX
, 0x001A},
1480 {0x0B, AC_VERB_SET_PROC_COEF
, 0x02A9}, /* Mute speaker */
1482 {0x0B, AC_VERB_SET_COEF_INDEX
, 0x001B},
1483 {0x0B, AC_VERB_SET_PROC_COEF
, 0X1006}, /* Remove noise */
1488 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1489 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale
, 900, 300, 0);
1491 static int cs421x_boost_vol_info(struct snd_kcontrol
*kcontrol
,
1492 struct snd_ctl_elem_info
*uinfo
)
1494 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1496 uinfo
->value
.integer
.min
= 0;
1497 uinfo
->value
.integer
.max
= 3;
1501 static int cs421x_boost_vol_get(struct snd_kcontrol
*kcontrol
,
1502 struct snd_ctl_elem_value
*ucontrol
)
1504 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1506 ucontrol
->value
.integer
.value
[0] =
1507 cs_vendor_coef_get(codec
, CS421X_IDX_SPK_CTL
) & 0x0003;
1511 static int cs421x_boost_vol_put(struct snd_kcontrol
*kcontrol
,
1512 struct snd_ctl_elem_value
*ucontrol
)
1514 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1516 unsigned int vol
= ucontrol
->value
.integer
.value
[0];
1518 cs_vendor_coef_get(codec
, CS421X_IDX_SPK_CTL
);
1519 unsigned int original_coef
= coef
;
1522 coef
|= (vol
& 0x0003);
1523 if (original_coef
== coef
)
1526 cs_vendor_coef_set(codec
, CS421X_IDX_SPK_CTL
, coef
);
1531 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl
= {
1533 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1534 .access
= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
1535 SNDRV_CTL_ELEM_ACCESS_TLV_READ
),
1536 .name
= "Speaker Boost Playback Volume",
1537 .info
= cs421x_boost_vol_info
,
1538 .get
= cs421x_boost_vol_get
,
1539 .put
= cs421x_boost_vol_put
,
1540 .tlv
= { .p
= cs421x_speaker_boost_db_scale
},
1543 static void cs421x_pinmux_init(struct hda_codec
*codec
)
1545 struct cs_spec
*spec
= codec
->spec
;
1546 unsigned int def_conf
, coef
;
1548 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1549 coef
= cs_vendor_coef_get(codec
, CS421X_IDX_DEV_CFG
);
1551 if (spec
->gpio_mask
)
1552 coef
|= 0x0008; /* B1,B2 are GPIOs */
1557 coef
|= 0x0010; /* B2 is SENSE_B, not inverted */
1561 cs_vendor_coef_set(codec
, CS421X_IDX_DEV_CFG
, coef
);
1563 if ((spec
->gpio_mask
|| spec
->sense_b
) &&
1564 is_active_pin(codec
, CS421X_DMIC_PIN_NID
)) {
1567 GPIO or SENSE_B forced - disconnect the DMIC pin.
1569 def_conf
= snd_hda_codec_get_pincfg(codec
, CS421X_DMIC_PIN_NID
);
1570 def_conf
&= ~AC_DEFCFG_PORT_CONN
;
1571 def_conf
|= (AC_JACK_PORT_NONE
<< AC_DEFCFG_PORT_CONN_SHIFT
);
1572 snd_hda_codec_set_pincfg(codec
, CS421X_DMIC_PIN_NID
, def_conf
);
1576 static void init_cs421x_digital(struct hda_codec
*codec
)
1578 struct cs_spec
*spec
= codec
->spec
;
1579 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1583 for (i
= 0; i
< cfg
->dig_outs
; i
++) {
1584 hda_nid_t nid
= cfg
->dig_out_pins
[i
];
1585 if (!cfg
->speaker_outs
)
1587 if (get_wcaps(codec
, nid
) & AC_WCAP_UNSOL_CAP
) {
1589 snd_hda_codec_write(codec
, nid
, 0,
1590 AC_VERB_SET_UNSOLICITED_ENABLE
,
1591 AC_USRSP_EN
| SPDIF_EVENT
);
1592 spec
->spdif_detect
= 1;
1597 static int cs421x_init(struct hda_codec
*codec
)
1599 struct cs_spec
*spec
= codec
->spec
;
1601 snd_hda_sequence_write(codec
, cs421x_coef_init_verbs
);
1602 snd_hda_sequence_write(codec
, cs421x_coef_init_verbs_A1_silicon_fixes
);
1604 cs421x_pinmux_init(codec
);
1606 if (spec
->gpio_mask
) {
1607 snd_hda_codec_write(codec
, 0x01, 0, AC_VERB_SET_GPIO_MASK
,
1609 snd_hda_codec_write(codec
, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION
,
1611 snd_hda_codec_write(codec
, 0x01, 0, AC_VERB_SET_GPIO_DATA
,
1617 init_cs421x_digital(codec
);
1623 * CS4210 Input MUX (1 ADC)
1625 static int cs421x_mux_enum_info(struct snd_kcontrol
*kcontrol
,
1626 struct snd_ctl_elem_info
*uinfo
)
1628 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1629 struct cs_spec
*spec
= codec
->spec
;
1631 return snd_hda_input_mux_info(&spec
->input_mux
, uinfo
);
1634 static int cs421x_mux_enum_get(struct snd_kcontrol
*kcontrol
,
1635 struct snd_ctl_elem_value
*ucontrol
)
1637 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1638 struct cs_spec
*spec
= codec
->spec
;
1640 ucontrol
->value
.enumerated
.item
[0] = spec
->cur_input
;
1644 static int cs421x_mux_enum_put(struct snd_kcontrol
*kcontrol
,
1645 struct snd_ctl_elem_value
*ucontrol
)
1647 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1648 struct cs_spec
*spec
= codec
->spec
;
1650 return snd_hda_input_mux_put(codec
, &spec
->input_mux
, ucontrol
,
1651 spec
->adc_nid
[0], &spec
->cur_input
);
1655 static struct snd_kcontrol_new cs421x_capture_source
= {
1657 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1658 .name
= "Capture Source",
1659 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
1660 .info
= cs421x_mux_enum_info
,
1661 .get
= cs421x_mux_enum_get
,
1662 .put
= cs421x_mux_enum_put
,
1665 static int cs421x_add_input_volume_control(struct hda_codec
*codec
, int item
)
1667 struct cs_spec
*spec
= codec
->spec
;
1668 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1669 const struct hda_input_mux
*imux
= &spec
->input_mux
;
1670 hda_nid_t pin
= cfg
->inputs
[item
].pin
;
1671 struct snd_kcontrol
*kctl
;
1674 if (!(get_wcaps(codec
, pin
) & AC_WCAP_IN_AMP
))
1677 caps
= query_amp_caps(codec
, pin
, HDA_INPUT
);
1678 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1682 return add_volume(codec
, imux
->items
[item
].label
, 0,
1683 HDA_COMPOSE_AMP_VAL(pin
, 3, 0, HDA_INPUT
), 1, &kctl
);
1686 /* add a (input-boost) volume control to the given input pin */
1687 static int build_cs421x_input(struct hda_codec
*codec
)
1689 struct cs_spec
*spec
= codec
->spec
;
1690 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1691 struct hda_input_mux
*imux
= &spec
->input_mux
;
1692 int i
, err
, type_idx
;
1695 if (!spec
->num_inputs
)
1698 /* make bind-capture */
1699 spec
->capture_bind
[0] = make_bind_capture(codec
, &snd_hda_bind_sw
);
1700 spec
->capture_bind
[1] = make_bind_capture(codec
, &snd_hda_bind_vol
);
1701 for (i
= 0; i
< 2; i
++) {
1702 struct snd_kcontrol
*kctl
;
1704 if (!spec
->capture_bind
[i
])
1706 kctl
= snd_ctl_new1(&cs_capture_ctls
[i
], codec
);
1709 kctl
->private_value
= (long)spec
->capture_bind
[i
];
1710 err
= snd_hda_ctl_add(codec
, 0, kctl
);
1713 for (n
= 0; n
< AUTO_PIN_LAST
; n
++) {
1714 if (!spec
->adc_nid
[n
])
1716 err
= snd_hda_add_nid(codec
, kctl
, 0, spec
->adc_nid
[n
]);
1722 /* Add Input MUX Items + Capture Volume/Switch */
1723 for (i
= 0; i
< spec
->num_inputs
; i
++) {
1724 label
= hda_get_autocfg_input_label(codec
, cfg
, i
);
1725 snd_hda_add_imux_item(imux
, label
, spec
->adc_idx
[i
], &type_idx
);
1727 err
= cs421x_add_input_volume_control(codec
, i
);
1733 Add 'Capture Source' Switch if
1734 * 2 inputs and no mic detec
1737 if ((spec
->num_inputs
== 2 && !spec
->mic_detect
) ||
1738 (spec
->num_inputs
== 3)) {
1740 err
= snd_hda_ctl_add(codec
, spec
->adc_nid
[0],
1741 snd_ctl_new1(&cs421x_capture_source
, codec
));
1749 /* Single DAC (Mute/Gain) */
1750 static int build_cs421x_output(struct hda_codec
*codec
)
1752 hda_nid_t dac
= CS4210_DAC_NID
;
1753 struct cs_spec
*spec
= codec
->spec
;
1754 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1755 struct snd_kcontrol
*kctl
;
1757 char *name
= "HP/Speakers";
1759 fix_volume_caps(codec
, dac
);
1760 if (!spec
->vmaster_sw
) {
1761 err
= add_vmaster(codec
, dac
);
1766 err
= add_mute(codec
, name
, 0,
1767 HDA_COMPOSE_AMP_VAL(dac
, 3, 0, HDA_OUTPUT
), 0, &kctl
);
1770 err
= snd_ctl_add_slave(spec
->vmaster_sw
, kctl
);
1774 err
= add_volume(codec
, name
, 0,
1775 HDA_COMPOSE_AMP_VAL(dac
, 3, 0, HDA_OUTPUT
), 0, &kctl
);
1778 err
= snd_ctl_add_slave(spec
->vmaster_vol
, kctl
);
1782 if (cfg
->speaker_outs
) {
1783 err
= snd_hda_ctl_add(codec
, 0,
1784 snd_ctl_new1(&cs421x_speaker_bost_ctl
, codec
));
1791 static int cs421x_build_controls(struct hda_codec
*codec
)
1795 err
= build_cs421x_output(codec
);
1798 err
= build_cs421x_input(codec
);
1801 err
= build_digital_output(codec
);
1804 return cs421x_init(codec
);
1807 static void cs421x_unsol_event(struct hda_codec
*codec
, unsigned int res
)
1809 switch ((res
>> 26) & 0x3f) {
1821 static int parse_cs421x_input(struct hda_codec
*codec
)
1823 struct cs_spec
*spec
= codec
->spec
;
1824 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1827 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1828 hda_nid_t pin
= cfg
->inputs
[i
].pin
;
1829 spec
->adc_nid
[i
] = get_adc(codec
, pin
, &spec
->adc_idx
[i
]);
1830 spec
->cur_input
= spec
->last_input
= i
;
1833 /* check whether the automatic mic switch is available */
1834 if (is_ext_mic(codec
, i
) && cfg
->num_inputs
>= 2) {
1835 spec
->mic_detect
= 1;
1836 spec
->automic_idx
= i
;
1842 static int cs421x_parse_auto_config(struct hda_codec
*codec
)
1844 struct cs_spec
*spec
= codec
->spec
;
1847 err
= snd_hda_parse_pin_def_config(codec
, &spec
->autocfg
, NULL
);
1850 err
= parse_output(codec
);
1853 err
= parse_cs421x_input(codec
);
1856 err
= parse_digital_output(codec
);
1864 Manage PDREF, when transitioning to D3hot
1865 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1867 static int cs421x_suspend(struct hda_codec
*codec
, pm_message_t state
)
1871 snd_hda_shutup_pins(codec
);
1873 snd_hda_codec_write(codec
, CS4210_DAC_NID
, 0,
1874 AC_VERB_SET_POWER_STATE
, AC_PWRST_D3
);
1875 snd_hda_codec_write(codec
, CS4210_ADC_NID
, 0,
1876 AC_VERB_SET_POWER_STATE
, AC_PWRST_D3
);
1878 coef
= cs_vendor_coef_get(codec
, CS421X_IDX_DEV_CFG
);
1879 coef
|= 0x0004; /* PDREF */
1880 cs_vendor_coef_set(codec
, CS421X_IDX_DEV_CFG
, coef
);
1886 static struct hda_codec_ops cs4210_patch_ops
= {
1887 .build_controls
= cs421x_build_controls
,
1888 .build_pcms
= cs_build_pcms
,
1889 .init
= cs421x_init
,
1891 .unsol_event
= cs421x_unsol_event
,
1893 .suspend
= cs421x_suspend
,
1897 static int patch_cs421x(struct hda_codec
*codec
)
1899 struct cs_spec
*spec
;
1902 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
1907 spec
->vendor_nid
= CS421X_VENDOR_NID
;
1909 spec
->board_config
=
1910 snd_hda_check_board_config(codec
, CS421X_MODELS
,
1911 cs421x_models
, cs421x_cfg_tbl
);
1912 if (spec
->board_config
>= 0)
1913 fix_pincfg(codec
, spec
->board_config
, cs421x_pincfgs
);
1915 Setup GPIO/SENSE for each board (if used)
1917 switch (spec
->board_config
) {
1918 case CS421X_CDB4210
:
1919 snd_printd("CS4210 board: %s\n",
1920 cs421x_models
[spec
->board_config
]);
1921 /* spec->gpio_mask = 3;
1923 spec->gpio_data = 3;
1931 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1932 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1935 cs421x_pinmux_init(codec
);
1937 err
= cs421x_parse_auto_config(codec
);
1941 codec
->patch_ops
= cs4210_patch_ops
;
1955 static const struct hda_codec_preset snd_hda_preset_cirrus
[] = {
1956 { .id
= 0x10134206, .name
= "CS4206", .patch
= patch_cs420x
},
1957 { .id
= 0x10134207, .name
= "CS4207", .patch
= patch_cs420x
},
1958 { .id
= 0x10134210, .name
= "CS4210", .patch
= patch_cs421x
},
1962 MODULE_ALIAS("snd-hda-codec-id:10134206");
1963 MODULE_ALIAS("snd-hda-codec-id:10134207");
1964 MODULE_ALIAS("snd-hda-codec-id:10134210");
1966 MODULE_LICENSE("GPL");
1967 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1969 static struct hda_codec_preset_list cirrus_list
= {
1970 .preset
= snd_hda_preset_cirrus
,
1971 .owner
= THIS_MODULE
,
1974 static int __init
patch_cirrus_init(void)
1976 return snd_hda_add_codec_preset(&cirrus_list
);
1979 static void __exit
patch_cirrus_exit(void)
1981 snd_hda_delete_codec_preset(&cirrus_list
);
1984 module_init(patch_cirrus_init
)
1985 module_exit(patch_cirrus_exit
)