]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/pci/hda/patch_cirrus.c
Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[karo-tx-linux.git] / sound / pci / hda / patch_cirrus.c
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/pci.h>
24 #include <linux/module.h>
25 #include <sound/core.h>
26 #include <sound/tlv.h>
27 #include "hda_codec.h"
28 #include "hda_local.h"
29 #include "hda_auto_parser.h"
30 #include "hda_jack.h"
31 #include "hda_generic.h"
32
33 /*
34  */
35
36 struct cs_spec {
37         struct hda_gen_spec gen;
38
39         unsigned int gpio_mask;
40         unsigned int gpio_dir;
41         unsigned int gpio_data;
42         unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
43         unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
44
45         /* CS421x */
46         unsigned int spdif_detect:1;
47         unsigned int spdif_present:1;
48         unsigned int sense_b:1;
49         hda_nid_t vendor_nid;
50 };
51
52 /* available models with CS420x */
53 enum {
54         CS420X_MBP53,
55         CS420X_MBP55,
56         CS420X_IMAC27,
57         CS420X_GPIO_13,
58         CS420X_GPIO_23,
59         CS420X_MBP101,
60         CS420X_MBP81,
61         CS420X_MBA42,
62         CS420X_AUTO,
63         /* aliases */
64         CS420X_IMAC27_122 = CS420X_GPIO_23,
65         CS420X_APPLE = CS420X_GPIO_13,
66 };
67
68 /* CS421x boards */
69 enum {
70         CS421X_CDB4210,
71         CS421X_SENSE_B,
72         CS421X_STUMPY,
73 };
74
75 /* Vendor-specific processing widget */
76 #define CS420X_VENDOR_NID       0x11
77 #define CS_DIG_OUT1_PIN_NID     0x10
78 #define CS_DIG_OUT2_PIN_NID     0x15
79 #define CS_DMIC1_PIN_NID        0x0e
80 #define CS_DMIC2_PIN_NID        0x12
81
82 /* coef indices */
83 #define IDX_SPDIF_STAT          0x0000
84 #define IDX_SPDIF_CTL           0x0001
85 #define IDX_ADC_CFG             0x0002
86 /* SZC bitmask, 4 modes below:
87  * 0 = immediate,
88  * 1 = digital immediate, analog zero-cross
89  * 2 = digtail & analog soft-ramp
90  * 3 = digital soft-ramp, analog zero-cross
91  */
92 #define   CS_COEF_ADC_SZC_MASK          (3 << 0)
93 #define   CS_COEF_ADC_MIC_SZC_MODE      (3 << 0) /* SZC setup for mic */
94 #define   CS_COEF_ADC_LI_SZC_MODE       (3 << 0) /* SZC setup for line-in */
95 /* PGA mode: 0 = differential, 1 = signle-ended */
96 #define   CS_COEF_ADC_MIC_PGA_MODE      (1 << 5) /* PGA setup for mic */
97 #define   CS_COEF_ADC_LI_PGA_MODE       (1 << 6) /* PGA setup for line-in */
98 #define IDX_DAC_CFG             0x0003
99 /* SZC bitmask, 4 modes below:
100  * 0 = Immediate
101  * 1 = zero-cross
102  * 2 = soft-ramp
103  * 3 = soft-ramp on zero-cross
104  */
105 #define   CS_COEF_DAC_HP_SZC_MODE       (3 << 0) /* nid 0x02 */
106 #define   CS_COEF_DAC_LO_SZC_MODE       (3 << 2) /* nid 0x03 */
107 #define   CS_COEF_DAC_SPK_SZC_MODE      (3 << 4) /* nid 0x04 */
108
109 #define IDX_BEEP_CFG            0x0004
110 /* 0x0008 - test reg key */
111 /* 0x0009 - 0x0014 -> 12 test regs */
112 /* 0x0015 - visibility reg */
113
114 /*
115  * Cirrus Logic CS4210
116  *
117  * 1 DAC => HP(sense) / Speakers,
118  * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
119  * 1 SPDIF OUT => SPDIF Trasmitter(sense)
120 */
121 #define CS4210_DAC_NID          0x02
122 #define CS4210_ADC_NID          0x03
123 #define CS4210_VENDOR_NID       0x0B
124 #define CS421X_DMIC_PIN_NID     0x09 /* Port E */
125 #define CS421X_SPDIF_PIN_NID    0x0A /* Port H */
126
127 #define CS421X_IDX_DEV_CFG      0x01
128 #define CS421X_IDX_ADC_CFG      0x02
129 #define CS421X_IDX_DAC_CFG      0x03
130 #define CS421X_IDX_SPK_CTL      0x04
131
132 #define SPDIF_EVENT             0x04
133
134 /* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
135 #define CS4213_VENDOR_NID       0x09
136
137
138 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
139 {
140         struct cs_spec *spec = codec->spec;
141         snd_hda_codec_write(codec, spec->vendor_nid, 0,
142                             AC_VERB_SET_COEF_INDEX, idx);
143         return snd_hda_codec_read(codec, spec->vendor_nid, 0,
144                                   AC_VERB_GET_PROC_COEF, 0);
145 }
146
147 static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
148                                       unsigned int coef)
149 {
150         struct cs_spec *spec = codec->spec;
151         snd_hda_codec_write(codec, spec->vendor_nid, 0,
152                             AC_VERB_SET_COEF_INDEX, idx);
153         snd_hda_codec_write(codec, spec->vendor_nid, 0,
154                             AC_VERB_SET_PROC_COEF, coef);
155 }
156
157 /*
158  * auto-mute and auto-mic switching
159  * CS421x auto-output redirecting
160  * HP/SPK/SPDIF
161  */
162
163 static void cs_automute(struct hda_codec *codec)
164 {
165         struct cs_spec *spec = codec->spec;
166
167         /* mute HPs if spdif jack (SENSE_B) is present */
168         spec->gen.master_mute = !!(spec->spdif_present && spec->sense_b);
169
170         snd_hda_gen_update_outputs(codec);
171
172         if (spec->gpio_eapd_hp) {
173                 spec->gpio_data = spec->gen.hp_jack_present ?
174                         spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
175                 snd_hda_codec_write(codec, 0x01, 0,
176                                     AC_VERB_SET_GPIO_DATA, spec->gpio_data);
177         }
178 }
179
180 static bool is_active_pin(struct hda_codec *codec, hda_nid_t nid)
181 {
182         unsigned int val;
183         val = snd_hda_codec_get_pincfg(codec, nid);
184         return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
185 }
186
187 static void init_input_coef(struct hda_codec *codec)
188 {
189         struct cs_spec *spec = codec->spec;
190         unsigned int coef;
191
192         /* CS420x has multiple ADC, CS421x has single ADC */
193         if (spec->vendor_nid == CS420X_VENDOR_NID) {
194                 coef = cs_vendor_coef_get(codec, IDX_BEEP_CFG);
195                 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
196                         coef |= 1 << 4; /* DMIC2 2 chan on, GPIO1 off */
197                 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
198                         coef |= 1 << 3; /* DMIC1 2 chan on, GPIO0 off
199                                          * No effect if SPDIF_OUT2 is
200                                          * selected in IDX_SPDIF_CTL.
201                                         */
202
203                 cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
204         }
205 }
206
207 static const struct hda_verb cs_coef_init_verbs[] = {
208         {0x11, AC_VERB_SET_PROC_STATE, 1},
209         {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
210         {0x11, AC_VERB_SET_PROC_COEF,
211          (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
212           | 0x0040 /* Mute DACs on FIFO error */
213           | 0x1000 /* Enable DACs High Pass Filter */
214           | 0x0400 /* Disable Coefficient Auto increment */
215           )},
216         /* ADC1/2 - Digital and Analog Soft Ramp */
217         {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
218         {0x11, AC_VERB_SET_PROC_COEF, 0x000a},
219         /* Beep */
220         {0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
221         {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
222
223         {} /* terminator */
224 };
225
226 /* Errata: CS4207 rev C0/C1/C2 Silicon
227  *
228  * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
229  *
230  * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
231  * may be excessive (up to an additional 200 μA), which is most easily
232  * observed while the part is being held in reset (RESET# active low).
233  *
234  * Root Cause: At initial powerup of the device, the logic that drives
235  * the clock and write enable to the S/PDIF SRC RAMs is not properly
236  * initialized.
237  * Certain random patterns will cause a steady leakage current in those
238  * RAM cells. The issue will resolve once the SRCs are used (turned on).
239  *
240  * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
241  * blocks, which will alleviate the issue.
242  */
243
244 static const struct hda_verb cs_errata_init_verbs[] = {
245         {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
246         {0x11, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
247
248         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
249         {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
250         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
251         {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
252         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
253         {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
254
255         {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
256         {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
257
258         {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
259         {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
260         {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
261         {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
262         {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
263         {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
264         {0x11, AC_VERB_SET_PROC_STATE, 0x00},
265
266 #if 0 /* Don't to set to D3 as we are in power-up sequence */
267         {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
268         {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
269         /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
270 #endif
271
272         {} /* terminator */
273 };
274
275 /* SPDIF setup */
276 static void init_digital_coef(struct hda_codec *codec)
277 {
278         unsigned int coef;
279
280         coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
281         coef |= 0x0008; /* Replace with mute on error */
282         if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
283                 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
284                                  * SPDIF_OUT2 is shared with GPIO1 and
285                                  * DMIC_SDA2.
286                                  */
287         cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
288 }
289
290 static int cs_init(struct hda_codec *codec)
291 {
292         struct cs_spec *spec = codec->spec;
293
294         /* init_verb sequence for C0/C1/C2 errata*/
295         snd_hda_sequence_write(codec, cs_errata_init_verbs);
296
297         snd_hda_sequence_write(codec, cs_coef_init_verbs);
298
299         snd_hda_gen_init(codec);
300
301         if (spec->gpio_mask) {
302                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
303                                     spec->gpio_mask);
304                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
305                                     spec->gpio_dir);
306                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
307                                     spec->gpio_data);
308         }
309
310         init_input_coef(codec);
311         init_digital_coef(codec);
312
313         return 0;
314 }
315
316 #define cs_free         snd_hda_gen_free
317
318 static const struct hda_codec_ops cs_patch_ops = {
319         .build_controls = snd_hda_gen_build_controls,
320         .build_pcms = snd_hda_gen_build_pcms,
321         .init = cs_init,
322         .free = cs_free,
323         .unsol_event = snd_hda_jack_unsol_event,
324 };
325
326 static int cs_parse_auto_config(struct hda_codec *codec)
327 {
328         struct cs_spec *spec = codec->spec;
329         int err;
330
331         err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
332         if (err < 0)
333                 return err;
334
335         err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
336         if (err < 0)
337                 return err;
338
339         return 0;
340 }
341
342 static const struct hda_model_fixup cs420x_models[] = {
343         { .id = CS420X_MBP53, .name = "mbp53" },
344         { .id = CS420X_MBP55, .name = "mbp55" },
345         { .id = CS420X_IMAC27, .name = "imac27" },
346         { .id = CS420X_IMAC27_122, .name = "imac27_122" },
347         { .id = CS420X_APPLE, .name = "apple" },
348         { .id = CS420X_MBP101, .name = "mbp101" },
349         { .id = CS420X_MBP81, .name = "mbp81" },
350         { .id = CS420X_MBA42, .name = "mba42" },
351         {}
352 };
353
354 static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
355         SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
356         SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
357         SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
358         SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
359         /* this conflicts with too many other models */
360         /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
361
362         /* codec SSID */
363         SND_PCI_QUIRK(0x106b, 0x1c00, "MacBookPro 8,1", CS420X_MBP81),
364         SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
365         SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
366         SND_PCI_QUIRK(0x106b, 0x5b00, "MacBookAir 4,2", CS420X_MBA42),
367         SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
368         {} /* terminator */
369 };
370
371 static const struct hda_pintbl mbp53_pincfgs[] = {
372         { 0x09, 0x012b4050 },
373         { 0x0a, 0x90100141 },
374         { 0x0b, 0x90100140 },
375         { 0x0c, 0x018b3020 },
376         { 0x0d, 0x90a00110 },
377         { 0x0e, 0x400000f0 },
378         { 0x0f, 0x01cbe030 },
379         { 0x10, 0x014be060 },
380         { 0x12, 0x400000f0 },
381         { 0x15, 0x400000f0 },
382         {} /* terminator */
383 };
384
385 static const struct hda_pintbl mbp55_pincfgs[] = {
386         { 0x09, 0x012b4030 },
387         { 0x0a, 0x90100121 },
388         { 0x0b, 0x90100120 },
389         { 0x0c, 0x400000f0 },
390         { 0x0d, 0x90a00110 },
391         { 0x0e, 0x400000f0 },
392         { 0x0f, 0x400000f0 },
393         { 0x10, 0x014be040 },
394         { 0x12, 0x400000f0 },
395         { 0x15, 0x400000f0 },
396         {} /* terminator */
397 };
398
399 static const struct hda_pintbl imac27_pincfgs[] = {
400         { 0x09, 0x012b4050 },
401         { 0x0a, 0x90100140 },
402         { 0x0b, 0x90100142 },
403         { 0x0c, 0x018b3020 },
404         { 0x0d, 0x90a00110 },
405         { 0x0e, 0x400000f0 },
406         { 0x0f, 0x01cbe030 },
407         { 0x10, 0x014be060 },
408         { 0x12, 0x01ab9070 },
409         { 0x15, 0x400000f0 },
410         {} /* terminator */
411 };
412
413 static const struct hda_pintbl mbp101_pincfgs[] = {
414         { 0x0d, 0x40ab90f0 },
415         { 0x0e, 0x90a600f0 },
416         { 0x12, 0x50a600f0 },
417         {} /* terminator */
418 };
419
420 static const struct hda_pintbl mba42_pincfgs[] = {
421         { 0x09, 0x012b4030 }, /* HP */
422         { 0x0a, 0x400000f0 },
423         { 0x0b, 0x90100120 }, /* speaker */
424         { 0x0c, 0x400000f0 },
425         { 0x0d, 0x90a00110 }, /* mic */
426         { 0x0e, 0x400000f0 },
427         { 0x0f, 0x400000f0 },
428         { 0x10, 0x400000f0 },
429         { 0x12, 0x400000f0 },
430         { 0x15, 0x400000f0 },
431         {} /* terminator */
432 };
433
434 static void cs420x_fixup_gpio_13(struct hda_codec *codec,
435                                  const struct hda_fixup *fix, int action)
436 {
437         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
438                 struct cs_spec *spec = codec->spec;
439                 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
440                 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
441                 spec->gpio_mask = spec->gpio_dir =
442                         spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
443         }
444 }
445
446 static void cs420x_fixup_gpio_23(struct hda_codec *codec,
447                                  const struct hda_fixup *fix, int action)
448 {
449         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
450                 struct cs_spec *spec = codec->spec;
451                 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
452                 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
453                 spec->gpio_mask = spec->gpio_dir =
454                         spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
455         }
456 }
457
458 static const struct hda_fixup cs420x_fixups[] = {
459         [CS420X_MBP53] = {
460                 .type = HDA_FIXUP_PINS,
461                 .v.pins = mbp53_pincfgs,
462                 .chained = true,
463                 .chain_id = CS420X_APPLE,
464         },
465         [CS420X_MBP55] = {
466                 .type = HDA_FIXUP_PINS,
467                 .v.pins = mbp55_pincfgs,
468                 .chained = true,
469                 .chain_id = CS420X_GPIO_13,
470         },
471         [CS420X_IMAC27] = {
472                 .type = HDA_FIXUP_PINS,
473                 .v.pins = imac27_pincfgs,
474                 .chained = true,
475                 .chain_id = CS420X_GPIO_13,
476         },
477         [CS420X_GPIO_13] = {
478                 .type = HDA_FIXUP_FUNC,
479                 .v.func = cs420x_fixup_gpio_13,
480         },
481         [CS420X_GPIO_23] = {
482                 .type = HDA_FIXUP_FUNC,
483                 .v.func = cs420x_fixup_gpio_23,
484         },
485         [CS420X_MBP101] = {
486                 .type = HDA_FIXUP_PINS,
487                 .v.pins = mbp101_pincfgs,
488                 .chained = true,
489                 .chain_id = CS420X_GPIO_13,
490         },
491         [CS420X_MBP81] = {
492                 .type = HDA_FIXUP_VERBS,
493                 .v.verbs = (const struct hda_verb[]) {
494                         /* internal mic ADC2: right only, single ended */
495                         {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
496                         {0x11, AC_VERB_SET_PROC_COEF, 0x102a},
497                         {}
498                 },
499                 .chained = true,
500                 .chain_id = CS420X_GPIO_13,
501         },
502         [CS420X_MBA42] = {
503                 .type = HDA_FIXUP_PINS,
504                 .v.pins = mba42_pincfgs,
505                 .chained = true,
506                 .chain_id = CS420X_GPIO_13,
507         },
508 };
509
510 static struct cs_spec *cs_alloc_spec(struct hda_codec *codec, int vendor_nid)
511 {
512         struct cs_spec *spec;
513
514         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
515         if (!spec)
516                 return NULL;
517         codec->spec = spec;
518         spec->vendor_nid = vendor_nid;
519         snd_hda_gen_spec_init(&spec->gen);
520
521         return spec;
522 }
523
524 static int patch_cs420x(struct hda_codec *codec)
525 {
526         struct cs_spec *spec;
527         int err;
528
529         spec = cs_alloc_spec(codec, CS420X_VENDOR_NID);
530         if (!spec)
531                 return -ENOMEM;
532
533         spec->gen.automute_hook = cs_automute;
534
535         snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
536                            cs420x_fixups);
537         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
538
539         err = cs_parse_auto_config(codec);
540         if (err < 0)
541                 goto error;
542
543         codec->patch_ops = cs_patch_ops;
544
545         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
546
547         return 0;
548
549  error:
550         cs_free(codec);
551         return err;
552 }
553
554 /*
555  * Cirrus Logic CS4210
556  *
557  * 1 DAC => HP(sense) / Speakers,
558  * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
559  * 1 SPDIF OUT => SPDIF Trasmitter(sense)
560 */
561
562 /* CS4210 board names */
563 static const struct hda_model_fixup cs421x_models[] = {
564         { .id = CS421X_CDB4210, .name = "cdb4210" },
565         { .id = CS421X_STUMPY, .name = "stumpy" },
566         {}
567 };
568
569 static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
570         /* Test Intel board + CDB2410  */
571         SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
572         {} /* terminator */
573 };
574
575 /* CS4210 board pinconfigs */
576 /* Default CS4210 (CDB4210)*/
577 static const struct hda_pintbl cdb4210_pincfgs[] = {
578         { 0x05, 0x0321401f },
579         { 0x06, 0x90170010 },
580         { 0x07, 0x03813031 },
581         { 0x08, 0xb7a70037 },
582         { 0x09, 0xb7a6003e },
583         { 0x0a, 0x034510f0 },
584         {} /* terminator */
585 };
586
587 /* Stumpy ChromeBox */
588 static const struct hda_pintbl stumpy_pincfgs[] = {
589         { 0x05, 0x022120f0 },
590         { 0x06, 0x901700f0 },
591         { 0x07, 0x02a120f0 },
592         { 0x08, 0x77a70037 },
593         { 0x09, 0x77a6003e },
594         { 0x0a, 0x434510f0 },
595         {} /* terminator */
596 };
597
598 /* Setup GPIO/SENSE for each board (if used) */
599 static void cs421x_fixup_sense_b(struct hda_codec *codec,
600                                  const struct hda_fixup *fix, int action)
601 {
602         struct cs_spec *spec = codec->spec;
603         if (action == HDA_FIXUP_ACT_PRE_PROBE)
604                 spec->sense_b = 1;
605 }
606
607 static const struct hda_fixup cs421x_fixups[] = {
608         [CS421X_CDB4210] = {
609                 .type = HDA_FIXUP_PINS,
610                 .v.pins = cdb4210_pincfgs,
611                 .chained = true,
612                 .chain_id = CS421X_SENSE_B,
613         },
614         [CS421X_SENSE_B] = {
615                 .type = HDA_FIXUP_FUNC,
616                 .v.func = cs421x_fixup_sense_b,
617         },
618         [CS421X_STUMPY] = {
619                 .type = HDA_FIXUP_PINS,
620                 .v.pins = stumpy_pincfgs,
621         },
622 };
623
624 static const struct hda_verb cs421x_coef_init_verbs[] = {
625         {0x0B, AC_VERB_SET_PROC_STATE, 1},
626         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
627         /*
628             Disable Coefficient Index Auto-Increment(DAI)=1,
629             PDREF=0
630         */
631         {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
632
633         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
634         /* ADC SZCMode = Digital Soft Ramp */
635         {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
636
637         {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
638         {0x0B, AC_VERB_SET_PROC_COEF,
639          (0x0002 /* DAC SZCMode = Digital Soft Ramp */
640           | 0x0004 /* Mute DAC on FIFO error */
641           | 0x0008 /* Enable DAC High Pass Filter */
642           )},
643         {} /* terminator */
644 };
645
646 /* Errata: CS4210 rev A1 Silicon
647  *
648  * http://www.cirrus.com/en/pubs/errata/
649  *
650  * Description:
651  * 1. Performance degredation is present in the ADC.
652  * 2. Speaker output is not completely muted upon HP detect.
653  * 3. Noise is present when clipping occurs on the amplified
654  *    speaker outputs.
655  *
656  * Workaround:
657  * The following verb sequence written to the registers during
658  * initialization will correct the issues listed above.
659  */
660
661 static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
662         {0x0B, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
663
664         {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
665         {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
666
667         {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
668         {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
669
670         {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
671         {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
672
673         {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
674         {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
675
676         {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
677         {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
678
679         {} /* terminator */
680 };
681
682 /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
683 static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
684
685 static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
686                                 struct snd_ctl_elem_info *uinfo)
687 {
688         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
689         uinfo->count = 1;
690         uinfo->value.integer.min = 0;
691         uinfo->value.integer.max = 3;
692         return 0;
693 }
694
695 static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
696                                 struct snd_ctl_elem_value *ucontrol)
697 {
698         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
699
700         ucontrol->value.integer.value[0] =
701                 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
702         return 0;
703 }
704
705 static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
706                                 struct snd_ctl_elem_value *ucontrol)
707 {
708         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
709
710         unsigned int vol = ucontrol->value.integer.value[0];
711         unsigned int coef =
712                 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
713         unsigned int original_coef = coef;
714
715         coef &= ~0x0003;
716         coef |= (vol & 0x0003);
717         if (original_coef == coef)
718                 return 0;
719         else {
720                 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
721                 return 1;
722         }
723 }
724
725 static const struct snd_kcontrol_new cs421x_speaker_boost_ctl = {
726
727         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
728         .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
729                         SNDRV_CTL_ELEM_ACCESS_TLV_READ),
730         .name = "Speaker Boost Playback Volume",
731         .info = cs421x_boost_vol_info,
732         .get = cs421x_boost_vol_get,
733         .put = cs421x_boost_vol_put,
734         .tlv = { .p = cs421x_speaker_boost_db_scale },
735 };
736
737 static void cs4210_pinmux_init(struct hda_codec *codec)
738 {
739         struct cs_spec *spec = codec->spec;
740         unsigned int def_conf, coef;
741
742         /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
743         coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
744
745         if (spec->gpio_mask)
746                 coef |= 0x0008; /* B1,B2 are GPIOs */
747         else
748                 coef &= ~0x0008;
749
750         if (spec->sense_b)
751                 coef |= 0x0010; /* B2 is SENSE_B, not inverted  */
752         else
753                 coef &= ~0x0010;
754
755         cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
756
757         if ((spec->gpio_mask || spec->sense_b) &&
758             is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
759
760                 /*
761                     GPIO or SENSE_B forced - disconnect the DMIC pin.
762                 */
763                 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
764                 def_conf &= ~AC_DEFCFG_PORT_CONN;
765                 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
766                 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
767         }
768 }
769
770 static void cs4210_spdif_automute(struct hda_codec *codec,
771                                   struct hda_jack_tbl *tbl)
772 {
773         struct cs_spec *spec = codec->spec;
774         bool spdif_present = false;
775         hda_nid_t spdif_pin = spec->gen.autocfg.dig_out_pins[0];
776
777         /* detect on spdif is specific to CS4210 */
778         if (!spec->spdif_detect ||
779             spec->vendor_nid != CS4210_VENDOR_NID)
780                 return;
781
782         spdif_present = snd_hda_jack_detect(codec, spdif_pin);
783         if (spdif_present == spec->spdif_present)
784                 return;
785
786         spec->spdif_present = spdif_present;
787         /* SPDIF TX on/off */
788         if (spdif_present)
789                 snd_hda_set_pin_ctl(codec, spdif_pin,
790                                     spdif_present ? PIN_OUT : 0);
791
792         cs_automute(codec);
793 }
794
795 static void parse_cs421x_digital(struct hda_codec *codec)
796 {
797         struct cs_spec *spec = codec->spec;
798         struct auto_pin_cfg *cfg = &spec->gen.autocfg;
799         int i;
800
801         for (i = 0; i < cfg->dig_outs; i++) {
802                 hda_nid_t nid = cfg->dig_out_pins[i];
803                 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
804                         spec->spdif_detect = 1;
805                         snd_hda_jack_detect_enable_callback(codec, nid,
806                                                             SPDIF_EVENT,
807                                                             cs4210_spdif_automute);
808                 }
809         }
810 }
811
812 static int cs421x_init(struct hda_codec *codec)
813 {
814         struct cs_spec *spec = codec->spec;
815
816         if (spec->vendor_nid == CS4210_VENDOR_NID) {
817                 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
818                 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
819                 cs4210_pinmux_init(codec);
820         }
821
822         snd_hda_gen_init(codec);
823
824         if (spec->gpio_mask) {
825                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
826                                     spec->gpio_mask);
827                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
828                                     spec->gpio_dir);
829                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
830                                     spec->gpio_data);
831         }
832
833         init_input_coef(codec);
834
835         cs4210_spdif_automute(codec, NULL);
836
837         return 0;
838 }
839
840 static int cs421x_build_controls(struct hda_codec *codec)
841 {
842         struct cs_spec *spec = codec->spec;
843         int err;
844
845         err = snd_hda_gen_build_controls(codec);
846         if (err < 0)
847                 return err;
848
849         if (spec->gen.autocfg.speaker_outs &&
850             spec->vendor_nid == CS4210_VENDOR_NID) {
851                 err = snd_hda_ctl_add(codec, 0,
852                         snd_ctl_new1(&cs421x_speaker_boost_ctl, codec));
853                 if (err < 0)
854                         return err;
855         }
856         return 0;
857 }
858
859 static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
860 {
861         unsigned int caps;
862
863         /* set the upper-limit for mixer amp to 0dB */
864         caps = query_amp_caps(codec, dac, HDA_OUTPUT);
865         caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
866         caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
867                 << AC_AMPCAP_NUM_STEPS_SHIFT;
868         snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
869 }
870
871 static int cs421x_parse_auto_config(struct hda_codec *codec)
872 {
873         struct cs_spec *spec = codec->spec;
874         hda_nid_t dac = CS4210_DAC_NID;
875         int err;
876
877         fix_volume_caps(codec, dac);
878
879         err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
880         if (err < 0)
881                 return err;
882
883         err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
884         if (err < 0)
885                 return err;
886
887         parse_cs421x_digital(codec);
888         return 0;
889 }
890
891 #ifdef CONFIG_PM
892 /*
893         Manage PDREF, when transitioning to D3hot
894         (DAC,ADC) -> D3, PDREF=1, AFG->D3
895 */
896 static int cs421x_suspend(struct hda_codec *codec)
897 {
898         struct cs_spec *spec = codec->spec;
899         unsigned int coef;
900
901         snd_hda_shutup_pins(codec);
902
903         snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
904                             AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
905         snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
906                             AC_VERB_SET_POWER_STATE,  AC_PWRST_D3);
907
908         if (spec->vendor_nid == CS4210_VENDOR_NID) {
909                 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
910                 coef |= 0x0004; /* PDREF */
911                 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
912         }
913
914         return 0;
915 }
916 #endif
917
918 static const struct hda_codec_ops cs421x_patch_ops = {
919         .build_controls = cs421x_build_controls,
920         .build_pcms = snd_hda_gen_build_pcms,
921         .init = cs421x_init,
922         .free = cs_free,
923         .unsol_event = snd_hda_jack_unsol_event,
924 #ifdef CONFIG_PM
925         .suspend = cs421x_suspend,
926 #endif
927 };
928
929 static int patch_cs4210(struct hda_codec *codec)
930 {
931         struct cs_spec *spec;
932         int err;
933
934         spec = cs_alloc_spec(codec, CS4210_VENDOR_NID);
935         if (!spec)
936                 return -ENOMEM;
937
938         spec->gen.automute_hook = cs_automute;
939
940         snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
941                            cs421x_fixups);
942         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
943
944         /*
945             Update the GPIO/DMIC/SENSE_B pinmux before the configuration
946             is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
947             is disabled.
948         */
949         cs4210_pinmux_init(codec);
950
951         err = cs421x_parse_auto_config(codec);
952         if (err < 0)
953                 goto error;
954
955         codec->patch_ops = cs421x_patch_ops;
956
957         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
958
959         return 0;
960
961  error:
962         cs_free(codec);
963         return err;
964 }
965
966 static int patch_cs4213(struct hda_codec *codec)
967 {
968         struct cs_spec *spec;
969         int err;
970
971         spec = cs_alloc_spec(codec, CS4213_VENDOR_NID);
972         if (!spec)
973                 return -ENOMEM;
974
975         err = cs421x_parse_auto_config(codec);
976         if (err < 0)
977                 goto error;
978
979         codec->patch_ops = cs421x_patch_ops;
980         return 0;
981
982  error:
983         cs_free(codec);
984         return err;
985 }
986
987
988 /*
989  * patch entries
990  */
991 static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
992         { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
993         { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
994         { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
995         { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
996         {} /* terminator */
997 };
998
999 MODULE_ALIAS("snd-hda-codec-id:10134206");
1000 MODULE_ALIAS("snd-hda-codec-id:10134207");
1001 MODULE_ALIAS("snd-hda-codec-id:10134210");
1002 MODULE_ALIAS("snd-hda-codec-id:10134213");
1003
1004 MODULE_LICENSE("GPL");
1005 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1006
1007 static struct hda_codec_preset_list cirrus_list = {
1008         .preset = snd_hda_preset_cirrus,
1009         .owner = THIS_MODULE,
1010 };
1011
1012 static int __init patch_cirrus_init(void)
1013 {
1014         return snd_hda_add_codec_preset(&cirrus_list);
1015 }
1016
1017 static void __exit patch_cirrus_exit(void)
1018 {
1019         snd_hda_delete_codec_preset(&cirrus_list);
1020 }
1021
1022 module_init(patch_cirrus_init)
1023 module_exit(patch_cirrus_exit)