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