ALSA: HDA: Fix master control for Cirrus Logic 421X
[deliverable/linux.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>
da155d5b 25#include <linux/module.h>
e5f14248
TI
26#include <sound/core.h>
27#include "hda_codec.h"
28#include "hda_local.h"
1835a0f9 29#include "hda_jack.h"
56487c27 30#include <sound/tlv.h>
e5f14248
TI
31
32/*
33 */
34
35struct cs_spec {
a6bae205 36 int board_config;
e5f14248
TI
37 struct auto_pin_cfg autocfg;
38 struct hda_multi_out multiout;
39 struct snd_kcontrol *vmaster_sw;
40 struct snd_kcontrol *vmaster_vol;
41
42 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
43 hda_nid_t slave_dig_outs[2];
44
45 unsigned int input_idx[AUTO_PIN_LAST];
46 unsigned int capsrc_idx[AUTO_PIN_LAST];
47 hda_nid_t adc_nid[AUTO_PIN_LAST];
48 unsigned int adc_idx[AUTO_PIN_LAST];
49 unsigned int num_inputs;
50 unsigned int cur_input;
51 unsigned int automic_idx;
52 hda_nid_t cur_adc;
53 unsigned int cur_adc_stream_tag;
54 unsigned int cur_adc_format;
55 hda_nid_t dig_in;
56
c42d4782 57 const struct hda_bind_ctls *capture_bind[2];
e5f14248 58
ed208255
TI
59 unsigned int gpio_mask;
60 unsigned int gpio_dir;
61 unsigned int gpio_data;
6dfeb703
TI
62 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
63 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
ed208255 64
e5f14248
TI
65 struct hda_pcm pcm_rec[2]; /* PCM information */
66
67 unsigned int hp_detect:1;
68 unsigned int mic_detect:1;
56487c27
TH
69 /* CS421x */
70 unsigned int spdif_detect:1;
71 unsigned int sense_b:1;
72 hda_nid_t vendor_nid;
73 struct hda_input_mux input_mux;
74 unsigned int last_input;
e5f14248
TI
75};
76
56487c27 77/* available models with CS420x */
a6bae205 78enum {
4e7d7c60 79 CS420X_MBP53,
a6bae205 80 CS420X_MBP55,
1a5ba2e9 81 CS420X_IMAC27,
6dfeb703 82 CS420X_APPLE,
a6bae205
TI
83 CS420X_AUTO,
84 CS420X_MODELS
85};
86
56487c27
TH
87/* CS421x boards */
88enum {
89 CS421X_CDB4210,
90 CS421X_MODELS
91};
92
40c20fa0
TI
93/* Vendor-specific processing widget */
94#define CS420X_VENDOR_NID 0x11
95#define CS_DIG_OUT1_PIN_NID 0x10
96#define CS_DIG_OUT2_PIN_NID 0x15
97#define CS_DMIC1_PIN_NID 0x12
98#define CS_DMIC2_PIN_NID 0x0e
99
100/* coef indices */
101#define IDX_SPDIF_STAT 0x0000
102#define IDX_SPDIF_CTL 0x0001
103#define IDX_ADC_CFG 0x0002
104/* SZC bitmask, 4 modes below:
105 * 0 = immediate,
106 * 1 = digital immediate, analog zero-cross
107 * 2 = digtail & analog soft-ramp
108 * 3 = digital soft-ramp, analog zero-cross
109 */
110#define CS_COEF_ADC_SZC_MASK (3 << 0)
111#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
112#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
113/* PGA mode: 0 = differential, 1 = signle-ended */
114#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
115#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
116#define IDX_DAC_CFG 0x0003
117/* SZC bitmask, 4 modes below:
118 * 0 = Immediate
119 * 1 = zero-cross
120 * 2 = soft-ramp
121 * 3 = soft-ramp on zero-cross
122 */
123#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
124#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
125#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
126
127#define IDX_BEEP_CFG 0x0004
128/* 0x0008 - test reg key */
129/* 0x0009 - 0x0014 -> 12 test regs */
130/* 0x0015 - visibility reg */
131
56487c27
TH
132/*
133 * Cirrus Logic CS4210
134 *
135 * 1 DAC => HP(sense) / Speakers,
136 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
137 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
138*/
139#define CS4210_DAC_NID 0x02
140#define CS4210_ADC_NID 0x03
141#define CS421X_VENDOR_NID 0x0B
142#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
143#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
144
145#define CS421X_IDX_DEV_CFG 0x01
146#define CS421X_IDX_ADC_CFG 0x02
147#define CS421X_IDX_DAC_CFG 0x03
148#define CS421X_IDX_SPK_CTL 0x04
149
150#define SPDIF_EVENT 0x04
40c20fa0 151
277a57c7 152static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
40c20fa0 153{
56487c27
TH
154 struct cs_spec *spec = codec->spec;
155 snd_hda_codec_write(codec, spec->vendor_nid, 0,
40c20fa0 156 AC_VERB_SET_COEF_INDEX, idx);
56487c27 157 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
40c20fa0
TI
158 AC_VERB_GET_PROC_COEF, 0);
159}
160
277a57c7
TI
161static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
162 unsigned int coef)
40c20fa0 163{
56487c27
TH
164 struct cs_spec *spec = codec->spec;
165 snd_hda_codec_write(codec, spec->vendor_nid, 0,
40c20fa0 166 AC_VERB_SET_COEF_INDEX, idx);
56487c27 167 snd_hda_codec_write(codec, spec->vendor_nid, 0,
40c20fa0
TI
168 AC_VERB_SET_PROC_COEF, coef);
169}
170
171
e5f14248
TI
172#define HP_EVENT 1
173#define MIC_EVENT 2
174
175/*
176 * PCM callbacks
177 */
178static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
179 struct hda_codec *codec,
180 struct snd_pcm_substream *substream)
181{
182 struct cs_spec *spec = codec->spec;
183 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
184 hinfo);
185}
186
187static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
188 struct hda_codec *codec,
189 unsigned int stream_tag,
190 unsigned int format,
191 struct snd_pcm_substream *substream)
192{
193 struct cs_spec *spec = codec->spec;
194 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
195 stream_tag, format, substream);
196}
197
198static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
199 struct hda_codec *codec,
200 struct snd_pcm_substream *substream)
201{
202 struct cs_spec *spec = codec->spec;
203 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
204}
205
206/*
207 * Digital out
208 */
209static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
210 struct hda_codec *codec,
211 struct snd_pcm_substream *substream)
212{
213 struct cs_spec *spec = codec->spec;
214 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
215}
216
217static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
218 struct hda_codec *codec,
219 struct snd_pcm_substream *substream)
220{
221 struct cs_spec *spec = codec->spec;
222 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
223}
224
225static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
226 struct hda_codec *codec,
227 unsigned int stream_tag,
228 unsigned int format,
229 struct snd_pcm_substream *substream)
230{
231 struct cs_spec *spec = codec->spec;
232 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
233 format, substream);
234}
235
236static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
237 struct hda_codec *codec,
238 struct snd_pcm_substream *substream)
239{
240 struct cs_spec *spec = codec->spec;
241 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
242}
243
05ee7964
TI
244static void cs_update_input_select(struct hda_codec *codec)
245{
246 struct cs_spec *spec = codec->spec;
247 if (spec->cur_adc)
248 snd_hda_codec_write(codec, spec->cur_adc, 0,
249 AC_VERB_SET_CONNECT_SEL,
250 spec->adc_idx[spec->cur_input]);
251}
252
e5f14248
TI
253/*
254 * Analog capture
255 */
256static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
257 struct hda_codec *codec,
258 unsigned int stream_tag,
259 unsigned int format,
260 struct snd_pcm_substream *substream)
261{
262 struct cs_spec *spec = codec->spec;
263 spec->cur_adc = spec->adc_nid[spec->cur_input];
264 spec->cur_adc_stream_tag = stream_tag;
265 spec->cur_adc_format = format;
05ee7964 266 cs_update_input_select(codec);
e5f14248
TI
267 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
268 return 0;
269}
270
271static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
272 struct hda_codec *codec,
273 struct snd_pcm_substream *substream)
274{
275 struct cs_spec *spec = codec->spec;
276 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
277 spec->cur_adc = 0;
278 return 0;
279}
280
281/*
282 */
c42d4782 283static const struct hda_pcm_stream cs_pcm_analog_playback = {
e5f14248
TI
284 .substreams = 1,
285 .channels_min = 2,
286 .channels_max = 2,
287 .ops = {
288 .open = cs_playback_pcm_open,
289 .prepare = cs_playback_pcm_prepare,
290 .cleanup = cs_playback_pcm_cleanup
291 },
292};
293
c42d4782 294static const struct hda_pcm_stream cs_pcm_analog_capture = {
e5f14248
TI
295 .substreams = 1,
296 .channels_min = 2,
297 .channels_max = 2,
298 .ops = {
299 .prepare = cs_capture_pcm_prepare,
300 .cleanup = cs_capture_pcm_cleanup
301 },
302};
303
c42d4782 304static const struct hda_pcm_stream cs_pcm_digital_playback = {
e5f14248
TI
305 .substreams = 1,
306 .channels_min = 2,
307 .channels_max = 2,
308 .ops = {
309 .open = cs_dig_playback_pcm_open,
310 .close = cs_dig_playback_pcm_close,
311 .prepare = cs_dig_playback_pcm_prepare,
312 .cleanup = cs_dig_playback_pcm_cleanup
313 },
314};
315
c42d4782 316static const struct hda_pcm_stream cs_pcm_digital_capture = {
e5f14248
TI
317 .substreams = 1,
318 .channels_min = 2,
319 .channels_max = 2,
320};
321
322static int cs_build_pcms(struct hda_codec *codec)
323{
324 struct cs_spec *spec = codec->spec;
325 struct hda_pcm *info = spec->pcm_rec;
326
327 codec->pcm_info = info;
328 codec->num_pcms = 0;
329
330 info->name = "Cirrus Analog";
331 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
332 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
333 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
334 spec->multiout.max_channels;
335 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
e5f14248
TI
336 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
337 spec->adc_nid[spec->cur_input];
338 codec->num_pcms++;
339
340 if (!spec->multiout.dig_out_nid && !spec->dig_in)
341 return 0;
342
343 info++;
344 info->name = "Cirrus Digital";
345 info->pcm_type = spec->autocfg.dig_out_type[0];
346 if (!info->pcm_type)
347 info->pcm_type = HDA_PCM_TYPE_SPDIF;
348 if (spec->multiout.dig_out_nid) {
349 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
350 cs_pcm_digital_playback;
351 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
352 spec->multiout.dig_out_nid;
353 }
354 if (spec->dig_in) {
355 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
356 cs_pcm_digital_capture;
357 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
358 }
359 codec->num_pcms++;
360
361 return 0;
362}
363
21a4dc43
TI
364/*
365 * parse codec topology
366 */
367
e5f14248
TI
368static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
369{
370 hda_nid_t dac;
371 if (!pin)
372 return 0;
373 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
374 return 0;
375 return dac;
376}
377
21a4dc43
TI
378static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
379{
380 struct cs_spec *spec = codec->spec;
381 struct auto_pin_cfg *cfg = &spec->autocfg;
c1e0bb92 382 hda_nid_t pin = cfg->inputs[idx].pin;
06dec228
TI
383 unsigned int val;
384 if (!is_jack_detectable(codec, pin))
21a4dc43
TI
385 return 0;
386 val = snd_hda_codec_get_pincfg(codec, pin);
99ae28be 387 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
21a4dc43
TI
388}
389
390static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
391 unsigned int *idxp)
392{
1b004d03 393 int i, idx;
21a4dc43
TI
394 hda_nid_t nid;
395
396 nid = codec->start_nid;
397 for (i = 0; i < codec->num_nodes; i++, nid++) {
21a4dc43 398 unsigned int type;
1682c817 399 type = get_wcaps_type(get_wcaps(codec, nid));
21a4dc43
TI
400 if (type != AC_WID_AUD_IN)
401 continue;
1b004d03
TI
402 idx = snd_hda_get_conn_index(codec, nid, pin, false);
403 if (idx >= 0) {
404 *idxp = idx;
8d087c76 405 return nid;
1b004d03 406 }
21a4dc43
TI
407 }
408 return 0;
409}
410
40c20fa0
TI
411static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
412{
40c20fa0
TI
413 unsigned int val;
414 val = snd_hda_codec_get_pincfg(codec, nid);
415 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
416}
417
21a4dc43
TI
418static int parse_output(struct hda_codec *codec)
419{
420 struct cs_spec *spec = codec->spec;
421 struct auto_pin_cfg *cfg = &spec->autocfg;
277a57c7 422 int i, extra_nids;
21a4dc43
TI
423 hda_nid_t dac;
424
425 for (i = 0; i < cfg->line_outs; i++) {
426 dac = get_dac(codec, cfg->line_out_pins[i]);
427 if (!dac)
428 break;
429 spec->dac_nid[i] = dac;
430 }
431 spec->multiout.num_dacs = i;
432 spec->multiout.dac_nids = spec->dac_nid;
433 spec->multiout.max_channels = i * 2;
434
435 /* add HP and speakers */
436 extra_nids = 0;
437 for (i = 0; i < cfg->hp_outs; i++) {
438 dac = get_dac(codec, cfg->hp_pins[i]);
439 if (!dac)
440 break;
441 if (!i)
442 spec->multiout.hp_nid = dac;
443 else
444 spec->multiout.extra_out_nid[extra_nids++] = dac;
445 }
446 for (i = 0; i < cfg->speaker_outs; i++) {
447 dac = get_dac(codec, cfg->speaker_pins[i]);
448 if (!dac)
449 break;
450 spec->multiout.extra_out_nid[extra_nids++] = dac;
451 }
452
453 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
454 cfg->speaker_outs = cfg->line_outs;
455 memcpy(cfg->speaker_pins, cfg->line_out_pins,
456 sizeof(cfg->speaker_pins));
457 cfg->line_outs = 0;
458 }
459
460 return 0;
461}
462
463static int parse_input(struct hda_codec *codec)
464{
465 struct cs_spec *spec = codec->spec;
466 struct auto_pin_cfg *cfg = &spec->autocfg;
277a57c7 467 int i;
21a4dc43 468
c1e0bb92
TI
469 for (i = 0; i < cfg->num_inputs; i++) {
470 hda_nid_t pin = cfg->inputs[i].pin;
21a4dc43
TI
471 spec->input_idx[spec->num_inputs] = i;
472 spec->capsrc_idx[i] = spec->num_inputs++;
473 spec->cur_input = i;
474 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
475 }
476 if (!spec->num_inputs)
477 return 0;
478
479 /* check whether the automatic mic switch is available */
480 if (spec->num_inputs == 2 &&
86e2959a
TI
481 cfg->inputs[0].type == AUTO_PIN_MIC &&
482 cfg->inputs[1].type == AUTO_PIN_MIC) {
c1e0bb92
TI
483 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
484 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
21a4dc43 485 spec->mic_detect = 1;
c1e0bb92 486 spec->automic_idx = 0;
21a4dc43
TI
487 }
488 } else {
c1e0bb92 489 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
21a4dc43 490 spec->mic_detect = 1;
c1e0bb92 491 spec->automic_idx = 1;
21a4dc43
TI
492 }
493 }
494 }
495 return 0;
496}
497
498
499static int parse_digital_output(struct hda_codec *codec)
500{
501 struct cs_spec *spec = codec->spec;
502 struct auto_pin_cfg *cfg = &spec->autocfg;
503 hda_nid_t nid;
21a4dc43
TI
504
505 if (!cfg->dig_outs)
506 return 0;
507 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
508 return 0;
509 spec->multiout.dig_out_nid = nid;
510 spec->multiout.share_spdif = 1;
511 if (cfg->dig_outs > 1 &&
512 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
513 spec->slave_dig_outs[0] = nid;
514 codec->slave_dig_outs = spec->slave_dig_outs;
515 }
516 return 0;
517}
518
519static int parse_digital_input(struct hda_codec *codec)
520{
521 struct cs_spec *spec = codec->spec;
522 struct auto_pin_cfg *cfg = &spec->autocfg;
523 int idx;
524
60e53882
TI
525 if (cfg->dig_in_pin)
526 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
527 return 0;
21a4dc43
TI
528}
529
530/*
531 * create mixer controls
532 */
533
ea734963 534static const char * const dir_sfx[2] = { "Playback", "Capture" };
e5f14248
TI
535
536static int add_mute(struct hda_codec *codec, const char *name, int index,
537 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
538{
b4dabfc4 539 char tmp[44];
e5f14248
TI
540 struct snd_kcontrol_new knew =
541 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
542 knew.private_value = pval;
543 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
544 *kctlp = snd_ctl_new1(&knew, codec);
9e3fd871 545 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
5e26dfd0 546 return snd_hda_ctl_add(codec, 0, *kctlp);
e5f14248
TI
547}
548
549static int add_volume(struct hda_codec *codec, const char *name,
550 int index, unsigned int pval, int dir,
551 struct snd_kcontrol **kctlp)
552{
2e1210bc 553 char tmp[44];
e5f14248
TI
554 struct snd_kcontrol_new knew =
555 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
556 knew.private_value = pval;
557 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
558 *kctlp = snd_ctl_new1(&knew, codec);
9e3fd871 559 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
5e26dfd0 560 return snd_hda_ctl_add(codec, 0, *kctlp);
e5f14248
TI
561}
562
563static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
564{
565 unsigned int caps;
566
567 /* set the upper-limit for mixer amp to 0dB */
568 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
569 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
570 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
571 << AC_AMPCAP_NUM_STEPS_SHIFT;
572 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
573}
574
575static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
576{
577 struct cs_spec *spec = codec->spec;
578 unsigned int tlv[4];
579 int err;
580
581 spec->vmaster_sw =
582 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
3911a4c1 583 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
e5f14248
TI
584 if (err < 0)
585 return err;
586
587 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
588 spec->vmaster_vol =
589 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
3911a4c1 590 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
e5f14248
TI
591 if (err < 0)
592 return err;
593 return 0;
594}
595
596static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
597 int num_ctls, int type)
598{
599 struct cs_spec *spec = codec->spec;
600 const char *name;
601 int err, index;
602 struct snd_kcontrol *kctl;
c42d4782 603 static const char * const speakers[] = {
e5f14248
TI
604 "Front Speaker", "Surround Speaker", "Bass Speaker"
605 };
c42d4782 606 static const char * const line_outs[] = {
e5f14248
TI
607 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
608 };
609
610 fix_volume_caps(codec, dac);
611 if (!spec->vmaster_sw) {
612 err = add_vmaster(codec, dac);
613 if (err < 0)
614 return err;
615 }
616
617 index = 0;
618 switch (type) {
619 case AUTO_PIN_HP_OUT:
620 name = "Headphone";
621 index = idx;
622 break;
623 case AUTO_PIN_SPEAKER_OUT:
624 if (num_ctls > 1)
625 name = speakers[idx];
626 else
627 name = "Speaker";
628 break;
629 default:
630 if (num_ctls > 1)
631 name = line_outs[idx];
632 else
633 name = "Line-Out";
634 break;
635 }
636
637 err = add_mute(codec, name, index,
638 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
639 if (err < 0)
640 return err;
641 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
642 if (err < 0)
643 return err;
644
645 err = add_volume(codec, name, index,
646 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
647 if (err < 0)
648 return err;
649 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
650 if (err < 0)
651 return err;
652
653 return 0;
654}
655
656static int build_output(struct hda_codec *codec)
657{
658 struct cs_spec *spec = codec->spec;
659 struct auto_pin_cfg *cfg = &spec->autocfg;
21a4dc43 660 int i, err;
e5f14248
TI
661
662 for (i = 0; i < cfg->line_outs; i++) {
21a4dc43
TI
663 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
664 i, cfg->line_outs, cfg->line_out_type);
e5f14248
TI
665 if (err < 0)
666 return err;
667 }
e5f14248 668 for (i = 0; i < cfg->hp_outs; i++) {
21a4dc43
TI
669 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
670 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
e5f14248
TI
671 if (err < 0)
672 return err;
673 }
674 for (i = 0; i < cfg->speaker_outs; i++) {
21a4dc43
TI
675 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
676 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
e5f14248
TI
677 if (err < 0)
678 return err;
679 }
e5f14248
TI
680 return 0;
681}
682
683/*
684 */
685
c42d4782 686static const struct snd_kcontrol_new cs_capture_ctls[] = {
e5f14248
TI
687 HDA_BIND_SW("Capture Switch", 0),
688 HDA_BIND_VOL("Capture Volume", 0),
689};
690
ea35929b
TI
691static int change_cur_input(struct hda_codec *codec, unsigned int idx,
692 int force)
e5f14248
TI
693{
694 struct cs_spec *spec = codec->spec;
e5f14248 695
ea35929b 696 if (spec->cur_input == idx && !force)
e5f14248
TI
697 return 0;
698 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
699 /* stream is running, let's swap the current ADC */
f0cea797 700 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
e5f14248
TI
701 spec->cur_adc = spec->adc_nid[idx];
702 snd_hda_codec_setup_stream(codec, spec->cur_adc,
703 spec->cur_adc_stream_tag, 0,
704 spec->cur_adc_format);
705 }
e5f14248 706 spec->cur_input = idx;
05ee7964 707 cs_update_input_select(codec);
e5f14248
TI
708 return 1;
709}
710
711static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
712 struct snd_ctl_elem_info *uinfo)
713{
714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
715 struct cs_spec *spec = codec->spec;
10a20af7 716 struct auto_pin_cfg *cfg = &spec->autocfg;
e5f14248
TI
717 unsigned int idx;
718
719 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
720 uinfo->count = 1;
721 uinfo->value.enumerated.items = spec->num_inputs;
722 if (uinfo->value.enumerated.item >= spec->num_inputs)
723 uinfo->value.enumerated.item = spec->num_inputs - 1;
724 idx = spec->input_idx[uinfo->value.enumerated.item];
201e06ff
TI
725 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
726 uinfo->value.enumerated.name,
727 sizeof(uinfo->value.enumerated.name), NULL);
e5f14248
TI
728 return 0;
729}
730
731static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
732 struct snd_ctl_elem_value *ucontrol)
733{
734 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
735 struct cs_spec *spec = codec->spec;
736 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
737 return 0;
738}
739
740static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
741 struct snd_ctl_elem_value *ucontrol)
742{
743 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
744 struct cs_spec *spec = codec->spec;
745 unsigned int idx = ucontrol->value.enumerated.item[0];
746
747 if (idx >= spec->num_inputs)
748 return -EINVAL;
749 idx = spec->input_idx[idx];
ea35929b 750 return change_cur_input(codec, idx, 0);
e5f14248
TI
751}
752
c42d4782 753static const struct snd_kcontrol_new cs_capture_source = {
e5f14248
TI
754 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
755 .name = "Capture Source",
756 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
757 .info = cs_capture_source_info,
758 .get = cs_capture_source_get,
759 .put = cs_capture_source_put,
760};
761
c42d4782 762static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
e5f14248
TI
763 struct hda_ctl_ops *ops)
764{
765 struct cs_spec *spec = codec->spec;
766 struct hda_bind_ctls *bind;
767 int i, n;
768
769 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
770 GFP_KERNEL);
771 if (!bind)
772 return NULL;
773 bind->ops = ops;
774 n = 0;
775 for (i = 0; i < AUTO_PIN_LAST; i++) {
776 if (!spec->adc_nid[i])
777 continue;
778 bind->values[n++] =
779 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
780 spec->adc_idx[i], HDA_INPUT);
781 }
782 return bind;
783}
784
6a92934d
TI
785/* add a (input-boost) volume control to the given input pin */
786static int add_input_volume_control(struct hda_codec *codec,
787 struct auto_pin_cfg *cfg,
788 int item)
789{
790 hda_nid_t pin = cfg->inputs[item].pin;
791 u32 caps;
792 const char *label;
793 struct snd_kcontrol *kctl;
794
795 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
796 return 0;
797 caps = query_amp_caps(codec, pin, HDA_INPUT);
798 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
799 if (caps <= 1)
800 return 0;
801 label = hda_get_autocfg_input_label(codec, cfg, item);
802 return add_volume(codec, label, 0,
803 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
804}
805
e5f14248
TI
806static int build_input(struct hda_codec *codec)
807{
808 struct cs_spec *spec = codec->spec;
21a4dc43 809 int i, err;
e5f14248 810
e5f14248
TI
811 if (!spec->num_inputs)
812 return 0;
813
e5f14248
TI
814 /* make bind-capture */
815 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
816 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
817 for (i = 0; i < 2; i++) {
818 struct snd_kcontrol *kctl;
21949f00 819 int n;
e5f14248
TI
820 if (!spec->capture_bind[i])
821 return -ENOMEM;
822 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
823 if (!kctl)
824 return -ENOMEM;
825 kctl->private_value = (long)spec->capture_bind[i];
3911a4c1 826 err = snd_hda_ctl_add(codec, 0, kctl);
e5f14248
TI
827 if (err < 0)
828 return err;
21949f00
TI
829 for (n = 0; n < AUTO_PIN_LAST; n++) {
830 if (!spec->adc_nid[n])
831 continue;
8dd34ab1 832 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
21949f00
TI
833 if (err < 0)
834 return err;
835 }
e5f14248
TI
836 }
837
838 if (spec->num_inputs > 1 && !spec->mic_detect) {
3911a4c1 839 err = snd_hda_ctl_add(codec, 0,
e5f14248
TI
840 snd_ctl_new1(&cs_capture_source, codec));
841 if (err < 0)
842 return err;
843 }
844
6a92934d
TI
845 for (i = 0; i < spec->num_inputs; i++) {
846 err = add_input_volume_control(codec, &spec->autocfg, i);
847 if (err < 0)
848 return err;
849 }
850
e5f14248
TI
851 return 0;
852}
853
21a4dc43
TI
854/*
855 */
856
e5f14248
TI
857static int build_digital_output(struct hda_codec *codec)
858{
859 struct cs_spec *spec = codec->spec;
e5f14248
TI
860 int err;
861
63b2413b
TI
862 if (!spec->multiout.dig_out_nid)
863 return 0;
864
74b654c9
SW
865 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
866 spec->multiout.dig_out_nid);
e5f14248
TI
867 if (err < 0)
868 return err;
869 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
870 if (err < 0)
871 return err;
e5f14248
TI
872 return 0;
873}
874
875static int build_digital_input(struct hda_codec *codec)
876{
877 struct cs_spec *spec = codec->spec;
21a4dc43
TI
878 if (spec->dig_in)
879 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
880 return 0;
e5f14248
TI
881}
882
21a4dc43
TI
883/*
884 * auto-mute and auto-mic switching
56487c27
TH
885 * CS421x auto-output redirecting
886 * HP/SPK/SPDIF
21a4dc43
TI
887 */
888
e5f14248
TI
889static void cs_automute(struct hda_codec *codec)
890{
891 struct cs_spec *spec = codec->spec;
892 struct auto_pin_cfg *cfg = &spec->autocfg;
06dec228 893 unsigned int hp_present;
56487c27 894 unsigned int spdif_present;
e5f14248
TI
895 hda_nid_t nid;
896 int i;
897
56487c27
TH
898 spdif_present = 0;
899 if (cfg->dig_outs) {
900 nid = cfg->dig_out_pins[0];
901 if (is_jack_detectable(codec, nid)) {
902 /*
903 TODO: SPDIF output redirect when SENSE_B is enabled.
904 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
905 assumed.
906 */
907 if (snd_hda_jack_detect(codec, nid)
908 /* && spec->sense_b */)
909 spdif_present = 1;
910 }
911 }
912
e5f14248
TI
913 hp_present = 0;
914 for (i = 0; i < cfg->hp_outs; i++) {
915 nid = cfg->hp_pins[i];
06dec228 916 if (!is_jack_detectable(codec, nid))
e5f14248 917 continue;
864f92be 918 hp_present = snd_hda_jack_detect(codec, nid);
e5f14248
TI
919 if (hp_present)
920 break;
921 }
56487c27
TH
922
923 /* mute speakers if spdif or hp jack is plugged in */
e5f14248
TI
924 for (i = 0; i < cfg->speaker_outs; i++) {
925 nid = cfg->speaker_pins[i];
926 snd_hda_codec_write(codec, nid, 0,
927 AC_VERB_SET_PIN_WIDGET_CONTROL,
928 hp_present ? 0 : PIN_OUT);
56487c27
TH
929 /* detect on spdif is specific to CS421x */
930 if (spec->vendor_nid == CS421X_VENDOR_NID) {
931 snd_hda_codec_write(codec, nid, 0,
932 AC_VERB_SET_PIN_WIDGET_CONTROL,
933 spdif_present ? 0 : PIN_OUT);
934 }
e5f14248 935 }
6dfeb703
TI
936 if (spec->gpio_eapd_hp) {
937 unsigned int gpio = hp_present ?
938 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
3a385167
SP
939 snd_hda_codec_write(codec, 0x01, 0,
940 AC_VERB_SET_GPIO_DATA, gpio);
941 }
56487c27
TH
942
943 /* specific to CS421x */
944 if (spec->vendor_nid == CS421X_VENDOR_NID) {
945 /* mute HPs if spdif jack (SENSE_B) is present */
946 for (i = 0; i < cfg->hp_outs; i++) {
947 nid = cfg->hp_pins[i];
948 snd_hda_codec_write(codec, nid, 0,
949 AC_VERB_SET_PIN_WIDGET_CONTROL,
950 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
951 }
952
953 /* SPDIF TX on/off */
954 if (cfg->dig_outs) {
955 nid = cfg->dig_out_pins[0];
956 snd_hda_codec_write(codec, nid, 0,
957 AC_VERB_SET_PIN_WIDGET_CONTROL,
958 spdif_present ? PIN_OUT : 0);
959
960 }
961 /* Update board GPIOs if neccessary ... */
962 }
e5f14248
TI
963}
964
56487c27
TH
965/*
966 * Auto-input redirect for CS421x
967 * Switch max 3 inputs of a single ADC (nid 3)
968*/
969
e5f14248
TI
970static void cs_automic(struct hda_codec *codec)
971{
972 struct cs_spec *spec = codec->spec;
973 struct auto_pin_cfg *cfg = &spec->autocfg;
974 hda_nid_t nid;
864f92be 975 unsigned int present;
56487c27 976
c1e0bb92 977 nid = cfg->inputs[spec->automic_idx].pin;
864f92be 978 present = snd_hda_jack_detect(codec, nid);
56487c27
TH
979
980 /* specific to CS421x, single ADC */
981 if (spec->vendor_nid == CS421X_VENDOR_NID) {
982 if (present) {
983 spec->last_input = spec->cur_input;
984 spec->cur_input = spec->automic_idx;
985 } else {
986 spec->cur_input = spec->last_input;
987 }
05ee7964 988 cs_update_input_select(codec);
56487c27
TH
989 } else {
990 if (present)
991 change_cur_input(codec, spec->automic_idx, 0);
992 else
993 change_cur_input(codec, !spec->automic_idx, 0);
994 }
e5f14248
TI
995}
996
997/*
998 */
999
1000static void init_output(struct hda_codec *codec)
1001{
1002 struct cs_spec *spec = codec->spec;
1003 struct auto_pin_cfg *cfg = &spec->autocfg;
1004 int i;
1005
1006 /* mute first */
1007 for (i = 0; i < spec->multiout.num_dacs; i++)
1008 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1009 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1010 if (spec->multiout.hp_nid)
1011 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1012 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1013 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1014 if (!spec->multiout.extra_out_nid[i])
1015 break;
1016 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1017 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1018 }
1019
1020 /* set appropriate pin controls */
1021 for (i = 0; i < cfg->line_outs; i++)
1022 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1023 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
56487c27 1024 /* HP */
e5f14248
TI
1025 for (i = 0; i < cfg->hp_outs; i++) {
1026 hda_nid_t nid = cfg->hp_pins[i];
1027 snd_hda_codec_write(codec, nid, 0,
1028 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1029 if (!cfg->speaker_outs)
1030 continue;
56487c27 1031 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1835a0f9 1032 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
e5f14248
TI
1033 spec->hp_detect = 1;
1034 }
1035 }
56487c27
TH
1036
1037 /* Speaker */
e5f14248
TI
1038 for (i = 0; i < cfg->speaker_outs; i++)
1039 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1040 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
56487c27
TH
1041
1042 /* SPDIF is enabled on presence detect for CS421x */
1043 if (spec->hp_detect || spec->spdif_detect)
e5f14248
TI
1044 cs_automute(codec);
1045}
1046
1047static void init_input(struct hda_codec *codec)
1048{
1049 struct cs_spec *spec = codec->spec;
1050 struct auto_pin_cfg *cfg = &spec->autocfg;
40c20fa0 1051 unsigned int coef;
e5f14248
TI
1052 int i;
1053
c1e0bb92 1054 for (i = 0; i < cfg->num_inputs; i++) {
e5f14248 1055 unsigned int ctl;
c1e0bb92
TI
1056 hda_nid_t pin = cfg->inputs[i].pin;
1057 if (!spec->adc_nid[i])
e5f14248
TI
1058 continue;
1059 /* set appropriate pin control and mute first */
1060 ctl = PIN_IN;
86e2959a 1061 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
e5f14248
TI
1062 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1063 caps >>= AC_PINCAP_VREF_SHIFT;
1064 if (caps & AC_PINCAP_VREF_80)
1065 ctl = PIN_VREF80;
1066 }
1067 snd_hda_codec_write(codec, pin, 0,
1068 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1069 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1070 AC_VERB_SET_AMP_GAIN_MUTE,
1071 AMP_IN_MUTE(spec->adc_idx[i]));
1072 if (spec->mic_detect && spec->automic_idx == i)
1835a0f9 1073 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
e5f14248 1074 }
56487c27
TH
1075 /* specific to CS421x */
1076 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1077 if (spec->mic_detect)
1078 cs_automic(codec);
1079 else {
1080 spec->cur_adc = spec->adc_nid[spec->cur_input];
05ee7964 1081 cs_update_input_select(codec);
56487c27
TH
1082 }
1083 } else {
1084 change_cur_input(codec, spec->cur_input, 1);
1085 if (spec->mic_detect)
1086 cs_automic(codec);
1087
1088 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1089 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1090 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1091 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1092 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1093 * No effect if SPDIF_OUT2 is
1094 * selected in IDX_SPDIF_CTL.
1095 */
1096 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1097 }
40c20fa0
TI
1098}
1099
c42d4782 1100static const struct hda_verb cs_coef_init_verbs[] = {
40c20fa0
TI
1101 {0x11, AC_VERB_SET_PROC_STATE, 1},
1102 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1103 {0x11, AC_VERB_SET_PROC_COEF,
1104 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1105 | 0x0040 /* Mute DACs on FIFO error */
1106 | 0x1000 /* Enable DACs High Pass Filter */
1107 | 0x0400 /* Disable Coefficient Auto increment */
1108 )},
1109 /* Beep */
1110 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1111 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1112
1113 {} /* terminator */
1114};
1115
a769cbcf
BA
1116/* Errata: CS4207 rev C0/C1/C2 Silicon
1117 *
1118 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1119 *
1120 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1121 * may be excessive (up to an additional 200 μA), which is most easily
1122 * observed while the part is being held in reset (RESET# active low).
1123 *
1124 * Root Cause: At initial powerup of the device, the logic that drives
1125 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1126 * initialized.
1127 * Certain random patterns will cause a steady leakage current in those
1128 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1129 *
1130 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1131 * blocks, which will alleviate the issue.
1132 */
1133
c42d4782 1134static const struct hda_verb cs_errata_init_verbs[] = {
a769cbcf
BA
1135 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1136 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1137
1138 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1139 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1140 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1141 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1142 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1143 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1144
1145 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1146 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1147
1148 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1149 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1150 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1151 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1152 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1153 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1154 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1155
38c07641 1156#if 0 /* Don't to set to D3 as we are in power-up sequence */
a769cbcf
BA
1157 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1158 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1159 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
38c07641 1160#endif
a769cbcf
BA
1161
1162 {} /* terminator */
1163};
1164
40c20fa0
TI
1165/* SPDIF setup */
1166static void init_digital(struct hda_codec *codec)
1167{
1168 unsigned int coef;
1169
1170 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1171 coef |= 0x0008; /* Replace with mute on error */
1172 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1173 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1174 * SPDIF_OUT2 is shared with GPIO1 and
1175 * DMIC_SDA2.
1176 */
1177 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
e5f14248
TI
1178}
1179
1180static int cs_init(struct hda_codec *codec)
1181{
1182 struct cs_spec *spec = codec->spec;
1183
a769cbcf
BA
1184 /* init_verb sequence for C0/C1/C2 errata*/
1185 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1186
40c20fa0 1187 snd_hda_sequence_write(codec, cs_coef_init_verbs);
ed208255
TI
1188
1189 if (spec->gpio_mask) {
1190 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1191 spec->gpio_mask);
1192 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1193 spec->gpio_dir);
1194 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1195 spec->gpio_data);
1196 }
1197
e5f14248
TI
1198 init_output(codec);
1199 init_input(codec);
40c20fa0 1200 init_digital(codec);
01a61e12
TI
1201 snd_hda_jack_report_sync(codec);
1202
e5f14248
TI
1203 return 0;
1204}
1205
1206static int cs_build_controls(struct hda_codec *codec)
1207{
01a61e12 1208 struct cs_spec *spec = codec->spec;
e5f14248
TI
1209 int err;
1210
1211 err = build_output(codec);
1212 if (err < 0)
1213 return err;
1214 err = build_input(codec);
1215 if (err < 0)
1216 return err;
1217 err = build_digital_output(codec);
1218 if (err < 0)
1219 return err;
1220 err = build_digital_input(codec);
1221 if (err < 0)
1222 return err;
01a61e12
TI
1223 err = cs_init(codec);
1224 if (err < 0)
1225 return err;
1226
1227 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1228 if (err < 0)
1229 return err;
1230
1231 return 0;
e5f14248
TI
1232}
1233
1234static void cs_free(struct hda_codec *codec)
1235{
1236 struct cs_spec *spec = codec->spec;
1237 kfree(spec->capture_bind[0]);
1238 kfree(spec->capture_bind[1]);
1239 kfree(codec->spec);
1240}
1241
1242static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1243{
3a93897e 1244 switch (snd_hda_jack_get_action(codec, res >> 26)) {
e5f14248
TI
1245 case HP_EVENT:
1246 cs_automute(codec);
1247 break;
1248 case MIC_EVENT:
1249 cs_automic(codec);
1250 break;
1251 }
01a61e12 1252 snd_hda_jack_report_sync(codec);
e5f14248
TI
1253}
1254
c42d4782 1255static const struct hda_codec_ops cs_patch_ops = {
e5f14248
TI
1256 .build_controls = cs_build_controls,
1257 .build_pcms = cs_build_pcms,
1258 .init = cs_init,
1259 .free = cs_free,
1260 .unsol_event = cs_unsol_event,
1261};
1262
1263static int cs_parse_auto_config(struct hda_codec *codec)
1264{
1265 struct cs_spec *spec = codec->spec;
1266 int err;
1267
1268 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
ed208255
TI
1269 if (err < 0)
1270 return err;
1271
1272 err = parse_output(codec);
1273 if (err < 0)
1274 return err;
1275 err = parse_input(codec);
1276 if (err < 0)
1277 return err;
1278 err = parse_digital_output(codec);
1279 if (err < 0)
1280 return err;
1281 err = parse_digital_input(codec);
e5f14248
TI
1282 if (err < 0)
1283 return err;
1284 return 0;
1285}
1286
ea734963 1287static const char * const cs420x_models[CS420X_MODELS] = {
4e7d7c60 1288 [CS420X_MBP53] = "mbp53",
a6bae205 1289 [CS420X_MBP55] = "mbp55",
1a5ba2e9 1290 [CS420X_IMAC27] = "imac27",
92bb43e6 1291 [CS420X_APPLE] = "apple",
a6bae205
TI
1292 [CS420X_AUTO] = "auto",
1293};
1294
1295
c42d4782 1296static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
4e7d7c60 1297 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
87232dd4 1298 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
a6bae205 1299 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
f46119b7 1300 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
6dfeb703
TI
1301 /* this conflicts with too many other models */
1302 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1303 {} /* terminator */
1304};
1305
1306static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
1307 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
a6bae205
TI
1308 {} /* terminator */
1309};
1310
1311struct cs_pincfg {
1312 hda_nid_t nid;
1313 u32 val;
1314};
1315
c42d4782 1316static const struct cs_pincfg mbp53_pincfgs[] = {
4e7d7c60
VW
1317 { 0x09, 0x012b4050 },
1318 { 0x0a, 0x90100141 },
1319 { 0x0b, 0x90100140 },
1320 { 0x0c, 0x018b3020 },
1321 { 0x0d, 0x90a00110 },
1322 { 0x0e, 0x400000f0 },
1323 { 0x0f, 0x01cbe030 },
1324 { 0x10, 0x014be060 },
1325 { 0x12, 0x400000f0 },
1326 { 0x15, 0x400000f0 },
1327 {} /* terminator */
1328};
1329
c42d4782 1330static const struct cs_pincfg mbp55_pincfgs[] = {
a6bae205
TI
1331 { 0x09, 0x012b4030 },
1332 { 0x0a, 0x90100121 },
1333 { 0x0b, 0x90100120 },
1334 { 0x0c, 0x400000f0 },
1335 { 0x0d, 0x90a00110 },
1336 { 0x0e, 0x400000f0 },
1337 { 0x0f, 0x400000f0 },
1338 { 0x10, 0x014be040 },
1339 { 0x12, 0x400000f0 },
1340 { 0x15, 0x400000f0 },
1341 {} /* terminator */
1342};
1343
c42d4782 1344static const struct cs_pincfg imac27_pincfgs[] = {
1a5ba2e9
RAE
1345 { 0x09, 0x012b4050 },
1346 { 0x0a, 0x90100140 },
1347 { 0x0b, 0x90100142 },
1348 { 0x0c, 0x018b3020 },
1349 { 0x0d, 0x90a00110 },
1350 { 0x0e, 0x400000f0 },
1351 { 0x0f, 0x01cbe030 },
1352 { 0x10, 0x014be060 },
1353 { 0x12, 0x01ab9070 },
1354 { 0x15, 0x400000f0 },
1355 {} /* terminator */
1356};
1357
c42d4782 1358static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
4e7d7c60 1359 [CS420X_MBP53] = mbp53_pincfgs,
a6bae205 1360 [CS420X_MBP55] = mbp55_pincfgs,
1a5ba2e9 1361 [CS420X_IMAC27] = imac27_pincfgs,
a6bae205
TI
1362};
1363
56487c27
TH
1364static void fix_pincfg(struct hda_codec *codec, int model,
1365 const struct cs_pincfg **pin_configs)
a6bae205 1366{
56487c27 1367 const struct cs_pincfg *cfg = pin_configs[model];
a6bae205
TI
1368 if (!cfg)
1369 return;
1370 for (; cfg->nid; cfg++)
1371 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1372}
1373
e5f14248
TI
1374static int patch_cs420x(struct hda_codec *codec)
1375{
1376 struct cs_spec *spec;
1377 int err;
1378
1379 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1380 if (!spec)
1381 return -ENOMEM;
1382 codec->spec = spec;
1383
56487c27
TH
1384 spec->vendor_nid = CS420X_VENDOR_NID;
1385
a6bae205
TI
1386 spec->board_config =
1387 snd_hda_check_board_config(codec, CS420X_MODELS,
1388 cs420x_models, cs420x_cfg_tbl);
6dfeb703
TI
1389 if (spec->board_config < 0)
1390 spec->board_config =
1391 snd_hda_check_board_codec_sid_config(codec,
1392 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
a6bae205 1393 if (spec->board_config >= 0)
56487c27 1394 fix_pincfg(codec, spec->board_config, cs_pincfgs);
a6bae205 1395
ed208255 1396 switch (spec->board_config) {
1a5ba2e9 1397 case CS420X_IMAC27:
4e7d7c60 1398 case CS420X_MBP53:
ed208255 1399 case CS420X_MBP55:
6dfeb703
TI
1400 case CS420X_APPLE:
1401 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1402 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1403 spec->gpio_mask = spec->gpio_dir =
1404 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
ed208255
TI
1405 break;
1406 }
e5f14248 1407
ed208255 1408 err = cs_parse_auto_config(codec);
21a4dc43
TI
1409 if (err < 0)
1410 goto error;
1411
e5f14248
TI
1412 codec->patch_ops = cs_patch_ops;
1413
1414 return 0;
1415
1416 error:
1417 kfree(codec->spec);
1418 codec->spec = NULL;
1419 return err;
1420}
1421
56487c27
TH
1422/*
1423 * Cirrus Logic CS4210
1424 *
1425 * 1 DAC => HP(sense) / Speakers,
1426 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1427 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1428*/
1429
1430/* CS4210 board names */
1431static const char *cs421x_models[CS421X_MODELS] = {
1432 [CS421X_CDB4210] = "cdb4210",
1433};
1434
1435static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1436 /* Test Intel board + CDB2410 */
1437 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1438 {} /* terminator */
1439};
1440
1441/* CS4210 board pinconfigs */
1442/* Default CS4210 (CDB4210)*/
1443static const struct cs_pincfg cdb4210_pincfgs[] = {
1444 { 0x05, 0x0321401f },
1445 { 0x06, 0x90170010 },
1446 { 0x07, 0x03813031 },
1447 { 0x08, 0xb7a70037 },
1448 { 0x09, 0xb7a6003e },
1449 { 0x0a, 0x034510f0 },
1450 {} /* terminator */
1451};
1452
1453static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1454 [CS421X_CDB4210] = cdb4210_pincfgs,
1455};
1456
1457static const struct hda_verb cs421x_coef_init_verbs[] = {
1458 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1459 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1460 /*
1461 Disable Coefficient Index Auto-Increment(DAI)=1,
1462 PDREF=0
1463 */
1464 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1465
1466 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1467 /* ADC SZCMode = Digital Soft Ramp */
1468 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1469
1470 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1471 {0x0B, AC_VERB_SET_PROC_COEF,
1472 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1473 | 0x0004 /* Mute DAC on FIFO error */
1474 | 0x0008 /* Enable DAC High Pass Filter */
1475 )},
1476 {} /* terminator */
1477};
1478
1479/* Errata: CS4210 rev A1 Silicon
1480 *
1481 * http://www.cirrus.com/en/pubs/errata/
1482 *
1483 * Description:
1484 * 1. Performance degredation is present in the ADC.
1485 * 2. Speaker output is not completely muted upon HP detect.
1486 * 3. Noise is present when clipping occurs on the amplified
1487 * speaker outputs.
1488 *
1489 * Workaround:
1490 * The following verb sequence written to the registers during
1491 * initialization will correct the issues listed above.
1492 */
1493
1494static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1495 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1496
1497 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1498 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1499
1500 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1501 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1502
1503 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1504 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1505
1506 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1507 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1508
1509 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1510 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1511
1512 {} /* terminator */
1513};
1514
1515/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1516static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1517
1518static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1519 struct snd_ctl_elem_info *uinfo)
1520{
1521 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1522 uinfo->count = 1;
1523 uinfo->value.integer.min = 0;
1524 uinfo->value.integer.max = 3;
1525 return 0;
1526}
1527
1528static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1529 struct snd_ctl_elem_value *ucontrol)
1530{
1531 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1532
1533 ucontrol->value.integer.value[0] =
1534 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1535 return 0;
1536}
1537
1538static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1539 struct snd_ctl_elem_value *ucontrol)
1540{
1541 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1542
1543 unsigned int vol = ucontrol->value.integer.value[0];
1544 unsigned int coef =
1545 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1546 unsigned int original_coef = coef;
1547
1548 coef &= ~0x0003;
1549 coef |= (vol & 0x0003);
1550 if (original_coef == coef)
1551 return 0;
1552 else {
1553 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1554 return 1;
1555 }
1556}
1557
1558static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1559
1560 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1561 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1562 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1563 .name = "Speaker Boost Playback Volume",
1564 .info = cs421x_boost_vol_info,
1565 .get = cs421x_boost_vol_get,
1566 .put = cs421x_boost_vol_put,
1567 .tlv = { .p = cs421x_speaker_boost_db_scale },
1568};
1569
1570static void cs421x_pinmux_init(struct hda_codec *codec)
1571{
1572 struct cs_spec *spec = codec->spec;
1573 unsigned int def_conf, coef;
1574
1575 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1576 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1577
1578 if (spec->gpio_mask)
1579 coef |= 0x0008; /* B1,B2 are GPIOs */
1580 else
1581 coef &= ~0x0008;
1582
1583 if (spec->sense_b)
1584 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1585 else
1586 coef &= ~0x0010;
1587
1588 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1589
1590 if ((spec->gpio_mask || spec->sense_b) &&
1591 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1592
1593 /*
1594 GPIO or SENSE_B forced - disconnect the DMIC pin.
1595 */
1596 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1597 def_conf &= ~AC_DEFCFG_PORT_CONN;
1598 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1599 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1600 }
1601}
1602
1603static void init_cs421x_digital(struct hda_codec *codec)
1604{
1605 struct cs_spec *spec = codec->spec;
1606 struct auto_pin_cfg *cfg = &spec->autocfg;
1607 int i;
1608
1609
1610 for (i = 0; i < cfg->dig_outs; i++) {
1611 hda_nid_t nid = cfg->dig_out_pins[i];
1612 if (!cfg->speaker_outs)
1613 continue;
1614 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1835a0f9 1615 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
56487c27
TH
1616 spec->spdif_detect = 1;
1617 }
1618 }
1619}
1620
1621static int cs421x_init(struct hda_codec *codec)
1622{
1623 struct cs_spec *spec = codec->spec;
1624
1625 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1626 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1627
1628 cs421x_pinmux_init(codec);
1629
1630 if (spec->gpio_mask) {
1631 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1632 spec->gpio_mask);
1633 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1634 spec->gpio_dir);
1635 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1636 spec->gpio_data);
1637 }
1638
1639 init_output(codec);
1640 init_input(codec);
1641 init_cs421x_digital(codec);
01a61e12 1642 snd_hda_jack_report_sync(codec);
56487c27
TH
1643
1644 return 0;
1645}
1646
1647/*
1648 * CS4210 Input MUX (1 ADC)
1649 */
1650static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1651 struct snd_ctl_elem_info *uinfo)
1652{
1653 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1654 struct cs_spec *spec = codec->spec;
1655
1656 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1657}
1658
1659static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1660 struct snd_ctl_elem_value *ucontrol)
1661{
1662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1663 struct cs_spec *spec = codec->spec;
1664
1665 ucontrol->value.enumerated.item[0] = spec->cur_input;
1666 return 0;
1667}
1668
1669static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1670 struct snd_ctl_elem_value *ucontrol)
1671{
1672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1673 struct cs_spec *spec = codec->spec;
1674
1675 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1676 spec->adc_nid[0], &spec->cur_input);
1677
1678}
1679
1680static struct snd_kcontrol_new cs421x_capture_source = {
1681
1682 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1683 .name = "Capture Source",
1684 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1685 .info = cs421x_mux_enum_info,
1686 .get = cs421x_mux_enum_get,
1687 .put = cs421x_mux_enum_put,
1688};
1689
1690static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1691{
1692 struct cs_spec *spec = codec->spec;
1693 struct auto_pin_cfg *cfg = &spec->autocfg;
1694 const struct hda_input_mux *imux = &spec->input_mux;
1695 hda_nid_t pin = cfg->inputs[item].pin;
1696 struct snd_kcontrol *kctl;
1697 u32 caps;
1698
1699 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1700 return 0;
1701
1702 caps = query_amp_caps(codec, pin, HDA_INPUT);
1703 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1704 if (caps <= 1)
1705 return 0;
1706
1707 return add_volume(codec, imux->items[item].label, 0,
1708 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1709}
1710
1711/* add a (input-boost) volume control to the given input pin */
1712static int build_cs421x_input(struct hda_codec *codec)
1713{
1714 struct cs_spec *spec = codec->spec;
1715 struct auto_pin_cfg *cfg = &spec->autocfg;
1716 struct hda_input_mux *imux = &spec->input_mux;
1717 int i, err, type_idx;
1718 const char *label;
1719
1720 if (!spec->num_inputs)
1721 return 0;
1722
1723 /* make bind-capture */
1724 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1725 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1726 for (i = 0; i < 2; i++) {
1727 struct snd_kcontrol *kctl;
1728 int n;
1729 if (!spec->capture_bind[i])
1730 return -ENOMEM;
1731 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1732 if (!kctl)
1733 return -ENOMEM;
1734 kctl->private_value = (long)spec->capture_bind[i];
1735 err = snd_hda_ctl_add(codec, 0, kctl);
1736 if (err < 0)
1737 return err;
1738 for (n = 0; n < AUTO_PIN_LAST; n++) {
1739 if (!spec->adc_nid[n])
1740 continue;
1741 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1742 if (err < 0)
1743 return err;
1744 }
1745 }
1746
1747 /* Add Input MUX Items + Capture Volume/Switch */
1748 for (i = 0; i < spec->num_inputs; i++) {
1749 label = hda_get_autocfg_input_label(codec, cfg, i);
1750 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1751
1752 err = cs421x_add_input_volume_control(codec, i);
1753 if (err < 0)
1754 return err;
1755 }
1756
1757 /*
1758 Add 'Capture Source' Switch if
1759 * 2 inputs and no mic detec
1760 * 3 inputs
1761 */
1762 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1763 (spec->num_inputs == 3)) {
1764
1765 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1766 snd_ctl_new1(&cs421x_capture_source, codec));
1767 if (err < 0)
1768 return err;
1769 }
1770
1771 return 0;
1772}
1773
1774/* Single DAC (Mute/Gain) */
1775static int build_cs421x_output(struct hda_codec *codec)
1776{
1777 hda_nid_t dac = CS4210_DAC_NID;
1778 struct cs_spec *spec = codec->spec;
1779 struct auto_pin_cfg *cfg = &spec->autocfg;
1780 struct snd_kcontrol *kctl;
1781 int err;
40d03e63 1782 char *name = "Master";
56487c27
TH
1783
1784 fix_volume_caps(codec, dac);
56487c27
TH
1785
1786 err = add_mute(codec, name, 0,
1787 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1788 if (err < 0)
1789 return err;
56487c27
TH
1790
1791 err = add_volume(codec, name, 0,
1792 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1793 if (err < 0)
1794 return err;
56487c27
TH
1795
1796 if (cfg->speaker_outs) {
1797 err = snd_hda_ctl_add(codec, 0,
1798 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1799 if (err < 0)
1800 return err;
1801 }
1802 return err;
1803}
1804
1805static int cs421x_build_controls(struct hda_codec *codec)
1806{
01a61e12 1807 struct cs_spec *spec = codec->spec;
56487c27
TH
1808 int err;
1809
1810 err = build_cs421x_output(codec);
1811 if (err < 0)
1812 return err;
1813 err = build_cs421x_input(codec);
1814 if (err < 0)
1815 return err;
1816 err = build_digital_output(codec);
1817 if (err < 0)
1818 return err;
01a61e12
TI
1819 err = cs421x_init(codec);
1820 if (err < 0)
1821 return err;
1822
1823 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1824 if (err < 0)
1825 return err;
1826
1827 return 0;
56487c27
TH
1828}
1829
1830static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1831{
3a93897e 1832 switch (snd_hda_jack_get_action(codec, res >> 26)) {
56487c27
TH
1833 case HP_EVENT:
1834 case SPDIF_EVENT:
1835 cs_automute(codec);
1836 break;
1837
1838 case MIC_EVENT:
1839 cs_automic(codec);
1840 break;
1841 }
01a61e12 1842 snd_hda_jack_report_sync(codec);
56487c27
TH
1843}
1844
1845static int parse_cs421x_input(struct hda_codec *codec)
1846{
1847 struct cs_spec *spec = codec->spec;
1848 struct auto_pin_cfg *cfg = &spec->autocfg;
1849 int i;
1850
1851 for (i = 0; i < cfg->num_inputs; i++) {
1852 hda_nid_t pin = cfg->inputs[i].pin;
1853 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1854 spec->cur_input = spec->last_input = i;
1855 spec->num_inputs++;
1856
1857 /* check whether the automatic mic switch is available */
1858 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1859 spec->mic_detect = 1;
1860 spec->automic_idx = i;
1861 }
1862 }
1863 return 0;
1864}
1865
1866static int cs421x_parse_auto_config(struct hda_codec *codec)
1867{
1868 struct cs_spec *spec = codec->spec;
1869 int err;
1870
1871 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1872 if (err < 0)
1873 return err;
1874 err = parse_output(codec);
1875 if (err < 0)
1876 return err;
1877 err = parse_cs421x_input(codec);
1878 if (err < 0)
1879 return err;
1880 err = parse_digital_output(codec);
1881 if (err < 0)
1882 return err;
1883 return 0;
1884}
1885
1886#ifdef CONFIG_PM
1887/*
1888 Manage PDREF, when transitioning to D3hot
1889 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1890*/
1891static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1892{
1893 unsigned int coef;
1894
1895 snd_hda_shutup_pins(codec);
1896
1897 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1898 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1899 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1900 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1901
1902 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1903 coef |= 0x0004; /* PDREF */
1904 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1905
1906 return 0;
1907}
1908#endif
1909
1910static struct hda_codec_ops cs4210_patch_ops = {
1911 .build_controls = cs421x_build_controls,
1912 .build_pcms = cs_build_pcms,
1913 .init = cs421x_init,
1914 .free = cs_free,
1915 .unsol_event = cs421x_unsol_event,
1916#ifdef CONFIG_PM
1917 .suspend = cs421x_suspend,
1918#endif
1919};
1920
1921static int patch_cs421x(struct hda_codec *codec)
1922{
1923 struct cs_spec *spec;
1924 int err;
1925
1926 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1927 if (!spec)
1928 return -ENOMEM;
1929 codec->spec = spec;
1930
1931 spec->vendor_nid = CS421X_VENDOR_NID;
1932
1933 spec->board_config =
1934 snd_hda_check_board_config(codec, CS421X_MODELS,
1935 cs421x_models, cs421x_cfg_tbl);
1936 if (spec->board_config >= 0)
1937 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1938 /*
1939 Setup GPIO/SENSE for each board (if used)
1940 */
1941 switch (spec->board_config) {
1942 case CS421X_CDB4210:
1943 snd_printd("CS4210 board: %s\n",
1944 cs421x_models[spec->board_config]);
1945/* spec->gpio_mask = 3;
1946 spec->gpio_dir = 3;
1947 spec->gpio_data = 3;
1948*/
1949 spec->sense_b = 1;
1950
1951 break;
1952 }
1953
1954 /*
1955 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1956 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1957 is disabled.
1958 */
1959 cs421x_pinmux_init(codec);
1960
1961 err = cs421x_parse_auto_config(codec);
1962 if (err < 0)
1963 goto error;
1964
1965 codec->patch_ops = cs4210_patch_ops;
1966
1967 return 0;
1968
1969 error:
1970 kfree(codec->spec);
1971 codec->spec = NULL;
1972 return err;
1973}
1974
e5f14248
TI
1975
1976/*
1977 * patch entries
1978 */
c42d4782 1979static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
e5f14248
TI
1980 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1981 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
56487c27 1982 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
e5f14248
TI
1983 {} /* terminator */
1984};
1985
1986MODULE_ALIAS("snd-hda-codec-id:10134206");
1987MODULE_ALIAS("snd-hda-codec-id:10134207");
56487c27 1988MODULE_ALIAS("snd-hda-codec-id:10134210");
e5f14248
TI
1989
1990MODULE_LICENSE("GPL");
1991MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1992
1993static struct hda_codec_preset_list cirrus_list = {
1994 .preset = snd_hda_preset_cirrus,
1995 .owner = THIS_MODULE,
1996};
1997
1998static int __init patch_cirrus_init(void)
1999{
2000 return snd_hda_add_codec_preset(&cirrus_list);
2001}
2002
2003static void __exit patch_cirrus_exit(void)
2004{
2005 snd_hda_delete_codec_preset(&cirrus_list);
2006}
2007
2008module_init(patch_cirrus_init)
2009module_exit(patch_cirrus_exit)
This page took 0.231398 seconds and 5 git commands to generate.