ALSA: hda - MacBookPro 5,3 line-in support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / pci / hda / patch_cirrus.c
blob871d7e50129f5c7e5c5b7d831ba0dfba124327a3
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"
32 struct cs_spec {
33 int board_config;
34 struct auto_pin_cfg autocfg;
35 struct hda_multi_out multiout;
36 struct snd_kcontrol *vmaster_sw;
37 struct snd_kcontrol *vmaster_vol;
39 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
40 hda_nid_t slave_dig_outs[2];
42 unsigned int input_idx[AUTO_PIN_LAST];
43 unsigned int capsrc_idx[AUTO_PIN_LAST];
44 hda_nid_t adc_nid[AUTO_PIN_LAST];
45 unsigned int adc_idx[AUTO_PIN_LAST];
46 unsigned int num_inputs;
47 unsigned int cur_input;
48 unsigned int automic_idx;
49 hda_nid_t cur_adc;
50 unsigned int cur_adc_stream_tag;
51 unsigned int cur_adc_format;
52 hda_nid_t dig_in;
54 struct hda_bind_ctls *capture_bind[2];
56 unsigned int gpio_mask;
57 unsigned int gpio_dir;
58 unsigned int gpio_data;
60 struct hda_pcm pcm_rec[2]; /* PCM information */
62 unsigned int hp_detect:1;
63 unsigned int mic_detect:1;
66 /* available models */
67 enum {
68 CS420X_MBP53,
69 CS420X_MBP55,
70 CS420X_IMAC27,
71 CS420X_AUTO,
72 CS420X_MODELS
75 /* Vendor-specific processing widget */
76 #define CS420X_VENDOR_NID 0x11
77 #define CS_DIG_OUT1_PIN_NID 0x10
78 #define CS_DIG_OUT2_PIN_NID 0x15
79 #define CS_DMIC1_PIN_NID 0x12
80 #define CS_DMIC2_PIN_NID 0x0e
82 /* coef indices */
83 #define IDX_SPDIF_STAT 0x0000
84 #define IDX_SPDIF_CTL 0x0001
85 #define IDX_ADC_CFG 0x0002
86 /* SZC bitmask, 4 modes below:
87 * 0 = immediate,
88 * 1 = digital immediate, analog zero-cross
89 * 2 = digtail & analog soft-ramp
90 * 3 = digital soft-ramp, analog zero-cross
92 #define CS_COEF_ADC_SZC_MASK (3 << 0)
93 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
94 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
95 /* PGA mode: 0 = differential, 1 = signle-ended */
96 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
97 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
98 #define IDX_DAC_CFG 0x0003
99 /* SZC bitmask, 4 modes below:
100 * 0 = Immediate
101 * 1 = zero-cross
102 * 2 = soft-ramp
103 * 3 = soft-ramp on zero-cross
105 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
106 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
107 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
109 #define IDX_BEEP_CFG 0x0004
110 /* 0x0008 - test reg key */
111 /* 0x0009 - 0x0014 -> 12 test regs */
112 /* 0x0015 - visibility reg */
115 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
117 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
118 AC_VERB_SET_COEF_INDEX, idx);
119 return snd_hda_codec_read(codec, CS420X_VENDOR_NID, 0,
120 AC_VERB_GET_PROC_COEF, 0);
123 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
124 unsigned int coef)
126 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
127 AC_VERB_SET_COEF_INDEX, idx);
128 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
129 AC_VERB_SET_PROC_COEF, coef);
133 #define HP_EVENT 1
134 #define MIC_EVENT 2
137 * PCM callbacks
139 static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
140 struct hda_codec *codec,
141 struct snd_pcm_substream *substream)
143 struct cs_spec *spec = codec->spec;
144 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
145 hinfo);
148 static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
149 struct hda_codec *codec,
150 unsigned int stream_tag,
151 unsigned int format,
152 struct snd_pcm_substream *substream)
154 struct cs_spec *spec = codec->spec;
155 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
156 stream_tag, format, substream);
159 static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
160 struct hda_codec *codec,
161 struct snd_pcm_substream *substream)
163 struct cs_spec *spec = codec->spec;
164 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
168 * Digital out
170 static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
171 struct hda_codec *codec,
172 struct snd_pcm_substream *substream)
174 struct cs_spec *spec = codec->spec;
175 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
178 static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
179 struct hda_codec *codec,
180 struct snd_pcm_substream *substream)
182 struct cs_spec *spec = codec->spec;
183 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
186 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
187 struct hda_codec *codec,
188 unsigned int stream_tag,
189 unsigned int format,
190 struct snd_pcm_substream *substream)
192 struct cs_spec *spec = codec->spec;
193 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
194 format, substream);
197 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
198 struct hda_codec *codec,
199 struct snd_pcm_substream *substream)
201 struct cs_spec *spec = codec->spec;
202 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
206 * Analog capture
208 static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
209 struct hda_codec *codec,
210 unsigned int stream_tag,
211 unsigned int format,
212 struct snd_pcm_substream *substream)
214 struct cs_spec *spec = codec->spec;
215 spec->cur_adc = spec->adc_nid[spec->cur_input];
216 spec->cur_adc_stream_tag = stream_tag;
217 spec->cur_adc_format = format;
218 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
219 return 0;
222 static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
223 struct hda_codec *codec,
224 struct snd_pcm_substream *substream)
226 struct cs_spec *spec = codec->spec;
227 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
228 spec->cur_adc = 0;
229 return 0;
234 static struct hda_pcm_stream cs_pcm_analog_playback = {
235 .substreams = 1,
236 .channels_min = 2,
237 .channels_max = 2,
238 .ops = {
239 .open = cs_playback_pcm_open,
240 .prepare = cs_playback_pcm_prepare,
241 .cleanup = cs_playback_pcm_cleanup
245 static struct hda_pcm_stream cs_pcm_analog_capture = {
246 .substreams = 1,
247 .channels_min = 2,
248 .channels_max = 2,
249 .ops = {
250 .prepare = cs_capture_pcm_prepare,
251 .cleanup = cs_capture_pcm_cleanup
255 static struct hda_pcm_stream cs_pcm_digital_playback = {
256 .substreams = 1,
257 .channels_min = 2,
258 .channels_max = 2,
259 .ops = {
260 .open = cs_dig_playback_pcm_open,
261 .close = cs_dig_playback_pcm_close,
262 .prepare = cs_dig_playback_pcm_prepare,
263 .cleanup = cs_dig_playback_pcm_cleanup
267 static struct hda_pcm_stream cs_pcm_digital_capture = {
268 .substreams = 1,
269 .channels_min = 2,
270 .channels_max = 2,
273 static int cs_build_pcms(struct hda_codec *codec)
275 struct cs_spec *spec = codec->spec;
276 struct hda_pcm *info = spec->pcm_rec;
278 codec->pcm_info = info;
279 codec->num_pcms = 0;
281 info->name = "Cirrus Analog";
282 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
283 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
284 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
285 spec->multiout.max_channels;
286 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
287 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
288 spec->adc_nid[spec->cur_input];
289 codec->num_pcms++;
291 if (!spec->multiout.dig_out_nid && !spec->dig_in)
292 return 0;
294 info++;
295 info->name = "Cirrus Digital";
296 info->pcm_type = spec->autocfg.dig_out_type[0];
297 if (!info->pcm_type)
298 info->pcm_type = HDA_PCM_TYPE_SPDIF;
299 if (spec->multiout.dig_out_nid) {
300 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
301 cs_pcm_digital_playback;
302 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
303 spec->multiout.dig_out_nid;
305 if (spec->dig_in) {
306 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
307 cs_pcm_digital_capture;
308 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
310 codec->num_pcms++;
312 return 0;
316 * parse codec topology
319 static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
321 hda_nid_t dac;
322 if (!pin)
323 return 0;
324 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
325 return 0;
326 return dac;
329 static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
331 struct cs_spec *spec = codec->spec;
332 struct auto_pin_cfg *cfg = &spec->autocfg;
333 hda_nid_t pin = cfg->input_pins[idx];
334 unsigned int val = snd_hda_query_pin_caps(codec, pin);
335 if (!(val & AC_PINCAP_PRES_DETECT))
336 return 0;
337 val = snd_hda_codec_get_pincfg(codec, pin);
338 return (get_defcfg_connect(val) == AC_JACK_PORT_COMPLEX);
341 static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
342 unsigned int *idxp)
344 int i;
345 hda_nid_t nid;
347 nid = codec->start_nid;
348 for (i = 0; i < codec->num_nodes; i++, nid++) {
349 hda_nid_t pins[2];
350 unsigned int type;
351 int j, nums;
352 type = (get_wcaps(codec, nid) & AC_WCAP_TYPE)
353 >> AC_WCAP_TYPE_SHIFT;
354 if (type != AC_WID_AUD_IN)
355 continue;
356 nums = snd_hda_get_connections(codec, nid, pins,
357 ARRAY_SIZE(pins));
358 if (nums <= 0)
359 continue;
360 for (j = 0; j < nums; j++) {
361 if (pins[j] == pin) {
362 *idxp = j;
363 return nid;
367 return 0;
370 static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
372 unsigned int val;
373 val = snd_hda_codec_get_pincfg(codec, nid);
374 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
377 static int parse_output(struct hda_codec *codec)
379 struct cs_spec *spec = codec->spec;
380 struct auto_pin_cfg *cfg = &spec->autocfg;
381 int i, extra_nids;
382 hda_nid_t dac;
384 for (i = 0; i < cfg->line_outs; i++) {
385 dac = get_dac(codec, cfg->line_out_pins[i]);
386 if (!dac)
387 break;
388 spec->dac_nid[i] = dac;
390 spec->multiout.num_dacs = i;
391 spec->multiout.dac_nids = spec->dac_nid;
392 spec->multiout.max_channels = i * 2;
394 /* add HP and speakers */
395 extra_nids = 0;
396 for (i = 0; i < cfg->hp_outs; i++) {
397 dac = get_dac(codec, cfg->hp_pins[i]);
398 if (!dac)
399 break;
400 if (!i)
401 spec->multiout.hp_nid = dac;
402 else
403 spec->multiout.extra_out_nid[extra_nids++] = dac;
405 for (i = 0; i < cfg->speaker_outs; i++) {
406 dac = get_dac(codec, cfg->speaker_pins[i]);
407 if (!dac)
408 break;
409 spec->multiout.extra_out_nid[extra_nids++] = dac;
412 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
413 cfg->speaker_outs = cfg->line_outs;
414 memcpy(cfg->speaker_pins, cfg->line_out_pins,
415 sizeof(cfg->speaker_pins));
416 cfg->line_outs = 0;
419 return 0;
422 static int parse_input(struct hda_codec *codec)
424 struct cs_spec *spec = codec->spec;
425 struct auto_pin_cfg *cfg = &spec->autocfg;
426 int i;
428 for (i = 0; i < AUTO_PIN_LAST; i++) {
429 hda_nid_t pin = cfg->input_pins[i];
430 if (!pin)
431 continue;
432 spec->input_idx[spec->num_inputs] = i;
433 spec->capsrc_idx[i] = spec->num_inputs++;
434 spec->cur_input = i;
435 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
437 if (!spec->num_inputs)
438 return 0;
440 /* check whether the automatic mic switch is available */
441 if (spec->num_inputs == 2 &&
442 spec->adc_nid[AUTO_PIN_MIC] && spec->adc_nid[AUTO_PIN_FRONT_MIC]) {
443 if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_FRONT_MIC])) {
444 if (!is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
445 spec->mic_detect = 1;
446 spec->automic_idx = AUTO_PIN_FRONT_MIC;
448 } else {
449 if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
450 spec->mic_detect = 1;
451 spec->automic_idx = AUTO_PIN_MIC;
455 return 0;
459 static int parse_digital_output(struct hda_codec *codec)
461 struct cs_spec *spec = codec->spec;
462 struct auto_pin_cfg *cfg = &spec->autocfg;
463 hda_nid_t nid;
465 if (!cfg->dig_outs)
466 return 0;
467 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
468 return 0;
469 spec->multiout.dig_out_nid = nid;
470 spec->multiout.share_spdif = 1;
471 if (cfg->dig_outs > 1 &&
472 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
473 spec->slave_dig_outs[0] = nid;
474 codec->slave_dig_outs = spec->slave_dig_outs;
476 return 0;
479 static int parse_digital_input(struct hda_codec *codec)
481 struct cs_spec *spec = codec->spec;
482 struct auto_pin_cfg *cfg = &spec->autocfg;
483 int idx;
485 if (cfg->dig_in_pin)
486 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
487 return 0;
491 * create mixer controls
494 static const char *dir_sfx[2] = { "Playback", "Capture" };
496 static int add_mute(struct hda_codec *codec, const char *name, int index,
497 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
499 char tmp[44];
500 struct snd_kcontrol_new knew =
501 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
502 knew.private_value = pval;
503 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
504 *kctlp = snd_ctl_new1(&knew, codec);
505 return snd_hda_ctl_add(codec, *kctlp);
508 static int add_volume(struct hda_codec *codec, const char *name,
509 int index, unsigned int pval, int dir,
510 struct snd_kcontrol **kctlp)
512 char tmp[32];
513 struct snd_kcontrol_new knew =
514 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
515 knew.private_value = pval;
516 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
517 *kctlp = snd_ctl_new1(&knew, codec);
518 return snd_hda_ctl_add(codec, *kctlp);
521 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
523 unsigned int caps;
525 /* set the upper-limit for mixer amp to 0dB */
526 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
527 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
528 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
529 << AC_AMPCAP_NUM_STEPS_SHIFT;
530 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
533 static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
535 struct cs_spec *spec = codec->spec;
536 unsigned int tlv[4];
537 int err;
539 spec->vmaster_sw =
540 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
541 err = snd_hda_ctl_add(codec, spec->vmaster_sw);
542 if (err < 0)
543 return err;
545 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
546 spec->vmaster_vol =
547 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
548 err = snd_hda_ctl_add(codec, spec->vmaster_vol);
549 if (err < 0)
550 return err;
551 return 0;
554 static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
555 int num_ctls, int type)
557 struct cs_spec *spec = codec->spec;
558 const char *name;
559 int err, index;
560 struct snd_kcontrol *kctl;
561 static char *speakers[] = {
562 "Front Speaker", "Surround Speaker", "Bass Speaker"
564 static char *line_outs[] = {
565 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
568 fix_volume_caps(codec, dac);
569 if (!spec->vmaster_sw) {
570 err = add_vmaster(codec, dac);
571 if (err < 0)
572 return err;
575 index = 0;
576 switch (type) {
577 case AUTO_PIN_HP_OUT:
578 name = "Headphone";
579 index = idx;
580 break;
581 case AUTO_PIN_SPEAKER_OUT:
582 if (num_ctls > 1)
583 name = speakers[idx];
584 else
585 name = "Speaker";
586 break;
587 default:
588 if (num_ctls > 1)
589 name = line_outs[idx];
590 else
591 name = "Line-Out";
592 break;
595 err = add_mute(codec, name, index,
596 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
597 if (err < 0)
598 return err;
599 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
600 if (err < 0)
601 return err;
603 err = add_volume(codec, name, index,
604 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
605 if (err < 0)
606 return err;
607 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
608 if (err < 0)
609 return err;
611 return 0;
614 static int build_output(struct hda_codec *codec)
616 struct cs_spec *spec = codec->spec;
617 struct auto_pin_cfg *cfg = &spec->autocfg;
618 int i, err;
620 for (i = 0; i < cfg->line_outs; i++) {
621 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
622 i, cfg->line_outs, cfg->line_out_type);
623 if (err < 0)
624 return err;
626 for (i = 0; i < cfg->hp_outs; i++) {
627 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
628 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
629 if (err < 0)
630 return err;
632 for (i = 0; i < cfg->speaker_outs; i++) {
633 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
634 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
635 if (err < 0)
636 return err;
638 return 0;
644 static struct snd_kcontrol_new cs_capture_ctls[] = {
645 HDA_BIND_SW("Capture Switch", 0),
646 HDA_BIND_VOL("Capture Volume", 0),
649 static int change_cur_input(struct hda_codec *codec, unsigned int idx,
650 int force)
652 struct cs_spec *spec = codec->spec;
654 if (spec->cur_input == idx && !force)
655 return 0;
656 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
657 /* stream is running, let's swap the current ADC */
658 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
659 spec->cur_adc = spec->adc_nid[idx];
660 snd_hda_codec_setup_stream(codec, spec->cur_adc,
661 spec->cur_adc_stream_tag, 0,
662 spec->cur_adc_format);
664 snd_hda_codec_write(codec, spec->cur_adc, 0,
665 AC_VERB_SET_CONNECT_SEL,
666 spec->adc_idx[idx]);
667 spec->cur_input = idx;
668 return 1;
671 static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
672 struct snd_ctl_elem_info *uinfo)
674 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
675 struct cs_spec *spec = codec->spec;
676 unsigned int idx;
678 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
679 uinfo->count = 1;
680 uinfo->value.enumerated.items = spec->num_inputs;
681 if (uinfo->value.enumerated.item >= spec->num_inputs)
682 uinfo->value.enumerated.item = spec->num_inputs - 1;
683 idx = spec->input_idx[uinfo->value.enumerated.item];
684 strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
685 return 0;
688 static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
689 struct snd_ctl_elem_value *ucontrol)
691 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
692 struct cs_spec *spec = codec->spec;
693 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
694 return 0;
697 static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
698 struct snd_ctl_elem_value *ucontrol)
700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
701 struct cs_spec *spec = codec->spec;
702 unsigned int idx = ucontrol->value.enumerated.item[0];
704 if (idx >= spec->num_inputs)
705 return -EINVAL;
706 idx = spec->input_idx[idx];
707 return change_cur_input(codec, idx, 0);
710 static struct snd_kcontrol_new cs_capture_source = {
711 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
712 .name = "Capture Source",
713 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
714 .info = cs_capture_source_info,
715 .get = cs_capture_source_get,
716 .put = cs_capture_source_put,
719 static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
720 struct hda_ctl_ops *ops)
722 struct cs_spec *spec = codec->spec;
723 struct hda_bind_ctls *bind;
724 int i, n;
726 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
727 GFP_KERNEL);
728 if (!bind)
729 return NULL;
730 bind->ops = ops;
731 n = 0;
732 for (i = 0; i < AUTO_PIN_LAST; i++) {
733 if (!spec->adc_nid[i])
734 continue;
735 bind->values[n++] =
736 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
737 spec->adc_idx[i], HDA_INPUT);
739 return bind;
742 static int build_input(struct hda_codec *codec)
744 struct cs_spec *spec = codec->spec;
745 int i, err;
747 if (!spec->num_inputs)
748 return 0;
750 /* make bind-capture */
751 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
752 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
753 for (i = 0; i < 2; i++) {
754 struct snd_kcontrol *kctl;
755 if (!spec->capture_bind[i])
756 return -ENOMEM;
757 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
758 if (!kctl)
759 return -ENOMEM;
760 kctl->private_value = (long)spec->capture_bind[i];
761 err = snd_hda_ctl_add(codec, kctl);
762 if (err < 0)
763 return err;
766 if (spec->num_inputs > 1 && !spec->mic_detect) {
767 err = snd_hda_ctl_add(codec,
768 snd_ctl_new1(&cs_capture_source, codec));
769 if (err < 0)
770 return err;
773 return 0;
779 static int build_digital_output(struct hda_codec *codec)
781 struct cs_spec *spec = codec->spec;
782 int err;
784 if (!spec->multiout.dig_out_nid)
785 return 0;
787 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
788 if (err < 0)
789 return err;
790 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
791 if (err < 0)
792 return err;
793 return 0;
796 static int build_digital_input(struct hda_codec *codec)
798 struct cs_spec *spec = codec->spec;
799 if (spec->dig_in)
800 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
801 return 0;
805 * auto-mute and auto-mic switching
808 static void cs_automute(struct hda_codec *codec)
810 struct cs_spec *spec = codec->spec;
811 struct auto_pin_cfg *cfg = &spec->autocfg;
812 unsigned int caps, present, hp_present;
813 hda_nid_t nid;
814 int i;
816 hp_present = 0;
817 for (i = 0; i < cfg->hp_outs; i++) {
818 nid = cfg->hp_pins[i];
819 caps = snd_hda_query_pin_caps(codec, nid);
820 if (!(caps & AC_PINCAP_PRES_DETECT))
821 continue;
822 if (caps & AC_PINCAP_TRIG_REQ)
823 snd_hda_codec_read(codec, nid, 0,
824 AC_VERB_SET_PIN_SENSE, 0);
825 present = snd_hda_codec_read(codec, nid, 0,
826 AC_VERB_GET_PIN_SENSE, 0);
827 hp_present |= (present & AC_PINSENSE_PRESENCE) != 0;
828 if (hp_present)
829 break;
831 for (i = 0; i < cfg->speaker_outs; i++) {
832 nid = cfg->speaker_pins[i];
833 snd_hda_codec_write(codec, nid, 0,
834 AC_VERB_SET_PIN_WIDGET_CONTROL,
835 hp_present ? 0 : PIN_OUT);
837 if (spec->board_config == CS420X_MBP53 ||
838 spec->board_config == CS420X_MBP55 ||
839 spec->board_config == CS420X_IMAC27) {
840 unsigned int gpio = hp_present ? 0x02 : 0x08;
841 snd_hda_codec_write(codec, 0x01, 0,
842 AC_VERB_SET_GPIO_DATA, gpio);
846 static void cs_automic(struct hda_codec *codec)
848 struct cs_spec *spec = codec->spec;
849 struct auto_pin_cfg *cfg = &spec->autocfg;
850 hda_nid_t nid;
851 unsigned int caps, present;
853 nid = cfg->input_pins[spec->automic_idx];
854 caps = snd_hda_query_pin_caps(codec, nid);
855 if (caps & AC_PINCAP_TRIG_REQ)
856 snd_hda_codec_read(codec, nid, 0, AC_VERB_SET_PIN_SENSE, 0);
857 present = snd_hda_codec_read(codec, nid, 0,
858 AC_VERB_GET_PIN_SENSE, 0);
859 if (present & AC_PINSENSE_PRESENCE)
860 change_cur_input(codec, spec->automic_idx, 0);
861 else {
862 unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
863 AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
864 change_cur_input(codec, imic, 0);
871 static void init_output(struct hda_codec *codec)
873 struct cs_spec *spec = codec->spec;
874 struct auto_pin_cfg *cfg = &spec->autocfg;
875 int i;
877 /* mute first */
878 for (i = 0; i < spec->multiout.num_dacs; i++)
879 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
880 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
881 if (spec->multiout.hp_nid)
882 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
883 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
884 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
885 if (!spec->multiout.extra_out_nid[i])
886 break;
887 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
888 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
891 /* set appropriate pin controls */
892 for (i = 0; i < cfg->line_outs; i++)
893 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
894 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
895 for (i = 0; i < cfg->hp_outs; i++) {
896 hda_nid_t nid = cfg->hp_pins[i];
897 snd_hda_codec_write(codec, nid, 0,
898 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
899 if (!cfg->speaker_outs)
900 continue;
901 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
902 snd_hda_codec_write(codec, nid, 0,
903 AC_VERB_SET_UNSOLICITED_ENABLE,
904 AC_USRSP_EN | HP_EVENT);
905 spec->hp_detect = 1;
908 for (i = 0; i < cfg->speaker_outs; i++)
909 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
910 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
911 if (spec->hp_detect)
912 cs_automute(codec);
915 static void init_input(struct hda_codec *codec)
917 struct cs_spec *spec = codec->spec;
918 struct auto_pin_cfg *cfg = &spec->autocfg;
919 unsigned int coef;
920 int i;
922 for (i = 0; i < AUTO_PIN_LAST; i++) {
923 unsigned int ctl;
924 hda_nid_t pin = cfg->input_pins[i];
925 if (!pin || !spec->adc_nid[i])
926 continue;
927 /* set appropriate pin control and mute first */
928 ctl = PIN_IN;
929 if (i <= AUTO_PIN_FRONT_MIC) {
930 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
931 caps >>= AC_PINCAP_VREF_SHIFT;
932 if (caps & AC_PINCAP_VREF_80)
933 ctl = PIN_VREF80;
935 snd_hda_codec_write(codec, pin, 0,
936 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
937 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
938 AC_VERB_SET_AMP_GAIN_MUTE,
939 AMP_IN_MUTE(spec->adc_idx[i]));
940 if (spec->mic_detect && spec->automic_idx == i)
941 snd_hda_codec_write(codec, pin, 0,
942 AC_VERB_SET_UNSOLICITED_ENABLE,
943 AC_USRSP_EN | MIC_EVENT);
945 change_cur_input(codec, spec->cur_input, 1);
946 if (spec->mic_detect)
947 cs_automic(codec);
949 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
950 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
951 coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
952 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
953 coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0
954 * No effect if SPDIF_OUT2 is slected in
955 * IDX_SPDIF_CTL.
957 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
960 static struct hda_verb cs_coef_init_verbs[] = {
961 {0x11, AC_VERB_SET_PROC_STATE, 1},
962 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
963 {0x11, AC_VERB_SET_PROC_COEF,
964 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
965 | 0x0040 /* Mute DACs on FIFO error */
966 | 0x1000 /* Enable DACs High Pass Filter */
967 | 0x0400 /* Disable Coefficient Auto increment */
969 /* Beep */
970 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
971 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
973 {} /* terminator */
976 /* SPDIF setup */
977 static void init_digital(struct hda_codec *codec)
979 unsigned int coef;
981 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
982 coef |= 0x0008; /* Replace with mute on error */
983 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
984 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
985 * SPDIF_OUT2 is shared with GPIO1 and
986 * DMIC_SDA2.
988 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
991 static int cs_init(struct hda_codec *codec)
993 struct cs_spec *spec = codec->spec;
995 snd_hda_sequence_write(codec, cs_coef_init_verbs);
997 if (spec->gpio_mask) {
998 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
999 spec->gpio_mask);
1000 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1001 spec->gpio_dir);
1002 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1003 spec->gpio_data);
1006 init_output(codec);
1007 init_input(codec);
1008 init_digital(codec);
1009 return 0;
1012 static int cs_build_controls(struct hda_codec *codec)
1014 int err;
1016 err = build_output(codec);
1017 if (err < 0)
1018 return err;
1019 err = build_input(codec);
1020 if (err < 0)
1021 return err;
1022 err = build_digital_output(codec);
1023 if (err < 0)
1024 return err;
1025 err = build_digital_input(codec);
1026 if (err < 0)
1027 return err;
1028 return cs_init(codec);
1031 static void cs_free(struct hda_codec *codec)
1033 struct cs_spec *spec = codec->spec;
1034 kfree(spec->capture_bind[0]);
1035 kfree(spec->capture_bind[1]);
1036 kfree(codec->spec);
1039 static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1041 switch ((res >> 26) & 0x7f) {
1042 case HP_EVENT:
1043 cs_automute(codec);
1044 break;
1045 case MIC_EVENT:
1046 cs_automic(codec);
1047 break;
1051 static struct hda_codec_ops cs_patch_ops = {
1052 .build_controls = cs_build_controls,
1053 .build_pcms = cs_build_pcms,
1054 .init = cs_init,
1055 .free = cs_free,
1056 .unsol_event = cs_unsol_event,
1059 static int cs_parse_auto_config(struct hda_codec *codec)
1061 struct cs_spec *spec = codec->spec;
1062 int err;
1064 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1065 if (err < 0)
1066 return err;
1068 err = parse_output(codec);
1069 if (err < 0)
1070 return err;
1071 err = parse_input(codec);
1072 if (err < 0)
1073 return err;
1074 err = parse_digital_output(codec);
1075 if (err < 0)
1076 return err;
1077 err = parse_digital_input(codec);
1078 if (err < 0)
1079 return err;
1080 return 0;
1083 static const char *cs420x_models[CS420X_MODELS] = {
1084 [CS420X_MBP53] = "mbp53",
1085 [CS420X_MBP55] = "mbp55",
1086 [CS420X_IMAC27] = "imac27",
1087 [CS420X_AUTO] = "auto",
1091 static struct snd_pci_quirk cs420x_cfg_tbl[] = {
1092 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
1093 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1094 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
1095 {} /* terminator */
1098 struct cs_pincfg {
1099 hda_nid_t nid;
1100 u32 val;
1103 static struct cs_pincfg mbp53_pincfgs[] = {
1104 { 0x09, 0x012b4050 },
1105 { 0x0a, 0x90100141 },
1106 { 0x0b, 0x90100140 },
1107 { 0x0c, 0x018b3020 },
1108 { 0x0d, 0x90a00110 },
1109 { 0x0e, 0x400000f0 },
1110 { 0x0f, 0x01cbe030 },
1111 { 0x10, 0x014be060 },
1112 { 0x12, 0x400000f0 },
1113 { 0x15, 0x400000f0 },
1114 {} /* terminator */
1117 static struct cs_pincfg mbp55_pincfgs[] = {
1118 { 0x09, 0x012b4030 },
1119 { 0x0a, 0x90100121 },
1120 { 0x0b, 0x90100120 },
1121 { 0x0c, 0x400000f0 },
1122 { 0x0d, 0x90a00110 },
1123 { 0x0e, 0x400000f0 },
1124 { 0x0f, 0x400000f0 },
1125 { 0x10, 0x014be040 },
1126 { 0x12, 0x400000f0 },
1127 { 0x15, 0x400000f0 },
1128 {} /* terminator */
1131 static struct cs_pincfg imac27_pincfgs[] = {
1132 { 0x09, 0x012b4050 },
1133 { 0x0a, 0x90100140 },
1134 { 0x0b, 0x90100142 },
1135 { 0x0c, 0x018b3020 },
1136 { 0x0d, 0x90a00110 },
1137 { 0x0e, 0x400000f0 },
1138 { 0x0f, 0x01cbe030 },
1139 { 0x10, 0x014be060 },
1140 { 0x12, 0x01ab9070 },
1141 { 0x15, 0x400000f0 },
1142 {} /* terminator */
1145 static struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1146 [CS420X_MBP53] = mbp53_pincfgs,
1147 [CS420X_MBP55] = mbp55_pincfgs,
1148 [CS420X_IMAC27] = imac27_pincfgs,
1151 static void fix_pincfg(struct hda_codec *codec, int model)
1153 const struct cs_pincfg *cfg = cs_pincfgs[model];
1154 if (!cfg)
1155 return;
1156 for (; cfg->nid; cfg++)
1157 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1161 static int patch_cs420x(struct hda_codec *codec)
1163 struct cs_spec *spec;
1164 int err;
1166 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1167 if (!spec)
1168 return -ENOMEM;
1169 codec->spec = spec;
1171 spec->board_config =
1172 snd_hda_check_board_config(codec, CS420X_MODELS,
1173 cs420x_models, cs420x_cfg_tbl);
1174 if (spec->board_config >= 0)
1175 fix_pincfg(codec, spec->board_config);
1177 switch (spec->board_config) {
1178 case CS420X_IMAC27:
1179 case CS420X_MBP53:
1180 case CS420X_MBP55:
1181 /* GPIO1 = headphones */
1182 /* GPIO3 = speakers */
1183 spec->gpio_mask = 0x0a;
1184 spec->gpio_dir = 0x0a;
1185 break;
1188 err = cs_parse_auto_config(codec);
1189 if (err < 0)
1190 goto error;
1192 codec->patch_ops = cs_patch_ops;
1194 return 0;
1196 error:
1197 kfree(codec->spec);
1198 codec->spec = NULL;
1199 return err;
1204 * patch entries
1206 static struct hda_codec_preset snd_hda_preset_cirrus[] = {
1207 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1208 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1209 {} /* terminator */
1212 MODULE_ALIAS("snd-hda-codec-id:10134206");
1213 MODULE_ALIAS("snd-hda-codec-id:10134207");
1215 MODULE_LICENSE("GPL");
1216 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1218 static struct hda_codec_preset_list cirrus_list = {
1219 .preset = snd_hda_preset_cirrus,
1220 .owner = THIS_MODULE,
1223 static int __init patch_cirrus_init(void)
1225 return snd_hda_add_codec_preset(&cirrus_list);
1228 static void __exit patch_cirrus_exit(void)
1230 snd_hda_delete_codec_preset(&cirrus_list);
1233 module_init(patch_cirrus_init)
1234 module_exit(patch_cirrus_exit)