blob: a7f8790ae885cc71a595af5042f8b2f3e047197a [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;
Tim Howe56487c22011-07-22 16:41:00 -050071 /* CS421x */
72 unsigned int spdif_detect:1;
73 unsigned int sense_b:1;
74 hda_nid_t vendor_nid;
75 struct hda_input_mux input_mux;
76 unsigned int last_input;
Takashi Iwaie5f14242009-07-01 18:11:44 +020077};
78
Tim Howe56487c22011-07-22 16:41:00 -050079/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020080enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040081 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020082 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010083 CS420X_IMAC27,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020084 CS420X_GPIO_13,
85 CS420X_GPIO_23,
Takashi Iwaief596a52012-09-11 16:53:08 +020086 CS420X_MBP101,
87 CS420X_MBP101_COEF,
Takashi Iwaia6bae202009-07-06 15:15:22 +020088 CS420X_AUTO,
Takashi Iwai03efce72012-09-13 09:56:57 +020089 /* aliases */
90 CS420X_IMAC27_122 = CS420X_GPIO_23,
91 CS420X_APPLE = CS420X_GPIO_13,
Takashi Iwaia6bae202009-07-06 15:15:22 +020092};
93
Tim Howe56487c22011-07-22 16:41:00 -050094/* CS421x boards */
95enum {
96 CS421X_CDB4210,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020097 CS421X_SENSE_B,
Tim Howe56487c22011-07-22 16:41:00 -050098};
99
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200100/* Vendor-specific processing widget */
101#define CS420X_VENDOR_NID 0x11
102#define CS_DIG_OUT1_PIN_NID 0x10
103#define CS_DIG_OUT2_PIN_NID 0x15
104#define CS_DMIC1_PIN_NID 0x12
105#define CS_DMIC2_PIN_NID 0x0e
106
107/* coef indices */
108#define IDX_SPDIF_STAT 0x0000
109#define IDX_SPDIF_CTL 0x0001
110#define IDX_ADC_CFG 0x0002
111/* SZC bitmask, 4 modes below:
112 * 0 = immediate,
113 * 1 = digital immediate, analog zero-cross
114 * 2 = digtail & analog soft-ramp
115 * 3 = digital soft-ramp, analog zero-cross
116 */
117#define CS_COEF_ADC_SZC_MASK (3 << 0)
118#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
119#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
120/* PGA mode: 0 = differential, 1 = signle-ended */
121#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
122#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
123#define IDX_DAC_CFG 0x0003
124/* SZC bitmask, 4 modes below:
125 * 0 = Immediate
126 * 1 = zero-cross
127 * 2 = soft-ramp
128 * 3 = soft-ramp on zero-cross
129 */
130#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
131#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
132#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
133
134#define IDX_BEEP_CFG 0x0004
135/* 0x0008 - test reg key */
136/* 0x0009 - 0x0014 -> 12 test regs */
137/* 0x0015 - visibility reg */
138
Tim Howe56487c22011-07-22 16:41:00 -0500139/*
140 * Cirrus Logic CS4210
141 *
142 * 1 DAC => HP(sense) / Speakers,
143 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
144 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
145*/
146#define CS4210_DAC_NID 0x02
147#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100148#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500149#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
150#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
151
152#define CS421X_IDX_DEV_CFG 0x01
153#define CS421X_IDX_ADC_CFG 0x02
154#define CS421X_IDX_DAC_CFG 0x03
155#define CS421X_IDX_SPK_CTL 0x04
156
157#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200158
David Henningsson5660ffd2012-01-02 12:40:17 +0100159/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
160#define CS4213_VENDOR_NID 0x09
161
162
Takashi Iwai277a57c2009-07-08 12:42:08 +0200163static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200164{
Tim Howe56487c22011-07-22 16:41:00 -0500165 struct cs_spec *spec = codec->spec;
166 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200167 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500168 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200169 AC_VERB_GET_PROC_COEF, 0);
170}
171
Takashi Iwai277a57c2009-07-08 12:42:08 +0200172static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
173 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200174{
Tim Howe56487c22011-07-22 16:41:00 -0500175 struct cs_spec *spec = codec->spec;
176 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200177 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500178 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200179 AC_VERB_SET_PROC_COEF, coef);
180}
181
182
Takashi Iwaie5f14242009-07-01 18:11:44 +0200183#define HP_EVENT 1
184#define MIC_EVENT 2
185
186/*
187 * PCM callbacks
188 */
189static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
190 struct hda_codec *codec,
191 struct snd_pcm_substream *substream)
192{
193 struct cs_spec *spec = codec->spec;
194 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
195 hinfo);
196}
197
198static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
199 struct hda_codec *codec,
200 unsigned int stream_tag,
201 unsigned int format,
202 struct snd_pcm_substream *substream)
203{
204 struct cs_spec *spec = codec->spec;
205 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
206 stream_tag, format, substream);
207}
208
209static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
210 struct hda_codec *codec,
211 struct snd_pcm_substream *substream)
212{
213 struct cs_spec *spec = codec->spec;
214 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
215}
216
217/*
218 * Digital out
219 */
220static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
221 struct hda_codec *codec,
222 struct snd_pcm_substream *substream)
223{
224 struct cs_spec *spec = codec->spec;
225 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
226}
227
228static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
229 struct hda_codec *codec,
230 struct snd_pcm_substream *substream)
231{
232 struct cs_spec *spec = codec->spec;
233 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
234}
235
236static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
237 struct hda_codec *codec,
238 unsigned int stream_tag,
239 unsigned int format,
240 struct snd_pcm_substream *substream)
241{
242 struct cs_spec *spec = codec->spec;
243 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
244 format, substream);
245}
246
247static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
248 struct hda_codec *codec,
249 struct snd_pcm_substream *substream)
250{
251 struct cs_spec *spec = codec->spec;
252 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
253}
254
Takashi Iwai05ee7962011-11-16 18:05:11 +0100255static void cs_update_input_select(struct hda_codec *codec)
256{
257 struct cs_spec *spec = codec->spec;
258 if (spec->cur_adc)
259 snd_hda_codec_write(codec, spec->cur_adc, 0,
260 AC_VERB_SET_CONNECT_SEL,
261 spec->adc_idx[spec->cur_input]);
262}
263
Takashi Iwaie5f14242009-07-01 18:11:44 +0200264/*
265 * Analog capture
266 */
267static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
268 struct hda_codec *codec,
269 unsigned int stream_tag,
270 unsigned int format,
271 struct snd_pcm_substream *substream)
272{
273 struct cs_spec *spec = codec->spec;
274 spec->cur_adc = spec->adc_nid[spec->cur_input];
275 spec->cur_adc_stream_tag = stream_tag;
276 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100277 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200278 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
279 return 0;
280}
281
282static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
283 struct hda_codec *codec,
284 struct snd_pcm_substream *substream)
285{
286 struct cs_spec *spec = codec->spec;
287 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
288 spec->cur_adc = 0;
289 return 0;
290}
291
292/*
293 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200294static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200295 .substreams = 1,
296 .channels_min = 2,
297 .channels_max = 2,
298 .ops = {
299 .open = cs_playback_pcm_open,
300 .prepare = cs_playback_pcm_prepare,
301 .cleanup = cs_playback_pcm_cleanup
302 },
303};
304
Takashi Iwaic42d4782011-05-02 11:36:09 +0200305static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200306 .substreams = 1,
307 .channels_min = 2,
308 .channels_max = 2,
309 .ops = {
310 .prepare = cs_capture_pcm_prepare,
311 .cleanup = cs_capture_pcm_cleanup
312 },
313};
314
Takashi Iwaic42d4782011-05-02 11:36:09 +0200315static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200316 .substreams = 1,
317 .channels_min = 2,
318 .channels_max = 2,
319 .ops = {
320 .open = cs_dig_playback_pcm_open,
321 .close = cs_dig_playback_pcm_close,
322 .prepare = cs_dig_playback_pcm_prepare,
323 .cleanup = cs_dig_playback_pcm_cleanup
324 },
325};
326
Takashi Iwaic42d4782011-05-02 11:36:09 +0200327static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200328 .substreams = 1,
329 .channels_min = 2,
330 .channels_max = 2,
331};
332
333static int cs_build_pcms(struct hda_codec *codec)
334{
335 struct cs_spec *spec = codec->spec;
336 struct hda_pcm *info = spec->pcm_rec;
337
338 codec->pcm_info = info;
339 codec->num_pcms = 0;
340
341 info->name = "Cirrus Analog";
342 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
343 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
344 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
345 spec->multiout.max_channels;
346 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200347 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
348 spec->adc_nid[spec->cur_input];
349 codec->num_pcms++;
350
351 if (!spec->multiout.dig_out_nid && !spec->dig_in)
352 return 0;
353
354 info++;
355 info->name = "Cirrus Digital";
356 info->pcm_type = spec->autocfg.dig_out_type[0];
357 if (!info->pcm_type)
358 info->pcm_type = HDA_PCM_TYPE_SPDIF;
359 if (spec->multiout.dig_out_nid) {
360 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
361 cs_pcm_digital_playback;
362 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
363 spec->multiout.dig_out_nid;
364 }
365 if (spec->dig_in) {
366 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
367 cs_pcm_digital_capture;
368 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
369 }
370 codec->num_pcms++;
371
372 return 0;
373}
374
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200375/*
376 * parse codec topology
377 */
378
Takashi Iwaie5f14242009-07-01 18:11:44 +0200379static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
380{
381 hda_nid_t dac;
382 if (!pin)
383 return 0;
384 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
385 return 0;
386 return dac;
387}
388
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200389static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
390{
391 struct cs_spec *spec = codec->spec;
392 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200393 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200394 unsigned int val;
395 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200396 return 0;
397 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200398 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200399}
400
401static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
402 unsigned int *idxp)
403{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200404 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200405 hda_nid_t nid;
406
407 nid = codec->start_nid;
408 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200409 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200410 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200411 if (type != AC_WID_AUD_IN)
412 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200413 idx = snd_hda_get_conn_index(codec, nid, pin, false);
414 if (idx >= 0) {
415 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200416 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200417 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200418 }
419 return 0;
420}
421
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200422static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
423{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200424 unsigned int val;
425 val = snd_hda_codec_get_pincfg(codec, nid);
426 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
427}
428
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200429static int parse_output(struct hda_codec *codec)
430{
431 struct cs_spec *spec = codec->spec;
432 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200433 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200434 hda_nid_t dac;
435
436 for (i = 0; i < cfg->line_outs; i++) {
437 dac = get_dac(codec, cfg->line_out_pins[i]);
438 if (!dac)
439 break;
440 spec->dac_nid[i] = dac;
441 }
442 spec->multiout.num_dacs = i;
443 spec->multiout.dac_nids = spec->dac_nid;
444 spec->multiout.max_channels = i * 2;
445
446 /* add HP and speakers */
447 extra_nids = 0;
448 for (i = 0; i < cfg->hp_outs; i++) {
449 dac = get_dac(codec, cfg->hp_pins[i]);
450 if (!dac)
451 break;
452 if (!i)
453 spec->multiout.hp_nid = dac;
454 else
455 spec->multiout.extra_out_nid[extra_nids++] = dac;
456 }
457 for (i = 0; i < cfg->speaker_outs; i++) {
458 dac = get_dac(codec, cfg->speaker_pins[i]);
459 if (!dac)
460 break;
461 spec->multiout.extra_out_nid[extra_nids++] = dac;
462 }
463
464 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
465 cfg->speaker_outs = cfg->line_outs;
466 memcpy(cfg->speaker_pins, cfg->line_out_pins,
467 sizeof(cfg->speaker_pins));
468 cfg->line_outs = 0;
469 }
470
471 return 0;
472}
473
474static int parse_input(struct hda_codec *codec)
475{
476 struct cs_spec *spec = codec->spec;
477 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200478 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200479
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200480 for (i = 0; i < cfg->num_inputs; i++) {
481 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200482 spec->input_idx[spec->num_inputs] = i;
483 spec->capsrc_idx[i] = spec->num_inputs++;
484 spec->cur_input = i;
485 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
486 }
487 if (!spec->num_inputs)
488 return 0;
489
490 /* check whether the automatic mic switch is available */
491 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200492 cfg->inputs[0].type == AUTO_PIN_MIC &&
493 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200494 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
495 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200496 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200497 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200498 }
499 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200500 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200501 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200502 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200503 }
504 }
505 }
506 return 0;
507}
508
509
510static int parse_digital_output(struct hda_codec *codec)
511{
512 struct cs_spec *spec = codec->spec;
513 struct auto_pin_cfg *cfg = &spec->autocfg;
514 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200515
516 if (!cfg->dig_outs)
517 return 0;
518 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
519 return 0;
520 spec->multiout.dig_out_nid = nid;
521 spec->multiout.share_spdif = 1;
522 if (cfg->dig_outs > 1 &&
523 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
524 spec->slave_dig_outs[0] = nid;
525 codec->slave_dig_outs = spec->slave_dig_outs;
526 }
527 return 0;
528}
529
530static int parse_digital_input(struct hda_codec *codec)
531{
532 struct cs_spec *spec = codec->spec;
533 struct auto_pin_cfg *cfg = &spec->autocfg;
534 int idx;
535
Takashi Iwai60e53882009-07-06 15:01:09 +0200536 if (cfg->dig_in_pin)
537 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
538 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200539}
540
541/*
542 * create mixer controls
543 */
544
Takashi Iwaiea734962011-01-17 11:29:34 +0100545static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200546
547static int add_mute(struct hda_codec *codec, const char *name, int index,
548 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
549{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200550 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200551 struct snd_kcontrol_new knew =
552 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
553 knew.private_value = pval;
554 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
555 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100556 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100557 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200558}
559
560static int add_volume(struct hda_codec *codec, const char *name,
561 int index, unsigned int pval, int dir,
562 struct snd_kcontrol **kctlp)
563{
David Henningsson2e1210b2011-09-14 13:22:54 +0200564 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200565 struct snd_kcontrol_new knew =
566 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
567 knew.private_value = pval;
568 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
569 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100570 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100571 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200572}
573
574static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
575{
576 unsigned int caps;
577
578 /* set the upper-limit for mixer amp to 0dB */
579 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
580 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
581 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
582 << AC_AMPCAP_NUM_STEPS_SHIFT;
583 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
584}
585
586static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
587{
588 struct cs_spec *spec = codec->spec;
589 unsigned int tlv[4];
590 int err;
591
592 spec->vmaster_sw =
593 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100594 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200595 if (err < 0)
596 return err;
597
598 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
599 spec->vmaster_vol =
600 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100601 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200602 if (err < 0)
603 return err;
604 return 0;
605}
606
607static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
608 int num_ctls, int type)
609{
610 struct cs_spec *spec = codec->spec;
611 const char *name;
612 int err, index;
613 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200614 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200615 "Front Speaker", "Surround Speaker", "Bass Speaker"
616 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200617 static const char * const line_outs[] = {
Takashi Iwaie49a3432012-03-01 18:14:41 +0100618 "Front Line Out", "Surround Line Out", "Bass Line Out"
Takashi Iwaie5f14242009-07-01 18:11:44 +0200619 };
620
621 fix_volume_caps(codec, dac);
622 if (!spec->vmaster_sw) {
623 err = add_vmaster(codec, dac);
624 if (err < 0)
625 return err;
626 }
627
628 index = 0;
629 switch (type) {
630 case AUTO_PIN_HP_OUT:
631 name = "Headphone";
632 index = idx;
633 break;
634 case AUTO_PIN_SPEAKER_OUT:
635 if (num_ctls > 1)
636 name = speakers[idx];
637 else
638 name = "Speaker";
639 break;
640 default:
641 if (num_ctls > 1)
642 name = line_outs[idx];
643 else
Takashi Iwaie49a3432012-03-01 18:14:41 +0100644 name = "Line Out";
Takashi Iwaie5f14242009-07-01 18:11:44 +0200645 break;
646 }
647
648 err = add_mute(codec, name, index,
649 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
650 if (err < 0)
651 return err;
652 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
653 if (err < 0)
654 return err;
655
656 err = add_volume(codec, name, index,
657 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
658 if (err < 0)
659 return err;
660 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
661 if (err < 0)
662 return err;
663
664 return 0;
665}
666
667static int build_output(struct hda_codec *codec)
668{
669 struct cs_spec *spec = codec->spec;
670 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200671 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200672
673 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200674 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
675 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200676 if (err < 0)
677 return err;
678 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200679 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200680 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
681 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200682 if (err < 0)
683 return err;
684 }
685 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200686 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
687 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200688 if (err < 0)
689 return err;
690 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200691 return 0;
692}
693
694/*
695 */
696
Takashi Iwaic42d4782011-05-02 11:36:09 +0200697static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200698 HDA_BIND_SW("Capture Switch", 0),
699 HDA_BIND_VOL("Capture Volume", 0),
700};
701
Takashi Iwaiea359292009-07-06 12:58:59 +0200702static int change_cur_input(struct hda_codec *codec, unsigned int idx,
703 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200704{
705 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200706
Takashi Iwaiea359292009-07-06 12:58:59 +0200707 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200708 return 0;
709 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
710 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200711 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200712 spec->cur_adc = spec->adc_nid[idx];
713 snd_hda_codec_setup_stream(codec, spec->cur_adc,
714 spec->cur_adc_stream_tag, 0,
715 spec->cur_adc_format);
716 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200717 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100718 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200719 return 1;
720}
721
722static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
723 struct snd_ctl_elem_info *uinfo)
724{
725 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
726 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200727 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200728 unsigned int idx;
729
730 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
731 uinfo->count = 1;
732 uinfo->value.enumerated.items = spec->num_inputs;
733 if (uinfo->value.enumerated.item >= spec->num_inputs)
734 uinfo->value.enumerated.item = spec->num_inputs - 1;
735 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100736 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
737 uinfo->value.enumerated.name,
738 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200739 return 0;
740}
741
742static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
743 struct snd_ctl_elem_value *ucontrol)
744{
745 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
746 struct cs_spec *spec = codec->spec;
747 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
748 return 0;
749}
750
751static int cs_capture_source_put(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 unsigned int idx = ucontrol->value.enumerated.item[0];
757
758 if (idx >= spec->num_inputs)
759 return -EINVAL;
760 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200761 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200762}
763
Takashi Iwaic42d4782011-05-02 11:36:09 +0200764static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200765 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
766 .name = "Capture Source",
767 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
768 .info = cs_capture_source_info,
769 .get = cs_capture_source_get,
770 .put = cs_capture_source_put,
771};
772
Takashi Iwaic42d4782011-05-02 11:36:09 +0200773static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200774 struct hda_ctl_ops *ops)
775{
776 struct cs_spec *spec = codec->spec;
777 struct hda_bind_ctls *bind;
778 int i, n;
779
780 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
781 GFP_KERNEL);
782 if (!bind)
783 return NULL;
784 bind->ops = ops;
785 n = 0;
786 for (i = 0; i < AUTO_PIN_LAST; i++) {
787 if (!spec->adc_nid[i])
788 continue;
789 bind->values[n++] =
790 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
791 spec->adc_idx[i], HDA_INPUT);
792 }
793 return bind;
794}
795
Takashi Iwai6a929342010-10-11 15:16:20 +0200796/* add a (input-boost) volume control to the given input pin */
797static int add_input_volume_control(struct hda_codec *codec,
798 struct auto_pin_cfg *cfg,
799 int item)
800{
801 hda_nid_t pin = cfg->inputs[item].pin;
802 u32 caps;
803 const char *label;
804 struct snd_kcontrol *kctl;
805
806 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
807 return 0;
808 caps = query_amp_caps(codec, pin, HDA_INPUT);
809 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
810 if (caps <= 1)
811 return 0;
812 label = hda_get_autocfg_input_label(codec, cfg, item);
813 return add_volume(codec, label, 0,
814 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
815}
816
Takashi Iwaie5f14242009-07-01 18:11:44 +0200817static int build_input(struct hda_codec *codec)
818{
819 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200820 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200821
Takashi Iwaie5f14242009-07-01 18:11:44 +0200822 if (!spec->num_inputs)
823 return 0;
824
Takashi Iwaie5f14242009-07-01 18:11:44 +0200825 /* make bind-capture */
826 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
827 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
828 for (i = 0; i < 2; i++) {
829 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100830 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200831 if (!spec->capture_bind[i])
832 return -ENOMEM;
833 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
834 if (!kctl)
835 return -ENOMEM;
836 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100837 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200838 if (err < 0)
839 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100840 for (n = 0; n < AUTO_PIN_LAST; n++) {
841 if (!spec->adc_nid[n])
842 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700843 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100844 if (err < 0)
845 return err;
846 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200847 }
848
849 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100850 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200851 snd_ctl_new1(&cs_capture_source, codec));
852 if (err < 0)
853 return err;
854 }
855
Takashi Iwai6a929342010-10-11 15:16:20 +0200856 for (i = 0; i < spec->num_inputs; i++) {
857 err = add_input_volume_control(codec, &spec->autocfg, i);
858 if (err < 0)
859 return err;
860 }
861
Takashi Iwaie5f14242009-07-01 18:11:44 +0200862 return 0;
863}
864
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200865/*
866 */
867
Takashi Iwaie5f14242009-07-01 18:11:44 +0200868static int build_digital_output(struct hda_codec *codec)
869{
870 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200871 int err;
872
Takashi Iwai63b24132009-07-09 11:45:59 +0200873 if (!spec->multiout.dig_out_nid)
874 return 0;
875
Takashi Iwaidcda5802012-10-12 17:24:51 +0200876 err = snd_hda_create_dig_out_ctls(codec, spec->multiout.dig_out_nid,
877 spec->multiout.dig_out_nid,
878 spec->pcm_rec[1].pcm_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200879 if (err < 0)
880 return err;
881 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
882 if (err < 0)
883 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200884 return 0;
885}
886
887static int build_digital_input(struct hda_codec *codec)
888{
889 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200890 if (spec->dig_in)
891 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
892 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200893}
894
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200895/*
896 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500897 * CS421x auto-output redirecting
898 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200899 */
900
David Henningsson5c2e4e02012-10-08 15:44:15 +0200901static void cs_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200902{
903 struct cs_spec *spec = codec->spec;
904 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200905 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500906 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200907 hda_nid_t nid;
908 int i;
909
Tim Howe56487c22011-07-22 16:41:00 -0500910 spdif_present = 0;
911 if (cfg->dig_outs) {
912 nid = cfg->dig_out_pins[0];
913 if (is_jack_detectable(codec, nid)) {
914 /*
915 TODO: SPDIF output redirect when SENSE_B is enabled.
916 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
917 assumed.
918 */
919 if (snd_hda_jack_detect(codec, nid)
920 /* && spec->sense_b */)
921 spdif_present = 1;
922 }
923 }
924
Takashi Iwaie5f14242009-07-01 18:11:44 +0200925 hp_present = 0;
926 for (i = 0; i < cfg->hp_outs; i++) {
927 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200928 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200929 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800930 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200931 if (hp_present)
932 break;
933 }
Tim Howe56487c22011-07-22 16:41:00 -0500934
935 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200936 for (i = 0; i < cfg->speaker_outs; i++) {
David Henningsson78e2a922012-01-02 12:40:16 +0100937 int pin_ctl = hp_present ? 0 : PIN_OUT;
David Henningsson5660ffd2012-01-02 12:40:17 +0100938 /* detect on spdif is specific to CS4210 */
939 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
David Henningsson78e2a922012-01-02 12:40:16 +0100940 pin_ctl = 0;
941
Takashi Iwaie5f14242009-07-01 18:11:44 +0200942 nid = cfg->speaker_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200943 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200944 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100945 if (spec->gpio_eapd_hp) {
946 unsigned int gpio = hp_present ?
947 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200948 snd_hda_codec_write(codec, 0x01, 0,
949 AC_VERB_SET_GPIO_DATA, gpio);
950 }
Tim Howe56487c22011-07-22 16:41:00 -0500951
David Henningsson5660ffd2012-01-02 12:40:17 +0100952 /* specific to CS4210 */
953 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500954 /* mute HPs if spdif jack (SENSE_B) is present */
955 for (i = 0; i < cfg->hp_outs; i++) {
956 nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200957 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500958 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
959 }
960
961 /* SPDIF TX on/off */
962 if (cfg->dig_outs) {
963 nid = cfg->dig_out_pins[0];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200964 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500965 spdif_present ? PIN_OUT : 0);
966
967 }
968 /* Update board GPIOs if neccessary ... */
969 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200970}
971
Tim Howe56487c22011-07-22 16:41:00 -0500972/*
973 * Auto-input redirect for CS421x
974 * Switch max 3 inputs of a single ADC (nid 3)
975*/
976
David Henningsson5c2e4e02012-10-08 15:44:15 +0200977static void cs_automic(struct hda_codec *codec, struct hda_jack_tbl *tbl)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200978{
979 struct cs_spec *spec = codec->spec;
980 struct auto_pin_cfg *cfg = &spec->autocfg;
981 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800982 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500983
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200984 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800985 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500986
987 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100988 if (spec->vendor_nid == CS420X_VENDOR_NID) {
989 if (present)
990 change_cur_input(codec, spec->automic_idx, 0);
991 else
992 change_cur_input(codec, !spec->automic_idx, 0);
993 } else {
Tim Howe56487c22011-07-22 16:41:00 -0500994 if (present) {
Dylan Reidf70eecd2012-01-31 13:04:41 -0800995 if (spec->cur_input != spec->automic_idx) {
996 spec->last_input = spec->cur_input;
997 spec->cur_input = spec->automic_idx;
998 }
Tim Howe56487c22011-07-22 16:41:00 -0500999 } else {
1000 spec->cur_input = spec->last_input;
1001 }
Takashi Iwai05ee7962011-11-16 18:05:11 +01001002 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001003 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001004}
1005
1006/*
1007 */
1008
1009static void init_output(struct hda_codec *codec)
1010{
1011 struct cs_spec *spec = codec->spec;
1012 struct auto_pin_cfg *cfg = &spec->autocfg;
1013 int i;
1014
1015 /* mute first */
1016 for (i = 0; i < spec->multiout.num_dacs; i++)
1017 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1018 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1019 if (spec->multiout.hp_nid)
1020 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1021 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1022 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1023 if (!spec->multiout.extra_out_nid[i])
1024 break;
1025 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1026 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1027 }
1028
1029 /* set appropriate pin controls */
1030 for (i = 0; i < cfg->line_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001031 snd_hda_set_pin_ctl(codec, cfg->line_out_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001032 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001033 for (i = 0; i < cfg->hp_outs; i++) {
1034 hda_nid_t nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001035 snd_hda_set_pin_ctl(codec, nid, PIN_HP);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001036 if (!cfg->speaker_outs)
1037 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001038 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
David Henningsson5c2e4e02012-10-08 15:44:15 +02001039 snd_hda_jack_detect_enable_callback(codec, nid, HP_EVENT, cs_automute);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001040 spec->hp_detect = 1;
1041 }
1042 }
Tim Howe56487c22011-07-22 16:41:00 -05001043
1044 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001045 for (i = 0; i < cfg->speaker_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001046 snd_hda_set_pin_ctl(codec, cfg->speaker_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001047
1048 /* SPDIF is enabled on presence detect for CS421x */
1049 if (spec->hp_detect || spec->spdif_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001050 cs_automute(codec, NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001051}
1052
1053static void init_input(struct hda_codec *codec)
1054{
1055 struct cs_spec *spec = codec->spec;
1056 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001057 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001058 int i;
1059
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001060 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001061 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001062 hda_nid_t pin = cfg->inputs[i].pin;
1063 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001064 continue;
1065 /* set appropriate pin control and mute first */
1066 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +02001067 if (cfg->inputs[i].type == AUTO_PIN_MIC)
1068 ctl |= snd_hda_get_default_vref(codec, pin);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001069 snd_hda_set_pin_ctl(codec, pin, ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001070 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1071 AC_VERB_SET_AMP_GAIN_MUTE,
1072 AMP_IN_MUTE(spec->adc_idx[i]));
1073 if (spec->mic_detect && spec->automic_idx == i)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001074 snd_hda_jack_detect_enable_callback(codec, pin, MIC_EVENT, cs_automic);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001075 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001076 /* CS420x has multiple ADC, CS421x has single ADC */
1077 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001078 change_cur_input(codec, spec->cur_input, 1);
1079 if (spec->mic_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001080 cs_automic(codec, NULL);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001081
Tim Howe56487c22011-07-22 16:41:00 -05001082 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1083 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1084 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1085 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1086 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1087 * No effect if SPDIF_OUT2 is
1088 * selected in IDX_SPDIF_CTL.
1089 */
1090 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001091 } else {
1092 if (spec->mic_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001093 cs_automic(codec, NULL);
David Henningsson5660ffd2012-01-02 12:40:17 +01001094 else {
1095 spec->cur_adc = spec->adc_nid[spec->cur_input];
1096 cs_update_input_select(codec);
1097 }
Tim Howe56487c22011-07-22 16:41:00 -05001098 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001099}
1100
Takashi Iwaic42d4782011-05-02 11:36:09 +02001101static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001102 {0x11, AC_VERB_SET_PROC_STATE, 1},
1103 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1104 {0x11, AC_VERB_SET_PROC_COEF,
1105 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1106 | 0x0040 /* Mute DACs on FIFO error */
1107 | 0x1000 /* Enable DACs High Pass Filter */
1108 | 0x0400 /* Disable Coefficient Auto increment */
1109 )},
1110 /* Beep */
1111 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1112 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1113
1114 {} /* terminator */
1115};
1116
Brian Austina769cbc2010-09-07 14:36:22 -05001117/* Errata: CS4207 rev C0/C1/C2 Silicon
1118 *
1119 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1120 *
1121 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1122 * may be excessive (up to an additional 200 μA), which is most easily
1123 * observed while the part is being held in reset (RESET# active low).
1124 *
1125 * Root Cause: At initial powerup of the device, the logic that drives
1126 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1127 * initialized.
1128 * Certain random patterns will cause a steady leakage current in those
1129 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1130 *
1131 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1132 * blocks, which will alleviate the issue.
1133 */
1134
Takashi Iwaic42d4782011-05-02 11:36:09 +02001135static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001136 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1137 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1138
1139 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1140 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1141 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1142 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1143 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1144 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1145
1146 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1147 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1148
1149 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1150 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1151 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1152 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1153 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1154 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1155 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1156
Takashi Iwai38c07642011-03-03 14:54:19 +01001157#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001158 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1159 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1160 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001161#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001162
1163 {} /* terminator */
1164};
1165
Takashi Iwaief596a52012-09-11 16:53:08 +02001166static const struct hda_verb mbp101_init_verbs[] = {
1167 {0x11, AC_VERB_SET_COEF_INDEX, 0x0002},
1168 {0x11, AC_VERB_SET_PROC_COEF, 0x100a},
1169 {0x11, AC_VERB_SET_COEF_INDEX, 0x0004},
1170 {0x11, AC_VERB_SET_PROC_COEF, 0x000f},
1171 {}
1172};
1173
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001174/* SPDIF setup */
1175static void init_digital(struct hda_codec *codec)
1176{
1177 unsigned int coef;
1178
1179 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1180 coef |= 0x0008; /* Replace with mute on error */
1181 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1182 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1183 * SPDIF_OUT2 is shared with GPIO1 and
1184 * DMIC_SDA2.
1185 */
1186 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001187}
1188
1189static int cs_init(struct hda_codec *codec)
1190{
1191 struct cs_spec *spec = codec->spec;
1192
Brian Austina769cbc2010-09-07 14:36:22 -05001193 /* init_verb sequence for C0/C1/C2 errata*/
1194 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1195
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001196 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001197
1198 if (spec->gpio_mask) {
1199 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1200 spec->gpio_mask);
1201 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1202 spec->gpio_dir);
1203 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1204 spec->gpio_data);
1205 }
1206
Takashi Iwaie5f14242009-07-01 18:11:44 +02001207 init_output(codec);
1208 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001209 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001210
Takashi Iwaie5f14242009-07-01 18:11:44 +02001211 return 0;
1212}
1213
1214static int cs_build_controls(struct hda_codec *codec)
1215{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001216 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001217 int err;
1218
1219 err = build_output(codec);
1220 if (err < 0)
1221 return err;
1222 err = build_input(codec);
1223 if (err < 0)
1224 return err;
1225 err = build_digital_output(codec);
1226 if (err < 0)
1227 return err;
1228 err = build_digital_input(codec);
1229 if (err < 0)
1230 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001231 err = cs_init(codec);
1232 if (err < 0)
1233 return err;
1234
1235 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1236 if (err < 0)
1237 return err;
1238
1239 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001240}
1241
1242static void cs_free(struct hda_codec *codec)
1243{
1244 struct cs_spec *spec = codec->spec;
1245 kfree(spec->capture_bind[0]);
1246 kfree(spec->capture_bind[1]);
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001247 snd_hda_gen_free(&spec->gen);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001248 kfree(codec->spec);
1249}
1250
Takashi Iwaic42d4782011-05-02 11:36:09 +02001251static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001252 .build_controls = cs_build_controls,
1253 .build_pcms = cs_build_pcms,
1254 .init = cs_init,
1255 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +02001256 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwaie5f14242009-07-01 18:11:44 +02001257};
1258
1259static int cs_parse_auto_config(struct hda_codec *codec)
1260{
1261 struct cs_spec *spec = codec->spec;
1262 int err;
1263
1264 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1265 if (err < 0)
1266 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001267
1268 err = parse_output(codec);
1269 if (err < 0)
1270 return err;
1271 err = parse_input(codec);
1272 if (err < 0)
1273 return err;
1274 err = parse_digital_output(codec);
1275 if (err < 0)
1276 return err;
1277 err = parse_digital_input(codec);
1278 if (err < 0)
1279 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001280 return 0;
1281}
1282
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001283static const struct hda_model_fixup cs420x_models[] = {
1284 { .id = CS420X_MBP53, .name = "mbp53" },
1285 { .id = CS420X_MBP55, .name = "mbp55" },
1286 { .id = CS420X_IMAC27, .name = "imac27" },
1287 { .id = CS420X_IMAC27_122, .name = "imac27_122" },
1288 { .id = CS420X_APPLE, .name = "apple" },
Takashi Iwaief596a52012-09-11 16:53:08 +02001289 { .id = CS420X_MBP101, .name = "mbp101" },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001290 {}
Takashi Iwaia6bae202009-07-06 15:15:22 +02001291};
1292
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001293static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001294 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001295 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001296 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001297 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001298 /* this conflicts with too many other models */
1299 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001300
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001301 /* codec SSID */
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001302 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwaief596a52012-09-11 16:53:08 +02001303 SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001304 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001305 {} /* terminator */
1306};
1307
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001308static const struct hda_pintbl mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001309 { 0x09, 0x012b4050 },
1310 { 0x0a, 0x90100141 },
1311 { 0x0b, 0x90100140 },
1312 { 0x0c, 0x018b3020 },
1313 { 0x0d, 0x90a00110 },
1314 { 0x0e, 0x400000f0 },
1315 { 0x0f, 0x01cbe030 },
1316 { 0x10, 0x014be060 },
1317 { 0x12, 0x400000f0 },
1318 { 0x15, 0x400000f0 },
1319 {} /* terminator */
1320};
1321
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001322static const struct hda_pintbl mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001323 { 0x09, 0x012b4030 },
1324 { 0x0a, 0x90100121 },
1325 { 0x0b, 0x90100120 },
1326 { 0x0c, 0x400000f0 },
1327 { 0x0d, 0x90a00110 },
1328 { 0x0e, 0x400000f0 },
1329 { 0x0f, 0x400000f0 },
1330 { 0x10, 0x014be040 },
1331 { 0x12, 0x400000f0 },
1332 { 0x15, 0x400000f0 },
1333 {} /* terminator */
1334};
1335
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001336static const struct hda_pintbl imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001337 { 0x09, 0x012b4050 },
1338 { 0x0a, 0x90100140 },
1339 { 0x0b, 0x90100142 },
1340 { 0x0c, 0x018b3020 },
1341 { 0x0d, 0x90a00110 },
1342 { 0x0e, 0x400000f0 },
1343 { 0x0f, 0x01cbe030 },
1344 { 0x10, 0x014be060 },
1345 { 0x12, 0x01ab9070 },
1346 { 0x15, 0x400000f0 },
1347 {} /* terminator */
1348};
1349
Takashi Iwaief596a52012-09-11 16:53:08 +02001350static const struct hda_pintbl mbp101_pincfgs[] = {
1351 { 0x0d, 0x40ab90f0 },
1352 { 0x0e, 0x90a600f0 },
1353 { 0x12, 0x50a600f0 },
1354 {} /* terminator */
1355};
1356
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001357static void cs420x_fixup_gpio_13(struct hda_codec *codec,
1358 const struct hda_fixup *fix, int action)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001359{
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001360 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1361 struct cs_spec *spec = codec->spec;
1362 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1363 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1364 spec->gpio_mask = spec->gpio_dir =
1365 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1366 }
Takashi Iwaia6bae202009-07-06 15:15:22 +02001367}
1368
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001369static void cs420x_fixup_gpio_23(struct hda_codec *codec,
1370 const struct hda_fixup *fix, int action)
1371{
1372 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1373 struct cs_spec *spec = codec->spec;
1374 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1375 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1376 spec->gpio_mask = spec->gpio_dir =
1377 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1378 }
1379}
1380
1381static const struct hda_fixup cs420x_fixups[] = {
1382 [CS420X_MBP53] = {
1383 .type = HDA_FIXUP_PINS,
1384 .v.pins = mbp53_pincfgs,
1385 .chained = true,
1386 .chain_id = CS420X_APPLE,
1387 },
1388 [CS420X_MBP55] = {
1389 .type = HDA_FIXUP_PINS,
1390 .v.pins = mbp55_pincfgs,
1391 .chained = true,
1392 .chain_id = CS420X_GPIO_13,
1393 },
1394 [CS420X_IMAC27] = {
1395 .type = HDA_FIXUP_PINS,
1396 .v.pins = imac27_pincfgs,
1397 .chained = true,
1398 .chain_id = CS420X_GPIO_13,
1399 },
1400 [CS420X_GPIO_13] = {
1401 .type = HDA_FIXUP_FUNC,
1402 .v.func = cs420x_fixup_gpio_13,
1403 },
1404 [CS420X_GPIO_23] = {
1405 .type = HDA_FIXUP_FUNC,
1406 .v.func = cs420x_fixup_gpio_23,
1407 },
Takashi Iwaief596a52012-09-11 16:53:08 +02001408 [CS420X_MBP101] = {
1409 .type = HDA_FIXUP_PINS,
1410 .v.pins = mbp101_pincfgs,
1411 .chained = true,
1412 .chain_id = CS420X_MBP101_COEF,
1413 },
1414 [CS420X_MBP101_COEF] = {
1415 .type = HDA_FIXUP_VERBS,
1416 .v.verbs = mbp101_init_verbs,
1417 .chained = true,
1418 .chain_id = CS420X_GPIO_13,
1419 },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001420};
1421
Takashi Iwaie5f14242009-07-01 18:11:44 +02001422static int patch_cs420x(struct hda_codec *codec)
1423{
1424 struct cs_spec *spec;
1425 int err;
1426
1427 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1428 if (!spec)
1429 return -ENOMEM;
1430 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001431 snd_hda_gen_init(&spec->gen);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001432
Tim Howe56487c22011-07-22 16:41:00 -05001433 spec->vendor_nid = CS420X_VENDOR_NID;
1434
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001435 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
1436 cs420x_fixups);
1437 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001438
Takashi Iwaied208252009-07-07 09:04:26 +02001439 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001440 if (err < 0)
1441 goto error;
1442
Takashi Iwaie5f14242009-07-01 18:11:44 +02001443 codec->patch_ops = cs_patch_ops;
1444
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001445 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1446
Takashi Iwaie5f14242009-07-01 18:11:44 +02001447 return 0;
1448
1449 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02001450 cs_free(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001451 codec->spec = NULL;
1452 return err;
1453}
1454
Tim Howe56487c22011-07-22 16:41:00 -05001455/*
1456 * Cirrus Logic CS4210
1457 *
1458 * 1 DAC => HP(sense) / Speakers,
1459 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1460 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1461*/
1462
1463/* CS4210 board names */
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001464static const struct hda_model_fixup cs421x_models[] = {
1465 { .id = CS421X_CDB4210, .name = "cdb4210" },
1466 {}
Tim Howe56487c22011-07-22 16:41:00 -05001467};
1468
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001469static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001470 /* Test Intel board + CDB2410 */
1471 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1472 {} /* terminator */
1473};
1474
1475/* CS4210 board pinconfigs */
1476/* Default CS4210 (CDB4210)*/
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001477static const struct hda_pintbl cdb4210_pincfgs[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001478 { 0x05, 0x0321401f },
1479 { 0x06, 0x90170010 },
1480 { 0x07, 0x03813031 },
1481 { 0x08, 0xb7a70037 },
1482 { 0x09, 0xb7a6003e },
1483 { 0x0a, 0x034510f0 },
1484 {} /* terminator */
1485};
1486
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001487/* Setup GPIO/SENSE for each board (if used) */
1488static void cs421x_fixup_sense_b(struct hda_codec *codec,
1489 const struct hda_fixup *fix, int action)
1490{
1491 struct cs_spec *spec = codec->spec;
1492 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1493 spec->sense_b = 1;
1494}
1495
1496static const struct hda_fixup cs421x_fixups[] = {
1497 [CS421X_CDB4210] = {
1498 .type = HDA_FIXUP_PINS,
1499 .v.pins = cdb4210_pincfgs,
1500 .chained = true,
1501 .chain_id = CS421X_SENSE_B,
1502 },
1503 [CS421X_SENSE_B] = {
1504 .type = HDA_FIXUP_FUNC,
1505 .v.func = cs421x_fixup_sense_b,
1506 }
Tim Howe56487c22011-07-22 16:41:00 -05001507};
1508
1509static const struct hda_verb cs421x_coef_init_verbs[] = {
1510 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1511 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1512 /*
1513 Disable Coefficient Index Auto-Increment(DAI)=1,
1514 PDREF=0
1515 */
1516 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1517
1518 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1519 /* ADC SZCMode = Digital Soft Ramp */
1520 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1521
1522 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1523 {0x0B, AC_VERB_SET_PROC_COEF,
1524 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1525 | 0x0004 /* Mute DAC on FIFO error */
1526 | 0x0008 /* Enable DAC High Pass Filter */
1527 )},
1528 {} /* terminator */
1529};
1530
1531/* Errata: CS4210 rev A1 Silicon
1532 *
1533 * http://www.cirrus.com/en/pubs/errata/
1534 *
1535 * Description:
1536 * 1. Performance degredation is present in the ADC.
1537 * 2. Speaker output is not completely muted upon HP detect.
1538 * 3. Noise is present when clipping occurs on the amplified
1539 * speaker outputs.
1540 *
1541 * Workaround:
1542 * The following verb sequence written to the registers during
1543 * initialization will correct the issues listed above.
1544 */
1545
1546static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1547 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1548
1549 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1550 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1551
1552 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1553 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1554
1555 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1556 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1557
1558 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1559 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1560
1561 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1562 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1563
1564 {} /* terminator */
1565};
1566
1567/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1568static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1569
1570static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1571 struct snd_ctl_elem_info *uinfo)
1572{
1573 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1574 uinfo->count = 1;
1575 uinfo->value.integer.min = 0;
1576 uinfo->value.integer.max = 3;
1577 return 0;
1578}
1579
1580static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1581 struct snd_ctl_elem_value *ucontrol)
1582{
1583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1584
1585 ucontrol->value.integer.value[0] =
1586 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1587 return 0;
1588}
1589
1590static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1591 struct snd_ctl_elem_value *ucontrol)
1592{
1593 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1594
1595 unsigned int vol = ucontrol->value.integer.value[0];
1596 unsigned int coef =
1597 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1598 unsigned int original_coef = coef;
1599
1600 coef &= ~0x0003;
1601 coef |= (vol & 0x0003);
1602 if (original_coef == coef)
1603 return 0;
1604 else {
1605 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1606 return 1;
1607 }
1608}
1609
1610static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1611
1612 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1613 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1614 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1615 .name = "Speaker Boost Playback Volume",
1616 .info = cs421x_boost_vol_info,
1617 .get = cs421x_boost_vol_get,
1618 .put = cs421x_boost_vol_put,
1619 .tlv = { .p = cs421x_speaker_boost_db_scale },
1620};
1621
David Henningsson5660ffd2012-01-02 12:40:17 +01001622static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001623{
1624 struct cs_spec *spec = codec->spec;
1625 unsigned int def_conf, coef;
1626
1627 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1628 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1629
1630 if (spec->gpio_mask)
1631 coef |= 0x0008; /* B1,B2 are GPIOs */
1632 else
1633 coef &= ~0x0008;
1634
1635 if (spec->sense_b)
1636 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1637 else
1638 coef &= ~0x0010;
1639
1640 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1641
1642 if ((spec->gpio_mask || spec->sense_b) &&
1643 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1644
1645 /*
1646 GPIO or SENSE_B forced - disconnect the DMIC pin.
1647 */
1648 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1649 def_conf &= ~AC_DEFCFG_PORT_CONN;
1650 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1651 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1652 }
1653}
1654
1655static void init_cs421x_digital(struct hda_codec *codec)
1656{
1657 struct cs_spec *spec = codec->spec;
1658 struct auto_pin_cfg *cfg = &spec->autocfg;
1659 int i;
1660
1661
1662 for (i = 0; i < cfg->dig_outs; i++) {
1663 hda_nid_t nid = cfg->dig_out_pins[i];
1664 if (!cfg->speaker_outs)
1665 continue;
1666 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
David Henningsson5c2e4e02012-10-08 15:44:15 +02001667 snd_hda_jack_detect_enable_callback(codec, nid, SPDIF_EVENT, cs_automute);
Tim Howe56487c22011-07-22 16:41:00 -05001668 spec->spdif_detect = 1;
1669 }
1670 }
1671}
1672
1673static int cs421x_init(struct hda_codec *codec)
1674{
1675 struct cs_spec *spec = codec->spec;
1676
David Henningsson5660ffd2012-01-02 12:40:17 +01001677 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1678 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1679 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1680 cs4210_pinmux_init(codec);
1681 }
Tim Howe56487c22011-07-22 16:41:00 -05001682
1683 if (spec->gpio_mask) {
1684 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1685 spec->gpio_mask);
1686 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1687 spec->gpio_dir);
1688 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1689 spec->gpio_data);
1690 }
1691
1692 init_output(codec);
1693 init_input(codec);
1694 init_cs421x_digital(codec);
1695
1696 return 0;
1697}
1698
1699/*
1700 * CS4210 Input MUX (1 ADC)
1701 */
1702static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1703 struct snd_ctl_elem_info *uinfo)
1704{
1705 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1706 struct cs_spec *spec = codec->spec;
1707
1708 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1709}
1710
1711static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_value *ucontrol)
1713{
1714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1715 struct cs_spec *spec = codec->spec;
1716
1717 ucontrol->value.enumerated.item[0] = spec->cur_input;
1718 return 0;
1719}
1720
1721static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1722 struct snd_ctl_elem_value *ucontrol)
1723{
1724 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1725 struct cs_spec *spec = codec->spec;
1726
1727 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1728 spec->adc_nid[0], &spec->cur_input);
1729
1730}
1731
1732static struct snd_kcontrol_new cs421x_capture_source = {
1733
1734 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1735 .name = "Capture Source",
1736 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1737 .info = cs421x_mux_enum_info,
1738 .get = cs421x_mux_enum_get,
1739 .put = cs421x_mux_enum_put,
1740};
1741
1742static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1743{
1744 struct cs_spec *spec = codec->spec;
1745 struct auto_pin_cfg *cfg = &spec->autocfg;
1746 const struct hda_input_mux *imux = &spec->input_mux;
1747 hda_nid_t pin = cfg->inputs[item].pin;
1748 struct snd_kcontrol *kctl;
1749 u32 caps;
1750
1751 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1752 return 0;
1753
1754 caps = query_amp_caps(codec, pin, HDA_INPUT);
1755 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1756 if (caps <= 1)
1757 return 0;
1758
1759 return add_volume(codec, imux->items[item].label, 0,
1760 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1761}
1762
1763/* add a (input-boost) volume control to the given input pin */
1764static int build_cs421x_input(struct hda_codec *codec)
1765{
1766 struct cs_spec *spec = codec->spec;
1767 struct auto_pin_cfg *cfg = &spec->autocfg;
1768 struct hda_input_mux *imux = &spec->input_mux;
1769 int i, err, type_idx;
1770 const char *label;
1771
1772 if (!spec->num_inputs)
1773 return 0;
1774
1775 /* make bind-capture */
1776 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1777 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1778 for (i = 0; i < 2; i++) {
1779 struct snd_kcontrol *kctl;
1780 int n;
1781 if (!spec->capture_bind[i])
1782 return -ENOMEM;
1783 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1784 if (!kctl)
1785 return -ENOMEM;
1786 kctl->private_value = (long)spec->capture_bind[i];
1787 err = snd_hda_ctl_add(codec, 0, kctl);
1788 if (err < 0)
1789 return err;
1790 for (n = 0; n < AUTO_PIN_LAST; n++) {
1791 if (!spec->adc_nid[n])
1792 continue;
1793 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1794 if (err < 0)
1795 return err;
1796 }
1797 }
1798
1799 /* Add Input MUX Items + Capture Volume/Switch */
1800 for (i = 0; i < spec->num_inputs; i++) {
1801 label = hda_get_autocfg_input_label(codec, cfg, i);
1802 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1803
1804 err = cs421x_add_input_volume_control(codec, i);
1805 if (err < 0)
1806 return err;
1807 }
1808
1809 /*
1810 Add 'Capture Source' Switch if
1811 * 2 inputs and no mic detec
1812 * 3 inputs
1813 */
1814 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1815 (spec->num_inputs == 3)) {
1816
1817 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1818 snd_ctl_new1(&cs421x_capture_source, codec));
1819 if (err < 0)
1820 return err;
1821 }
1822
1823 return 0;
1824}
1825
1826/* Single DAC (Mute/Gain) */
1827static int build_cs421x_output(struct hda_codec *codec)
1828{
1829 hda_nid_t dac = CS4210_DAC_NID;
1830 struct cs_spec *spec = codec->spec;
1831 struct auto_pin_cfg *cfg = &spec->autocfg;
1832 struct snd_kcontrol *kctl;
1833 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001834 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001835
1836 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001837
1838 err = add_mute(codec, name, 0,
1839 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1840 if (err < 0)
1841 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001842
1843 err = add_volume(codec, name, 0,
1844 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1845 if (err < 0)
1846 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001847
David Henningsson5660ffd2012-01-02 12:40:17 +01001848 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001849 err = snd_hda_ctl_add(codec, 0,
1850 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1851 if (err < 0)
1852 return err;
1853 }
1854 return err;
1855}
1856
1857static int cs421x_build_controls(struct hda_codec *codec)
1858{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001859 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001860 int err;
1861
1862 err = build_cs421x_output(codec);
1863 if (err < 0)
1864 return err;
1865 err = build_cs421x_input(codec);
1866 if (err < 0)
1867 return err;
1868 err = build_digital_output(codec);
1869 if (err < 0)
1870 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001871 err = cs421x_init(codec);
1872 if (err < 0)
1873 return err;
1874
1875 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1876 if (err < 0)
1877 return err;
1878
1879 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001880}
1881
Tim Howe56487c22011-07-22 16:41:00 -05001882static int parse_cs421x_input(struct hda_codec *codec)
1883{
1884 struct cs_spec *spec = codec->spec;
1885 struct auto_pin_cfg *cfg = &spec->autocfg;
1886 int i;
1887
1888 for (i = 0; i < cfg->num_inputs; i++) {
1889 hda_nid_t pin = cfg->inputs[i].pin;
1890 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1891 spec->cur_input = spec->last_input = i;
1892 spec->num_inputs++;
1893
1894 /* check whether the automatic mic switch is available */
1895 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1896 spec->mic_detect = 1;
1897 spec->automic_idx = i;
1898 }
1899 }
1900 return 0;
1901}
1902
1903static int cs421x_parse_auto_config(struct hda_codec *codec)
1904{
1905 struct cs_spec *spec = codec->spec;
1906 int err;
1907
1908 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1909 if (err < 0)
1910 return err;
1911 err = parse_output(codec);
1912 if (err < 0)
1913 return err;
1914 err = parse_cs421x_input(codec);
1915 if (err < 0)
1916 return err;
1917 err = parse_digital_output(codec);
1918 if (err < 0)
1919 return err;
1920 return 0;
1921}
1922
1923#ifdef CONFIG_PM
1924/*
1925 Manage PDREF, when transitioning to D3hot
1926 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1927*/
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001928static int cs421x_suspend(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001929{
David Henningsson5660ffd2012-01-02 12:40:17 +01001930 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001931 unsigned int coef;
1932
1933 snd_hda_shutup_pins(codec);
1934
1935 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1936 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1937 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1938 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1939
David Henningsson5660ffd2012-01-02 12:40:17 +01001940 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1941 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1942 coef |= 0x0004; /* PDREF */
1943 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1944 }
Tim Howe56487c22011-07-22 16:41:00 -05001945
1946 return 0;
1947}
1948#endif
1949
David Henningsson5660ffd2012-01-02 12:40:17 +01001950static struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001951 .build_controls = cs421x_build_controls,
1952 .build_pcms = cs_build_pcms,
1953 .init = cs421x_init,
1954 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +02001955 .unsol_event = snd_hda_jack_unsol_event,
Tim Howe56487c22011-07-22 16:41:00 -05001956#ifdef CONFIG_PM
1957 .suspend = cs421x_suspend,
1958#endif
1959};
1960
David Henningsson5660ffd2012-01-02 12:40:17 +01001961static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001962{
1963 struct cs_spec *spec;
1964 int err;
1965
1966 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1967 if (!spec)
1968 return -ENOMEM;
1969 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001970 snd_hda_gen_init(&spec->gen);
Tim Howe56487c22011-07-22 16:41:00 -05001971
David Henningsson5660ffd2012-01-02 12:40:17 +01001972 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001973
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001974 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1975 cs421x_fixups);
1976 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Tim Howe56487c22011-07-22 16:41:00 -05001977
1978 /*
1979 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1980 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1981 is disabled.
1982 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001983 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001984
1985 err = cs421x_parse_auto_config(codec);
1986 if (err < 0)
1987 goto error;
1988
David Henningsson5660ffd2012-01-02 12:40:17 +01001989 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05001990
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001991 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1992
Tim Howe56487c22011-07-22 16:41:00 -05001993 return 0;
1994
1995 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02001996 cs_free(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001997 codec->spec = NULL;
1998 return err;
1999}
2000
David Henningsson5660ffd2012-01-02 12:40:17 +01002001static int patch_cs4213(struct hda_codec *codec)
2002{
2003 struct cs_spec *spec;
2004 int err;
2005
2006 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2007 if (!spec)
2008 return -ENOMEM;
2009 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02002010 snd_hda_gen_init(&spec->gen);
David Henningsson5660ffd2012-01-02 12:40:17 +01002011
2012 spec->vendor_nid = CS4213_VENDOR_NID;
2013
2014 err = cs421x_parse_auto_config(codec);
2015 if (err < 0)
2016 goto error;
2017
2018 codec->patch_ops = cs421x_patch_ops;
2019 return 0;
2020
2021 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02002022 cs_free(codec);
David Henningsson5660ffd2012-01-02 12:40:17 +01002023 codec->spec = NULL;
2024 return err;
2025}
2026
Takashi Iwaie5f14242009-07-01 18:11:44 +02002027
2028/*
2029 * patch entries
2030 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002031static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002032 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2033 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002034 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2035 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002036 {} /* terminator */
2037};
2038
2039MODULE_ALIAS("snd-hda-codec-id:10134206");
2040MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002041MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002042MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002043
2044MODULE_LICENSE("GPL");
2045MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2046
2047static struct hda_codec_preset_list cirrus_list = {
2048 .preset = snd_hda_preset_cirrus,
2049 .owner = THIS_MODULE,
2050};
2051
2052static int __init patch_cirrus_init(void)
2053{
2054 return snd_hda_add_codec_preset(&cirrus_list);
2055}
2056
2057static void __exit patch_cirrus_exit(void)
2058{
2059 snd_hda_delete_codec_preset(&cirrus_list);
2060}
2061
2062module_init(patch_cirrus_init)
2063module_exit(patch_cirrus_exit)