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