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