1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HD audio interface patch for Cirrus Logic CS420x chip
5 * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
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"
17 #include "hda_generic.h"
23 struct hda_gen_spec gen;
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 */
32 unsigned int spdif_detect:1;
33 unsigned int spdif_present:1;
34 unsigned int sense_b:1;
37 /* for MBP SPDIF control */
38 int (*spdif_sw_put)(struct snd_kcontrol *kcontrol,
39 struct snd_ctl_elem_value *ucontrol);
42 /* available models with CS420x */
54 CS420X_IMAC27_122 = CS420X_GPIO_23,
55 CS420X_APPLE = CS420X_GPIO_13,
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
73 #define IDX_SPDIF_STAT 0x0000
74 #define IDX_SPDIF_CTL 0x0001
75 #define IDX_ADC_CFG 0x0002
76 /* SZC bitmask, 4 modes below:
78 * 1 = digital immediate, analog zero-cross
79 * 2 = digtail & analog soft-ramp
80 * 3 = digital soft-ramp, analog zero-cross
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:
93 * 3 = soft-ramp on zero-cross
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 */
99 #define IDX_BEEP_CFG 0x0004
100 /* 0x0008 - test reg key */
101 /* 0x0009 - 0x0014 -> 12 test regs */
102 /* 0x0015 - visibility reg */
104 /* Cirrus Logic CS4208 */
105 #define CS4208_VENDOR_NID 0x24
108 * Cirrus Logic CS4210
110 * 1 DAC => HP(sense) / Speakers,
111 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
112 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
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 */
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
125 /* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
126 #define CS4213_VENDOR_NID 0x09
129 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
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);
138 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
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);
149 * auto-mute and auto-mic switching
150 * CS421x auto-output redirecting
154 static void cs_automute(struct hda_codec *codec)
156 struct cs_spec *spec = codec->spec;
158 /* mute HPs if spdif jack (SENSE_B) is present */
159 spec->gen.master_mute = !!(spec->spdif_present && spec->sense_b);
161 snd_hda_gen_update_outputs(codec);
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;
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);
175 static bool is_active_pin(struct hda_codec *codec, hda_nid_t nid)
178 val = snd_hda_codec_get_pincfg(codec, nid);
179 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
182 static void init_input_coef(struct hda_codec *codec)
184 struct cs_spec *spec = codec->spec;
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.
198 cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
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 */
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},
215 {0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
216 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
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 */
231 /* Errata: CS4207 rev C0/C1/C2 Silicon
233 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
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).
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
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).
245 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
246 * blocks, which will alleviate the issue.
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 */
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},
260 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
261 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
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},
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 */
281 static void init_digital_coef(struct hda_codec *codec)
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
292 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
295 static int cs_init(struct hda_codec *codec)
297 struct cs_spec *spec = codec->spec;
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);
307 snd_hda_gen_init(codec);
309 if (spec->gpio_mask) {
310 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
312 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
314 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
318 if (spec->vendor_nid == CS420X_VENDOR_NID) {
319 init_input_coef(codec);
320 init_digital_coef(codec);
326 static int cs_build_controls(struct hda_codec *codec)
330 err = snd_hda_gen_build_controls(codec);
333 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
337 #define cs_free snd_hda_gen_free
339 static const struct hda_codec_ops cs_patch_ops = {
340 .build_controls = cs_build_controls,
341 .build_pcms = snd_hda_gen_build_pcms,
344 .unsol_event = snd_hda_jack_unsol_event,
347 static int cs_parse_auto_config(struct hda_codec *codec)
349 struct cs_spec *spec = codec->spec;
353 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
357 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
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))
368 snd_hda_gen_fix_pin_power(codec,
369 spec->gen.adc_nids[idx]);
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" },
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),*/
398 SND_PCI_QUIRK(0x106b, 0x0600, "iMac 14,1", CS420X_IMAC27_122),
399 SND_PCI_QUIRK(0x106b, 0x1c00, "MacBookPro 8,1", CS420X_MBP81),
400 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
401 SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
402 SND_PCI_QUIRK(0x106b, 0x5600, "MacBookAir 5,2", CS420X_MBP81),
403 SND_PCI_QUIRK(0x106b, 0x5b00, "MacBookAir 4,2", CS420X_MBA42),
404 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
408 static const struct hda_pintbl mbp53_pincfgs[] = {
409 { 0x09, 0x012b4050 },
410 { 0x0a, 0x90100141 },
411 { 0x0b, 0x90100140 },
412 { 0x0c, 0x018b3020 },
413 { 0x0d, 0x90a00110 },
414 { 0x0e, 0x400000f0 },
415 { 0x0f, 0x01cbe030 },
416 { 0x10, 0x014be060 },
417 { 0x12, 0x400000f0 },
418 { 0x15, 0x400000f0 },
422 static const struct hda_pintbl mbp55_pincfgs[] = {
423 { 0x09, 0x012b4030 },
424 { 0x0a, 0x90100121 },
425 { 0x0b, 0x90100120 },
426 { 0x0c, 0x400000f0 },
427 { 0x0d, 0x90a00110 },
428 { 0x0e, 0x400000f0 },
429 { 0x0f, 0x400000f0 },
430 { 0x10, 0x014be040 },
431 { 0x12, 0x400000f0 },
432 { 0x15, 0x400000f0 },
436 static const struct hda_pintbl imac27_pincfgs[] = {
437 { 0x09, 0x012b4050 },
438 { 0x0a, 0x90100140 },
439 { 0x0b, 0x90100142 },
440 { 0x0c, 0x018b3020 },
441 { 0x0d, 0x90a00110 },
442 { 0x0e, 0x400000f0 },
443 { 0x0f, 0x01cbe030 },
444 { 0x10, 0x014be060 },
445 { 0x12, 0x01ab9070 },
446 { 0x15, 0x400000f0 },
450 static const struct hda_pintbl mbp101_pincfgs[] = {
451 { 0x0d, 0x40ab90f0 },
452 { 0x0e, 0x90a600f0 },
453 { 0x12, 0x50a600f0 },
457 static const struct hda_pintbl mba42_pincfgs[] = {
458 { 0x09, 0x012b4030 }, /* HP */
459 { 0x0a, 0x400000f0 },
460 { 0x0b, 0x90100120 }, /* speaker */
461 { 0x0c, 0x400000f0 },
462 { 0x0d, 0x90a00110 }, /* mic */
463 { 0x0e, 0x400000f0 },
464 { 0x0f, 0x400000f0 },
465 { 0x10, 0x400000f0 },
466 { 0x12, 0x400000f0 },
467 { 0x15, 0x400000f0 },
471 static const struct hda_pintbl mba6_pincfgs[] = {
472 { 0x10, 0x032120f0 }, /* HP */
473 { 0x11, 0x500000f0 },
474 { 0x12, 0x90100010 }, /* Speaker */
475 { 0x13, 0x500000f0 },
476 { 0x14, 0x500000f0 },
477 { 0x15, 0x770000f0 },
478 { 0x16, 0x770000f0 },
479 { 0x17, 0x430000f0 },
480 { 0x18, 0x43ab9030 }, /* Mic */
481 { 0x19, 0x770000f0 },
482 { 0x1a, 0x770000f0 },
483 { 0x1b, 0x770000f0 },
484 { 0x1c, 0x90a00090 },
485 { 0x1d, 0x500000f0 },
486 { 0x1e, 0x500000f0 },
487 { 0x1f, 0x500000f0 },
488 { 0x20, 0x500000f0 },
489 { 0x21, 0x430000f0 },
490 { 0x22, 0x430000f0 },
494 static void cs420x_fixup_gpio_13(struct hda_codec *codec,
495 const struct hda_fixup *fix, int action)
497 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
498 struct cs_spec *spec = codec->spec;
499 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
500 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
501 spec->gpio_mask = spec->gpio_dir =
502 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
506 static void cs420x_fixup_gpio_23(struct hda_codec *codec,
507 const struct hda_fixup *fix, int action)
509 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
510 struct cs_spec *spec = codec->spec;
511 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
512 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
513 spec->gpio_mask = spec->gpio_dir =
514 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
518 static const struct hda_fixup cs420x_fixups[] = {
520 .type = HDA_FIXUP_PINS,
521 .v.pins = mbp53_pincfgs,
523 .chain_id = CS420X_APPLE,
526 .type = HDA_FIXUP_PINS,
527 .v.pins = mbp55_pincfgs,
529 .chain_id = CS420X_GPIO_13,
532 .type = HDA_FIXUP_PINS,
533 .v.pins = imac27_pincfgs,
535 .chain_id = CS420X_GPIO_13,
538 .type = HDA_FIXUP_FUNC,
539 .v.func = cs420x_fixup_gpio_13,
542 .type = HDA_FIXUP_FUNC,
543 .v.func = cs420x_fixup_gpio_23,
546 .type = HDA_FIXUP_PINS,
547 .v.pins = mbp101_pincfgs,
549 .chain_id = CS420X_GPIO_13,
552 .type = HDA_FIXUP_VERBS,
553 .v.verbs = (const struct hda_verb[]) {
554 /* internal mic ADC2: right only, single ended */
555 {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
556 {0x11, AC_VERB_SET_PROC_COEF, 0x102a},
560 .chain_id = CS420X_GPIO_13,
563 .type = HDA_FIXUP_PINS,
564 .v.pins = mba42_pincfgs,
566 .chain_id = CS420X_GPIO_13,
570 static struct cs_spec *cs_alloc_spec(struct hda_codec *codec, int vendor_nid)
572 struct cs_spec *spec;
574 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
578 spec->vendor_nid = vendor_nid;
579 codec->power_save_node = 1;
580 snd_hda_gen_spec_init(&spec->gen);
585 static int patch_cs420x(struct hda_codec *codec)
587 struct cs_spec *spec;
590 spec = cs_alloc_spec(codec, CS420X_VENDOR_NID);
594 codec->patch_ops = cs_patch_ops;
595 spec->gen.automute_hook = cs_automute;
596 codec->single_adc_amp = 1;
598 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
600 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
602 err = cs_parse_auto_config(codec);
606 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
617 * Its layout is no longer compatible with CS4206/CS4207
627 static const struct hda_model_fixup cs4208_models[] = {
628 { .id = CS4208_GPIO0, .name = "gpio0" },
629 { .id = CS4208_MBA6, .name = "mba6" },
630 { .id = CS4208_MBP11, .name = "mbp11" },
631 { .id = CS4208_MACMINI, .name = "macmini" },
635 static const struct snd_pci_quirk cs4208_fixup_tbl[] = {
636 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS4208_MAC_AUTO),
640 /* codec SSID matching */
641 static const struct snd_pci_quirk cs4208_mac_fixup_tbl[] = {
642 SND_PCI_QUIRK(0x106b, 0x5e00, "MacBookPro 11,2", CS4208_MBP11),
643 SND_PCI_QUIRK(0x106b, 0x6c00, "MacMini 7,1", CS4208_MACMINI),
644 SND_PCI_QUIRK(0x106b, 0x7100, "MacBookAir 6,1", CS4208_MBA6),
645 SND_PCI_QUIRK(0x106b, 0x7200, "MacBookAir 6,2", CS4208_MBA6),
646 SND_PCI_QUIRK(0x106b, 0x7b00, "MacBookPro 12,1", CS4208_MBP11),
650 static void cs4208_fixup_gpio0(struct hda_codec *codec,
651 const struct hda_fixup *fix, int action)
653 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
654 struct cs_spec *spec = codec->spec;
655 spec->gpio_eapd_hp = 0;
656 spec->gpio_eapd_speaker = 1;
657 spec->gpio_mask = spec->gpio_dir =
658 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
662 static const struct hda_fixup cs4208_fixups[];
664 /* remap the fixup from codec SSID and apply it */
665 static void cs4208_fixup_mac(struct hda_codec *codec,
666 const struct hda_fixup *fix, int action)
668 if (action != HDA_FIXUP_ACT_PRE_PROBE)
671 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
672 snd_hda_pick_fixup(codec, NULL, cs4208_mac_fixup_tbl, cs4208_fixups);
673 if (codec->fixup_id == HDA_FIXUP_ID_NOT_SET)
674 codec->fixup_id = CS4208_GPIO0; /* default fixup */
675 snd_hda_apply_fixup(codec, action);
678 /* MacMini 7,1 has the inverted jack detection */
679 static void cs4208_fixup_macmini(struct hda_codec *codec,
680 const struct hda_fixup *fix, int action)
682 static const struct hda_pintbl pincfgs[] = {
683 { 0x18, 0x00ab9150 }, /* mic (audio-in) jack: disable detect */
684 { 0x21, 0x004be140 }, /* SPDIF: disable detect */
688 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
689 /* HP pin (0x10) has an inverted detection */
690 codec->inv_jack_detect = 1;
691 /* disable the bogus Mic and SPDIF jack detections */
692 snd_hda_apply_pincfgs(codec, pincfgs);
696 static int cs4208_spdif_sw_put(struct snd_kcontrol *kcontrol,
697 struct snd_ctl_elem_value *ucontrol)
699 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
700 struct cs_spec *spec = codec->spec;
701 hda_nid_t pin = spec->gen.autocfg.dig_out_pins[0];
702 int pinctl = ucontrol->value.integer.value[0] ? PIN_OUT : 0;
704 snd_hda_set_pin_ctl_cache(codec, pin, pinctl);
705 return spec->spdif_sw_put(kcontrol, ucontrol);
708 /* hook the SPDIF switch */
709 static void cs4208_fixup_spdif_switch(struct hda_codec *codec,
710 const struct hda_fixup *fix, int action)
712 if (action == HDA_FIXUP_ACT_BUILD) {
713 struct cs_spec *spec = codec->spec;
714 struct snd_kcontrol *kctl;
716 if (!spec->gen.autocfg.dig_out_pins[0])
718 kctl = snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch");
721 spec->spdif_sw_put = kctl->put;
722 kctl->put = cs4208_spdif_sw_put;
726 static const struct hda_fixup cs4208_fixups[] = {
728 .type = HDA_FIXUP_PINS,
729 .v.pins = mba6_pincfgs,
731 .chain_id = CS4208_GPIO0,
734 .type = HDA_FIXUP_FUNC,
735 .v.func = cs4208_fixup_spdif_switch,
737 .chain_id = CS4208_GPIO0,
740 .type = HDA_FIXUP_FUNC,
741 .v.func = cs4208_fixup_macmini,
743 .chain_id = CS4208_GPIO0,
746 .type = HDA_FIXUP_FUNC,
747 .v.func = cs4208_fixup_gpio0,
749 [CS4208_MAC_AUTO] = {
750 .type = HDA_FIXUP_FUNC,
751 .v.func = cs4208_fixup_mac,
755 /* correct the 0dB offset of input pins */
756 static void cs4208_fix_amp_caps(struct hda_codec *codec, hda_nid_t adc)
760 caps = query_amp_caps(codec, adc, HDA_INPUT);
761 caps &= ~(AC_AMPCAP_OFFSET);
763 snd_hda_override_amp_caps(codec, adc, HDA_INPUT, caps);
766 static int patch_cs4208(struct hda_codec *codec)
768 struct cs_spec *spec;
771 spec = cs_alloc_spec(codec, CS4208_VENDOR_NID);
775 codec->patch_ops = cs_patch_ops;
776 spec->gen.automute_hook = cs_automute;
777 /* exclude NID 0x10 (HP) from output volumes due to different steps */
778 spec->gen.out_vol_mask = 1ULL << 0x10;
780 snd_hda_pick_fixup(codec, cs4208_models, cs4208_fixup_tbl,
782 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
784 snd_hda_override_wcaps(codec, 0x18,
785 get_wcaps(codec, 0x18) | AC_WCAP_STEREO);
786 cs4208_fix_amp_caps(codec, 0x18);
787 cs4208_fix_amp_caps(codec, 0x1b);
788 cs4208_fix_amp_caps(codec, 0x1c);
790 err = cs_parse_auto_config(codec);
794 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
804 * Cirrus Logic CS4210
806 * 1 DAC => HP(sense) / Speakers,
807 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
808 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
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" },
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),
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 },
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 },
847 /* Setup GPIO/SENSE for each board (if used) */
848 static void cs421x_fixup_sense_b(struct hda_codec *codec,
849 const struct hda_fixup *fix, int action)
851 struct cs_spec *spec = codec->spec;
852 if (action == HDA_FIXUP_ACT_PRE_PROBE)
856 static const struct hda_fixup cs421x_fixups[] = {
858 .type = HDA_FIXUP_PINS,
859 .v.pins = cdb4210_pincfgs,
861 .chain_id = CS421X_SENSE_B,
864 .type = HDA_FIXUP_FUNC,
865 .v.func = cs421x_fixup_sense_b,
868 .type = HDA_FIXUP_PINS,
869 .v.pins = stumpy_pincfgs,
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},
877 Disable Coefficient Index Auto-Increment(DAI)=1,
880 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
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 },
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 */
895 /* Errata: CS4210 rev A1 Silicon
897 * http://www.cirrus.com/en/pubs/errata/
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
906 * The following verb sequence written to the registers during
907 * initialization will correct the issues listed above.
910 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
911 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
913 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
914 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
916 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
917 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
919 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
920 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
922 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
923 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
925 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
926 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
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);
934 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
935 struct snd_ctl_elem_info *uinfo)
937 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
939 uinfo->value.integer.min = 0;
940 uinfo->value.integer.max = 3;
944 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
945 struct snd_ctl_elem_value *ucontrol)
947 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
949 ucontrol->value.integer.value[0] =
950 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
954 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
955 struct snd_ctl_elem_value *ucontrol)
957 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
959 unsigned int vol = ucontrol->value.integer.value[0];
961 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
962 unsigned int original_coef = coef;
965 coef |= (vol & 0x0003);
966 if (original_coef == coef)
969 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
974 static const struct snd_kcontrol_new cs421x_speaker_boost_ctl = {
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 },
986 static void cs4210_pinmux_init(struct hda_codec *codec)
988 struct cs_spec *spec = codec->spec;
989 unsigned int def_conf, coef;
991 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
992 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
995 coef |= 0x0008; /* B1,B2 are GPIOs */
1000 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1004 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1006 if ((spec->gpio_mask || spec->sense_b) &&
1007 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1010 GPIO or SENSE_B forced - disconnect the DMIC pin.
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);
1019 static void cs4210_spdif_automute(struct hda_codec *codec,
1020 struct hda_jack_callback *tbl)
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];
1026 /* detect on spdif is specific to CS4210 */
1027 if (!spec->spdif_detect ||
1028 spec->vendor_nid != CS4210_VENDOR_NID)
1031 spdif_present = snd_hda_jack_detect(codec, spdif_pin);
1032 if (spdif_present == spec->spdif_present)
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);
1042 static void parse_cs421x_digital(struct hda_codec *codec)
1044 struct cs_spec *spec = codec->spec;
1045 struct auto_pin_cfg *cfg = &spec->gen.autocfg;
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);
1058 static int cs421x_init(struct hda_codec *codec)
1060 struct cs_spec *spec = codec->spec;
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);
1068 snd_hda_gen_init(codec);
1070 if (spec->gpio_mask) {
1071 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1073 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1075 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1079 init_input_coef(codec);
1081 cs4210_spdif_automute(codec, NULL);
1086 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
1090 /* set the upper-limit for mixer amp to 0dB */
1091 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
1092 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
1093 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
1094 << AC_AMPCAP_NUM_STEPS_SHIFT;
1095 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
1098 static int cs421x_parse_auto_config(struct hda_codec *codec)
1100 struct cs_spec *spec = codec->spec;
1101 hda_nid_t dac = CS4210_DAC_NID;
1104 fix_volume_caps(codec, dac);
1106 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
1110 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
1114 parse_cs421x_digital(codec);
1116 if (spec->gen.autocfg.speaker_outs &&
1117 spec->vendor_nid == CS4210_VENDOR_NID) {
1118 if (!snd_hda_gen_add_kctl(&spec->gen, NULL,
1119 &cs421x_speaker_boost_ctl))
1128 Manage PDREF, when transitioning to D3hot
1129 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1131 static int cs421x_suspend(struct hda_codec *codec)
1133 struct cs_spec *spec = codec->spec;
1136 snd_hda_shutup_pins(codec);
1138 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1139 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1140 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1141 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1143 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1144 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1145 coef |= 0x0004; /* PDREF */
1146 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1153 static const struct hda_codec_ops cs421x_patch_ops = {
1154 .build_controls = snd_hda_gen_build_controls,
1155 .build_pcms = snd_hda_gen_build_pcms,
1156 .init = cs421x_init,
1158 .unsol_event = snd_hda_jack_unsol_event,
1160 .suspend = cs421x_suspend,
1164 static int patch_cs4210(struct hda_codec *codec)
1166 struct cs_spec *spec;
1169 spec = cs_alloc_spec(codec, CS4210_VENDOR_NID);
1173 codec->patch_ops = cs421x_patch_ops;
1174 spec->gen.automute_hook = cs_automute;
1176 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1178 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1181 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1182 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1185 cs4210_pinmux_init(codec);
1187 err = cs421x_parse_auto_config(codec);
1191 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1200 static int patch_cs4213(struct hda_codec *codec)
1202 struct cs_spec *spec;
1205 spec = cs_alloc_spec(codec, CS4213_VENDOR_NID);
1209 codec->patch_ops = cs421x_patch_ops;
1211 err = cs421x_parse_auto_config(codec);
1226 static const struct hda_device_id snd_hda_id_cirrus[] = {
1227 HDA_CODEC_ENTRY(0x10134206, "CS4206", patch_cs420x),
1228 HDA_CODEC_ENTRY(0x10134207, "CS4207", patch_cs420x),
1229 HDA_CODEC_ENTRY(0x10134208, "CS4208", patch_cs4208),
1230 HDA_CODEC_ENTRY(0x10134210, "CS4210", patch_cs4210),
1231 HDA_CODEC_ENTRY(0x10134213, "CS4213", patch_cs4213),
1234 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_cirrus);
1236 MODULE_LICENSE("GPL");
1237 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1239 static struct hda_codec_driver cirrus_driver = {
1240 .id = snd_hda_id_cirrus,
1243 module_hda_codec_driver(cirrus_driver);