MAINTAINERS: stable: Update address
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / pci / hda / patch_cirrus.c
blob4346ad2c9062123a08140aef23a7f3b1cb181e5c
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 <sound/core.h>
26 #include "hda_codec.h"
27 #include "hda_local.h"
28 #include <sound/tlv.h>
33 struct cs_spec {
34 int board_config;
35 struct auto_pin_cfg autocfg;
36 struct hda_multi_out multiout;
37 struct snd_kcontrol *vmaster_sw;
38 struct snd_kcontrol *vmaster_vol;
40 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
41 hda_nid_t slave_dig_outs[2];
43 unsigned int input_idx[AUTO_PIN_LAST];
44 unsigned int capsrc_idx[AUTO_PIN_LAST];
45 hda_nid_t adc_nid[AUTO_PIN_LAST];
46 unsigned int adc_idx[AUTO_PIN_LAST];
47 unsigned int num_inputs;
48 unsigned int cur_input;
49 unsigned int automic_idx;
50 hda_nid_t cur_adc;
51 unsigned int cur_adc_stream_tag;
52 unsigned int cur_adc_format;
53 hda_nid_t dig_in;
55 const struct hda_bind_ctls *capture_bind[2];
57 unsigned int gpio_mask;
58 unsigned int gpio_dir;
59 unsigned int gpio_data;
61 struct hda_pcm pcm_rec[2]; /* PCM information */
63 unsigned int hp_detect:1;
64 unsigned int mic_detect:1;
65 /* CS421x */
66 unsigned int spdif_detect:1;
67 unsigned int sense_b:1;
68 hda_nid_t vendor_nid;
69 struct hda_input_mux input_mux;
70 unsigned int last_input;
73 /* available models with CS420x */
74 enum {
75 CS420X_MBP53,
76 CS420X_MBP55,
77 CS420X_IMAC27,
78 CS420X_AUTO,
79 CS420X_MODELS
82 /* CS421x boards */
83 enum {
84 CS421X_CDB4210,
85 CS421X_MODELS
88 /* Vendor-specific processing widget */
89 #define CS420X_VENDOR_NID 0x11
90 #define CS_DIG_OUT1_PIN_NID 0x10
91 #define CS_DIG_OUT2_PIN_NID 0x15
92 #define CS_DMIC1_PIN_NID 0x12
93 #define CS_DMIC2_PIN_NID 0x0e
95 /* coef indices */
96 #define IDX_SPDIF_STAT 0x0000
97 #define IDX_SPDIF_CTL 0x0001
98 #define IDX_ADC_CFG 0x0002
99 /* SZC bitmask, 4 modes below:
100 * 0 = immediate,
101 * 1 = digital immediate, analog zero-cross
102 * 2 = digtail & analog soft-ramp
103 * 3 = digital soft-ramp, analog zero-cross
105 #define CS_COEF_ADC_SZC_MASK (3 << 0)
106 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
107 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
108 /* PGA mode: 0 = differential, 1 = signle-ended */
109 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
110 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
111 #define IDX_DAC_CFG 0x0003
112 /* SZC bitmask, 4 modes below:
113 * 0 = Immediate
114 * 1 = zero-cross
115 * 2 = soft-ramp
116 * 3 = soft-ramp on zero-cross
118 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
119 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
120 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
122 #define IDX_BEEP_CFG 0x0004
123 /* 0x0008 - test reg key */
124 /* 0x0009 - 0x0014 -> 12 test regs */
125 /* 0x0015 - visibility reg */
128 * Cirrus Logic CS4210
130 * 1 DAC => HP(sense) / Speakers,
131 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
132 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
134 #define CS4210_DAC_NID 0x02
135 #define CS4210_ADC_NID 0x03
136 #define CS421X_VENDOR_NID 0x0B
137 #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
138 #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
140 #define CS421X_IDX_DEV_CFG 0x01
141 #define CS421X_IDX_ADC_CFG 0x02
142 #define CS421X_IDX_DAC_CFG 0x03
143 #define CS421X_IDX_SPK_CTL 0x04
145 #define SPDIF_EVENT 0x04
147 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
149 struct cs_spec *spec = codec->spec;
150 snd_hda_codec_write(codec, spec->vendor_nid, 0,
151 AC_VERB_SET_COEF_INDEX, idx);
152 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
153 AC_VERB_GET_PROC_COEF, 0);
156 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
157 unsigned int coef)
159 struct cs_spec *spec = codec->spec;
160 snd_hda_codec_write(codec, spec->vendor_nid, 0,
161 AC_VERB_SET_COEF_INDEX, idx);
162 snd_hda_codec_write(codec, spec->vendor_nid, 0,
163 AC_VERB_SET_PROC_COEF, coef);
167 #define HP_EVENT 1
168 #define MIC_EVENT 2
171 * PCM callbacks
173 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
174 struct hda_codec *codec,
175 struct snd_pcm_substream *substream)
177 struct cs_spec *spec = codec->spec;
178 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
179 hinfo);
182 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
183 struct hda_codec *codec,
184 unsigned int stream_tag,
185 unsigned int format,
186 struct snd_pcm_substream *substream)
188 struct cs_spec *spec = codec->spec;
189 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
190 stream_tag, format, substream);
193 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
194 struct hda_codec *codec,
195 struct snd_pcm_substream *substream)
197 struct cs_spec *spec = codec->spec;
198 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
202 * Digital out
204 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
205 struct hda_codec *codec,
206 struct snd_pcm_substream *substream)
208 struct cs_spec *spec = codec->spec;
209 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
212 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
213 struct hda_codec *codec,
214 struct snd_pcm_substream *substream)
216 struct cs_spec *spec = codec->spec;
217 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
220 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
221 struct hda_codec *codec,
222 unsigned int stream_tag,
223 unsigned int format,
224 struct snd_pcm_substream *substream)
226 struct cs_spec *spec = codec->spec;
227 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
228 format, substream);
231 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
232 struct hda_codec *codec,
233 struct snd_pcm_substream *substream)
235 struct cs_spec *spec = codec->spec;
236 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
239 static void cs_update_input_select(struct hda_codec *codec)
241 struct cs_spec *spec = codec->spec;
242 if (spec->cur_adc)
243 snd_hda_codec_write(codec, spec->cur_adc, 0,
244 AC_VERB_SET_CONNECT_SEL,
245 spec->adc_idx[spec->cur_input]);
249 * Analog capture
251 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
252 struct hda_codec *codec,
253 unsigned int stream_tag,
254 unsigned int format,
255 struct snd_pcm_substream *substream)
257 struct cs_spec *spec = codec->spec;
258 spec->cur_adc = spec->adc_nid[spec->cur_input];
259 spec->cur_adc_stream_tag = stream_tag;
260 spec->cur_adc_format = format;
261 cs_update_input_select(codec);
262 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
263 return 0;
266 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
267 struct hda_codec *codec,
268 struct snd_pcm_substream *substream)
270 struct cs_spec *spec = codec->spec;
271 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
272 spec->cur_adc = 0;
273 return 0;
278 static const struct hda_pcm_stream cs_pcm_analog_playback = {
279 .substreams = 1,
280 .channels_min = 2,
281 .channels_max = 2,
282 .ops = {
283 .open = cs_playback_pcm_open,
284 .prepare = cs_playback_pcm_prepare,
285 .cleanup = cs_playback_pcm_cleanup
289 static const struct hda_pcm_stream cs_pcm_analog_capture = {
290 .substreams = 1,
291 .channels_min = 2,
292 .channels_max = 2,
293 .ops = {
294 .prepare = cs_capture_pcm_prepare,
295 .cleanup = cs_capture_pcm_cleanup
299 static const struct hda_pcm_stream cs_pcm_digital_playback = {
300 .substreams = 1,
301 .channels_min = 2,
302 .channels_max = 2,
303 .ops = {
304 .open = cs_dig_playback_pcm_open,
305 .close = cs_dig_playback_pcm_close,
306 .prepare = cs_dig_playback_pcm_prepare,
307 .cleanup = cs_dig_playback_pcm_cleanup
311 static const struct hda_pcm_stream cs_pcm_digital_capture = {
312 .substreams = 1,
313 .channels_min = 2,
314 .channels_max = 2,
317 static int cs_build_pcms(struct hda_codec *codec)
319 struct cs_spec *spec = codec->spec;
320 struct hda_pcm *info = spec->pcm_rec;
322 codec->pcm_info = info;
323 codec->num_pcms = 0;
325 info->name = "Cirrus Analog";
326 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
327 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
328 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
329 spec->multiout.max_channels;
330 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
331 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
332 spec->adc_nid[spec->cur_input];
333 codec->num_pcms++;
335 if (!spec->multiout.dig_out_nid && !spec->dig_in)
336 return 0;
338 info++;
339 info->name = "Cirrus Digital";
340 info->pcm_type = spec->autocfg.dig_out_type[0];
341 if (!info->pcm_type)
342 info->pcm_type = HDA_PCM_TYPE_SPDIF;
343 if (spec->multiout.dig_out_nid) {
344 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
345 cs_pcm_digital_playback;
346 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
347 spec->multiout.dig_out_nid;
349 if (spec->dig_in) {
350 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
351 cs_pcm_digital_capture;
352 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
354 codec->num_pcms++;
356 return 0;
360 * parse codec topology
363 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
365 hda_nid_t dac;
366 if (!pin)
367 return 0;
368 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
369 return 0;
370 return dac;
373 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
375 struct cs_spec *spec = codec->spec;
376 struct auto_pin_cfg *cfg = &spec->autocfg;
377 hda_nid_t pin = cfg->inputs[idx].pin;
378 unsigned int val;
379 if (!is_jack_detectable(codec, pin))
380 return 0;
381 val = snd_hda_codec_get_pincfg(codec, pin);
382 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
385 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
386 unsigned int *idxp)
388 int i, idx;
389 hda_nid_t nid;
391 nid = codec->start_nid;
392 for (i = 0; i < codec->num_nodes; i++, nid++) {
393 unsigned int type;
394 type = get_wcaps_type(get_wcaps(codec, nid));
395 if (type != AC_WID_AUD_IN)
396 continue;
397 idx = snd_hda_get_conn_index(codec, nid, pin, false);
398 if (idx >= 0) {
399 *idxp = idx;
400 return nid;
403 return 0;
406 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
408 unsigned int val;
409 val = snd_hda_codec_get_pincfg(codec, nid);
410 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
413 static int parse_output(struct hda_codec *codec)
415 struct cs_spec *spec = codec->spec;
416 struct auto_pin_cfg *cfg = &spec->autocfg;
417 int i, extra_nids;
418 hda_nid_t dac;
420 for (i = 0; i < cfg->line_outs; i++) {
421 dac = get_dac(codec, cfg->line_out_pins[i]);
422 if (!dac)
423 break;
424 spec->dac_nid[i] = dac;
426 spec->multiout.num_dacs = i;
427 spec->multiout.dac_nids = spec->dac_nid;
428 spec->multiout.max_channels = i * 2;
430 /* add HP and speakers */
431 extra_nids = 0;
432 for (i = 0; i < cfg->hp_outs; i++) {
433 dac = get_dac(codec, cfg->hp_pins[i]);
434 if (!dac)
435 break;
436 if (!i)
437 spec->multiout.hp_nid = dac;
438 else
439 spec->multiout.extra_out_nid[extra_nids++] = dac;
441 for (i = 0; i < cfg->speaker_outs; i++) {
442 dac = get_dac(codec, cfg->speaker_pins[i]);
443 if (!dac)
444 break;
445 spec->multiout.extra_out_nid[extra_nids++] = dac;
448 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
449 cfg->speaker_outs = cfg->line_outs;
450 memcpy(cfg->speaker_pins, cfg->line_out_pins,
451 sizeof(cfg->speaker_pins));
452 cfg->line_outs = 0;
455 return 0;
458 static int parse_input(struct hda_codec *codec)
460 struct cs_spec *spec = codec->spec;
461 struct auto_pin_cfg *cfg = &spec->autocfg;
462 int i;
464 for (i = 0; i < cfg->num_inputs; i++) {
465 hda_nid_t pin = cfg->inputs[i].pin;
466 spec->input_idx[spec->num_inputs] = i;
467 spec->capsrc_idx[i] = spec->num_inputs++;
468 spec->cur_input = i;
469 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
471 if (!spec->num_inputs)
472 return 0;
474 /* check whether the automatic mic switch is available */
475 if (spec->num_inputs == 2 &&
476 cfg->inputs[0].type == AUTO_PIN_MIC &&
477 cfg->inputs[1].type == AUTO_PIN_MIC) {
478 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
479 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
480 spec->mic_detect = 1;
481 spec->automic_idx = 0;
483 } else {
484 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
485 spec->mic_detect = 1;
486 spec->automic_idx = 1;
490 return 0;
494 static int parse_digital_output(struct hda_codec *codec)
496 struct cs_spec *spec = codec->spec;
497 struct auto_pin_cfg *cfg = &spec->autocfg;
498 hda_nid_t nid;
500 if (!cfg->dig_outs)
501 return 0;
502 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
503 return 0;
504 spec->multiout.dig_out_nid = nid;
505 spec->multiout.share_spdif = 1;
506 if (cfg->dig_outs > 1 &&
507 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
508 spec->slave_dig_outs[0] = nid;
509 codec->slave_dig_outs = spec->slave_dig_outs;
511 return 0;
514 static int parse_digital_input(struct hda_codec *codec)
516 struct cs_spec *spec = codec->spec;
517 struct auto_pin_cfg *cfg = &spec->autocfg;
518 int idx;
520 if (cfg->dig_in_pin)
521 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
522 return 0;
526 * create mixer controls
529 static const char * const dir_sfx[2] = { "Playback", "Capture" };
531 static int add_mute(struct hda_codec *codec, const char *name, int index,
532 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
534 char tmp[44];
535 struct snd_kcontrol_new knew =
536 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
537 knew.private_value = pval;
538 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
539 *kctlp = snd_ctl_new1(&knew, codec);
540 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
541 return snd_hda_ctl_add(codec, 0, *kctlp);
544 static int add_volume(struct hda_codec *codec, const char *name,
545 int index, unsigned int pval, int dir,
546 struct snd_kcontrol **kctlp)
548 char tmp[44];
549 struct snd_kcontrol_new knew =
550 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
551 knew.private_value = pval;
552 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
553 *kctlp = snd_ctl_new1(&knew, codec);
554 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
555 return snd_hda_ctl_add(codec, 0, *kctlp);
558 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
560 unsigned int caps;
562 /* set the upper-limit for mixer amp to 0dB */
563 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
564 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
565 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
566 << AC_AMPCAP_NUM_STEPS_SHIFT;
567 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
570 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
572 struct cs_spec *spec = codec->spec;
573 unsigned int tlv[4];
574 int err;
576 spec->vmaster_sw =
577 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
578 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
579 if (err < 0)
580 return err;
582 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
583 spec->vmaster_vol =
584 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
585 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
586 if (err < 0)
587 return err;
588 return 0;
591 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
592 int num_ctls, int type)
594 struct cs_spec *spec = codec->spec;
595 const char *name;
596 int err, index;
597 struct snd_kcontrol *kctl;
598 static const char * const speakers[] = {
599 "Front Speaker", "Surround Speaker", "Bass Speaker"
601 static const char * const line_outs[] = {
602 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
605 fix_volume_caps(codec, dac);
606 if (!spec->vmaster_sw) {
607 err = add_vmaster(codec, dac);
608 if (err < 0)
609 return err;
612 index = 0;
613 switch (type) {
614 case AUTO_PIN_HP_OUT:
615 name = "Headphone";
616 index = idx;
617 break;
618 case AUTO_PIN_SPEAKER_OUT:
619 if (num_ctls > 1)
620 name = speakers[idx];
621 else
622 name = "Speaker";
623 break;
624 default:
625 if (num_ctls > 1)
626 name = line_outs[idx];
627 else
628 name = "Line-Out";
629 break;
632 err = add_mute(codec, name, index,
633 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
634 if (err < 0)
635 return err;
636 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
637 if (err < 0)
638 return err;
640 err = add_volume(codec, name, index,
641 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
642 if (err < 0)
643 return err;
644 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
645 if (err < 0)
646 return err;
648 return 0;
651 static int build_output(struct hda_codec *codec)
653 struct cs_spec *spec = codec->spec;
654 struct auto_pin_cfg *cfg = &spec->autocfg;
655 int i, err;
657 for (i = 0; i < cfg->line_outs; i++) {
658 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
659 i, cfg->line_outs, cfg->line_out_type);
660 if (err < 0)
661 return err;
663 for (i = 0; i < cfg->hp_outs; i++) {
664 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
665 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
666 if (err < 0)
667 return err;
669 for (i = 0; i < cfg->speaker_outs; i++) {
670 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
671 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
672 if (err < 0)
673 return err;
675 return 0;
681 static const struct snd_kcontrol_new cs_capture_ctls[] = {
682 HDA_BIND_SW("Capture Switch", 0),
683 HDA_BIND_VOL("Capture Volume", 0),
686 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
687 int force)
689 struct cs_spec *spec = codec->spec;
691 if (spec->cur_input == idx && !force)
692 return 0;
693 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
694 /* stream is running, let's swap the current ADC */
695 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
696 spec->cur_adc = spec->adc_nid[idx];
697 snd_hda_codec_setup_stream(codec, spec->cur_adc,
698 spec->cur_adc_stream_tag, 0,
699 spec->cur_adc_format);
701 spec->cur_input = idx;
702 cs_update_input_select(codec);
703 return 1;
706 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
707 struct snd_ctl_elem_info *uinfo)
709 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
710 struct cs_spec *spec = codec->spec;
711 struct auto_pin_cfg *cfg = &spec->autocfg;
712 unsigned int idx;
714 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
715 uinfo->count = 1;
716 uinfo->value.enumerated.items = spec->num_inputs;
717 if (uinfo->value.enumerated.item >= spec->num_inputs)
718 uinfo->value.enumerated.item = spec->num_inputs - 1;
719 idx = spec->input_idx[uinfo->value.enumerated.item];
720 strcpy(uinfo->value.enumerated.name,
721 hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
722 return 0;
725 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
726 struct snd_ctl_elem_value *ucontrol)
728 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
729 struct cs_spec *spec = codec->spec;
730 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
731 return 0;
734 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
735 struct snd_ctl_elem_value *ucontrol)
737 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
738 struct cs_spec *spec = codec->spec;
739 unsigned int idx = ucontrol->value.enumerated.item[0];
741 if (idx >= spec->num_inputs)
742 return -EINVAL;
743 idx = spec->input_idx[idx];
744 return change_cur_input(codec, idx, 0);
747 static const struct snd_kcontrol_new cs_capture_source = {
748 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
749 .name = "Capture Source",
750 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
751 .info = cs_capture_source_info,
752 .get = cs_capture_source_get,
753 .put = cs_capture_source_put,
756 static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
757 struct hda_ctl_ops *ops)
759 struct cs_spec *spec = codec->spec;
760 struct hda_bind_ctls *bind;
761 int i, n;
763 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
764 GFP_KERNEL);
765 if (!bind)
766 return NULL;
767 bind->ops = ops;
768 n = 0;
769 for (i = 0; i < AUTO_PIN_LAST; i++) {
770 if (!spec->adc_nid[i])
771 continue;
772 bind->values[n++] =
773 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
774 spec->adc_idx[i], HDA_INPUT);
776 return bind;
779 /* add a (input-boost) volume control to the given input pin */
780 static int add_input_volume_control(struct hda_codec *codec,
781 struct auto_pin_cfg *cfg,
782 int item)
784 hda_nid_t pin = cfg->inputs[item].pin;
785 u32 caps;
786 const char *label;
787 struct snd_kcontrol *kctl;
789 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
790 return 0;
791 caps = query_amp_caps(codec, pin, HDA_INPUT);
792 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
793 if (caps <= 1)
794 return 0;
795 label = hda_get_autocfg_input_label(codec, cfg, item);
796 return add_volume(codec, label, 0,
797 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
800 static int build_input(struct hda_codec *codec)
802 struct cs_spec *spec = codec->spec;
803 int i, err;
805 if (!spec->num_inputs)
806 return 0;
808 /* make bind-capture */
809 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
810 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
811 for (i = 0; i < 2; i++) {
812 struct snd_kcontrol *kctl;
813 int n;
814 if (!spec->capture_bind[i])
815 return -ENOMEM;
816 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
817 if (!kctl)
818 return -ENOMEM;
819 kctl->private_value = (long)spec->capture_bind[i];
820 err = snd_hda_ctl_add(codec, 0, kctl);
821 if (err < 0)
822 return err;
823 for (n = 0; n < AUTO_PIN_LAST; n++) {
824 if (!spec->adc_nid[n])
825 continue;
826 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
827 if (err < 0)
828 return err;
832 if (spec->num_inputs > 1 && !spec->mic_detect) {
833 err = snd_hda_ctl_add(codec, 0,
834 snd_ctl_new1(&cs_capture_source, codec));
835 if (err < 0)
836 return err;
839 for (i = 0; i < spec->num_inputs; i++) {
840 err = add_input_volume_control(codec, &spec->autocfg, i);
841 if (err < 0)
842 return err;
845 return 0;
851 static int build_digital_output(struct hda_codec *codec)
853 struct cs_spec *spec = codec->spec;
854 int err;
856 if (!spec->multiout.dig_out_nid)
857 return 0;
859 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
860 spec->multiout.dig_out_nid);
861 if (err < 0)
862 return err;
863 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
864 if (err < 0)
865 return err;
866 return 0;
869 static int build_digital_input(struct hda_codec *codec)
871 struct cs_spec *spec = codec->spec;
872 if (spec->dig_in)
873 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
874 return 0;
878 * auto-mute and auto-mic switching
879 * CS421x auto-output redirecting
880 * HP/SPK/SPDIF
883 static void cs_automute(struct hda_codec *codec)
885 struct cs_spec *spec = codec->spec;
886 struct auto_pin_cfg *cfg = &spec->autocfg;
887 unsigned int hp_present;
888 unsigned int spdif_present;
889 hda_nid_t nid;
890 int i;
892 spdif_present = 0;
893 if (cfg->dig_outs) {
894 nid = cfg->dig_out_pins[0];
895 if (is_jack_detectable(codec, nid)) {
897 TODO: SPDIF output redirect when SENSE_B is enabled.
898 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
899 assumed.
901 if (snd_hda_jack_detect(codec, nid)
902 /* && spec->sense_b */)
903 spdif_present = 1;
907 hp_present = 0;
908 for (i = 0; i < cfg->hp_outs; i++) {
909 nid = cfg->hp_pins[i];
910 if (!is_jack_detectable(codec, nid))
911 continue;
912 hp_present = snd_hda_jack_detect(codec, nid);
913 if (hp_present)
914 break;
917 /* mute speakers if spdif or hp jack is plugged in */
918 for (i = 0; i < cfg->speaker_outs; i++) {
919 nid = cfg->speaker_pins[i];
920 snd_hda_codec_write(codec, nid, 0,
921 AC_VERB_SET_PIN_WIDGET_CONTROL,
922 hp_present ? 0 : PIN_OUT);
923 /* detect on spdif is specific to CS421x */
924 if (spec->vendor_nid == CS421X_VENDOR_NID) {
925 snd_hda_codec_write(codec, nid, 0,
926 AC_VERB_SET_PIN_WIDGET_CONTROL,
927 spdif_present ? 0 : PIN_OUT);
930 if (spec->board_config == CS420X_MBP53 ||
931 spec->board_config == CS420X_MBP55 ||
932 spec->board_config == CS420X_IMAC27) {
933 unsigned int gpio = hp_present ? 0x02 : 0x08;
934 snd_hda_codec_write(codec, 0x01, 0,
935 AC_VERB_SET_GPIO_DATA, gpio);
938 /* specific to CS421x */
939 if (spec->vendor_nid == CS421X_VENDOR_NID) {
940 /* mute HPs if spdif jack (SENSE_B) is present */
941 for (i = 0; i < cfg->hp_outs; i++) {
942 nid = cfg->hp_pins[i];
943 snd_hda_codec_write(codec, nid, 0,
944 AC_VERB_SET_PIN_WIDGET_CONTROL,
945 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
948 /* SPDIF TX on/off */
949 if (cfg->dig_outs) {
950 nid = cfg->dig_out_pins[0];
951 snd_hda_codec_write(codec, nid, 0,
952 AC_VERB_SET_PIN_WIDGET_CONTROL,
953 spdif_present ? PIN_OUT : 0);
956 /* Update board GPIOs if neccessary ... */
961 * Auto-input redirect for CS421x
962 * Switch max 3 inputs of a single ADC (nid 3)
965 static void cs_automic(struct hda_codec *codec)
967 struct cs_spec *spec = codec->spec;
968 struct auto_pin_cfg *cfg = &spec->autocfg;
969 hda_nid_t nid;
970 unsigned int present;
972 nid = cfg->inputs[spec->automic_idx].pin;
973 present = snd_hda_jack_detect(codec, nid);
975 /* specific to CS421x, single ADC */
976 if (spec->vendor_nid == CS421X_VENDOR_NID) {
977 if (present) {
978 spec->last_input = spec->cur_input;
979 spec->cur_input = spec->automic_idx;
980 } else {
981 spec->cur_input = spec->last_input;
983 cs_update_input_select(codec);
984 } else {
985 if (present)
986 change_cur_input(codec, spec->automic_idx, 0);
987 else
988 change_cur_input(codec, !spec->automic_idx, 0);
995 static void init_output(struct hda_codec *codec)
997 struct cs_spec *spec = codec->spec;
998 struct auto_pin_cfg *cfg = &spec->autocfg;
999 int i;
1001 /* mute first */
1002 for (i = 0; i < spec->multiout.num_dacs; i++)
1003 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1004 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1005 if (spec->multiout.hp_nid)
1006 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1007 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1008 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1009 if (!spec->multiout.extra_out_nid[i])
1010 break;
1011 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1012 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1015 /* set appropriate pin controls */
1016 for (i = 0; i < cfg->line_outs; i++)
1017 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1018 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1019 /* HP */
1020 for (i = 0; i < cfg->hp_outs; i++) {
1021 hda_nid_t nid = cfg->hp_pins[i];
1022 snd_hda_codec_write(codec, nid, 0,
1023 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1024 if (!cfg->speaker_outs)
1025 continue;
1026 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1027 snd_hda_codec_write(codec, nid, 0,
1028 AC_VERB_SET_UNSOLICITED_ENABLE,
1029 AC_USRSP_EN | HP_EVENT);
1030 spec->hp_detect = 1;
1034 /* Speaker */
1035 for (i = 0; i < cfg->speaker_outs; i++)
1036 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1037 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1039 /* SPDIF is enabled on presence detect for CS421x */
1040 if (spec->hp_detect || spec->spdif_detect)
1041 cs_automute(codec);
1044 static void init_input(struct hda_codec *codec)
1046 struct cs_spec *spec = codec->spec;
1047 struct auto_pin_cfg *cfg = &spec->autocfg;
1048 unsigned int coef;
1049 int i;
1051 for (i = 0; i < cfg->num_inputs; i++) {
1052 unsigned int ctl;
1053 hda_nid_t pin = cfg->inputs[i].pin;
1054 if (!spec->adc_nid[i])
1055 continue;
1056 /* set appropriate pin control and mute first */
1057 ctl = PIN_IN;
1058 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1059 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1060 caps >>= AC_PINCAP_VREF_SHIFT;
1061 if (caps & AC_PINCAP_VREF_80)
1062 ctl = PIN_VREF80;
1064 snd_hda_codec_write(codec, pin, 0,
1065 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1066 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1067 AC_VERB_SET_AMP_GAIN_MUTE,
1068 AMP_IN_MUTE(spec->adc_idx[i]));
1069 if (spec->mic_detect && spec->automic_idx == i)
1070 snd_hda_codec_write(codec, pin, 0,
1071 AC_VERB_SET_UNSOLICITED_ENABLE,
1072 AC_USRSP_EN | MIC_EVENT);
1074 /* specific to CS421x */
1075 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1076 if (spec->mic_detect)
1077 cs_automic(codec);
1078 else {
1079 spec->cur_adc = spec->adc_nid[spec->cur_input];
1080 cs_update_input_select(codec);
1082 } else {
1083 change_cur_input(codec, spec->cur_input, 1);
1084 if (spec->mic_detect)
1085 cs_automic(codec);
1087 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1088 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1089 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1090 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1091 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1092 * No effect if SPDIF_OUT2 is
1093 * selected in IDX_SPDIF_CTL.
1095 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1099 static const struct hda_verb cs_coef_init_verbs[] = {
1100 {0x11, AC_VERB_SET_PROC_STATE, 1},
1101 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1102 {0x11, AC_VERB_SET_PROC_COEF,
1103 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1104 | 0x0040 /* Mute DACs on FIFO error */
1105 | 0x1000 /* Enable DACs High Pass Filter */
1106 | 0x0400 /* Disable Coefficient Auto increment */
1108 /* Beep */
1109 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1110 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1112 {} /* terminator */
1115 /* Errata: CS4207 rev C0/C1/C2 Silicon
1117 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1119 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1120 * may be excessive (up to an additional 200 μA), which is most easily
1121 * observed while the part is being held in reset (RESET# active low).
1123 * Root Cause: At initial powerup of the device, the logic that drives
1124 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1125 * initialized.
1126 * Certain random patterns will cause a steady leakage current in those
1127 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1129 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1130 * blocks, which will alleviate the issue.
1133 static const struct hda_verb cs_errata_init_verbs[] = {
1134 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1135 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1137 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1138 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1139 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1140 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1141 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1142 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1144 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1145 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1147 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1148 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1149 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1150 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1151 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1152 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1153 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1155 #if 0 /* Don't to set to D3 as we are in power-up sequence */
1156 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1157 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1158 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
1159 #endif
1161 {} /* terminator */
1164 /* SPDIF setup */
1165 static void init_digital(struct hda_codec *codec)
1167 unsigned int coef;
1169 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1170 coef |= 0x0008; /* Replace with mute on error */
1171 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1172 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1173 * SPDIF_OUT2 is shared with GPIO1 and
1174 * DMIC_SDA2.
1176 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
1179 static int cs_init(struct hda_codec *codec)
1181 struct cs_spec *spec = codec->spec;
1183 /* init_verb sequence for C0/C1/C2 errata*/
1184 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1186 snd_hda_sequence_write(codec, cs_coef_init_verbs);
1188 if (spec->gpio_mask) {
1189 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1190 spec->gpio_mask);
1191 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1192 spec->gpio_dir);
1193 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1194 spec->gpio_data);
1197 init_output(codec);
1198 init_input(codec);
1199 init_digital(codec);
1200 return 0;
1203 static int cs_build_controls(struct hda_codec *codec)
1205 int err;
1207 err = build_output(codec);
1208 if (err < 0)
1209 return err;
1210 err = build_input(codec);
1211 if (err < 0)
1212 return err;
1213 err = build_digital_output(codec);
1214 if (err < 0)
1215 return err;
1216 err = build_digital_input(codec);
1217 if (err < 0)
1218 return err;
1219 return cs_init(codec);
1222 static void cs_free(struct hda_codec *codec)
1224 struct cs_spec *spec = codec->spec;
1225 kfree(spec->capture_bind[0]);
1226 kfree(spec->capture_bind[1]);
1227 kfree(codec->spec);
1230 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1232 switch ((res >> 26) & 0x7f) {
1233 case HP_EVENT:
1234 cs_automute(codec);
1235 break;
1236 case MIC_EVENT:
1237 cs_automic(codec);
1238 break;
1242 static const struct hda_codec_ops cs_patch_ops = {
1243 .build_controls = cs_build_controls,
1244 .build_pcms = cs_build_pcms,
1245 .init = cs_init,
1246 .free = cs_free,
1247 .unsol_event = cs_unsol_event,
1250 static int cs_parse_auto_config(struct hda_codec *codec)
1252 struct cs_spec *spec = codec->spec;
1253 int err;
1255 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1256 if (err < 0)
1257 return err;
1259 err = parse_output(codec);
1260 if (err < 0)
1261 return err;
1262 err = parse_input(codec);
1263 if (err < 0)
1264 return err;
1265 err = parse_digital_output(codec);
1266 if (err < 0)
1267 return err;
1268 err = parse_digital_input(codec);
1269 if (err < 0)
1270 return err;
1271 return 0;
1274 static const char * const cs420x_models[CS420X_MODELS] = {
1275 [CS420X_MBP53] = "mbp53",
1276 [CS420X_MBP55] = "mbp55",
1277 [CS420X_IMAC27] = "imac27",
1278 [CS420X_AUTO] = "auto",
1282 static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
1283 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1284 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
1285 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1286 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
1287 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
1288 {} /* terminator */
1291 struct cs_pincfg {
1292 hda_nid_t nid;
1293 u32 val;
1296 static const struct cs_pincfg mbp53_pincfgs[] = {
1297 { 0x09, 0x012b4050 },
1298 { 0x0a, 0x90100141 },
1299 { 0x0b, 0x90100140 },
1300 { 0x0c, 0x018b3020 },
1301 { 0x0d, 0x90a00110 },
1302 { 0x0e, 0x400000f0 },
1303 { 0x0f, 0x01cbe030 },
1304 { 0x10, 0x014be060 },
1305 { 0x12, 0x400000f0 },
1306 { 0x15, 0x400000f0 },
1307 {} /* terminator */
1310 static const struct cs_pincfg mbp55_pincfgs[] = {
1311 { 0x09, 0x012b4030 },
1312 { 0x0a, 0x90100121 },
1313 { 0x0b, 0x90100120 },
1314 { 0x0c, 0x400000f0 },
1315 { 0x0d, 0x90a00110 },
1316 { 0x0e, 0x400000f0 },
1317 { 0x0f, 0x400000f0 },
1318 { 0x10, 0x014be040 },
1319 { 0x12, 0x400000f0 },
1320 { 0x15, 0x400000f0 },
1321 {} /* terminator */
1324 static const struct cs_pincfg imac27_pincfgs[] = {
1325 { 0x09, 0x012b4050 },
1326 { 0x0a, 0x90100140 },
1327 { 0x0b, 0x90100142 },
1328 { 0x0c, 0x018b3020 },
1329 { 0x0d, 0x90a00110 },
1330 { 0x0e, 0x400000f0 },
1331 { 0x0f, 0x01cbe030 },
1332 { 0x10, 0x014be060 },
1333 { 0x12, 0x01ab9070 },
1334 { 0x15, 0x400000f0 },
1335 {} /* terminator */
1338 static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1339 [CS420X_MBP53] = mbp53_pincfgs,
1340 [CS420X_MBP55] = mbp55_pincfgs,
1341 [CS420X_IMAC27] = imac27_pincfgs,
1344 static void fix_pincfg(struct hda_codec *codec, int model,
1345 const struct cs_pincfg **pin_configs)
1347 const struct cs_pincfg *cfg = pin_configs[model];
1348 if (!cfg)
1349 return;
1350 for (; cfg->nid; cfg++)
1351 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1354 static int patch_cs420x(struct hda_codec *codec)
1356 struct cs_spec *spec;
1357 int err;
1359 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1360 if (!spec)
1361 return -ENOMEM;
1362 codec->spec = spec;
1364 spec->vendor_nid = CS420X_VENDOR_NID;
1366 spec->board_config =
1367 snd_hda_check_board_config(codec, CS420X_MODELS,
1368 cs420x_models, cs420x_cfg_tbl);
1369 if (spec->board_config >= 0)
1370 fix_pincfg(codec, spec->board_config, cs_pincfgs);
1372 switch (spec->board_config) {
1373 case CS420X_IMAC27:
1374 case CS420X_MBP53:
1375 case CS420X_MBP55:
1376 /* GPIO1 = headphones */
1377 /* GPIO3 = speakers */
1378 spec->gpio_mask = 0x0a;
1379 spec->gpio_dir = 0x0a;
1380 break;
1383 err = cs_parse_auto_config(codec);
1384 if (err < 0)
1385 goto error;
1387 codec->patch_ops = cs_patch_ops;
1389 return 0;
1391 error:
1392 kfree(codec->spec);
1393 codec->spec = NULL;
1394 return err;
1398 * Cirrus Logic CS4210
1400 * 1 DAC => HP(sense) / Speakers,
1401 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1402 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1405 /* CS4210 board names */
1406 static const char *cs421x_models[CS421X_MODELS] = {
1407 [CS421X_CDB4210] = "cdb4210",
1410 static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1411 /* Test Intel board + CDB2410 */
1412 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1413 {} /* terminator */
1416 /* CS4210 board pinconfigs */
1417 /* Default CS4210 (CDB4210)*/
1418 static const struct cs_pincfg cdb4210_pincfgs[] = {
1419 { 0x05, 0x0321401f },
1420 { 0x06, 0x90170010 },
1421 { 0x07, 0x03813031 },
1422 { 0x08, 0xb7a70037 },
1423 { 0x09, 0xb7a6003e },
1424 { 0x0a, 0x034510f0 },
1425 {} /* terminator */
1428 static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1429 [CS421X_CDB4210] = cdb4210_pincfgs,
1432 static const struct hda_verb cs421x_coef_init_verbs[] = {
1433 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1434 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1436 Disable Coefficient Index Auto-Increment(DAI)=1,
1437 PDREF=0
1439 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1441 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1442 /* ADC SZCMode = Digital Soft Ramp */
1443 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1445 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1446 {0x0B, AC_VERB_SET_PROC_COEF,
1447 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1448 | 0x0004 /* Mute DAC on FIFO error */
1449 | 0x0008 /* Enable DAC High Pass Filter */
1451 {} /* terminator */
1454 /* Errata: CS4210 rev A1 Silicon
1456 * http://www.cirrus.com/en/pubs/errata/
1458 * Description:
1459 * 1. Performance degredation is present in the ADC.
1460 * 2. Speaker output is not completely muted upon HP detect.
1461 * 3. Noise is present when clipping occurs on the amplified
1462 * speaker outputs.
1464 * Workaround:
1465 * The following verb sequence written to the registers during
1466 * initialization will correct the issues listed above.
1469 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1470 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1472 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1473 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1475 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1476 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1478 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1479 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1481 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1482 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1484 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1485 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1487 {} /* terminator */
1490 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1491 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1493 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1494 struct snd_ctl_elem_info *uinfo)
1496 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1497 uinfo->count = 1;
1498 uinfo->value.integer.min = 0;
1499 uinfo->value.integer.max = 3;
1500 return 0;
1503 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_value *ucontrol)
1506 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1508 ucontrol->value.integer.value[0] =
1509 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1510 return 0;
1513 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1514 struct snd_ctl_elem_value *ucontrol)
1516 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1518 unsigned int vol = ucontrol->value.integer.value[0];
1519 unsigned int coef =
1520 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1521 unsigned int original_coef = coef;
1523 coef &= ~0x0003;
1524 coef |= (vol & 0x0003);
1525 if (original_coef == coef)
1526 return 0;
1527 else {
1528 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1529 return 1;
1533 static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1535 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1536 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1537 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1538 .name = "Speaker Boost Playback Volume",
1539 .info = cs421x_boost_vol_info,
1540 .get = cs421x_boost_vol_get,
1541 .put = cs421x_boost_vol_put,
1542 .tlv = { .p = cs421x_speaker_boost_db_scale },
1545 static void cs421x_pinmux_init(struct hda_codec *codec)
1547 struct cs_spec *spec = codec->spec;
1548 unsigned int def_conf, coef;
1550 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1551 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1553 if (spec->gpio_mask)
1554 coef |= 0x0008; /* B1,B2 are GPIOs */
1555 else
1556 coef &= ~0x0008;
1558 if (spec->sense_b)
1559 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1560 else
1561 coef &= ~0x0010;
1563 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1565 if ((spec->gpio_mask || spec->sense_b) &&
1566 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1569 GPIO or SENSE_B forced - disconnect the DMIC pin.
1571 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1572 def_conf &= ~AC_DEFCFG_PORT_CONN;
1573 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1574 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1578 static void init_cs421x_digital(struct hda_codec *codec)
1580 struct cs_spec *spec = codec->spec;
1581 struct auto_pin_cfg *cfg = &spec->autocfg;
1582 int i;
1585 for (i = 0; i < cfg->dig_outs; i++) {
1586 hda_nid_t nid = cfg->dig_out_pins[i];
1587 if (!cfg->speaker_outs)
1588 continue;
1589 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1591 snd_hda_codec_write(codec, nid, 0,
1592 AC_VERB_SET_UNSOLICITED_ENABLE,
1593 AC_USRSP_EN | SPDIF_EVENT);
1594 spec->spdif_detect = 1;
1599 static int cs421x_init(struct hda_codec *codec)
1601 struct cs_spec *spec = codec->spec;
1603 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1604 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1606 cs421x_pinmux_init(codec);
1608 if (spec->gpio_mask) {
1609 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1610 spec->gpio_mask);
1611 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1612 spec->gpio_dir);
1613 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1614 spec->gpio_data);
1617 init_output(codec);
1618 init_input(codec);
1619 init_cs421x_digital(codec);
1621 return 0;
1625 * CS4210 Input MUX (1 ADC)
1627 static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1628 struct snd_ctl_elem_info *uinfo)
1630 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1631 struct cs_spec *spec = codec->spec;
1633 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1636 static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1637 struct snd_ctl_elem_value *ucontrol)
1639 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1640 struct cs_spec *spec = codec->spec;
1642 ucontrol->value.enumerated.item[0] = spec->cur_input;
1643 return 0;
1646 static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol)
1649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1650 struct cs_spec *spec = codec->spec;
1652 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1653 spec->adc_nid[0], &spec->cur_input);
1657 static struct snd_kcontrol_new cs421x_capture_source = {
1659 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1660 .name = "Capture Source",
1661 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1662 .info = cs421x_mux_enum_info,
1663 .get = cs421x_mux_enum_get,
1664 .put = cs421x_mux_enum_put,
1667 static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1669 struct cs_spec *spec = codec->spec;
1670 struct auto_pin_cfg *cfg = &spec->autocfg;
1671 const struct hda_input_mux *imux = &spec->input_mux;
1672 hda_nid_t pin = cfg->inputs[item].pin;
1673 struct snd_kcontrol *kctl;
1674 u32 caps;
1676 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1677 return 0;
1679 caps = query_amp_caps(codec, pin, HDA_INPUT);
1680 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1681 if (caps <= 1)
1682 return 0;
1684 return add_volume(codec, imux->items[item].label, 0,
1685 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1688 /* add a (input-boost) volume control to the given input pin */
1689 static int build_cs421x_input(struct hda_codec *codec)
1691 struct cs_spec *spec = codec->spec;
1692 struct auto_pin_cfg *cfg = &spec->autocfg;
1693 struct hda_input_mux *imux = &spec->input_mux;
1694 int i, err, type_idx;
1695 const char *label;
1697 if (!spec->num_inputs)
1698 return 0;
1700 /* make bind-capture */
1701 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1702 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1703 for (i = 0; i < 2; i++) {
1704 struct snd_kcontrol *kctl;
1705 int n;
1706 if (!spec->capture_bind[i])
1707 return -ENOMEM;
1708 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1709 if (!kctl)
1710 return -ENOMEM;
1711 kctl->private_value = (long)spec->capture_bind[i];
1712 err = snd_hda_ctl_add(codec, 0, kctl);
1713 if (err < 0)
1714 return err;
1715 for (n = 0; n < AUTO_PIN_LAST; n++) {
1716 if (!spec->adc_nid[n])
1717 continue;
1718 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1719 if (err < 0)
1720 return err;
1724 /* Add Input MUX Items + Capture Volume/Switch */
1725 for (i = 0; i < spec->num_inputs; i++) {
1726 label = hda_get_autocfg_input_label(codec, cfg, i);
1727 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1729 err = cs421x_add_input_volume_control(codec, i);
1730 if (err < 0)
1731 return err;
1735 Add 'Capture Source' Switch if
1736 * 2 inputs and no mic detec
1737 * 3 inputs
1739 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1740 (spec->num_inputs == 3)) {
1742 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1743 snd_ctl_new1(&cs421x_capture_source, codec));
1744 if (err < 0)
1745 return err;
1748 return 0;
1751 /* Single DAC (Mute/Gain) */
1752 static int build_cs421x_output(struct hda_codec *codec)
1754 hda_nid_t dac = CS4210_DAC_NID;
1755 struct cs_spec *spec = codec->spec;
1756 struct auto_pin_cfg *cfg = &spec->autocfg;
1757 struct snd_kcontrol *kctl;
1758 int err;
1759 char *name = "HP/Speakers";
1761 fix_volume_caps(codec, dac);
1762 if (!spec->vmaster_sw) {
1763 err = add_vmaster(codec, dac);
1764 if (err < 0)
1765 return err;
1768 err = add_mute(codec, name, 0,
1769 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1770 if (err < 0)
1771 return err;
1772 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1773 if (err < 0)
1774 return err;
1776 err = add_volume(codec, name, 0,
1777 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1778 if (err < 0)
1779 return err;
1780 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1781 if (err < 0)
1782 return err;
1784 if (cfg->speaker_outs) {
1785 err = snd_hda_ctl_add(codec, 0,
1786 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1787 if (err < 0)
1788 return err;
1790 return err;
1793 static int cs421x_build_controls(struct hda_codec *codec)
1795 int err;
1797 err = build_cs421x_output(codec);
1798 if (err < 0)
1799 return err;
1800 err = build_cs421x_input(codec);
1801 if (err < 0)
1802 return err;
1803 err = build_digital_output(codec);
1804 if (err < 0)
1805 return err;
1806 return cs421x_init(codec);
1809 static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1811 switch ((res >> 26) & 0x3f) {
1812 case HP_EVENT:
1813 case SPDIF_EVENT:
1814 cs_automute(codec);
1815 break;
1817 case MIC_EVENT:
1818 cs_automic(codec);
1819 break;
1823 static int parse_cs421x_input(struct hda_codec *codec)
1825 struct cs_spec *spec = codec->spec;
1826 struct auto_pin_cfg *cfg = &spec->autocfg;
1827 int i;
1829 for (i = 0; i < cfg->num_inputs; i++) {
1830 hda_nid_t pin = cfg->inputs[i].pin;
1831 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1832 spec->cur_input = spec->last_input = i;
1833 spec->num_inputs++;
1835 /* check whether the automatic mic switch is available */
1836 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1837 spec->mic_detect = 1;
1838 spec->automic_idx = i;
1841 return 0;
1844 static int cs421x_parse_auto_config(struct hda_codec *codec)
1846 struct cs_spec *spec = codec->spec;
1847 int err;
1849 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1850 if (err < 0)
1851 return err;
1852 err = parse_output(codec);
1853 if (err < 0)
1854 return err;
1855 err = parse_cs421x_input(codec);
1856 if (err < 0)
1857 return err;
1858 err = parse_digital_output(codec);
1859 if (err < 0)
1860 return err;
1861 return 0;
1864 #ifdef CONFIG_PM
1866 Manage PDREF, when transitioning to D3hot
1867 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1869 static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1871 unsigned int coef;
1873 snd_hda_shutup_pins(codec);
1875 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1876 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1877 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1878 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1880 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1881 coef |= 0x0004; /* PDREF */
1882 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1884 return 0;
1886 #endif
1888 static struct hda_codec_ops cs4210_patch_ops = {
1889 .build_controls = cs421x_build_controls,
1890 .build_pcms = cs_build_pcms,
1891 .init = cs421x_init,
1892 .free = cs_free,
1893 .unsol_event = cs421x_unsol_event,
1894 #ifdef CONFIG_PM
1895 .suspend = cs421x_suspend,
1896 #endif
1899 static int patch_cs421x(struct hda_codec *codec)
1901 struct cs_spec *spec;
1902 int err;
1904 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1905 if (!spec)
1906 return -ENOMEM;
1907 codec->spec = spec;
1909 spec->vendor_nid = CS421X_VENDOR_NID;
1911 spec->board_config =
1912 snd_hda_check_board_config(codec, CS421X_MODELS,
1913 cs421x_models, cs421x_cfg_tbl);
1914 if (spec->board_config >= 0)
1915 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1917 Setup GPIO/SENSE for each board (if used)
1919 switch (spec->board_config) {
1920 case CS421X_CDB4210:
1921 snd_printd("CS4210 board: %s\n",
1922 cs421x_models[spec->board_config]);
1923 /* spec->gpio_mask = 3;
1924 spec->gpio_dir = 3;
1925 spec->gpio_data = 3;
1927 spec->sense_b = 1;
1929 break;
1933 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1934 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1935 is disabled.
1937 cs421x_pinmux_init(codec);
1939 err = cs421x_parse_auto_config(codec);
1940 if (err < 0)
1941 goto error;
1943 codec->patch_ops = cs4210_patch_ops;
1945 return 0;
1947 error:
1948 kfree(codec->spec);
1949 codec->spec = NULL;
1950 return err;
1955 * patch entries
1957 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
1958 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1959 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1960 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
1961 {} /* terminator */
1964 MODULE_ALIAS("snd-hda-codec-id:10134206");
1965 MODULE_ALIAS("snd-hda-codec-id:10134207");
1966 MODULE_ALIAS("snd-hda-codec-id:10134210");
1968 MODULE_LICENSE("GPL");
1969 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1971 static struct hda_codec_preset_list cirrus_list = {
1972 .preset = snd_hda_preset_cirrus,
1973 .owner = THIS_MODULE,
1976 static int __init patch_cirrus_init(void)
1978 return snd_hda_add_codec_preset(&cirrus_list);
1981 static void __exit patch_cirrus_exit(void)
1983 snd_hda_delete_codec_preset(&cirrus_list);
1986 module_init(patch_cirrus_init)
1987 module_exit(patch_cirrus_exit)