]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/pci/hda/patch_cirrus.c
ALSA: hda - Add GPIO setup for MacBook pro 5,5 with CS420x
[net-next-2.6.git] / sound / pci / hda / patch_cirrus.c
CommitLineData
e5f14248
TI
1/*
2 * HD audio interface patch for Cirrus Logic CS420x chip
3 *
4 * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
5 *
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.
10 *
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.
15 *
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
19 */
20
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
29/*
30 */
31
32struct cs_spec {
a6bae205 33 int board_config;
e5f14248
TI
34 struct auto_pin_cfg autocfg;
35 struct hda_multi_out multiout;
36 struct snd_kcontrol *vmaster_sw;
37 struct snd_kcontrol *vmaster_vol;
38
39 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
40 hda_nid_t slave_dig_outs[2];
41
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;
53
54 struct hda_bind_ctls *capture_bind[2];
55
ed208255
TI
56 unsigned int gpio_mask;
57 unsigned int gpio_dir;
58 unsigned int gpio_data;
59
e5f14248
TI
60 struct hda_pcm pcm_rec[2]; /* PCM information */
61
62 unsigned int hp_detect:1;
63 unsigned int mic_detect:1;
e5f14248
TI
64};
65
a6bae205
TI
66/* available models */
67enum {
68 CS420X_MBP55,
69 CS420X_AUTO,
70 CS420X_MODELS
71};
72
40c20fa0
TI
73/* Vendor-specific processing widget */
74#define CS420X_VENDOR_NID 0x11
75#define CS_DIG_OUT1_PIN_NID 0x10
76#define CS_DIG_OUT2_PIN_NID 0x15
77#define CS_DMIC1_PIN_NID 0x12
78#define CS_DMIC2_PIN_NID 0x0e
79
80/* coef indices */
81#define IDX_SPDIF_STAT 0x0000
82#define IDX_SPDIF_CTL 0x0001
83#define IDX_ADC_CFG 0x0002
84/* SZC bitmask, 4 modes below:
85 * 0 = immediate,
86 * 1 = digital immediate, analog zero-cross
87 * 2 = digtail & analog soft-ramp
88 * 3 = digital soft-ramp, analog zero-cross
89 */
90#define CS_COEF_ADC_SZC_MASK (3 << 0)
91#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
92#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
93/* PGA mode: 0 = differential, 1 = signle-ended */
94#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
95#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
96#define IDX_DAC_CFG 0x0003
97/* SZC bitmask, 4 modes below:
98 * 0 = Immediate
99 * 1 = zero-cross
100 * 2 = soft-ramp
101 * 3 = soft-ramp on zero-cross
102 */
103#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
104#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
105#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
106
107#define IDX_BEEP_CFG 0x0004
108/* 0x0008 - test reg key */
109/* 0x0009 - 0x0014 -> 12 test regs */
110/* 0x0015 - visibility reg */
111
112
113static int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
114{
115 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
116 AC_VERB_SET_COEF_INDEX, idx);
117 return snd_hda_codec_read(codec, CS420X_VENDOR_NID, 0,
118 AC_VERB_GET_PROC_COEF, 0);
119}
120
121static void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
122 unsigned int coef)
123{
124 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
125 AC_VERB_SET_COEF_INDEX, idx);
126 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
127 AC_VERB_SET_PROC_COEF, coef);
128}
129
130
e5f14248
TI
131#define HP_EVENT 1
132#define MIC_EVENT 2
133
134/*
135 * PCM callbacks
136 */
137static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
138 struct hda_codec *codec,
139 struct snd_pcm_substream *substream)
140{
141 struct cs_spec *spec = codec->spec;
142 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
143 hinfo);
144}
145
146static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
147 struct hda_codec *codec,
148 unsigned int stream_tag,
149 unsigned int format,
150 struct snd_pcm_substream *substream)
151{
152 struct cs_spec *spec = codec->spec;
153 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
154 stream_tag, format, substream);
155}
156
157static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
158 struct hda_codec *codec,
159 struct snd_pcm_substream *substream)
160{
161 struct cs_spec *spec = codec->spec;
162 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
163}
164
165/*
166 * Digital out
167 */
168static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
169 struct hda_codec *codec,
170 struct snd_pcm_substream *substream)
171{
172 struct cs_spec *spec = codec->spec;
173 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
174}
175
176static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
177 struct hda_codec *codec,
178 struct snd_pcm_substream *substream)
179{
180 struct cs_spec *spec = codec->spec;
181 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
182}
183
184static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
185 struct hda_codec *codec,
186 unsigned int stream_tag,
187 unsigned int format,
188 struct snd_pcm_substream *substream)
189{
190 struct cs_spec *spec = codec->spec;
191 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
192 format, substream);
193}
194
195static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
196 struct hda_codec *codec,
197 struct snd_pcm_substream *substream)
198{
199 struct cs_spec *spec = codec->spec;
200 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
201}
202
203/*
204 * Analog capture
205 */
206static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
207 struct hda_codec *codec,
208 unsigned int stream_tag,
209 unsigned int format,
210 struct snd_pcm_substream *substream)
211{
212 struct cs_spec *spec = codec->spec;
213 spec->cur_adc = spec->adc_nid[spec->cur_input];
214 spec->cur_adc_stream_tag = stream_tag;
215 spec->cur_adc_format = format;
216 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
217 return 0;
218}
219
220static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
221 struct hda_codec *codec,
222 struct snd_pcm_substream *substream)
223{
224 struct cs_spec *spec = codec->spec;
225 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
226 spec->cur_adc = 0;
227 return 0;
228}
229
230/*
231 */
232static struct hda_pcm_stream cs_pcm_analog_playback = {
233 .substreams = 1,
234 .channels_min = 2,
235 .channels_max = 2,
236 .ops = {
237 .open = cs_playback_pcm_open,
238 .prepare = cs_playback_pcm_prepare,
239 .cleanup = cs_playback_pcm_cleanup
240 },
241};
242
243static struct hda_pcm_stream cs_pcm_analog_capture = {
244 .substreams = 1,
245 .channels_min = 2,
246 .channels_max = 2,
247 .ops = {
248 .prepare = cs_capture_pcm_prepare,
249 .cleanup = cs_capture_pcm_cleanup
250 },
251};
252
253static struct hda_pcm_stream cs_pcm_digital_playback = {
254 .substreams = 1,
255 .channels_min = 2,
256 .channels_max = 2,
257 .ops = {
258 .open = cs_dig_playback_pcm_open,
259 .close = cs_dig_playback_pcm_close,
260 .prepare = cs_dig_playback_pcm_prepare,
261 .cleanup = cs_dig_playback_pcm_cleanup
262 },
263};
264
265static struct hda_pcm_stream cs_pcm_digital_capture = {
266 .substreams = 1,
267 .channels_min = 2,
268 .channels_max = 2,
269};
270
271static int cs_build_pcms(struct hda_codec *codec)
272{
273 struct cs_spec *spec = codec->spec;
274 struct hda_pcm *info = spec->pcm_rec;
275
276 codec->pcm_info = info;
277 codec->num_pcms = 0;
278
279 info->name = "Cirrus Analog";
280 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
281 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
282 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
283 spec->multiout.max_channels;
284 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
e5f14248
TI
285 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
286 spec->adc_nid[spec->cur_input];
287 codec->num_pcms++;
288
289 if (!spec->multiout.dig_out_nid && !spec->dig_in)
290 return 0;
291
292 info++;
293 info->name = "Cirrus Digital";
294 info->pcm_type = spec->autocfg.dig_out_type[0];
295 if (!info->pcm_type)
296 info->pcm_type = HDA_PCM_TYPE_SPDIF;
297 if (spec->multiout.dig_out_nid) {
298 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
299 cs_pcm_digital_playback;
300 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
301 spec->multiout.dig_out_nid;
302 }
303 if (spec->dig_in) {
304 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
305 cs_pcm_digital_capture;
306 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
307 }
308 codec->num_pcms++;
309
310 return 0;
311}
312
21a4dc43
TI
313/*
314 * parse codec topology
315 */
316
e5f14248
TI
317static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
318{
319 hda_nid_t dac;
320 if (!pin)
321 return 0;
322 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
323 return 0;
324 return dac;
325}
326
21a4dc43
TI
327static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
328{
329 struct cs_spec *spec = codec->spec;
330 struct auto_pin_cfg *cfg = &spec->autocfg;
331 hda_nid_t pin = cfg->input_pins[idx];
332 unsigned int val = snd_hda_query_pin_caps(codec, pin);
333 if (!(val & AC_PINCAP_PRES_DETECT))
334 return 0;
335 val = snd_hda_codec_get_pincfg(codec, pin);
336 return (get_defcfg_connect(val) == AC_JACK_PORT_COMPLEX);
337}
338
339static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
340 unsigned int *idxp)
341{
342 int i;
343 hda_nid_t nid;
344
345 nid = codec->start_nid;
346 for (i = 0; i < codec->num_nodes; i++, nid++) {
347 hda_nid_t pins[2];
348 unsigned int type;
349 int j, nums;
350 type = (get_wcaps(codec, nid) & AC_WCAP_TYPE)
351 >> AC_WCAP_TYPE_SHIFT;
352 if (type != AC_WID_AUD_IN)
353 continue;
354 nums = snd_hda_get_connections(codec, nid, pins,
355 ARRAY_SIZE(pins));
356 if (nums <= 0)
357 continue;
358 for (j = 0; j < nums; j++) {
359 if (pins[j] == pin) {
360 *idxp = j;
361 return nid;
362 }
363 }
364 }
365 return 0;
366}
367
40c20fa0
TI
368static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
369{
370 struct cs_spec *spec = codec->spec;
371 unsigned int val;
372 val = snd_hda_codec_get_pincfg(codec, nid);
373 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
374}
375
21a4dc43
TI
376static int parse_output(struct hda_codec *codec)
377{
378 struct cs_spec *spec = codec->spec;
379 struct auto_pin_cfg *cfg = &spec->autocfg;
380 int i, err, extra_nids;
381 hda_nid_t dac;
382
383 for (i = 0; i < cfg->line_outs; i++) {
384 dac = get_dac(codec, cfg->line_out_pins[i]);
385 if (!dac)
386 break;
387 spec->dac_nid[i] = dac;
388 }
389 spec->multiout.num_dacs = i;
390 spec->multiout.dac_nids = spec->dac_nid;
391 spec->multiout.max_channels = i * 2;
392
393 /* add HP and speakers */
394 extra_nids = 0;
395 for (i = 0; i < cfg->hp_outs; i++) {
396 dac = get_dac(codec, cfg->hp_pins[i]);
397 if (!dac)
398 break;
399 if (!i)
400 spec->multiout.hp_nid = dac;
401 else
402 spec->multiout.extra_out_nid[extra_nids++] = dac;
403 }
404 for (i = 0; i < cfg->speaker_outs; i++) {
405 dac = get_dac(codec, cfg->speaker_pins[i]);
406 if (!dac)
407 break;
408 spec->multiout.extra_out_nid[extra_nids++] = dac;
409 }
410
411 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
412 cfg->speaker_outs = cfg->line_outs;
413 memcpy(cfg->speaker_pins, cfg->line_out_pins,
414 sizeof(cfg->speaker_pins));
415 cfg->line_outs = 0;
416 }
417
418 return 0;
419}
420
421static int parse_input(struct hda_codec *codec)
422{
423 struct cs_spec *spec = codec->spec;
424 struct auto_pin_cfg *cfg = &spec->autocfg;
425 int i, n, err;
426
427 for (i = 0; i < AUTO_PIN_LAST; i++) {
428 hda_nid_t pin = cfg->input_pins[i];
429 struct snd_kcontrol *kctl;
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]);
436 }
437 if (!spec->num_inputs)
438 return 0;
439
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;
447 }
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;
452 }
453 }
454 }
455 return 0;
456}
457
458
459static int parse_digital_output(struct hda_codec *codec)
460{
461 struct cs_spec *spec = codec->spec;
462 struct auto_pin_cfg *cfg = &spec->autocfg;
463 hda_nid_t nid;
464 int err;
465
466 if (!cfg->dig_outs)
467 return 0;
468 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
469 return 0;
470 spec->multiout.dig_out_nid = nid;
471 spec->multiout.share_spdif = 1;
472 if (cfg->dig_outs > 1 &&
473 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
474 spec->slave_dig_outs[0] = nid;
475 codec->slave_dig_outs = spec->slave_dig_outs;
476 }
477 return 0;
478}
479
480static int parse_digital_input(struct hda_codec *codec)
481{
482 struct cs_spec *spec = codec->spec;
483 struct auto_pin_cfg *cfg = &spec->autocfg;
484 int idx;
485
60e53882
TI
486 if (cfg->dig_in_pin)
487 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
488 return 0;
21a4dc43
TI
489}
490
491/*
492 * create mixer controls
493 */
494
e5f14248
TI
495static const char *dir_sfx[2] = { "Playback", "Capture" };
496
497static int add_mute(struct hda_codec *codec, const char *name, int index,
498 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
499{
500 char tmp[32];
501 struct snd_kcontrol_new knew =
502 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
503 knew.private_value = pval;
504 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
505 *kctlp = snd_ctl_new1(&knew, codec);
506 return snd_hda_ctl_add(codec, *kctlp);
507}
508
509static int add_volume(struct hda_codec *codec, const char *name,
510 int index, unsigned int pval, int dir,
511 struct snd_kcontrol **kctlp)
512{
513 char tmp[32];
514 struct snd_kcontrol_new knew =
515 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
516 knew.private_value = pval;
517 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
518 *kctlp = snd_ctl_new1(&knew, codec);
519 return snd_hda_ctl_add(codec, *kctlp);
520}
521
522static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
523{
524 unsigned int caps;
525
526 /* set the upper-limit for mixer amp to 0dB */
527 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
528 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
529 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
530 << AC_AMPCAP_NUM_STEPS_SHIFT;
531 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
532}
533
534static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
535{
536 struct cs_spec *spec = codec->spec;
537 unsigned int tlv[4];
538 int err;
539
540 spec->vmaster_sw =
541 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
542 err = snd_hda_ctl_add(codec, spec->vmaster_sw);
543 if (err < 0)
544 return err;
545
546 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
547 spec->vmaster_vol =
548 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
549 err = snd_hda_ctl_add(codec, spec->vmaster_vol);
550 if (err < 0)
551 return err;
552 return 0;
553}
554
555static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
556 int num_ctls, int type)
557{
558 struct cs_spec *spec = codec->spec;
559 const char *name;
560 int err, index;
561 struct snd_kcontrol *kctl;
562 static char *speakers[] = {
563 "Front Speaker", "Surround Speaker", "Bass Speaker"
564 };
565 static char *line_outs[] = {
566 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
567 };
568
569 fix_volume_caps(codec, dac);
570 if (!spec->vmaster_sw) {
571 err = add_vmaster(codec, dac);
572 if (err < 0)
573 return err;
574 }
575
576 index = 0;
577 switch (type) {
578 case AUTO_PIN_HP_OUT:
579 name = "Headphone";
580 index = idx;
581 break;
582 case AUTO_PIN_SPEAKER_OUT:
583 if (num_ctls > 1)
584 name = speakers[idx];
585 else
586 name = "Speaker";
587 break;
588 default:
589 if (num_ctls > 1)
590 name = line_outs[idx];
591 else
592 name = "Line-Out";
593 break;
594 }
595
596 err = add_mute(codec, name, index,
597 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
598 if (err < 0)
599 return err;
600 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
601 if (err < 0)
602 return err;
603
604 err = add_volume(codec, name, index,
605 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
606 if (err < 0)
607 return err;
608 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
609 if (err < 0)
610 return err;
611
612 return 0;
613}
614
615static int build_output(struct hda_codec *codec)
616{
617 struct cs_spec *spec = codec->spec;
618 struct auto_pin_cfg *cfg = &spec->autocfg;
21a4dc43 619 int i, err;
e5f14248
TI
620
621 for (i = 0; i < cfg->line_outs; i++) {
21a4dc43
TI
622 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
623 i, cfg->line_outs, cfg->line_out_type);
e5f14248
TI
624 if (err < 0)
625 return err;
626 }
e5f14248 627 for (i = 0; i < cfg->hp_outs; i++) {
21a4dc43
TI
628 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
629 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
e5f14248
TI
630 if (err < 0)
631 return err;
632 }
633 for (i = 0; i < cfg->speaker_outs; i++) {
21a4dc43
TI
634 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
635 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
e5f14248
TI
636 if (err < 0)
637 return err;
638 }
e5f14248
TI
639 return 0;
640}
641
642/*
643 */
644
645static struct snd_kcontrol_new cs_capture_ctls[] = {
646 HDA_BIND_SW("Capture Switch", 0),
647 HDA_BIND_VOL("Capture Volume", 0),
648};
649
ea35929b
TI
650static int change_cur_input(struct hda_codec *codec, unsigned int idx,
651 int force)
e5f14248
TI
652{
653 struct cs_spec *spec = codec->spec;
654 struct auto_pin_cfg *cfg = &spec->autocfg;
655
ea35929b 656 if (spec->cur_input == idx && !force)
e5f14248
TI
657 return 0;
658 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
659 /* stream is running, let's swap the current ADC */
660 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
661 spec->cur_adc = spec->adc_nid[idx];
662 snd_hda_codec_setup_stream(codec, spec->cur_adc,
663 spec->cur_adc_stream_tag, 0,
664 spec->cur_adc_format);
665 }
666 snd_hda_codec_write(codec, spec->cur_adc, 0,
667 AC_VERB_SET_CONNECT_SEL,
668 spec->adc_idx[idx]);
669 spec->cur_input = idx;
670 return 1;
671}
672
673static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
674 struct snd_ctl_elem_info *uinfo)
675{
676 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
677 struct cs_spec *spec = codec->spec;
678 unsigned int idx;
679
680 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
681 uinfo->count = 1;
682 uinfo->value.enumerated.items = spec->num_inputs;
683 if (uinfo->value.enumerated.item >= spec->num_inputs)
684 uinfo->value.enumerated.item = spec->num_inputs - 1;
685 idx = spec->input_idx[uinfo->value.enumerated.item];
686 strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
687 return 0;
688}
689
690static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
691 struct snd_ctl_elem_value *ucontrol)
692{
693 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
694 struct cs_spec *spec = codec->spec;
695 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
696 return 0;
697}
698
699static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
700 struct snd_ctl_elem_value *ucontrol)
701{
702 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
703 struct cs_spec *spec = codec->spec;
704 unsigned int idx = ucontrol->value.enumerated.item[0];
705
706 if (idx >= spec->num_inputs)
707 return -EINVAL;
708 idx = spec->input_idx[idx];
ea35929b 709 return change_cur_input(codec, idx, 0);
e5f14248
TI
710}
711
712static struct snd_kcontrol_new cs_capture_source = {
713 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
714 .name = "Capture Source",
715 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
716 .info = cs_capture_source_info,
717 .get = cs_capture_source_get,
718 .put = cs_capture_source_put,
719};
720
e5f14248
TI
721static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
722 struct hda_ctl_ops *ops)
723{
724 struct cs_spec *spec = codec->spec;
725 struct hda_bind_ctls *bind;
726 int i, n;
727
728 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
729 GFP_KERNEL);
730 if (!bind)
731 return NULL;
732 bind->ops = ops;
733 n = 0;
734 for (i = 0; i < AUTO_PIN_LAST; i++) {
735 if (!spec->adc_nid[i])
736 continue;
737 bind->values[n++] =
738 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
739 spec->adc_idx[i], HDA_INPUT);
740 }
741 return bind;
742}
743
744static int build_input(struct hda_codec *codec)
745{
746 struct cs_spec *spec = codec->spec;
21a4dc43 747 int i, err;
e5f14248 748
e5f14248
TI
749 if (!spec->num_inputs)
750 return 0;
751
e5f14248
TI
752 /* make bind-capture */
753 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
754 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
755 for (i = 0; i < 2; i++) {
756 struct snd_kcontrol *kctl;
757 if (!spec->capture_bind[i])
758 return -ENOMEM;
759 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
760 if (!kctl)
761 return -ENOMEM;
762 kctl->private_value = (long)spec->capture_bind[i];
763 err = snd_hda_ctl_add(codec, kctl);
764 if (err < 0)
765 return err;
766 }
767
768 if (spec->num_inputs > 1 && !spec->mic_detect) {
769 err = snd_hda_ctl_add(codec,
770 snd_ctl_new1(&cs_capture_source, codec));
771 if (err < 0)
772 return err;
773 }
774
775 return 0;
776}
777
21a4dc43
TI
778/*
779 */
780
e5f14248
TI
781static int build_digital_output(struct hda_codec *codec)
782{
783 struct cs_spec *spec = codec->spec;
e5f14248
TI
784 int err;
785
e5f14248
TI
786 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
787 if (err < 0)
788 return err;
789 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
790 if (err < 0)
791 return err;
e5f14248
TI
792 return 0;
793}
794
795static int build_digital_input(struct hda_codec *codec)
796{
797 struct cs_spec *spec = codec->spec;
21a4dc43
TI
798 if (spec->dig_in)
799 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
800 return 0;
e5f14248
TI
801}
802
21a4dc43
TI
803/*
804 * auto-mute and auto-mic switching
805 */
806
e5f14248
TI
807static void cs_automute(struct hda_codec *codec)
808{
809 struct cs_spec *spec = codec->spec;
810 struct auto_pin_cfg *cfg = &spec->autocfg;
811 unsigned int caps, present, hp_present;
812 hda_nid_t nid;
813 int i;
814
815 hp_present = 0;
816 for (i = 0; i < cfg->hp_outs; i++) {
817 nid = cfg->hp_pins[i];
818 caps = snd_hda_query_pin_caps(codec, nid);
819 if (!(caps & AC_PINCAP_PRES_DETECT))
820 continue;
821 if (caps & AC_PINCAP_TRIG_REQ)
822 snd_hda_codec_read(codec, nid, 0,
823 AC_VERB_SET_PIN_SENSE, 0);
824 present = snd_hda_codec_read(codec, nid, 0,
825 AC_VERB_GET_PIN_SENSE, 0);
826 hp_present |= (present & AC_PINSENSE_PRESENCE) != 0;
827 if (hp_present)
828 break;
829 }
830 for (i = 0; i < cfg->speaker_outs; i++) {
831 nid = cfg->speaker_pins[i];
832 snd_hda_codec_write(codec, nid, 0,
833 AC_VERB_SET_PIN_WIDGET_CONTROL,
834 hp_present ? 0 : PIN_OUT);
835 }
836}
837
838static void cs_automic(struct hda_codec *codec)
839{
840 struct cs_spec *spec = codec->spec;
841 struct auto_pin_cfg *cfg = &spec->autocfg;
842 hda_nid_t nid;
843 unsigned int caps, present;
844
845 nid = cfg->input_pins[spec->automic_idx];
846 caps = snd_hda_query_pin_caps(codec, nid);
847 if (caps & AC_PINCAP_TRIG_REQ)
848 snd_hda_codec_read(codec, nid, 0, AC_VERB_SET_PIN_SENSE, 0);
849 present = snd_hda_codec_read(codec, nid, 0,
850 AC_VERB_GET_PIN_SENSE, 0);
851 if (present & AC_PINSENSE_PRESENCE)
ea35929b 852 change_cur_input(codec, spec->automic_idx, 0);
e5f14248
TI
853 else {
854 unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
855 AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
ea35929b 856 change_cur_input(codec, imic, 0);
e5f14248
TI
857 }
858}
859
860/*
861 */
862
863static void init_output(struct hda_codec *codec)
864{
865 struct cs_spec *spec = codec->spec;
866 struct auto_pin_cfg *cfg = &spec->autocfg;
867 int i;
868
869 /* mute first */
870 for (i = 0; i < spec->multiout.num_dacs; i++)
871 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
872 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
873 if (spec->multiout.hp_nid)
874 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
875 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
876 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
877 if (!spec->multiout.extra_out_nid[i])
878 break;
879 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
880 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
881 }
882
883 /* set appropriate pin controls */
884 for (i = 0; i < cfg->line_outs; i++)
885 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
886 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
887 for (i = 0; i < cfg->hp_outs; i++) {
888 hda_nid_t nid = cfg->hp_pins[i];
889 snd_hda_codec_write(codec, nid, 0,
890 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
891 if (!cfg->speaker_outs)
892 continue;
893 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
894 snd_hda_codec_write(codec, nid, 0,
895 AC_VERB_SET_UNSOLICITED_ENABLE,
896 AC_USRSP_EN | HP_EVENT);
897 spec->hp_detect = 1;
898 }
899 }
900 for (i = 0; i < cfg->speaker_outs; i++)
901 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
902 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
903 if (spec->hp_detect)
904 cs_automute(codec);
905}
906
907static void init_input(struct hda_codec *codec)
908{
909 struct cs_spec *spec = codec->spec;
910 struct auto_pin_cfg *cfg = &spec->autocfg;
40c20fa0 911 unsigned int coef;
e5f14248
TI
912 int i;
913
914 for (i = 0; i < AUTO_PIN_LAST; i++) {
915 unsigned int ctl;
916 hda_nid_t pin = cfg->input_pins[i];
917 if (!pin || !spec->adc_nid[i])
918 continue;
919 /* set appropriate pin control and mute first */
920 ctl = PIN_IN;
921 if (i <= AUTO_PIN_FRONT_MIC) {
922 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
923 caps >>= AC_PINCAP_VREF_SHIFT;
924 if (caps & AC_PINCAP_VREF_80)
925 ctl = PIN_VREF80;
926 }
927 snd_hda_codec_write(codec, pin, 0,
928 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
929 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
930 AC_VERB_SET_AMP_GAIN_MUTE,
931 AMP_IN_MUTE(spec->adc_idx[i]));
932 if (spec->mic_detect && spec->automic_idx == i)
933 snd_hda_codec_write(codec, pin, 0,
934 AC_VERB_SET_UNSOLICITED_ENABLE,
935 AC_USRSP_EN | MIC_EVENT);
936 }
ea35929b 937 change_cur_input(codec, spec->cur_input, 1);
e5f14248
TI
938 if (spec->mic_detect)
939 cs_automic(codec);
40c20fa0
TI
940
941 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
942 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
943 coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
944 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
945 coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0
946 * No effect if SPDIF_OUT2 is slected in
947 * IDX_SPDIF_CTL.
948 */
949 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
950}
951
952static struct hda_verb cs_coef_init_verbs[] = {
953 {0x11, AC_VERB_SET_PROC_STATE, 1},
954 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
955 {0x11, AC_VERB_SET_PROC_COEF,
956 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
957 | 0x0040 /* Mute DACs on FIFO error */
958 | 0x1000 /* Enable DACs High Pass Filter */
959 | 0x0400 /* Disable Coefficient Auto increment */
960 )},
961 /* Beep */
962 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
963 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
964
965 {} /* terminator */
966};
967
968/* SPDIF setup */
969static void init_digital(struct hda_codec *codec)
970{
971 unsigned int coef;
972
973 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
974 coef |= 0x0008; /* Replace with mute on error */
975 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
976 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
977 * SPDIF_OUT2 is shared with GPIO1 and
978 * DMIC_SDA2.
979 */
980 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
e5f14248
TI
981}
982
983static int cs_init(struct hda_codec *codec)
984{
985 struct cs_spec *spec = codec->spec;
986
40c20fa0 987 snd_hda_sequence_write(codec, cs_coef_init_verbs);
ed208255
TI
988
989 if (spec->gpio_mask) {
990 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
991 spec->gpio_mask);
992 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
993 spec->gpio_dir);
994 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
995 spec->gpio_data);
996 }
997
e5f14248
TI
998 init_output(codec);
999 init_input(codec);
40c20fa0 1000 init_digital(codec);
e5f14248
TI
1001 return 0;
1002}
1003
1004static int cs_build_controls(struct hda_codec *codec)
1005{
1006 struct cs_spec *spec = codec->spec;
1007 int err;
1008
1009 err = build_output(codec);
1010 if (err < 0)
1011 return err;
1012 err = build_input(codec);
1013 if (err < 0)
1014 return err;
1015 err = build_digital_output(codec);
1016 if (err < 0)
1017 return err;
1018 err = build_digital_input(codec);
1019 if (err < 0)
1020 return err;
e5f14248
TI
1021 return cs_init(codec);
1022}
1023
1024static void cs_free(struct hda_codec *codec)
1025{
1026 struct cs_spec *spec = codec->spec;
1027 kfree(spec->capture_bind[0]);
1028 kfree(spec->capture_bind[1]);
1029 kfree(codec->spec);
1030}
1031
1032static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1033{
1034 switch ((res >> 26) & 0x7f) {
1035 case HP_EVENT:
1036 cs_automute(codec);
1037 break;
1038 case MIC_EVENT:
1039 cs_automic(codec);
1040 break;
1041 }
1042}
1043
1044static struct hda_codec_ops cs_patch_ops = {
1045 .build_controls = cs_build_controls,
1046 .build_pcms = cs_build_pcms,
1047 .init = cs_init,
1048 .free = cs_free,
1049 .unsol_event = cs_unsol_event,
1050};
1051
1052static int cs_parse_auto_config(struct hda_codec *codec)
1053{
1054 struct cs_spec *spec = codec->spec;
1055 int err;
1056
1057 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
ed208255
TI
1058 if (err < 0)
1059 return err;
1060
1061 err = parse_output(codec);
1062 if (err < 0)
1063 return err;
1064 err = parse_input(codec);
1065 if (err < 0)
1066 return err;
1067 err = parse_digital_output(codec);
1068 if (err < 0)
1069 return err;
1070 err = parse_digital_input(codec);
e5f14248
TI
1071 if (err < 0)
1072 return err;
1073 return 0;
1074}
1075
a6bae205
TI
1076static const char *cs420x_models[CS420X_MODELS] = {
1077 [CS420X_MBP55] = "mbp55",
1078 [CS420X_AUTO] = "auto",
1079};
1080
1081
1082static struct snd_pci_quirk cs420x_cfg_tbl[] = {
1083 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1084 {} /* terminator */
1085};
1086
1087struct cs_pincfg {
1088 hda_nid_t nid;
1089 u32 val;
1090};
1091
1092static struct cs_pincfg mbp55_pincfgs[] = {
1093 { 0x09, 0x012b4030 },
1094 { 0x0a, 0x90100121 },
1095 { 0x0b, 0x90100120 },
1096 { 0x0c, 0x400000f0 },
1097 { 0x0d, 0x90a00110 },
1098 { 0x0e, 0x400000f0 },
1099 { 0x0f, 0x400000f0 },
1100 { 0x10, 0x014be040 },
1101 { 0x12, 0x400000f0 },
1102 { 0x15, 0x400000f0 },
1103 {} /* terminator */
1104};
1105
1106static struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1107 [CS420X_MBP55] = mbp55_pincfgs,
1108};
1109
1110static void fix_pincfg(struct hda_codec *codec, int model)
1111{
1112 const struct cs_pincfg *cfg = cs_pincfgs[model];
1113 if (!cfg)
1114 return;
1115 for (; cfg->nid; cfg++)
1116 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1117}
1118
e5f14248
TI
1119
1120static int patch_cs420x(struct hda_codec *codec)
1121{
1122 struct cs_spec *spec;
1123 int err;
1124
1125 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1126 if (!spec)
1127 return -ENOMEM;
1128 codec->spec = spec;
1129
a6bae205
TI
1130 spec->board_config =
1131 snd_hda_check_board_config(codec, CS420X_MODELS,
1132 cs420x_models, cs420x_cfg_tbl);
1133 if (spec->board_config >= 0)
1134 fix_pincfg(codec, spec->board_config);
1135
ed208255
TI
1136 switch (spec->board_config) {
1137 case CS420X_MBP55:
1138 /* GPIO3 = EAPD? */
1139 spec->gpio_mask = 0x08;
1140 spec->gpio_dir = 0x08;
1141 spec->gpio_data = 0x08;
1142 break;
1143 }
e5f14248 1144
ed208255 1145 err = cs_parse_auto_config(codec);
21a4dc43
TI
1146 if (err < 0)
1147 goto error;
1148
e5f14248
TI
1149 codec->patch_ops = cs_patch_ops;
1150
1151 return 0;
1152
1153 error:
1154 kfree(codec->spec);
1155 codec->spec = NULL;
1156 return err;
1157}
1158
1159
1160/*
1161 * patch entries
1162 */
1163static struct hda_codec_preset snd_hda_preset_cirrus[] = {
1164 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1165 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1166 {} /* terminator */
1167};
1168
1169MODULE_ALIAS("snd-hda-codec-id:10134206");
1170MODULE_ALIAS("snd-hda-codec-id:10134207");
1171
1172MODULE_LICENSE("GPL");
1173MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1174
1175static struct hda_codec_preset_list cirrus_list = {
1176 .preset = snd_hda_preset_cirrus,
1177 .owner = THIS_MODULE,
1178};
1179
1180static int __init patch_cirrus_init(void)
1181{
1182 return snd_hda_add_codec_preset(&cirrus_list);
1183}
1184
1185static void __exit patch_cirrus_exit(void)
1186{
1187 snd_hda_delete_codec_preset(&cirrus_list);
1188}
1189
1190module_init(patch_cirrus_init)
1191module_exit(patch_cirrus_exit)