pch_phub: Improve ADE(Address Decode Enable) control
[zen-stable.git] / sound / pci / hda / patch_cirrus.c
blob2a2d8645ba0933e8e9e20ef3bd33a656f1bb69c5
1 /*
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>
34 struct cs_spec {
35 int board_config;
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;
51 hda_nid_t cur_adc;
52 unsigned int cur_adc_stream_tag;
53 unsigned int cur_adc_format;
54 hda_nid_t dig_in;
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;
66 /* CS421x */
67 unsigned int spdif_detect:1;
68 unsigned int sense_b:1;
69 hda_nid_t vendor_nid;
70 struct hda_input_mux input_mux;
71 unsigned int last_input;
74 /* available models with CS420x */
75 enum {
76 CS420X_MBP53,
77 CS420X_MBP55,
78 CS420X_IMAC27,
79 CS420X_AUTO,
80 CS420X_MODELS
83 /* CS421x boards */
84 enum {
85 CS421X_CDB4210,
86 CS421X_MODELS
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
96 /* coef indices */
97 #define IDX_SPDIF_STAT 0x0000
98 #define IDX_SPDIF_CTL 0x0001
99 #define IDX_ADC_CFG 0x0002
100 /* SZC bitmask, 4 modes below:
101 * 0 = immediate,
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:
114 * 0 = Immediate
115 * 1 = zero-cross
116 * 2 = soft-ramp
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,
158 unsigned int coef)
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);
168 #define HP_EVENT 1
169 #define MIC_EVENT 2
172 * PCM callbacks
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,
180 hinfo);
183 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
184 struct hda_codec *codec,
185 unsigned int stream_tag,
186 unsigned int format,
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);
203 * Digital out
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,
224 unsigned int format,
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,
229 format, substream);
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);
241 * Analog capture
243 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
244 struct hda_codec *codec,
245 unsigned int stream_tag,
246 unsigned int format,
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);
254 return 0;
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);
263 spec->cur_adc = 0;
264 return 0;
269 static const struct hda_pcm_stream cs_pcm_analog_playback = {
270 .substreams = 1,
271 .channels_min = 2,
272 .channels_max = 2,
273 .ops = {
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 = {
281 .substreams = 1,
282 .channels_min = 2,
283 .channels_max = 2,
284 .ops = {
285 .prepare = cs_capture_pcm_prepare,
286 .cleanup = cs_capture_pcm_cleanup
290 static const struct hda_pcm_stream cs_pcm_digital_playback = {
291 .substreams = 1,
292 .channels_min = 2,
293 .channels_max = 2,
294 .ops = {
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 = {
303 .substreams = 1,
304 .channels_min = 2,
305 .channels_max = 2,
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;
314 codec->num_pcms = 0;
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];
324 codec->num_pcms++;
326 if (!spec->multiout.dig_out_nid && !spec->dig_in)
327 return 0;
329 info++;
330 info->name = "Cirrus Digital";
331 info->pcm_type = spec->autocfg.dig_out_type[0];
332 if (!info->pcm_type)
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;
340 if (spec->dig_in) {
341 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
342 cs_pcm_digital_capture;
343 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
345 codec->num_pcms++;
347 return 0;
351 * parse codec topology
354 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
356 hda_nid_t dac;
357 if (!pin)
358 return 0;
359 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
360 return 0;
361 return dac;
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;
369 unsigned int val;
370 if (!is_jack_detectable(codec, pin))
371 return 0;
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,
377 unsigned int *idxp)
379 int i, idx;
380 hda_nid_t nid;
382 nid = codec->start_nid;
383 for (i = 0; i < codec->num_nodes; i++, nid++) {
384 unsigned int type;
385 type = get_wcaps_type(get_wcaps(codec, nid));
386 if (type != AC_WID_AUD_IN)
387 continue;
388 idx = snd_hda_get_conn_index(codec, nid, pin, false);
389 if (idx >= 0) {
390 *idxp = idx;
391 return nid;
394 return 0;
397 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
399 unsigned int val;
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;
408 int i, extra_nids;
409 hda_nid_t dac;
411 for (i = 0; i < cfg->line_outs; i++) {
412 dac = get_dac(codec, cfg->line_out_pins[i]);
413 if (!dac)
414 break;
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 */
422 extra_nids = 0;
423 for (i = 0; i < cfg->hp_outs; i++) {
424 dac = get_dac(codec, cfg->hp_pins[i]);
425 if (!dac)
426 break;
427 if (!i)
428 spec->multiout.hp_nid = dac;
429 else
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]);
434 if (!dac)
435 break;
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));
443 cfg->line_outs = 0;
446 return 0;
449 static int parse_input(struct hda_codec *codec)
451 struct cs_spec *spec = codec->spec;
452 struct auto_pin_cfg *cfg = &spec->autocfg;
453 int i;
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++;
459 spec->cur_input = i;
460 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
462 if (!spec->num_inputs)
463 return 0;
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;
474 } else {
475 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
476 spec->mic_detect = 1;
477 spec->automic_idx = 1;
481 return 0;
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;
489 hda_nid_t nid;
491 if (!cfg->dig_outs)
492 return 0;
493 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
494 return 0;
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;
502 return 0;
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;
509 int idx;
511 if (cfg->dig_in_pin)
512 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
513 return 0;
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)
525 char tmp[44];
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)
539 char tmp[44];
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)
551 unsigned int caps;
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;
564 unsigned int tlv[4];
565 int err;
567 spec->vmaster_sw =
568 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
569 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
570 if (err < 0)
571 return err;
573 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
574 spec->vmaster_vol =
575 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
576 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
577 if (err < 0)
578 return err;
579 return 0;
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;
586 const char *name;
587 int err, index;
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);
599 if (err < 0)
600 return err;
603 index = 0;
604 switch (type) {
605 case AUTO_PIN_HP_OUT:
606 name = "Headphone";
607 index = idx;
608 break;
609 case AUTO_PIN_SPEAKER_OUT:
610 if (num_ctls > 1)
611 name = speakers[idx];
612 else
613 name = "Speaker";
614 break;
615 default:
616 if (num_ctls > 1)
617 name = line_outs[idx];
618 else
619 name = "Line-Out";
620 break;
623 err = add_mute(codec, name, index,
624 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
625 if (err < 0)
626 return err;
627 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
628 if (err < 0)
629 return err;
631 err = add_volume(codec, name, index,
632 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
633 if (err < 0)
634 return err;
635 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
636 if (err < 0)
637 return err;
639 return 0;
642 static int build_output(struct hda_codec *codec)
644 struct cs_spec *spec = codec->spec;
645 struct auto_pin_cfg *cfg = &spec->autocfg;
646 int i, err;
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);
651 if (err < 0)
652 return err;
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);
657 if (err < 0)
658 return err;
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);
663 if (err < 0)
664 return err;
666 return 0;
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,
678 int force)
680 struct cs_spec *spec = codec->spec;
682 if (spec->cur_input == idx && !force)
683 return 0;
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,
694 spec->adc_idx[idx]);
695 spec->cur_input = idx;
696 return 1;
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;
705 unsigned int idx;
707 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
708 uinfo->count = 1;
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));
715 return 0;
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];
724 return 0;
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)
735 return -EINVAL;
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;
754 int i, n;
756 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
757 GFP_KERNEL);
758 if (!bind)
759 return NULL;
760 bind->ops = ops;
761 n = 0;
762 for (i = 0; i < AUTO_PIN_LAST; i++) {
763 if (!spec->adc_nid[i])
764 continue;
765 bind->values[n++] =
766 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
767 spec->adc_idx[i], HDA_INPUT);
769 return bind;
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,
775 int item)
777 hda_nid_t pin = cfg->inputs[item].pin;
778 u32 caps;
779 const char *label;
780 struct snd_kcontrol *kctl;
782 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
783 return 0;
784 caps = query_amp_caps(codec, pin, HDA_INPUT);
785 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
786 if (caps <= 1)
787 return 0;
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;
796 int i, err;
798 if (!spec->num_inputs)
799 return 0;
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;
806 int n;
807 if (!spec->capture_bind[i])
808 return -ENOMEM;
809 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
810 if (!kctl)
811 return -ENOMEM;
812 kctl->private_value = (long)spec->capture_bind[i];
813 err = snd_hda_ctl_add(codec, 0, kctl);
814 if (err < 0)
815 return err;
816 for (n = 0; n < AUTO_PIN_LAST; n++) {
817 if (!spec->adc_nid[n])
818 continue;
819 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
820 if (err < 0)
821 return err;
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));
828 if (err < 0)
829 return err;
832 for (i = 0; i < spec->num_inputs; i++) {
833 err = add_input_volume_control(codec, &spec->autocfg, i);
834 if (err < 0)
835 return err;
838 return 0;
844 static int build_digital_output(struct hda_codec *codec)
846 struct cs_spec *spec = codec->spec;
847 int err;
849 if (!spec->multiout.dig_out_nid)
850 return 0;
852 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
853 spec->multiout.dig_out_nid);
854 if (err < 0)
855 return err;
856 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
857 if (err < 0)
858 return err;
859 return 0;
862 static int build_digital_input(struct hda_codec *codec)
864 struct cs_spec *spec = codec->spec;
865 if (spec->dig_in)
866 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
867 return 0;
871 * auto-mute and auto-mic switching
872 * CS421x auto-output redirecting
873 * HP/SPK/SPDIF
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;
882 hda_nid_t nid;
883 int i;
885 spdif_present = 0;
886 if (cfg->dig_outs) {
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)
892 assumed.
894 if (snd_hda_jack_detect(codec, nid)
895 /* && spec->sense_b */)
896 spdif_present = 1;
900 hp_present = 0;
901 for (i = 0; i < cfg->hp_outs; i++) {
902 nid = cfg->hp_pins[i];
903 if (!is_jack_detectable(codec, nid))
904 continue;
905 hp_present = snd_hda_jack_detect(codec, nid);
906 if (hp_present)
907 break;
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 */
942 if (cfg->dig_outs) {
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;
962 hda_nid_t nid;
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) {
970 if (present) {
971 spec->last_input = spec->cur_input;
972 spec->cur_input = spec->automic_idx;
973 } else {
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]);
980 } else {
981 if (present)
982 change_cur_input(codec, spec->automic_idx, 0);
983 else
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;
995 int i;
997 /* mute first */
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])
1006 break;
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);
1015 /* HP */
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)
1021 continue;
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;
1030 /* Speaker */
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)
1037 cs_automute(codec);
1040 static void init_input(struct hda_codec *codec)
1042 struct cs_spec *spec = codec->spec;
1043 struct auto_pin_cfg *cfg = &spec->autocfg;
1044 unsigned int coef;
1045 int i;
1047 for (i = 0; i < cfg->num_inputs; i++) {
1048 unsigned int ctl;
1049 hda_nid_t pin = cfg->inputs[i].pin;
1050 if (!spec->adc_nid[i])
1051 continue;
1052 /* set appropriate pin control and mute first */
1053 ctl = PIN_IN;
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)
1058 ctl = PIN_VREF80;
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)
1073 cs_automic(codec);
1074 else {
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]);
1080 } else {
1081 change_cur_input(codec, spec->cur_input, 1);
1082 if (spec->mic_detect)
1083 cs_automic(codec);
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 */
1106 /* Beep */
1107 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1108 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1110 {} /* terminator */
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
1123 * initialized.
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 */
1157 #endif
1159 {} /* terminator */
1162 /* SPDIF setup */
1163 static void init_digital(struct hda_codec *codec)
1165 unsigned int coef;
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
1172 * DMIC_SDA2.
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,
1188 spec->gpio_mask);
1189 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1190 spec->gpio_dir);
1191 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1192 spec->gpio_data);
1195 init_output(codec);
1196 init_input(codec);
1197 init_digital(codec);
1198 return 0;
1201 static int cs_build_controls(struct hda_codec *codec)
1203 int err;
1205 err = build_output(codec);
1206 if (err < 0)
1207 return err;
1208 err = build_input(codec);
1209 if (err < 0)
1210 return err;
1211 err = build_digital_output(codec);
1212 if (err < 0)
1213 return err;
1214 err = build_digital_input(codec);
1215 if (err < 0)
1216 return err;
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]);
1225 kfree(codec->spec);
1228 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1230 switch ((res >> 26) & 0x7f) {
1231 case HP_EVENT:
1232 cs_automute(codec);
1233 break;
1234 case MIC_EVENT:
1235 cs_automic(codec);
1236 break;
1240 static const struct hda_codec_ops cs_patch_ops = {
1241 .build_controls = cs_build_controls,
1242 .build_pcms = cs_build_pcms,
1243 .init = cs_init,
1244 .free = cs_free,
1245 .unsol_event = cs_unsol_event,
1248 static int cs_parse_auto_config(struct hda_codec *codec)
1250 struct cs_spec *spec = codec->spec;
1251 int err;
1253 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1254 if (err < 0)
1255 return err;
1257 err = parse_output(codec);
1258 if (err < 0)
1259 return err;
1260 err = parse_input(codec);
1261 if (err < 0)
1262 return err;
1263 err = parse_digital_output(codec);
1264 if (err < 0)
1265 return err;
1266 err = parse_digital_input(codec);
1267 if (err < 0)
1268 return err;
1269 return 0;
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),
1286 {} /* terminator */
1289 struct cs_pincfg {
1290 hda_nid_t nid;
1291 u32 val;
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 },
1305 {} /* terminator */
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 },
1319 {} /* terminator */
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 },
1333 {} /* terminator */
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];
1346 if (!cfg)
1347 return;
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;
1355 int err;
1357 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1358 if (!spec)
1359 return -ENOMEM;
1360 codec->spec = spec;
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) {
1371 case CS420X_IMAC27:
1372 case CS420X_MBP53:
1373 case CS420X_MBP55:
1374 /* GPIO1 = headphones */
1375 /* GPIO3 = speakers */
1376 spec->gpio_mask = 0x0a;
1377 spec->gpio_dir = 0x0a;
1378 break;
1381 err = cs_parse_auto_config(codec);
1382 if (err < 0)
1383 goto error;
1385 codec->patch_ops = cs_patch_ops;
1387 return 0;
1389 error:
1390 kfree(codec->spec);
1391 codec->spec = NULL;
1392 return err;
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),
1411 {} /* terminator */
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 },
1423 {} /* terminator */
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,
1435 PDREF=0
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 */
1449 {} /* terminator */
1452 /* Errata: CS4210 rev A1 Silicon
1454 * http://www.cirrus.com/en/pubs/errata/
1456 * Description:
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
1460 * speaker outputs.
1462 * Workaround:
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 */
1485 {} /* terminator */
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;
1495 uinfo->count = 1;
1496 uinfo->value.integer.min = 0;
1497 uinfo->value.integer.max = 3;
1498 return 0;
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;
1508 return 0;
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];
1517 unsigned int coef =
1518 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1519 unsigned int original_coef = coef;
1521 coef &= ~0x0003;
1522 coef |= (vol & 0x0003);
1523 if (original_coef == coef)
1524 return 0;
1525 else {
1526 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1527 return 1;
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 */
1553 else
1554 coef &= ~0x0008;
1556 if (spec->sense_b)
1557 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1558 else
1559 coef &= ~0x0010;
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;
1580 int i;
1583 for (i = 0; i < cfg->dig_outs; i++) {
1584 hda_nid_t nid = cfg->dig_out_pins[i];
1585 if (!cfg->speaker_outs)
1586 continue;
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,
1608 spec->gpio_mask);
1609 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1610 spec->gpio_dir);
1611 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1612 spec->gpio_data);
1615 init_output(codec);
1616 init_input(codec);
1617 init_cs421x_digital(codec);
1619 return 0;
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;
1641 return 0;
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;
1672 u32 caps;
1674 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1675 return 0;
1677 caps = query_amp_caps(codec, pin, HDA_INPUT);
1678 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1679 if (caps <= 1)
1680 return 0;
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;
1693 const char *label;
1695 if (!spec->num_inputs)
1696 return 0;
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;
1703 int n;
1704 if (!spec->capture_bind[i])
1705 return -ENOMEM;
1706 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1707 if (!kctl)
1708 return -ENOMEM;
1709 kctl->private_value = (long)spec->capture_bind[i];
1710 err = snd_hda_ctl_add(codec, 0, kctl);
1711 if (err < 0)
1712 return err;
1713 for (n = 0; n < AUTO_PIN_LAST; n++) {
1714 if (!spec->adc_nid[n])
1715 continue;
1716 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1717 if (err < 0)
1718 return err;
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);
1728 if (err < 0)
1729 return err;
1733 Add 'Capture Source' Switch if
1734 * 2 inputs and no mic detec
1735 * 3 inputs
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));
1742 if (err < 0)
1743 return err;
1746 return 0;
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;
1756 int err;
1757 char *name = "HP/Speakers";
1759 fix_volume_caps(codec, dac);
1760 if (!spec->vmaster_sw) {
1761 err = add_vmaster(codec, dac);
1762 if (err < 0)
1763 return err;
1766 err = add_mute(codec, name, 0,
1767 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1768 if (err < 0)
1769 return err;
1770 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1771 if (err < 0)
1772 return err;
1774 err = add_volume(codec, name, 0,
1775 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1776 if (err < 0)
1777 return err;
1778 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1779 if (err < 0)
1780 return err;
1782 if (cfg->speaker_outs) {
1783 err = snd_hda_ctl_add(codec, 0,
1784 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1785 if (err < 0)
1786 return err;
1788 return err;
1791 static int cs421x_build_controls(struct hda_codec *codec)
1793 int err;
1795 err = build_cs421x_output(codec);
1796 if (err < 0)
1797 return err;
1798 err = build_cs421x_input(codec);
1799 if (err < 0)
1800 return err;
1801 err = build_digital_output(codec);
1802 if (err < 0)
1803 return err;
1804 return cs421x_init(codec);
1807 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1809 switch ((res >> 26) & 0x3f) {
1810 case HP_EVENT:
1811 case SPDIF_EVENT:
1812 cs_automute(codec);
1813 break;
1815 case MIC_EVENT:
1816 cs_automic(codec);
1817 break;
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;
1825 int i;
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;
1831 spec->num_inputs++;
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;
1839 return 0;
1842 static int cs421x_parse_auto_config(struct hda_codec *codec)
1844 struct cs_spec *spec = codec->spec;
1845 int err;
1847 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1848 if (err < 0)
1849 return err;
1850 err = parse_output(codec);
1851 if (err < 0)
1852 return err;
1853 err = parse_cs421x_input(codec);
1854 if (err < 0)
1855 return err;
1856 err = parse_digital_output(codec);
1857 if (err < 0)
1858 return err;
1859 return 0;
1862 #ifdef CONFIG_PM
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)
1869 unsigned int coef;
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);
1882 return 0;
1884 #endif
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,
1890 .free = cs_free,
1891 .unsol_event = cs421x_unsol_event,
1892 #ifdef CONFIG_PM
1893 .suspend = cs421x_suspend,
1894 #endif
1897 static int patch_cs421x(struct hda_codec *codec)
1899 struct cs_spec *spec;
1900 int err;
1902 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1903 if (!spec)
1904 return -ENOMEM;
1905 codec->spec = spec;
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;
1922 spec->gpio_dir = 3;
1923 spec->gpio_data = 3;
1925 spec->sense_b = 1;
1927 break;
1931 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1932 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1933 is disabled.
1935 cs421x_pinmux_init(codec);
1937 err = cs421x_parse_auto_config(codec);
1938 if (err < 0)
1939 goto error;
1941 codec->patch_ops = cs4210_patch_ops;
1943 return 0;
1945 error:
1946 kfree(codec->spec);
1947 codec->spec = NULL;
1948 return err;
1953 * patch entries
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 },
1959 {} /* terminator */
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)