]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/pci/hda/patch_cirrus.c
ALSA: hda - add more NID->Control mapping
[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
277a57c7 113static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
40c20fa0
TI
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
277a57c7
TI
121static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
122 unsigned int coef)
40c20fa0
TI
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{
40c20fa0
TI
370 unsigned int val;
371 val = snd_hda_codec_get_pincfg(codec, nid);
372 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
373}
374
21a4dc43
TI
375static int parse_output(struct hda_codec *codec)
376{
377 struct cs_spec *spec = codec->spec;
378 struct auto_pin_cfg *cfg = &spec->autocfg;
277a57c7 379 int i, extra_nids;
21a4dc43
TI
380 hda_nid_t dac;
381
382 for (i = 0; i < cfg->line_outs; i++) {
383 dac = get_dac(codec, cfg->line_out_pins[i]);
384 if (!dac)
385 break;
386 spec->dac_nid[i] = dac;
387 }
388 spec->multiout.num_dacs = i;
389 spec->multiout.dac_nids = spec->dac_nid;
390 spec->multiout.max_channels = i * 2;
391
392 /* add HP and speakers */
393 extra_nids = 0;
394 for (i = 0; i < cfg->hp_outs; i++) {
395 dac = get_dac(codec, cfg->hp_pins[i]);
396 if (!dac)
397 break;
398 if (!i)
399 spec->multiout.hp_nid = dac;
400 else
401 spec->multiout.extra_out_nid[extra_nids++] = dac;
402 }
403 for (i = 0; i < cfg->speaker_outs; i++) {
404 dac = get_dac(codec, cfg->speaker_pins[i]);
405 if (!dac)
406 break;
407 spec->multiout.extra_out_nid[extra_nids++] = dac;
408 }
409
410 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
411 cfg->speaker_outs = cfg->line_outs;
412 memcpy(cfg->speaker_pins, cfg->line_out_pins,
413 sizeof(cfg->speaker_pins));
414 cfg->line_outs = 0;
415 }
416
417 return 0;
418}
419
420static int parse_input(struct hda_codec *codec)
421{
422 struct cs_spec *spec = codec->spec;
423 struct auto_pin_cfg *cfg = &spec->autocfg;
277a57c7 424 int i;
21a4dc43
TI
425
426 for (i = 0; i < AUTO_PIN_LAST; i++) {
427 hda_nid_t pin = cfg->input_pins[i];
21a4dc43
TI
428 if (!pin)
429 continue;
430 spec->input_idx[spec->num_inputs] = i;
431 spec->capsrc_idx[i] = spec->num_inputs++;
432 spec->cur_input = i;
433 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
434 }
435 if (!spec->num_inputs)
436 return 0;
437
438 /* check whether the automatic mic switch is available */
439 if (spec->num_inputs == 2 &&
440 spec->adc_nid[AUTO_PIN_MIC] && spec->adc_nid[AUTO_PIN_FRONT_MIC]) {
441 if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_FRONT_MIC])) {
442 if (!is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
443 spec->mic_detect = 1;
444 spec->automic_idx = AUTO_PIN_FRONT_MIC;
445 }
446 } else {
447 if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
448 spec->mic_detect = 1;
449 spec->automic_idx = AUTO_PIN_MIC;
450 }
451 }
452 }
453 return 0;
454}
455
456
457static int parse_digital_output(struct hda_codec *codec)
458{
459 struct cs_spec *spec = codec->spec;
460 struct auto_pin_cfg *cfg = &spec->autocfg;
461 hda_nid_t nid;
21a4dc43
TI
462
463 if (!cfg->dig_outs)
464 return 0;
465 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
466 return 0;
467 spec->multiout.dig_out_nid = nid;
468 spec->multiout.share_spdif = 1;
469 if (cfg->dig_outs > 1 &&
470 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
471 spec->slave_dig_outs[0] = nid;
472 codec->slave_dig_outs = spec->slave_dig_outs;
473 }
474 return 0;
475}
476
477static int parse_digital_input(struct hda_codec *codec)
478{
479 struct cs_spec *spec = codec->spec;
480 struct auto_pin_cfg *cfg = &spec->autocfg;
481 int idx;
482
60e53882
TI
483 if (cfg->dig_in_pin)
484 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
485 return 0;
21a4dc43
TI
486}
487
488/*
489 * create mixer controls
490 */
491
e5f14248
TI
492static const char *dir_sfx[2] = { "Playback", "Capture" };
493
494static int add_mute(struct hda_codec *codec, const char *name, int index,
495 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
496{
b4dabfc4 497 char tmp[44];
e5f14248
TI
498 struct snd_kcontrol_new knew =
499 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
500 knew.private_value = pval;
501 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
502 *kctlp = snd_ctl_new1(&knew, codec);
3911a4c1 503 return snd_hda_ctl_add(codec, get_amp_nid_(pval), *kctlp);
e5f14248
TI
504}
505
506static int add_volume(struct hda_codec *codec, const char *name,
507 int index, unsigned int pval, int dir,
508 struct snd_kcontrol **kctlp)
509{
510 char tmp[32];
511 struct snd_kcontrol_new knew =
512 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
513 knew.private_value = pval;
514 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
515 *kctlp = snd_ctl_new1(&knew, codec);
3911a4c1 516 return snd_hda_ctl_add(codec, get_amp_nid_(pval), *kctlp);
e5f14248
TI
517}
518
519static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
520{
521 unsigned int caps;
522
523 /* set the upper-limit for mixer amp to 0dB */
524 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
525 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
526 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
527 << AC_AMPCAP_NUM_STEPS_SHIFT;
528 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
529}
530
531static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
532{
533 struct cs_spec *spec = codec->spec;
534 unsigned int tlv[4];
535 int err;
536
537 spec->vmaster_sw =
538 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
3911a4c1 539 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
e5f14248
TI
540 if (err < 0)
541 return err;
542
543 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
544 spec->vmaster_vol =
545 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
3911a4c1 546 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
e5f14248
TI
547 if (err < 0)
548 return err;
549 return 0;
550}
551
552static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
553 int num_ctls, int type)
554{
555 struct cs_spec *spec = codec->spec;
556 const char *name;
557 int err, index;
558 struct snd_kcontrol *kctl;
559 static char *speakers[] = {
560 "Front Speaker", "Surround Speaker", "Bass Speaker"
561 };
562 static char *line_outs[] = {
563 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
564 };
565
566 fix_volume_caps(codec, dac);
567 if (!spec->vmaster_sw) {
568 err = add_vmaster(codec, dac);
569 if (err < 0)
570 return err;
571 }
572
573 index = 0;
574 switch (type) {
575 case AUTO_PIN_HP_OUT:
576 name = "Headphone";
577 index = idx;
578 break;
579 case AUTO_PIN_SPEAKER_OUT:
580 if (num_ctls > 1)
581 name = speakers[idx];
582 else
583 name = "Speaker";
584 break;
585 default:
586 if (num_ctls > 1)
587 name = line_outs[idx];
588 else
589 name = "Line-Out";
590 break;
591 }
592
593 err = add_mute(codec, name, index,
594 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
595 if (err < 0)
596 return err;
597 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
598 if (err < 0)
599 return err;
600
601 err = add_volume(codec, name, index,
602 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
603 if (err < 0)
604 return err;
605 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
606 if (err < 0)
607 return err;
608
609 return 0;
610}
611
612static int build_output(struct hda_codec *codec)
613{
614 struct cs_spec *spec = codec->spec;
615 struct auto_pin_cfg *cfg = &spec->autocfg;
21a4dc43 616 int i, err;
e5f14248
TI
617
618 for (i = 0; i < cfg->line_outs; i++) {
21a4dc43
TI
619 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
620 i, cfg->line_outs, cfg->line_out_type);
e5f14248
TI
621 if (err < 0)
622 return err;
623 }
e5f14248 624 for (i = 0; i < cfg->hp_outs; i++) {
21a4dc43
TI
625 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
626 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
e5f14248
TI
627 if (err < 0)
628 return err;
629 }
630 for (i = 0; i < cfg->speaker_outs; i++) {
21a4dc43
TI
631 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
632 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
e5f14248
TI
633 if (err < 0)
634 return err;
635 }
e5f14248
TI
636 return 0;
637}
638
639/*
640 */
641
642static struct snd_kcontrol_new cs_capture_ctls[] = {
643 HDA_BIND_SW("Capture Switch", 0),
644 HDA_BIND_VOL("Capture Volume", 0),
645};
646
ea35929b
TI
647static int change_cur_input(struct hda_codec *codec, unsigned int idx,
648 int force)
e5f14248
TI
649{
650 struct cs_spec *spec = codec->spec;
e5f14248 651
ea35929b 652 if (spec->cur_input == idx && !force)
e5f14248
TI
653 return 0;
654 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
655 /* stream is running, let's swap the current ADC */
656 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
657 spec->cur_adc = spec->adc_nid[idx];
658 snd_hda_codec_setup_stream(codec, spec->cur_adc,
659 spec->cur_adc_stream_tag, 0,
660 spec->cur_adc_format);
661 }
662 snd_hda_codec_write(codec, spec->cur_adc, 0,
663 AC_VERB_SET_CONNECT_SEL,
664 spec->adc_idx[idx]);
665 spec->cur_input = idx;
666 return 1;
667}
668
669static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
670 struct snd_ctl_elem_info *uinfo)
671{
672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
673 struct cs_spec *spec = codec->spec;
674 unsigned int idx;
675
676 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
677 uinfo->count = 1;
678 uinfo->value.enumerated.items = spec->num_inputs;
679 if (uinfo->value.enumerated.item >= spec->num_inputs)
680 uinfo->value.enumerated.item = spec->num_inputs - 1;
681 idx = spec->input_idx[uinfo->value.enumerated.item];
682 strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
683 return 0;
684}
685
686static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
687 struct snd_ctl_elem_value *ucontrol)
688{
689 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
690 struct cs_spec *spec = codec->spec;
691 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
692 return 0;
693}
694
695static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
696 struct snd_ctl_elem_value *ucontrol)
697{
698 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
699 struct cs_spec *spec = codec->spec;
700 unsigned int idx = ucontrol->value.enumerated.item[0];
701
702 if (idx >= spec->num_inputs)
703 return -EINVAL;
704 idx = spec->input_idx[idx];
ea35929b 705 return change_cur_input(codec, idx, 0);
e5f14248
TI
706}
707
708static struct snd_kcontrol_new cs_capture_source = {
709 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
710 .name = "Capture Source",
711 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
712 .info = cs_capture_source_info,
713 .get = cs_capture_source_get,
714 .put = cs_capture_source_put,
715};
716
e5f14248
TI
717static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
718 struct hda_ctl_ops *ops)
719{
720 struct cs_spec *spec = codec->spec;
721 struct hda_bind_ctls *bind;
722 int i, n;
723
724 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
725 GFP_KERNEL);
726 if (!bind)
727 return NULL;
728 bind->ops = ops;
729 n = 0;
730 for (i = 0; i < AUTO_PIN_LAST; i++) {
731 if (!spec->adc_nid[i])
732 continue;
733 bind->values[n++] =
734 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
735 spec->adc_idx[i], HDA_INPUT);
736 }
737 return bind;
738}
739
740static int build_input(struct hda_codec *codec)
741{
742 struct cs_spec *spec = codec->spec;
21a4dc43 743 int i, err;
e5f14248 744
e5f14248
TI
745 if (!spec->num_inputs)
746 return 0;
747
e5f14248
TI
748 /* make bind-capture */
749 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
750 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
751 for (i = 0; i < 2; i++) {
752 struct snd_kcontrol *kctl;
753 if (!spec->capture_bind[i])
754 return -ENOMEM;
755 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
756 if (!kctl)
757 return -ENOMEM;
758 kctl->private_value = (long)spec->capture_bind[i];
3911a4c1 759 err = snd_hda_ctl_add(codec, 0, kctl);
e5f14248
TI
760 if (err < 0)
761 return err;
5b0cb1d8
JK
762 err = snd_hda_add_nids(codec, kctl, 0, spec->adc_nid,
763 spec->num_inputs);
764 if (err < 0)
765 return err;
e5f14248
TI
766 }
767
768 if (spec->num_inputs > 1 && !spec->mic_detect) {
3911a4c1 769 err = snd_hda_ctl_add(codec, 0,
e5f14248
TI
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
63b2413b
TI
786 if (!spec->multiout.dig_out_nid)
787 return 0;
788
e5f14248
TI
789 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
790 if (err < 0)
791 return err;
792 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
793 if (err < 0)
794 return err;
e5f14248
TI
795 return 0;
796}
797
798static int build_digital_input(struct hda_codec *codec)
799{
800 struct cs_spec *spec = codec->spec;
21a4dc43
TI
801 if (spec->dig_in)
802 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
803 return 0;
e5f14248
TI
804}
805
21a4dc43
TI
806/*
807 * auto-mute and auto-mic switching
808 */
809
e5f14248
TI
810static void cs_automute(struct hda_codec *codec)
811{
812 struct cs_spec *spec = codec->spec;
813 struct auto_pin_cfg *cfg = &spec->autocfg;
864f92be 814 unsigned int caps, hp_present;
e5f14248
TI
815 hda_nid_t nid;
816 int i;
817
818 hp_present = 0;
819 for (i = 0; i < cfg->hp_outs; i++) {
820 nid = cfg->hp_pins[i];
821 caps = snd_hda_query_pin_caps(codec, nid);
822 if (!(caps & AC_PINCAP_PRES_DETECT))
823 continue;
864f92be 824 hp_present = snd_hda_jack_detect(codec, nid);
e5f14248
TI
825 if (hp_present)
826 break;
827 }
828 for (i = 0; i < cfg->speaker_outs; i++) {
829 nid = cfg->speaker_pins[i];
830 snd_hda_codec_write(codec, nid, 0,
831 AC_VERB_SET_PIN_WIDGET_CONTROL,
832 hp_present ? 0 : PIN_OUT);
833 }
3a385167
SP
834 if (spec->board_config == CS420X_MBP55) {
835 unsigned int gpio = hp_present ? 0x02 : 0x08;
836 snd_hda_codec_write(codec, 0x01, 0,
837 AC_VERB_SET_GPIO_DATA, gpio);
838 }
e5f14248
TI
839}
840
841static void cs_automic(struct hda_codec *codec)
842{
843 struct cs_spec *spec = codec->spec;
844 struct auto_pin_cfg *cfg = &spec->autocfg;
845 hda_nid_t nid;
864f92be 846 unsigned int present;
e5f14248
TI
847
848 nid = cfg->input_pins[spec->automic_idx];
864f92be
WF
849 present = snd_hda_jack_detect(codec, nid);
850 if (present)
ea35929b 851 change_cur_input(codec, spec->automic_idx, 0);
e5f14248
TI
852 else {
853 unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
854 AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
ea35929b 855 change_cur_input(codec, imic, 0);
e5f14248
TI
856 }
857}
858
859/*
860 */
861
862static void init_output(struct hda_codec *codec)
863{
864 struct cs_spec *spec = codec->spec;
865 struct auto_pin_cfg *cfg = &spec->autocfg;
866 int i;
867
868 /* mute first */
869 for (i = 0; i < spec->multiout.num_dacs; i++)
870 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
871 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
872 if (spec->multiout.hp_nid)
873 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
874 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
875 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
876 if (!spec->multiout.extra_out_nid[i])
877 break;
878 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
879 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
880 }
881
882 /* set appropriate pin controls */
883 for (i = 0; i < cfg->line_outs; i++)
884 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
885 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
886 for (i = 0; i < cfg->hp_outs; i++) {
887 hda_nid_t nid = cfg->hp_pins[i];
888 snd_hda_codec_write(codec, nid, 0,
889 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
890 if (!cfg->speaker_outs)
891 continue;
892 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
893 snd_hda_codec_write(codec, nid, 0,
894 AC_VERB_SET_UNSOLICITED_ENABLE,
895 AC_USRSP_EN | HP_EVENT);
896 spec->hp_detect = 1;
897 }
898 }
899 for (i = 0; i < cfg->speaker_outs; i++)
900 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
901 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
902 if (spec->hp_detect)
903 cs_automute(codec);
904}
905
906static void init_input(struct hda_codec *codec)
907{
908 struct cs_spec *spec = codec->spec;
909 struct auto_pin_cfg *cfg = &spec->autocfg;
40c20fa0 910 unsigned int coef;
e5f14248
TI
911 int i;
912
913 for (i = 0; i < AUTO_PIN_LAST; i++) {
914 unsigned int ctl;
915 hda_nid_t pin = cfg->input_pins[i];
916 if (!pin || !spec->adc_nid[i])
917 continue;
918 /* set appropriate pin control and mute first */
919 ctl = PIN_IN;
920 if (i <= AUTO_PIN_FRONT_MIC) {
921 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
922 caps >>= AC_PINCAP_VREF_SHIFT;
923 if (caps & AC_PINCAP_VREF_80)
924 ctl = PIN_VREF80;
925 }
926 snd_hda_codec_write(codec, pin, 0,
927 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
928 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
929 AC_VERB_SET_AMP_GAIN_MUTE,
930 AMP_IN_MUTE(spec->adc_idx[i]));
931 if (spec->mic_detect && spec->automic_idx == i)
932 snd_hda_codec_write(codec, pin, 0,
933 AC_VERB_SET_UNSOLICITED_ENABLE,
934 AC_USRSP_EN | MIC_EVENT);
935 }
ea35929b 936 change_cur_input(codec, spec->cur_input, 1);
e5f14248
TI
937 if (spec->mic_detect)
938 cs_automic(codec);
40c20fa0
TI
939
940 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
941 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
942 coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
943 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
944 coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0
b71a8eb0 945 * No effect if SPDIF_OUT2 is selected in
40c20fa0
TI
946 * IDX_SPDIF_CTL.
947 */
948 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
949}
950
951static struct hda_verb cs_coef_init_verbs[] = {
952 {0x11, AC_VERB_SET_PROC_STATE, 1},
953 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
954 {0x11, AC_VERB_SET_PROC_COEF,
955 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
956 | 0x0040 /* Mute DACs on FIFO error */
957 | 0x1000 /* Enable DACs High Pass Filter */
958 | 0x0400 /* Disable Coefficient Auto increment */
959 )},
960 /* Beep */
961 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
962 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
963
964 {} /* terminator */
965};
966
967/* SPDIF setup */
968static void init_digital(struct hda_codec *codec)
969{
970 unsigned int coef;
971
972 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
973 coef |= 0x0008; /* Replace with mute on error */
974 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
975 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
976 * SPDIF_OUT2 is shared with GPIO1 and
977 * DMIC_SDA2.
978 */
979 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
e5f14248
TI
980}
981
982static int cs_init(struct hda_codec *codec)
983{
984 struct cs_spec *spec = codec->spec;
985
40c20fa0 986 snd_hda_sequence_write(codec, cs_coef_init_verbs);
ed208255
TI
987
988 if (spec->gpio_mask) {
989 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
990 spec->gpio_mask);
991 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
992 spec->gpio_dir);
993 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
994 spec->gpio_data);
995 }
996
e5f14248
TI
997 init_output(codec);
998 init_input(codec);
40c20fa0 999 init_digital(codec);
e5f14248
TI
1000 return 0;
1001}
1002
1003static int cs_build_controls(struct hda_codec *codec)
1004{
e5f14248
TI
1005 int err;
1006
1007 err = build_output(codec);
1008 if (err < 0)
1009 return err;
1010 err = build_input(codec);
1011 if (err < 0)
1012 return err;
1013 err = build_digital_output(codec);
1014 if (err < 0)
1015 return err;
1016 err = build_digital_input(codec);
1017 if (err < 0)
1018 return err;
e5f14248
TI
1019 return cs_init(codec);
1020}
1021
1022static void cs_free(struct hda_codec *codec)
1023{
1024 struct cs_spec *spec = codec->spec;
1025 kfree(spec->capture_bind[0]);
1026 kfree(spec->capture_bind[1]);
1027 kfree(codec->spec);
1028}
1029
1030static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1031{
1032 switch ((res >> 26) & 0x7f) {
1033 case HP_EVENT:
1034 cs_automute(codec);
1035 break;
1036 case MIC_EVENT:
1037 cs_automic(codec);
1038 break;
1039 }
1040}
1041
1042static struct hda_codec_ops cs_patch_ops = {
1043 .build_controls = cs_build_controls,
1044 .build_pcms = cs_build_pcms,
1045 .init = cs_init,
1046 .free = cs_free,
1047 .unsol_event = cs_unsol_event,
1048};
1049
1050static int cs_parse_auto_config(struct hda_codec *codec)
1051{
1052 struct cs_spec *spec = codec->spec;
1053 int err;
1054
1055 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
ed208255
TI
1056 if (err < 0)
1057 return err;
1058
1059 err = parse_output(codec);
1060 if (err < 0)
1061 return err;
1062 err = parse_input(codec);
1063 if (err < 0)
1064 return err;
1065 err = parse_digital_output(codec);
1066 if (err < 0)
1067 return err;
1068 err = parse_digital_input(codec);
e5f14248
TI
1069 if (err < 0)
1070 return err;
1071 return 0;
1072}
1073
a6bae205
TI
1074static const char *cs420x_models[CS420X_MODELS] = {
1075 [CS420X_MBP55] = "mbp55",
1076 [CS420X_AUTO] = "auto",
1077};
1078
1079
1080static struct snd_pci_quirk cs420x_cfg_tbl[] = {
1081 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1082 {} /* terminator */
1083};
1084
1085struct cs_pincfg {
1086 hda_nid_t nid;
1087 u32 val;
1088};
1089
1090static struct cs_pincfg mbp55_pincfgs[] = {
1091 { 0x09, 0x012b4030 },
1092 { 0x0a, 0x90100121 },
1093 { 0x0b, 0x90100120 },
1094 { 0x0c, 0x400000f0 },
1095 { 0x0d, 0x90a00110 },
1096 { 0x0e, 0x400000f0 },
1097 { 0x0f, 0x400000f0 },
1098 { 0x10, 0x014be040 },
1099 { 0x12, 0x400000f0 },
1100 { 0x15, 0x400000f0 },
1101 {} /* terminator */
1102};
1103
1104static struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1105 [CS420X_MBP55] = mbp55_pincfgs,
1106};
1107
1108static void fix_pincfg(struct hda_codec *codec, int model)
1109{
1110 const struct cs_pincfg *cfg = cs_pincfgs[model];
1111 if (!cfg)
1112 return;
1113 for (; cfg->nid; cfg++)
1114 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1115}
1116
e5f14248
TI
1117
1118static int patch_cs420x(struct hda_codec *codec)
1119{
1120 struct cs_spec *spec;
1121 int err;
1122
1123 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1124 if (!spec)
1125 return -ENOMEM;
1126 codec->spec = spec;
1127
a6bae205
TI
1128 spec->board_config =
1129 snd_hda_check_board_config(codec, CS420X_MODELS,
1130 cs420x_models, cs420x_cfg_tbl);
1131 if (spec->board_config >= 0)
1132 fix_pincfg(codec, spec->board_config);
1133
ed208255
TI
1134 switch (spec->board_config) {
1135 case CS420X_MBP55:
3a385167
SP
1136 /* GPIO1 = headphones */
1137 /* GPIO3 = speakers */
1138 spec->gpio_mask = 0x0a;
1139 spec->gpio_dir = 0x0a;
ed208255
TI
1140 break;
1141 }
e5f14248 1142
ed208255 1143 err = cs_parse_auto_config(codec);
21a4dc43
TI
1144 if (err < 0)
1145 goto error;
1146
e5f14248
TI
1147 codec->patch_ops = cs_patch_ops;
1148
1149 return 0;
1150
1151 error:
1152 kfree(codec->spec);
1153 codec->spec = NULL;
1154 return err;
1155}
1156
1157
1158/*
1159 * patch entries
1160 */
1161static struct hda_codec_preset snd_hda_preset_cirrus[] = {
1162 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1163 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1164 {} /* terminator */
1165};
1166
1167MODULE_ALIAS("snd-hda-codec-id:10134206");
1168MODULE_ALIAS("snd-hda-codec-id:10134207");
1169
1170MODULE_LICENSE("GPL");
1171MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1172
1173static struct hda_codec_preset_list cirrus_list = {
1174 .preset = snd_hda_preset_cirrus,
1175 .owner = THIS_MODULE,
1176};
1177
1178static int __init patch_cirrus_init(void)
1179{
1180 return snd_hda_add_codec_preset(&cirrus_list);
1181}
1182
1183static void __exit patch_cirrus_exit(void)
1184{
1185 snd_hda_delete_codec_preset(&cirrus_list);
1186}
1187
1188module_init(patch_cirrus_init)
1189module_exit(patch_cirrus_exit)