blob: f99cbf942b8d6893b67932021ed2e3637916facc [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;
476 }
477
478 return 0;
479}
480
481static int parse_input(struct hda_codec *codec)
482{
483 struct cs_spec *spec = codec->spec;
484 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200485 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200486
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200487 for (i = 0; i < cfg->num_inputs; i++) {
488 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200489 spec->input_idx[spec->num_inputs] = i;
490 spec->capsrc_idx[i] = spec->num_inputs++;
491 spec->cur_input = i;
492 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
493 }
494 if (!spec->num_inputs)
495 return 0;
496
497 /* check whether the automatic mic switch is available */
498 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200499 cfg->inputs[0].type == AUTO_PIN_MIC &&
500 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200501 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
502 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200503 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200504 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200505 }
506 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200507 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200508 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200509 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200510 }
511 }
512 }
513 return 0;
514}
515
516
517static int parse_digital_output(struct hda_codec *codec)
518{
519 struct cs_spec *spec = codec->spec;
520 struct auto_pin_cfg *cfg = &spec->autocfg;
521 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200522
523 if (!cfg->dig_outs)
524 return 0;
525 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
526 return 0;
527 spec->multiout.dig_out_nid = nid;
528 spec->multiout.share_spdif = 1;
529 if (cfg->dig_outs > 1 &&
530 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
531 spec->slave_dig_outs[0] = nid;
532 codec->slave_dig_outs = spec->slave_dig_outs;
533 }
534 return 0;
535}
536
537static int parse_digital_input(struct hda_codec *codec)
538{
539 struct cs_spec *spec = codec->spec;
540 struct auto_pin_cfg *cfg = &spec->autocfg;
541 int idx;
542
Takashi Iwai60e53882009-07-06 15:01:09 +0200543 if (cfg->dig_in_pin)
544 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
545 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200546}
547
548/*
549 * create mixer controls
550 */
551
Takashi Iwaiea734962011-01-17 11:29:34 +0100552static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200553
554static int add_mute(struct hda_codec *codec, const char *name, int index,
555 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
556{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200557 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200558 struct snd_kcontrol_new knew =
559 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
560 knew.private_value = pval;
561 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
562 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100563 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100564 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200565}
566
567static int add_volume(struct hda_codec *codec, const char *name,
568 int index, unsigned int pval, int dir,
569 struct snd_kcontrol **kctlp)
570{
David Henningsson2e1210b2011-09-14 13:22:54 +0200571 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200572 struct snd_kcontrol_new knew =
573 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
574 knew.private_value = pval;
575 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
576 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100577 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100578 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200579}
580
581static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
582{
583 unsigned int caps;
584
585 /* set the upper-limit for mixer amp to 0dB */
586 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
587 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
588 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
589 << AC_AMPCAP_NUM_STEPS_SHIFT;
590 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
591}
592
593static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
594{
595 struct cs_spec *spec = codec->spec;
596 unsigned int tlv[4];
597 int err;
598
599 spec->vmaster_sw =
600 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100601 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200602 if (err < 0)
603 return err;
604
605 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
606 spec->vmaster_vol =
607 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100608 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200609 if (err < 0)
610 return err;
611 return 0;
612}
613
614static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
615 int num_ctls, int type)
616{
617 struct cs_spec *spec = codec->spec;
618 const char *name;
619 int err, index;
620 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200621 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200622 "Front Speaker", "Surround Speaker", "Bass Speaker"
623 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200624 static const char * const line_outs[] = {
Takashi Iwaie49a3432012-03-01 18:14:41 +0100625 "Front Line Out", "Surround Line Out", "Bass Line Out"
Takashi Iwaie5f14242009-07-01 18:11:44 +0200626 };
627
628 fix_volume_caps(codec, dac);
629 if (!spec->vmaster_sw) {
630 err = add_vmaster(codec, dac);
631 if (err < 0)
632 return err;
633 }
634
635 index = 0;
636 switch (type) {
637 case AUTO_PIN_HP_OUT:
638 name = "Headphone";
639 index = idx;
640 break;
641 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwaif37bc7a2012-11-08 15:59:23 +0100642 if (spec->speaker_2_1)
643 name = idx ? "Bass Speaker" : "Speaker";
644 else if (num_ctls > 1)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200645 name = speakers[idx];
646 else
647 name = "Speaker";
648 break;
649 default:
650 if (num_ctls > 1)
651 name = line_outs[idx];
652 else
Takashi Iwaie49a3432012-03-01 18:14:41 +0100653 name = "Line Out";
Takashi Iwaie5f14242009-07-01 18:11:44 +0200654 break;
655 }
656
657 err = add_mute(codec, name, index,
658 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
659 if (err < 0)
660 return err;
661 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
662 if (err < 0)
663 return err;
664
665 err = add_volume(codec, name, index,
666 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
667 if (err < 0)
668 return err;
669 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
670 if (err < 0)
671 return err;
672
673 return 0;
674}
675
676static int build_output(struct hda_codec *codec)
677{
678 struct cs_spec *spec = codec->spec;
679 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200680 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200681
682 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200683 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
684 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200685 if (err < 0)
686 return err;
687 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200688 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200689 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
690 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200691 if (err < 0)
692 return err;
693 }
694 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200695 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
696 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200697 if (err < 0)
698 return err;
699 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200700 return 0;
701}
702
703/*
704 */
705
Takashi Iwaic42d4782011-05-02 11:36:09 +0200706static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200707 HDA_BIND_SW("Capture Switch", 0),
708 HDA_BIND_VOL("Capture Volume", 0),
709};
710
Takashi Iwaiea359292009-07-06 12:58:59 +0200711static int change_cur_input(struct hda_codec *codec, unsigned int idx,
712 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200713{
714 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200715
Takashi Iwaiea359292009-07-06 12:58:59 +0200716 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200717 return 0;
718 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
719 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200720 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200721 spec->cur_adc = spec->adc_nid[idx];
722 snd_hda_codec_setup_stream(codec, spec->cur_adc,
723 spec->cur_adc_stream_tag, 0,
724 spec->cur_adc_format);
725 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200726 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100727 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200728 return 1;
729}
730
731static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
732 struct snd_ctl_elem_info *uinfo)
733{
734 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
735 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200736 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200737 unsigned int idx;
738
739 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
740 uinfo->count = 1;
741 uinfo->value.enumerated.items = spec->num_inputs;
742 if (uinfo->value.enumerated.item >= spec->num_inputs)
743 uinfo->value.enumerated.item = spec->num_inputs - 1;
744 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100745 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
746 uinfo->value.enumerated.name,
747 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200748 return 0;
749}
750
751static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
752 struct snd_ctl_elem_value *ucontrol)
753{
754 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
755 struct cs_spec *spec = codec->spec;
756 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
757 return 0;
758}
759
760static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
761 struct snd_ctl_elem_value *ucontrol)
762{
763 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
764 struct cs_spec *spec = codec->spec;
765 unsigned int idx = ucontrol->value.enumerated.item[0];
766
767 if (idx >= spec->num_inputs)
768 return -EINVAL;
769 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200770 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200771}
772
Takashi Iwaic42d4782011-05-02 11:36:09 +0200773static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200774 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
775 .name = "Capture Source",
776 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
777 .info = cs_capture_source_info,
778 .get = cs_capture_source_get,
779 .put = cs_capture_source_put,
780};
781
Takashi Iwaic42d4782011-05-02 11:36:09 +0200782static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200783 struct hda_ctl_ops *ops)
784{
785 struct cs_spec *spec = codec->spec;
786 struct hda_bind_ctls *bind;
787 int i, n;
788
789 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
790 GFP_KERNEL);
791 if (!bind)
792 return NULL;
793 bind->ops = ops;
794 n = 0;
795 for (i = 0; i < AUTO_PIN_LAST; i++) {
796 if (!spec->adc_nid[i])
797 continue;
798 bind->values[n++] =
799 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
800 spec->adc_idx[i], HDA_INPUT);
801 }
802 return bind;
803}
804
Takashi Iwai6a929342010-10-11 15:16:20 +0200805/* add a (input-boost) volume control to the given input pin */
806static int add_input_volume_control(struct hda_codec *codec,
807 struct auto_pin_cfg *cfg,
808 int item)
809{
810 hda_nid_t pin = cfg->inputs[item].pin;
811 u32 caps;
812 const char *label;
813 struct snd_kcontrol *kctl;
814
815 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
816 return 0;
817 caps = query_amp_caps(codec, pin, HDA_INPUT);
818 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
819 if (caps <= 1)
820 return 0;
821 label = hda_get_autocfg_input_label(codec, cfg, item);
822 return add_volume(codec, label, 0,
823 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
824}
825
Takashi Iwaie5f14242009-07-01 18:11:44 +0200826static int build_input(struct hda_codec *codec)
827{
828 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200829 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200830
Takashi Iwaie5f14242009-07-01 18:11:44 +0200831 if (!spec->num_inputs)
832 return 0;
833
Takashi Iwaie5f14242009-07-01 18:11:44 +0200834 /* make bind-capture */
835 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
836 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
837 for (i = 0; i < 2; i++) {
838 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100839 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200840 if (!spec->capture_bind[i])
841 return -ENOMEM;
842 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
843 if (!kctl)
844 return -ENOMEM;
845 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100846 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200847 if (err < 0)
848 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100849 for (n = 0; n < AUTO_PIN_LAST; n++) {
850 if (!spec->adc_nid[n])
851 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700852 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100853 if (err < 0)
854 return err;
855 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200856 }
857
858 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100859 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200860 snd_ctl_new1(&cs_capture_source, codec));
861 if (err < 0)
862 return err;
863 }
864
Takashi Iwai6a929342010-10-11 15:16:20 +0200865 for (i = 0; i < spec->num_inputs; i++) {
866 err = add_input_volume_control(codec, &spec->autocfg, i);
867 if (err < 0)
868 return err;
869 }
870
Takashi Iwaie5f14242009-07-01 18:11:44 +0200871 return 0;
872}
873
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200874/*
875 */
876
Takashi Iwaie5f14242009-07-01 18:11:44 +0200877static int build_digital_output(struct hda_codec *codec)
878{
879 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200880 int err;
881
Takashi Iwai63b24132009-07-09 11:45:59 +0200882 if (!spec->multiout.dig_out_nid)
883 return 0;
884
Takashi Iwaidcda5802012-10-12 17:24:51 +0200885 err = snd_hda_create_dig_out_ctls(codec, spec->multiout.dig_out_nid,
886 spec->multiout.dig_out_nid,
887 spec->pcm_rec[1].pcm_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200888 if (err < 0)
889 return err;
890 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
891 if (err < 0)
892 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200893 return 0;
894}
895
896static int build_digital_input(struct hda_codec *codec)
897{
898 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200899 if (spec->dig_in)
900 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
901 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200902}
903
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200904/*
905 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500906 * CS421x auto-output redirecting
907 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200908 */
909
David Henningsson5c2e4e02012-10-08 15:44:15 +0200910static void cs_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200911{
912 struct cs_spec *spec = codec->spec;
913 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200914 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500915 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200916 hda_nid_t nid;
917 int i;
918
Tim Howe56487c22011-07-22 16:41:00 -0500919 spdif_present = 0;
920 if (cfg->dig_outs) {
921 nid = cfg->dig_out_pins[0];
922 if (is_jack_detectable(codec, nid)) {
923 /*
924 TODO: SPDIF output redirect when SENSE_B is enabled.
925 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
926 assumed.
927 */
928 if (snd_hda_jack_detect(codec, nid)
929 /* && spec->sense_b */)
930 spdif_present = 1;
931 }
932 }
933
Takashi Iwaie5f14242009-07-01 18:11:44 +0200934 hp_present = 0;
935 for (i = 0; i < cfg->hp_outs; i++) {
936 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200937 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200938 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800939 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200940 if (hp_present)
941 break;
942 }
Tim Howe56487c22011-07-22 16:41:00 -0500943
944 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200945 for (i = 0; i < cfg->speaker_outs; i++) {
David Henningsson78e2a922012-01-02 12:40:16 +0100946 int pin_ctl = hp_present ? 0 : PIN_OUT;
David Henningsson5660ffd2012-01-02 12:40:17 +0100947 /* detect on spdif is specific to CS4210 */
948 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
David Henningsson78e2a922012-01-02 12:40:16 +0100949 pin_ctl = 0;
950
Takashi Iwaie5f14242009-07-01 18:11:44 +0200951 nid = cfg->speaker_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200952 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200953 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100954 if (spec->gpio_eapd_hp) {
955 unsigned int gpio = hp_present ?
956 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200957 snd_hda_codec_write(codec, 0x01, 0,
958 AC_VERB_SET_GPIO_DATA, gpio);
959 }
Tim Howe56487c22011-07-22 16:41:00 -0500960
David Henningsson5660ffd2012-01-02 12:40:17 +0100961 /* specific to CS4210 */
962 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500963 /* mute HPs if spdif jack (SENSE_B) is present */
964 for (i = 0; i < cfg->hp_outs; i++) {
965 nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200966 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500967 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
968 }
969
970 /* SPDIF TX on/off */
971 if (cfg->dig_outs) {
972 nid = cfg->dig_out_pins[0];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200973 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500974 spdif_present ? PIN_OUT : 0);
975
976 }
977 /* Update board GPIOs if neccessary ... */
978 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200979}
980
Tim Howe56487c22011-07-22 16:41:00 -0500981/*
982 * Auto-input redirect for CS421x
983 * Switch max 3 inputs of a single ADC (nid 3)
984*/
985
David Henningsson5c2e4e02012-10-08 15:44:15 +0200986static void cs_automic(struct hda_codec *codec, struct hda_jack_tbl *tbl)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200987{
988 struct cs_spec *spec = codec->spec;
989 struct auto_pin_cfg *cfg = &spec->autocfg;
990 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800991 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500992
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200993 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800994 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500995
996 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100997 if (spec->vendor_nid == CS420X_VENDOR_NID) {
998 if (present)
999 change_cur_input(codec, spec->automic_idx, 0);
1000 else
1001 change_cur_input(codec, !spec->automic_idx, 0);
1002 } else {
Tim Howe56487c22011-07-22 16:41:00 -05001003 if (present) {
Dylan Reidf70eecd2012-01-31 13:04:41 -08001004 if (spec->cur_input != spec->automic_idx) {
1005 spec->last_input = spec->cur_input;
1006 spec->cur_input = spec->automic_idx;
1007 }
Tim Howe56487c22011-07-22 16:41:00 -05001008 } else {
1009 spec->cur_input = spec->last_input;
1010 }
Takashi Iwai05ee7962011-11-16 18:05:11 +01001011 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001012 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001013}
1014
1015/*
1016 */
1017
1018static void init_output(struct hda_codec *codec)
1019{
1020 struct cs_spec *spec = codec->spec;
1021 struct auto_pin_cfg *cfg = &spec->autocfg;
1022 int i;
1023
1024 /* mute first */
1025 for (i = 0; i < spec->multiout.num_dacs; i++)
1026 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1027 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1028 if (spec->multiout.hp_nid)
1029 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1030 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1031 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1032 if (!spec->multiout.extra_out_nid[i])
1033 break;
1034 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1035 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1036 }
1037
1038 /* set appropriate pin controls */
1039 for (i = 0; i < cfg->line_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001040 snd_hda_set_pin_ctl(codec, cfg->line_out_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001041 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001042 for (i = 0; i < cfg->hp_outs; i++) {
1043 hda_nid_t nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001044 snd_hda_set_pin_ctl(codec, nid, PIN_HP);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001045 if (!cfg->speaker_outs)
1046 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001047 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
David Henningsson5c2e4e02012-10-08 15:44:15 +02001048 snd_hda_jack_detect_enable_callback(codec, nid, HP_EVENT, cs_automute);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001049 spec->hp_detect = 1;
1050 }
1051 }
Tim Howe56487c22011-07-22 16:41:00 -05001052
1053 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001054 for (i = 0; i < cfg->speaker_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001055 snd_hda_set_pin_ctl(codec, cfg->speaker_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001056
1057 /* SPDIF is enabled on presence detect for CS421x */
1058 if (spec->hp_detect || spec->spdif_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001059 cs_automute(codec, NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001060}
1061
1062static void init_input(struct hda_codec *codec)
1063{
1064 struct cs_spec *spec = codec->spec;
1065 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001066 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001067 int i;
1068
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001069 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001070 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001071 hda_nid_t pin = cfg->inputs[i].pin;
1072 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001073 continue;
1074 /* set appropriate pin control and mute first */
1075 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +02001076 if (cfg->inputs[i].type == AUTO_PIN_MIC)
1077 ctl |= snd_hda_get_default_vref(codec, pin);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001078 snd_hda_set_pin_ctl(codec, pin, ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001079 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1080 AC_VERB_SET_AMP_GAIN_MUTE,
1081 AMP_IN_MUTE(spec->adc_idx[i]));
1082 if (spec->mic_detect && spec->automic_idx == i)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001083 snd_hda_jack_detect_enable_callback(codec, pin, MIC_EVENT, cs_automic);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001084 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001085 /* CS420x has multiple ADC, CS421x has single ADC */
1086 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001087 change_cur_input(codec, spec->cur_input, 1);
1088 if (spec->mic_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001089 cs_automic(codec, NULL);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001090
Tim Howe56487c22011-07-22 16:41:00 -05001091 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
Daniel J Blueman16337e02012-11-04 13:19:03 +08001092 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1093
1094 coef = cs_vendor_coef_get(codec, IDX_BEEP_CFG);
Tim Howe56487c22011-07-22 16:41:00 -05001095 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
Daniel J Blueman16337e02012-11-04 13:19:03 +08001096 coef |= 1 << 4; /* DMIC2 2 chan on, GPIO1 off */
Tim Howe56487c22011-07-22 16:41:00 -05001097 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
Daniel J Blueman16337e02012-11-04 13:19:03 +08001098 coef |= 1 << 3; /* DMIC1 2 chan on, GPIO0 off
Tim Howe56487c22011-07-22 16:41:00 -05001099 * No effect if SPDIF_OUT2 is
1100 * selected in IDX_SPDIF_CTL.
1101 */
Daniel J Blueman16337e02012-11-04 13:19:03 +08001102
1103 cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001104 } else {
1105 if (spec->mic_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001106 cs_automic(codec, NULL);
David Henningsson5660ffd2012-01-02 12:40:17 +01001107 else {
1108 spec->cur_adc = spec->adc_nid[spec->cur_input];
1109 cs_update_input_select(codec);
1110 }
Tim Howe56487c22011-07-22 16:41:00 -05001111 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001112}
1113
Takashi Iwaic42d4782011-05-02 11:36:09 +02001114static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001115 {0x11, AC_VERB_SET_PROC_STATE, 1},
1116 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1117 {0x11, AC_VERB_SET_PROC_COEF,
1118 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1119 | 0x0040 /* Mute DACs on FIFO error */
1120 | 0x1000 /* Enable DACs High Pass Filter */
1121 | 0x0400 /* Disable Coefficient Auto increment */
1122 )},
1123 /* Beep */
Alexander Stein5a83b4b2012-11-01 13:42:37 +01001124 {0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001125 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1126
1127 {} /* terminator */
1128};
1129
Brian Austina769cbc2010-09-07 14:36:22 -05001130/* Errata: CS4207 rev C0/C1/C2 Silicon
1131 *
1132 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1133 *
1134 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1135 * may be excessive (up to an additional 200 μA), which is most easily
1136 * observed while the part is being held in reset (RESET# active low).
1137 *
1138 * Root Cause: At initial powerup of the device, the logic that drives
1139 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1140 * initialized.
1141 * Certain random patterns will cause a steady leakage current in those
1142 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1143 *
1144 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1145 * blocks, which will alleviate the issue.
1146 */
1147
Takashi Iwaic42d4782011-05-02 11:36:09 +02001148static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001149 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1150 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1151
1152 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1153 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1154 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1155 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1156 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1157 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1158
1159 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1160 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1161
1162 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1163 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1164 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1165 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1166 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1167 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1168 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1169
Takashi Iwai38c07642011-03-03 14:54:19 +01001170#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001171 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1172 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1173 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001174#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001175
1176 {} /* terminator */
1177};
1178
Takashi Iwaief596a52012-09-11 16:53:08 +02001179static const struct hda_verb mbp101_init_verbs[] = {
1180 {0x11, AC_VERB_SET_COEF_INDEX, 0x0002},
1181 {0x11, AC_VERB_SET_PROC_COEF, 0x100a},
1182 {0x11, AC_VERB_SET_COEF_INDEX, 0x0004},
1183 {0x11, AC_VERB_SET_PROC_COEF, 0x000f},
1184 {}
1185};
1186
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001187/* SPDIF setup */
1188static void init_digital(struct hda_codec *codec)
1189{
1190 unsigned int coef;
1191
1192 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1193 coef |= 0x0008; /* Replace with mute on error */
1194 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1195 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1196 * SPDIF_OUT2 is shared with GPIO1 and
1197 * DMIC_SDA2.
1198 */
1199 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001200}
1201
1202static int cs_init(struct hda_codec *codec)
1203{
1204 struct cs_spec *spec = codec->spec;
1205
Brian Austina769cbc2010-09-07 14:36:22 -05001206 /* init_verb sequence for C0/C1/C2 errata*/
1207 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1208
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001209 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001210
1211 if (spec->gpio_mask) {
1212 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1213 spec->gpio_mask);
1214 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1215 spec->gpio_dir);
1216 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1217 spec->gpio_data);
1218 }
1219
Takashi Iwaie5f14242009-07-01 18:11:44 +02001220 init_output(codec);
1221 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001222 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001223
Takashi Iwaie5f14242009-07-01 18:11:44 +02001224 return 0;
1225}
1226
1227static int cs_build_controls(struct hda_codec *codec)
1228{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001229 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001230 int err;
1231
1232 err = build_output(codec);
1233 if (err < 0)
1234 return err;
1235 err = build_input(codec);
1236 if (err < 0)
1237 return err;
1238 err = build_digital_output(codec);
1239 if (err < 0)
1240 return err;
1241 err = build_digital_input(codec);
1242 if (err < 0)
1243 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001244 err = cs_init(codec);
1245 if (err < 0)
1246 return err;
1247
1248 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1249 if (err < 0)
1250 return err;
1251
1252 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001253}
1254
1255static void cs_free(struct hda_codec *codec)
1256{
1257 struct cs_spec *spec = codec->spec;
1258 kfree(spec->capture_bind[0]);
1259 kfree(spec->capture_bind[1]);
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001260 snd_hda_gen_free(&spec->gen);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001261 kfree(codec->spec);
1262}
1263
Takashi Iwaic42d4782011-05-02 11:36:09 +02001264static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001265 .build_controls = cs_build_controls,
1266 .build_pcms = cs_build_pcms,
1267 .init = cs_init,
1268 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +02001269 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwaie5f14242009-07-01 18:11:44 +02001270};
1271
1272static int cs_parse_auto_config(struct hda_codec *codec)
1273{
1274 struct cs_spec *spec = codec->spec;
1275 int err;
1276
1277 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1278 if (err < 0)
1279 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001280
1281 err = parse_output(codec);
1282 if (err < 0)
1283 return err;
1284 err = parse_input(codec);
1285 if (err < 0)
1286 return err;
1287 err = parse_digital_output(codec);
1288 if (err < 0)
1289 return err;
1290 err = parse_digital_input(codec);
1291 if (err < 0)
1292 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001293 return 0;
1294}
1295
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001296static const struct hda_model_fixup cs420x_models[] = {
1297 { .id = CS420X_MBP53, .name = "mbp53" },
1298 { .id = CS420X_MBP55, .name = "mbp55" },
1299 { .id = CS420X_IMAC27, .name = "imac27" },
1300 { .id = CS420X_IMAC27_122, .name = "imac27_122" },
1301 { .id = CS420X_APPLE, .name = "apple" },
Takashi Iwaief596a52012-09-11 16:53:08 +02001302 { .id = CS420X_MBP101, .name = "mbp101" },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001303 {}
Takashi Iwaia6bae202009-07-06 15:15:22 +02001304};
1305
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001306static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001307 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001308 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001309 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001310 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001311 /* this conflicts with too many other models */
1312 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001313
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001314 /* codec SSID */
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001315 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwaief596a52012-09-11 16:53:08 +02001316 SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001317 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001318 {} /* terminator */
1319};
1320
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001321static const struct hda_pintbl mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001322 { 0x09, 0x012b4050 },
1323 { 0x0a, 0x90100141 },
1324 { 0x0b, 0x90100140 },
1325 { 0x0c, 0x018b3020 },
1326 { 0x0d, 0x90a00110 },
1327 { 0x0e, 0x400000f0 },
1328 { 0x0f, 0x01cbe030 },
1329 { 0x10, 0x014be060 },
1330 { 0x12, 0x400000f0 },
1331 { 0x15, 0x400000f0 },
1332 {} /* terminator */
1333};
1334
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001335static const struct hda_pintbl mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001336 { 0x09, 0x012b4030 },
1337 { 0x0a, 0x90100121 },
1338 { 0x0b, 0x90100120 },
1339 { 0x0c, 0x400000f0 },
1340 { 0x0d, 0x90a00110 },
1341 { 0x0e, 0x400000f0 },
1342 { 0x0f, 0x400000f0 },
1343 { 0x10, 0x014be040 },
1344 { 0x12, 0x400000f0 },
1345 { 0x15, 0x400000f0 },
1346 {} /* terminator */
1347};
1348
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001349static const struct hda_pintbl imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001350 { 0x09, 0x012b4050 },
1351 { 0x0a, 0x90100140 },
1352 { 0x0b, 0x90100142 },
1353 { 0x0c, 0x018b3020 },
1354 { 0x0d, 0x90a00110 },
1355 { 0x0e, 0x400000f0 },
1356 { 0x0f, 0x01cbe030 },
1357 { 0x10, 0x014be060 },
1358 { 0x12, 0x01ab9070 },
1359 { 0x15, 0x400000f0 },
1360 {} /* terminator */
1361};
1362
Takashi Iwaief596a52012-09-11 16:53:08 +02001363static const struct hda_pintbl mbp101_pincfgs[] = {
1364 { 0x0d, 0x40ab90f0 },
1365 { 0x0e, 0x90a600f0 },
1366 { 0x12, 0x50a600f0 },
1367 {} /* terminator */
1368};
1369
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001370static void cs420x_fixup_gpio_13(struct hda_codec *codec,
1371 const struct hda_fixup *fix, int action)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001372{
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001373 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1374 struct cs_spec *spec = codec->spec;
1375 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1376 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1377 spec->gpio_mask = spec->gpio_dir =
1378 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1379 }
Takashi Iwaia6bae202009-07-06 15:15:22 +02001380}
1381
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001382static void cs420x_fixup_gpio_23(struct hda_codec *codec,
1383 const struct hda_fixup *fix, int action)
1384{
1385 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1386 struct cs_spec *spec = codec->spec;
1387 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1388 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1389 spec->gpio_mask = spec->gpio_dir =
1390 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1391 }
1392}
1393
1394static const struct hda_fixup cs420x_fixups[] = {
1395 [CS420X_MBP53] = {
1396 .type = HDA_FIXUP_PINS,
1397 .v.pins = mbp53_pincfgs,
1398 .chained = true,
1399 .chain_id = CS420X_APPLE,
1400 },
1401 [CS420X_MBP55] = {
1402 .type = HDA_FIXUP_PINS,
1403 .v.pins = mbp55_pincfgs,
1404 .chained = true,
1405 .chain_id = CS420X_GPIO_13,
1406 },
1407 [CS420X_IMAC27] = {
1408 .type = HDA_FIXUP_PINS,
1409 .v.pins = imac27_pincfgs,
1410 .chained = true,
1411 .chain_id = CS420X_GPIO_13,
1412 },
1413 [CS420X_GPIO_13] = {
1414 .type = HDA_FIXUP_FUNC,
1415 .v.func = cs420x_fixup_gpio_13,
1416 },
1417 [CS420X_GPIO_23] = {
1418 .type = HDA_FIXUP_FUNC,
1419 .v.func = cs420x_fixup_gpio_23,
1420 },
Takashi Iwaief596a52012-09-11 16:53:08 +02001421 [CS420X_MBP101] = {
1422 .type = HDA_FIXUP_PINS,
1423 .v.pins = mbp101_pincfgs,
1424 .chained = true,
1425 .chain_id = CS420X_MBP101_COEF,
1426 },
1427 [CS420X_MBP101_COEF] = {
1428 .type = HDA_FIXUP_VERBS,
1429 .v.verbs = mbp101_init_verbs,
1430 .chained = true,
1431 .chain_id = CS420X_GPIO_13,
1432 },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001433};
1434
Takashi Iwaie5f14242009-07-01 18:11:44 +02001435static int patch_cs420x(struct hda_codec *codec)
1436{
1437 struct cs_spec *spec;
1438 int err;
1439
1440 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1441 if (!spec)
1442 return -ENOMEM;
1443 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001444 snd_hda_gen_init(&spec->gen);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001445
Tim Howe56487c22011-07-22 16:41:00 -05001446 spec->vendor_nid = CS420X_VENDOR_NID;
1447
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001448 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
1449 cs420x_fixups);
1450 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001451
Takashi Iwaied208252009-07-07 09:04:26 +02001452 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001453 if (err < 0)
1454 goto error;
1455
Takashi Iwaie5f14242009-07-01 18:11:44 +02001456 codec->patch_ops = cs_patch_ops;
1457
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001458 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1459
Takashi Iwaie5f14242009-07-01 18:11:44 +02001460 return 0;
1461
1462 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02001463 cs_free(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001464 codec->spec = NULL;
1465 return err;
1466}
1467
Tim Howe56487c22011-07-22 16:41:00 -05001468/*
1469 * Cirrus Logic CS4210
1470 *
1471 * 1 DAC => HP(sense) / Speakers,
1472 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1473 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1474*/
1475
1476/* CS4210 board names */
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001477static const struct hda_model_fixup cs421x_models[] = {
1478 { .id = CS421X_CDB4210, .name = "cdb4210" },
1479 {}
Tim Howe56487c22011-07-22 16:41:00 -05001480};
1481
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001482static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001483 /* Test Intel board + CDB2410 */
1484 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1485 {} /* terminator */
1486};
1487
1488/* CS4210 board pinconfigs */
1489/* Default CS4210 (CDB4210)*/
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001490static const struct hda_pintbl cdb4210_pincfgs[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001491 { 0x05, 0x0321401f },
1492 { 0x06, 0x90170010 },
1493 { 0x07, 0x03813031 },
1494 { 0x08, 0xb7a70037 },
1495 { 0x09, 0xb7a6003e },
1496 { 0x0a, 0x034510f0 },
1497 {} /* terminator */
1498};
1499
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001500/* Setup GPIO/SENSE for each board (if used) */
1501static void cs421x_fixup_sense_b(struct hda_codec *codec,
1502 const struct hda_fixup *fix, int action)
1503{
1504 struct cs_spec *spec = codec->spec;
1505 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1506 spec->sense_b = 1;
1507}
1508
1509static const struct hda_fixup cs421x_fixups[] = {
1510 [CS421X_CDB4210] = {
1511 .type = HDA_FIXUP_PINS,
1512 .v.pins = cdb4210_pincfgs,
1513 .chained = true,
1514 .chain_id = CS421X_SENSE_B,
1515 },
1516 [CS421X_SENSE_B] = {
1517 .type = HDA_FIXUP_FUNC,
1518 .v.func = cs421x_fixup_sense_b,
1519 }
Tim Howe56487c22011-07-22 16:41:00 -05001520};
1521
1522static const struct hda_verb cs421x_coef_init_verbs[] = {
1523 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1524 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1525 /*
1526 Disable Coefficient Index Auto-Increment(DAI)=1,
1527 PDREF=0
1528 */
1529 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1530
1531 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1532 /* ADC SZCMode = Digital Soft Ramp */
1533 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1534
1535 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1536 {0x0B, AC_VERB_SET_PROC_COEF,
1537 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1538 | 0x0004 /* Mute DAC on FIFO error */
1539 | 0x0008 /* Enable DAC High Pass Filter */
1540 )},
1541 {} /* terminator */
1542};
1543
1544/* Errata: CS4210 rev A1 Silicon
1545 *
1546 * http://www.cirrus.com/en/pubs/errata/
1547 *
1548 * Description:
1549 * 1. Performance degredation is present in the ADC.
1550 * 2. Speaker output is not completely muted upon HP detect.
1551 * 3. Noise is present when clipping occurs on the amplified
1552 * speaker outputs.
1553 *
1554 * Workaround:
1555 * The following verb sequence written to the registers during
1556 * initialization will correct the issues listed above.
1557 */
1558
1559static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1560 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1561
1562 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1563 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1564
1565 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1566 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1567
1568 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1569 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1570
1571 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1572 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1573
1574 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1575 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1576
1577 {} /* terminator */
1578};
1579
1580/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1581static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1582
1583static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1584 struct snd_ctl_elem_info *uinfo)
1585{
1586 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1587 uinfo->count = 1;
1588 uinfo->value.integer.min = 0;
1589 uinfo->value.integer.max = 3;
1590 return 0;
1591}
1592
1593static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1594 struct snd_ctl_elem_value *ucontrol)
1595{
1596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1597
1598 ucontrol->value.integer.value[0] =
1599 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1600 return 0;
1601}
1602
1603static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1604 struct snd_ctl_elem_value *ucontrol)
1605{
1606 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1607
1608 unsigned int vol = ucontrol->value.integer.value[0];
1609 unsigned int coef =
1610 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1611 unsigned int original_coef = coef;
1612
1613 coef &= ~0x0003;
1614 coef |= (vol & 0x0003);
1615 if (original_coef == coef)
1616 return 0;
1617 else {
1618 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1619 return 1;
1620 }
1621}
1622
1623static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1624
1625 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1626 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1627 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1628 .name = "Speaker Boost Playback Volume",
1629 .info = cs421x_boost_vol_info,
1630 .get = cs421x_boost_vol_get,
1631 .put = cs421x_boost_vol_put,
1632 .tlv = { .p = cs421x_speaker_boost_db_scale },
1633};
1634
David Henningsson5660ffd2012-01-02 12:40:17 +01001635static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001636{
1637 struct cs_spec *spec = codec->spec;
1638 unsigned int def_conf, coef;
1639
1640 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1641 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1642
1643 if (spec->gpio_mask)
1644 coef |= 0x0008; /* B1,B2 are GPIOs */
1645 else
1646 coef &= ~0x0008;
1647
1648 if (spec->sense_b)
1649 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1650 else
1651 coef &= ~0x0010;
1652
1653 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1654
1655 if ((spec->gpio_mask || spec->sense_b) &&
1656 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1657
1658 /*
1659 GPIO or SENSE_B forced - disconnect the DMIC pin.
1660 */
1661 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1662 def_conf &= ~AC_DEFCFG_PORT_CONN;
1663 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1664 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1665 }
1666}
1667
1668static void init_cs421x_digital(struct hda_codec *codec)
1669{
1670 struct cs_spec *spec = codec->spec;
1671 struct auto_pin_cfg *cfg = &spec->autocfg;
1672 int i;
1673
1674
1675 for (i = 0; i < cfg->dig_outs; i++) {
1676 hda_nid_t nid = cfg->dig_out_pins[i];
1677 if (!cfg->speaker_outs)
1678 continue;
1679 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
David Henningsson5c2e4e02012-10-08 15:44:15 +02001680 snd_hda_jack_detect_enable_callback(codec, nid, SPDIF_EVENT, cs_automute);
Tim Howe56487c22011-07-22 16:41:00 -05001681 spec->spdif_detect = 1;
1682 }
1683 }
1684}
1685
1686static int cs421x_init(struct hda_codec *codec)
1687{
1688 struct cs_spec *spec = codec->spec;
1689
David Henningsson5660ffd2012-01-02 12:40:17 +01001690 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1691 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1692 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1693 cs4210_pinmux_init(codec);
1694 }
Tim Howe56487c22011-07-22 16:41:00 -05001695
1696 if (spec->gpio_mask) {
1697 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1698 spec->gpio_mask);
1699 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1700 spec->gpio_dir);
1701 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1702 spec->gpio_data);
1703 }
1704
1705 init_output(codec);
1706 init_input(codec);
1707 init_cs421x_digital(codec);
1708
1709 return 0;
1710}
1711
1712/*
1713 * CS4210 Input MUX (1 ADC)
1714 */
1715static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1716 struct snd_ctl_elem_info *uinfo)
1717{
1718 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1719 struct cs_spec *spec = codec->spec;
1720
1721 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1722}
1723
1724static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_value *ucontrol)
1726{
1727 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1728 struct cs_spec *spec = codec->spec;
1729
1730 ucontrol->value.enumerated.item[0] = spec->cur_input;
1731 return 0;
1732}
1733
1734static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1735 struct snd_ctl_elem_value *ucontrol)
1736{
1737 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1738 struct cs_spec *spec = codec->spec;
1739
1740 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1741 spec->adc_nid[0], &spec->cur_input);
1742
1743}
1744
Daniel J Blueman00e17f72012-11-04 13:19:04 +08001745static const struct snd_kcontrol_new cs421x_capture_source = {
Tim Howe56487c22011-07-22 16:41:00 -05001746 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1747 .name = "Capture Source",
1748 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1749 .info = cs421x_mux_enum_info,
1750 .get = cs421x_mux_enum_get,
1751 .put = cs421x_mux_enum_put,
1752};
1753
1754static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1755{
1756 struct cs_spec *spec = codec->spec;
1757 struct auto_pin_cfg *cfg = &spec->autocfg;
1758 const struct hda_input_mux *imux = &spec->input_mux;
1759 hda_nid_t pin = cfg->inputs[item].pin;
1760 struct snd_kcontrol *kctl;
1761 u32 caps;
1762
1763 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1764 return 0;
1765
1766 caps = query_amp_caps(codec, pin, HDA_INPUT);
1767 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1768 if (caps <= 1)
1769 return 0;
1770
1771 return add_volume(codec, imux->items[item].label, 0,
1772 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1773}
1774
1775/* add a (input-boost) volume control to the given input pin */
1776static int build_cs421x_input(struct hda_codec *codec)
1777{
1778 struct cs_spec *spec = codec->spec;
1779 struct auto_pin_cfg *cfg = &spec->autocfg;
1780 struct hda_input_mux *imux = &spec->input_mux;
1781 int i, err, type_idx;
1782 const char *label;
1783
1784 if (!spec->num_inputs)
1785 return 0;
1786
1787 /* make bind-capture */
1788 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1789 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1790 for (i = 0; i < 2; i++) {
1791 struct snd_kcontrol *kctl;
1792 int n;
1793 if (!spec->capture_bind[i])
1794 return -ENOMEM;
1795 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1796 if (!kctl)
1797 return -ENOMEM;
1798 kctl->private_value = (long)spec->capture_bind[i];
1799 err = snd_hda_ctl_add(codec, 0, kctl);
1800 if (err < 0)
1801 return err;
1802 for (n = 0; n < AUTO_PIN_LAST; n++) {
1803 if (!spec->adc_nid[n])
1804 continue;
1805 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1806 if (err < 0)
1807 return err;
1808 }
1809 }
1810
1811 /* Add Input MUX Items + Capture Volume/Switch */
1812 for (i = 0; i < spec->num_inputs; i++) {
1813 label = hda_get_autocfg_input_label(codec, cfg, i);
1814 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1815
1816 err = cs421x_add_input_volume_control(codec, i);
1817 if (err < 0)
1818 return err;
1819 }
1820
1821 /*
1822 Add 'Capture Source' Switch if
1823 * 2 inputs and no mic detec
1824 * 3 inputs
1825 */
1826 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1827 (spec->num_inputs == 3)) {
1828
1829 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1830 snd_ctl_new1(&cs421x_capture_source, codec));
1831 if (err < 0)
1832 return err;
1833 }
1834
1835 return 0;
1836}
1837
1838/* Single DAC (Mute/Gain) */
1839static int build_cs421x_output(struct hda_codec *codec)
1840{
1841 hda_nid_t dac = CS4210_DAC_NID;
1842 struct cs_spec *spec = codec->spec;
1843 struct auto_pin_cfg *cfg = &spec->autocfg;
1844 struct snd_kcontrol *kctl;
1845 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001846 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001847
1848 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001849
1850 err = add_mute(codec, name, 0,
1851 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1852 if (err < 0)
1853 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001854
1855 err = add_volume(codec, name, 0,
1856 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1857 if (err < 0)
1858 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001859
David Henningsson5660ffd2012-01-02 12:40:17 +01001860 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001861 err = snd_hda_ctl_add(codec, 0,
1862 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1863 if (err < 0)
1864 return err;
1865 }
1866 return err;
1867}
1868
1869static int cs421x_build_controls(struct hda_codec *codec)
1870{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001871 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001872 int err;
1873
1874 err = build_cs421x_output(codec);
1875 if (err < 0)
1876 return err;
1877 err = build_cs421x_input(codec);
1878 if (err < 0)
1879 return err;
1880 err = build_digital_output(codec);
1881 if (err < 0)
1882 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001883 err = cs421x_init(codec);
1884 if (err < 0)
1885 return err;
1886
1887 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1888 if (err < 0)
1889 return err;
1890
1891 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001892}
1893
Tim Howe56487c22011-07-22 16:41:00 -05001894static int parse_cs421x_input(struct hda_codec *codec)
1895{
1896 struct cs_spec *spec = codec->spec;
1897 struct auto_pin_cfg *cfg = &spec->autocfg;
1898 int i;
1899
1900 for (i = 0; i < cfg->num_inputs; i++) {
1901 hda_nid_t pin = cfg->inputs[i].pin;
1902 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1903 spec->cur_input = spec->last_input = i;
1904 spec->num_inputs++;
1905
1906 /* check whether the automatic mic switch is available */
1907 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1908 spec->mic_detect = 1;
1909 spec->automic_idx = i;
1910 }
1911 }
1912 return 0;
1913}
1914
1915static int cs421x_parse_auto_config(struct hda_codec *codec)
1916{
1917 struct cs_spec *spec = codec->spec;
1918 int err;
1919
1920 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1921 if (err < 0)
1922 return err;
1923 err = parse_output(codec);
1924 if (err < 0)
1925 return err;
1926 err = parse_cs421x_input(codec);
1927 if (err < 0)
1928 return err;
1929 err = parse_digital_output(codec);
1930 if (err < 0)
1931 return err;
1932 return 0;
1933}
1934
1935#ifdef CONFIG_PM
1936/*
1937 Manage PDREF, when transitioning to D3hot
1938 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1939*/
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001940static int cs421x_suspend(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001941{
David Henningsson5660ffd2012-01-02 12:40:17 +01001942 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001943 unsigned int coef;
1944
1945 snd_hda_shutup_pins(codec);
1946
1947 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1948 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1949 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1950 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1951
David Henningsson5660ffd2012-01-02 12:40:17 +01001952 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1953 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1954 coef |= 0x0004; /* PDREF */
1955 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1956 }
Tim Howe56487c22011-07-22 16:41:00 -05001957
1958 return 0;
1959}
1960#endif
1961
Daniel J Blueman00e17f72012-11-04 13:19:04 +08001962static const struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001963 .build_controls = cs421x_build_controls,
1964 .build_pcms = cs_build_pcms,
1965 .init = cs421x_init,
1966 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +02001967 .unsol_event = snd_hda_jack_unsol_event,
Tim Howe56487c22011-07-22 16:41:00 -05001968#ifdef CONFIG_PM
1969 .suspend = cs421x_suspend,
1970#endif
1971};
1972
David Henningsson5660ffd2012-01-02 12:40:17 +01001973static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001974{
1975 struct cs_spec *spec;
1976 int err;
1977
1978 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1979 if (!spec)
1980 return -ENOMEM;
1981 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001982 snd_hda_gen_init(&spec->gen);
Tim Howe56487c22011-07-22 16:41:00 -05001983
David Henningsson5660ffd2012-01-02 12:40:17 +01001984 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001985
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001986 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1987 cs421x_fixups);
1988 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Tim Howe56487c22011-07-22 16:41:00 -05001989
1990 /*
1991 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1992 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1993 is disabled.
1994 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001995 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001996
1997 err = cs421x_parse_auto_config(codec);
1998 if (err < 0)
1999 goto error;
2000
David Henningsson5660ffd2012-01-02 12:40:17 +01002001 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05002002
Takashi Iwaib35aabd2012-08-24 19:09:45 +02002003 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
2004
Tim Howe56487c22011-07-22 16:41:00 -05002005 return 0;
2006
2007 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02002008 cs_free(codec);
Tim Howe56487c22011-07-22 16:41:00 -05002009 codec->spec = NULL;
2010 return err;
2011}
2012
David Henningsson5660ffd2012-01-02 12:40:17 +01002013static int patch_cs4213(struct hda_codec *codec)
2014{
2015 struct cs_spec *spec;
2016 int err;
2017
2018 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2019 if (!spec)
2020 return -ENOMEM;
2021 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02002022 snd_hda_gen_init(&spec->gen);
David Henningsson5660ffd2012-01-02 12:40:17 +01002023
2024 spec->vendor_nid = CS4213_VENDOR_NID;
2025
2026 err = cs421x_parse_auto_config(codec);
2027 if (err < 0)
2028 goto error;
2029
2030 codec->patch_ops = cs421x_patch_ops;
2031 return 0;
2032
2033 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02002034 cs_free(codec);
David Henningsson5660ffd2012-01-02 12:40:17 +01002035 codec->spec = NULL;
2036 return err;
2037}
2038
Takashi Iwaie5f14242009-07-01 18:11:44 +02002039
2040/*
2041 * patch entries
2042 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002043static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002044 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2045 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002046 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2047 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002048 {} /* terminator */
2049};
2050
2051MODULE_ALIAS("snd-hda-codec-id:10134206");
2052MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002053MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002054MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002055
2056MODULE_LICENSE("GPL");
2057MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2058
2059static struct hda_codec_preset_list cirrus_list = {
2060 .preset = snd_hda_preset_cirrus,
2061 .owner = THIS_MODULE,
2062};
2063
2064static int __init patch_cirrus_init(void)
2065{
2066 return snd_hda_add_codec_preset(&cirrus_list);
2067}
2068
2069static void __exit patch_cirrus_exit(void)
2070{
2071 snd_hda_delete_codec_preset(&cirrus_list);
2072}
2073
2074module_init(patch_cirrus_init)
2075module_exit(patch_cirrus_exit)