xref: /linux/sound/hda/codecs/cirrus/cs420x.c (revision 177bf8620cf4ed290ee170a6c5966adc0924b336)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Cirrus Logic CS420x HD-audio codec
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 <linux/pci.h>
13 #include <sound/tlv.h>
14 #include <sound/hda_codec.h>
15 #include "hda_local.h"
16 #include "hda_auto_parser.h"
17 #include "hda_jack.h"
18 #include "../generic.h"
19 
20 struct cs_spec {
21 	struct hda_gen_spec gen;
22 
23 	unsigned int gpio_mask;
24 	unsigned int gpio_dir;
25 	unsigned int gpio_data;
26 	unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
27 	unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
28 
29 	hda_nid_t vendor_nid;
30 
31 	/* for MBP SPDIF control */
32 	int (*spdif_sw_put)(struct snd_kcontrol *kcontrol,
33 			    struct snd_ctl_elem_value *ucontrol);
34 };
35 
36 /* available models with CS420x */
37 enum {
38 	CS420X_MBP53,
39 	CS420X_MBP55,
40 	CS420X_IMAC27,
41 	CS420X_GPIO_13,
42 	CS420X_GPIO_23,
43 	CS420X_MBP101,
44 	CS420X_MBP81,
45 	CS420X_MBA42,
46 	CS420X_AUTO,
47 	/* aliases */
48 	CS420X_IMAC27_122 = CS420X_GPIO_23,
49 	CS420X_APPLE = CS420X_GPIO_13,
50 };
51 
52 /* Vendor-specific processing widget */
53 #define CS420X_VENDOR_NID	0x11
54 #define CS_DIG_OUT1_PIN_NID	0x10
55 #define CS_DIG_OUT2_PIN_NID	0x15
56 #define CS_DMIC1_PIN_NID	0x0e
57 #define CS_DMIC2_PIN_NID	0x12
58 
59 /* coef indices */
60 #define IDX_SPDIF_STAT		0x0000
61 #define IDX_SPDIF_CTL		0x0001
62 #define IDX_ADC_CFG		0x0002
63 /* SZC bitmask, 4 modes below:
64  * 0 = immediate,
65  * 1 = digital immediate, analog zero-cross
66  * 2 = digtail & analog soft-ramp
67  * 3 = digital soft-ramp, analog zero-cross
68  */
69 #define   CS_COEF_ADC_SZC_MASK		(3 << 0)
70 #define   CS_COEF_ADC_MIC_SZC_MODE	(3 << 0) /* SZC setup for mic */
71 #define   CS_COEF_ADC_LI_SZC_MODE	(3 << 0) /* SZC setup for line-in */
72 /* PGA mode: 0 = differential, 1 = signle-ended */
73 #define   CS_COEF_ADC_MIC_PGA_MODE	(1 << 5) /* PGA setup for mic */
74 #define   CS_COEF_ADC_LI_PGA_MODE	(1 << 6) /* PGA setup for line-in */
75 #define IDX_DAC_CFG		0x0003
76 /* SZC bitmask, 4 modes below:
77  * 0 = Immediate
78  * 1 = zero-cross
79  * 2 = soft-ramp
80  * 3 = soft-ramp on zero-cross
81  */
82 #define   CS_COEF_DAC_HP_SZC_MODE	(3 << 0) /* nid 0x02 */
83 #define   CS_COEF_DAC_LO_SZC_MODE	(3 << 2) /* nid 0x03 */
84 #define   CS_COEF_DAC_SPK_SZC_MODE	(3 << 4) /* nid 0x04 */
85 
86 #define IDX_BEEP_CFG		0x0004
87 /* 0x0008 - test reg key */
88 /* 0x0009 - 0x0014 -> 12 test regs */
89 /* 0x0015 - visibility reg */
90 
91 /* Cirrus Logic CS4208 */
92 #define CS4208_VENDOR_NID	0x24
93 
cs_vendor_coef_get(struct hda_codec * codec,unsigned int idx)94 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
95 {
96 	struct cs_spec *spec = codec->spec;
97 
98 	snd_hda_codec_write(codec, spec->vendor_nid, 0,
99 			    AC_VERB_SET_COEF_INDEX, idx);
100 	return snd_hda_codec_read(codec, spec->vendor_nid, 0,
101 				  AC_VERB_GET_PROC_COEF, 0);
102 }
103 
cs_vendor_coef_set(struct hda_codec * codec,unsigned int idx,unsigned int coef)104 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
105 				      unsigned int coef)
106 {
107 	struct cs_spec *spec = codec->spec;
108 
109 	snd_hda_codec_write(codec, spec->vendor_nid, 0,
110 			    AC_VERB_SET_COEF_INDEX, idx);
111 	snd_hda_codec_write(codec, spec->vendor_nid, 0,
112 			    AC_VERB_SET_PROC_COEF, coef);
113 }
114 
115 /*
116  * auto-mute and auto-mic switching
117  * CS421x auto-output redirecting
118  * HP/SPK/SPDIF
119  */
120 
cs_automute(struct hda_codec * codec)121 static void cs_automute(struct hda_codec *codec)
122 {
123 	struct cs_spec *spec = codec->spec;
124 
125 	snd_hda_gen_update_outputs(codec);
126 
127 	if (spec->gpio_eapd_hp || spec->gpio_eapd_speaker) {
128 		if (spec->gen.automute_speaker)
129 			spec->gpio_data = spec->gen.hp_jack_present ?
130 				spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
131 		else
132 			spec->gpio_data =
133 				spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
134 		snd_hda_codec_write(codec, 0x01, 0,
135 				    AC_VERB_SET_GPIO_DATA, spec->gpio_data);
136 	}
137 }
138 
is_active_pin(struct hda_codec * codec,hda_nid_t nid)139 static bool is_active_pin(struct hda_codec *codec, hda_nid_t nid)
140 {
141 	unsigned int val;
142 
143 	val = snd_hda_codec_get_pincfg(codec, nid);
144 	return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
145 }
146 
init_input_coef(struct hda_codec * codec)147 static void init_input_coef(struct hda_codec *codec)
148 {
149 	struct cs_spec *spec = codec->spec;
150 	unsigned int coef;
151 
152 	/* CS420x has multiple ADC, CS421x has single ADC */
153 	if (spec->vendor_nid == CS420X_VENDOR_NID) {
154 		coef = cs_vendor_coef_get(codec, IDX_BEEP_CFG);
155 		if (is_active_pin(codec, CS_DMIC2_PIN_NID))
156 			coef |= 1 << 4; /* DMIC2 2 chan on, GPIO1 off */
157 		if (is_active_pin(codec, CS_DMIC1_PIN_NID))
158 			coef |= 1 << 3; /* DMIC1 2 chan on, GPIO0 off
159 					 * No effect if SPDIF_OUT2 is
160 					 * selected in IDX_SPDIF_CTL.
161 					 */
162 
163 		cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
164 	}
165 }
166 
167 static const struct hda_verb cs_coef_init_verbs[] = {
168 	{0x11, AC_VERB_SET_PROC_STATE, 1},
169 	{0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
170 	{0x11, AC_VERB_SET_PROC_COEF,
171 	 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
172 	  | 0x0040 /* Mute DACs on FIFO error */
173 	  | 0x1000 /* Enable DACs High Pass Filter */
174 	  | 0x0400 /* Disable Coefficient Auto increment */
175 	  )},
176 	/* ADC1/2 - Digital and Analog Soft Ramp */
177 	{0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
178 	{0x11, AC_VERB_SET_PROC_COEF, 0x000a},
179 	/* Beep */
180 	{0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
181 	{0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
182 
183 	{} /* terminator */
184 };
185 
186 static const struct hda_verb cs4208_coef_init_verbs[] = {
187 	{0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
188 	{0x24, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
189 	{0x24, AC_VERB_SET_COEF_INDEX, 0x0033},
190 	{0x24, AC_VERB_SET_PROC_COEF, 0x0001}, /* A1 ICS */
191 	{0x24, AC_VERB_SET_COEF_INDEX, 0x0034},
192 	{0x24, AC_VERB_SET_PROC_COEF, 0x1C01}, /* A1 Enable, A Thresh = 300mV */
193 	{} /* terminator */
194 };
195 
196 /* Errata: CS4207 rev C0/C1/C2 Silicon
197  *
198  * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
199  *
200  * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
201  * may be excessive (up to an additional 200 μA), which is most easily
202  * observed while the part is being held in reset (RESET# active low).
203  *
204  * Root Cause: At initial powerup of the device, the logic that drives
205  * the clock and write enable to the S/PDIF SRC RAMs is not properly
206  * initialized.
207  * Certain random patterns will cause a steady leakage current in those
208  * RAM cells. The issue will resolve once the SRCs are used (turned on).
209  *
210  * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
211  * blocks, which will alleviate the issue.
212  */
213 
214 static const struct hda_verb cs_errata_init_verbs[] = {
215 	{0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
216 	{0x11, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
217 
218 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
219 	{0x11, AC_VERB_SET_PROC_COEF, 0x9999},
220 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
221 	{0x11, AC_VERB_SET_PROC_COEF, 0xa412},
222 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
223 	{0x11, AC_VERB_SET_PROC_COEF, 0x0009},
224 
225 	{0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
226 	{0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
227 
228 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
229 	{0x11, AC_VERB_SET_PROC_COEF, 0x2412},
230 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
231 	{0x11, AC_VERB_SET_PROC_COEF, 0x0000},
232 	{0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
233 	{0x11, AC_VERB_SET_PROC_COEF, 0x0008},
234 	{0x11, AC_VERB_SET_PROC_STATE, 0x00},
235 	{} /* terminator */
236 };
237 
238 /* SPDIF setup */
init_digital_coef(struct hda_codec * codec)239 static void init_digital_coef(struct hda_codec *codec)
240 {
241 	unsigned int coef;
242 
243 	coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
244 	coef |= 0x0008; /* Replace with mute on error */
245 	if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
246 		coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
247 				 * SPDIF_OUT2 is shared with GPIO1 and
248 				 * DMIC_SDA2.
249 				 */
250 	cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
251 }
252 
cs_init(struct hda_codec * codec)253 static int cs_init(struct hda_codec *codec)
254 {
255 	struct cs_spec *spec = codec->spec;
256 
257 	if (spec->vendor_nid == CS420X_VENDOR_NID) {
258 		/* init_verb sequence for C0/C1/C2 errata*/
259 		snd_hda_sequence_write(codec, cs_errata_init_verbs);
260 		snd_hda_sequence_write(codec, cs_coef_init_verbs);
261 	} else if (spec->vendor_nid == CS4208_VENDOR_NID) {
262 		snd_hda_sequence_write(codec, cs4208_coef_init_verbs);
263 	}
264 
265 	snd_hda_gen_init(codec);
266 
267 	if (spec->gpio_mask) {
268 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
269 				    spec->gpio_mask);
270 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
271 				    spec->gpio_dir);
272 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
273 				    spec->gpio_data);
274 	}
275 
276 	if (spec->vendor_nid == CS420X_VENDOR_NID) {
277 		init_input_coef(codec);
278 		init_digital_coef(codec);
279 	}
280 
281 	return 0;
282 }
283 
cs_build_controls(struct hda_codec * codec)284 static int cs_build_controls(struct hda_codec *codec)
285 {
286 	int err;
287 
288 	err = snd_hda_gen_build_controls(codec);
289 	if (err < 0)
290 		return err;
291 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
292 	return 0;
293 }
294 
cs_parse_auto_config(struct hda_codec * codec)295 static int cs_parse_auto_config(struct hda_codec *codec)
296 {
297 	struct cs_spec *spec = codec->spec;
298 	int err;
299 	int i;
300 
301 	err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
302 	if (err < 0)
303 		return err;
304 
305 	err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
306 	if (err < 0)
307 		return err;
308 
309 	/* keep the ADCs powered up when it's dynamically switchable */
310 	if (spec->gen.dyn_adc_switch) {
311 		unsigned int done = 0;
312 
313 		for (i = 0; i < spec->gen.input_mux.num_items; i++) {
314 			int idx = spec->gen.dyn_adc_idx[i];
315 
316 			if (done & (1 << idx))
317 				continue;
318 			snd_hda_gen_fix_pin_power(codec,
319 						  spec->gen.adc_nids[idx]);
320 			done |= 1 << idx;
321 		}
322 	}
323 
324 	return 0;
325 }
326 
327 static const struct hda_model_fixup cs420x_models[] = {
328 	{ .id = CS420X_MBP53, .name = "mbp53" },
329 	{ .id = CS420X_MBP55, .name = "mbp55" },
330 	{ .id = CS420X_IMAC27, .name = "imac27" },
331 	{ .id = CS420X_IMAC27_122, .name = "imac27_122" },
332 	{ .id = CS420X_APPLE, .name = "apple" },
333 	{ .id = CS420X_MBP101, .name = "mbp101" },
334 	{ .id = CS420X_MBP81, .name = "mbp81" },
335 	{ .id = CS420X_MBA42, .name = "mba42" },
336 	{}
337 };
338 
339 static const struct hda_quirk cs420x_fixup_tbl[] = {
340 	SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
341 	SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
342 	SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
343 	SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
344 	/* this conflicts with too many other models */
345 	/*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
346 
347 	/* codec SSID */
348 	SND_PCI_QUIRK(0x106b, 0x0600, "iMac 14,1", CS420X_IMAC27_122),
349 	SND_PCI_QUIRK(0x106b, 0x0900, "iMac 12,1", CS420X_IMAC27_122),
350 	SND_PCI_QUIRK(0x106b, 0x1c00, "MacBookPro 8,1", CS420X_MBP81),
351 	SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
352 	SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
353 	SND_PCI_QUIRK(0x106b, 0x5600, "MacBookAir 5,2", CS420X_MBP81),
354 	SND_PCI_QUIRK(0x106b, 0x5b00, "MacBookAir 4,2", CS420X_MBA42),
355 	SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
356 	{} /* terminator */
357 };
358 
359 static const struct hda_pintbl mbp53_pincfgs[] = {
360 	{ 0x09, 0x012b4050 },
361 	{ 0x0a, 0x90100141 },
362 	{ 0x0b, 0x90100140 },
363 	{ 0x0c, 0x018b3020 },
364 	{ 0x0d, 0x90a00110 },
365 	{ 0x0e, 0x400000f0 },
366 	{ 0x0f, 0x01cbe030 },
367 	{ 0x10, 0x014be060 },
368 	{ 0x12, 0x400000f0 },
369 	{ 0x15, 0x400000f0 },
370 	{} /* terminator */
371 };
372 
373 static const struct hda_pintbl mbp55_pincfgs[] = {
374 	{ 0x09, 0x012b4030 },
375 	{ 0x0a, 0x90100121 },
376 	{ 0x0b, 0x90100120 },
377 	{ 0x0c, 0x400000f0 },
378 	{ 0x0d, 0x90a00110 },
379 	{ 0x0e, 0x400000f0 },
380 	{ 0x0f, 0x400000f0 },
381 	{ 0x10, 0x014be040 },
382 	{ 0x12, 0x400000f0 },
383 	{ 0x15, 0x400000f0 },
384 	{} /* terminator */
385 };
386 
387 static const struct hda_pintbl imac27_pincfgs[] = {
388 	{ 0x09, 0x012b4050 },
389 	{ 0x0a, 0x90100140 },
390 	{ 0x0b, 0x90100142 },
391 	{ 0x0c, 0x018b3020 },
392 	{ 0x0d, 0x90a00110 },
393 	{ 0x0e, 0x400000f0 },
394 	{ 0x0f, 0x01cbe030 },
395 	{ 0x10, 0x014be060 },
396 	{ 0x12, 0x01ab9070 },
397 	{ 0x15, 0x400000f0 },
398 	{} /* terminator */
399 };
400 
401 static const struct hda_pintbl mbp101_pincfgs[] = {
402 	{ 0x0d, 0x40ab90f0 },
403 	{ 0x0e, 0x90a600f0 },
404 	{ 0x12, 0x50a600f0 },
405 	{} /* terminator */
406 };
407 
408 static const struct hda_pintbl mba42_pincfgs[] = {
409 	{ 0x09, 0x012b4030 }, /* HP */
410 	{ 0x0a, 0x400000f0 },
411 	{ 0x0b, 0x90100120 }, /* speaker */
412 	{ 0x0c, 0x400000f0 },
413 	{ 0x0d, 0x90a00110 }, /* mic */
414 	{ 0x0e, 0x400000f0 },
415 	{ 0x0f, 0x400000f0 },
416 	{ 0x10, 0x400000f0 },
417 	{ 0x12, 0x400000f0 },
418 	{ 0x15, 0x400000f0 },
419 	{} /* terminator */
420 };
421 
422 static const struct hda_pintbl mba6_pincfgs[] = {
423 	{ 0x10, 0x032120f0 }, /* HP */
424 	{ 0x11, 0x500000f0 },
425 	{ 0x12, 0x90100010 }, /* Speaker */
426 	{ 0x13, 0x500000f0 },
427 	{ 0x14, 0x500000f0 },
428 	{ 0x15, 0x770000f0 },
429 	{ 0x16, 0x770000f0 },
430 	{ 0x17, 0x430000f0 },
431 	{ 0x18, 0x43ab9030 }, /* Mic */
432 	{ 0x19, 0x770000f0 },
433 	{ 0x1a, 0x770000f0 },
434 	{ 0x1b, 0x770000f0 },
435 	{ 0x1c, 0x90a00090 },
436 	{ 0x1d, 0x500000f0 },
437 	{ 0x1e, 0x500000f0 },
438 	{ 0x1f, 0x500000f0 },
439 	{ 0x20, 0x500000f0 },
440 	{ 0x21, 0x430000f0 },
441 	{ 0x22, 0x430000f0 },
442 	{} /* terminator */
443 };
444 
cs420x_fixup_gpio_13(struct hda_codec * codec,const struct hda_fixup * fix,int action)445 static void cs420x_fixup_gpio_13(struct hda_codec *codec,
446 				 const struct hda_fixup *fix, int action)
447 {
448 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
449 		struct cs_spec *spec = codec->spec;
450 
451 		spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
452 		spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
453 		spec->gpio_mask = spec->gpio_dir =
454 			spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
455 	}
456 }
457 
cs420x_fixup_gpio_23(struct hda_codec * codec,const struct hda_fixup * fix,int action)458 static void cs420x_fixup_gpio_23(struct hda_codec *codec,
459 				 const struct hda_fixup *fix, int action)
460 {
461 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
462 		struct cs_spec *spec = codec->spec;
463 
464 		spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
465 		spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
466 		spec->gpio_mask = spec->gpio_dir =
467 			spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
468 	}
469 }
470 
471 static const struct hda_fixup cs420x_fixups[] = {
472 	[CS420X_MBP53] = {
473 		.type = HDA_FIXUP_PINS,
474 		.v.pins = mbp53_pincfgs,
475 		.chained = true,
476 		.chain_id = CS420X_APPLE,
477 	},
478 	[CS420X_MBP55] = {
479 		.type = HDA_FIXUP_PINS,
480 		.v.pins = mbp55_pincfgs,
481 		.chained = true,
482 		.chain_id = CS420X_GPIO_13,
483 	},
484 	[CS420X_IMAC27] = {
485 		.type = HDA_FIXUP_PINS,
486 		.v.pins = imac27_pincfgs,
487 		.chained = true,
488 		.chain_id = CS420X_GPIO_13,
489 	},
490 	[CS420X_GPIO_13] = {
491 		.type = HDA_FIXUP_FUNC,
492 		.v.func = cs420x_fixup_gpio_13,
493 	},
494 	[CS420X_GPIO_23] = {
495 		.type = HDA_FIXUP_FUNC,
496 		.v.func = cs420x_fixup_gpio_23,
497 	},
498 	[CS420X_MBP101] = {
499 		.type = HDA_FIXUP_PINS,
500 		.v.pins = mbp101_pincfgs,
501 		.chained = true,
502 		.chain_id = CS420X_GPIO_13,
503 	},
504 	[CS420X_MBP81] = {
505 		.type = HDA_FIXUP_VERBS,
506 		.v.verbs = (const struct hda_verb[]) {
507 			/* internal mic ADC2: right only, single ended */
508 			{0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
509 			{0x11, AC_VERB_SET_PROC_COEF, 0x102a},
510 			{}
511 		},
512 		.chained = true,
513 		.chain_id = CS420X_GPIO_13,
514 	},
515 	[CS420X_MBA42] = {
516 		.type = HDA_FIXUP_PINS,
517 		.v.pins = mba42_pincfgs,
518 		.chained = true,
519 		.chain_id = CS420X_GPIO_13,
520 	},
521 };
522 
cs_alloc_spec(struct hda_codec * codec,int vendor_nid)523 static struct cs_spec *cs_alloc_spec(struct hda_codec *codec, int vendor_nid)
524 {
525 	struct cs_spec *spec;
526 
527 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
528 	if (!spec)
529 		return NULL;
530 	codec->spec = spec;
531 	spec->vendor_nid = vendor_nid;
532 	codec->power_save_node = 1;
533 	snd_hda_gen_spec_init(&spec->gen);
534 
535 	return spec;
536 }
537 
cs420x_probe(struct hda_codec * codec)538 static int cs420x_probe(struct hda_codec *codec)
539 {
540 	int err;
541 
542 	codec->single_adc_amp = 1;
543 
544 	snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
545 			   cs420x_fixups);
546 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
547 
548 	err = cs_parse_auto_config(codec);
549 	if (err < 0)
550 		return err;
551 
552 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
553 
554 	return 0;
555 }
556 
557 /*
558  * CS4208 support:
559  * Its layout is no longer compatible with CS4206/CS4207
560  */
561 enum {
562 	CS4208_MAC_AUTO,
563 	CS4208_MBA6,
564 	CS4208_MBP11,
565 	CS4208_MACMINI,
566 	CS4208_GPIO0,
567 };
568 
569 static const struct hda_model_fixup cs4208_models[] = {
570 	{ .id = CS4208_GPIO0, .name = "gpio0" },
571 	{ .id = CS4208_MBA6, .name = "mba6" },
572 	{ .id = CS4208_MBP11, .name = "mbp11" },
573 	{ .id = CS4208_MACMINI, .name = "macmini" },
574 	{}
575 };
576 
577 static const struct hda_quirk cs4208_fixup_tbl[] = {
578 	SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS4208_MAC_AUTO),
579 	{} /* terminator */
580 };
581 
582 /* codec SSID matching */
583 static const struct hda_quirk cs4208_mac_fixup_tbl[] = {
584 	SND_PCI_QUIRK(0x106b, 0x5e00, "MacBookPro 11,2", CS4208_MBP11),
585 	SND_PCI_QUIRK(0x106b, 0x6c00, "MacMini 7,1", CS4208_MACMINI),
586 	SND_PCI_QUIRK(0x106b, 0x7100, "MacBookAir 6,1", CS4208_MBA6),
587 	SND_PCI_QUIRK(0x106b, 0x7200, "MacBookAir 6,2", CS4208_MBA6),
588 	SND_PCI_QUIRK(0x106b, 0x7b00, "MacBookPro 12,1", CS4208_MBP11),
589 	{} /* terminator */
590 };
591 
cs4208_fixup_gpio0(struct hda_codec * codec,const struct hda_fixup * fix,int action)592 static void cs4208_fixup_gpio0(struct hda_codec *codec,
593 			       const struct hda_fixup *fix, int action)
594 {
595 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
596 		struct cs_spec *spec = codec->spec;
597 
598 		spec->gpio_eapd_hp = 0;
599 		spec->gpio_eapd_speaker = 1;
600 		spec->gpio_mask = spec->gpio_dir =
601 			spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
602 	}
603 }
604 
605 static const struct hda_fixup cs4208_fixups[];
606 
607 /* remap the fixup from codec SSID and apply it */
cs4208_fixup_mac(struct hda_codec * codec,const struct hda_fixup * fix,int action)608 static void cs4208_fixup_mac(struct hda_codec *codec,
609 			     const struct hda_fixup *fix, int action)
610 {
611 	if (action != HDA_FIXUP_ACT_PRE_PROBE)
612 		return;
613 
614 	codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
615 	snd_hda_pick_fixup(codec, NULL, cs4208_mac_fixup_tbl, cs4208_fixups);
616 	if (codec->fixup_id == HDA_FIXUP_ID_NOT_SET)
617 		codec->fixup_id = CS4208_GPIO0; /* default fixup */
618 	snd_hda_apply_fixup(codec, action);
619 }
620 
621 /* MacMini 7,1 has the inverted jack detection */
cs4208_fixup_macmini(struct hda_codec * codec,const struct hda_fixup * fix,int action)622 static void cs4208_fixup_macmini(struct hda_codec *codec,
623 				 const struct hda_fixup *fix, int action)
624 {
625 	static const struct hda_pintbl pincfgs[] = {
626 		{ 0x18, 0x00ab9150 }, /* mic (audio-in) jack: disable detect */
627 		{ 0x21, 0x004be140 }, /* SPDIF: disable detect */
628 		{ }
629 	};
630 
631 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
632 		/* HP pin (0x10) has an inverted detection */
633 		codec->inv_jack_detect = 1;
634 		/* disable the bogus Mic and SPDIF jack detections */
635 		snd_hda_apply_pincfgs(codec, pincfgs);
636 	}
637 }
638 
cs4208_spdif_sw_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)639 static int cs4208_spdif_sw_put(struct snd_kcontrol *kcontrol,
640 			       struct snd_ctl_elem_value *ucontrol)
641 {
642 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
643 	struct cs_spec *spec = codec->spec;
644 	hda_nid_t pin = spec->gen.autocfg.dig_out_pins[0];
645 	int pinctl = ucontrol->value.integer.value[0] ? PIN_OUT : 0;
646 
647 	snd_hda_set_pin_ctl_cache(codec, pin, pinctl);
648 	return spec->spdif_sw_put(kcontrol, ucontrol);
649 }
650 
651 /* hook the SPDIF switch */
cs4208_fixup_spdif_switch(struct hda_codec * codec,const struct hda_fixup * fix,int action)652 static void cs4208_fixup_spdif_switch(struct hda_codec *codec,
653 				      const struct hda_fixup *fix, int action)
654 {
655 	if (action == HDA_FIXUP_ACT_BUILD) {
656 		struct cs_spec *spec = codec->spec;
657 		struct snd_kcontrol *kctl;
658 
659 		if (!spec->gen.autocfg.dig_out_pins[0])
660 			return;
661 		kctl = snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch");
662 		if (!kctl)
663 			return;
664 		spec->spdif_sw_put = kctl->put;
665 		kctl->put = cs4208_spdif_sw_put;
666 	}
667 }
668 
669 static const struct hda_fixup cs4208_fixups[] = {
670 	[CS4208_MBA6] = {
671 		.type = HDA_FIXUP_PINS,
672 		.v.pins = mba6_pincfgs,
673 		.chained = true,
674 		.chain_id = CS4208_GPIO0,
675 	},
676 	[CS4208_MBP11] = {
677 		.type = HDA_FIXUP_FUNC,
678 		.v.func = cs4208_fixup_spdif_switch,
679 		.chained = true,
680 		.chain_id = CS4208_GPIO0,
681 	},
682 	[CS4208_MACMINI] = {
683 		.type = HDA_FIXUP_FUNC,
684 		.v.func = cs4208_fixup_macmini,
685 		.chained = true,
686 		.chain_id = CS4208_GPIO0,
687 	},
688 	[CS4208_GPIO0] = {
689 		.type = HDA_FIXUP_FUNC,
690 		.v.func = cs4208_fixup_gpio0,
691 	},
692 	[CS4208_MAC_AUTO] = {
693 		.type = HDA_FIXUP_FUNC,
694 		.v.func = cs4208_fixup_mac,
695 	},
696 };
697 
698 /* correct the 0dB offset of input pins */
cs4208_fix_amp_caps(struct hda_codec * codec,hda_nid_t adc)699 static void cs4208_fix_amp_caps(struct hda_codec *codec, hda_nid_t adc)
700 {
701 	unsigned int caps;
702 
703 	caps = query_amp_caps(codec, adc, HDA_INPUT);
704 	caps &= ~(AC_AMPCAP_OFFSET);
705 	caps |= 0x02;
706 	snd_hda_override_amp_caps(codec, adc, HDA_INPUT, caps);
707 }
708 
cs4208_probe(struct hda_codec * codec)709 static int cs4208_probe(struct hda_codec *codec)
710 {
711 	struct cs_spec *spec = codec->spec;
712 	int err;
713 
714 	/* exclude NID 0x10 (HP) from output volumes due to different steps */
715 	spec->gen.out_vol_mask = 1ULL << 0x10;
716 
717 	snd_hda_pick_fixup(codec, cs4208_models, cs4208_fixup_tbl,
718 			   cs4208_fixups);
719 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
720 
721 	snd_hda_override_wcaps(codec, 0x18,
722 			       get_wcaps(codec, 0x18) | AC_WCAP_STEREO);
723 	cs4208_fix_amp_caps(codec, 0x18);
724 	cs4208_fix_amp_caps(codec, 0x1b);
725 	cs4208_fix_amp_caps(codec, 0x1c);
726 
727 	err = cs_parse_auto_config(codec);
728 	if (err < 0)
729 		return err;
730 
731 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
732 
733 	return 0;
734 }
735 
cs_codec_probe(struct hda_codec * codec,const struct hda_device_id * id)736 static int cs_codec_probe(struct hda_codec *codec,
737 			  const struct hda_device_id *id)
738 {
739 	struct cs_spec *spec;
740 	int err;
741 
742 	spec = cs_alloc_spec(codec, id->driver_data);
743 	if (!spec)
744 		return -ENOMEM;
745 	spec->gen.automute_hook = cs_automute;
746 
747 	if (spec->vendor_nid == CS4208_VENDOR_NID)
748 		err = cs4208_probe(codec);
749 	else
750 		err = cs420x_probe(codec);
751 	if (err < 0)
752 		snd_hda_gen_remove(codec);
753 	return err;
754 }
755 
756 static const struct hda_codec_ops cs_codec_ops = {
757 	.probe = cs_codec_probe,
758 	.remove = snd_hda_gen_remove,
759 	.build_controls = cs_build_controls,
760 	.build_pcms = snd_hda_gen_build_pcms,
761 	.init = cs_init,
762 	.unsol_event = snd_hda_jack_unsol_event,
763 	.stream_pm = snd_hda_gen_stream_pm,
764 };
765 
766 /*
767  * driver entries
768  */
769 static const struct hda_device_id snd_hda_id_cs420x[] = {
770 	HDA_CODEC_ID_MODEL(0x10134206, "CS4206", CS420X_VENDOR_NID),
771 	HDA_CODEC_ID_MODEL(0x10134207, "CS4207", CS420X_VENDOR_NID),
772 	HDA_CODEC_ID_MODEL(0x10134208, "CS4208", CS4208_VENDOR_NID),
773 	{} /* terminator */
774 };
775 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_cs420x);
776 
777 MODULE_LICENSE("GPL");
778 MODULE_DESCRIPTION("Cirrus Logic CS420x HD-audio codec");
779 
780 static struct hda_codec_driver cs420x_driver = {
781 	.id = snd_hda_id_cs420x,
782 	.ops = &cs_codec_ops,
783 };
784 
785 module_hda_codec_driver(cs420x_driver);
786