]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/pci/hda/patch_cirrus.c
ALSA: hda: Use STAC_DELL_M6_BOTH quirk for Dell Studio 1558
[mirror_ubuntu-bionic-kernel.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,
1a5ba2e9 69 CS420X_IMAC27,
a6bae205
TI
70 CS420X_AUTO,
71 CS420X_MODELS
72};
73
40c20fa0
TI
74/* Vendor-specific processing widget */
75#define CS420X_VENDOR_NID 0x11
76#define CS_DIG_OUT1_PIN_NID 0x10
77#define CS_DIG_OUT2_PIN_NID 0x15
78#define CS_DMIC1_PIN_NID 0x12
79#define CS_DMIC2_PIN_NID 0x0e
80
81/* coef indices */
82#define IDX_SPDIF_STAT 0x0000
83#define IDX_SPDIF_CTL 0x0001
84#define IDX_ADC_CFG 0x0002
85/* SZC bitmask, 4 modes below:
86 * 0 = immediate,
87 * 1 = digital immediate, analog zero-cross
88 * 2 = digtail & analog soft-ramp
89 * 3 = digital soft-ramp, analog zero-cross
90 */
91#define CS_COEF_ADC_SZC_MASK (3 << 0)
92#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
93#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
94/* PGA mode: 0 = differential, 1 = signle-ended */
95#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
96#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
97#define IDX_DAC_CFG 0x0003
98/* SZC bitmask, 4 modes below:
99 * 0 = Immediate
100 * 1 = zero-cross
101 * 2 = soft-ramp
102 * 3 = soft-ramp on zero-cross
103 */
104#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
105#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
106#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
107
108#define IDX_BEEP_CFG 0x0004
109/* 0x0008 - test reg key */
110/* 0x0009 - 0x0014 -> 12 test regs */
111/* 0x0015 - visibility reg */
112
113
277a57c7 114static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
40c20fa0
TI
115{
116 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
117 AC_VERB_SET_COEF_INDEX, idx);
118 return snd_hda_codec_read(codec, CS420X_VENDOR_NID, 0,
119 AC_VERB_GET_PROC_COEF, 0);
120}
121
277a57c7
TI
122static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
123 unsigned int coef)
40c20fa0
TI
124{
125 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
126 AC_VERB_SET_COEF_INDEX, idx);
127 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
128 AC_VERB_SET_PROC_COEF, coef);
129}
130
131
e5f14248
TI
132#define HP_EVENT 1
133#define MIC_EVENT 2
134
135/*
136 * PCM callbacks
137 */
138static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
139 struct hda_codec *codec,
140 struct snd_pcm_substream *substream)
141{
142 struct cs_spec *spec = codec->spec;
143 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
144 hinfo);
145}
146
147static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
148 struct hda_codec *codec,
149 unsigned int stream_tag,
150 unsigned int format,
151 struct snd_pcm_substream *substream)
152{
153 struct cs_spec *spec = codec->spec;
154 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
155 stream_tag, format, substream);
156}
157
158static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
159 struct hda_codec *codec,
160 struct snd_pcm_substream *substream)
161{
162 struct cs_spec *spec = codec->spec;
163 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
164}
165
166/*
167 * Digital out
168 */
169static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
170 struct hda_codec *codec,
171 struct snd_pcm_substream *substream)
172{
173 struct cs_spec *spec = codec->spec;
174 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
175}
176
177static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
178 struct hda_codec *codec,
179 struct snd_pcm_substream *substream)
180{
181 struct cs_spec *spec = codec->spec;
182 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
183}
184
185static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
186 struct hda_codec *codec,
187 unsigned int stream_tag,
188 unsigned int format,
189 struct snd_pcm_substream *substream)
190{
191 struct cs_spec *spec = codec->spec;
192 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
193 format, substream);
194}
195
196static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
197 struct hda_codec *codec,
198 struct snd_pcm_substream *substream)
199{
200 struct cs_spec *spec = codec->spec;
201 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
202}
203
204/*
205 * Analog capture
206 */
207static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
208 struct hda_codec *codec,
209 unsigned int stream_tag,
210 unsigned int format,
211 struct snd_pcm_substream *substream)
212{
213 struct cs_spec *spec = codec->spec;
214 spec->cur_adc = spec->adc_nid[spec->cur_input];
215 spec->cur_adc_stream_tag = stream_tag;
216 spec->cur_adc_format = format;
217 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
218 return 0;
219}
220
221static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
222 struct hda_codec *codec,
223 struct snd_pcm_substream *substream)
224{
225 struct cs_spec *spec = codec->spec;
226 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
227 spec->cur_adc = 0;
228 return 0;
229}
230
231/*
232 */
233static struct hda_pcm_stream cs_pcm_analog_playback = {
234 .substreams = 1,
235 .channels_min = 2,
236 .channels_max = 2,
237 .ops = {
238 .open = cs_playback_pcm_open,
239 .prepare = cs_playback_pcm_prepare,
240 .cleanup = cs_playback_pcm_cleanup
241 },
242};
243
244static struct hda_pcm_stream cs_pcm_analog_capture = {
245 .substreams = 1,
246 .channels_min = 2,
247 .channels_max = 2,
248 .ops = {
249 .prepare = cs_capture_pcm_prepare,
250 .cleanup = cs_capture_pcm_cleanup
251 },
252};
253
254static struct hda_pcm_stream cs_pcm_digital_playback = {
255 .substreams = 1,
256 .channels_min = 2,
257 .channels_max = 2,
258 .ops = {
259 .open = cs_dig_playback_pcm_open,
260 .close = cs_dig_playback_pcm_close,
261 .prepare = cs_dig_playback_pcm_prepare,
262 .cleanup = cs_dig_playback_pcm_cleanup
263 },
264};
265
266static struct hda_pcm_stream cs_pcm_digital_capture = {
267 .substreams = 1,
268 .channels_min = 2,
269 .channels_max = 2,
270};
271
272static int cs_build_pcms(struct hda_codec *codec)
273{
274 struct cs_spec *spec = codec->spec;
275 struct hda_pcm *info = spec->pcm_rec;
276
277 codec->pcm_info = info;
278 codec->num_pcms = 0;
279
280 info->name = "Cirrus Analog";
281 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
282 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
283 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
284 spec->multiout.max_channels;
285 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
e5f14248
TI
286 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
287 spec->adc_nid[spec->cur_input];
288 codec->num_pcms++;
289
290 if (!spec->multiout.dig_out_nid && !spec->dig_in)
291 return 0;
292
293 info++;
294 info->name = "Cirrus Digital";
295 info->pcm_type = spec->autocfg.dig_out_type[0];
296 if (!info->pcm_type)
297 info->pcm_type = HDA_PCM_TYPE_SPDIF;
298 if (spec->multiout.dig_out_nid) {
299 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
300 cs_pcm_digital_playback;
301 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
302 spec->multiout.dig_out_nid;
303 }
304 if (spec->dig_in) {
305 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
306 cs_pcm_digital_capture;
307 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
308 }
309 codec->num_pcms++;
310
311 return 0;
312}
313
21a4dc43
TI
314/*
315 * parse codec topology
316 */
317
e5f14248
TI
318static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
319{
320 hda_nid_t dac;
321 if (!pin)
322 return 0;
323 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
324 return 0;
325 return dac;
326}
327
21a4dc43
TI
328static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
329{
330 struct cs_spec *spec = codec->spec;
331 struct auto_pin_cfg *cfg = &spec->autocfg;
332 hda_nid_t pin = cfg->input_pins[idx];
333 unsigned int val = snd_hda_query_pin_caps(codec, pin);
334 if (!(val & AC_PINCAP_PRES_DETECT))
335 return 0;
336 val = snd_hda_codec_get_pincfg(codec, pin);
337 return (get_defcfg_connect(val) == AC_JACK_PORT_COMPLEX);
338}
339
340static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
341 unsigned int *idxp)
342{
343 int i;
344 hda_nid_t nid;
345
346 nid = codec->start_nid;
347 for (i = 0; i < codec->num_nodes; i++, nid++) {
348 hda_nid_t pins[2];
349 unsigned int type;
350 int j, nums;
351 type = (get_wcaps(codec, nid) & AC_WCAP_TYPE)
352 >> AC_WCAP_TYPE_SHIFT;
353 if (type != AC_WID_AUD_IN)
354 continue;
355 nums = snd_hda_get_connections(codec, nid, pins,
356 ARRAY_SIZE(pins));
357 if (nums <= 0)
358 continue;
359 for (j = 0; j < nums; j++) {
360 if (pins[j] == pin) {
361 *idxp = j;
362 return nid;
363 }
364 }
365 }
366 return 0;
367}
368
40c20fa0
TI
369static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
370{
40c20fa0
TI
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;
277a57c7 380 int i, extra_nids;
21a4dc43
TI
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;
277a57c7 425 int i;
21a4dc43
TI
426
427 for (i = 0; i < AUTO_PIN_LAST; i++) {
428 hda_nid_t pin = cfg->input_pins[i];
21a4dc43
TI
429 if (!pin)
430 continue;
431 spec->input_idx[spec->num_inputs] = i;
432 spec->capsrc_idx[i] = spec->num_inputs++;
433 spec->cur_input = i;
434 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
435 }
436 if (!spec->num_inputs)
437 return 0;
438
439 /* check whether the automatic mic switch is available */
440 if (spec->num_inputs == 2 &&
441 spec->adc_nid[AUTO_PIN_MIC] && spec->adc_nid[AUTO_PIN_FRONT_MIC]) {
442 if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_FRONT_MIC])) {
443 if (!is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
444 spec->mic_detect = 1;
445 spec->automic_idx = AUTO_PIN_FRONT_MIC;
446 }
447 } else {
448 if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
449 spec->mic_detect = 1;
450 spec->automic_idx = AUTO_PIN_MIC;
451 }
452 }
453 }
454 return 0;
455}
456
457
458static int parse_digital_output(struct hda_codec *codec)
459{
460 struct cs_spec *spec = codec->spec;
461 struct auto_pin_cfg *cfg = &spec->autocfg;
462 hda_nid_t nid;
21a4dc43
TI
463
464 if (!cfg->dig_outs)
465 return 0;
466 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
467 return 0;
468 spec->multiout.dig_out_nid = nid;
469 spec->multiout.share_spdif = 1;
470 if (cfg->dig_outs > 1 &&
471 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
472 spec->slave_dig_outs[0] = nid;
473 codec->slave_dig_outs = spec->slave_dig_outs;
474 }
475 return 0;
476}
477
478static int parse_digital_input(struct hda_codec *codec)
479{
480 struct cs_spec *spec = codec->spec;
481 struct auto_pin_cfg *cfg = &spec->autocfg;
482 int idx;
483
60e53882
TI
484 if (cfg->dig_in_pin)
485 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
486 return 0;
21a4dc43
TI
487}
488
489/*
490 * create mixer controls
491 */
492
e5f14248
TI
493static const char *dir_sfx[2] = { "Playback", "Capture" };
494
495static int add_mute(struct hda_codec *codec, const char *name, int index,
496 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
497{
b4dabfc4 498 char tmp[44];
e5f14248
TI
499 struct snd_kcontrol_new knew =
500 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
501 knew.private_value = pval;
502 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
503 *kctlp = snd_ctl_new1(&knew, codec);
9e3fd871 504 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
5e26dfd0 505 return snd_hda_ctl_add(codec, 0, *kctlp);
e5f14248
TI
506}
507
508static int add_volume(struct hda_codec *codec, const char *name,
509 int index, unsigned int pval, int dir,
510 struct snd_kcontrol **kctlp)
511{
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);
9e3fd871 518 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
5e26dfd0 519 return snd_hda_ctl_add(codec, 0, *kctlp);
e5f14248
TI
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);
3911a4c1 542 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
e5f14248
TI
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);
3911a4c1 549 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
e5f14248
TI
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;
e5f14248 654
ea35929b 655 if (spec->cur_input == idx && !force)
e5f14248
TI
656 return 0;
657 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
658 /* stream is running, let's swap the current ADC */
659 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
660 spec->cur_adc = spec->adc_nid[idx];
661 snd_hda_codec_setup_stream(codec, spec->cur_adc,
662 spec->cur_adc_stream_tag, 0,
663 spec->cur_adc_format);
664 }
665 snd_hda_codec_write(codec, spec->cur_adc, 0,
666 AC_VERB_SET_CONNECT_SEL,
667 spec->adc_idx[idx]);
668 spec->cur_input = idx;
669 return 1;
670}
671
672static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
673 struct snd_ctl_elem_info *uinfo)
674{
675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
676 struct cs_spec *spec = codec->spec;
677 unsigned int idx;
678
679 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
680 uinfo->count = 1;
681 uinfo->value.enumerated.items = spec->num_inputs;
682 if (uinfo->value.enumerated.item >= spec->num_inputs)
683 uinfo->value.enumerated.item = spec->num_inputs - 1;
684 idx = spec->input_idx[uinfo->value.enumerated.item];
685 strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
686 return 0;
687}
688
689static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
690 struct snd_ctl_elem_value *ucontrol)
691{
692 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
693 struct cs_spec *spec = codec->spec;
694 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
695 return 0;
696}
697
698static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
699 struct snd_ctl_elem_value *ucontrol)
700{
701 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
702 struct cs_spec *spec = codec->spec;
703 unsigned int idx = ucontrol->value.enumerated.item[0];
704
705 if (idx >= spec->num_inputs)
706 return -EINVAL;
707 idx = spec->input_idx[idx];
ea35929b 708 return change_cur_input(codec, idx, 0);
e5f14248
TI
709}
710
711static struct snd_kcontrol_new cs_capture_source = {
712 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
713 .name = "Capture Source",
714 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
715 .info = cs_capture_source_info,
716 .get = cs_capture_source_get,
717 .put = cs_capture_source_put,
718};
719
e5f14248
TI
720static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
721 struct hda_ctl_ops *ops)
722{
723 struct cs_spec *spec = codec->spec;
724 struct hda_bind_ctls *bind;
725 int i, n;
726
727 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
728 GFP_KERNEL);
729 if (!bind)
730 return NULL;
731 bind->ops = ops;
732 n = 0;
733 for (i = 0; i < AUTO_PIN_LAST; i++) {
734 if (!spec->adc_nid[i])
735 continue;
736 bind->values[n++] =
737 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
738 spec->adc_idx[i], HDA_INPUT);
739 }
740 return bind;
741}
742
743static int build_input(struct hda_codec *codec)
744{
745 struct cs_spec *spec = codec->spec;
21a4dc43 746 int i, err;
e5f14248 747
e5f14248
TI
748 if (!spec->num_inputs)
749 return 0;
750
e5f14248
TI
751 /* make bind-capture */
752 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
753 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
754 for (i = 0; i < 2; i++) {
755 struct snd_kcontrol *kctl;
21949f00 756 int n;
e5f14248
TI
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];
3911a4c1 763 err = snd_hda_ctl_add(codec, 0, kctl);
e5f14248
TI
764 if (err < 0)
765 return err;
21949f00
TI
766 for (n = 0; n < AUTO_PIN_LAST; n++) {
767 if (!spec->adc_nid[n])
768 continue;
769 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[i]);
770 if (err < 0)
771 return err;
772 }
e5f14248
TI
773 }
774
775 if (spec->num_inputs > 1 && !spec->mic_detect) {
3911a4c1 776 err = snd_hda_ctl_add(codec, 0,
e5f14248
TI
777 snd_ctl_new1(&cs_capture_source, codec));
778 if (err < 0)
779 return err;
780 }
781
782 return 0;
783}
784
21a4dc43
TI
785/*
786 */
787
e5f14248
TI
788static int build_digital_output(struct hda_codec *codec)
789{
790 struct cs_spec *spec = codec->spec;
e5f14248
TI
791 int err;
792
63b2413b
TI
793 if (!spec->multiout.dig_out_nid)
794 return 0;
795
e5f14248
TI
796 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
797 if (err < 0)
798 return err;
799 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
800 if (err < 0)
801 return err;
e5f14248
TI
802 return 0;
803}
804
805static int build_digital_input(struct hda_codec *codec)
806{
807 struct cs_spec *spec = codec->spec;
21a4dc43
TI
808 if (spec->dig_in)
809 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
810 return 0;
e5f14248
TI
811}
812
21a4dc43
TI
813/*
814 * auto-mute and auto-mic switching
815 */
816
e5f14248
TI
817static void cs_automute(struct hda_codec *codec)
818{
819 struct cs_spec *spec = codec->spec;
820 struct auto_pin_cfg *cfg = &spec->autocfg;
864f92be 821 unsigned int caps, hp_present;
e5f14248
TI
822 hda_nid_t nid;
823 int i;
824
825 hp_present = 0;
826 for (i = 0; i < cfg->hp_outs; i++) {
827 nid = cfg->hp_pins[i];
828 caps = snd_hda_query_pin_caps(codec, nid);
829 if (!(caps & AC_PINCAP_PRES_DETECT))
830 continue;
864f92be 831 hp_present = snd_hda_jack_detect(codec, nid);
e5f14248
TI
832 if (hp_present)
833 break;
834 }
835 for (i = 0; i < cfg->speaker_outs; i++) {
836 nid = cfg->speaker_pins[i];
837 snd_hda_codec_write(codec, nid, 0,
838 AC_VERB_SET_PIN_WIDGET_CONTROL,
839 hp_present ? 0 : PIN_OUT);
840 }
1a5ba2e9
RAE
841 if (spec->board_config == CS420X_MBP55 ||
842 spec->board_config == CS420X_IMAC27) {
3a385167
SP
843 unsigned int gpio = hp_present ? 0x02 : 0x08;
844 snd_hda_codec_write(codec, 0x01, 0,
845 AC_VERB_SET_GPIO_DATA, gpio);
846 }
e5f14248
TI
847}
848
849static void cs_automic(struct hda_codec *codec)
850{
851 struct cs_spec *spec = codec->spec;
852 struct auto_pin_cfg *cfg = &spec->autocfg;
853 hda_nid_t nid;
864f92be 854 unsigned int present;
e5f14248
TI
855
856 nid = cfg->input_pins[spec->automic_idx];
864f92be
WF
857 present = snd_hda_jack_detect(codec, nid);
858 if (present)
ea35929b 859 change_cur_input(codec, spec->automic_idx, 0);
e5f14248
TI
860 else {
861 unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
862 AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
ea35929b 863 change_cur_input(codec, imic, 0);
e5f14248
TI
864 }
865}
866
867/*
868 */
869
870static void init_output(struct hda_codec *codec)
871{
872 struct cs_spec *spec = codec->spec;
873 struct auto_pin_cfg *cfg = &spec->autocfg;
874 int i;
875
876 /* mute first */
877 for (i = 0; i < spec->multiout.num_dacs; i++)
878 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
879 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
880 if (spec->multiout.hp_nid)
881 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
882 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
883 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
884 if (!spec->multiout.extra_out_nid[i])
885 break;
886 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
887 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
888 }
889
890 /* set appropriate pin controls */
891 for (i = 0; i < cfg->line_outs; i++)
892 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
893 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
894 for (i = 0; i < cfg->hp_outs; i++) {
895 hda_nid_t nid = cfg->hp_pins[i];
896 snd_hda_codec_write(codec, nid, 0,
897 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
898 if (!cfg->speaker_outs)
899 continue;
900 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
901 snd_hda_codec_write(codec, nid, 0,
902 AC_VERB_SET_UNSOLICITED_ENABLE,
903 AC_USRSP_EN | HP_EVENT);
904 spec->hp_detect = 1;
905 }
906 }
907 for (i = 0; i < cfg->speaker_outs; i++)
908 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
909 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
910 if (spec->hp_detect)
911 cs_automute(codec);
912}
913
914static void init_input(struct hda_codec *codec)
915{
916 struct cs_spec *spec = codec->spec;
917 struct auto_pin_cfg *cfg = &spec->autocfg;
40c20fa0 918 unsigned int coef;
e5f14248
TI
919 int i;
920
921 for (i = 0; i < AUTO_PIN_LAST; i++) {
922 unsigned int ctl;
923 hda_nid_t pin = cfg->input_pins[i];
924 if (!pin || !spec->adc_nid[i])
925 continue;
926 /* set appropriate pin control and mute first */
927 ctl = PIN_IN;
928 if (i <= AUTO_PIN_FRONT_MIC) {
929 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
930 caps >>= AC_PINCAP_VREF_SHIFT;
931 if (caps & AC_PINCAP_VREF_80)
932 ctl = PIN_VREF80;
933 }
934 snd_hda_codec_write(codec, pin, 0,
935 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
936 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
937 AC_VERB_SET_AMP_GAIN_MUTE,
938 AMP_IN_MUTE(spec->adc_idx[i]));
939 if (spec->mic_detect && spec->automic_idx == i)
940 snd_hda_codec_write(codec, pin, 0,
941 AC_VERB_SET_UNSOLICITED_ENABLE,
942 AC_USRSP_EN | MIC_EVENT);
943 }
ea35929b 944 change_cur_input(codec, spec->cur_input, 1);
e5f14248
TI
945 if (spec->mic_detect)
946 cs_automic(codec);
40c20fa0
TI
947
948 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
949 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
950 coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
951 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
952 coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0
b71a8eb0 953 * No effect if SPDIF_OUT2 is selected in
40c20fa0
TI
954 * IDX_SPDIF_CTL.
955 */
956 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
957}
958
959static struct hda_verb cs_coef_init_verbs[] = {
960 {0x11, AC_VERB_SET_PROC_STATE, 1},
961 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
962 {0x11, AC_VERB_SET_PROC_COEF,
963 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
964 | 0x0040 /* Mute DACs on FIFO error */
965 | 0x1000 /* Enable DACs High Pass Filter */
966 | 0x0400 /* Disable Coefficient Auto increment */
967 )},
968 /* Beep */
969 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
970 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
971
972 {} /* terminator */
973};
974
975/* SPDIF setup */
976static void init_digital(struct hda_codec *codec)
977{
978 unsigned int coef;
979
980 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
981 coef |= 0x0008; /* Replace with mute on error */
982 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
983 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
984 * SPDIF_OUT2 is shared with GPIO1 and
985 * DMIC_SDA2.
986 */
987 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
e5f14248
TI
988}
989
990static int cs_init(struct hda_codec *codec)
991{
992 struct cs_spec *spec = codec->spec;
993
40c20fa0 994 snd_hda_sequence_write(codec, cs_coef_init_verbs);
ed208255
TI
995
996 if (spec->gpio_mask) {
997 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
998 spec->gpio_mask);
999 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1000 spec->gpio_dir);
1001 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1002 spec->gpio_data);
1003 }
1004
e5f14248
TI
1005 init_output(codec);
1006 init_input(codec);
40c20fa0 1007 init_digital(codec);
e5f14248
TI
1008 return 0;
1009}
1010
1011static int cs_build_controls(struct hda_codec *codec)
1012{
e5f14248
TI
1013 int err;
1014
1015 err = build_output(codec);
1016 if (err < 0)
1017 return err;
1018 err = build_input(codec);
1019 if (err < 0)
1020 return err;
1021 err = build_digital_output(codec);
1022 if (err < 0)
1023 return err;
1024 err = build_digital_input(codec);
1025 if (err < 0)
1026 return err;
e5f14248
TI
1027 return cs_init(codec);
1028}
1029
1030static void cs_free(struct hda_codec *codec)
1031{
1032 struct cs_spec *spec = codec->spec;
1033 kfree(spec->capture_bind[0]);
1034 kfree(spec->capture_bind[1]);
1035 kfree(codec->spec);
1036}
1037
1038static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1039{
1040 switch ((res >> 26) & 0x7f) {
1041 case HP_EVENT:
1042 cs_automute(codec);
1043 break;
1044 case MIC_EVENT:
1045 cs_automic(codec);
1046 break;
1047 }
1048}
1049
1050static struct hda_codec_ops cs_patch_ops = {
1051 .build_controls = cs_build_controls,
1052 .build_pcms = cs_build_pcms,
1053 .init = cs_init,
1054 .free = cs_free,
1055 .unsol_event = cs_unsol_event,
1056};
1057
1058static int cs_parse_auto_config(struct hda_codec *codec)
1059{
1060 struct cs_spec *spec = codec->spec;
1061 int err;
1062
1063 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
ed208255
TI
1064 if (err < 0)
1065 return err;
1066
1067 err = parse_output(codec);
1068 if (err < 0)
1069 return err;
1070 err = parse_input(codec);
1071 if (err < 0)
1072 return err;
1073 err = parse_digital_output(codec);
1074 if (err < 0)
1075 return err;
1076 err = parse_digital_input(codec);
e5f14248
TI
1077 if (err < 0)
1078 return err;
1079 return 0;
1080}
1081
a6bae205
TI
1082static const char *cs420x_models[CS420X_MODELS] = {
1083 [CS420X_MBP55] = "mbp55",
1a5ba2e9 1084 [CS420X_IMAC27] = "imac27",
a6bae205
TI
1085 [CS420X_AUTO] = "auto",
1086};
1087
1088
1089static struct snd_pci_quirk cs420x_cfg_tbl[] = {
1090 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
1a5ba2e9 1091 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
a6bae205
TI
1092 {} /* terminator */
1093};
1094
1095struct cs_pincfg {
1096 hda_nid_t nid;
1097 u32 val;
1098};
1099
1100static struct cs_pincfg mbp55_pincfgs[] = {
1101 { 0x09, 0x012b4030 },
1102 { 0x0a, 0x90100121 },
1103 { 0x0b, 0x90100120 },
1104 { 0x0c, 0x400000f0 },
1105 { 0x0d, 0x90a00110 },
1106 { 0x0e, 0x400000f0 },
1107 { 0x0f, 0x400000f0 },
1108 { 0x10, 0x014be040 },
1109 { 0x12, 0x400000f0 },
1110 { 0x15, 0x400000f0 },
1111 {} /* terminator */
1112};
1113
1a5ba2e9
RAE
1114static struct cs_pincfg imac27_pincfgs[] = {
1115 { 0x09, 0x012b4050 },
1116 { 0x0a, 0x90100140 },
1117 { 0x0b, 0x90100142 },
1118 { 0x0c, 0x018b3020 },
1119 { 0x0d, 0x90a00110 },
1120 { 0x0e, 0x400000f0 },
1121 { 0x0f, 0x01cbe030 },
1122 { 0x10, 0x014be060 },
1123 { 0x12, 0x01ab9070 },
1124 { 0x15, 0x400000f0 },
1125 {} /* terminator */
1126};
1127
a6bae205
TI
1128static struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
1129 [CS420X_MBP55] = mbp55_pincfgs,
1a5ba2e9 1130 [CS420X_IMAC27] = imac27_pincfgs,
a6bae205
TI
1131};
1132
1133static void fix_pincfg(struct hda_codec *codec, int model)
1134{
1135 const struct cs_pincfg *cfg = cs_pincfgs[model];
1136 if (!cfg)
1137 return;
1138 for (; cfg->nid; cfg++)
1139 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1140}
1141
e5f14248
TI
1142
1143static int patch_cs420x(struct hda_codec *codec)
1144{
1145 struct cs_spec *spec;
1146 int err;
1147
1148 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1149 if (!spec)
1150 return -ENOMEM;
1151 codec->spec = spec;
1152
a6bae205
TI
1153 spec->board_config =
1154 snd_hda_check_board_config(codec, CS420X_MODELS,
1155 cs420x_models, cs420x_cfg_tbl);
1156 if (spec->board_config >= 0)
1157 fix_pincfg(codec, spec->board_config);
1158
ed208255 1159 switch (spec->board_config) {
1a5ba2e9 1160 case CS420X_IMAC27:
ed208255 1161 case CS420X_MBP55:
3a385167
SP
1162 /* GPIO1 = headphones */
1163 /* GPIO3 = speakers */
1164 spec->gpio_mask = 0x0a;
1165 spec->gpio_dir = 0x0a;
ed208255
TI
1166 break;
1167 }
e5f14248 1168
ed208255 1169 err = cs_parse_auto_config(codec);
21a4dc43
TI
1170 if (err < 0)
1171 goto error;
1172
e5f14248
TI
1173 codec->patch_ops = cs_patch_ops;
1174
1175 return 0;
1176
1177 error:
1178 kfree(codec->spec);
1179 codec->spec = NULL;
1180 return err;
1181}
1182
1183
1184/*
1185 * patch entries
1186 */
1187static struct hda_codec_preset snd_hda_preset_cirrus[] = {
1188 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1189 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1190 {} /* terminator */
1191};
1192
1193MODULE_ALIAS("snd-hda-codec-id:10134206");
1194MODULE_ALIAS("snd-hda-codec-id:10134207");
1195
1196MODULE_LICENSE("GPL");
1197MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1198
1199static struct hda_codec_preset_list cirrus_list = {
1200 .preset = snd_hda_preset_cirrus,
1201 .owner = THIS_MODULE,
1202};
1203
1204static int __init patch_cirrus_init(void)
1205{
1206 return snd_hda_add_codec_preset(&cirrus_list);
1207}
1208
1209static void __exit patch_cirrus_exit(void)
1210{
1211 snd_hda_delete_codec_preset(&cirrus_list);
1212}
1213
1214module_init(patch_cirrus_init)
1215module_exit(patch_cirrus_exit)