blob: 82253f13735902a6c9ac5ea3c8c5cabbaf44ac9c [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,
86 CS420X_IMAC27_122 = CS420X_GPIO_23,
87 CS420X_APPLE = CS420X_GPIO_13,
Takashi Iwaief596a52012-09-11 16:53:08 +020088 CS420X_MBP101,
89 CS420X_MBP101_COEF,
Takashi Iwaia6bae202009-07-06 15:15:22 +020090 CS420X_AUTO,
Takashi Iwaia6bae202009-07-06 15:15:22 +020091};
92
Tim Howe56487c22011-07-22 16:41:00 -050093/* CS421x boards */
94enum {
95 CS421X_CDB4210,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020096 CS421X_SENSE_B,
Tim Howe56487c22011-07-22 16:41:00 -050097};
98
Takashi Iwai40c20fa2009-07-06 13:00:57 +020099/* Vendor-specific processing widget */
100#define CS420X_VENDOR_NID 0x11
101#define CS_DIG_OUT1_PIN_NID 0x10
102#define CS_DIG_OUT2_PIN_NID 0x15
103#define CS_DMIC1_PIN_NID 0x12
104#define CS_DMIC2_PIN_NID 0x0e
105
106/* coef indices */
107#define IDX_SPDIF_STAT 0x0000
108#define IDX_SPDIF_CTL 0x0001
109#define IDX_ADC_CFG 0x0002
110/* SZC bitmask, 4 modes below:
111 * 0 = immediate,
112 * 1 = digital immediate, analog zero-cross
113 * 2 = digtail & analog soft-ramp
114 * 3 = digital soft-ramp, analog zero-cross
115 */
116#define CS_COEF_ADC_SZC_MASK (3 << 0)
117#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
118#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
119/* PGA mode: 0 = differential, 1 = signle-ended */
120#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
121#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
122#define IDX_DAC_CFG 0x0003
123/* SZC bitmask, 4 modes below:
124 * 0 = Immediate
125 * 1 = zero-cross
126 * 2 = soft-ramp
127 * 3 = soft-ramp on zero-cross
128 */
129#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
130#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
131#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
132
133#define IDX_BEEP_CFG 0x0004
134/* 0x0008 - test reg key */
135/* 0x0009 - 0x0014 -> 12 test regs */
136/* 0x0015 - visibility reg */
137
Tim Howe56487c22011-07-22 16:41:00 -0500138/*
139 * Cirrus Logic CS4210
140 *
141 * 1 DAC => HP(sense) / Speakers,
142 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
143 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
144*/
145#define CS4210_DAC_NID 0x02
146#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100147#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500148#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
149#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
150
151#define CS421X_IDX_DEV_CFG 0x01
152#define CS421X_IDX_ADC_CFG 0x02
153#define CS421X_IDX_DAC_CFG 0x03
154#define CS421X_IDX_SPK_CTL 0x04
155
156#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200157
David Henningsson5660ffd2012-01-02 12:40:17 +0100158/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
159#define CS4213_VENDOR_NID 0x09
160
161
Takashi Iwai277a57c2009-07-08 12:42:08 +0200162static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200163{
Tim Howe56487c22011-07-22 16:41:00 -0500164 struct cs_spec *spec = codec->spec;
165 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200166 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500167 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200168 AC_VERB_GET_PROC_COEF, 0);
169}
170
Takashi Iwai277a57c2009-07-08 12:42:08 +0200171static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
172 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200173{
Tim Howe56487c22011-07-22 16:41:00 -0500174 struct cs_spec *spec = codec->spec;
175 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200176 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500177 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200178 AC_VERB_SET_PROC_COEF, coef);
179}
180
181
Takashi Iwaie5f14242009-07-01 18:11:44 +0200182#define HP_EVENT 1
183#define MIC_EVENT 2
184
185/*
186 * PCM callbacks
187 */
188static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
189 struct hda_codec *codec,
190 struct snd_pcm_substream *substream)
191{
192 struct cs_spec *spec = codec->spec;
193 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
194 hinfo);
195}
196
197static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
198 struct hda_codec *codec,
199 unsigned int stream_tag,
200 unsigned int format,
201 struct snd_pcm_substream *substream)
202{
203 struct cs_spec *spec = codec->spec;
204 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
205 stream_tag, format, substream);
206}
207
208static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
209 struct hda_codec *codec,
210 struct snd_pcm_substream *substream)
211{
212 struct cs_spec *spec = codec->spec;
213 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
214}
215
216/*
217 * Digital out
218 */
219static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
220 struct hda_codec *codec,
221 struct snd_pcm_substream *substream)
222{
223 struct cs_spec *spec = codec->spec;
224 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
225}
226
227static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
228 struct hda_codec *codec,
229 struct snd_pcm_substream *substream)
230{
231 struct cs_spec *spec = codec->spec;
232 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
233}
234
235static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
236 struct hda_codec *codec,
237 unsigned int stream_tag,
238 unsigned int format,
239 struct snd_pcm_substream *substream)
240{
241 struct cs_spec *spec = codec->spec;
242 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
243 format, substream);
244}
245
246static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
247 struct hda_codec *codec,
248 struct snd_pcm_substream *substream)
249{
250 struct cs_spec *spec = codec->spec;
251 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
252}
253
Takashi Iwai05ee7962011-11-16 18:05:11 +0100254static void cs_update_input_select(struct hda_codec *codec)
255{
256 struct cs_spec *spec = codec->spec;
257 if (spec->cur_adc)
258 snd_hda_codec_write(codec, spec->cur_adc, 0,
259 AC_VERB_SET_CONNECT_SEL,
260 spec->adc_idx[spec->cur_input]);
261}
262
Takashi Iwaie5f14242009-07-01 18:11:44 +0200263/*
264 * Analog capture
265 */
266static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
267 struct hda_codec *codec,
268 unsigned int stream_tag,
269 unsigned int format,
270 struct snd_pcm_substream *substream)
271{
272 struct cs_spec *spec = codec->spec;
273 spec->cur_adc = spec->adc_nid[spec->cur_input];
274 spec->cur_adc_stream_tag = stream_tag;
275 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100276 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200277 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
278 return 0;
279}
280
281static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
282 struct hda_codec *codec,
283 struct snd_pcm_substream *substream)
284{
285 struct cs_spec *spec = codec->spec;
286 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
287 spec->cur_adc = 0;
288 return 0;
289}
290
291/*
292 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200293static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200294 .substreams = 1,
295 .channels_min = 2,
296 .channels_max = 2,
297 .ops = {
298 .open = cs_playback_pcm_open,
299 .prepare = cs_playback_pcm_prepare,
300 .cleanup = cs_playback_pcm_cleanup
301 },
302};
303
Takashi Iwaic42d4782011-05-02 11:36:09 +0200304static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200305 .substreams = 1,
306 .channels_min = 2,
307 .channels_max = 2,
308 .ops = {
309 .prepare = cs_capture_pcm_prepare,
310 .cleanup = cs_capture_pcm_cleanup
311 },
312};
313
Takashi Iwaic42d4782011-05-02 11:36:09 +0200314static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200315 .substreams = 1,
316 .channels_min = 2,
317 .channels_max = 2,
318 .ops = {
319 .open = cs_dig_playback_pcm_open,
320 .close = cs_dig_playback_pcm_close,
321 .prepare = cs_dig_playback_pcm_prepare,
322 .cleanup = cs_dig_playback_pcm_cleanup
323 },
324};
325
Takashi Iwaic42d4782011-05-02 11:36:09 +0200326static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200327 .substreams = 1,
328 .channels_min = 2,
329 .channels_max = 2,
330};
331
332static int cs_build_pcms(struct hda_codec *codec)
333{
334 struct cs_spec *spec = codec->spec;
335 struct hda_pcm *info = spec->pcm_rec;
336
337 codec->pcm_info = info;
338 codec->num_pcms = 0;
339
340 info->name = "Cirrus Analog";
341 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
342 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
343 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
344 spec->multiout.max_channels;
345 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200346 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
347 spec->adc_nid[spec->cur_input];
348 codec->num_pcms++;
349
350 if (!spec->multiout.dig_out_nid && !spec->dig_in)
351 return 0;
352
353 info++;
354 info->name = "Cirrus Digital";
355 info->pcm_type = spec->autocfg.dig_out_type[0];
356 if (!info->pcm_type)
357 info->pcm_type = HDA_PCM_TYPE_SPDIF;
358 if (spec->multiout.dig_out_nid) {
359 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
360 cs_pcm_digital_playback;
361 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
362 spec->multiout.dig_out_nid;
363 }
364 if (spec->dig_in) {
365 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
366 cs_pcm_digital_capture;
367 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
368 }
369 codec->num_pcms++;
370
371 return 0;
372}
373
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200374/*
375 * parse codec topology
376 */
377
Takashi Iwaie5f14242009-07-01 18:11:44 +0200378static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
379{
380 hda_nid_t dac;
381 if (!pin)
382 return 0;
383 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
384 return 0;
385 return dac;
386}
387
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200388static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
389{
390 struct cs_spec *spec = codec->spec;
391 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200392 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200393 unsigned int val;
394 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200395 return 0;
396 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200397 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200398}
399
400static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
401 unsigned int *idxp)
402{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200403 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200404 hda_nid_t nid;
405
406 nid = codec->start_nid;
407 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200408 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200409 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200410 if (type != AC_WID_AUD_IN)
411 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200412 idx = snd_hda_get_conn_index(codec, nid, pin, false);
413 if (idx >= 0) {
414 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200415 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200416 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200417 }
418 return 0;
419}
420
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200421static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
422{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200423 unsigned int val;
424 val = snd_hda_codec_get_pincfg(codec, nid);
425 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
426}
427
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200428static int parse_output(struct hda_codec *codec)
429{
430 struct cs_spec *spec = codec->spec;
431 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200432 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200433 hda_nid_t dac;
434
435 for (i = 0; i < cfg->line_outs; i++) {
436 dac = get_dac(codec, cfg->line_out_pins[i]);
437 if (!dac)
438 break;
439 spec->dac_nid[i] = dac;
440 }
441 spec->multiout.num_dacs = i;
442 spec->multiout.dac_nids = spec->dac_nid;
443 spec->multiout.max_channels = i * 2;
444
445 /* add HP and speakers */
446 extra_nids = 0;
447 for (i = 0; i < cfg->hp_outs; i++) {
448 dac = get_dac(codec, cfg->hp_pins[i]);
449 if (!dac)
450 break;
451 if (!i)
452 spec->multiout.hp_nid = dac;
453 else
454 spec->multiout.extra_out_nid[extra_nids++] = dac;
455 }
456 for (i = 0; i < cfg->speaker_outs; i++) {
457 dac = get_dac(codec, cfg->speaker_pins[i]);
458 if (!dac)
459 break;
460 spec->multiout.extra_out_nid[extra_nids++] = dac;
461 }
462
463 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
464 cfg->speaker_outs = cfg->line_outs;
465 memcpy(cfg->speaker_pins, cfg->line_out_pins,
466 sizeof(cfg->speaker_pins));
467 cfg->line_outs = 0;
468 }
469
470 return 0;
471}
472
473static int parse_input(struct hda_codec *codec)
474{
475 struct cs_spec *spec = codec->spec;
476 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200477 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200478
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200479 for (i = 0; i < cfg->num_inputs; i++) {
480 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200481 spec->input_idx[spec->num_inputs] = i;
482 spec->capsrc_idx[i] = spec->num_inputs++;
483 spec->cur_input = i;
484 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
485 }
486 if (!spec->num_inputs)
487 return 0;
488
489 /* check whether the automatic mic switch is available */
490 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200491 cfg->inputs[0].type == AUTO_PIN_MIC &&
492 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200493 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
494 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200495 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200496 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200497 }
498 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200499 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200500 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200501 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200502 }
503 }
504 }
505 return 0;
506}
507
508
509static int parse_digital_output(struct hda_codec *codec)
510{
511 struct cs_spec *spec = codec->spec;
512 struct auto_pin_cfg *cfg = &spec->autocfg;
513 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200514
515 if (!cfg->dig_outs)
516 return 0;
517 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
518 return 0;
519 spec->multiout.dig_out_nid = nid;
520 spec->multiout.share_spdif = 1;
521 if (cfg->dig_outs > 1 &&
522 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
523 spec->slave_dig_outs[0] = nid;
524 codec->slave_dig_outs = spec->slave_dig_outs;
525 }
526 return 0;
527}
528
529static int parse_digital_input(struct hda_codec *codec)
530{
531 struct cs_spec *spec = codec->spec;
532 struct auto_pin_cfg *cfg = &spec->autocfg;
533 int idx;
534
Takashi Iwai60e53882009-07-06 15:01:09 +0200535 if (cfg->dig_in_pin)
536 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
537 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200538}
539
540/*
541 * create mixer controls
542 */
543
Takashi Iwaiea734962011-01-17 11:29:34 +0100544static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200545
546static int add_mute(struct hda_codec *codec, const char *name, int index,
547 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
548{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200549 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200550 struct snd_kcontrol_new knew =
551 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
552 knew.private_value = pval;
553 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
554 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100555 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100556 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200557}
558
559static int add_volume(struct hda_codec *codec, const char *name,
560 int index, unsigned int pval, int dir,
561 struct snd_kcontrol **kctlp)
562{
David Henningsson2e1210b2011-09-14 13:22:54 +0200563 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200564 struct snd_kcontrol_new knew =
565 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
566 knew.private_value = pval;
567 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
568 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100569 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100570 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200571}
572
573static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
574{
575 unsigned int caps;
576
577 /* set the upper-limit for mixer amp to 0dB */
578 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
579 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
580 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
581 << AC_AMPCAP_NUM_STEPS_SHIFT;
582 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
583}
584
585static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
586{
587 struct cs_spec *spec = codec->spec;
588 unsigned int tlv[4];
589 int err;
590
591 spec->vmaster_sw =
592 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100593 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200594 if (err < 0)
595 return err;
596
597 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
598 spec->vmaster_vol =
599 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100600 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200601 if (err < 0)
602 return err;
603 return 0;
604}
605
606static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
607 int num_ctls, int type)
608{
609 struct cs_spec *spec = codec->spec;
610 const char *name;
611 int err, index;
612 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200613 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200614 "Front Speaker", "Surround Speaker", "Bass Speaker"
615 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200616 static const char * const line_outs[] = {
Takashi Iwaie49a3432012-03-01 18:14:41 +0100617 "Front Line Out", "Surround Line Out", "Bass Line Out"
Takashi Iwaie5f14242009-07-01 18:11:44 +0200618 };
619
620 fix_volume_caps(codec, dac);
621 if (!spec->vmaster_sw) {
622 err = add_vmaster(codec, dac);
623 if (err < 0)
624 return err;
625 }
626
627 index = 0;
628 switch (type) {
629 case AUTO_PIN_HP_OUT:
630 name = "Headphone";
631 index = idx;
632 break;
633 case AUTO_PIN_SPEAKER_OUT:
634 if (num_ctls > 1)
635 name = speakers[idx];
636 else
637 name = "Speaker";
638 break;
639 default:
640 if (num_ctls > 1)
641 name = line_outs[idx];
642 else
Takashi Iwaie49a3432012-03-01 18:14:41 +0100643 name = "Line Out";
Takashi Iwaie5f14242009-07-01 18:11:44 +0200644 break;
645 }
646
647 err = add_mute(codec, name, index,
648 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
649 if (err < 0)
650 return err;
651 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
652 if (err < 0)
653 return err;
654
655 err = add_volume(codec, name, index,
656 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
657 if (err < 0)
658 return err;
659 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
660 if (err < 0)
661 return err;
662
663 return 0;
664}
665
666static int build_output(struct hda_codec *codec)
667{
668 struct cs_spec *spec = codec->spec;
669 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200670 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200671
672 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200673 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
674 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200675 if (err < 0)
676 return err;
677 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200678 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200679 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
680 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200681 if (err < 0)
682 return err;
683 }
684 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200685 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
686 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200687 if (err < 0)
688 return err;
689 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200690 return 0;
691}
692
693/*
694 */
695
Takashi Iwaic42d4782011-05-02 11:36:09 +0200696static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200697 HDA_BIND_SW("Capture Switch", 0),
698 HDA_BIND_VOL("Capture Volume", 0),
699};
700
Takashi Iwaiea359292009-07-06 12:58:59 +0200701static int change_cur_input(struct hda_codec *codec, unsigned int idx,
702 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200703{
704 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200705
Takashi Iwaiea359292009-07-06 12:58:59 +0200706 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200707 return 0;
708 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
709 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200710 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200711 spec->cur_adc = spec->adc_nid[idx];
712 snd_hda_codec_setup_stream(codec, spec->cur_adc,
713 spec->cur_adc_stream_tag, 0,
714 spec->cur_adc_format);
715 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200716 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100717 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200718 return 1;
719}
720
721static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
722 struct snd_ctl_elem_info *uinfo)
723{
724 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
725 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200726 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200727 unsigned int idx;
728
729 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
730 uinfo->count = 1;
731 uinfo->value.enumerated.items = spec->num_inputs;
732 if (uinfo->value.enumerated.item >= spec->num_inputs)
733 uinfo->value.enumerated.item = spec->num_inputs - 1;
734 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100735 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
736 uinfo->value.enumerated.name,
737 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200738 return 0;
739}
740
741static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
742 struct snd_ctl_elem_value *ucontrol)
743{
744 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
745 struct cs_spec *spec = codec->spec;
746 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
747 return 0;
748}
749
750static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
751 struct snd_ctl_elem_value *ucontrol)
752{
753 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
754 struct cs_spec *spec = codec->spec;
755 unsigned int idx = ucontrol->value.enumerated.item[0];
756
757 if (idx >= spec->num_inputs)
758 return -EINVAL;
759 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200760 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200761}
762
Takashi Iwaic42d4782011-05-02 11:36:09 +0200763static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200764 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
765 .name = "Capture Source",
766 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
767 .info = cs_capture_source_info,
768 .get = cs_capture_source_get,
769 .put = cs_capture_source_put,
770};
771
Takashi Iwaic42d4782011-05-02 11:36:09 +0200772static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200773 struct hda_ctl_ops *ops)
774{
775 struct cs_spec *spec = codec->spec;
776 struct hda_bind_ctls *bind;
777 int i, n;
778
779 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
780 GFP_KERNEL);
781 if (!bind)
782 return NULL;
783 bind->ops = ops;
784 n = 0;
785 for (i = 0; i < AUTO_PIN_LAST; i++) {
786 if (!spec->adc_nid[i])
787 continue;
788 bind->values[n++] =
789 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
790 spec->adc_idx[i], HDA_INPUT);
791 }
792 return bind;
793}
794
Takashi Iwai6a929342010-10-11 15:16:20 +0200795/* add a (input-boost) volume control to the given input pin */
796static int add_input_volume_control(struct hda_codec *codec,
797 struct auto_pin_cfg *cfg,
798 int item)
799{
800 hda_nid_t pin = cfg->inputs[item].pin;
801 u32 caps;
802 const char *label;
803 struct snd_kcontrol *kctl;
804
805 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
806 return 0;
807 caps = query_amp_caps(codec, pin, HDA_INPUT);
808 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
809 if (caps <= 1)
810 return 0;
811 label = hda_get_autocfg_input_label(codec, cfg, item);
812 return add_volume(codec, label, 0,
813 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
814}
815
Takashi Iwaie5f14242009-07-01 18:11:44 +0200816static int build_input(struct hda_codec *codec)
817{
818 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200819 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200820
Takashi Iwaie5f14242009-07-01 18:11:44 +0200821 if (!spec->num_inputs)
822 return 0;
823
Takashi Iwaie5f14242009-07-01 18:11:44 +0200824 /* make bind-capture */
825 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
826 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
827 for (i = 0; i < 2; i++) {
828 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100829 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200830 if (!spec->capture_bind[i])
831 return -ENOMEM;
832 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
833 if (!kctl)
834 return -ENOMEM;
835 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100836 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200837 if (err < 0)
838 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100839 for (n = 0; n < AUTO_PIN_LAST; n++) {
840 if (!spec->adc_nid[n])
841 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700842 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100843 if (err < 0)
844 return err;
845 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200846 }
847
848 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100849 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200850 snd_ctl_new1(&cs_capture_source, codec));
851 if (err < 0)
852 return err;
853 }
854
Takashi Iwai6a929342010-10-11 15:16:20 +0200855 for (i = 0; i < spec->num_inputs; i++) {
856 err = add_input_volume_control(codec, &spec->autocfg, i);
857 if (err < 0)
858 return err;
859 }
860
Takashi Iwaie5f14242009-07-01 18:11:44 +0200861 return 0;
862}
863
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200864/*
865 */
866
Takashi Iwaie5f14242009-07-01 18:11:44 +0200867static int build_digital_output(struct hda_codec *codec)
868{
869 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200870 int err;
871
Takashi Iwai63b24132009-07-09 11:45:59 +0200872 if (!spec->multiout.dig_out_nid)
873 return 0;
874
Stephen Warren74b654c2011-06-01 11:14:18 -0600875 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
876 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200877 if (err < 0)
878 return err;
879 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
880 if (err < 0)
881 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200882 return 0;
883}
884
885static int build_digital_input(struct hda_codec *codec)
886{
887 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200888 if (spec->dig_in)
889 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
890 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200891}
892
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200893/*
894 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500895 * CS421x auto-output redirecting
896 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200897 */
898
Takashi Iwaie5f14242009-07-01 18:11:44 +0200899static void cs_automute(struct hda_codec *codec)
900{
901 struct cs_spec *spec = codec->spec;
902 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200903 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500904 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200905 hda_nid_t nid;
906 int i;
907
Tim Howe56487c22011-07-22 16:41:00 -0500908 spdif_present = 0;
909 if (cfg->dig_outs) {
910 nid = cfg->dig_out_pins[0];
911 if (is_jack_detectable(codec, nid)) {
912 /*
913 TODO: SPDIF output redirect when SENSE_B is enabled.
914 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
915 assumed.
916 */
917 if (snd_hda_jack_detect(codec, nid)
918 /* && spec->sense_b */)
919 spdif_present = 1;
920 }
921 }
922
Takashi Iwaie5f14242009-07-01 18:11:44 +0200923 hp_present = 0;
924 for (i = 0; i < cfg->hp_outs; i++) {
925 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200926 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200927 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800928 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200929 if (hp_present)
930 break;
931 }
Tim Howe56487c22011-07-22 16:41:00 -0500932
933 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200934 for (i = 0; i < cfg->speaker_outs; i++) {
David Henningsson78e2a922012-01-02 12:40:16 +0100935 int pin_ctl = hp_present ? 0 : PIN_OUT;
David Henningsson5660ffd2012-01-02 12:40:17 +0100936 /* detect on spdif is specific to CS4210 */
937 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
David Henningsson78e2a922012-01-02 12:40:16 +0100938 pin_ctl = 0;
939
Takashi Iwaie5f14242009-07-01 18:11:44 +0200940 nid = cfg->speaker_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200941 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200942 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100943 if (spec->gpio_eapd_hp) {
944 unsigned int gpio = hp_present ?
945 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200946 snd_hda_codec_write(codec, 0x01, 0,
947 AC_VERB_SET_GPIO_DATA, gpio);
948 }
Tim Howe56487c22011-07-22 16:41:00 -0500949
David Henningsson5660ffd2012-01-02 12:40:17 +0100950 /* specific to CS4210 */
951 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500952 /* mute HPs if spdif jack (SENSE_B) is present */
953 for (i = 0; i < cfg->hp_outs; i++) {
954 nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200955 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500956 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
957 }
958
959 /* SPDIF TX on/off */
960 if (cfg->dig_outs) {
961 nid = cfg->dig_out_pins[0];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200962 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500963 spdif_present ? PIN_OUT : 0);
964
965 }
966 /* Update board GPIOs if neccessary ... */
967 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200968}
969
Tim Howe56487c22011-07-22 16:41:00 -0500970/*
971 * Auto-input redirect for CS421x
972 * Switch max 3 inputs of a single ADC (nid 3)
973*/
974
Takashi Iwaie5f14242009-07-01 18:11:44 +0200975static void cs_automic(struct hda_codec *codec)
976{
977 struct cs_spec *spec = codec->spec;
978 struct auto_pin_cfg *cfg = &spec->autocfg;
979 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800980 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500981
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200982 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800983 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500984
985 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100986 if (spec->vendor_nid == CS420X_VENDOR_NID) {
987 if (present)
988 change_cur_input(codec, spec->automic_idx, 0);
989 else
990 change_cur_input(codec, !spec->automic_idx, 0);
991 } else {
Tim Howe56487c22011-07-22 16:41:00 -0500992 if (present) {
Dylan Reidf70eecd2012-01-31 13:04:41 -0800993 if (spec->cur_input != spec->automic_idx) {
994 spec->last_input = spec->cur_input;
995 spec->cur_input = spec->automic_idx;
996 }
Tim Howe56487c22011-07-22 16:41:00 -0500997 } else {
998 spec->cur_input = spec->last_input;
999 }
Takashi Iwai05ee7962011-11-16 18:05:11 +01001000 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001001 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001002}
1003
1004/*
1005 */
1006
1007static void init_output(struct hda_codec *codec)
1008{
1009 struct cs_spec *spec = codec->spec;
1010 struct auto_pin_cfg *cfg = &spec->autocfg;
1011 int i;
1012
1013 /* mute first */
1014 for (i = 0; i < spec->multiout.num_dacs; i++)
1015 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1016 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1017 if (spec->multiout.hp_nid)
1018 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1019 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1020 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1021 if (!spec->multiout.extra_out_nid[i])
1022 break;
1023 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1024 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1025 }
1026
1027 /* set appropriate pin controls */
1028 for (i = 0; i < cfg->line_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001029 snd_hda_set_pin_ctl(codec, cfg->line_out_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001030 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001031 for (i = 0; i < cfg->hp_outs; i++) {
1032 hda_nid_t nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001033 snd_hda_set_pin_ctl(codec, nid, PIN_HP);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001034 if (!cfg->speaker_outs)
1035 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001036 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001037 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001038 spec->hp_detect = 1;
1039 }
1040 }
Tim Howe56487c22011-07-22 16:41:00 -05001041
1042 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001043 for (i = 0; i < cfg->speaker_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001044 snd_hda_set_pin_ctl(codec, cfg->speaker_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001045
1046 /* SPDIF is enabled on presence detect for CS421x */
1047 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001048 cs_automute(codec);
1049}
1050
1051static void init_input(struct hda_codec *codec)
1052{
1053 struct cs_spec *spec = codec->spec;
1054 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001055 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001056 int i;
1057
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001058 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001059 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001060 hda_nid_t pin = cfg->inputs[i].pin;
1061 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001062 continue;
1063 /* set appropriate pin control and mute first */
1064 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +02001065 if (cfg->inputs[i].type == AUTO_PIN_MIC)
1066 ctl |= snd_hda_get_default_vref(codec, pin);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001067 snd_hda_set_pin_ctl(codec, pin, ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001068 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1069 AC_VERB_SET_AMP_GAIN_MUTE,
1070 AMP_IN_MUTE(spec->adc_idx[i]));
1071 if (spec->mic_detect && spec->automic_idx == i)
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001072 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001073 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001074 /* CS420x has multiple ADC, CS421x has single ADC */
1075 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001076 change_cur_input(codec, spec->cur_input, 1);
1077 if (spec->mic_detect)
1078 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001079
Tim Howe56487c22011-07-22 16:41:00 -05001080 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1081 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1082 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1083 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1084 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1085 * No effect if SPDIF_OUT2 is
1086 * selected in IDX_SPDIF_CTL.
1087 */
1088 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001089 } else {
1090 if (spec->mic_detect)
1091 cs_automic(codec);
1092 else {
1093 spec->cur_adc = spec->adc_nid[spec->cur_input];
1094 cs_update_input_select(codec);
1095 }
Tim Howe56487c22011-07-22 16:41:00 -05001096 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001097}
1098
Takashi Iwaic42d4782011-05-02 11:36:09 +02001099static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001100 {0x11, AC_VERB_SET_PROC_STATE, 1},
1101 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1102 {0x11, AC_VERB_SET_PROC_COEF,
1103 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1104 | 0x0040 /* Mute DACs on FIFO error */
1105 | 0x1000 /* Enable DACs High Pass Filter */
1106 | 0x0400 /* Disable Coefficient Auto increment */
1107 )},
1108 /* Beep */
1109 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1110 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1111
1112 {} /* terminator */
1113};
1114
Brian Austina769cbc2010-09-07 14:36:22 -05001115/* Errata: CS4207 rev C0/C1/C2 Silicon
1116 *
1117 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1118 *
1119 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1120 * may be excessive (up to an additional 200 μA), which is most easily
1121 * observed while the part is being held in reset (RESET# active low).
1122 *
1123 * Root Cause: At initial powerup of the device, the logic that drives
1124 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1125 * initialized.
1126 * Certain random patterns will cause a steady leakage current in those
1127 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1128 *
1129 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1130 * blocks, which will alleviate the issue.
1131 */
1132
Takashi Iwaic42d4782011-05-02 11:36:09 +02001133static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001134 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1135 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1136
1137 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1138 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1139 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1140 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1141 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1142 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1143
1144 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1145 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1146
1147 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1148 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1149 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1150 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1151 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1152 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1153 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1154
Takashi Iwai38c07642011-03-03 14:54:19 +01001155#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001156 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1157 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1158 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001159#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001160
1161 {} /* terminator */
1162};
1163
Takashi Iwaief596a52012-09-11 16:53:08 +02001164static const struct hda_verb mbp101_init_verbs[] = {
1165 {0x11, AC_VERB_SET_COEF_INDEX, 0x0002},
1166 {0x11, AC_VERB_SET_PROC_COEF, 0x100a},
1167 {0x11, AC_VERB_SET_COEF_INDEX, 0x0004},
1168 {0x11, AC_VERB_SET_PROC_COEF, 0x000f},
1169 {}
1170};
1171
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001172/* SPDIF setup */
1173static void init_digital(struct hda_codec *codec)
1174{
1175 unsigned int coef;
1176
1177 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1178 coef |= 0x0008; /* Replace with mute on error */
1179 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1180 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1181 * SPDIF_OUT2 is shared with GPIO1 and
1182 * DMIC_SDA2.
1183 */
1184 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001185}
1186
1187static int cs_init(struct hda_codec *codec)
1188{
1189 struct cs_spec *spec = codec->spec;
1190
Brian Austina769cbc2010-09-07 14:36:22 -05001191 /* init_verb sequence for C0/C1/C2 errata*/
1192 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1193
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001194 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001195
1196 if (spec->gpio_mask) {
1197 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1198 spec->gpio_mask);
1199 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1200 spec->gpio_dir);
1201 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1202 spec->gpio_data);
1203 }
1204
Takashi Iwaie5f14242009-07-01 18:11:44 +02001205 init_output(codec);
1206 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001207 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001208
Takashi Iwaie5f14242009-07-01 18:11:44 +02001209 return 0;
1210}
1211
1212static int cs_build_controls(struct hda_codec *codec)
1213{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001214 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001215 int err;
1216
1217 err = build_output(codec);
1218 if (err < 0)
1219 return err;
1220 err = build_input(codec);
1221 if (err < 0)
1222 return err;
1223 err = build_digital_output(codec);
1224 if (err < 0)
1225 return err;
1226 err = build_digital_input(codec);
1227 if (err < 0)
1228 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001229 err = cs_init(codec);
1230 if (err < 0)
1231 return err;
1232
1233 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1234 if (err < 0)
1235 return err;
1236
1237 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001238}
1239
1240static void cs_free(struct hda_codec *codec)
1241{
1242 struct cs_spec *spec = codec->spec;
1243 kfree(spec->capture_bind[0]);
1244 kfree(spec->capture_bind[1]);
1245 kfree(codec->spec);
1246}
1247
1248static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1249{
Takashi Iwai3a938972011-10-28 01:16:55 +02001250 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001251 case HP_EVENT:
1252 cs_automute(codec);
1253 break;
1254 case MIC_EVENT:
1255 cs_automic(codec);
1256 break;
1257 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001258 snd_hda_jack_report_sync(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001259}
1260
Takashi Iwaic42d4782011-05-02 11:36:09 +02001261static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001262 .build_controls = cs_build_controls,
1263 .build_pcms = cs_build_pcms,
1264 .init = cs_init,
1265 .free = cs_free,
1266 .unsol_event = cs_unsol_event,
1267};
1268
1269static int cs_parse_auto_config(struct hda_codec *codec)
1270{
1271 struct cs_spec *spec = codec->spec;
1272 int err;
1273
1274 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1275 if (err < 0)
1276 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001277
1278 err = parse_output(codec);
1279 if (err < 0)
1280 return err;
1281 err = parse_input(codec);
1282 if (err < 0)
1283 return err;
1284 err = parse_digital_output(codec);
1285 if (err < 0)
1286 return err;
1287 err = parse_digital_input(codec);
1288 if (err < 0)
1289 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001290 return 0;
1291}
1292
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001293static const struct hda_model_fixup cs420x_models[] = {
1294 { .id = CS420X_MBP53, .name = "mbp53" },
1295 { .id = CS420X_MBP55, .name = "mbp55" },
1296 { .id = CS420X_IMAC27, .name = "imac27" },
1297 { .id = CS420X_IMAC27_122, .name = "imac27_122" },
1298 { .id = CS420X_APPLE, .name = "apple" },
Takashi Iwaief596a52012-09-11 16:53:08 +02001299 { .id = CS420X_MBP101, .name = "mbp101" },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001300 {}
Takashi Iwaia6bae202009-07-06 15:15:22 +02001301};
1302
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001303static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001304 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001305 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001306 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001307 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001308 /* this conflicts with too many other models */
1309 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001310
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001311 /* codec SSID */
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001312 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwaief596a52012-09-11 16:53:08 +02001313 SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001314 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001315 {} /* terminator */
1316};
1317
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001318static const struct hda_pintbl mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001319 { 0x09, 0x012b4050 },
1320 { 0x0a, 0x90100141 },
1321 { 0x0b, 0x90100140 },
1322 { 0x0c, 0x018b3020 },
1323 { 0x0d, 0x90a00110 },
1324 { 0x0e, 0x400000f0 },
1325 { 0x0f, 0x01cbe030 },
1326 { 0x10, 0x014be060 },
1327 { 0x12, 0x400000f0 },
1328 { 0x15, 0x400000f0 },
1329 {} /* terminator */
1330};
1331
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001332static const struct hda_pintbl mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001333 { 0x09, 0x012b4030 },
1334 { 0x0a, 0x90100121 },
1335 { 0x0b, 0x90100120 },
1336 { 0x0c, 0x400000f0 },
1337 { 0x0d, 0x90a00110 },
1338 { 0x0e, 0x400000f0 },
1339 { 0x0f, 0x400000f0 },
1340 { 0x10, 0x014be040 },
1341 { 0x12, 0x400000f0 },
1342 { 0x15, 0x400000f0 },
1343 {} /* terminator */
1344};
1345
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001346static const struct hda_pintbl imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001347 { 0x09, 0x012b4050 },
1348 { 0x0a, 0x90100140 },
1349 { 0x0b, 0x90100142 },
1350 { 0x0c, 0x018b3020 },
1351 { 0x0d, 0x90a00110 },
1352 { 0x0e, 0x400000f0 },
1353 { 0x0f, 0x01cbe030 },
1354 { 0x10, 0x014be060 },
1355 { 0x12, 0x01ab9070 },
1356 { 0x15, 0x400000f0 },
1357 {} /* terminator */
1358};
1359
Takashi Iwaief596a52012-09-11 16:53:08 +02001360static const struct hda_pintbl mbp101_pincfgs[] = {
1361 { 0x0d, 0x40ab90f0 },
1362 { 0x0e, 0x90a600f0 },
1363 { 0x12, 0x50a600f0 },
1364 {} /* terminator */
1365};
1366
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001367static void cs420x_fixup_gpio_13(struct hda_codec *codec,
1368 const struct hda_fixup *fix, int action)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001369{
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001370 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1371 struct cs_spec *spec = codec->spec;
1372 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1373 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1374 spec->gpio_mask = spec->gpio_dir =
1375 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1376 }
Takashi Iwaia6bae202009-07-06 15:15:22 +02001377}
1378
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001379static void cs420x_fixup_gpio_23(struct hda_codec *codec,
1380 const struct hda_fixup *fix, int action)
1381{
1382 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1383 struct cs_spec *spec = codec->spec;
1384 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1385 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1386 spec->gpio_mask = spec->gpio_dir =
1387 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1388 }
1389}
1390
1391static const struct hda_fixup cs420x_fixups[] = {
1392 [CS420X_MBP53] = {
1393 .type = HDA_FIXUP_PINS,
1394 .v.pins = mbp53_pincfgs,
1395 .chained = true,
1396 .chain_id = CS420X_APPLE,
1397 },
1398 [CS420X_MBP55] = {
1399 .type = HDA_FIXUP_PINS,
1400 .v.pins = mbp55_pincfgs,
1401 .chained = true,
1402 .chain_id = CS420X_GPIO_13,
1403 },
1404 [CS420X_IMAC27] = {
1405 .type = HDA_FIXUP_PINS,
1406 .v.pins = imac27_pincfgs,
1407 .chained = true,
1408 .chain_id = CS420X_GPIO_13,
1409 },
1410 [CS420X_GPIO_13] = {
1411 .type = HDA_FIXUP_FUNC,
1412 .v.func = cs420x_fixup_gpio_13,
1413 },
1414 [CS420X_GPIO_23] = {
1415 .type = HDA_FIXUP_FUNC,
1416 .v.func = cs420x_fixup_gpio_23,
1417 },
Takashi Iwaief596a52012-09-11 16:53:08 +02001418 [CS420X_MBP101] = {
1419 .type = HDA_FIXUP_PINS,
1420 .v.pins = mbp101_pincfgs,
1421 .chained = true,
1422 .chain_id = CS420X_MBP101_COEF,
1423 },
1424 [CS420X_MBP101_COEF] = {
1425 .type = HDA_FIXUP_VERBS,
1426 .v.verbs = mbp101_init_verbs,
1427 .chained = true,
1428 .chain_id = CS420X_GPIO_13,
1429 },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001430};
1431
Takashi Iwaie5f14242009-07-01 18:11:44 +02001432static int patch_cs420x(struct hda_codec *codec)
1433{
1434 struct cs_spec *spec;
1435 int err;
1436
1437 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1438 if (!spec)
1439 return -ENOMEM;
1440 codec->spec = spec;
1441
Tim Howe56487c22011-07-22 16:41:00 -05001442 spec->vendor_nid = CS420X_VENDOR_NID;
1443
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001444 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
1445 cs420x_fixups);
1446 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001447
Takashi Iwaied208252009-07-07 09:04:26 +02001448 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001449 if (err < 0)
1450 goto error;
1451
Takashi Iwaie5f14242009-07-01 18:11:44 +02001452 codec->patch_ops = cs_patch_ops;
1453
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001454 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1455
Takashi Iwaie5f14242009-07-01 18:11:44 +02001456 return 0;
1457
1458 error:
1459 kfree(codec->spec);
1460 codec->spec = NULL;
1461 return err;
1462}
1463
Tim Howe56487c22011-07-22 16:41:00 -05001464/*
1465 * Cirrus Logic CS4210
1466 *
1467 * 1 DAC => HP(sense) / Speakers,
1468 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1469 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1470*/
1471
1472/* CS4210 board names */
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001473static const struct hda_model_fixup cs421x_models[] = {
1474 { .id = CS421X_CDB4210, .name = "cdb4210" },
1475 {}
Tim Howe56487c22011-07-22 16:41:00 -05001476};
1477
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001478static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001479 /* Test Intel board + CDB2410 */
1480 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1481 {} /* terminator */
1482};
1483
1484/* CS4210 board pinconfigs */
1485/* Default CS4210 (CDB4210)*/
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001486static const struct hda_pintbl cdb4210_pincfgs[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001487 { 0x05, 0x0321401f },
1488 { 0x06, 0x90170010 },
1489 { 0x07, 0x03813031 },
1490 { 0x08, 0xb7a70037 },
1491 { 0x09, 0xb7a6003e },
1492 { 0x0a, 0x034510f0 },
1493 {} /* terminator */
1494};
1495
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001496/* Setup GPIO/SENSE for each board (if used) */
1497static void cs421x_fixup_sense_b(struct hda_codec *codec,
1498 const struct hda_fixup *fix, int action)
1499{
1500 struct cs_spec *spec = codec->spec;
1501 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1502 spec->sense_b = 1;
1503}
1504
1505static const struct hda_fixup cs421x_fixups[] = {
1506 [CS421X_CDB4210] = {
1507 .type = HDA_FIXUP_PINS,
1508 .v.pins = cdb4210_pincfgs,
1509 .chained = true,
1510 .chain_id = CS421X_SENSE_B,
1511 },
1512 [CS421X_SENSE_B] = {
1513 .type = HDA_FIXUP_FUNC,
1514 .v.func = cs421x_fixup_sense_b,
1515 }
Tim Howe56487c22011-07-22 16:41:00 -05001516};
1517
1518static const struct hda_verb cs421x_coef_init_verbs[] = {
1519 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1520 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1521 /*
1522 Disable Coefficient Index Auto-Increment(DAI)=1,
1523 PDREF=0
1524 */
1525 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1526
1527 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1528 /* ADC SZCMode = Digital Soft Ramp */
1529 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1530
1531 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1532 {0x0B, AC_VERB_SET_PROC_COEF,
1533 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1534 | 0x0004 /* Mute DAC on FIFO error */
1535 | 0x0008 /* Enable DAC High Pass Filter */
1536 )},
1537 {} /* terminator */
1538};
1539
1540/* Errata: CS4210 rev A1 Silicon
1541 *
1542 * http://www.cirrus.com/en/pubs/errata/
1543 *
1544 * Description:
1545 * 1. Performance degredation is present in the ADC.
1546 * 2. Speaker output is not completely muted upon HP detect.
1547 * 3. Noise is present when clipping occurs on the amplified
1548 * speaker outputs.
1549 *
1550 * Workaround:
1551 * The following verb sequence written to the registers during
1552 * initialization will correct the issues listed above.
1553 */
1554
1555static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1556 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1557
1558 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1559 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1560
1561 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1562 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1563
1564 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1565 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1566
1567 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1568 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1569
1570 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1571 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1572
1573 {} /* terminator */
1574};
1575
1576/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1577static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1578
1579static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1580 struct snd_ctl_elem_info *uinfo)
1581{
1582 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1583 uinfo->count = 1;
1584 uinfo->value.integer.min = 0;
1585 uinfo->value.integer.max = 3;
1586 return 0;
1587}
1588
1589static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1590 struct snd_ctl_elem_value *ucontrol)
1591{
1592 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1593
1594 ucontrol->value.integer.value[0] =
1595 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1596 return 0;
1597}
1598
1599static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1600 struct snd_ctl_elem_value *ucontrol)
1601{
1602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1603
1604 unsigned int vol = ucontrol->value.integer.value[0];
1605 unsigned int coef =
1606 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1607 unsigned int original_coef = coef;
1608
1609 coef &= ~0x0003;
1610 coef |= (vol & 0x0003);
1611 if (original_coef == coef)
1612 return 0;
1613 else {
1614 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1615 return 1;
1616 }
1617}
1618
1619static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1620
1621 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1622 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1623 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1624 .name = "Speaker Boost Playback Volume",
1625 .info = cs421x_boost_vol_info,
1626 .get = cs421x_boost_vol_get,
1627 .put = cs421x_boost_vol_put,
1628 .tlv = { .p = cs421x_speaker_boost_db_scale },
1629};
1630
David Henningsson5660ffd2012-01-02 12:40:17 +01001631static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001632{
1633 struct cs_spec *spec = codec->spec;
1634 unsigned int def_conf, coef;
1635
1636 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1637 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1638
1639 if (spec->gpio_mask)
1640 coef |= 0x0008; /* B1,B2 are GPIOs */
1641 else
1642 coef &= ~0x0008;
1643
1644 if (spec->sense_b)
1645 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1646 else
1647 coef &= ~0x0010;
1648
1649 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1650
1651 if ((spec->gpio_mask || spec->sense_b) &&
1652 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1653
1654 /*
1655 GPIO or SENSE_B forced - disconnect the DMIC pin.
1656 */
1657 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1658 def_conf &= ~AC_DEFCFG_PORT_CONN;
1659 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1660 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1661 }
1662}
1663
1664static void init_cs421x_digital(struct hda_codec *codec)
1665{
1666 struct cs_spec *spec = codec->spec;
1667 struct auto_pin_cfg *cfg = &spec->autocfg;
1668 int i;
1669
1670
1671 for (i = 0; i < cfg->dig_outs; i++) {
1672 hda_nid_t nid = cfg->dig_out_pins[i];
1673 if (!cfg->speaker_outs)
1674 continue;
1675 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001676 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
Tim Howe56487c22011-07-22 16:41:00 -05001677 spec->spdif_detect = 1;
1678 }
1679 }
1680}
1681
1682static int cs421x_init(struct hda_codec *codec)
1683{
1684 struct cs_spec *spec = codec->spec;
1685
David Henningsson5660ffd2012-01-02 12:40:17 +01001686 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1687 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1688 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1689 cs4210_pinmux_init(codec);
1690 }
Tim Howe56487c22011-07-22 16:41:00 -05001691
1692 if (spec->gpio_mask) {
1693 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1694 spec->gpio_mask);
1695 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1696 spec->gpio_dir);
1697 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1698 spec->gpio_data);
1699 }
1700
1701 init_output(codec);
1702 init_input(codec);
1703 init_cs421x_digital(codec);
1704
1705 return 0;
1706}
1707
1708/*
1709 * CS4210 Input MUX (1 ADC)
1710 */
1711static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_info *uinfo)
1713{
1714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1715 struct cs_spec *spec = codec->spec;
1716
1717 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1718}
1719
1720static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
1722{
1723 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1724 struct cs_spec *spec = codec->spec;
1725
1726 ucontrol->value.enumerated.item[0] = spec->cur_input;
1727 return 0;
1728}
1729
1730static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
1732{
1733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1734 struct cs_spec *spec = codec->spec;
1735
1736 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1737 spec->adc_nid[0], &spec->cur_input);
1738
1739}
1740
1741static struct snd_kcontrol_new cs421x_capture_source = {
1742
1743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1744 .name = "Capture Source",
1745 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1746 .info = cs421x_mux_enum_info,
1747 .get = cs421x_mux_enum_get,
1748 .put = cs421x_mux_enum_put,
1749};
1750
1751static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1752{
1753 struct cs_spec *spec = codec->spec;
1754 struct auto_pin_cfg *cfg = &spec->autocfg;
1755 const struct hda_input_mux *imux = &spec->input_mux;
1756 hda_nid_t pin = cfg->inputs[item].pin;
1757 struct snd_kcontrol *kctl;
1758 u32 caps;
1759
1760 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1761 return 0;
1762
1763 caps = query_amp_caps(codec, pin, HDA_INPUT);
1764 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1765 if (caps <= 1)
1766 return 0;
1767
1768 return add_volume(codec, imux->items[item].label, 0,
1769 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1770}
1771
1772/* add a (input-boost) volume control to the given input pin */
1773static int build_cs421x_input(struct hda_codec *codec)
1774{
1775 struct cs_spec *spec = codec->spec;
1776 struct auto_pin_cfg *cfg = &spec->autocfg;
1777 struct hda_input_mux *imux = &spec->input_mux;
1778 int i, err, type_idx;
1779 const char *label;
1780
1781 if (!spec->num_inputs)
1782 return 0;
1783
1784 /* make bind-capture */
1785 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1786 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1787 for (i = 0; i < 2; i++) {
1788 struct snd_kcontrol *kctl;
1789 int n;
1790 if (!spec->capture_bind[i])
1791 return -ENOMEM;
1792 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1793 if (!kctl)
1794 return -ENOMEM;
1795 kctl->private_value = (long)spec->capture_bind[i];
1796 err = snd_hda_ctl_add(codec, 0, kctl);
1797 if (err < 0)
1798 return err;
1799 for (n = 0; n < AUTO_PIN_LAST; n++) {
1800 if (!spec->adc_nid[n])
1801 continue;
1802 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1803 if (err < 0)
1804 return err;
1805 }
1806 }
1807
1808 /* Add Input MUX Items + Capture Volume/Switch */
1809 for (i = 0; i < spec->num_inputs; i++) {
1810 label = hda_get_autocfg_input_label(codec, cfg, i);
1811 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1812
1813 err = cs421x_add_input_volume_control(codec, i);
1814 if (err < 0)
1815 return err;
1816 }
1817
1818 /*
1819 Add 'Capture Source' Switch if
1820 * 2 inputs and no mic detec
1821 * 3 inputs
1822 */
1823 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1824 (spec->num_inputs == 3)) {
1825
1826 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1827 snd_ctl_new1(&cs421x_capture_source, codec));
1828 if (err < 0)
1829 return err;
1830 }
1831
1832 return 0;
1833}
1834
1835/* Single DAC (Mute/Gain) */
1836static int build_cs421x_output(struct hda_codec *codec)
1837{
1838 hda_nid_t dac = CS4210_DAC_NID;
1839 struct cs_spec *spec = codec->spec;
1840 struct auto_pin_cfg *cfg = &spec->autocfg;
1841 struct snd_kcontrol *kctl;
1842 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001843 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001844
1845 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001846
1847 err = add_mute(codec, name, 0,
1848 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1849 if (err < 0)
1850 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001851
1852 err = add_volume(codec, name, 0,
1853 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1854 if (err < 0)
1855 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001856
David Henningsson5660ffd2012-01-02 12:40:17 +01001857 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001858 err = snd_hda_ctl_add(codec, 0,
1859 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1860 if (err < 0)
1861 return err;
1862 }
1863 return err;
1864}
1865
1866static int cs421x_build_controls(struct hda_codec *codec)
1867{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001868 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001869 int err;
1870
1871 err = build_cs421x_output(codec);
1872 if (err < 0)
1873 return err;
1874 err = build_cs421x_input(codec);
1875 if (err < 0)
1876 return err;
1877 err = build_digital_output(codec);
1878 if (err < 0)
1879 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001880 err = cs421x_init(codec);
1881 if (err < 0)
1882 return err;
1883
1884 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1885 if (err < 0)
1886 return err;
1887
1888 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001889}
1890
1891static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1892{
Takashi Iwai3a938972011-10-28 01:16:55 +02001893 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Tim Howe56487c22011-07-22 16:41:00 -05001894 case HP_EVENT:
1895 case SPDIF_EVENT:
1896 cs_automute(codec);
1897 break;
1898
1899 case MIC_EVENT:
1900 cs_automic(codec);
1901 break;
1902 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001903 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001904}
1905
1906static int parse_cs421x_input(struct hda_codec *codec)
1907{
1908 struct cs_spec *spec = codec->spec;
1909 struct auto_pin_cfg *cfg = &spec->autocfg;
1910 int i;
1911
1912 for (i = 0; i < cfg->num_inputs; i++) {
1913 hda_nid_t pin = cfg->inputs[i].pin;
1914 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1915 spec->cur_input = spec->last_input = i;
1916 spec->num_inputs++;
1917
1918 /* check whether the automatic mic switch is available */
1919 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1920 spec->mic_detect = 1;
1921 spec->automic_idx = i;
1922 }
1923 }
1924 return 0;
1925}
1926
1927static int cs421x_parse_auto_config(struct hda_codec *codec)
1928{
1929 struct cs_spec *spec = codec->spec;
1930 int err;
1931
1932 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1933 if (err < 0)
1934 return err;
1935 err = parse_output(codec);
1936 if (err < 0)
1937 return err;
1938 err = parse_cs421x_input(codec);
1939 if (err < 0)
1940 return err;
1941 err = parse_digital_output(codec);
1942 if (err < 0)
1943 return err;
1944 return 0;
1945}
1946
1947#ifdef CONFIG_PM
1948/*
1949 Manage PDREF, when transitioning to D3hot
1950 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1951*/
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001952static int cs421x_suspend(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001953{
David Henningsson5660ffd2012-01-02 12:40:17 +01001954 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001955 unsigned int coef;
1956
1957 snd_hda_shutup_pins(codec);
1958
1959 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1960 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1961 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1962 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1963
David Henningsson5660ffd2012-01-02 12:40:17 +01001964 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1965 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1966 coef |= 0x0004; /* PDREF */
1967 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1968 }
Tim Howe56487c22011-07-22 16:41:00 -05001969
1970 return 0;
1971}
1972#endif
1973
David Henningsson5660ffd2012-01-02 12:40:17 +01001974static struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001975 .build_controls = cs421x_build_controls,
1976 .build_pcms = cs_build_pcms,
1977 .init = cs421x_init,
1978 .free = cs_free,
1979 .unsol_event = cs421x_unsol_event,
1980#ifdef CONFIG_PM
1981 .suspend = cs421x_suspend,
1982#endif
1983};
1984
David Henningsson5660ffd2012-01-02 12:40:17 +01001985static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001986{
1987 struct cs_spec *spec;
1988 int err;
1989
1990 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1991 if (!spec)
1992 return -ENOMEM;
1993 codec->spec = spec;
1994
David Henningsson5660ffd2012-01-02 12:40:17 +01001995 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001996
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001997 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1998 cs421x_fixups);
1999 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Tim Howe56487c22011-07-22 16:41:00 -05002000
2001 /*
2002 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
2003 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
2004 is disabled.
2005 */
David Henningsson5660ffd2012-01-02 12:40:17 +01002006 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05002007
2008 err = cs421x_parse_auto_config(codec);
2009 if (err < 0)
2010 goto error;
2011
David Henningsson5660ffd2012-01-02 12:40:17 +01002012 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05002013
Takashi Iwaib35aabd2012-08-24 19:09:45 +02002014 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
2015
Tim Howe56487c22011-07-22 16:41:00 -05002016 return 0;
2017
2018 error:
2019 kfree(codec->spec);
2020 codec->spec = NULL;
2021 return err;
2022}
2023
David Henningsson5660ffd2012-01-02 12:40:17 +01002024static int patch_cs4213(struct hda_codec *codec)
2025{
2026 struct cs_spec *spec;
2027 int err;
2028
2029 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2030 if (!spec)
2031 return -ENOMEM;
2032 codec->spec = spec;
2033
2034 spec->vendor_nid = CS4213_VENDOR_NID;
2035
2036 err = cs421x_parse_auto_config(codec);
2037 if (err < 0)
2038 goto error;
2039
2040 codec->patch_ops = cs421x_patch_ops;
2041 return 0;
2042
2043 error:
2044 kfree(codec->spec);
2045 codec->spec = NULL;
2046 return err;
2047}
2048
Takashi Iwaie5f14242009-07-01 18:11:44 +02002049
2050/*
2051 * patch entries
2052 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002053static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002054 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2055 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002056 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2057 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002058 {} /* terminator */
2059};
2060
2061MODULE_ALIAS("snd-hda-codec-id:10134206");
2062MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002063MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002064MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002065
2066MODULE_LICENSE("GPL");
2067MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2068
2069static struct hda_codec_preset_list cirrus_list = {
2070 .preset = snd_hda_preset_cirrus,
2071 .owner = THIS_MODULE,
2072};
2073
2074static int __init patch_cirrus_init(void)
2075{
2076 return snd_hda_add_codec_preset(&cirrus_list);
2077}
2078
2079static void __exit patch_cirrus_exit(void)
2080{
2081 snd_hda_delete_codec_preset(&cirrus_list);
2082}
2083
2084module_init(patch_cirrus_init)
2085module_exit(patch_cirrus_exit)