blob: fc492ac24caa917c9d3670a37f83cb1d3e0bca95 [file] [log] [blame]
Takashi Iwaie5f14242009-07-01 18:11:44 +02001/*
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>
Takashi Iwaie5f14242009-07-01 18:11:44 +020022#include <linux/slab.h>
23#include <linux/pci.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020025#include <sound/core.h>
Takashi Iwai1077a022012-12-19 16:39:18 +010026#include <sound/tlv.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020027#include "hda_codec.h"
28#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020029#include "hda_auto_parser.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020030#include "hda_jack.h"
Takashi Iwai1077a022012-12-19 16:39:18 +010031#include "hda_generic.h"
Takashi Iwaie5f14242009-07-01 18:11:44 +020032
33/*
34 */
35
36struct cs_spec {
Takashi Iwai1077a022012-12-19 16:39:18 +010037 struct hda_gen_spec gen;
Takashi Iwaie5f14242009-07-01 18:11:44 +020038
Takashi Iwaied208252009-07-07 09:04:26 +020039 unsigned int gpio_mask;
40 unsigned int gpio_dir;
41 unsigned int gpio_data;
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010042 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
43 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
Takashi Iwaied208252009-07-07 09:04:26 +020044
Tim Howe56487c22011-07-22 16:41:00 -050045 /* CS421x */
46 unsigned int spdif_detect:1;
Takashi Iwai1077a022012-12-19 16:39:18 +010047 unsigned int spdif_present:1;
Tim Howe56487c22011-07-22 16:41:00 -050048 unsigned int sense_b:1;
49 hda_nid_t vendor_nid;
Takashi Iwai0c52db82013-11-08 13:03:57 +010050
51 /* for MBP SPDIF control */
52 int (*spdif_sw_put)(struct snd_kcontrol *kcontrol,
53 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaie5f14242009-07-01 18:11:44 +020054};
55
Tim Howe56487c22011-07-22 16:41:00 -050056/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020057enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040058 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020059 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010060 CS420X_IMAC27,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020061 CS420X_GPIO_13,
62 CS420X_GPIO_23,
Takashi Iwaief596a52012-09-11 16:53:08 +020063 CS420X_MBP101,
Takashi Iwaiffe4d12b2012-11-23 08:34:13 +010064 CS420X_MBP81,
Takashi Iwai6ab982e2013-06-17 10:19:49 +020065 CS420X_MBA42,
Takashi Iwaia6bae202009-07-06 15:15:22 +020066 CS420X_AUTO,
Takashi Iwai03efce72012-09-13 09:56:57 +020067 /* aliases */
68 CS420X_IMAC27_122 = CS420X_GPIO_23,
69 CS420X_APPLE = CS420X_GPIO_13,
Takashi Iwaia6bae202009-07-06 15:15:22 +020070};
71
Tim Howe56487c22011-07-22 16:41:00 -050072/* CS421x boards */
73enum {
74 CS421X_CDB4210,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020075 CS421X_SENSE_B,
Dylan Reid4af161072013-04-04 15:35:31 -070076 CS421X_STUMPY,
Tim Howe56487c22011-07-22 16:41:00 -050077};
78
Takashi Iwai40c20fa2009-07-06 13:00:57 +020079/* Vendor-specific processing widget */
80#define CS420X_VENDOR_NID 0x11
81#define CS_DIG_OUT1_PIN_NID 0x10
82#define CS_DIG_OUT2_PIN_NID 0x15
Daniel J Blueman16337e02012-11-04 13:19:03 +080083#define CS_DMIC1_PIN_NID 0x0e
84#define CS_DMIC2_PIN_NID 0x12
Takashi Iwai40c20fa2009-07-06 13:00:57 +020085
86/* coef indices */
87#define IDX_SPDIF_STAT 0x0000
88#define IDX_SPDIF_CTL 0x0001
89#define IDX_ADC_CFG 0x0002
90/* SZC bitmask, 4 modes below:
91 * 0 = immediate,
92 * 1 = digital immediate, analog zero-cross
93 * 2 = digtail & analog soft-ramp
94 * 3 = digital soft-ramp, analog zero-cross
95 */
96#define CS_COEF_ADC_SZC_MASK (3 << 0)
97#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
98#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
99/* PGA mode: 0 = differential, 1 = signle-ended */
100#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
101#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
102#define IDX_DAC_CFG 0x0003
103/* SZC bitmask, 4 modes below:
104 * 0 = Immediate
105 * 1 = zero-cross
106 * 2 = soft-ramp
107 * 3 = soft-ramp on zero-cross
108 */
109#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
110#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
111#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
112
113#define IDX_BEEP_CFG 0x0004
114/* 0x0008 - test reg key */
115/* 0x0009 - 0x0014 -> 12 test regs */
116/* 0x0015 - visibility reg */
117
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200118/* Cirrus Logic CS4208 */
119#define CS4208_VENDOR_NID 0x24
120
Tim Howe56487c22011-07-22 16:41:00 -0500121/*
122 * Cirrus Logic CS4210
123 *
124 * 1 DAC => HP(sense) / Speakers,
125 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
126 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
127*/
128#define CS4210_DAC_NID 0x02
129#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100130#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500131#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
132#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
133
134#define CS421X_IDX_DEV_CFG 0x01
135#define CS421X_IDX_ADC_CFG 0x02
136#define CS421X_IDX_DAC_CFG 0x03
137#define CS421X_IDX_SPK_CTL 0x04
138
139#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200140
David Henningsson5660ffd2012-01-02 12:40:17 +0100141/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
142#define CS4213_VENDOR_NID 0x09
143
144
Takashi Iwai277a57c2009-07-08 12:42:08 +0200145static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200146{
Tim Howe56487c22011-07-22 16:41:00 -0500147 struct cs_spec *spec = codec->spec;
148 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200149 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500150 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200151 AC_VERB_GET_PROC_COEF, 0);
152}
153
Takashi Iwai277a57c2009-07-08 12:42:08 +0200154static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
155 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200156{
Tim Howe56487c22011-07-22 16:41:00 -0500157 struct cs_spec *spec = codec->spec;
158 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200159 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500160 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200161 AC_VERB_SET_PROC_COEF, coef);
162}
163
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200164/*
165 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500166 * CS421x auto-output redirecting
167 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200168 */
169
Takashi Iwai1077a022012-12-19 16:39:18 +0100170static void cs_automute(struct hda_codec *codec)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200171{
172 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200173
Takashi Iwai1077a022012-12-19 16:39:18 +0100174 /* mute HPs if spdif jack (SENSE_B) is present */
175 spec->gen.master_mute = !!(spec->spdif_present && spec->sense_b);
Tim Howe56487c22011-07-22 16:41:00 -0500176
Takashi Iwai1077a022012-12-19 16:39:18 +0100177 snd_hda_gen_update_outputs(codec);
Tim Howe56487c22011-07-22 16:41:00 -0500178
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200179 if (spec->gpio_eapd_hp || spec->gpio_eapd_speaker) {
Takashi Iwai039eb752013-03-18 16:55:49 +0100180 spec->gpio_data = spec->gen.hp_jack_present ?
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100181 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200182 snd_hda_codec_write(codec, 0x01, 0,
Takashi Iwai039eb752013-03-18 16:55:49 +0100183 AC_VERB_SET_GPIO_DATA, spec->gpio_data);
Stelian Pop3a385162009-07-30 14:44:27 +0200184 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200185}
186
Takashi Iwai1077a022012-12-19 16:39:18 +0100187static bool is_active_pin(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200188{
Takashi Iwai1077a022012-12-19 16:39:18 +0100189 unsigned int val;
190 val = snd_hda_codec_get_pincfg(codec, nid);
191 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200192}
193
Takashi Iwai1077a022012-12-19 16:39:18 +0100194static void init_input_coef(struct hda_codec *codec)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200195{
196 struct cs_spec *spec = codec->spec;
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200197 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200198
David Henningsson5660ffd2012-01-02 12:40:17 +0100199 /* CS420x has multiple ADC, CS421x has single ADC */
200 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Daniel J Blueman16337e02012-11-04 13:19:03 +0800201 coef = cs_vendor_coef_get(codec, IDX_BEEP_CFG);
Tim Howe56487c22011-07-22 16:41:00 -0500202 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
Daniel J Blueman16337e02012-11-04 13:19:03 +0800203 coef |= 1 << 4; /* DMIC2 2 chan on, GPIO1 off */
Tim Howe56487c22011-07-22 16:41:00 -0500204 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
Daniel J Blueman16337e02012-11-04 13:19:03 +0800205 coef |= 1 << 3; /* DMIC1 2 chan on, GPIO0 off
Tim Howe56487c22011-07-22 16:41:00 -0500206 * No effect if SPDIF_OUT2 is
207 * selected in IDX_SPDIF_CTL.
208 */
Daniel J Blueman16337e02012-11-04 13:19:03 +0800209
210 cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
Tim Howe56487c22011-07-22 16:41:00 -0500211 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200212}
213
Takashi Iwaic42d4782011-05-02 11:36:09 +0200214static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200215 {0x11, AC_VERB_SET_PROC_STATE, 1},
216 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
217 {0x11, AC_VERB_SET_PROC_COEF,
218 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
219 | 0x0040 /* Mute DACs on FIFO error */
220 | 0x1000 /* Enable DACs High Pass Filter */
221 | 0x0400 /* Disable Coefficient Auto increment */
222 )},
Takashi Iwai829e87e2012-11-23 08:25:25 +0100223 /* ADC1/2 - Digital and Analog Soft Ramp */
224 {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
225 {0x11, AC_VERB_SET_PROC_COEF, 0x000a},
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200226 /* Beep */
Alexander Stein5a83b4b2012-11-01 13:42:37 +0100227 {0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200228 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
229
230 {} /* terminator */
231};
232
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200233static const struct hda_verb cs4208_coef_init_verbs[] = {
234 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
235 {0x24, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
236 {0x24, AC_VERB_SET_COEF_INDEX, 0x0033},
237 {0x24, AC_VERB_SET_PROC_COEF, 0x0001}, /* A1 ICS */
238 {0x24, AC_VERB_SET_COEF_INDEX, 0x0034},
239 {0x24, AC_VERB_SET_PROC_COEF, 0x1C01}, /* A1 Enable, A Thresh = 300mV */
240 {} /* terminator */
241};
242
Brian Austina769cbc2010-09-07 14:36:22 -0500243/* Errata: CS4207 rev C0/C1/C2 Silicon
244 *
245 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
246 *
247 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
248 * may be excessive (up to an additional 200 μA), which is most easily
249 * observed while the part is being held in reset (RESET# active low).
250 *
251 * Root Cause: At initial powerup of the device, the logic that drives
252 * the clock and write enable to the S/PDIF SRC RAMs is not properly
253 * initialized.
254 * Certain random patterns will cause a steady leakage current in those
255 * RAM cells. The issue will resolve once the SRCs are used (turned on).
256 *
257 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
258 * blocks, which will alleviate the issue.
259 */
260
Takashi Iwaic42d4782011-05-02 11:36:09 +0200261static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -0500262 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
263 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
264
265 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
266 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
267 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
268 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
269 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
270 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
271
272 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
273 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
274
275 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
276 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
277 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
278 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
279 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
280 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
281 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
282
Takashi Iwai38c07642011-03-03 14:54:19 +0100283#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -0500284 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
285 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
286 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +0100287#endif
Brian Austina769cbc2010-09-07 14:36:22 -0500288
289 {} /* terminator */
290};
291
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200292/* SPDIF setup */
Takashi Iwai1077a022012-12-19 16:39:18 +0100293static void init_digital_coef(struct hda_codec *codec)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200294{
295 unsigned int coef;
296
297 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
298 coef |= 0x0008; /* Replace with mute on error */
299 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
300 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
301 * SPDIF_OUT2 is shared with GPIO1 and
302 * DMIC_SDA2.
303 */
304 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200305}
306
307static int cs_init(struct hda_codec *codec)
308{
309 struct cs_spec *spec = codec->spec;
310
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200311 if (spec->vendor_nid == CS420X_VENDOR_NID) {
312 /* init_verb sequence for C0/C1/C2 errata*/
313 snd_hda_sequence_write(codec, cs_errata_init_verbs);
314 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200315 } else if (spec->vendor_nid == CS4208_VENDOR_NID) {
316 snd_hda_sequence_write(codec, cs4208_coef_init_verbs);
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200317 }
Takashi Iwaied208252009-07-07 09:04:26 +0200318
Takashi Iwai1077a022012-12-19 16:39:18 +0100319 snd_hda_gen_init(codec);
Takashi Iwai98415ea2012-11-23 08:26:58 +0100320
Takashi Iwaied208252009-07-07 09:04:26 +0200321 if (spec->gpio_mask) {
322 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
323 spec->gpio_mask);
324 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
325 spec->gpio_dir);
326 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
327 spec->gpio_data);
328 }
329
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200330 if (spec->vendor_nid == CS420X_VENDOR_NID) {
331 init_input_coef(codec);
332 init_digital_coef(codec);
333 }
Takashi Iwai01a61e12011-10-28 00:03:22 +0200334
Takashi Iwaie5f14242009-07-01 18:11:44 +0200335 return 0;
336}
337
Takashi Iwai0c52db82013-11-08 13:03:57 +0100338static int cs_build_controls(struct hda_codec *codec)
339{
340 int err;
341
342 err = snd_hda_gen_build_controls(codec);
343 if (err < 0)
344 return err;
345 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
346 return 0;
347}
348
Takashi Iwai1077a022012-12-19 16:39:18 +0100349#define cs_free snd_hda_gen_free
Takashi Iwaie5f14242009-07-01 18:11:44 +0200350
Takashi Iwaic42d4782011-05-02 11:36:09 +0200351static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwai0c52db82013-11-08 13:03:57 +0100352 .build_controls = cs_build_controls,
Takashi Iwai1077a022012-12-19 16:39:18 +0100353 .build_pcms = snd_hda_gen_build_pcms,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200354 .init = cs_init,
355 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +0200356 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200357};
358
359static int cs_parse_auto_config(struct hda_codec *codec)
360{
361 struct cs_spec *spec = codec->spec;
362 int err;
363
Takashi Iwai1077a022012-12-19 16:39:18 +0100364 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200365 if (err < 0)
366 return err;
Takashi Iwaied208252009-07-07 09:04:26 +0200367
Takashi Iwai1077a022012-12-19 16:39:18 +0100368 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
Takashi Iwaied208252009-07-07 09:04:26 +0200369 if (err < 0)
370 return err;
Takashi Iwai1077a022012-12-19 16:39:18 +0100371
Takashi Iwaie5f14242009-07-01 18:11:44 +0200372 return 0;
373}
374
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200375static const struct hda_model_fixup cs420x_models[] = {
376 { .id = CS420X_MBP53, .name = "mbp53" },
377 { .id = CS420X_MBP55, .name = "mbp55" },
378 { .id = CS420X_IMAC27, .name = "imac27" },
379 { .id = CS420X_IMAC27_122, .name = "imac27_122" },
380 { .id = CS420X_APPLE, .name = "apple" },
Takashi Iwaief596a52012-09-11 16:53:08 +0200381 { .id = CS420X_MBP101, .name = "mbp101" },
Takashi Iwaiffe4d12b2012-11-23 08:34:13 +0100382 { .id = CS420X_MBP81, .name = "mbp81" },
Takashi Iwai6ab982e2013-06-17 10:19:49 +0200383 { .id = CS420X_MBA42, .name = "mba42" },
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200384 {}
Takashi Iwaia6bae202009-07-06 15:15:22 +0200385};
386
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200387static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -0400388 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +0100389 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +0200390 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +0200391 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100392 /* this conflicts with too many other models */
393 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100394
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200395 /* codec SSID */
Takashi Iwaiffe4d12b2012-11-23 08:34:13 +0100396 SND_PCI_QUIRK(0x106b, 0x1c00, "MacBookPro 8,1", CS420X_MBP81),
Jérémy Lal7e5bea12012-01-09 17:19:45 +0100397 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwaief596a52012-09-11 16:53:08 +0200398 SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
Takashi Iwai6ab982e2013-06-17 10:19:49 +0200399 SND_PCI_QUIRK(0x106b, 0x5b00, "MacBookAir 4,2", CS420X_MBA42),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100400 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +0200401 {} /* terminator */
402};
403
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200404static const struct hda_pintbl mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -0400405 { 0x09, 0x012b4050 },
406 { 0x0a, 0x90100141 },
407 { 0x0b, 0x90100140 },
408 { 0x0c, 0x018b3020 },
409 { 0x0d, 0x90a00110 },
410 { 0x0e, 0x400000f0 },
411 { 0x0f, 0x01cbe030 },
412 { 0x10, 0x014be060 },
413 { 0x12, 0x400000f0 },
414 { 0x15, 0x400000f0 },
415 {} /* terminator */
416};
417
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200418static const struct hda_pintbl mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +0200419 { 0x09, 0x012b4030 },
420 { 0x0a, 0x90100121 },
421 { 0x0b, 0x90100120 },
422 { 0x0c, 0x400000f0 },
423 { 0x0d, 0x90a00110 },
424 { 0x0e, 0x400000f0 },
425 { 0x0f, 0x400000f0 },
426 { 0x10, 0x014be040 },
427 { 0x12, 0x400000f0 },
428 { 0x15, 0x400000f0 },
429 {} /* terminator */
430};
431
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200432static const struct hda_pintbl imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +0100433 { 0x09, 0x012b4050 },
434 { 0x0a, 0x90100140 },
435 { 0x0b, 0x90100142 },
436 { 0x0c, 0x018b3020 },
437 { 0x0d, 0x90a00110 },
438 { 0x0e, 0x400000f0 },
439 { 0x0f, 0x01cbe030 },
440 { 0x10, 0x014be060 },
441 { 0x12, 0x01ab9070 },
442 { 0x15, 0x400000f0 },
443 {} /* terminator */
444};
445
Takashi Iwaief596a52012-09-11 16:53:08 +0200446static const struct hda_pintbl mbp101_pincfgs[] = {
447 { 0x0d, 0x40ab90f0 },
448 { 0x0e, 0x90a600f0 },
449 { 0x12, 0x50a600f0 },
450 {} /* terminator */
451};
452
Takashi Iwai6ab982e2013-06-17 10:19:49 +0200453static const struct hda_pintbl mba42_pincfgs[] = {
454 { 0x09, 0x012b4030 }, /* HP */
455 { 0x0a, 0x400000f0 },
456 { 0x0b, 0x90100120 }, /* speaker */
457 { 0x0c, 0x400000f0 },
458 { 0x0d, 0x90a00110 }, /* mic */
459 { 0x0e, 0x400000f0 },
460 { 0x0f, 0x400000f0 },
461 { 0x10, 0x400000f0 },
462 { 0x12, 0x400000f0 },
463 { 0x15, 0x400000f0 },
464 {} /* terminator */
465};
466
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200467static const struct hda_pintbl mba6_pincfgs[] = {
468 { 0x10, 0x032120f0 }, /* HP */
469 { 0x11, 0x500000f0 },
470 { 0x12, 0x90100010 }, /* Speaker */
471 { 0x13, 0x500000f0 },
472 { 0x14, 0x500000f0 },
473 { 0x15, 0x770000f0 },
474 { 0x16, 0x770000f0 },
475 { 0x17, 0x430000f0 },
476 { 0x18, 0x43ab9030 }, /* Mic */
477 { 0x19, 0x770000f0 },
478 { 0x1a, 0x770000f0 },
479 { 0x1b, 0x770000f0 },
480 { 0x1c, 0x90a00090 },
481 { 0x1d, 0x500000f0 },
482 { 0x1e, 0x500000f0 },
483 { 0x1f, 0x500000f0 },
484 { 0x20, 0x500000f0 },
485 { 0x21, 0x430000f0 },
486 { 0x22, 0x430000f0 },
487 {} /* terminator */
488};
489
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200490static void cs420x_fixup_gpio_13(struct hda_codec *codec,
491 const struct hda_fixup *fix, int action)
Takashi Iwaia6bae202009-07-06 15:15:22 +0200492{
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200493 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
494 struct cs_spec *spec = codec->spec;
495 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
496 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
497 spec->gpio_mask = spec->gpio_dir =
498 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
499 }
Takashi Iwaia6bae202009-07-06 15:15:22 +0200500}
501
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200502static void cs420x_fixup_gpio_23(struct hda_codec *codec,
503 const struct hda_fixup *fix, int action)
504{
505 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
506 struct cs_spec *spec = codec->spec;
507 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
508 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
509 spec->gpio_mask = spec->gpio_dir =
510 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
511 }
512}
513
514static const struct hda_fixup cs420x_fixups[] = {
515 [CS420X_MBP53] = {
516 .type = HDA_FIXUP_PINS,
517 .v.pins = mbp53_pincfgs,
518 .chained = true,
519 .chain_id = CS420X_APPLE,
520 },
521 [CS420X_MBP55] = {
522 .type = HDA_FIXUP_PINS,
523 .v.pins = mbp55_pincfgs,
524 .chained = true,
525 .chain_id = CS420X_GPIO_13,
526 },
527 [CS420X_IMAC27] = {
528 .type = HDA_FIXUP_PINS,
529 .v.pins = imac27_pincfgs,
530 .chained = true,
531 .chain_id = CS420X_GPIO_13,
532 },
533 [CS420X_GPIO_13] = {
534 .type = HDA_FIXUP_FUNC,
535 .v.func = cs420x_fixup_gpio_13,
536 },
537 [CS420X_GPIO_23] = {
538 .type = HDA_FIXUP_FUNC,
539 .v.func = cs420x_fixup_gpio_23,
540 },
Takashi Iwaief596a52012-09-11 16:53:08 +0200541 [CS420X_MBP101] = {
542 .type = HDA_FIXUP_PINS,
543 .v.pins = mbp101_pincfgs,
544 .chained = true,
Takashi Iwaief596a52012-09-11 16:53:08 +0200545 .chain_id = CS420X_GPIO_13,
546 },
Takashi Iwaiffe4d12b2012-11-23 08:34:13 +0100547 [CS420X_MBP81] = {
548 .type = HDA_FIXUP_VERBS,
549 .v.verbs = (const struct hda_verb[]) {
550 /* internal mic ADC2: right only, single ended */
551 {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
552 {0x11, AC_VERB_SET_PROC_COEF, 0x102a},
553 {}
554 },
555 .chained = true,
556 .chain_id = CS420X_GPIO_13,
557 },
Takashi Iwai6ab982e2013-06-17 10:19:49 +0200558 [CS420X_MBA42] = {
559 .type = HDA_FIXUP_PINS,
560 .v.pins = mba42_pincfgs,
561 .chained = true,
562 .chain_id = CS420X_GPIO_13,
563 },
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200564};
565
Takashi Iwai1077a022012-12-19 16:39:18 +0100566static struct cs_spec *cs_alloc_spec(struct hda_codec *codec, int vendor_nid)
567{
568 struct cs_spec *spec;
569
570 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
571 if (!spec)
572 return NULL;
573 codec->spec = spec;
574 spec->vendor_nid = vendor_nid;
575 snd_hda_gen_spec_init(&spec->gen);
576
577 return spec;
578}
579
Takashi Iwaie5f14242009-07-01 18:11:44 +0200580static int patch_cs420x(struct hda_codec *codec)
581{
582 struct cs_spec *spec;
583 int err;
584
Takashi Iwai1077a022012-12-19 16:39:18 +0100585 spec = cs_alloc_spec(codec, CS420X_VENDOR_NID);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200586 if (!spec)
587 return -ENOMEM;
Tim Howe56487c22011-07-22 16:41:00 -0500588
Takashi Iwai6d3073e2013-03-15 14:23:32 +0100589 spec->gen.automute_hook = cs_automute;
590
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200591 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
592 cs420x_fixups);
593 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200594
Takashi Iwaied208252009-07-07 09:04:26 +0200595 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200596 if (err < 0)
597 goto error;
598
Takashi Iwaie5f14242009-07-01 18:11:44 +0200599 codec->patch_ops = cs_patch_ops;
600
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200601 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
602
Takashi Iwaie5f14242009-07-01 18:11:44 +0200603 return 0;
604
605 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +0200606 cs_free(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200607 return err;
608}
609
Tim Howe56487c22011-07-22 16:41:00 -0500610/*
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200611 * CS4208 support:
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200612 * Its layout is no longer compatible with CS4206/CS4207
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200613 */
614enum {
Takashi Iwai885845d2013-11-08 12:50:31 +0100615 CS4208_MAC_AUTO,
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200616 CS4208_MBA6,
Takashi Iwai0c52db82013-11-08 13:03:57 +0100617 CS4208_MBP11,
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200618 CS4208_GPIO0,
619};
620
621static const struct hda_model_fixup cs4208_models[] = {
622 { .id = CS4208_GPIO0, .name = "gpio0" },
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200623 { .id = CS4208_MBA6, .name = "mba6" },
Takashi Iwai0c52db82013-11-08 13:03:57 +0100624 { .id = CS4208_MBP11, .name = "mbp11" },
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200625 {}
626};
627
628static const struct snd_pci_quirk cs4208_fixup_tbl[] = {
Takashi Iwai885845d2013-11-08 12:50:31 +0100629 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS4208_MAC_AUTO),
630 {} /* terminator */
631};
632
633/* codec SSID matching */
634static const struct snd_pci_quirk cs4208_mac_fixup_tbl[] = {
Takashi Iwai0c52db82013-11-08 13:03:57 +0100635 SND_PCI_QUIRK(0x106b, 0x5e00, "MacBookPro 11,2", CS4208_MBP11),
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200636 SND_PCI_QUIRK(0x106b, 0x7100, "MacBookAir 6,1", CS4208_MBA6),
637 SND_PCI_QUIRK(0x106b, 0x7200, "MacBookAir 6,2", CS4208_MBA6),
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200638 {} /* terminator */
639};
640
641static void cs4208_fixup_gpio0(struct hda_codec *codec,
642 const struct hda_fixup *fix, int action)
643{
644 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
645 struct cs_spec *spec = codec->spec;
646 spec->gpio_eapd_hp = 0;
647 spec->gpio_eapd_speaker = 1;
648 spec->gpio_mask = spec->gpio_dir =
649 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
650 }
651}
652
Takashi Iwai885845d2013-11-08 12:50:31 +0100653static const struct hda_fixup cs4208_fixups[];
654
655/* remap the fixup from codec SSID and apply it */
656static void cs4208_fixup_mac(struct hda_codec *codec,
657 const struct hda_fixup *fix, int action)
658{
659 if (action != HDA_FIXUP_ACT_PRE_PROBE)
660 return;
661 snd_hda_pick_fixup(codec, NULL, cs4208_mac_fixup_tbl, cs4208_fixups);
662 if (codec->fixup_id < 0 || codec->fixup_id == CS4208_MAC_AUTO)
663 codec->fixup_id = CS4208_GPIO0; /* default fixup */
664 snd_hda_apply_fixup(codec, action);
665}
666
Takashi Iwai0c52db82013-11-08 13:03:57 +0100667static int cs4208_spdif_sw_put(struct snd_kcontrol *kcontrol,
668 struct snd_ctl_elem_value *ucontrol)
669{
670 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
671 struct cs_spec *spec = codec->spec;
672 hda_nid_t pin = spec->gen.autocfg.dig_out_pins[0];
673 int pinctl = ucontrol->value.integer.value[0] ? PIN_OUT : 0;
674
675 snd_hda_set_pin_ctl_cache(codec, pin, pinctl);
676 return spec->spdif_sw_put(kcontrol, ucontrol);
677}
678
679/* hook the SPDIF switch */
680static void cs4208_fixup_spdif_switch(struct hda_codec *codec,
681 const struct hda_fixup *fix, int action)
682{
683 if (action == HDA_FIXUP_ACT_BUILD) {
684 struct cs_spec *spec = codec->spec;
685 struct snd_kcontrol *kctl;
686
687 if (!spec->gen.autocfg.dig_out_pins[0])
688 return;
689 kctl = snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch");
690 if (!kctl)
691 return;
692 spec->spdif_sw_put = kctl->put;
693 kctl->put = cs4208_spdif_sw_put;
694 }
695}
696
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200697static const struct hda_fixup cs4208_fixups[] = {
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200698 [CS4208_MBA6] = {
699 .type = HDA_FIXUP_PINS,
700 .v.pins = mba6_pincfgs,
701 .chained = true,
702 .chain_id = CS4208_GPIO0,
703 },
Takashi Iwai0c52db82013-11-08 13:03:57 +0100704 [CS4208_MBP11] = {
705 .type = HDA_FIXUP_FUNC,
706 .v.func = cs4208_fixup_spdif_switch,
707 .chained = true,
708 .chain_id = CS4208_GPIO0,
709 },
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200710 [CS4208_GPIO0] = {
711 .type = HDA_FIXUP_FUNC,
712 .v.func = cs4208_fixup_gpio0,
713 },
Takashi Iwai885845d2013-11-08 12:50:31 +0100714 [CS4208_MAC_AUTO] = {
715 .type = HDA_FIXUP_FUNC,
716 .v.func = cs4208_fixup_mac,
717 },
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200718};
719
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200720/* correct the 0dB offset of input pins */
721static void cs4208_fix_amp_caps(struct hda_codec *codec, hda_nid_t adc)
722{
723 unsigned int caps;
724
725 caps = query_amp_caps(codec, adc, HDA_INPUT);
726 caps &= ~(AC_AMPCAP_OFFSET);
727 caps |= 0x02;
728 snd_hda_override_amp_caps(codec, adc, HDA_INPUT, caps);
729}
730
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200731static int patch_cs4208(struct hda_codec *codec)
732{
733 struct cs_spec *spec;
734 int err;
735
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200736 spec = cs_alloc_spec(codec, CS4208_VENDOR_NID);
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200737 if (!spec)
738 return -ENOMEM;
739
740 spec->gen.automute_hook = cs_automute;
Takashi Iwaia1114a82013-11-04 16:32:01 +0100741 /* exclude NID 0x10 (HP) from output volumes due to different steps */
742 spec->gen.out_vol_mask = 1ULL << 0x10;
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200743
744 snd_hda_pick_fixup(codec, cs4208_models, cs4208_fixup_tbl,
745 cs4208_fixups);
746 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
747
Ben Whittenb5bf0a92013-09-27 10:13:51 +0200748 snd_hda_override_wcaps(codec, 0x18,
749 get_wcaps(codec, 0x18) | AC_WCAP_STEREO);
750 cs4208_fix_amp_caps(codec, 0x18);
751 cs4208_fix_amp_caps(codec, 0x1b);
752 cs4208_fix_amp_caps(codec, 0x1c);
753
Takashi Iwaibe8cf442013-09-09 13:57:57 +0200754 err = cs_parse_auto_config(codec);
755 if (err < 0)
756 goto error;
757
758 codec->patch_ops = cs_patch_ops;
759
760 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
761
762 return 0;
763
764 error:
765 cs_free(codec);
766 return err;
767}
768
769/*
Tim Howe56487c22011-07-22 16:41:00 -0500770 * Cirrus Logic CS4210
771 *
772 * 1 DAC => HP(sense) / Speakers,
773 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
774 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
775*/
776
777/* CS4210 board names */
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200778static const struct hda_model_fixup cs421x_models[] = {
779 { .id = CS421X_CDB4210, .name = "cdb4210" },
Dylan Reid4af161072013-04-04 15:35:31 -0700780 { .id = CS421X_STUMPY, .name = "stumpy" },
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200781 {}
Tim Howe56487c22011-07-22 16:41:00 -0500782};
783
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200784static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
Tim Howe56487c22011-07-22 16:41:00 -0500785 /* Test Intel board + CDB2410 */
786 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
787 {} /* terminator */
788};
789
790/* CS4210 board pinconfigs */
791/* Default CS4210 (CDB4210)*/
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200792static const struct hda_pintbl cdb4210_pincfgs[] = {
Tim Howe56487c22011-07-22 16:41:00 -0500793 { 0x05, 0x0321401f },
794 { 0x06, 0x90170010 },
795 { 0x07, 0x03813031 },
796 { 0x08, 0xb7a70037 },
797 { 0x09, 0xb7a6003e },
798 { 0x0a, 0x034510f0 },
799 {} /* terminator */
800};
801
Dylan Reid4af161072013-04-04 15:35:31 -0700802/* Stumpy ChromeBox */
803static const struct hda_pintbl stumpy_pincfgs[] = {
804 { 0x05, 0x022120f0 },
805 { 0x06, 0x901700f0 },
806 { 0x07, 0x02a120f0 },
807 { 0x08, 0x77a70037 },
808 { 0x09, 0x77a6003e },
809 { 0x0a, 0x434510f0 },
810 {} /* terminator */
811};
812
Takashi Iwaib35aabd2012-08-24 19:09:45 +0200813/* Setup GPIO/SENSE for each board (if used) */
814static void cs421x_fixup_sense_b(struct hda_codec *codec,
815 const struct hda_fixup *fix, int action)
816{
817 struct cs_spec *spec = codec->spec;
818 if (action == HDA_FIXUP_ACT_PRE_PROBE)
819 spec->sense_b = 1;
820}
821
822static const struct hda_fixup cs421x_fixups[] = {
823 [CS421X_CDB4210] = {
824 .type = HDA_FIXUP_PINS,
825 .v.pins = cdb4210_pincfgs,
826 .chained = true,
827 .chain_id = CS421X_SENSE_B,
828 },
829 [CS421X_SENSE_B] = {
830 .type = HDA_FIXUP_FUNC,
831 .v.func = cs421x_fixup_sense_b,
Dylan Reid4af161072013-04-04 15:35:31 -0700832 },
833 [CS421X_STUMPY] = {
834 .type = HDA_FIXUP_PINS,
835 .v.pins = stumpy_pincfgs,
836 },
Tim Howe56487c22011-07-22 16:41:00 -0500837};
838
839static const struct hda_verb cs421x_coef_init_verbs[] = {
840 {0x0B, AC_VERB_SET_PROC_STATE, 1},
841 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
842 /*
843 Disable Coefficient Index Auto-Increment(DAI)=1,
844 PDREF=0
845 */
846 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
847
848 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
849 /* ADC SZCMode = Digital Soft Ramp */
850 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
851
852 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
853 {0x0B, AC_VERB_SET_PROC_COEF,
854 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
855 | 0x0004 /* Mute DAC on FIFO error */
856 | 0x0008 /* Enable DAC High Pass Filter */
857 )},
858 {} /* terminator */
859};
860
861/* Errata: CS4210 rev A1 Silicon
862 *
863 * http://www.cirrus.com/en/pubs/errata/
864 *
865 * Description:
866 * 1. Performance degredation is present in the ADC.
867 * 2. Speaker output is not completely muted upon HP detect.
868 * 3. Noise is present when clipping occurs on the amplified
869 * speaker outputs.
870 *
871 * Workaround:
872 * The following verb sequence written to the registers during
873 * initialization will correct the issues listed above.
874 */
875
876static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
877 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
878
879 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
880 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
881
882 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
883 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
884
885 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
886 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
887
888 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
889 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
890
891 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
892 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
893
894 {} /* terminator */
895};
896
897/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
898static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
899
900static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
901 struct snd_ctl_elem_info *uinfo)
902{
903 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
904 uinfo->count = 1;
905 uinfo->value.integer.min = 0;
906 uinfo->value.integer.max = 3;
907 return 0;
908}
909
910static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
911 struct snd_ctl_elem_value *ucontrol)
912{
913 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
914
915 ucontrol->value.integer.value[0] =
916 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
917 return 0;
918}
919
920static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
921 struct snd_ctl_elem_value *ucontrol)
922{
923 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
924
925 unsigned int vol = ucontrol->value.integer.value[0];
926 unsigned int coef =
927 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
928 unsigned int original_coef = coef;
929
930 coef &= ~0x0003;
931 coef |= (vol & 0x0003);
932 if (original_coef == coef)
933 return 0;
934 else {
935 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
936 return 1;
937 }
938}
939
Takashi Iwai1077a022012-12-19 16:39:18 +0100940static const struct snd_kcontrol_new cs421x_speaker_boost_ctl = {
Tim Howe56487c22011-07-22 16:41:00 -0500941
942 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
943 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
944 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
945 .name = "Speaker Boost Playback Volume",
946 .info = cs421x_boost_vol_info,
947 .get = cs421x_boost_vol_get,
948 .put = cs421x_boost_vol_put,
949 .tlv = { .p = cs421x_speaker_boost_db_scale },
950};
951
David Henningsson5660ffd2012-01-02 12:40:17 +0100952static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -0500953{
954 struct cs_spec *spec = codec->spec;
955 unsigned int def_conf, coef;
956
957 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
958 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
959
960 if (spec->gpio_mask)
961 coef |= 0x0008; /* B1,B2 are GPIOs */
962 else
963 coef &= ~0x0008;
964
965 if (spec->sense_b)
966 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
967 else
968 coef &= ~0x0010;
969
970 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
971
972 if ((spec->gpio_mask || spec->sense_b) &&
973 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
974
975 /*
976 GPIO or SENSE_B forced - disconnect the DMIC pin.
977 */
978 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
979 def_conf &= ~AC_DEFCFG_PORT_CONN;
980 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
981 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
982 }
983}
984
Takashi Iwai1077a022012-12-19 16:39:18 +0100985static void cs4210_spdif_automute(struct hda_codec *codec,
986 struct hda_jack_tbl *tbl)
Tim Howe56487c22011-07-22 16:41:00 -0500987{
988 struct cs_spec *spec = codec->spec;
Takashi Iwai1077a022012-12-19 16:39:18 +0100989 bool spdif_present = false;
990 hda_nid_t spdif_pin = spec->gen.autocfg.dig_out_pins[0];
Tim Howe56487c22011-07-22 16:41:00 -0500991
Takashi Iwai1077a022012-12-19 16:39:18 +0100992 /* detect on spdif is specific to CS4210 */
993 if (!spec->spdif_detect ||
994 spec->vendor_nid != CS4210_VENDOR_NID)
995 return;
996
997 spdif_present = snd_hda_jack_detect(codec, spdif_pin);
998 if (spdif_present == spec->spdif_present)
999 return;
1000
1001 spec->spdif_present = spdif_present;
1002 /* SPDIF TX on/off */
1003 if (spdif_present)
1004 snd_hda_set_pin_ctl(codec, spdif_pin,
1005 spdif_present ? PIN_OUT : 0);
1006
1007 cs_automute(codec);
1008}
1009
1010static void parse_cs421x_digital(struct hda_codec *codec)
1011{
1012 struct cs_spec *spec = codec->spec;
1013 struct auto_pin_cfg *cfg = &spec->gen.autocfg;
1014 int i;
Tim Howe56487c22011-07-22 16:41:00 -05001015
1016 for (i = 0; i < cfg->dig_outs; i++) {
1017 hda_nid_t nid = cfg->dig_out_pins[i];
Tim Howe56487c22011-07-22 16:41:00 -05001018 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Tim Howe56487c22011-07-22 16:41:00 -05001019 spec->spdif_detect = 1;
Takashi Iwai1077a022012-12-19 16:39:18 +01001020 snd_hda_jack_detect_enable_callback(codec, nid,
1021 SPDIF_EVENT,
1022 cs4210_spdif_automute);
Tim Howe56487c22011-07-22 16:41:00 -05001023 }
1024 }
1025}
1026
1027static int cs421x_init(struct hda_codec *codec)
1028{
1029 struct cs_spec *spec = codec->spec;
1030
David Henningsson5660ffd2012-01-02 12:40:17 +01001031 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1032 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1033 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1034 cs4210_pinmux_init(codec);
1035 }
Tim Howe56487c22011-07-22 16:41:00 -05001036
Takashi Iwai1077a022012-12-19 16:39:18 +01001037 snd_hda_gen_init(codec);
1038
Tim Howe56487c22011-07-22 16:41:00 -05001039 if (spec->gpio_mask) {
1040 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1041 spec->gpio_mask);
1042 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1043 spec->gpio_dir);
1044 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1045 spec->gpio_data);
1046 }
1047
Takashi Iwai1077a022012-12-19 16:39:18 +01001048 init_input_coef(codec);
1049
1050 cs4210_spdif_automute(codec, NULL);
Tim Howe56487c22011-07-22 16:41:00 -05001051
1052 return 0;
1053}
1054
Tim Howe56487c22011-07-22 16:41:00 -05001055static int cs421x_build_controls(struct hda_codec *codec)
1056{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001057 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001058 int err;
1059
Takashi Iwai1077a022012-12-19 16:39:18 +01001060 err = snd_hda_gen_build_controls(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001061 if (err < 0)
1062 return err;
1063
Takashi Iwai1077a022012-12-19 16:39:18 +01001064 if (spec->gen.autocfg.speaker_outs &&
1065 spec->vendor_nid == CS4210_VENDOR_NID) {
1066 err = snd_hda_ctl_add(codec, 0,
1067 snd_ctl_new1(&cs421x_speaker_boost_ctl, codec));
1068 if (err < 0)
1069 return err;
1070 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001071 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001072}
1073
Takashi Iwai1077a022012-12-19 16:39:18 +01001074static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
Tim Howe56487c22011-07-22 16:41:00 -05001075{
Takashi Iwai1077a022012-12-19 16:39:18 +01001076 unsigned int caps;
Tim Howe56487c22011-07-22 16:41:00 -05001077
Takashi Iwai1077a022012-12-19 16:39:18 +01001078 /* set the upper-limit for mixer amp to 0dB */
1079 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
1080 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
1081 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
1082 << AC_AMPCAP_NUM_STEPS_SHIFT;
1083 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
Tim Howe56487c22011-07-22 16:41:00 -05001084}
1085
1086static int cs421x_parse_auto_config(struct hda_codec *codec)
1087{
1088 struct cs_spec *spec = codec->spec;
Takashi Iwai1077a022012-12-19 16:39:18 +01001089 hda_nid_t dac = CS4210_DAC_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001090 int err;
1091
Takashi Iwai1077a022012-12-19 16:39:18 +01001092 fix_volume_caps(codec, dac);
1093
1094 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
Tim Howe56487c22011-07-22 16:41:00 -05001095 if (err < 0)
1096 return err;
Takashi Iwai1077a022012-12-19 16:39:18 +01001097
1098 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
Tim Howe56487c22011-07-22 16:41:00 -05001099 if (err < 0)
1100 return err;
Takashi Iwai1077a022012-12-19 16:39:18 +01001101
1102 parse_cs421x_digital(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001103 return 0;
1104}
1105
1106#ifdef CONFIG_PM
1107/*
1108 Manage PDREF, when transitioning to D3hot
1109 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1110*/
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001111static int cs421x_suspend(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001112{
David Henningsson5660ffd2012-01-02 12:40:17 +01001113 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001114 unsigned int coef;
1115
1116 snd_hda_shutup_pins(codec);
1117
1118 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1119 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1120 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1121 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1122
David Henningsson5660ffd2012-01-02 12:40:17 +01001123 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1124 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1125 coef |= 0x0004; /* PDREF */
1126 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1127 }
Tim Howe56487c22011-07-22 16:41:00 -05001128
1129 return 0;
1130}
1131#endif
1132
Daniel J Blueman00e17f72012-11-04 13:19:04 +08001133static const struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001134 .build_controls = cs421x_build_controls,
Takashi Iwai1077a022012-12-19 16:39:18 +01001135 .build_pcms = snd_hda_gen_build_pcms,
Tim Howe56487c22011-07-22 16:41:00 -05001136 .init = cs421x_init,
1137 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +02001138 .unsol_event = snd_hda_jack_unsol_event,
Tim Howe56487c22011-07-22 16:41:00 -05001139#ifdef CONFIG_PM
1140 .suspend = cs421x_suspend,
1141#endif
1142};
1143
David Henningsson5660ffd2012-01-02 12:40:17 +01001144static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001145{
1146 struct cs_spec *spec;
1147 int err;
1148
Takashi Iwai1077a022012-12-19 16:39:18 +01001149 spec = cs_alloc_spec(codec, CS4210_VENDOR_NID);
Tim Howe56487c22011-07-22 16:41:00 -05001150 if (!spec)
1151 return -ENOMEM;
Tim Howe56487c22011-07-22 16:41:00 -05001152
Takashi Iwai6d3073e2013-03-15 14:23:32 +01001153 spec->gen.automute_hook = cs_automute;
1154
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001155 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1156 cs421x_fixups);
1157 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Tim Howe56487c22011-07-22 16:41:00 -05001158
1159 /*
1160 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1161 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1162 is disabled.
1163 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001164 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001165
1166 err = cs421x_parse_auto_config(codec);
1167 if (err < 0)
1168 goto error;
1169
David Henningsson5660ffd2012-01-02 12:40:17 +01001170 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05001171
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001172 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1173
Tim Howe56487c22011-07-22 16:41:00 -05001174 return 0;
1175
1176 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02001177 cs_free(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001178 return err;
1179}
1180
David Henningsson5660ffd2012-01-02 12:40:17 +01001181static int patch_cs4213(struct hda_codec *codec)
1182{
1183 struct cs_spec *spec;
1184 int err;
1185
Takashi Iwai1077a022012-12-19 16:39:18 +01001186 spec = cs_alloc_spec(codec, CS4213_VENDOR_NID);
David Henningsson5660ffd2012-01-02 12:40:17 +01001187 if (!spec)
1188 return -ENOMEM;
David Henningsson5660ffd2012-01-02 12:40:17 +01001189
1190 err = cs421x_parse_auto_config(codec);
1191 if (err < 0)
1192 goto error;
1193
1194 codec->patch_ops = cs421x_patch_ops;
1195 return 0;
1196
1197 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02001198 cs_free(codec);
David Henningsson5660ffd2012-01-02 12:40:17 +01001199 return err;
1200}
1201
Takashi Iwaie5f14242009-07-01 18:11:44 +02001202
1203/*
1204 * patch entries
1205 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02001206static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001207 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1208 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
Takashi Iwaibe8cf442013-09-09 13:57:57 +02001209 { .id = 0x10134208, .name = "CS4208", .patch = patch_cs4208 },
David Henningsson5660ffd2012-01-02 12:40:17 +01001210 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
1211 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02001212 {} /* terminator */
1213};
1214
1215MODULE_ALIAS("snd-hda-codec-id:10134206");
1216MODULE_ALIAS("snd-hda-codec-id:10134207");
Takashi Iwaibe8cf442013-09-09 13:57:57 +02001217MODULE_ALIAS("snd-hda-codec-id:10134208");
Tim Howe56487c22011-07-22 16:41:00 -05001218MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01001219MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02001220
1221MODULE_LICENSE("GPL");
1222MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1223
1224static struct hda_codec_preset_list cirrus_list = {
1225 .preset = snd_hda_preset_cirrus,
1226 .owner = THIS_MODULE,
1227};
1228
1229static int __init patch_cirrus_init(void)
1230{
1231 return snd_hda_add_codec_preset(&cirrus_list);
1232}
1233
1234static void __exit patch_cirrus_exit(void)
1235{
1236 snd_hda_delete_codec_preset(&cirrus_list);
1237}
1238
1239module_init(patch_cirrus_init)
1240module_exit(patch_cirrus_exit)