1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
35struct 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;
42 unsigned int gpio_eapd_speaker;
43
44
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
51 int (*spdif_sw_put)(struct snd_kcontrol *kcontrol,
52 struct snd_ctl_elem_value *ucontrol);
53};
54
55
56enum {
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
67 CS420X_IMAC27_122 = CS420X_GPIO_23,
68 CS420X_APPLE = CS420X_GPIO_13,
69};
70
71
72enum {
73 CS421X_CDB4210,
74 CS421X_SENSE_B,
75 CS421X_STUMPY,
76};
77
78
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
86#define IDX_SPDIF_STAT 0x0000
87#define IDX_SPDIF_CTL 0x0001
88#define IDX_ADC_CFG 0x0002
89
90
91
92
93
94
95#define CS_COEF_ADC_SZC_MASK (3 << 0)
96#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0)
97#define CS_COEF_ADC_LI_SZC_MODE (3 << 0)
98
99#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5)
100#define CS_COEF_ADC_LI_PGA_MODE (1 << 6)
101#define IDX_DAC_CFG 0x0003
102
103
104
105
106
107
108#define CS_COEF_DAC_HP_SZC_MODE (3 << 0)
109#define CS_COEF_DAC_LO_SZC_MODE (3 << 2)
110#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4)
111
112#define IDX_BEEP_CFG 0x0004
113
114
115
116
117
118#define CS4208_VENDOR_NID 0x24
119
120
121
122
123
124
125
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
131#define CS421X_SPDIF_PIN_NID 0x0A
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
139#define CS4213_VENDOR_NID 0x09
140
141
142static 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
151static 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
163
164
165
166
167static void cs_automute(struct hda_codec *codec)
168{
169 struct cs_spec *spec = codec->spec;
170
171
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
188static 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
195static void init_input_coef(struct hda_codec *codec)
196{
197 struct cs_spec *spec = codec->spec;
198 unsigned int coef;
199
200
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;
205 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
206 coef |= 1 << 3;
207
208
209
210
211 cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
212 }
213}
214
215static 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
220 | 0x0040
221 | 0x1000
222 | 0x0400
223 )},
224
225 {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
226 {0x11, AC_VERB_SET_PROC_COEF, 0x000a},
227
228 {0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
229 {0x11, AC_VERB_SET_PROC_COEF, 0x0007},
230
231 {}
232};
233
234static const struct hda_verb cs4208_coef_init_verbs[] = {
235 {0x01, AC_VERB_SET_POWER_STATE, 0x00},
236 {0x24, AC_VERB_SET_PROC_STATE, 0x01},
237 {0x24, AC_VERB_SET_COEF_INDEX, 0x0033},
238 {0x24, AC_VERB_SET_PROC_COEF, 0x0001},
239 {0x24, AC_VERB_SET_COEF_INDEX, 0x0034},
240 {0x24, AC_VERB_SET_PROC_COEF, 0x1C01},
241 {}
242};
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262static const struct hda_verb cs_errata_init_verbs[] = {
263 {0x01, AC_VERB_SET_POWER_STATE, 0x00},
264 {0x11, AC_VERB_SET_PROC_STATE, 0x01},
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},
274 {0x08, AC_VERB_SET_POWER_STATE, 0x00},
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
285 {0x07, AC_VERB_SET_POWER_STATE, 0x03},
286 {0x08, AC_VERB_SET_POWER_STATE, 0x03},
287
288#endif
289
290 {}
291};
292
293
294static void init_digital_coef(struct hda_codec *codec)
295{
296 unsigned int coef;
297
298 coef = 0x0002;
299 coef |= 0x0008;
300 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
301 coef |= 0x4000;
302
303
304
305 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
306}
307
308static 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
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
339static 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
352static 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
360static int cs_parse_auto_config(struct hda_codec *codec)
361{
362 struct cs_spec *spec = codec->spec;
363 int err;
364 int i;
365
366 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
367 if (err < 0)
368 return err;
369
370 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
371 if (err < 0)
372 return err;
373
374
375 if (spec->gen.dyn_adc_switch) {
376 unsigned int done = 0;
377 for (i = 0; i < spec->gen.input_mux.num_items; i++) {
378 int idx = spec->gen.dyn_adc_idx[i];
379 if (done & (1 << idx))
380 continue;
381 snd_hda_gen_fix_pin_power(codec,
382 spec->gen.adc_nids[idx]);
383 done |= 1 << idx;
384 }
385 }
386
387 return 0;
388}
389
390static const struct hda_model_fixup cs420x_models[] = {
391 { .id = CS420X_MBP53, .name = "mbp53" },
392 { .id = CS420X_MBP55, .name = "mbp55" },
393 { .id = CS420X_IMAC27, .name = "imac27" },
394 { .id = CS420X_IMAC27_122, .name = "imac27_122" },
395 { .id = CS420X_APPLE, .name = "apple" },
396 { .id = CS420X_MBP101, .name = "mbp101" },
397 { .id = CS420X_MBP81, .name = "mbp81" },
398 { .id = CS420X_MBA42, .name = "mba42" },
399 {}
400};
401
402static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
403 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
404 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
405 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
406 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
407
408
409
410
411 SND_PCI_QUIRK(0x106b, 0x0600, "iMac 14,1", CS420X_IMAC27_122),
412 SND_PCI_QUIRK(0x106b, 0x1c00, "MacBookPro 8,1", CS420X_MBP81),
413 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
414 SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
415 SND_PCI_QUIRK(0x106b, 0x5600, "MacBookAir 5,2", CS420X_MBP81),
416 SND_PCI_QUIRK(0x106b, 0x5b00, "MacBookAir 4,2", CS420X_MBA42),
417 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
418 {}
419};
420
421static const struct hda_pintbl mbp53_pincfgs[] = {
422 { 0x09, 0x012b4050 },
423 { 0x0a, 0x90100141 },
424 { 0x0b, 0x90100140 },
425 { 0x0c, 0x018b3020 },
426 { 0x0d, 0x90a00110 },
427 { 0x0e, 0x400000f0 },
428 { 0x0f, 0x01cbe030 },
429 { 0x10, 0x014be060 },
430 { 0x12, 0x400000f0 },
431 { 0x15, 0x400000f0 },
432 {}
433};
434
435static const struct hda_pintbl mbp55_pincfgs[] = {
436 { 0x09, 0x012b4030 },
437 { 0x0a, 0x90100121 },
438 { 0x0b, 0x90100120 },
439 { 0x0c, 0x400000f0 },
440 { 0x0d, 0x90a00110 },
441 { 0x0e, 0x400000f0 },
442 { 0x0f, 0x400000f0 },
443 { 0x10, 0x014be040 },
444 { 0x12, 0x400000f0 },
445 { 0x15, 0x400000f0 },
446 {}
447};
448
449static const struct hda_pintbl imac27_pincfgs[] = {
450 { 0x09, 0x012b4050 },
451 { 0x0a, 0x90100140 },
452 { 0x0b, 0x90100142 },
453 { 0x0c, 0x018b3020 },
454 { 0x0d, 0x90a00110 },
455 { 0x0e, 0x400000f0 },
456 { 0x0f, 0x01cbe030 },
457 { 0x10, 0x014be060 },
458 { 0x12, 0x01ab9070 },
459 { 0x15, 0x400000f0 },
460 {}
461};
462
463static const struct hda_pintbl mbp101_pincfgs[] = {
464 { 0x0d, 0x40ab90f0 },
465 { 0x0e, 0x90a600f0 },
466 { 0x12, 0x50a600f0 },
467 {}
468};
469
470static const struct hda_pintbl mba42_pincfgs[] = {
471 { 0x09, 0x012b4030 },
472 { 0x0a, 0x400000f0 },
473 { 0x0b, 0x90100120 },
474 { 0x0c, 0x400000f0 },
475 { 0x0d, 0x90a00110 },
476 { 0x0e, 0x400000f0 },
477 { 0x0f, 0x400000f0 },
478 { 0x10, 0x400000f0 },
479 { 0x12, 0x400000f0 },
480 { 0x15, 0x400000f0 },
481 {}
482};
483
484static const struct hda_pintbl mba6_pincfgs[] = {
485 { 0x10, 0x032120f0 },
486 { 0x11, 0x500000f0 },
487 { 0x12, 0x90100010 },
488 { 0x13, 0x500000f0 },
489 { 0x14, 0x500000f0 },
490 { 0x15, 0x770000f0 },
491 { 0x16, 0x770000f0 },
492 { 0x17, 0x430000f0 },
493 { 0x18, 0x43ab9030 },
494 { 0x19, 0x770000f0 },
495 { 0x1a, 0x770000f0 },
496 { 0x1b, 0x770000f0 },
497 { 0x1c, 0x90a00090 },
498 { 0x1d, 0x500000f0 },
499 { 0x1e, 0x500000f0 },
500 { 0x1f, 0x500000f0 },
501 { 0x20, 0x500000f0 },
502 { 0x21, 0x430000f0 },
503 { 0x22, 0x430000f0 },
504 {}
505};
506
507static void cs420x_fixup_gpio_13(struct hda_codec *codec,
508 const struct hda_fixup *fix, int action)
509{
510 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
511 struct cs_spec *spec = codec->spec;
512 spec->gpio_eapd_hp = 2;
513 spec->gpio_eapd_speaker = 8;
514 spec->gpio_mask = spec->gpio_dir =
515 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
516 }
517}
518
519static void cs420x_fixup_gpio_23(struct hda_codec *codec,
520 const struct hda_fixup *fix, int action)
521{
522 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
523 struct cs_spec *spec = codec->spec;
524 spec->gpio_eapd_hp = 4;
525 spec->gpio_eapd_speaker = 8;
526 spec->gpio_mask = spec->gpio_dir =
527 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
528 }
529}
530
531static const struct hda_fixup cs420x_fixups[] = {
532 [CS420X_MBP53] = {
533 .type = HDA_FIXUP_PINS,
534 .v.pins = mbp53_pincfgs,
535 .chained = true,
536 .chain_id = CS420X_APPLE,
537 },
538 [CS420X_MBP55] = {
539 .type = HDA_FIXUP_PINS,
540 .v.pins = mbp55_pincfgs,
541 .chained = true,
542 .chain_id = CS420X_GPIO_13,
543 },
544 [CS420X_IMAC27] = {
545 .type = HDA_FIXUP_PINS,
546 .v.pins = imac27_pincfgs,
547 .chained = true,
548 .chain_id = CS420X_GPIO_13,
549 },
550 [CS420X_GPIO_13] = {
551 .type = HDA_FIXUP_FUNC,
552 .v.func = cs420x_fixup_gpio_13,
553 },
554 [CS420X_GPIO_23] = {
555 .type = HDA_FIXUP_FUNC,
556 .v.func = cs420x_fixup_gpio_23,
557 },
558 [CS420X_MBP101] = {
559 .type = HDA_FIXUP_PINS,
560 .v.pins = mbp101_pincfgs,
561 .chained = true,
562 .chain_id = CS420X_GPIO_13,
563 },
564 [CS420X_MBP81] = {
565 .type = HDA_FIXUP_VERBS,
566 .v.verbs = (const struct hda_verb[]) {
567
568 {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
569 {0x11, AC_VERB_SET_PROC_COEF, 0x102a},
570 {}
571 },
572 .chained = true,
573 .chain_id = CS420X_GPIO_13,
574 },
575 [CS420X_MBA42] = {
576 .type = HDA_FIXUP_PINS,
577 .v.pins = mba42_pincfgs,
578 .chained = true,
579 .chain_id = CS420X_GPIO_13,
580 },
581};
582
583static struct cs_spec *cs_alloc_spec(struct hda_codec *codec, int vendor_nid)
584{
585 struct cs_spec *spec;
586
587 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
588 if (!spec)
589 return NULL;
590 codec->spec = spec;
591 spec->vendor_nid = vendor_nid;
592 codec->power_save_node = 1;
593 snd_hda_gen_spec_init(&spec->gen);
594
595 return spec;
596}
597
598static int patch_cs420x(struct hda_codec *codec)
599{
600 struct cs_spec *spec;
601 int err;
602
603 spec = cs_alloc_spec(codec, CS420X_VENDOR_NID);
604 if (!spec)
605 return -ENOMEM;
606
607 codec->patch_ops = cs_patch_ops;
608 spec->gen.automute_hook = cs_automute;
609 codec->single_adc_amp = 1;
610
611 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
612 cs420x_fixups);
613 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
614
615 err = cs_parse_auto_config(codec);
616 if (err < 0)
617 goto error;
618
619 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
620
621 return 0;
622
623 error:
624 cs_free(codec);
625 return err;
626}
627
628
629
630
631
632enum {
633 CS4208_MAC_AUTO,
634 CS4208_MBA6,
635 CS4208_MBP11,
636 CS4208_MACMINI,
637 CS4208_GPIO0,
638};
639
640static const struct hda_model_fixup cs4208_models[] = {
641 { .id = CS4208_GPIO0, .name = "gpio0" },
642 { .id = CS4208_MBA6, .name = "mba6" },
643 { .id = CS4208_MBP11, .name = "mbp11" },
644 { .id = CS4208_MACMINI, .name = "macmini" },
645 {}
646};
647
648static const struct snd_pci_quirk cs4208_fixup_tbl[] = {
649 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS4208_MAC_AUTO),
650 {}
651};
652
653
654static const struct snd_pci_quirk cs4208_mac_fixup_tbl[] = {
655 SND_PCI_QUIRK(0x106b, 0x5e00, "MacBookPro 11,2", CS4208_MBP11),
656 SND_PCI_QUIRK(0x106b, 0x6c00, "MacMini 7,1", CS4208_MACMINI),
657 SND_PCI_QUIRK(0x106b, 0x7100, "MacBookAir 6,1", CS4208_MBA6),
658 SND_PCI_QUIRK(0x106b, 0x7200, "MacBookAir 6,2", CS4208_MBA6),
659 SND_PCI_QUIRK(0x106b, 0x7b00, "MacBookPro 12,1", CS4208_MBP11),
660 {}
661};
662
663static void cs4208_fixup_gpio0(struct hda_codec *codec,
664 const struct hda_fixup *fix, int action)
665{
666 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
667 struct cs_spec *spec = codec->spec;
668 spec->gpio_eapd_hp = 0;
669 spec->gpio_eapd_speaker = 1;
670 spec->gpio_mask = spec->gpio_dir =
671 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
672 }
673}
674
675static const struct hda_fixup cs4208_fixups[];
676
677
678static void cs4208_fixup_mac(struct hda_codec *codec,
679 const struct hda_fixup *fix, int action)
680{
681 if (action != HDA_FIXUP_ACT_PRE_PROBE)
682 return;
683
684 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
685 snd_hda_pick_fixup(codec, NULL, cs4208_mac_fixup_tbl, cs4208_fixups);
686 if (codec->fixup_id == HDA_FIXUP_ID_NOT_SET)
687 codec->fixup_id = CS4208_GPIO0;
688 snd_hda_apply_fixup(codec, action);
689}
690
691
692static void cs4208_fixup_macmini(struct hda_codec *codec,
693 const struct hda_fixup *fix, int action)
694{
695 static const struct hda_pintbl pincfgs[] = {
696 { 0x18, 0x00ab9150 },
697 { 0x21, 0x004be140 },
698 { }
699 };
700
701 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
702
703 codec->inv_jack_detect = 1;
704
705 snd_hda_apply_pincfgs(codec, pincfgs);
706 }
707}
708
709static int cs4208_spdif_sw_put(struct snd_kcontrol *kcontrol,
710 struct snd_ctl_elem_value *ucontrol)
711{
712 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
713 struct cs_spec *spec = codec->spec;
714 hda_nid_t pin = spec->gen.autocfg.dig_out_pins[0];
715 int pinctl = ucontrol->value.integer.value[0] ? PIN_OUT : 0;
716
717 snd_hda_set_pin_ctl_cache(codec, pin, pinctl);
718 return spec->spdif_sw_put(kcontrol, ucontrol);
719}
720
721
722static void cs4208_fixup_spdif_switch(struct hda_codec *codec,
723 const struct hda_fixup *fix, int action)
724{
725 if (action == HDA_FIXUP_ACT_BUILD) {
726 struct cs_spec *spec = codec->spec;
727 struct snd_kcontrol *kctl;
728
729 if (!spec->gen.autocfg.dig_out_pins[0])
730 return;
731 kctl = snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch");
732 if (!kctl)
733 return;
734 spec->spdif_sw_put = kctl->put;
735 kctl->put = cs4208_spdif_sw_put;
736 }
737}
738
739static const struct hda_fixup cs4208_fixups[] = {
740 [CS4208_MBA6] = {
741 .type = HDA_FIXUP_PINS,
742 .v.pins = mba6_pincfgs,
743 .chained = true,
744 .chain_id = CS4208_GPIO0,
745 },
746 [CS4208_MBP11] = {
747 .type = HDA_FIXUP_FUNC,
748 .v.func = cs4208_fixup_spdif_switch,
749 .chained = true,
750 .chain_id = CS4208_GPIO0,
751 },
752 [CS4208_MACMINI] = {
753 .type = HDA_FIXUP_FUNC,
754 .v.func = cs4208_fixup_macmini,
755 .chained = true,
756 .chain_id = CS4208_GPIO0,
757 },
758 [CS4208_GPIO0] = {
759 .type = HDA_FIXUP_FUNC,
760 .v.func = cs4208_fixup_gpio0,
761 },
762 [CS4208_MAC_AUTO] = {
763 .type = HDA_FIXUP_FUNC,
764 .v.func = cs4208_fixup_mac,
765 },
766};
767
768
769static void cs4208_fix_amp_caps(struct hda_codec *codec, hda_nid_t adc)
770{
771 unsigned int caps;
772
773 caps = query_amp_caps(codec, adc, HDA_INPUT);
774 caps &= ~(AC_AMPCAP_OFFSET);
775 caps |= 0x02;
776 snd_hda_override_amp_caps(codec, adc, HDA_INPUT, caps);
777}
778
779static int patch_cs4208(struct hda_codec *codec)
780{
781 struct cs_spec *spec;
782 int err;
783
784 spec = cs_alloc_spec(codec, CS4208_VENDOR_NID);
785 if (!spec)
786 return -ENOMEM;
787
788 codec->patch_ops = cs_patch_ops;
789 spec->gen.automute_hook = cs_automute;
790
791 spec->gen.out_vol_mask = 1ULL << 0x10;
792
793 snd_hda_pick_fixup(codec, cs4208_models, cs4208_fixup_tbl,
794 cs4208_fixups);
795 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
796
797 snd_hda_override_wcaps(codec, 0x18,
798 get_wcaps(codec, 0x18) | AC_WCAP_STEREO);
799 cs4208_fix_amp_caps(codec, 0x18);
800 cs4208_fix_amp_caps(codec, 0x1b);
801 cs4208_fix_amp_caps(codec, 0x1c);
802
803 err = cs_parse_auto_config(codec);
804 if (err < 0)
805 goto error;
806
807 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
808
809 return 0;
810
811 error:
812 cs_free(codec);
813 return err;
814}
815
816
817
818
819
820
821
822
823
824
825static const struct hda_model_fixup cs421x_models[] = {
826 { .id = CS421X_CDB4210, .name = "cdb4210" },
827 { .id = CS421X_STUMPY, .name = "stumpy" },
828 {}
829};
830
831static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
832
833 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
834 {}
835};
836
837
838
839static const struct hda_pintbl cdb4210_pincfgs[] = {
840 { 0x05, 0x0321401f },
841 { 0x06, 0x90170010 },
842 { 0x07, 0x03813031 },
843 { 0x08, 0xb7a70037 },
844 { 0x09, 0xb7a6003e },
845 { 0x0a, 0x034510f0 },
846 {}
847};
848
849
850static const struct hda_pintbl stumpy_pincfgs[] = {
851 { 0x05, 0x022120f0 },
852 { 0x06, 0x901700f0 },
853 { 0x07, 0x02a120f0 },
854 { 0x08, 0x77a70037 },
855 { 0x09, 0x77a6003e },
856 { 0x0a, 0x434510f0 },
857 {}
858};
859
860
861static void cs421x_fixup_sense_b(struct hda_codec *codec,
862 const struct hda_fixup *fix, int action)
863{
864 struct cs_spec *spec = codec->spec;
865 if (action == HDA_FIXUP_ACT_PRE_PROBE)
866 spec->sense_b = 1;
867}
868
869static const struct hda_fixup cs421x_fixups[] = {
870 [CS421X_CDB4210] = {
871 .type = HDA_FIXUP_PINS,
872 .v.pins = cdb4210_pincfgs,
873 .chained = true,
874 .chain_id = CS421X_SENSE_B,
875 },
876 [CS421X_SENSE_B] = {
877 .type = HDA_FIXUP_FUNC,
878 .v.func = cs421x_fixup_sense_b,
879 },
880 [CS421X_STUMPY] = {
881 .type = HDA_FIXUP_PINS,
882 .v.pins = stumpy_pincfgs,
883 },
884};
885
886static const struct hda_verb cs421x_coef_init_verbs[] = {
887 {0x0B, AC_VERB_SET_PROC_STATE, 1},
888 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
889
890
891
892
893 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
894
895 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
896
897 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
898
899 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
900 {0x0B, AC_VERB_SET_PROC_COEF,
901 (0x0002
902 | 0x0004
903 | 0x0008
904 )},
905 {}
906};
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
924 {0x0B, AC_VERB_SET_PROC_STATE, 0x01},
925
926 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
927 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999},
928
929 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
930 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB},
931
932 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
933 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0},
934
935 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
936 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9},
937
938 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
939 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006},
940
941 {}
942};
943
944
945static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
946
947static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
948 struct snd_ctl_elem_info *uinfo)
949{
950 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
951 uinfo->count = 1;
952 uinfo->value.integer.min = 0;
953 uinfo->value.integer.max = 3;
954 return 0;
955}
956
957static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
958 struct snd_ctl_elem_value *ucontrol)
959{
960 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
961
962 ucontrol->value.integer.value[0] =
963 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
964 return 0;
965}
966
967static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
968 struct snd_ctl_elem_value *ucontrol)
969{
970 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
971
972 unsigned int vol = ucontrol->value.integer.value[0];
973 unsigned int coef =
974 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
975 unsigned int original_coef = coef;
976
977 coef &= ~0x0003;
978 coef |= (vol & 0x0003);
979 if (original_coef == coef)
980 return 0;
981 else {
982 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
983 return 1;
984 }
985}
986
987static const struct snd_kcontrol_new cs421x_speaker_boost_ctl = {
988
989 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
990 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
991 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
992 .name = "Speaker Boost Playback Volume",
993 .info = cs421x_boost_vol_info,
994 .get = cs421x_boost_vol_get,
995 .put = cs421x_boost_vol_put,
996 .tlv = { .p = cs421x_speaker_boost_db_scale },
997};
998
999static void cs4210_pinmux_init(struct hda_codec *codec)
1000{
1001 struct cs_spec *spec = codec->spec;
1002 unsigned int def_conf, coef;
1003
1004
1005 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1006
1007 if (spec->gpio_mask)
1008 coef |= 0x0008;
1009 else
1010 coef &= ~0x0008;
1011
1012 if (spec->sense_b)
1013 coef |= 0x0010;
1014 else
1015 coef &= ~0x0010;
1016
1017 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1018
1019 if ((spec->gpio_mask || spec->sense_b) &&
1020 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1021
1022
1023
1024
1025 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1026 def_conf &= ~AC_DEFCFG_PORT_CONN;
1027 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1028 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1029 }
1030}
1031
1032static void cs4210_spdif_automute(struct hda_codec *codec,
1033 struct hda_jack_callback *tbl)
1034{
1035 struct cs_spec *spec = codec->spec;
1036 bool spdif_present = false;
1037 hda_nid_t spdif_pin = spec->gen.autocfg.dig_out_pins[0];
1038
1039
1040 if (!spec->spdif_detect ||
1041 spec->vendor_nid != CS4210_VENDOR_NID)
1042 return;
1043
1044 spdif_present = snd_hda_jack_detect(codec, spdif_pin);
1045 if (spdif_present == spec->spdif_present)
1046 return;
1047
1048 spec->spdif_present = spdif_present;
1049
1050 snd_hda_set_pin_ctl(codec, spdif_pin, spdif_present ? PIN_OUT : 0);
1051
1052 cs_automute(codec);
1053}
1054
1055static void parse_cs421x_digital(struct hda_codec *codec)
1056{
1057 struct cs_spec *spec = codec->spec;
1058 struct auto_pin_cfg *cfg = &spec->gen.autocfg;
1059 int i;
1060
1061 for (i = 0; i < cfg->dig_outs; i++) {
1062 hda_nid_t nid = cfg->dig_out_pins[i];
1063 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1064 spec->spdif_detect = 1;
1065 snd_hda_jack_detect_enable_callback(codec, nid,
1066 cs4210_spdif_automute);
1067 }
1068 }
1069}
1070
1071static int cs421x_init(struct hda_codec *codec)
1072{
1073 struct cs_spec *spec = codec->spec;
1074
1075 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1076 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1077 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1078 cs4210_pinmux_init(codec);
1079 }
1080
1081 snd_hda_gen_init(codec);
1082
1083 if (spec->gpio_mask) {
1084 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1085 spec->gpio_mask);
1086 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1087 spec->gpio_dir);
1088 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1089 spec->gpio_data);
1090 }
1091
1092 init_input_coef(codec);
1093
1094 cs4210_spdif_automute(codec, NULL);
1095
1096 return 0;
1097}
1098
1099static int cs421x_build_controls(struct hda_codec *codec)
1100{
1101 struct cs_spec *spec = codec->spec;
1102 int err;
1103
1104 err = snd_hda_gen_build_controls(codec);
1105 if (err < 0)
1106 return err;
1107
1108 if (spec->gen.autocfg.speaker_outs &&
1109 spec->vendor_nid == CS4210_VENDOR_NID) {
1110 err = snd_hda_ctl_add(codec, 0,
1111 snd_ctl_new1(&cs421x_speaker_boost_ctl, codec));
1112 if (err < 0)
1113 return err;
1114 }
1115 return 0;
1116}
1117
1118static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
1119{
1120 unsigned int caps;
1121
1122
1123 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
1124 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
1125 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
1126 << AC_AMPCAP_NUM_STEPS_SHIFT;
1127 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
1128}
1129
1130static int cs421x_parse_auto_config(struct hda_codec *codec)
1131{
1132 struct cs_spec *spec = codec->spec;
1133 hda_nid_t dac = CS4210_DAC_NID;
1134 int err;
1135
1136 fix_volume_caps(codec, dac);
1137
1138 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
1139 if (err < 0)
1140 return err;
1141
1142 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
1143 if (err < 0)
1144 return err;
1145
1146 parse_cs421x_digital(codec);
1147 return 0;
1148}
1149
1150#ifdef CONFIG_PM
1151
1152
1153
1154
1155static int cs421x_suspend(struct hda_codec *codec)
1156{
1157 struct cs_spec *spec = codec->spec;
1158 unsigned int coef;
1159
1160 snd_hda_shutup_pins(codec);
1161
1162 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1163 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1164 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1165 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1166
1167 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1168 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1169 coef |= 0x0004;
1170 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1171 }
1172
1173 return 0;
1174}
1175#endif
1176
1177static const struct hda_codec_ops cs421x_patch_ops = {
1178 .build_controls = cs421x_build_controls,
1179 .build_pcms = snd_hda_gen_build_pcms,
1180 .init = cs421x_init,
1181 .free = cs_free,
1182 .unsol_event = snd_hda_jack_unsol_event,
1183#ifdef CONFIG_PM
1184 .suspend = cs421x_suspend,
1185#endif
1186};
1187
1188static int patch_cs4210(struct hda_codec *codec)
1189{
1190 struct cs_spec *spec;
1191 int err;
1192
1193 spec = cs_alloc_spec(codec, CS4210_VENDOR_NID);
1194 if (!spec)
1195 return -ENOMEM;
1196
1197 codec->patch_ops = cs421x_patch_ops;
1198 spec->gen.automute_hook = cs_automute;
1199
1200 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1201 cs421x_fixups);
1202 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1203
1204
1205
1206
1207
1208
1209 cs4210_pinmux_init(codec);
1210
1211 err = cs421x_parse_auto_config(codec);
1212 if (err < 0)
1213 goto error;
1214
1215 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1216
1217 return 0;
1218
1219 error:
1220 cs_free(codec);
1221 return err;
1222}
1223
1224static int patch_cs4213(struct hda_codec *codec)
1225{
1226 struct cs_spec *spec;
1227 int err;
1228
1229 spec = cs_alloc_spec(codec, CS4213_VENDOR_NID);
1230 if (!spec)
1231 return -ENOMEM;
1232
1233 codec->patch_ops = cs421x_patch_ops;
1234
1235 err = cs421x_parse_auto_config(codec);
1236 if (err < 0)
1237 goto error;
1238
1239 return 0;
1240
1241 error:
1242 cs_free(codec);
1243 return err;
1244}
1245
1246
1247
1248
1249
1250static const struct hda_device_id snd_hda_id_cirrus[] = {
1251 HDA_CODEC_ENTRY(0x10134206, "CS4206", patch_cs420x),
1252 HDA_CODEC_ENTRY(0x10134207, "CS4207", patch_cs420x),
1253 HDA_CODEC_ENTRY(0x10134208, "CS4208", patch_cs4208),
1254 HDA_CODEC_ENTRY(0x10134210, "CS4210", patch_cs4210),
1255 HDA_CODEC_ENTRY(0x10134213, "CS4213", patch_cs4213),
1256 {}
1257};
1258MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_cirrus);
1259
1260MODULE_LICENSE("GPL");
1261MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1262
1263static struct hda_codec_driver cirrus_driver = {
1264 .id = snd_hda_id_cirrus,
1265};
1266
1267module_hda_codec_driver(cirrus_driver);
1268