blob: ed981723cdd5fcccac44461e4ae7b74448e6f6cc [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>
22#include <linux/delay.h>
23#include <linux/slab.h>
24#include <linux/pci.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040025#include <linux/module.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020026#include <sound/core.h>
27#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"
Tim Howe56487c22011-07-22 16:41:00 -050031#include <sound/tlv.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020032
33/*
34 */
35
36struct cs_spec {
Takashi Iwaib35aabd2012-08-24 19:09:45 +020037 struct hda_gen_spec gen;
38
Takashi Iwaie5f14242009-07-01 18:11:44 +020039 struct auto_pin_cfg autocfg;
40 struct hda_multi_out multiout;
41 struct snd_kcontrol *vmaster_sw;
42 struct snd_kcontrol *vmaster_vol;
43
44 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
45 hda_nid_t slave_dig_outs[2];
46
47 unsigned int input_idx[AUTO_PIN_LAST];
48 unsigned int capsrc_idx[AUTO_PIN_LAST];
49 hda_nid_t adc_nid[AUTO_PIN_LAST];
50 unsigned int adc_idx[AUTO_PIN_LAST];
51 unsigned int num_inputs;
52 unsigned int cur_input;
53 unsigned int automic_idx;
54 hda_nid_t cur_adc;
55 unsigned int cur_adc_stream_tag;
56 unsigned int cur_adc_format;
57 hda_nid_t dig_in;
58
Takashi Iwaic42d4782011-05-02 11:36:09 +020059 const struct hda_bind_ctls *capture_bind[2];
Takashi Iwaie5f14242009-07-01 18:11:44 +020060
Takashi Iwaied208252009-07-07 09:04:26 +020061 unsigned int gpio_mask;
62 unsigned int gpio_dir;
63 unsigned int gpio_data;
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010064 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
65 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
Takashi Iwaied208252009-07-07 09:04:26 +020066
Takashi Iwaie5f14242009-07-01 18:11:44 +020067 struct hda_pcm pcm_rec[2]; /* PCM information */
68
69 unsigned int hp_detect:1;
70 unsigned int mic_detect:1;
Takashi Iwaif37bc7a2012-11-08 15:59:23 +010071 unsigned int speaker_2_1:1;
Tim Howe56487c22011-07-22 16:41:00 -050072 /* CS421x */
73 unsigned int spdif_detect:1;
74 unsigned int sense_b:1;
75 hda_nid_t vendor_nid;
76 struct hda_input_mux input_mux;
77 unsigned int last_input;
Takashi Iwaie5f14242009-07-01 18:11:44 +020078};
79
Tim Howe56487c22011-07-22 16:41:00 -050080/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020081enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040082 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020083 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010084 CS420X_IMAC27,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020085 CS420X_GPIO_13,
86 CS420X_GPIO_23,
Takashi Iwaief596a52012-09-11 16:53:08 +020087 CS420X_MBP101,
88 CS420X_MBP101_COEF,
Takashi Iwaia6bae202009-07-06 15:15:22 +020089 CS420X_AUTO,
Takashi Iwai03efce72012-09-13 09:56:57 +020090 /* aliases */
91 CS420X_IMAC27_122 = CS420X_GPIO_23,
92 CS420X_APPLE = CS420X_GPIO_13,
Takashi Iwaia6bae202009-07-06 15:15:22 +020093};
94
Tim Howe56487c22011-07-22 16:41:00 -050095/* CS421x boards */
96enum {
97 CS421X_CDB4210,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020098 CS421X_SENSE_B,
Tim Howe56487c22011-07-22 16:41:00 -050099};
100
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200101/* Vendor-specific processing widget */
102#define CS420X_VENDOR_NID 0x11
103#define CS_DIG_OUT1_PIN_NID 0x10
104#define CS_DIG_OUT2_PIN_NID 0x15
Daniel J Blueman16337e02012-11-04 13:19:03 +0800105#define CS_DMIC1_PIN_NID 0x0e
106#define CS_DMIC2_PIN_NID 0x12
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200107
108/* coef indices */
109#define IDX_SPDIF_STAT 0x0000
110#define IDX_SPDIF_CTL 0x0001
111#define IDX_ADC_CFG 0x0002
112/* SZC bitmask, 4 modes below:
113 * 0 = immediate,
114 * 1 = digital immediate, analog zero-cross
115 * 2 = digtail & analog soft-ramp
116 * 3 = digital soft-ramp, analog zero-cross
117 */
118#define CS_COEF_ADC_SZC_MASK (3 << 0)
119#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
120#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
121/* PGA mode: 0 = differential, 1 = signle-ended */
122#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
123#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
124#define IDX_DAC_CFG 0x0003
125/* SZC bitmask, 4 modes below:
126 * 0 = Immediate
127 * 1 = zero-cross
128 * 2 = soft-ramp
129 * 3 = soft-ramp on zero-cross
130 */
131#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
132#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
133#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
134
135#define IDX_BEEP_CFG 0x0004
136/* 0x0008 - test reg key */
137/* 0x0009 - 0x0014 -> 12 test regs */
138/* 0x0015 - visibility reg */
139
Tim Howe56487c22011-07-22 16:41:00 -0500140/*
141 * Cirrus Logic CS4210
142 *
143 * 1 DAC => HP(sense) / Speakers,
144 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
145 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
146*/
147#define CS4210_DAC_NID 0x02
148#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100149#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500150#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
151#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
152
153#define CS421X_IDX_DEV_CFG 0x01
154#define CS421X_IDX_ADC_CFG 0x02
155#define CS421X_IDX_DAC_CFG 0x03
156#define CS421X_IDX_SPK_CTL 0x04
157
158#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200159
David Henningsson5660ffd2012-01-02 12:40:17 +0100160/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
161#define CS4213_VENDOR_NID 0x09
162
163
Takashi Iwai277a57c2009-07-08 12:42:08 +0200164static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200165{
Tim Howe56487c22011-07-22 16:41:00 -0500166 struct cs_spec *spec = codec->spec;
167 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200168 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500169 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200170 AC_VERB_GET_PROC_COEF, 0);
171}
172
Takashi Iwai277a57c2009-07-08 12:42:08 +0200173static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
174 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200175{
Tim Howe56487c22011-07-22 16:41:00 -0500176 struct cs_spec *spec = codec->spec;
177 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200178 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500179 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200180 AC_VERB_SET_PROC_COEF, coef);
181}
182
183
Takashi Iwaie5f14242009-07-01 18:11:44 +0200184#define HP_EVENT 1
185#define MIC_EVENT 2
186
187/*
188 * PCM callbacks
189 */
190static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
191 struct hda_codec *codec,
192 struct snd_pcm_substream *substream)
193{
194 struct cs_spec *spec = codec->spec;
195 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
196 hinfo);
197}
198
199static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
200 struct hda_codec *codec,
201 unsigned int stream_tag,
202 unsigned int format,
203 struct snd_pcm_substream *substream)
204{
205 struct cs_spec *spec = codec->spec;
206 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
207 stream_tag, format, substream);
208}
209
210static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
211 struct hda_codec *codec,
212 struct snd_pcm_substream *substream)
213{
214 struct cs_spec *spec = codec->spec;
215 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
216}
217
218/*
219 * Digital out
220 */
221static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
222 struct hda_codec *codec,
223 struct snd_pcm_substream *substream)
224{
225 struct cs_spec *spec = codec->spec;
226 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
227}
228
229static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
230 struct hda_codec *codec,
231 struct snd_pcm_substream *substream)
232{
233 struct cs_spec *spec = codec->spec;
234 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
235}
236
237static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
238 struct hda_codec *codec,
239 unsigned int stream_tag,
240 unsigned int format,
241 struct snd_pcm_substream *substream)
242{
243 struct cs_spec *spec = codec->spec;
244 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
245 format, substream);
246}
247
248static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
249 struct hda_codec *codec,
250 struct snd_pcm_substream *substream)
251{
252 struct cs_spec *spec = codec->spec;
253 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
254}
255
Takashi Iwai05ee7962011-11-16 18:05:11 +0100256static void cs_update_input_select(struct hda_codec *codec)
257{
258 struct cs_spec *spec = codec->spec;
259 if (spec->cur_adc)
260 snd_hda_codec_write(codec, spec->cur_adc, 0,
261 AC_VERB_SET_CONNECT_SEL,
262 spec->adc_idx[spec->cur_input]);
263}
264
Takashi Iwaie5f14242009-07-01 18:11:44 +0200265/*
266 * Analog capture
267 */
268static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
269 struct hda_codec *codec,
270 unsigned int stream_tag,
271 unsigned int format,
272 struct snd_pcm_substream *substream)
273{
274 struct cs_spec *spec = codec->spec;
275 spec->cur_adc = spec->adc_nid[spec->cur_input];
276 spec->cur_adc_stream_tag = stream_tag;
277 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100278 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200279 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
280 return 0;
281}
282
283static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
284 struct hda_codec *codec,
285 struct snd_pcm_substream *substream)
286{
287 struct cs_spec *spec = codec->spec;
288 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
289 spec->cur_adc = 0;
290 return 0;
291}
292
293/*
294 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200295static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200296 .substreams = 1,
297 .channels_min = 2,
298 .channels_max = 2,
299 .ops = {
300 .open = cs_playback_pcm_open,
301 .prepare = cs_playback_pcm_prepare,
302 .cleanup = cs_playback_pcm_cleanup
303 },
304};
305
Takashi Iwaic42d4782011-05-02 11:36:09 +0200306static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200307 .substreams = 1,
308 .channels_min = 2,
309 .channels_max = 2,
310 .ops = {
311 .prepare = cs_capture_pcm_prepare,
312 .cleanup = cs_capture_pcm_cleanup
313 },
314};
315
Takashi Iwaic42d4782011-05-02 11:36:09 +0200316static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200317 .substreams = 1,
318 .channels_min = 2,
319 .channels_max = 2,
320 .ops = {
321 .open = cs_dig_playback_pcm_open,
322 .close = cs_dig_playback_pcm_close,
323 .prepare = cs_dig_playback_pcm_prepare,
324 .cleanup = cs_dig_playback_pcm_cleanup
325 },
326};
327
Takashi Iwaic42d4782011-05-02 11:36:09 +0200328static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200329 .substreams = 1,
330 .channels_min = 2,
331 .channels_max = 2,
332};
333
334static int cs_build_pcms(struct hda_codec *codec)
335{
336 struct cs_spec *spec = codec->spec;
337 struct hda_pcm *info = spec->pcm_rec;
338
339 codec->pcm_info = info;
340 codec->num_pcms = 0;
341
342 info->name = "Cirrus Analog";
343 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
344 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
345 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
346 spec->multiout.max_channels;
Takashi Iwaiee81abb2012-11-08 17:12:10 +0100347 if (spec->speaker_2_1)
348 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
349 snd_pcm_2_1_chmaps;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200350 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200351 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
352 spec->adc_nid[spec->cur_input];
353 codec->num_pcms++;
354
355 if (!spec->multiout.dig_out_nid && !spec->dig_in)
356 return 0;
357
358 info++;
359 info->name = "Cirrus Digital";
360 info->pcm_type = spec->autocfg.dig_out_type[0];
361 if (!info->pcm_type)
362 info->pcm_type = HDA_PCM_TYPE_SPDIF;
363 if (spec->multiout.dig_out_nid) {
364 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
365 cs_pcm_digital_playback;
366 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
367 spec->multiout.dig_out_nid;
368 }
369 if (spec->dig_in) {
370 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
371 cs_pcm_digital_capture;
372 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
373 }
374 codec->num_pcms++;
375
376 return 0;
377}
378
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200379/*
380 * parse codec topology
381 */
382
Takashi Iwaie5f14242009-07-01 18:11:44 +0200383static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
384{
385 hda_nid_t dac;
386 if (!pin)
387 return 0;
388 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
389 return 0;
390 return dac;
391}
392
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200393static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
394{
395 struct cs_spec *spec = codec->spec;
396 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200397 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200398 unsigned int val;
399 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200400 return 0;
401 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200402 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200403}
404
405static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
406 unsigned int *idxp)
407{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200408 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200409 hda_nid_t nid;
410
411 nid = codec->start_nid;
412 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200413 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200414 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200415 if (type != AC_WID_AUD_IN)
416 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200417 idx = snd_hda_get_conn_index(codec, nid, pin, false);
418 if (idx >= 0) {
419 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200420 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200421 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200422 }
423 return 0;
424}
425
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200426static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
427{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200428 unsigned int val;
429 val = snd_hda_codec_get_pincfg(codec, nid);
430 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
431}
432
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200433static int parse_output(struct hda_codec *codec)
434{
435 struct cs_spec *spec = codec->spec;
436 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200437 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200438 hda_nid_t dac;
439
440 for (i = 0; i < cfg->line_outs; i++) {
441 dac = get_dac(codec, cfg->line_out_pins[i]);
442 if (!dac)
443 break;
444 spec->dac_nid[i] = dac;
445 }
446 spec->multiout.num_dacs = i;
447 spec->multiout.dac_nids = spec->dac_nid;
448 spec->multiout.max_channels = i * 2;
449
Takashi Iwaif37bc7a2012-11-08 15:59:23 +0100450 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT && i == 2)
451 spec->speaker_2_1 = 1; /* assume 2.1 speakers */
452
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200453 /* add HP and speakers */
454 extra_nids = 0;
455 for (i = 0; i < cfg->hp_outs; i++) {
456 dac = get_dac(codec, cfg->hp_pins[i]);
457 if (!dac)
458 break;
459 if (!i)
460 spec->multiout.hp_nid = dac;
461 else
462 spec->multiout.extra_out_nid[extra_nids++] = dac;
463 }
464 for (i = 0; i < cfg->speaker_outs; i++) {
465 dac = get_dac(codec, cfg->speaker_pins[i]);
466 if (!dac)
467 break;
468 spec->multiout.extra_out_nid[extra_nids++] = dac;
469 }
470
471 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
472 cfg->speaker_outs = cfg->line_outs;
473 memcpy(cfg->speaker_pins, cfg->line_out_pins,
474 sizeof(cfg->speaker_pins));
475 cfg->line_outs = 0;
David Henningsson34c3d192012-11-21 10:03:10 +0100476 memset(cfg->line_out_pins, 0, sizeof(cfg->line_out_pins));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200477 }
478
479 return 0;
480}
481
482static int parse_input(struct hda_codec *codec)
483{
484 struct cs_spec *spec = codec->spec;
485 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200486 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200487
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200488 for (i = 0; i < cfg->num_inputs; i++) {
489 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200490 spec->input_idx[spec->num_inputs] = i;
491 spec->capsrc_idx[i] = spec->num_inputs++;
492 spec->cur_input = i;
493 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
494 }
495 if (!spec->num_inputs)
496 return 0;
497
498 /* check whether the automatic mic switch is available */
499 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200500 cfg->inputs[0].type == AUTO_PIN_MIC &&
501 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200502 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
503 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200504 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200505 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200506 }
507 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200508 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200509 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200510 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200511 }
512 }
513 }
514 return 0;
515}
516
517
518static int parse_digital_output(struct hda_codec *codec)
519{
520 struct cs_spec *spec = codec->spec;
521 struct auto_pin_cfg *cfg = &spec->autocfg;
522 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200523
524 if (!cfg->dig_outs)
525 return 0;
526 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
527 return 0;
528 spec->multiout.dig_out_nid = nid;
529 spec->multiout.share_spdif = 1;
530 if (cfg->dig_outs > 1 &&
531 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
532 spec->slave_dig_outs[0] = nid;
533 codec->slave_dig_outs = spec->slave_dig_outs;
534 }
535 return 0;
536}
537
538static int parse_digital_input(struct hda_codec *codec)
539{
540 struct cs_spec *spec = codec->spec;
541 struct auto_pin_cfg *cfg = &spec->autocfg;
542 int idx;
543
Takashi Iwai60e53882009-07-06 15:01:09 +0200544 if (cfg->dig_in_pin)
545 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
546 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200547}
548
549/*
550 * create mixer controls
551 */
552
Takashi Iwaiea734962011-01-17 11:29:34 +0100553static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200554
555static int add_mute(struct hda_codec *codec, const char *name, int index,
556 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
557{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200558 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200559 struct snd_kcontrol_new knew =
560 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
561 knew.private_value = pval;
562 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
563 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100564 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100565 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200566}
567
568static int add_volume(struct hda_codec *codec, const char *name,
569 int index, unsigned int pval, int dir,
570 struct snd_kcontrol **kctlp)
571{
David Henningsson2e1210b2011-09-14 13:22:54 +0200572 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200573 struct snd_kcontrol_new knew =
574 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
575 knew.private_value = pval;
576 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
577 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100578 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100579 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200580}
581
582static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
583{
584 unsigned int caps;
585
586 /* set the upper-limit for mixer amp to 0dB */
587 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
588 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
589 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
590 << AC_AMPCAP_NUM_STEPS_SHIFT;
591 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
592}
593
594static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
595{
596 struct cs_spec *spec = codec->spec;
597 unsigned int tlv[4];
598 int err;
599
600 spec->vmaster_sw =
601 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100602 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200603 if (err < 0)
604 return err;
605
606 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
607 spec->vmaster_vol =
608 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100609 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200610 if (err < 0)
611 return err;
612 return 0;
613}
614
615static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
616 int num_ctls, int type)
617{
618 struct cs_spec *spec = codec->spec;
619 const char *name;
620 int err, index;
621 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200622 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200623 "Front Speaker", "Surround Speaker", "Bass Speaker"
624 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200625 static const char * const line_outs[] = {
Takashi Iwaie49a3432012-03-01 18:14:41 +0100626 "Front Line Out", "Surround Line Out", "Bass Line Out"
Takashi Iwaie5f14242009-07-01 18:11:44 +0200627 };
628
629 fix_volume_caps(codec, dac);
630 if (!spec->vmaster_sw) {
631 err = add_vmaster(codec, dac);
632 if (err < 0)
633 return err;
634 }
635
636 index = 0;
637 switch (type) {
638 case AUTO_PIN_HP_OUT:
639 name = "Headphone";
640 index = idx;
641 break;
642 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwaif37bc7a2012-11-08 15:59:23 +0100643 if (spec->speaker_2_1)
644 name = idx ? "Bass Speaker" : "Speaker";
645 else if (num_ctls > 1)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200646 name = speakers[idx];
647 else
648 name = "Speaker";
649 break;
650 default:
651 if (num_ctls > 1)
652 name = line_outs[idx];
653 else
Takashi Iwaie49a3432012-03-01 18:14:41 +0100654 name = "Line Out";
Takashi Iwaie5f14242009-07-01 18:11:44 +0200655 break;
656 }
657
658 err = add_mute(codec, name, index,
659 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
660 if (err < 0)
661 return err;
662 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
663 if (err < 0)
664 return err;
665
666 err = add_volume(codec, name, index,
667 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
668 if (err < 0)
669 return err;
670 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
671 if (err < 0)
672 return err;
673
674 return 0;
675}
676
677static int build_output(struct hda_codec *codec)
678{
679 struct cs_spec *spec = codec->spec;
680 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200681 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200682
683 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200684 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
685 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200686 if (err < 0)
687 return err;
688 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200689 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200690 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
691 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200692 if (err < 0)
693 return err;
694 }
695 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200696 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
697 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200698 if (err < 0)
699 return err;
700 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200701 return 0;
702}
703
704/*
705 */
706
Takashi Iwaic42d4782011-05-02 11:36:09 +0200707static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200708 HDA_BIND_SW("Capture Switch", 0),
709 HDA_BIND_VOL("Capture Volume", 0),
710};
711
Takashi Iwaiea359292009-07-06 12:58:59 +0200712static int change_cur_input(struct hda_codec *codec, unsigned int idx,
713 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200714{
715 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200716
Takashi Iwaiea359292009-07-06 12:58:59 +0200717 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200718 return 0;
719 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
720 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200721 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200722 spec->cur_adc = spec->adc_nid[idx];
723 snd_hda_codec_setup_stream(codec, spec->cur_adc,
724 spec->cur_adc_stream_tag, 0,
725 spec->cur_adc_format);
726 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200727 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100728 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200729 return 1;
730}
731
732static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
733 struct snd_ctl_elem_info *uinfo)
734{
735 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
736 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200737 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200738 unsigned int idx;
739
740 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
741 uinfo->count = 1;
742 uinfo->value.enumerated.items = spec->num_inputs;
743 if (uinfo->value.enumerated.item >= spec->num_inputs)
744 uinfo->value.enumerated.item = spec->num_inputs - 1;
745 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100746 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
747 uinfo->value.enumerated.name,
748 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200749 return 0;
750}
751
752static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
753 struct snd_ctl_elem_value *ucontrol)
754{
755 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
756 struct cs_spec *spec = codec->spec;
757 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
758 return 0;
759}
760
761static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
762 struct snd_ctl_elem_value *ucontrol)
763{
764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
765 struct cs_spec *spec = codec->spec;
766 unsigned int idx = ucontrol->value.enumerated.item[0];
767
768 if (idx >= spec->num_inputs)
769 return -EINVAL;
770 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200771 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200772}
773
Takashi Iwaic42d4782011-05-02 11:36:09 +0200774static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200775 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
776 .name = "Capture Source",
777 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
778 .info = cs_capture_source_info,
779 .get = cs_capture_source_get,
780 .put = cs_capture_source_put,
781};
782
Takashi Iwaic42d4782011-05-02 11:36:09 +0200783static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200784 struct hda_ctl_ops *ops)
785{
786 struct cs_spec *spec = codec->spec;
787 struct hda_bind_ctls *bind;
788 int i, n;
789
790 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
791 GFP_KERNEL);
792 if (!bind)
793 return NULL;
794 bind->ops = ops;
795 n = 0;
796 for (i = 0; i < AUTO_PIN_LAST; i++) {
797 if (!spec->adc_nid[i])
798 continue;
799 bind->values[n++] =
800 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
801 spec->adc_idx[i], HDA_INPUT);
802 }
803 return bind;
804}
805
Takashi Iwai6a929342010-10-11 15:16:20 +0200806/* add a (input-boost) volume control to the given input pin */
807static int add_input_volume_control(struct hda_codec *codec,
808 struct auto_pin_cfg *cfg,
809 int item)
810{
811 hda_nid_t pin = cfg->inputs[item].pin;
812 u32 caps;
813 const char *label;
814 struct snd_kcontrol *kctl;
815
816 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
817 return 0;
818 caps = query_amp_caps(codec, pin, HDA_INPUT);
819 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
820 if (caps <= 1)
821 return 0;
822 label = hda_get_autocfg_input_label(codec, cfg, item);
823 return add_volume(codec, label, 0,
824 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
825}
826
Takashi Iwaie5f14242009-07-01 18:11:44 +0200827static int build_input(struct hda_codec *codec)
828{
829 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200830 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200831
Takashi Iwaie5f14242009-07-01 18:11:44 +0200832 if (!spec->num_inputs)
833 return 0;
834
Takashi Iwaie5f14242009-07-01 18:11:44 +0200835 /* make bind-capture */
836 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
837 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
838 for (i = 0; i < 2; i++) {
839 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100840 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200841 if (!spec->capture_bind[i])
842 return -ENOMEM;
843 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
844 if (!kctl)
845 return -ENOMEM;
846 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100847 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200848 if (err < 0)
849 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100850 for (n = 0; n < AUTO_PIN_LAST; n++) {
851 if (!spec->adc_nid[n])
852 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700853 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100854 if (err < 0)
855 return err;
856 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200857 }
858
859 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100860 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200861 snd_ctl_new1(&cs_capture_source, codec));
862 if (err < 0)
863 return err;
864 }
865
Takashi Iwai6a929342010-10-11 15:16:20 +0200866 for (i = 0; i < spec->num_inputs; i++) {
867 err = add_input_volume_control(codec, &spec->autocfg, i);
868 if (err < 0)
869 return err;
870 }
871
Takashi Iwaie5f14242009-07-01 18:11:44 +0200872 return 0;
873}
874
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200875/*
876 */
877
Takashi Iwaie5f14242009-07-01 18:11:44 +0200878static int build_digital_output(struct hda_codec *codec)
879{
880 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200881 int err;
882
Takashi Iwai63b24132009-07-09 11:45:59 +0200883 if (!spec->multiout.dig_out_nid)
884 return 0;
885
Takashi Iwaidcda5802012-10-12 17:24:51 +0200886 err = snd_hda_create_dig_out_ctls(codec, spec->multiout.dig_out_nid,
887 spec->multiout.dig_out_nid,
888 spec->pcm_rec[1].pcm_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200889 if (err < 0)
890 return err;
891 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
892 if (err < 0)
893 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200894 return 0;
895}
896
897static int build_digital_input(struct hda_codec *codec)
898{
899 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200900 if (spec->dig_in)
901 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
902 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200903}
904
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200905/*
906 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500907 * CS421x auto-output redirecting
908 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200909 */
910
David Henningsson5c2e4e02012-10-08 15:44:15 +0200911static void cs_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200912{
913 struct cs_spec *spec = codec->spec;
914 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200915 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500916 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200917 hda_nid_t nid;
918 int i;
919
Tim Howe56487c22011-07-22 16:41:00 -0500920 spdif_present = 0;
921 if (cfg->dig_outs) {
922 nid = cfg->dig_out_pins[0];
923 if (is_jack_detectable(codec, nid)) {
924 /*
925 TODO: SPDIF output redirect when SENSE_B is enabled.
926 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
927 assumed.
928 */
929 if (snd_hda_jack_detect(codec, nid)
930 /* && spec->sense_b */)
931 spdif_present = 1;
932 }
933 }
934
Takashi Iwaie5f14242009-07-01 18:11:44 +0200935 hp_present = 0;
936 for (i = 0; i < cfg->hp_outs; i++) {
937 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200938 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200939 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800940 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200941 if (hp_present)
942 break;
943 }
Tim Howe56487c22011-07-22 16:41:00 -0500944
945 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200946 for (i = 0; i < cfg->speaker_outs; i++) {
David Henningsson78e2a922012-01-02 12:40:16 +0100947 int pin_ctl = hp_present ? 0 : PIN_OUT;
David Henningsson5660ffd2012-01-02 12:40:17 +0100948 /* detect on spdif is specific to CS4210 */
949 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
David Henningsson78e2a922012-01-02 12:40:16 +0100950 pin_ctl = 0;
951
Takashi Iwaie5f14242009-07-01 18:11:44 +0200952 nid = cfg->speaker_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200953 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200954 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100955 if (spec->gpio_eapd_hp) {
956 unsigned int gpio = hp_present ?
957 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200958 snd_hda_codec_write(codec, 0x01, 0,
959 AC_VERB_SET_GPIO_DATA, gpio);
960 }
Tim Howe56487c22011-07-22 16:41:00 -0500961
David Henningsson5660ffd2012-01-02 12:40:17 +0100962 /* specific to CS4210 */
963 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500964 /* mute HPs if spdif jack (SENSE_B) is present */
965 for (i = 0; i < cfg->hp_outs; i++) {
966 nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200967 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500968 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
969 }
970
971 /* SPDIF TX on/off */
972 if (cfg->dig_outs) {
973 nid = cfg->dig_out_pins[0];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200974 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500975 spdif_present ? PIN_OUT : 0);
976
977 }
978 /* Update board GPIOs if neccessary ... */
979 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200980}
981
Tim Howe56487c22011-07-22 16:41:00 -0500982/*
983 * Auto-input redirect for CS421x
984 * Switch max 3 inputs of a single ADC (nid 3)
985*/
986
David Henningsson5c2e4e02012-10-08 15:44:15 +0200987static void cs_automic(struct hda_codec *codec, struct hda_jack_tbl *tbl)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200988{
989 struct cs_spec *spec = codec->spec;
990 struct auto_pin_cfg *cfg = &spec->autocfg;
991 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800992 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500993
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200994 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800995 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500996
997 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100998 if (spec->vendor_nid == CS420X_VENDOR_NID) {
999 if (present)
1000 change_cur_input(codec, spec->automic_idx, 0);
1001 else
1002 change_cur_input(codec, !spec->automic_idx, 0);
1003 } else {
Tim Howe56487c22011-07-22 16:41:00 -05001004 if (present) {
Dylan Reidf70eecd2012-01-31 13:04:41 -08001005 if (spec->cur_input != spec->automic_idx) {
1006 spec->last_input = spec->cur_input;
1007 spec->cur_input = spec->automic_idx;
1008 }
Tim Howe56487c22011-07-22 16:41:00 -05001009 } else {
1010 spec->cur_input = spec->last_input;
1011 }
Takashi Iwai05ee7962011-11-16 18:05:11 +01001012 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001013 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001014}
1015
1016/*
1017 */
1018
1019static void init_output(struct hda_codec *codec)
1020{
1021 struct cs_spec *spec = codec->spec;
1022 struct auto_pin_cfg *cfg = &spec->autocfg;
1023 int i;
1024
1025 /* mute first */
1026 for (i = 0; i < spec->multiout.num_dacs; i++)
1027 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1028 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1029 if (spec->multiout.hp_nid)
1030 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1031 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1032 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1033 if (!spec->multiout.extra_out_nid[i])
1034 break;
1035 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1036 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1037 }
1038
1039 /* set appropriate pin controls */
1040 for (i = 0; i < cfg->line_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001041 snd_hda_set_pin_ctl(codec, cfg->line_out_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001042 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001043 for (i = 0; i < cfg->hp_outs; i++) {
1044 hda_nid_t nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001045 snd_hda_set_pin_ctl(codec, nid, PIN_HP);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001046 if (!cfg->speaker_outs)
1047 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001048 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
David Henningsson5c2e4e02012-10-08 15:44:15 +02001049 snd_hda_jack_detect_enable_callback(codec, nid, HP_EVENT, cs_automute);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001050 spec->hp_detect = 1;
1051 }
1052 }
Tim Howe56487c22011-07-22 16:41:00 -05001053
1054 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001055 for (i = 0; i < cfg->speaker_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001056 snd_hda_set_pin_ctl(codec, cfg->speaker_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001057
1058 /* SPDIF is enabled on presence detect for CS421x */
1059 if (spec->hp_detect || spec->spdif_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001060 cs_automute(codec, NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001061}
1062
1063static void init_input(struct hda_codec *codec)
1064{
1065 struct cs_spec *spec = codec->spec;
1066 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001067 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001068 int i;
1069
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001070 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001071 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001072 hda_nid_t pin = cfg->inputs[i].pin;
1073 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001074 continue;
1075 /* set appropriate pin control and mute first */
1076 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +02001077 if (cfg->inputs[i].type == AUTO_PIN_MIC)
1078 ctl |= snd_hda_get_default_vref(codec, pin);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001079 snd_hda_set_pin_ctl(codec, pin, ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001080 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1081 AC_VERB_SET_AMP_GAIN_MUTE,
1082 AMP_IN_MUTE(spec->adc_idx[i]));
1083 if (spec->mic_detect && spec->automic_idx == i)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001084 snd_hda_jack_detect_enable_callback(codec, pin, MIC_EVENT, cs_automic);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001085 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001086 /* CS420x has multiple ADC, CS421x has single ADC */
1087 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001088 change_cur_input(codec, spec->cur_input, 1);
1089 if (spec->mic_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001090 cs_automic(codec, NULL);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001091
Tim Howe56487c22011-07-22 16:41:00 -05001092 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
Daniel J Blueman16337e02012-11-04 13:19:03 +08001093 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1094
1095 coef = cs_vendor_coef_get(codec, IDX_BEEP_CFG);
Tim Howe56487c22011-07-22 16:41:00 -05001096 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
Daniel J Blueman16337e02012-11-04 13:19:03 +08001097 coef |= 1 << 4; /* DMIC2 2 chan on, GPIO1 off */
Tim Howe56487c22011-07-22 16:41:00 -05001098 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
Daniel J Blueman16337e02012-11-04 13:19:03 +08001099 coef |= 1 << 3; /* DMIC1 2 chan on, GPIO0 off
Tim Howe56487c22011-07-22 16:41:00 -05001100 * No effect if SPDIF_OUT2 is
1101 * selected in IDX_SPDIF_CTL.
1102 */
Daniel J Blueman16337e02012-11-04 13:19:03 +08001103
1104 cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001105 } else {
1106 if (spec->mic_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001107 cs_automic(codec, NULL);
David Henningsson5660ffd2012-01-02 12:40:17 +01001108 else {
1109 spec->cur_adc = spec->adc_nid[spec->cur_input];
1110 cs_update_input_select(codec);
1111 }
Tim Howe56487c22011-07-22 16:41:00 -05001112 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001113}
1114
Takashi Iwaic42d4782011-05-02 11:36:09 +02001115static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001116 {0x11, AC_VERB_SET_PROC_STATE, 1},
1117 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1118 {0x11, AC_VERB_SET_PROC_COEF,
1119 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1120 | 0x0040 /* Mute DACs on FIFO error */
1121 | 0x1000 /* Enable DACs High Pass Filter */
1122 | 0x0400 /* Disable Coefficient Auto increment */
1123 )},
1124 /* Beep */
Alexander Stein5a83b4b2012-11-01 13:42:37 +01001125 {0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001126 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1127
1128 {} /* terminator */
1129};
1130
Brian Austina769cbc2010-09-07 14:36:22 -05001131/* Errata: CS4207 rev C0/C1/C2 Silicon
1132 *
1133 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1134 *
1135 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1136 * may be excessive (up to an additional 200 μA), which is most easily
1137 * observed while the part is being held in reset (RESET# active low).
1138 *
1139 * Root Cause: At initial powerup of the device, the logic that drives
1140 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1141 * initialized.
1142 * Certain random patterns will cause a steady leakage current in those
1143 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1144 *
1145 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1146 * blocks, which will alleviate the issue.
1147 */
1148
Takashi Iwaic42d4782011-05-02 11:36:09 +02001149static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001150 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1151 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1152
1153 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1154 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1155 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1156 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1157 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1158 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1159
1160 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1161 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1162
1163 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1164 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1165 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1166 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1167 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1168 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1169 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1170
Takashi Iwai38c07642011-03-03 14:54:19 +01001171#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001172 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1173 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1174 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001175#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001176
1177 {} /* terminator */
1178};
1179
Takashi Iwaief596a52012-09-11 16:53:08 +02001180static const struct hda_verb mbp101_init_verbs[] = {
1181 {0x11, AC_VERB_SET_COEF_INDEX, 0x0002},
1182 {0x11, AC_VERB_SET_PROC_COEF, 0x100a},
1183 {0x11, AC_VERB_SET_COEF_INDEX, 0x0004},
1184 {0x11, AC_VERB_SET_PROC_COEF, 0x000f},
1185 {}
1186};
1187
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001188/* SPDIF setup */
1189static void init_digital(struct hda_codec *codec)
1190{
1191 unsigned int coef;
1192
1193 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1194 coef |= 0x0008; /* Replace with mute on error */
1195 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1196 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1197 * SPDIF_OUT2 is shared with GPIO1 and
1198 * DMIC_SDA2.
1199 */
1200 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001201}
1202
1203static int cs_init(struct hda_codec *codec)
1204{
1205 struct cs_spec *spec = codec->spec;
1206
Brian Austina769cbc2010-09-07 14:36:22 -05001207 /* init_verb sequence for C0/C1/C2 errata*/
1208 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1209
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001210 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001211
1212 if (spec->gpio_mask) {
1213 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1214 spec->gpio_mask);
1215 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1216 spec->gpio_dir);
1217 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1218 spec->gpio_data);
1219 }
1220
Takashi Iwaie5f14242009-07-01 18:11:44 +02001221 init_output(codec);
1222 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001223 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001224
Takashi Iwaie5f14242009-07-01 18:11:44 +02001225 return 0;
1226}
1227
1228static int cs_build_controls(struct hda_codec *codec)
1229{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001230 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001231 int err;
1232
1233 err = build_output(codec);
1234 if (err < 0)
1235 return err;
1236 err = build_input(codec);
1237 if (err < 0)
1238 return err;
1239 err = build_digital_output(codec);
1240 if (err < 0)
1241 return err;
1242 err = build_digital_input(codec);
1243 if (err < 0)
1244 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001245 err = cs_init(codec);
1246 if (err < 0)
1247 return err;
1248
1249 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1250 if (err < 0)
1251 return err;
1252
1253 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001254}
1255
1256static void cs_free(struct hda_codec *codec)
1257{
1258 struct cs_spec *spec = codec->spec;
1259 kfree(spec->capture_bind[0]);
1260 kfree(spec->capture_bind[1]);
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001261 snd_hda_gen_free(&spec->gen);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001262 kfree(codec->spec);
1263}
1264
Takashi Iwaic42d4782011-05-02 11:36:09 +02001265static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001266 .build_controls = cs_build_controls,
1267 .build_pcms = cs_build_pcms,
1268 .init = cs_init,
1269 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +02001270 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwaie5f14242009-07-01 18:11:44 +02001271};
1272
1273static int cs_parse_auto_config(struct hda_codec *codec)
1274{
1275 struct cs_spec *spec = codec->spec;
1276 int err;
1277
1278 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1279 if (err < 0)
1280 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001281
1282 err = parse_output(codec);
1283 if (err < 0)
1284 return err;
1285 err = parse_input(codec);
1286 if (err < 0)
1287 return err;
1288 err = parse_digital_output(codec);
1289 if (err < 0)
1290 return err;
1291 err = parse_digital_input(codec);
1292 if (err < 0)
1293 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001294 return 0;
1295}
1296
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001297static const struct hda_model_fixup cs420x_models[] = {
1298 { .id = CS420X_MBP53, .name = "mbp53" },
1299 { .id = CS420X_MBP55, .name = "mbp55" },
1300 { .id = CS420X_IMAC27, .name = "imac27" },
1301 { .id = CS420X_IMAC27_122, .name = "imac27_122" },
1302 { .id = CS420X_APPLE, .name = "apple" },
Takashi Iwaief596a52012-09-11 16:53:08 +02001303 { .id = CS420X_MBP101, .name = "mbp101" },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001304 {}
Takashi Iwaia6bae202009-07-06 15:15:22 +02001305};
1306
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001307static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001308 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001309 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001310 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001311 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001312 /* this conflicts with too many other models */
1313 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001314
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001315 /* codec SSID */
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001316 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwaief596a52012-09-11 16:53:08 +02001317 SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001318 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001319 {} /* terminator */
1320};
1321
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001322static const struct hda_pintbl mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001323 { 0x09, 0x012b4050 },
1324 { 0x0a, 0x90100141 },
1325 { 0x0b, 0x90100140 },
1326 { 0x0c, 0x018b3020 },
1327 { 0x0d, 0x90a00110 },
1328 { 0x0e, 0x400000f0 },
1329 { 0x0f, 0x01cbe030 },
1330 { 0x10, 0x014be060 },
1331 { 0x12, 0x400000f0 },
1332 { 0x15, 0x400000f0 },
1333 {} /* terminator */
1334};
1335
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001336static const struct hda_pintbl mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001337 { 0x09, 0x012b4030 },
1338 { 0x0a, 0x90100121 },
1339 { 0x0b, 0x90100120 },
1340 { 0x0c, 0x400000f0 },
1341 { 0x0d, 0x90a00110 },
1342 { 0x0e, 0x400000f0 },
1343 { 0x0f, 0x400000f0 },
1344 { 0x10, 0x014be040 },
1345 { 0x12, 0x400000f0 },
1346 { 0x15, 0x400000f0 },
1347 {} /* terminator */
1348};
1349
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001350static const struct hda_pintbl imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001351 { 0x09, 0x012b4050 },
1352 { 0x0a, 0x90100140 },
1353 { 0x0b, 0x90100142 },
1354 { 0x0c, 0x018b3020 },
1355 { 0x0d, 0x90a00110 },
1356 { 0x0e, 0x400000f0 },
1357 { 0x0f, 0x01cbe030 },
1358 { 0x10, 0x014be060 },
1359 { 0x12, 0x01ab9070 },
1360 { 0x15, 0x400000f0 },
1361 {} /* terminator */
1362};
1363
Takashi Iwaief596a52012-09-11 16:53:08 +02001364static const struct hda_pintbl mbp101_pincfgs[] = {
1365 { 0x0d, 0x40ab90f0 },
1366 { 0x0e, 0x90a600f0 },
1367 { 0x12, 0x50a600f0 },
1368 {} /* terminator */
1369};
1370
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001371static void cs420x_fixup_gpio_13(struct hda_codec *codec,
1372 const struct hda_fixup *fix, int action)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001373{
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001374 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1375 struct cs_spec *spec = codec->spec;
1376 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1377 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1378 spec->gpio_mask = spec->gpio_dir =
1379 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1380 }
Takashi Iwaia6bae202009-07-06 15:15:22 +02001381}
1382
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001383static void cs420x_fixup_gpio_23(struct hda_codec *codec,
1384 const struct hda_fixup *fix, int action)
1385{
1386 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1387 struct cs_spec *spec = codec->spec;
1388 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1389 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1390 spec->gpio_mask = spec->gpio_dir =
1391 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1392 }
1393}
1394
1395static const struct hda_fixup cs420x_fixups[] = {
1396 [CS420X_MBP53] = {
1397 .type = HDA_FIXUP_PINS,
1398 .v.pins = mbp53_pincfgs,
1399 .chained = true,
1400 .chain_id = CS420X_APPLE,
1401 },
1402 [CS420X_MBP55] = {
1403 .type = HDA_FIXUP_PINS,
1404 .v.pins = mbp55_pincfgs,
1405 .chained = true,
1406 .chain_id = CS420X_GPIO_13,
1407 },
1408 [CS420X_IMAC27] = {
1409 .type = HDA_FIXUP_PINS,
1410 .v.pins = imac27_pincfgs,
1411 .chained = true,
1412 .chain_id = CS420X_GPIO_13,
1413 },
1414 [CS420X_GPIO_13] = {
1415 .type = HDA_FIXUP_FUNC,
1416 .v.func = cs420x_fixup_gpio_13,
1417 },
1418 [CS420X_GPIO_23] = {
1419 .type = HDA_FIXUP_FUNC,
1420 .v.func = cs420x_fixup_gpio_23,
1421 },
Takashi Iwaief596a52012-09-11 16:53:08 +02001422 [CS420X_MBP101] = {
1423 .type = HDA_FIXUP_PINS,
1424 .v.pins = mbp101_pincfgs,
1425 .chained = true,
1426 .chain_id = CS420X_MBP101_COEF,
1427 },
1428 [CS420X_MBP101_COEF] = {
1429 .type = HDA_FIXUP_VERBS,
1430 .v.verbs = mbp101_init_verbs,
1431 .chained = true,
1432 .chain_id = CS420X_GPIO_13,
1433 },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001434};
1435
Takashi Iwaie5f14242009-07-01 18:11:44 +02001436static int patch_cs420x(struct hda_codec *codec)
1437{
1438 struct cs_spec *spec;
1439 int err;
1440
1441 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1442 if (!spec)
1443 return -ENOMEM;
1444 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001445 snd_hda_gen_init(&spec->gen);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001446
Tim Howe56487c22011-07-22 16:41:00 -05001447 spec->vendor_nid = CS420X_VENDOR_NID;
1448
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001449 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
1450 cs420x_fixups);
1451 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001452
Takashi Iwaied208252009-07-07 09:04:26 +02001453 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001454 if (err < 0)
1455 goto error;
1456
Takashi Iwaie5f14242009-07-01 18:11:44 +02001457 codec->patch_ops = cs_patch_ops;
1458
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001459 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1460
Takashi Iwaie5f14242009-07-01 18:11:44 +02001461 return 0;
1462
1463 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02001464 cs_free(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001465 codec->spec = NULL;
1466 return err;
1467}
1468
Tim Howe56487c22011-07-22 16:41:00 -05001469/*
1470 * Cirrus Logic CS4210
1471 *
1472 * 1 DAC => HP(sense) / Speakers,
1473 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1474 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1475*/
1476
1477/* CS4210 board names */
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001478static const struct hda_model_fixup cs421x_models[] = {
1479 { .id = CS421X_CDB4210, .name = "cdb4210" },
1480 {}
Tim Howe56487c22011-07-22 16:41:00 -05001481};
1482
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001483static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001484 /* Test Intel board + CDB2410 */
1485 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1486 {} /* terminator */
1487};
1488
1489/* CS4210 board pinconfigs */
1490/* Default CS4210 (CDB4210)*/
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001491static const struct hda_pintbl cdb4210_pincfgs[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001492 { 0x05, 0x0321401f },
1493 { 0x06, 0x90170010 },
1494 { 0x07, 0x03813031 },
1495 { 0x08, 0xb7a70037 },
1496 { 0x09, 0xb7a6003e },
1497 { 0x0a, 0x034510f0 },
1498 {} /* terminator */
1499};
1500
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001501/* Setup GPIO/SENSE for each board (if used) */
1502static void cs421x_fixup_sense_b(struct hda_codec *codec,
1503 const struct hda_fixup *fix, int action)
1504{
1505 struct cs_spec *spec = codec->spec;
1506 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1507 spec->sense_b = 1;
1508}
1509
1510static const struct hda_fixup cs421x_fixups[] = {
1511 [CS421X_CDB4210] = {
1512 .type = HDA_FIXUP_PINS,
1513 .v.pins = cdb4210_pincfgs,
1514 .chained = true,
1515 .chain_id = CS421X_SENSE_B,
1516 },
1517 [CS421X_SENSE_B] = {
1518 .type = HDA_FIXUP_FUNC,
1519 .v.func = cs421x_fixup_sense_b,
1520 }
Tim Howe56487c22011-07-22 16:41:00 -05001521};
1522
1523static const struct hda_verb cs421x_coef_init_verbs[] = {
1524 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1525 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1526 /*
1527 Disable Coefficient Index Auto-Increment(DAI)=1,
1528 PDREF=0
1529 */
1530 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1531
1532 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1533 /* ADC SZCMode = Digital Soft Ramp */
1534 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1535
1536 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1537 {0x0B, AC_VERB_SET_PROC_COEF,
1538 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1539 | 0x0004 /* Mute DAC on FIFO error */
1540 | 0x0008 /* Enable DAC High Pass Filter */
1541 )},
1542 {} /* terminator */
1543};
1544
1545/* Errata: CS4210 rev A1 Silicon
1546 *
1547 * http://www.cirrus.com/en/pubs/errata/
1548 *
1549 * Description:
1550 * 1. Performance degredation is present in the ADC.
1551 * 2. Speaker output is not completely muted upon HP detect.
1552 * 3. Noise is present when clipping occurs on the amplified
1553 * speaker outputs.
1554 *
1555 * Workaround:
1556 * The following verb sequence written to the registers during
1557 * initialization will correct the issues listed above.
1558 */
1559
1560static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1561 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1562
1563 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1564 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1565
1566 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1567 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1568
1569 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1570 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1571
1572 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1573 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1574
1575 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1576 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1577
1578 {} /* terminator */
1579};
1580
1581/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1582static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1583
1584static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1585 struct snd_ctl_elem_info *uinfo)
1586{
1587 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1588 uinfo->count = 1;
1589 uinfo->value.integer.min = 0;
1590 uinfo->value.integer.max = 3;
1591 return 0;
1592}
1593
1594static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1595 struct snd_ctl_elem_value *ucontrol)
1596{
1597 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1598
1599 ucontrol->value.integer.value[0] =
1600 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1601 return 0;
1602}
1603
1604static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1605 struct snd_ctl_elem_value *ucontrol)
1606{
1607 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1608
1609 unsigned int vol = ucontrol->value.integer.value[0];
1610 unsigned int coef =
1611 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1612 unsigned int original_coef = coef;
1613
1614 coef &= ~0x0003;
1615 coef |= (vol & 0x0003);
1616 if (original_coef == coef)
1617 return 0;
1618 else {
1619 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1620 return 1;
1621 }
1622}
1623
1624static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1625
1626 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1627 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1628 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1629 .name = "Speaker Boost Playback Volume",
1630 .info = cs421x_boost_vol_info,
1631 .get = cs421x_boost_vol_get,
1632 .put = cs421x_boost_vol_put,
1633 .tlv = { .p = cs421x_speaker_boost_db_scale },
1634};
1635
David Henningsson5660ffd2012-01-02 12:40:17 +01001636static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001637{
1638 struct cs_spec *spec = codec->spec;
1639 unsigned int def_conf, coef;
1640
1641 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1642 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1643
1644 if (spec->gpio_mask)
1645 coef |= 0x0008; /* B1,B2 are GPIOs */
1646 else
1647 coef &= ~0x0008;
1648
1649 if (spec->sense_b)
1650 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1651 else
1652 coef &= ~0x0010;
1653
1654 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1655
1656 if ((spec->gpio_mask || spec->sense_b) &&
1657 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1658
1659 /*
1660 GPIO or SENSE_B forced - disconnect the DMIC pin.
1661 */
1662 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1663 def_conf &= ~AC_DEFCFG_PORT_CONN;
1664 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1665 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1666 }
1667}
1668
1669static void init_cs421x_digital(struct hda_codec *codec)
1670{
1671 struct cs_spec *spec = codec->spec;
1672 struct auto_pin_cfg *cfg = &spec->autocfg;
1673 int i;
1674
1675
1676 for (i = 0; i < cfg->dig_outs; i++) {
1677 hda_nid_t nid = cfg->dig_out_pins[i];
1678 if (!cfg->speaker_outs)
1679 continue;
1680 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
David Henningsson5c2e4e02012-10-08 15:44:15 +02001681 snd_hda_jack_detect_enable_callback(codec, nid, SPDIF_EVENT, cs_automute);
Tim Howe56487c22011-07-22 16:41:00 -05001682 spec->spdif_detect = 1;
1683 }
1684 }
1685}
1686
1687static int cs421x_init(struct hda_codec *codec)
1688{
1689 struct cs_spec *spec = codec->spec;
1690
David Henningsson5660ffd2012-01-02 12:40:17 +01001691 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1692 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1693 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1694 cs4210_pinmux_init(codec);
1695 }
Tim Howe56487c22011-07-22 16:41:00 -05001696
1697 if (spec->gpio_mask) {
1698 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1699 spec->gpio_mask);
1700 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1701 spec->gpio_dir);
1702 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1703 spec->gpio_data);
1704 }
1705
1706 init_output(codec);
1707 init_input(codec);
1708 init_cs421x_digital(codec);
1709
1710 return 0;
1711}
1712
1713/*
1714 * CS4210 Input MUX (1 ADC)
1715 */
1716static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1717 struct snd_ctl_elem_info *uinfo)
1718{
1719 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1720 struct cs_spec *spec = codec->spec;
1721
1722 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1723}
1724
1725static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1726 struct snd_ctl_elem_value *ucontrol)
1727{
1728 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1729 struct cs_spec *spec = codec->spec;
1730
1731 ucontrol->value.enumerated.item[0] = spec->cur_input;
1732 return 0;
1733}
1734
1735static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1736 struct snd_ctl_elem_value *ucontrol)
1737{
1738 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1739 struct cs_spec *spec = codec->spec;
1740
1741 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1742 spec->adc_nid[0], &spec->cur_input);
1743
1744}
1745
Daniel J Blueman00e17f72012-11-04 13:19:04 +08001746static const struct snd_kcontrol_new cs421x_capture_source = {
Tim Howe56487c22011-07-22 16:41:00 -05001747 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1748 .name = "Capture Source",
1749 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1750 .info = cs421x_mux_enum_info,
1751 .get = cs421x_mux_enum_get,
1752 .put = cs421x_mux_enum_put,
1753};
1754
1755static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1756{
1757 struct cs_spec *spec = codec->spec;
1758 struct auto_pin_cfg *cfg = &spec->autocfg;
1759 const struct hda_input_mux *imux = &spec->input_mux;
1760 hda_nid_t pin = cfg->inputs[item].pin;
1761 struct snd_kcontrol *kctl;
1762 u32 caps;
1763
1764 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1765 return 0;
1766
1767 caps = query_amp_caps(codec, pin, HDA_INPUT);
1768 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1769 if (caps <= 1)
1770 return 0;
1771
1772 return add_volume(codec, imux->items[item].label, 0,
1773 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1774}
1775
1776/* add a (input-boost) volume control to the given input pin */
1777static int build_cs421x_input(struct hda_codec *codec)
1778{
1779 struct cs_spec *spec = codec->spec;
1780 struct auto_pin_cfg *cfg = &spec->autocfg;
1781 struct hda_input_mux *imux = &spec->input_mux;
1782 int i, err, type_idx;
1783 const char *label;
1784
1785 if (!spec->num_inputs)
1786 return 0;
1787
1788 /* make bind-capture */
1789 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1790 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1791 for (i = 0; i < 2; i++) {
1792 struct snd_kcontrol *kctl;
1793 int n;
1794 if (!spec->capture_bind[i])
1795 return -ENOMEM;
1796 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1797 if (!kctl)
1798 return -ENOMEM;
1799 kctl->private_value = (long)spec->capture_bind[i];
1800 err = snd_hda_ctl_add(codec, 0, kctl);
1801 if (err < 0)
1802 return err;
1803 for (n = 0; n < AUTO_PIN_LAST; n++) {
1804 if (!spec->adc_nid[n])
1805 continue;
1806 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1807 if (err < 0)
1808 return err;
1809 }
1810 }
1811
1812 /* Add Input MUX Items + Capture Volume/Switch */
1813 for (i = 0; i < spec->num_inputs; i++) {
1814 label = hda_get_autocfg_input_label(codec, cfg, i);
1815 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1816
1817 err = cs421x_add_input_volume_control(codec, i);
1818 if (err < 0)
1819 return err;
1820 }
1821
1822 /*
1823 Add 'Capture Source' Switch if
1824 * 2 inputs and no mic detec
1825 * 3 inputs
1826 */
1827 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1828 (spec->num_inputs == 3)) {
1829
1830 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1831 snd_ctl_new1(&cs421x_capture_source, codec));
1832 if (err < 0)
1833 return err;
1834 }
1835
1836 return 0;
1837}
1838
1839/* Single DAC (Mute/Gain) */
1840static int build_cs421x_output(struct hda_codec *codec)
1841{
1842 hda_nid_t dac = CS4210_DAC_NID;
1843 struct cs_spec *spec = codec->spec;
1844 struct auto_pin_cfg *cfg = &spec->autocfg;
1845 struct snd_kcontrol *kctl;
1846 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001847 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001848
1849 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001850
1851 err = add_mute(codec, name, 0,
1852 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1853 if (err < 0)
1854 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001855
1856 err = add_volume(codec, name, 0,
1857 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1858 if (err < 0)
1859 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001860
David Henningsson5660ffd2012-01-02 12:40:17 +01001861 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001862 err = snd_hda_ctl_add(codec, 0,
1863 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1864 if (err < 0)
1865 return err;
1866 }
1867 return err;
1868}
1869
1870static int cs421x_build_controls(struct hda_codec *codec)
1871{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001872 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001873 int err;
1874
1875 err = build_cs421x_output(codec);
1876 if (err < 0)
1877 return err;
1878 err = build_cs421x_input(codec);
1879 if (err < 0)
1880 return err;
1881 err = build_digital_output(codec);
1882 if (err < 0)
1883 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001884 err = cs421x_init(codec);
1885 if (err < 0)
1886 return err;
1887
1888 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1889 if (err < 0)
1890 return err;
1891
1892 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001893}
1894
Tim Howe56487c22011-07-22 16:41:00 -05001895static int parse_cs421x_input(struct hda_codec *codec)
1896{
1897 struct cs_spec *spec = codec->spec;
1898 struct auto_pin_cfg *cfg = &spec->autocfg;
1899 int i;
1900
1901 for (i = 0; i < cfg->num_inputs; i++) {
1902 hda_nid_t pin = cfg->inputs[i].pin;
1903 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1904 spec->cur_input = spec->last_input = i;
1905 spec->num_inputs++;
1906
1907 /* check whether the automatic mic switch is available */
1908 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1909 spec->mic_detect = 1;
1910 spec->automic_idx = i;
1911 }
1912 }
1913 return 0;
1914}
1915
1916static int cs421x_parse_auto_config(struct hda_codec *codec)
1917{
1918 struct cs_spec *spec = codec->spec;
1919 int err;
1920
1921 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1922 if (err < 0)
1923 return err;
1924 err = parse_output(codec);
1925 if (err < 0)
1926 return err;
1927 err = parse_cs421x_input(codec);
1928 if (err < 0)
1929 return err;
1930 err = parse_digital_output(codec);
1931 if (err < 0)
1932 return err;
1933 return 0;
1934}
1935
1936#ifdef CONFIG_PM
1937/*
1938 Manage PDREF, when transitioning to D3hot
1939 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1940*/
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001941static int cs421x_suspend(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001942{
David Henningsson5660ffd2012-01-02 12:40:17 +01001943 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001944 unsigned int coef;
1945
1946 snd_hda_shutup_pins(codec);
1947
1948 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1949 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1950 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1951 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1952
David Henningsson5660ffd2012-01-02 12:40:17 +01001953 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1954 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1955 coef |= 0x0004; /* PDREF */
1956 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1957 }
Tim Howe56487c22011-07-22 16:41:00 -05001958
1959 return 0;
1960}
1961#endif
1962
Daniel J Blueman00e17f72012-11-04 13:19:04 +08001963static const struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001964 .build_controls = cs421x_build_controls,
1965 .build_pcms = cs_build_pcms,
1966 .init = cs421x_init,
1967 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +02001968 .unsol_event = snd_hda_jack_unsol_event,
Tim Howe56487c22011-07-22 16:41:00 -05001969#ifdef CONFIG_PM
1970 .suspend = cs421x_suspend,
1971#endif
1972};
1973
David Henningsson5660ffd2012-01-02 12:40:17 +01001974static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001975{
1976 struct cs_spec *spec;
1977 int err;
1978
1979 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1980 if (!spec)
1981 return -ENOMEM;
1982 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001983 snd_hda_gen_init(&spec->gen);
Tim Howe56487c22011-07-22 16:41:00 -05001984
David Henningsson5660ffd2012-01-02 12:40:17 +01001985 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001986
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001987 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1988 cs421x_fixups);
1989 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Tim Howe56487c22011-07-22 16:41:00 -05001990
1991 /*
1992 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1993 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1994 is disabled.
1995 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001996 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001997
1998 err = cs421x_parse_auto_config(codec);
1999 if (err < 0)
2000 goto error;
2001
David Henningsson5660ffd2012-01-02 12:40:17 +01002002 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05002003
Takashi Iwaib35aabd2012-08-24 19:09:45 +02002004 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
2005
Tim Howe56487c22011-07-22 16:41:00 -05002006 return 0;
2007
2008 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02002009 cs_free(codec);
Tim Howe56487c22011-07-22 16:41:00 -05002010 codec->spec = NULL;
2011 return err;
2012}
2013
David Henningsson5660ffd2012-01-02 12:40:17 +01002014static int patch_cs4213(struct hda_codec *codec)
2015{
2016 struct cs_spec *spec;
2017 int err;
2018
2019 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2020 if (!spec)
2021 return -ENOMEM;
2022 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02002023 snd_hda_gen_init(&spec->gen);
David Henningsson5660ffd2012-01-02 12:40:17 +01002024
2025 spec->vendor_nid = CS4213_VENDOR_NID;
2026
2027 err = cs421x_parse_auto_config(codec);
2028 if (err < 0)
2029 goto error;
2030
2031 codec->patch_ops = cs421x_patch_ops;
2032 return 0;
2033
2034 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02002035 cs_free(codec);
David Henningsson5660ffd2012-01-02 12:40:17 +01002036 codec->spec = NULL;
2037 return err;
2038}
2039
Takashi Iwaie5f14242009-07-01 18:11:44 +02002040
2041/*
2042 * patch entries
2043 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002044static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002045 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2046 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002047 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2048 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002049 {} /* terminator */
2050};
2051
2052MODULE_ALIAS("snd-hda-codec-id:10134206");
2053MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002054MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002055MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002056
2057MODULE_LICENSE("GPL");
2058MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2059
2060static struct hda_codec_preset_list cirrus_list = {
2061 .preset = snd_hda_preset_cirrus,
2062 .owner = THIS_MODULE,
2063};
2064
2065static int __init patch_cirrus_init(void)
2066{
2067 return snd_hda_add_codec_preset(&cirrus_list);
2068}
2069
2070static void __exit patch_cirrus_exit(void)
2071{
2072 snd_hda_delete_codec_preset(&cirrus_list);
2073}
2074
2075module_init(patch_cirrus_init)
2076module_exit(patch_cirrus_exit)