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