xref: /linux/sound/pci/hda/patch_cirrus.c (revision c0c914eca7f251c70facc37dfebeaf176601918d)
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/slab.h>
23 #include <linux/module.h>
24 #include <sound/core.h>
25 #include <sound/tlv.h>
26 #include "hda_codec.h"
27 #include "hda_local.h"
28 #include "hda_auto_parser.h"
29 #include "hda_jack.h"
30 #include "hda_generic.h"
31 
32 /*
33  */
34 
35 struct cs_spec {
36 	struct hda_gen_spec gen;
37 
38 	unsigned int gpio_mask;
39 	unsigned int gpio_dir;
40 	unsigned int gpio_data;
41 	unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
42 	unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
43 
44 	/* CS421x */
45 	unsigned int spdif_detect:1;
46 	unsigned int spdif_present:1;
47 	unsigned int sense_b:1;
48 	hda_nid_t vendor_nid;
49 
50 	/* for MBP SPDIF control */
51 	int (*spdif_sw_put)(struct snd_kcontrol *kcontrol,
52 			    struct snd_ctl_elem_value *ucontrol);
53 };
54 
55 /* available models with CS420x */
56 enum {
57 	CS420X_MBP53,
58 	CS420X_MBP55,
59 	CS420X_IMAC27,
60 	CS420X_GPIO_13,
61 	CS420X_GPIO_23,
62 	CS420X_MBP101,
63 	CS420X_MBP81,
64 	CS420X_MBA42,
65 	CS420X_AUTO,
66 	/* aliases */
67 	CS420X_IMAC27_122 = CS420X_GPIO_23,
68 	CS420X_APPLE = CS420X_GPIO_13,
69 };
70 
71 /* CS421x boards */
72 enum {
73 	CS421X_CDB4210,
74 	CS421X_SENSE_B,
75 	CS421X_STUMPY,
76 };
77 
78 /* Vendor-specific processing widget */
79 #define CS420X_VENDOR_NID	0x11
80 #define CS_DIG_OUT1_PIN_NID	0x10
81 #define CS_DIG_OUT2_PIN_NID	0x15
82 #define CS_DMIC1_PIN_NID	0x0e
83 #define CS_DMIC2_PIN_NID	0x12
84 
85 /* coef indices */
86 #define IDX_SPDIF_STAT		0x0000
87 #define IDX_SPDIF_CTL		0x0001
88 #define IDX_ADC_CFG		0x0002
89 /* SZC bitmask, 4 modes below:
90  * 0 = immediate,
91  * 1 = digital immediate, analog zero-cross
92  * 2 = digtail & analog soft-ramp
93  * 3 = digital soft-ramp, analog zero-cross
94  */
95 #define   CS_COEF_ADC_SZC_MASK		(3 << 0)
96 #define   CS_COEF_ADC_MIC_SZC_MODE	(3 << 0) /* SZC setup for mic */
97 #define   CS_COEF_ADC_LI_SZC_MODE	(3 << 0) /* SZC setup for line-in */
98 /* PGA mode: 0 = differential, 1 = signle-ended */
99 #define   CS_COEF_ADC_MIC_PGA_MODE	(1 << 5) /* PGA setup for mic */
100 #define   CS_COEF_ADC_LI_PGA_MODE	(1 << 6) /* PGA setup for line-in */
101 #define IDX_DAC_CFG		0x0003
102 /* SZC bitmask, 4 modes below:
103  * 0 = Immediate
104  * 1 = zero-cross
105  * 2 = soft-ramp
106  * 3 = soft-ramp on zero-cross
107  */
108 #define   CS_COEF_DAC_HP_SZC_MODE	(3 << 0) /* nid 0x02 */
109 #define   CS_COEF_DAC_LO_SZC_MODE	(3 << 2) /* nid 0x03 */
110 #define   CS_COEF_DAC_SPK_SZC_MODE	(3 << 4) /* nid 0x04 */
111 
112 #define IDX_BEEP_CFG		0x0004
113 /* 0x0008 - test reg key */
114 /* 0x0009 - 0x0014 -> 12 test regs */
115 /* 0x0015 - visibility reg */
116 
117 /* Cirrus Logic CS4208 */
118 #define CS4208_VENDOR_NID	0x24
119 
120 /*
121  * Cirrus Logic CS4210
122  *
123  * 1 DAC => HP(sense) / Speakers,
124  * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
125  * 1 SPDIF OUT => SPDIF Trasmitter(sense)
126 */
127 #define CS4210_DAC_NID		0x02
128 #define CS4210_ADC_NID		0x03
129 #define CS4210_VENDOR_NID	0x0B
130 #define CS421X_DMIC_PIN_NID	0x09 /* Port E */
131 #define CS421X_SPDIF_PIN_NID	0x0A /* Port H */
132 
133 #define CS421X_IDX_DEV_CFG	0x01
134 #define CS421X_IDX_ADC_CFG	0x02
135 #define CS421X_IDX_DAC_CFG	0x03
136 #define CS421X_IDX_SPK_CTL	0x04
137 
138 /* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
139 #define CS4213_VENDOR_NID	0x09
140 
141 
142 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
143 {
144 	struct cs_spec *spec = codec->spec;
145 	snd_hda_codec_write(codec, spec->vendor_nid, 0,
146 			    AC_VERB_SET_COEF_INDEX, idx);
147 	return snd_hda_codec_read(codec, spec->vendor_nid, 0,
148 				  AC_VERB_GET_PROC_COEF, 0);
149 }
150 
151 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
152 				      unsigned int coef)
153 {
154 	struct cs_spec *spec = codec->spec;
155 	snd_hda_codec_write(codec, spec->vendor_nid, 0,
156 			    AC_VERB_SET_COEF_INDEX, idx);
157 	snd_hda_codec_write(codec, spec->vendor_nid, 0,
158 			    AC_VERB_SET_PROC_COEF, coef);
159 }
160 
161 /*
162  * auto-mute and auto-mic switching
163  * CS421x auto-output redirecting
164  * HP/SPK/SPDIF
165  */
166 
167 static void cs_automute(struct hda_codec *codec)
168 {
169 	struct cs_spec *spec = codec->spec;
170 
171 	/* mute HPs if spdif jack (SENSE_B) is present */
172 	spec->gen.master_mute = !!(spec->spdif_present && spec->sense_b);
173 
174 	snd_hda_gen_update_outputs(codec);
175 
176 	if (spec->gpio_eapd_hp || spec->gpio_eapd_speaker) {
177 		spec->gpio_data = spec->gen.hp_jack_present ?
178 			spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
179 		snd_hda_codec_write(codec, 0x01, 0,
180 				    AC_VERB_SET_GPIO_DATA, spec->gpio_data);
181 	}
182 }
183 
184 static bool is_active_pin(struct hda_codec *codec, hda_nid_t nid)
185 {
186 	unsigned int val;
187 	val = snd_hda_codec_get_pincfg(codec, nid);
188 	return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
189 }
190 
191 static void init_input_coef(struct hda_codec *codec)
192 {
193 	struct cs_spec *spec = codec->spec;
194 	unsigned int coef;
195 
196 	/* CS420x has multiple ADC, CS421x has single ADC */
197 	if (spec->vendor_nid == CS420X_VENDOR_NID) {
198 		coef = cs_vendor_coef_get(codec, IDX_BEEP_CFG);
199 		if (is_active_pin(codec, CS_DMIC2_PIN_NID))
200 			coef |= 1 << 4; /* DMIC2 2 chan on, GPIO1 off */
201 		if (is_active_pin(codec, CS_DMIC1_PIN_NID))
202 			coef |= 1 << 3; /* DMIC1 2 chan on, GPIO0 off
203 					 * No effect if SPDIF_OUT2 is
204 					 * selected in IDX_SPDIF_CTL.
205 					*/
206 
207 		cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
208 	}
209 }
210 
211 static const struct hda_verb cs_coef_init_verbs[] = {
212 	{0x11, AC_VERB_SET_PROC_STATE, 1},
213 	{0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
214 	{0x11, AC_VERB_SET_PROC_COEF,
215 	 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
216 	  | 0x0040 /* Mute DACs on FIFO error */
217 	  | 0x1000 /* Enable DACs High Pass Filter */
218 	  | 0x0400 /* Disable Coefficient Auto increment */
219 	  )},
220 	/* ADC1/2 - Digital and Analog Soft Ramp */
221 	{0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
222 	{0x11, AC_VERB_SET_PROC_COEF, 0x000a},
223 	/* Beep */
224 	{0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
225 	{0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
226 
227 	{} /* terminator */
228 };
229 
230 static const struct hda_verb cs4208_coef_init_verbs[] = {
231 	{0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
232 	{0x24, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
233 	{0x24, AC_VERB_SET_COEF_INDEX, 0x0033},
234 	{0x24, AC_VERB_SET_PROC_COEF, 0x0001}, /* A1 ICS */
235 	{0x24, AC_VERB_SET_COEF_INDEX, 0x0034},
236 	{0x24, AC_VERB_SET_PROC_COEF, 0x1C01}, /* A1 Enable, A Thresh = 300mV */
237 	{} /* terminator */
238 };
239 
240 /* Errata: CS4207 rev C0/C1/C2 Silicon
241  *
242  * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
243  *
244  * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
245  * may be excessive (up to an additional 200 μA), which is most easily
246  * observed while the part is being held in reset (RESET# active low).
247  *
248  * Root Cause: At initial powerup of the device, the logic that drives
249  * the clock and write enable to the S/PDIF SRC RAMs is not properly
250  * initialized.
251  * Certain random patterns will cause a steady leakage current in those
252  * RAM cells. The issue will resolve once the SRCs are used (turned on).
253  *
254  * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
255  * blocks, which will alleviate the issue.
256  */
257 
258 static const struct hda_verb cs_errata_init_verbs[] = {
259 	{0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
260 	{0x11, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
261 
262 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
263 	{0x11, AC_VERB_SET_PROC_COEF, 0x9999},
264 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
265 	{0x11, AC_VERB_SET_PROC_COEF, 0xa412},
266 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
267 	{0x11, AC_VERB_SET_PROC_COEF, 0x0009},
268 
269 	{0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
270 	{0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
271 
272 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
273 	{0x11, AC_VERB_SET_PROC_COEF, 0x2412},
274 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
275 	{0x11, AC_VERB_SET_PROC_COEF, 0x0000},
276 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
277 	{0x11, AC_VERB_SET_PROC_COEF, 0x0008},
278 	{0x11, AC_VERB_SET_PROC_STATE, 0x00},
279 
280 #if 0 /* Don't to set to D3 as we are in power-up sequence */
281 	{0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
282 	{0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
283 	/*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
284 #endif
285 
286 	{} /* terminator */
287 };
288 
289 /* SPDIF setup */
290 static void init_digital_coef(struct hda_codec *codec)
291 {
292 	unsigned int coef;
293 
294 	coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
295 	coef |= 0x0008; /* Replace with mute on error */
296 	if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
297 		coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
298 				 * SPDIF_OUT2 is shared with GPIO1 and
299 				 * DMIC_SDA2.
300 				 */
301 	cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
302 }
303 
304 static int cs_init(struct hda_codec *codec)
305 {
306 	struct cs_spec *spec = codec->spec;
307 
308 	if (spec->vendor_nid == CS420X_VENDOR_NID) {
309 		/* init_verb sequence for C0/C1/C2 errata*/
310 		snd_hda_sequence_write(codec, cs_errata_init_verbs);
311 		snd_hda_sequence_write(codec, cs_coef_init_verbs);
312 	} else if (spec->vendor_nid == CS4208_VENDOR_NID) {
313 		snd_hda_sequence_write(codec, cs4208_coef_init_verbs);
314 	}
315 
316 	snd_hda_gen_init(codec);
317 
318 	if (spec->gpio_mask) {
319 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
320 				    spec->gpio_mask);
321 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
322 				    spec->gpio_dir);
323 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
324 				    spec->gpio_data);
325 	}
326 
327 	if (spec->vendor_nid == CS420X_VENDOR_NID) {
328 		init_input_coef(codec);
329 		init_digital_coef(codec);
330 	}
331 
332 	return 0;
333 }
334 
335 static int cs_build_controls(struct hda_codec *codec)
336 {
337 	int err;
338 
339 	err = snd_hda_gen_build_controls(codec);
340 	if (err < 0)
341 		return err;
342 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
343 	return 0;
344 }
345 
346 #define cs_free		snd_hda_gen_free
347 
348 static const struct hda_codec_ops cs_patch_ops = {
349 	.build_controls = cs_build_controls,
350 	.build_pcms = snd_hda_gen_build_pcms,
351 	.init = cs_init,
352 	.free = cs_free,
353 	.unsol_event = snd_hda_jack_unsol_event,
354 };
355 
356 static int cs_parse_auto_config(struct hda_codec *codec)
357 {
358 	struct cs_spec *spec = codec->spec;
359 	int err;
360 
361 	err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
362 	if (err < 0)
363 		return err;
364 
365 	err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
366 	if (err < 0)
367 		return err;
368 
369 	return 0;
370 }
371 
372 static const struct hda_model_fixup cs420x_models[] = {
373 	{ .id = CS420X_MBP53, .name = "mbp53" },
374 	{ .id = CS420X_MBP55, .name = "mbp55" },
375 	{ .id = CS420X_IMAC27, .name = "imac27" },
376 	{ .id = CS420X_IMAC27_122, .name = "imac27_122" },
377 	{ .id = CS420X_APPLE, .name = "apple" },
378 	{ .id = CS420X_MBP101, .name = "mbp101" },
379 	{ .id = CS420X_MBP81, .name = "mbp81" },
380 	{ .id = CS420X_MBA42, .name = "mba42" },
381 	{}
382 };
383 
384 static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
385 	SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
386 	SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
387 	SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
388 	SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
389 	/* this conflicts with too many other models */
390 	/*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
391 
392 	/* codec SSID */
393 	SND_PCI_QUIRK(0x106b, 0x1c00, "MacBookPro 8,1", CS420X_MBP81),
394 	SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
395 	SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
396 	SND_PCI_QUIRK(0x106b, 0x5600, "MacBookAir 5,2", CS420X_MBP81),
397 	SND_PCI_QUIRK(0x106b, 0x5b00, "MacBookAir 4,2", CS420X_MBA42),
398 	SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
399 	{} /* terminator */
400 };
401 
402 static const struct hda_pintbl mbp53_pincfgs[] = {
403 	{ 0x09, 0x012b4050 },
404 	{ 0x0a, 0x90100141 },
405 	{ 0x0b, 0x90100140 },
406 	{ 0x0c, 0x018b3020 },
407 	{ 0x0d, 0x90a00110 },
408 	{ 0x0e, 0x400000f0 },
409 	{ 0x0f, 0x01cbe030 },
410 	{ 0x10, 0x014be060 },
411 	{ 0x12, 0x400000f0 },
412 	{ 0x15, 0x400000f0 },
413 	{} /* terminator */
414 };
415 
416 static const struct hda_pintbl mbp55_pincfgs[] = {
417 	{ 0x09, 0x012b4030 },
418 	{ 0x0a, 0x90100121 },
419 	{ 0x0b, 0x90100120 },
420 	{ 0x0c, 0x400000f0 },
421 	{ 0x0d, 0x90a00110 },
422 	{ 0x0e, 0x400000f0 },
423 	{ 0x0f, 0x400000f0 },
424 	{ 0x10, 0x014be040 },
425 	{ 0x12, 0x400000f0 },
426 	{ 0x15, 0x400000f0 },
427 	{} /* terminator */
428 };
429 
430 static const struct hda_pintbl imac27_pincfgs[] = {
431 	{ 0x09, 0x012b4050 },
432 	{ 0x0a, 0x90100140 },
433 	{ 0x0b, 0x90100142 },
434 	{ 0x0c, 0x018b3020 },
435 	{ 0x0d, 0x90a00110 },
436 	{ 0x0e, 0x400000f0 },
437 	{ 0x0f, 0x01cbe030 },
438 	{ 0x10, 0x014be060 },
439 	{ 0x12, 0x01ab9070 },
440 	{ 0x15, 0x400000f0 },
441 	{} /* terminator */
442 };
443 
444 static const struct hda_pintbl mbp101_pincfgs[] = {
445 	{ 0x0d, 0x40ab90f0 },
446 	{ 0x0e, 0x90a600f0 },
447 	{ 0x12, 0x50a600f0 },
448 	{} /* terminator */
449 };
450 
451 static const struct hda_pintbl mba42_pincfgs[] = {
452 	{ 0x09, 0x012b4030 }, /* HP */
453 	{ 0x0a, 0x400000f0 },
454 	{ 0x0b, 0x90100120 }, /* speaker */
455 	{ 0x0c, 0x400000f0 },
456 	{ 0x0d, 0x90a00110 }, /* mic */
457 	{ 0x0e, 0x400000f0 },
458 	{ 0x0f, 0x400000f0 },
459 	{ 0x10, 0x400000f0 },
460 	{ 0x12, 0x400000f0 },
461 	{ 0x15, 0x400000f0 },
462 	{} /* terminator */
463 };
464 
465 static const struct hda_pintbl mba6_pincfgs[] = {
466 	{ 0x10, 0x032120f0 }, /* HP */
467 	{ 0x11, 0x500000f0 },
468 	{ 0x12, 0x90100010 }, /* Speaker */
469 	{ 0x13, 0x500000f0 },
470 	{ 0x14, 0x500000f0 },
471 	{ 0x15, 0x770000f0 },
472 	{ 0x16, 0x770000f0 },
473 	{ 0x17, 0x430000f0 },
474 	{ 0x18, 0x43ab9030 }, /* Mic */
475 	{ 0x19, 0x770000f0 },
476 	{ 0x1a, 0x770000f0 },
477 	{ 0x1b, 0x770000f0 },
478 	{ 0x1c, 0x90a00090 },
479 	{ 0x1d, 0x500000f0 },
480 	{ 0x1e, 0x500000f0 },
481 	{ 0x1f, 0x500000f0 },
482 	{ 0x20, 0x500000f0 },
483 	{ 0x21, 0x430000f0 },
484 	{ 0x22, 0x430000f0 },
485 	{} /* terminator */
486 };
487 
488 static void cs420x_fixup_gpio_13(struct hda_codec *codec,
489 				 const struct hda_fixup *fix, int action)
490 {
491 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
492 		struct cs_spec *spec = codec->spec;
493 		spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
494 		spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
495 		spec->gpio_mask = spec->gpio_dir =
496 			spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
497 	}
498 }
499 
500 static void cs420x_fixup_gpio_23(struct hda_codec *codec,
501 				 const struct hda_fixup *fix, int action)
502 {
503 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
504 		struct cs_spec *spec = codec->spec;
505 		spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
506 		spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
507 		spec->gpio_mask = spec->gpio_dir =
508 			spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
509 	}
510 }
511 
512 static const struct hda_fixup cs420x_fixups[] = {
513 	[CS420X_MBP53] = {
514 		.type = HDA_FIXUP_PINS,
515 		.v.pins = mbp53_pincfgs,
516 		.chained = true,
517 		.chain_id = CS420X_APPLE,
518 	},
519 	[CS420X_MBP55] = {
520 		.type = HDA_FIXUP_PINS,
521 		.v.pins = mbp55_pincfgs,
522 		.chained = true,
523 		.chain_id = CS420X_GPIO_13,
524 	},
525 	[CS420X_IMAC27] = {
526 		.type = HDA_FIXUP_PINS,
527 		.v.pins = imac27_pincfgs,
528 		.chained = true,
529 		.chain_id = CS420X_GPIO_13,
530 	},
531 	[CS420X_GPIO_13] = {
532 		.type = HDA_FIXUP_FUNC,
533 		.v.func = cs420x_fixup_gpio_13,
534 	},
535 	[CS420X_GPIO_23] = {
536 		.type = HDA_FIXUP_FUNC,
537 		.v.func = cs420x_fixup_gpio_23,
538 	},
539 	[CS420X_MBP101] = {
540 		.type = HDA_FIXUP_PINS,
541 		.v.pins = mbp101_pincfgs,
542 		.chained = true,
543 		.chain_id = CS420X_GPIO_13,
544 	},
545 	[CS420X_MBP81] = {
546 		.type = HDA_FIXUP_VERBS,
547 		.v.verbs = (const struct hda_verb[]) {
548 			/* internal mic ADC2: right only, single ended */
549 			{0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
550 			{0x11, AC_VERB_SET_PROC_COEF, 0x102a},
551 			{}
552 		},
553 		.chained = true,
554 		.chain_id = CS420X_GPIO_13,
555 	},
556 	[CS420X_MBA42] = {
557 		.type = HDA_FIXUP_PINS,
558 		.v.pins = mba42_pincfgs,
559 		.chained = true,
560 		.chain_id = CS420X_GPIO_13,
561 	},
562 };
563 
564 static struct cs_spec *cs_alloc_spec(struct hda_codec *codec, int vendor_nid)
565 {
566 	struct cs_spec *spec;
567 
568 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
569 	if (!spec)
570 		return NULL;
571 	codec->spec = spec;
572 	spec->vendor_nid = vendor_nid;
573 	codec->power_save_node = 1;
574 	snd_hda_gen_spec_init(&spec->gen);
575 
576 	return spec;
577 }
578 
579 static int patch_cs420x(struct hda_codec *codec)
580 {
581 	struct cs_spec *spec;
582 	int err;
583 
584 	spec = cs_alloc_spec(codec, CS420X_VENDOR_NID);
585 	if (!spec)
586 		return -ENOMEM;
587 
588 	codec->patch_ops = cs_patch_ops;
589 	spec->gen.automute_hook = cs_automute;
590 	codec->single_adc_amp = 1;
591 
592 	snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
593 			   cs420x_fixups);
594 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
595 
596 	err = cs_parse_auto_config(codec);
597 	if (err < 0)
598 		goto error;
599 
600 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
601 
602 	return 0;
603 
604  error:
605 	cs_free(codec);
606 	return err;
607 }
608 
609 /*
610  * CS4208 support:
611  * Its layout is no longer compatible with CS4206/CS4207
612  */
613 enum {
614 	CS4208_MAC_AUTO,
615 	CS4208_MBA6,
616 	CS4208_MBP11,
617 	CS4208_MACMINI,
618 	CS4208_GPIO0,
619 };
620 
621 static const struct hda_model_fixup cs4208_models[] = {
622 	{ .id = CS4208_GPIO0, .name = "gpio0" },
623 	{ .id = CS4208_MBA6, .name = "mba6" },
624 	{ .id = CS4208_MBP11, .name = "mbp11" },
625 	{ .id = CS4208_MACMINI, .name = "macmini" },
626 	{}
627 };
628 
629 static const struct snd_pci_quirk cs4208_fixup_tbl[] = {
630 	SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS4208_MAC_AUTO),
631 	{} /* terminator */
632 };
633 
634 /* codec SSID matching */
635 static const struct snd_pci_quirk cs4208_mac_fixup_tbl[] = {
636 	SND_PCI_QUIRK(0x106b, 0x5e00, "MacBookPro 11,2", CS4208_MBP11),
637 	SND_PCI_QUIRK(0x106b, 0x6c00, "MacMini 7,1", CS4208_MACMINI),
638 	SND_PCI_QUIRK(0x106b, 0x7100, "MacBookAir 6,1", CS4208_MBA6),
639 	SND_PCI_QUIRK(0x106b, 0x7200, "MacBookAir 6,2", CS4208_MBA6),
640 	SND_PCI_QUIRK(0x106b, 0x7b00, "MacBookPro 12,1", CS4208_MBP11),
641 	{} /* terminator */
642 };
643 
644 static void cs4208_fixup_gpio0(struct hda_codec *codec,
645 			       const struct hda_fixup *fix, int action)
646 {
647 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
648 		struct cs_spec *spec = codec->spec;
649 		spec->gpio_eapd_hp = 0;
650 		spec->gpio_eapd_speaker = 1;
651 		spec->gpio_mask = spec->gpio_dir =
652 			spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
653 	}
654 }
655 
656 static const struct hda_fixup cs4208_fixups[];
657 
658 /* remap the fixup from codec SSID and apply it */
659 static void cs4208_fixup_mac(struct hda_codec *codec,
660 			     const struct hda_fixup *fix, int action)
661 {
662 	if (action != HDA_FIXUP_ACT_PRE_PROBE)
663 		return;
664 
665 	codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
666 	snd_hda_pick_fixup(codec, NULL, cs4208_mac_fixup_tbl, cs4208_fixups);
667 	if (codec->fixup_id == HDA_FIXUP_ID_NOT_SET)
668 		codec->fixup_id = CS4208_GPIO0; /* default fixup */
669 	snd_hda_apply_fixup(codec, action);
670 }
671 
672 /* MacMini 7,1 has the inverted jack detection */
673 static void cs4208_fixup_macmini(struct hda_codec *codec,
674 				 const struct hda_fixup *fix, int action)
675 {
676 	static const struct hda_pintbl pincfgs[] = {
677 		{ 0x18, 0x00ab9150 }, /* mic (audio-in) jack: disable detect */
678 		{ 0x21, 0x004be140 }, /* SPDIF: disable detect */
679 		{ }
680 	};
681 
682 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
683 		/* HP pin (0x10) has an inverted detection */
684 		codec->inv_jack_detect = 1;
685 		/* disable the bogus Mic and SPDIF jack detections */
686 		snd_hda_apply_pincfgs(codec, pincfgs);
687 	}
688 }
689 
690 static int cs4208_spdif_sw_put(struct snd_kcontrol *kcontrol,
691 			       struct snd_ctl_elem_value *ucontrol)
692 {
693 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
694 	struct cs_spec *spec = codec->spec;
695 	hda_nid_t pin = spec->gen.autocfg.dig_out_pins[0];
696 	int pinctl = ucontrol->value.integer.value[0] ? PIN_OUT : 0;
697 
698 	snd_hda_set_pin_ctl_cache(codec, pin, pinctl);
699 	return spec->spdif_sw_put(kcontrol, ucontrol);
700 }
701 
702 /* hook the SPDIF switch */
703 static void cs4208_fixup_spdif_switch(struct hda_codec *codec,
704 				      const struct hda_fixup *fix, int action)
705 {
706 	if (action == HDA_FIXUP_ACT_BUILD) {
707 		struct cs_spec *spec = codec->spec;
708 		struct snd_kcontrol *kctl;
709 
710 		if (!spec->gen.autocfg.dig_out_pins[0])
711 			return;
712 		kctl = snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch");
713 		if (!kctl)
714 			return;
715 		spec->spdif_sw_put = kctl->put;
716 		kctl->put = cs4208_spdif_sw_put;
717 	}
718 }
719 
720 static const struct hda_fixup cs4208_fixups[] = {
721 	[CS4208_MBA6] = {
722 		.type = HDA_FIXUP_PINS,
723 		.v.pins = mba6_pincfgs,
724 		.chained = true,
725 		.chain_id = CS4208_GPIO0,
726 	},
727 	[CS4208_MBP11] = {
728 		.type = HDA_FIXUP_FUNC,
729 		.v.func = cs4208_fixup_spdif_switch,
730 		.chained = true,
731 		.chain_id = CS4208_GPIO0,
732 	},
733 	[CS4208_MACMINI] = {
734 		.type = HDA_FIXUP_FUNC,
735 		.v.func = cs4208_fixup_macmini,
736 		.chained = true,
737 		.chain_id = CS4208_GPIO0,
738 	},
739 	[CS4208_GPIO0] = {
740 		.type = HDA_FIXUP_FUNC,
741 		.v.func = cs4208_fixup_gpio0,
742 	},
743 	[CS4208_MAC_AUTO] = {
744 		.type = HDA_FIXUP_FUNC,
745 		.v.func = cs4208_fixup_mac,
746 	},
747 };
748 
749 /* correct the 0dB offset of input pins */
750 static void cs4208_fix_amp_caps(struct hda_codec *codec, hda_nid_t adc)
751 {
752 	unsigned int caps;
753 
754 	caps = query_amp_caps(codec, adc, HDA_INPUT);
755 	caps &= ~(AC_AMPCAP_OFFSET);
756 	caps |= 0x02;
757 	snd_hda_override_amp_caps(codec, adc, HDA_INPUT, caps);
758 }
759 
760 static int patch_cs4208(struct hda_codec *codec)
761 {
762 	struct cs_spec *spec;
763 	int err;
764 
765 	spec = cs_alloc_spec(codec, CS4208_VENDOR_NID);
766 	if (!spec)
767 		return -ENOMEM;
768 
769 	codec->patch_ops = cs_patch_ops;
770 	spec->gen.automute_hook = cs_automute;
771 	/* exclude NID 0x10 (HP) from output volumes due to different steps */
772 	spec->gen.out_vol_mask = 1ULL << 0x10;
773 
774 	snd_hda_pick_fixup(codec, cs4208_models, cs4208_fixup_tbl,
775 			   cs4208_fixups);
776 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
777 
778 	snd_hda_override_wcaps(codec, 0x18,
779 			       get_wcaps(codec, 0x18) | AC_WCAP_STEREO);
780 	cs4208_fix_amp_caps(codec, 0x18);
781 	cs4208_fix_amp_caps(codec, 0x1b);
782 	cs4208_fix_amp_caps(codec, 0x1c);
783 
784 	err = cs_parse_auto_config(codec);
785 	if (err < 0)
786 		goto error;
787 
788 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
789 
790 	return 0;
791 
792  error:
793 	cs_free(codec);
794 	return err;
795 }
796 
797 /*
798  * Cirrus Logic CS4210
799  *
800  * 1 DAC => HP(sense) / Speakers,
801  * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
802  * 1 SPDIF OUT => SPDIF Trasmitter(sense)
803 */
804 
805 /* CS4210 board names */
806 static const struct hda_model_fixup cs421x_models[] = {
807 	{ .id = CS421X_CDB4210, .name = "cdb4210" },
808 	{ .id = CS421X_STUMPY, .name = "stumpy" },
809 	{}
810 };
811 
812 static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
813 	/* Test Intel board + CDB2410  */
814 	SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
815 	{} /* terminator */
816 };
817 
818 /* CS4210 board pinconfigs */
819 /* Default CS4210 (CDB4210)*/
820 static const struct hda_pintbl cdb4210_pincfgs[] = {
821 	{ 0x05, 0x0321401f },
822 	{ 0x06, 0x90170010 },
823 	{ 0x07, 0x03813031 },
824 	{ 0x08, 0xb7a70037 },
825 	{ 0x09, 0xb7a6003e },
826 	{ 0x0a, 0x034510f0 },
827 	{} /* terminator */
828 };
829 
830 /* Stumpy ChromeBox */
831 static const struct hda_pintbl stumpy_pincfgs[] = {
832 	{ 0x05, 0x022120f0 },
833 	{ 0x06, 0x901700f0 },
834 	{ 0x07, 0x02a120f0 },
835 	{ 0x08, 0x77a70037 },
836 	{ 0x09, 0x77a6003e },
837 	{ 0x0a, 0x434510f0 },
838 	{} /* terminator */
839 };
840 
841 /* Setup GPIO/SENSE for each board (if used) */
842 static void cs421x_fixup_sense_b(struct hda_codec *codec,
843 				 const struct hda_fixup *fix, int action)
844 {
845 	struct cs_spec *spec = codec->spec;
846 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
847 		spec->sense_b = 1;
848 }
849 
850 static const struct hda_fixup cs421x_fixups[] = {
851 	[CS421X_CDB4210] = {
852 		.type = HDA_FIXUP_PINS,
853 		.v.pins = cdb4210_pincfgs,
854 		.chained = true,
855 		.chain_id = CS421X_SENSE_B,
856 	},
857 	[CS421X_SENSE_B] = {
858 		.type = HDA_FIXUP_FUNC,
859 		.v.func = cs421x_fixup_sense_b,
860 	},
861 	[CS421X_STUMPY] = {
862 		.type = HDA_FIXUP_PINS,
863 		.v.pins = stumpy_pincfgs,
864 	},
865 };
866 
867 static const struct hda_verb cs421x_coef_init_verbs[] = {
868 	{0x0B, AC_VERB_SET_PROC_STATE, 1},
869 	{0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
870 	/*
871 	    Disable Coefficient Index Auto-Increment(DAI)=1,
872 	    PDREF=0
873 	*/
874 	{0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
875 
876 	{0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
877 	/* ADC SZCMode = Digital Soft Ramp */
878 	{0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
879 
880 	{0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
881 	{0x0B, AC_VERB_SET_PROC_COEF,
882 	 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
883 	  | 0x0004 /* Mute DAC on FIFO error */
884 	  | 0x0008 /* Enable DAC High Pass Filter */
885 	  )},
886 	{} /* terminator */
887 };
888 
889 /* Errata: CS4210 rev A1 Silicon
890  *
891  * http://www.cirrus.com/en/pubs/errata/
892  *
893  * Description:
894  * 1. Performance degredation is present in the ADC.
895  * 2. Speaker output is not completely muted upon HP detect.
896  * 3. Noise is present when clipping occurs on the amplified
897  *    speaker outputs.
898  *
899  * Workaround:
900  * The following verb sequence written to the registers during
901  * initialization will correct the issues listed above.
902  */
903 
904 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
905 	{0x0B, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
906 
907 	{0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
908 	{0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
909 
910 	{0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
911 	{0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
912 
913 	{0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
914 	{0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
915 
916 	{0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
917 	{0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
918 
919 	{0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
920 	{0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
921 
922 	{} /* terminator */
923 };
924 
925 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
926 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
927 
928 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
929 				struct snd_ctl_elem_info *uinfo)
930 {
931 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
932 	uinfo->count = 1;
933 	uinfo->value.integer.min = 0;
934 	uinfo->value.integer.max = 3;
935 	return 0;
936 }
937 
938 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
939 				struct snd_ctl_elem_value *ucontrol)
940 {
941 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
942 
943 	ucontrol->value.integer.value[0] =
944 		cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
945 	return 0;
946 }
947 
948 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
949 				struct snd_ctl_elem_value *ucontrol)
950 {
951 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
952 
953 	unsigned int vol = ucontrol->value.integer.value[0];
954 	unsigned int coef =
955 		cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
956 	unsigned int original_coef = coef;
957 
958 	coef &= ~0x0003;
959 	coef |= (vol & 0x0003);
960 	if (original_coef == coef)
961 		return 0;
962 	else {
963 		cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
964 		return 1;
965 	}
966 }
967 
968 static const struct snd_kcontrol_new cs421x_speaker_boost_ctl = {
969 
970 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
971 	.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
972 			SNDRV_CTL_ELEM_ACCESS_TLV_READ),
973 	.name = "Speaker Boost Playback Volume",
974 	.info = cs421x_boost_vol_info,
975 	.get = cs421x_boost_vol_get,
976 	.put = cs421x_boost_vol_put,
977 	.tlv = { .p = cs421x_speaker_boost_db_scale },
978 };
979 
980 static void cs4210_pinmux_init(struct hda_codec *codec)
981 {
982 	struct cs_spec *spec = codec->spec;
983 	unsigned int def_conf, coef;
984 
985 	/* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
986 	coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
987 
988 	if (spec->gpio_mask)
989 		coef |= 0x0008; /* B1,B2 are GPIOs */
990 	else
991 		coef &= ~0x0008;
992 
993 	if (spec->sense_b)
994 		coef |= 0x0010; /* B2 is SENSE_B, not inverted  */
995 	else
996 		coef &= ~0x0010;
997 
998 	cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
999 
1000 	if ((spec->gpio_mask || spec->sense_b) &&
1001 	    is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1002 
1003 		/*
1004 		    GPIO or SENSE_B forced - disconnect the DMIC pin.
1005 		*/
1006 		def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1007 		def_conf &= ~AC_DEFCFG_PORT_CONN;
1008 		def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1009 		snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1010 	}
1011 }
1012 
1013 static void cs4210_spdif_automute(struct hda_codec *codec,
1014 				  struct hda_jack_callback *tbl)
1015 {
1016 	struct cs_spec *spec = codec->spec;
1017 	bool spdif_present = false;
1018 	hda_nid_t spdif_pin = spec->gen.autocfg.dig_out_pins[0];
1019 
1020 	/* detect on spdif is specific to CS4210 */
1021 	if (!spec->spdif_detect ||
1022 	    spec->vendor_nid != CS4210_VENDOR_NID)
1023 		return;
1024 
1025 	spdif_present = snd_hda_jack_detect(codec, spdif_pin);
1026 	if (spdif_present == spec->spdif_present)
1027 		return;
1028 
1029 	spec->spdif_present = spdif_present;
1030 	/* SPDIF TX on/off */
1031 	snd_hda_set_pin_ctl(codec, spdif_pin, spdif_present ? PIN_OUT : 0);
1032 
1033 	cs_automute(codec);
1034 }
1035 
1036 static void parse_cs421x_digital(struct hda_codec *codec)
1037 {
1038 	struct cs_spec *spec = codec->spec;
1039 	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
1040 	int i;
1041 
1042 	for (i = 0; i < cfg->dig_outs; i++) {
1043 		hda_nid_t nid = cfg->dig_out_pins[i];
1044 		if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1045 			spec->spdif_detect = 1;
1046 			snd_hda_jack_detect_enable_callback(codec, nid,
1047 							    cs4210_spdif_automute);
1048 		}
1049 	}
1050 }
1051 
1052 static int cs421x_init(struct hda_codec *codec)
1053 {
1054 	struct cs_spec *spec = codec->spec;
1055 
1056 	if (spec->vendor_nid == CS4210_VENDOR_NID) {
1057 		snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1058 		snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1059 		cs4210_pinmux_init(codec);
1060 	}
1061 
1062 	snd_hda_gen_init(codec);
1063 
1064 	if (spec->gpio_mask) {
1065 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1066 				    spec->gpio_mask);
1067 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1068 				    spec->gpio_dir);
1069 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1070 				    spec->gpio_data);
1071 	}
1072 
1073 	init_input_coef(codec);
1074 
1075 	cs4210_spdif_automute(codec, NULL);
1076 
1077 	return 0;
1078 }
1079 
1080 static int cs421x_build_controls(struct hda_codec *codec)
1081 {
1082 	struct cs_spec *spec = codec->spec;
1083 	int err;
1084 
1085 	err = snd_hda_gen_build_controls(codec);
1086 	if (err < 0)
1087 		return err;
1088 
1089 	if (spec->gen.autocfg.speaker_outs &&
1090 	    spec->vendor_nid == CS4210_VENDOR_NID) {
1091 		err = snd_hda_ctl_add(codec, 0,
1092 			snd_ctl_new1(&cs421x_speaker_boost_ctl, codec));
1093 		if (err < 0)
1094 			return err;
1095 	}
1096 	return 0;
1097 }
1098 
1099 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
1100 {
1101 	unsigned int caps;
1102 
1103 	/* set the upper-limit for mixer amp to 0dB */
1104 	caps = query_amp_caps(codec, dac, HDA_OUTPUT);
1105 	caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
1106 	caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
1107 		<< AC_AMPCAP_NUM_STEPS_SHIFT;
1108 	snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
1109 }
1110 
1111 static int cs421x_parse_auto_config(struct hda_codec *codec)
1112 {
1113 	struct cs_spec *spec = codec->spec;
1114 	hda_nid_t dac = CS4210_DAC_NID;
1115 	int err;
1116 
1117 	fix_volume_caps(codec, dac);
1118 
1119 	err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
1120 	if (err < 0)
1121 		return err;
1122 
1123 	err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
1124 	if (err < 0)
1125 		return err;
1126 
1127 	parse_cs421x_digital(codec);
1128 	return 0;
1129 }
1130 
1131 #ifdef CONFIG_PM
1132 /*
1133 	Manage PDREF, when transitioning to D3hot
1134 	(DAC,ADC) -> D3, PDREF=1, AFG->D3
1135 */
1136 static int cs421x_suspend(struct hda_codec *codec)
1137 {
1138 	struct cs_spec *spec = codec->spec;
1139 	unsigned int coef;
1140 
1141 	snd_hda_shutup_pins(codec);
1142 
1143 	snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1144 			    AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
1145 	snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1146 			    AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
1147 
1148 	if (spec->vendor_nid == CS4210_VENDOR_NID) {
1149 		coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1150 		coef |= 0x0004; /* PDREF */
1151 		cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1152 	}
1153 
1154 	return 0;
1155 }
1156 #endif
1157 
1158 static const struct hda_codec_ops cs421x_patch_ops = {
1159 	.build_controls = cs421x_build_controls,
1160 	.build_pcms = snd_hda_gen_build_pcms,
1161 	.init = cs421x_init,
1162 	.free = cs_free,
1163 	.unsol_event = snd_hda_jack_unsol_event,
1164 #ifdef CONFIG_PM
1165 	.suspend = cs421x_suspend,
1166 #endif
1167 };
1168 
1169 static int patch_cs4210(struct hda_codec *codec)
1170 {
1171 	struct cs_spec *spec;
1172 	int err;
1173 
1174 	spec = cs_alloc_spec(codec, CS4210_VENDOR_NID);
1175 	if (!spec)
1176 		return -ENOMEM;
1177 
1178 	codec->patch_ops = cs421x_patch_ops;
1179 	spec->gen.automute_hook = cs_automute;
1180 
1181 	snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1182 			   cs421x_fixups);
1183 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1184 
1185 	/*
1186 	    Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1187 	    is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1188 	    is disabled.
1189 	*/
1190 	cs4210_pinmux_init(codec);
1191 
1192 	err = cs421x_parse_auto_config(codec);
1193 	if (err < 0)
1194 		goto error;
1195 
1196 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1197 
1198 	return 0;
1199 
1200  error:
1201 	cs_free(codec);
1202 	return err;
1203 }
1204 
1205 static int patch_cs4213(struct hda_codec *codec)
1206 {
1207 	struct cs_spec *spec;
1208 	int err;
1209 
1210 	spec = cs_alloc_spec(codec, CS4213_VENDOR_NID);
1211 	if (!spec)
1212 		return -ENOMEM;
1213 
1214 	codec->patch_ops = cs421x_patch_ops;
1215 
1216 	err = cs421x_parse_auto_config(codec);
1217 	if (err < 0)
1218 		goto error;
1219 
1220 	return 0;
1221 
1222  error:
1223 	cs_free(codec);
1224 	return err;
1225 }
1226 
1227 
1228 /*
1229  * patch entries
1230  */
1231 static const struct hda_device_id snd_hda_id_cirrus[] = {
1232 	HDA_CODEC_ENTRY(0x10134206, "CS4206", patch_cs420x),
1233 	HDA_CODEC_ENTRY(0x10134207, "CS4207", patch_cs420x),
1234 	HDA_CODEC_ENTRY(0x10134208, "CS4208", patch_cs4208),
1235 	HDA_CODEC_ENTRY(0x10134210, "CS4210", patch_cs4210),
1236 	HDA_CODEC_ENTRY(0x10134213, "CS4213", patch_cs4213),
1237 	{} /* terminator */
1238 };
1239 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_cirrus);
1240 
1241 MODULE_LICENSE("GPL");
1242 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1243 
1244 static struct hda_codec_driver cirrus_driver = {
1245 	.id = snd_hda_id_cirrus,
1246 };
1247 
1248 module_hda_codec_driver(cirrus_driver);
1249