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