blob: 6aca4a05a329108604d9f8a557825d6d0d7c85ed [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 Iwaia6bae202009-07-06 15:15:22 +020088 CS420X_AUTO,
Takashi Iwaia6bae202009-07-06 15:15:22 +020089};
90
Tim Howe56487c22011-07-22 16:41:00 -050091/* CS421x boards */
92enum {
93 CS421X_CDB4210,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020094 CS421X_SENSE_B,
Tim Howe56487c22011-07-22 16:41:00 -050095};
96
Takashi Iwai40c20fa2009-07-06 13:00:57 +020097/* Vendor-specific processing widget */
98#define CS420X_VENDOR_NID 0x11
99#define CS_DIG_OUT1_PIN_NID 0x10
100#define CS_DIG_OUT2_PIN_NID 0x15
101#define CS_DMIC1_PIN_NID 0x12
102#define CS_DMIC2_PIN_NID 0x0e
103
104/* coef indices */
105#define IDX_SPDIF_STAT 0x0000
106#define IDX_SPDIF_CTL 0x0001
107#define IDX_ADC_CFG 0x0002
108/* SZC bitmask, 4 modes below:
109 * 0 = immediate,
110 * 1 = digital immediate, analog zero-cross
111 * 2 = digtail & analog soft-ramp
112 * 3 = digital soft-ramp, analog zero-cross
113 */
114#define CS_COEF_ADC_SZC_MASK (3 << 0)
115#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
116#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
117/* PGA mode: 0 = differential, 1 = signle-ended */
118#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
119#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
120#define IDX_DAC_CFG 0x0003
121/* SZC bitmask, 4 modes below:
122 * 0 = Immediate
123 * 1 = zero-cross
124 * 2 = soft-ramp
125 * 3 = soft-ramp on zero-cross
126 */
127#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
128#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
129#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
130
131#define IDX_BEEP_CFG 0x0004
132/* 0x0008 - test reg key */
133/* 0x0009 - 0x0014 -> 12 test regs */
134/* 0x0015 - visibility reg */
135
Tim Howe56487c22011-07-22 16:41:00 -0500136/*
137 * Cirrus Logic CS4210
138 *
139 * 1 DAC => HP(sense) / Speakers,
140 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
141 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
142*/
143#define CS4210_DAC_NID 0x02
144#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100145#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500146#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
147#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
148
149#define CS421X_IDX_DEV_CFG 0x01
150#define CS421X_IDX_ADC_CFG 0x02
151#define CS421X_IDX_DAC_CFG 0x03
152#define CS421X_IDX_SPK_CTL 0x04
153
154#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200155
David Henningsson5660ffd2012-01-02 12:40:17 +0100156/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
157#define CS4213_VENDOR_NID 0x09
158
159
Takashi Iwai277a57c2009-07-08 12:42:08 +0200160static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200161{
Tim Howe56487c22011-07-22 16:41:00 -0500162 struct cs_spec *spec = codec->spec;
163 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200164 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500165 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200166 AC_VERB_GET_PROC_COEF, 0);
167}
168
Takashi Iwai277a57c2009-07-08 12:42:08 +0200169static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
170 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200171{
Tim Howe56487c22011-07-22 16:41:00 -0500172 struct cs_spec *spec = codec->spec;
173 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200174 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500175 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200176 AC_VERB_SET_PROC_COEF, coef);
177}
178
179
Takashi Iwaie5f14242009-07-01 18:11:44 +0200180#define HP_EVENT 1
181#define MIC_EVENT 2
182
183/*
184 * PCM callbacks
185 */
186static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
187 struct hda_codec *codec,
188 struct snd_pcm_substream *substream)
189{
190 struct cs_spec *spec = codec->spec;
191 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
192 hinfo);
193}
194
195static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
196 struct hda_codec *codec,
197 unsigned int stream_tag,
198 unsigned int format,
199 struct snd_pcm_substream *substream)
200{
201 struct cs_spec *spec = codec->spec;
202 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
203 stream_tag, format, substream);
204}
205
206static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
207 struct hda_codec *codec,
208 struct snd_pcm_substream *substream)
209{
210 struct cs_spec *spec = codec->spec;
211 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
212}
213
214/*
215 * Digital out
216 */
217static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
218 struct hda_codec *codec,
219 struct snd_pcm_substream *substream)
220{
221 struct cs_spec *spec = codec->spec;
222 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
223}
224
225static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
226 struct hda_codec *codec,
227 struct snd_pcm_substream *substream)
228{
229 struct cs_spec *spec = codec->spec;
230 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
231}
232
233static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
234 struct hda_codec *codec,
235 unsigned int stream_tag,
236 unsigned int format,
237 struct snd_pcm_substream *substream)
238{
239 struct cs_spec *spec = codec->spec;
240 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
241 format, substream);
242}
243
244static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
245 struct hda_codec *codec,
246 struct snd_pcm_substream *substream)
247{
248 struct cs_spec *spec = codec->spec;
249 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
250}
251
Takashi Iwai05ee7962011-11-16 18:05:11 +0100252static void cs_update_input_select(struct hda_codec *codec)
253{
254 struct cs_spec *spec = codec->spec;
255 if (spec->cur_adc)
256 snd_hda_codec_write(codec, spec->cur_adc, 0,
257 AC_VERB_SET_CONNECT_SEL,
258 spec->adc_idx[spec->cur_input]);
259}
260
Takashi Iwaie5f14242009-07-01 18:11:44 +0200261/*
262 * Analog capture
263 */
264static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
265 struct hda_codec *codec,
266 unsigned int stream_tag,
267 unsigned int format,
268 struct snd_pcm_substream *substream)
269{
270 struct cs_spec *spec = codec->spec;
271 spec->cur_adc = spec->adc_nid[spec->cur_input];
272 spec->cur_adc_stream_tag = stream_tag;
273 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100274 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200275 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
276 return 0;
277}
278
279static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
280 struct hda_codec *codec,
281 struct snd_pcm_substream *substream)
282{
283 struct cs_spec *spec = codec->spec;
284 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
285 spec->cur_adc = 0;
286 return 0;
287}
288
289/*
290 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200291static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200292 .substreams = 1,
293 .channels_min = 2,
294 .channels_max = 2,
295 .ops = {
296 .open = cs_playback_pcm_open,
297 .prepare = cs_playback_pcm_prepare,
298 .cleanup = cs_playback_pcm_cleanup
299 },
300};
301
Takashi Iwaic42d4782011-05-02 11:36:09 +0200302static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200303 .substreams = 1,
304 .channels_min = 2,
305 .channels_max = 2,
306 .ops = {
307 .prepare = cs_capture_pcm_prepare,
308 .cleanup = cs_capture_pcm_cleanup
309 },
310};
311
Takashi Iwaic42d4782011-05-02 11:36:09 +0200312static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200313 .substreams = 1,
314 .channels_min = 2,
315 .channels_max = 2,
316 .ops = {
317 .open = cs_dig_playback_pcm_open,
318 .close = cs_dig_playback_pcm_close,
319 .prepare = cs_dig_playback_pcm_prepare,
320 .cleanup = cs_dig_playback_pcm_cleanup
321 },
322};
323
Takashi Iwaic42d4782011-05-02 11:36:09 +0200324static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200325 .substreams = 1,
326 .channels_min = 2,
327 .channels_max = 2,
328};
329
330static int cs_build_pcms(struct hda_codec *codec)
331{
332 struct cs_spec *spec = codec->spec;
333 struct hda_pcm *info = spec->pcm_rec;
334
335 codec->pcm_info = info;
336 codec->num_pcms = 0;
337
338 info->name = "Cirrus Analog";
339 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
340 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
341 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
342 spec->multiout.max_channels;
343 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200344 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
345 spec->adc_nid[spec->cur_input];
346 codec->num_pcms++;
347
348 if (!spec->multiout.dig_out_nid && !spec->dig_in)
349 return 0;
350
351 info++;
352 info->name = "Cirrus Digital";
353 info->pcm_type = spec->autocfg.dig_out_type[0];
354 if (!info->pcm_type)
355 info->pcm_type = HDA_PCM_TYPE_SPDIF;
356 if (spec->multiout.dig_out_nid) {
357 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
358 cs_pcm_digital_playback;
359 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
360 spec->multiout.dig_out_nid;
361 }
362 if (spec->dig_in) {
363 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
364 cs_pcm_digital_capture;
365 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
366 }
367 codec->num_pcms++;
368
369 return 0;
370}
371
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200372/*
373 * parse codec topology
374 */
375
Takashi Iwaie5f14242009-07-01 18:11:44 +0200376static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
377{
378 hda_nid_t dac;
379 if (!pin)
380 return 0;
381 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
382 return 0;
383 return dac;
384}
385
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200386static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
387{
388 struct cs_spec *spec = codec->spec;
389 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200390 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200391 unsigned int val;
392 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200393 return 0;
394 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200395 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200396}
397
398static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
399 unsigned int *idxp)
400{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200401 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200402 hda_nid_t nid;
403
404 nid = codec->start_nid;
405 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200406 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200407 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200408 if (type != AC_WID_AUD_IN)
409 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200410 idx = snd_hda_get_conn_index(codec, nid, pin, false);
411 if (idx >= 0) {
412 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200413 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200414 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200415 }
416 return 0;
417}
418
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200419static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
420{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200421 unsigned int val;
422 val = snd_hda_codec_get_pincfg(codec, nid);
423 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
424}
425
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200426static int parse_output(struct hda_codec *codec)
427{
428 struct cs_spec *spec = codec->spec;
429 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200430 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200431 hda_nid_t dac;
432
433 for (i = 0; i < cfg->line_outs; i++) {
434 dac = get_dac(codec, cfg->line_out_pins[i]);
435 if (!dac)
436 break;
437 spec->dac_nid[i] = dac;
438 }
439 spec->multiout.num_dacs = i;
440 spec->multiout.dac_nids = spec->dac_nid;
441 spec->multiout.max_channels = i * 2;
442
443 /* add HP and speakers */
444 extra_nids = 0;
445 for (i = 0; i < cfg->hp_outs; i++) {
446 dac = get_dac(codec, cfg->hp_pins[i]);
447 if (!dac)
448 break;
449 if (!i)
450 spec->multiout.hp_nid = dac;
451 else
452 spec->multiout.extra_out_nid[extra_nids++] = dac;
453 }
454 for (i = 0; i < cfg->speaker_outs; i++) {
455 dac = get_dac(codec, cfg->speaker_pins[i]);
456 if (!dac)
457 break;
458 spec->multiout.extra_out_nid[extra_nids++] = dac;
459 }
460
461 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
462 cfg->speaker_outs = cfg->line_outs;
463 memcpy(cfg->speaker_pins, cfg->line_out_pins,
464 sizeof(cfg->speaker_pins));
465 cfg->line_outs = 0;
466 }
467
468 return 0;
469}
470
471static int parse_input(struct hda_codec *codec)
472{
473 struct cs_spec *spec = codec->spec;
474 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200475 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200476
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200477 for (i = 0; i < cfg->num_inputs; i++) {
478 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200479 spec->input_idx[spec->num_inputs] = i;
480 spec->capsrc_idx[i] = spec->num_inputs++;
481 spec->cur_input = i;
482 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
483 }
484 if (!spec->num_inputs)
485 return 0;
486
487 /* check whether the automatic mic switch is available */
488 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200489 cfg->inputs[0].type == AUTO_PIN_MIC &&
490 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200491 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
492 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200493 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200494 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200495 }
496 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200497 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200498 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200499 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200500 }
501 }
502 }
503 return 0;
504}
505
506
507static int parse_digital_output(struct hda_codec *codec)
508{
509 struct cs_spec *spec = codec->spec;
510 struct auto_pin_cfg *cfg = &spec->autocfg;
511 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200512
513 if (!cfg->dig_outs)
514 return 0;
515 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
516 return 0;
517 spec->multiout.dig_out_nid = nid;
518 spec->multiout.share_spdif = 1;
519 if (cfg->dig_outs > 1 &&
520 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
521 spec->slave_dig_outs[0] = nid;
522 codec->slave_dig_outs = spec->slave_dig_outs;
523 }
524 return 0;
525}
526
527static int parse_digital_input(struct hda_codec *codec)
528{
529 struct cs_spec *spec = codec->spec;
530 struct auto_pin_cfg *cfg = &spec->autocfg;
531 int idx;
532
Takashi Iwai60e53882009-07-06 15:01:09 +0200533 if (cfg->dig_in_pin)
534 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
535 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200536}
537
538/*
539 * create mixer controls
540 */
541
Takashi Iwaiea734962011-01-17 11:29:34 +0100542static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200543
544static int add_mute(struct hda_codec *codec, const char *name, int index,
545 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
546{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200547 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200548 struct snd_kcontrol_new knew =
549 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
550 knew.private_value = pval;
551 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
552 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100553 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100554 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200555}
556
557static int add_volume(struct hda_codec *codec, const char *name,
558 int index, unsigned int pval, int dir,
559 struct snd_kcontrol **kctlp)
560{
David Henningsson2e1210b2011-09-14 13:22:54 +0200561 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200562 struct snd_kcontrol_new knew =
563 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
564 knew.private_value = pval;
565 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
566 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100567 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100568 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200569}
570
571static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
572{
573 unsigned int caps;
574
575 /* set the upper-limit for mixer amp to 0dB */
576 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
577 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
578 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
579 << AC_AMPCAP_NUM_STEPS_SHIFT;
580 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
581}
582
583static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
584{
585 struct cs_spec *spec = codec->spec;
586 unsigned int tlv[4];
587 int err;
588
589 spec->vmaster_sw =
590 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100591 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200592 if (err < 0)
593 return err;
594
595 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
596 spec->vmaster_vol =
597 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100598 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200599 if (err < 0)
600 return err;
601 return 0;
602}
603
604static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
605 int num_ctls, int type)
606{
607 struct cs_spec *spec = codec->spec;
608 const char *name;
609 int err, index;
610 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200611 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200612 "Front Speaker", "Surround Speaker", "Bass Speaker"
613 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200614 static const char * const line_outs[] = {
Takashi Iwaie49a3432012-03-01 18:14:41 +0100615 "Front Line Out", "Surround Line Out", "Bass Line Out"
Takashi Iwaie5f14242009-07-01 18:11:44 +0200616 };
617
618 fix_volume_caps(codec, dac);
619 if (!spec->vmaster_sw) {
620 err = add_vmaster(codec, dac);
621 if (err < 0)
622 return err;
623 }
624
625 index = 0;
626 switch (type) {
627 case AUTO_PIN_HP_OUT:
628 name = "Headphone";
629 index = idx;
630 break;
631 case AUTO_PIN_SPEAKER_OUT:
632 if (num_ctls > 1)
633 name = speakers[idx];
634 else
635 name = "Speaker";
636 break;
637 default:
638 if (num_ctls > 1)
639 name = line_outs[idx];
640 else
Takashi Iwaie49a3432012-03-01 18:14:41 +0100641 name = "Line Out";
Takashi Iwaie5f14242009-07-01 18:11:44 +0200642 break;
643 }
644
645 err = add_mute(codec, name, index,
646 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
647 if (err < 0)
648 return err;
649 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
650 if (err < 0)
651 return err;
652
653 err = add_volume(codec, name, index,
654 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
655 if (err < 0)
656 return err;
657 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
658 if (err < 0)
659 return err;
660
661 return 0;
662}
663
664static int build_output(struct hda_codec *codec)
665{
666 struct cs_spec *spec = codec->spec;
667 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200668 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200669
670 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200671 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
672 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200673 if (err < 0)
674 return err;
675 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200676 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200677 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
678 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200679 if (err < 0)
680 return err;
681 }
682 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200683 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
684 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200685 if (err < 0)
686 return err;
687 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200688 return 0;
689}
690
691/*
692 */
693
Takashi Iwaic42d4782011-05-02 11:36:09 +0200694static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200695 HDA_BIND_SW("Capture Switch", 0),
696 HDA_BIND_VOL("Capture Volume", 0),
697};
698
Takashi Iwaiea359292009-07-06 12:58:59 +0200699static int change_cur_input(struct hda_codec *codec, unsigned int idx,
700 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200701{
702 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200703
Takashi Iwaiea359292009-07-06 12:58:59 +0200704 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200705 return 0;
706 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
707 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200708 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200709 spec->cur_adc = spec->adc_nid[idx];
710 snd_hda_codec_setup_stream(codec, spec->cur_adc,
711 spec->cur_adc_stream_tag, 0,
712 spec->cur_adc_format);
713 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200714 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100715 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200716 return 1;
717}
718
719static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
720 struct snd_ctl_elem_info *uinfo)
721{
722 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
723 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200724 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200725 unsigned int idx;
726
727 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
728 uinfo->count = 1;
729 uinfo->value.enumerated.items = spec->num_inputs;
730 if (uinfo->value.enumerated.item >= spec->num_inputs)
731 uinfo->value.enumerated.item = spec->num_inputs - 1;
732 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100733 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
734 uinfo->value.enumerated.name,
735 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200736 return 0;
737}
738
739static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
740 struct snd_ctl_elem_value *ucontrol)
741{
742 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
743 struct cs_spec *spec = codec->spec;
744 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
745 return 0;
746}
747
748static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
749 struct snd_ctl_elem_value *ucontrol)
750{
751 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
752 struct cs_spec *spec = codec->spec;
753 unsigned int idx = ucontrol->value.enumerated.item[0];
754
755 if (idx >= spec->num_inputs)
756 return -EINVAL;
757 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200758 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200759}
760
Takashi Iwaic42d4782011-05-02 11:36:09 +0200761static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200762 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
763 .name = "Capture Source",
764 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
765 .info = cs_capture_source_info,
766 .get = cs_capture_source_get,
767 .put = cs_capture_source_put,
768};
769
Takashi Iwaic42d4782011-05-02 11:36:09 +0200770static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200771 struct hda_ctl_ops *ops)
772{
773 struct cs_spec *spec = codec->spec;
774 struct hda_bind_ctls *bind;
775 int i, n;
776
777 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
778 GFP_KERNEL);
779 if (!bind)
780 return NULL;
781 bind->ops = ops;
782 n = 0;
783 for (i = 0; i < AUTO_PIN_LAST; i++) {
784 if (!spec->adc_nid[i])
785 continue;
786 bind->values[n++] =
787 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
788 spec->adc_idx[i], HDA_INPUT);
789 }
790 return bind;
791}
792
Takashi Iwai6a929342010-10-11 15:16:20 +0200793/* add a (input-boost) volume control to the given input pin */
794static int add_input_volume_control(struct hda_codec *codec,
795 struct auto_pin_cfg *cfg,
796 int item)
797{
798 hda_nid_t pin = cfg->inputs[item].pin;
799 u32 caps;
800 const char *label;
801 struct snd_kcontrol *kctl;
802
803 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
804 return 0;
805 caps = query_amp_caps(codec, pin, HDA_INPUT);
806 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
807 if (caps <= 1)
808 return 0;
809 label = hda_get_autocfg_input_label(codec, cfg, item);
810 return add_volume(codec, label, 0,
811 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
812}
813
Takashi Iwaie5f14242009-07-01 18:11:44 +0200814static int build_input(struct hda_codec *codec)
815{
816 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200817 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200818
Takashi Iwaie5f14242009-07-01 18:11:44 +0200819 if (!spec->num_inputs)
820 return 0;
821
Takashi Iwaie5f14242009-07-01 18:11:44 +0200822 /* make bind-capture */
823 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
824 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
825 for (i = 0; i < 2; i++) {
826 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100827 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200828 if (!spec->capture_bind[i])
829 return -ENOMEM;
830 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
831 if (!kctl)
832 return -ENOMEM;
833 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100834 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200835 if (err < 0)
836 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100837 for (n = 0; n < AUTO_PIN_LAST; n++) {
838 if (!spec->adc_nid[n])
839 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700840 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100841 if (err < 0)
842 return err;
843 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200844 }
845
846 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100847 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200848 snd_ctl_new1(&cs_capture_source, codec));
849 if (err < 0)
850 return err;
851 }
852
Takashi Iwai6a929342010-10-11 15:16:20 +0200853 for (i = 0; i < spec->num_inputs; i++) {
854 err = add_input_volume_control(codec, &spec->autocfg, i);
855 if (err < 0)
856 return err;
857 }
858
Takashi Iwaie5f14242009-07-01 18:11:44 +0200859 return 0;
860}
861
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200862/*
863 */
864
Takashi Iwaie5f14242009-07-01 18:11:44 +0200865static int build_digital_output(struct hda_codec *codec)
866{
867 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200868 int err;
869
Takashi Iwai63b24132009-07-09 11:45:59 +0200870 if (!spec->multiout.dig_out_nid)
871 return 0;
872
Stephen Warren74b654c2011-06-01 11:14:18 -0600873 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
874 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200875 if (err < 0)
876 return err;
877 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
878 if (err < 0)
879 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200880 return 0;
881}
882
883static int build_digital_input(struct hda_codec *codec)
884{
885 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200886 if (spec->dig_in)
887 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
888 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200889}
890
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200891/*
892 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500893 * CS421x auto-output redirecting
894 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200895 */
896
Takashi Iwaie5f14242009-07-01 18:11:44 +0200897static void cs_automute(struct hda_codec *codec)
898{
899 struct cs_spec *spec = codec->spec;
900 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200901 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500902 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200903 hda_nid_t nid;
904 int i;
905
Tim Howe56487c22011-07-22 16:41:00 -0500906 spdif_present = 0;
907 if (cfg->dig_outs) {
908 nid = cfg->dig_out_pins[0];
909 if (is_jack_detectable(codec, nid)) {
910 /*
911 TODO: SPDIF output redirect when SENSE_B is enabled.
912 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
913 assumed.
914 */
915 if (snd_hda_jack_detect(codec, nid)
916 /* && spec->sense_b */)
917 spdif_present = 1;
918 }
919 }
920
Takashi Iwaie5f14242009-07-01 18:11:44 +0200921 hp_present = 0;
922 for (i = 0; i < cfg->hp_outs; i++) {
923 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200924 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200925 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800926 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200927 if (hp_present)
928 break;
929 }
Tim Howe56487c22011-07-22 16:41:00 -0500930
931 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200932 for (i = 0; i < cfg->speaker_outs; i++) {
David Henningsson78e2a922012-01-02 12:40:16 +0100933 int pin_ctl = hp_present ? 0 : PIN_OUT;
David Henningsson5660ffd2012-01-02 12:40:17 +0100934 /* detect on spdif is specific to CS4210 */
935 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
David Henningsson78e2a922012-01-02 12:40:16 +0100936 pin_ctl = 0;
937
Takashi Iwaie5f14242009-07-01 18:11:44 +0200938 nid = cfg->speaker_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200939 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200940 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100941 if (spec->gpio_eapd_hp) {
942 unsigned int gpio = hp_present ?
943 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200944 snd_hda_codec_write(codec, 0x01, 0,
945 AC_VERB_SET_GPIO_DATA, gpio);
946 }
Tim Howe56487c22011-07-22 16:41:00 -0500947
David Henningsson5660ffd2012-01-02 12:40:17 +0100948 /* specific to CS4210 */
949 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500950 /* mute HPs if spdif jack (SENSE_B) is present */
951 for (i = 0; i < cfg->hp_outs; i++) {
952 nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200953 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500954 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
955 }
956
957 /* SPDIF TX on/off */
958 if (cfg->dig_outs) {
959 nid = cfg->dig_out_pins[0];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200960 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500961 spdif_present ? PIN_OUT : 0);
962
963 }
964 /* Update board GPIOs if neccessary ... */
965 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200966}
967
Tim Howe56487c22011-07-22 16:41:00 -0500968/*
969 * Auto-input redirect for CS421x
970 * Switch max 3 inputs of a single ADC (nid 3)
971*/
972
Takashi Iwaie5f14242009-07-01 18:11:44 +0200973static void cs_automic(struct hda_codec *codec)
974{
975 struct cs_spec *spec = codec->spec;
976 struct auto_pin_cfg *cfg = &spec->autocfg;
977 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800978 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500979
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200980 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800981 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500982
983 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100984 if (spec->vendor_nid == CS420X_VENDOR_NID) {
985 if (present)
986 change_cur_input(codec, spec->automic_idx, 0);
987 else
988 change_cur_input(codec, !spec->automic_idx, 0);
989 } else {
Tim Howe56487c22011-07-22 16:41:00 -0500990 if (present) {
Dylan Reidf70eecd2012-01-31 13:04:41 -0800991 if (spec->cur_input != spec->automic_idx) {
992 spec->last_input = spec->cur_input;
993 spec->cur_input = spec->automic_idx;
994 }
Tim Howe56487c22011-07-22 16:41:00 -0500995 } else {
996 spec->cur_input = spec->last_input;
997 }
Takashi Iwai05ee7962011-11-16 18:05:11 +0100998 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -0500999 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001000}
1001
1002/*
1003 */
1004
1005static void init_output(struct hda_codec *codec)
1006{
1007 struct cs_spec *spec = codec->spec;
1008 struct auto_pin_cfg *cfg = &spec->autocfg;
1009 int i;
1010
1011 /* mute first */
1012 for (i = 0; i < spec->multiout.num_dacs; i++)
1013 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1014 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1015 if (spec->multiout.hp_nid)
1016 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1017 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1018 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1019 if (!spec->multiout.extra_out_nid[i])
1020 break;
1021 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1022 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1023 }
1024
1025 /* set appropriate pin controls */
1026 for (i = 0; i < cfg->line_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001027 snd_hda_set_pin_ctl(codec, cfg->line_out_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001028 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001029 for (i = 0; i < cfg->hp_outs; i++) {
1030 hda_nid_t nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001031 snd_hda_set_pin_ctl(codec, nid, PIN_HP);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001032 if (!cfg->speaker_outs)
1033 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001034 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001035 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001036 spec->hp_detect = 1;
1037 }
1038 }
Tim Howe56487c22011-07-22 16:41:00 -05001039
1040 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001041 for (i = 0; i < cfg->speaker_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001042 snd_hda_set_pin_ctl(codec, cfg->speaker_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001043
1044 /* SPDIF is enabled on presence detect for CS421x */
1045 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001046 cs_automute(codec);
1047}
1048
1049static void init_input(struct hda_codec *codec)
1050{
1051 struct cs_spec *spec = codec->spec;
1052 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001053 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001054 int i;
1055
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001056 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001057 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001058 hda_nid_t pin = cfg->inputs[i].pin;
1059 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001060 continue;
1061 /* set appropriate pin control and mute first */
1062 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +02001063 if (cfg->inputs[i].type == AUTO_PIN_MIC)
1064 ctl |= snd_hda_get_default_vref(codec, pin);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001065 snd_hda_set_pin_ctl(codec, pin, ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001066 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1067 AC_VERB_SET_AMP_GAIN_MUTE,
1068 AMP_IN_MUTE(spec->adc_idx[i]));
1069 if (spec->mic_detect && spec->automic_idx == i)
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001070 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001071 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001072 /* CS420x has multiple ADC, CS421x has single ADC */
1073 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001074 change_cur_input(codec, spec->cur_input, 1);
1075 if (spec->mic_detect)
1076 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001077
Tim Howe56487c22011-07-22 16:41:00 -05001078 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1079 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1080 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1081 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1082 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1083 * No effect if SPDIF_OUT2 is
1084 * selected in IDX_SPDIF_CTL.
1085 */
1086 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001087 } else {
1088 if (spec->mic_detect)
1089 cs_automic(codec);
1090 else {
1091 spec->cur_adc = spec->adc_nid[spec->cur_input];
1092 cs_update_input_select(codec);
1093 }
Tim Howe56487c22011-07-22 16:41:00 -05001094 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001095}
1096
Takashi Iwaic42d4782011-05-02 11:36:09 +02001097static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001098 {0x11, AC_VERB_SET_PROC_STATE, 1},
1099 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1100 {0x11, AC_VERB_SET_PROC_COEF,
1101 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1102 | 0x0040 /* Mute DACs on FIFO error */
1103 | 0x1000 /* Enable DACs High Pass Filter */
1104 | 0x0400 /* Disable Coefficient Auto increment */
1105 )},
1106 /* Beep */
1107 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1108 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1109
1110 {} /* terminator */
1111};
1112
Brian Austina769cbc2010-09-07 14:36:22 -05001113/* Errata: CS4207 rev C0/C1/C2 Silicon
1114 *
1115 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1116 *
1117 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1118 * may be excessive (up to an additional 200 μA), which is most easily
1119 * observed while the part is being held in reset (RESET# active low).
1120 *
1121 * Root Cause: At initial powerup of the device, the logic that drives
1122 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1123 * initialized.
1124 * Certain random patterns will cause a steady leakage current in those
1125 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1126 *
1127 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1128 * blocks, which will alleviate the issue.
1129 */
1130
Takashi Iwaic42d4782011-05-02 11:36:09 +02001131static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001132 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1133 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1134
1135 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1136 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1137 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1138 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1139 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1140 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1141
1142 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1143 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1144
1145 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1146 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1147 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1148 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1149 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1150 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1151 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1152
Takashi Iwai38c07642011-03-03 14:54:19 +01001153#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001154 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1155 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1156 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001157#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001158
1159 {} /* terminator */
1160};
1161
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001162/* SPDIF setup */
1163static void init_digital(struct hda_codec *codec)
1164{
1165 unsigned int coef;
1166
1167 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1168 coef |= 0x0008; /* Replace with mute on error */
1169 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1170 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1171 * SPDIF_OUT2 is shared with GPIO1 and
1172 * DMIC_SDA2.
1173 */
1174 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001175}
1176
1177static int cs_init(struct hda_codec *codec)
1178{
1179 struct cs_spec *spec = codec->spec;
1180
Brian Austina769cbc2010-09-07 14:36:22 -05001181 /* init_verb sequence for C0/C1/C2 errata*/
1182 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1183
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001184 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001185
1186 if (spec->gpio_mask) {
1187 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1188 spec->gpio_mask);
1189 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1190 spec->gpio_dir);
1191 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1192 spec->gpio_data);
1193 }
1194
Takashi Iwaie5f14242009-07-01 18:11:44 +02001195 init_output(codec);
1196 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001197 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001198
Takashi Iwaie5f14242009-07-01 18:11:44 +02001199 return 0;
1200}
1201
1202static int cs_build_controls(struct hda_codec *codec)
1203{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001204 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001205 int err;
1206
1207 err = build_output(codec);
1208 if (err < 0)
1209 return err;
1210 err = build_input(codec);
1211 if (err < 0)
1212 return err;
1213 err = build_digital_output(codec);
1214 if (err < 0)
1215 return err;
1216 err = build_digital_input(codec);
1217 if (err < 0)
1218 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001219 err = cs_init(codec);
1220 if (err < 0)
1221 return err;
1222
1223 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1224 if (err < 0)
1225 return err;
1226
1227 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001228}
1229
1230static void cs_free(struct hda_codec *codec)
1231{
1232 struct cs_spec *spec = codec->spec;
1233 kfree(spec->capture_bind[0]);
1234 kfree(spec->capture_bind[1]);
1235 kfree(codec->spec);
1236}
1237
1238static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1239{
Takashi Iwai3a938972011-10-28 01:16:55 +02001240 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001241 case HP_EVENT:
1242 cs_automute(codec);
1243 break;
1244 case MIC_EVENT:
1245 cs_automic(codec);
1246 break;
1247 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001248 snd_hda_jack_report_sync(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001249}
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,
1256 .unsol_event = cs_unsol_event,
1257};
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" },
1289 {}
Takashi Iwaia6bae202009-07-06 15:15:22 +02001290};
1291
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001292static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001293 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001294 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001295 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001296 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001297 /* this conflicts with too many other models */
1298 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001299
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001300 /* codec SSID */
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001301 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001302 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001303 {} /* terminator */
1304};
1305
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001306static const struct hda_pintbl mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001307 { 0x09, 0x012b4050 },
1308 { 0x0a, 0x90100141 },
1309 { 0x0b, 0x90100140 },
1310 { 0x0c, 0x018b3020 },
1311 { 0x0d, 0x90a00110 },
1312 { 0x0e, 0x400000f0 },
1313 { 0x0f, 0x01cbe030 },
1314 { 0x10, 0x014be060 },
1315 { 0x12, 0x400000f0 },
1316 { 0x15, 0x400000f0 },
1317 {} /* terminator */
1318};
1319
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001320static const struct hda_pintbl mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001321 { 0x09, 0x012b4030 },
1322 { 0x0a, 0x90100121 },
1323 { 0x0b, 0x90100120 },
1324 { 0x0c, 0x400000f0 },
1325 { 0x0d, 0x90a00110 },
1326 { 0x0e, 0x400000f0 },
1327 { 0x0f, 0x400000f0 },
1328 { 0x10, 0x014be040 },
1329 { 0x12, 0x400000f0 },
1330 { 0x15, 0x400000f0 },
1331 {} /* terminator */
1332};
1333
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001334static const struct hda_pintbl imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001335 { 0x09, 0x012b4050 },
1336 { 0x0a, 0x90100140 },
1337 { 0x0b, 0x90100142 },
1338 { 0x0c, 0x018b3020 },
1339 { 0x0d, 0x90a00110 },
1340 { 0x0e, 0x400000f0 },
1341 { 0x0f, 0x01cbe030 },
1342 { 0x10, 0x014be060 },
1343 { 0x12, 0x01ab9070 },
1344 { 0x15, 0x400000f0 },
1345 {} /* terminator */
1346};
1347
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001348static void cs420x_fixup_gpio_13(struct hda_codec *codec,
1349 const struct hda_fixup *fix, int action)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001350{
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001351 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1352 struct cs_spec *spec = codec->spec;
1353 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1354 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1355 spec->gpio_mask = spec->gpio_dir =
1356 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1357 }
Takashi Iwaia6bae202009-07-06 15:15:22 +02001358}
1359
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001360static void cs420x_fixup_gpio_23(struct hda_codec *codec,
1361 const struct hda_fixup *fix, int action)
1362{
1363 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1364 struct cs_spec *spec = codec->spec;
1365 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1366 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1367 spec->gpio_mask = spec->gpio_dir =
1368 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1369 }
1370}
1371
1372static const struct hda_fixup cs420x_fixups[] = {
1373 [CS420X_MBP53] = {
1374 .type = HDA_FIXUP_PINS,
1375 .v.pins = mbp53_pincfgs,
1376 .chained = true,
1377 .chain_id = CS420X_APPLE,
1378 },
1379 [CS420X_MBP55] = {
1380 .type = HDA_FIXUP_PINS,
1381 .v.pins = mbp55_pincfgs,
1382 .chained = true,
1383 .chain_id = CS420X_GPIO_13,
1384 },
1385 [CS420X_IMAC27] = {
1386 .type = HDA_FIXUP_PINS,
1387 .v.pins = imac27_pincfgs,
1388 .chained = true,
1389 .chain_id = CS420X_GPIO_13,
1390 },
1391 [CS420X_GPIO_13] = {
1392 .type = HDA_FIXUP_FUNC,
1393 .v.func = cs420x_fixup_gpio_13,
1394 },
1395 [CS420X_GPIO_23] = {
1396 .type = HDA_FIXUP_FUNC,
1397 .v.func = cs420x_fixup_gpio_23,
1398 },
1399};
1400
Takashi Iwaie5f14242009-07-01 18:11:44 +02001401static int patch_cs420x(struct hda_codec *codec)
1402{
1403 struct cs_spec *spec;
1404 int err;
1405
1406 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1407 if (!spec)
1408 return -ENOMEM;
1409 codec->spec = spec;
1410
Tim Howe56487c22011-07-22 16:41:00 -05001411 spec->vendor_nid = CS420X_VENDOR_NID;
1412
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001413 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
1414 cs420x_fixups);
1415 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001416
Takashi Iwaied208252009-07-07 09:04:26 +02001417 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001418 if (err < 0)
1419 goto error;
1420
Takashi Iwaie5f14242009-07-01 18:11:44 +02001421 codec->patch_ops = cs_patch_ops;
1422
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001423 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1424
Takashi Iwaie5f14242009-07-01 18:11:44 +02001425 return 0;
1426
1427 error:
1428 kfree(codec->spec);
1429 codec->spec = NULL;
1430 return err;
1431}
1432
Tim Howe56487c22011-07-22 16:41:00 -05001433/*
1434 * Cirrus Logic CS4210
1435 *
1436 * 1 DAC => HP(sense) / Speakers,
1437 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1438 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1439*/
1440
1441/* CS4210 board names */
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001442static const struct hda_model_fixup cs421x_models[] = {
1443 { .id = CS421X_CDB4210, .name = "cdb4210" },
1444 {}
Tim Howe56487c22011-07-22 16:41:00 -05001445};
1446
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001447static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001448 /* Test Intel board + CDB2410 */
1449 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1450 {} /* terminator */
1451};
1452
1453/* CS4210 board pinconfigs */
1454/* Default CS4210 (CDB4210)*/
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001455static const struct hda_pintbl cdb4210_pincfgs[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001456 { 0x05, 0x0321401f },
1457 { 0x06, 0x90170010 },
1458 { 0x07, 0x03813031 },
1459 { 0x08, 0xb7a70037 },
1460 { 0x09, 0xb7a6003e },
1461 { 0x0a, 0x034510f0 },
1462 {} /* terminator */
1463};
1464
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001465/* Setup GPIO/SENSE for each board (if used) */
1466static void cs421x_fixup_sense_b(struct hda_codec *codec,
1467 const struct hda_fixup *fix, int action)
1468{
1469 struct cs_spec *spec = codec->spec;
1470 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1471 spec->sense_b = 1;
1472}
1473
1474static const struct hda_fixup cs421x_fixups[] = {
1475 [CS421X_CDB4210] = {
1476 .type = HDA_FIXUP_PINS,
1477 .v.pins = cdb4210_pincfgs,
1478 .chained = true,
1479 .chain_id = CS421X_SENSE_B,
1480 },
1481 [CS421X_SENSE_B] = {
1482 .type = HDA_FIXUP_FUNC,
1483 .v.func = cs421x_fixup_sense_b,
1484 }
Tim Howe56487c22011-07-22 16:41:00 -05001485};
1486
1487static const struct hda_verb cs421x_coef_init_verbs[] = {
1488 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1489 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1490 /*
1491 Disable Coefficient Index Auto-Increment(DAI)=1,
1492 PDREF=0
1493 */
1494 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1495
1496 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1497 /* ADC SZCMode = Digital Soft Ramp */
1498 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1499
1500 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1501 {0x0B, AC_VERB_SET_PROC_COEF,
1502 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1503 | 0x0004 /* Mute DAC on FIFO error */
1504 | 0x0008 /* Enable DAC High Pass Filter */
1505 )},
1506 {} /* terminator */
1507};
1508
1509/* Errata: CS4210 rev A1 Silicon
1510 *
1511 * http://www.cirrus.com/en/pubs/errata/
1512 *
1513 * Description:
1514 * 1. Performance degredation is present in the ADC.
1515 * 2. Speaker output is not completely muted upon HP detect.
1516 * 3. Noise is present when clipping occurs on the amplified
1517 * speaker outputs.
1518 *
1519 * Workaround:
1520 * The following verb sequence written to the registers during
1521 * initialization will correct the issues listed above.
1522 */
1523
1524static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1525 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1526
1527 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1528 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1529
1530 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1531 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1532
1533 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1534 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1535
1536 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1537 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1538
1539 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1540 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1541
1542 {} /* terminator */
1543};
1544
1545/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1546static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1547
1548static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1549 struct snd_ctl_elem_info *uinfo)
1550{
1551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1552 uinfo->count = 1;
1553 uinfo->value.integer.min = 0;
1554 uinfo->value.integer.max = 3;
1555 return 0;
1556}
1557
1558static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1559 struct snd_ctl_elem_value *ucontrol)
1560{
1561 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1562
1563 ucontrol->value.integer.value[0] =
1564 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1565 return 0;
1566}
1567
1568static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1569 struct snd_ctl_elem_value *ucontrol)
1570{
1571 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1572
1573 unsigned int vol = ucontrol->value.integer.value[0];
1574 unsigned int coef =
1575 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1576 unsigned int original_coef = coef;
1577
1578 coef &= ~0x0003;
1579 coef |= (vol & 0x0003);
1580 if (original_coef == coef)
1581 return 0;
1582 else {
1583 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1584 return 1;
1585 }
1586}
1587
1588static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1589
1590 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1591 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1592 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1593 .name = "Speaker Boost Playback Volume",
1594 .info = cs421x_boost_vol_info,
1595 .get = cs421x_boost_vol_get,
1596 .put = cs421x_boost_vol_put,
1597 .tlv = { .p = cs421x_speaker_boost_db_scale },
1598};
1599
David Henningsson5660ffd2012-01-02 12:40:17 +01001600static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001601{
1602 struct cs_spec *spec = codec->spec;
1603 unsigned int def_conf, coef;
1604
1605 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1606 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1607
1608 if (spec->gpio_mask)
1609 coef |= 0x0008; /* B1,B2 are GPIOs */
1610 else
1611 coef &= ~0x0008;
1612
1613 if (spec->sense_b)
1614 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1615 else
1616 coef &= ~0x0010;
1617
1618 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1619
1620 if ((spec->gpio_mask || spec->sense_b) &&
1621 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1622
1623 /*
1624 GPIO or SENSE_B forced - disconnect the DMIC pin.
1625 */
1626 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1627 def_conf &= ~AC_DEFCFG_PORT_CONN;
1628 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1629 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1630 }
1631}
1632
1633static void init_cs421x_digital(struct hda_codec *codec)
1634{
1635 struct cs_spec *spec = codec->spec;
1636 struct auto_pin_cfg *cfg = &spec->autocfg;
1637 int i;
1638
1639
1640 for (i = 0; i < cfg->dig_outs; i++) {
1641 hda_nid_t nid = cfg->dig_out_pins[i];
1642 if (!cfg->speaker_outs)
1643 continue;
1644 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001645 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
Tim Howe56487c22011-07-22 16:41:00 -05001646 spec->spdif_detect = 1;
1647 }
1648 }
1649}
1650
1651static int cs421x_init(struct hda_codec *codec)
1652{
1653 struct cs_spec *spec = codec->spec;
1654
David Henningsson5660ffd2012-01-02 12:40:17 +01001655 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1656 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1657 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1658 cs4210_pinmux_init(codec);
1659 }
Tim Howe56487c22011-07-22 16:41:00 -05001660
1661 if (spec->gpio_mask) {
1662 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1663 spec->gpio_mask);
1664 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1665 spec->gpio_dir);
1666 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1667 spec->gpio_data);
1668 }
1669
1670 init_output(codec);
1671 init_input(codec);
1672 init_cs421x_digital(codec);
1673
1674 return 0;
1675}
1676
1677/*
1678 * CS4210 Input MUX (1 ADC)
1679 */
1680static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_info *uinfo)
1682{
1683 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1684 struct cs_spec *spec = codec->spec;
1685
1686 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1687}
1688
1689static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1690 struct snd_ctl_elem_value *ucontrol)
1691{
1692 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1693 struct cs_spec *spec = codec->spec;
1694
1695 ucontrol->value.enumerated.item[0] = spec->cur_input;
1696 return 0;
1697}
1698
1699static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1700 struct snd_ctl_elem_value *ucontrol)
1701{
1702 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1703 struct cs_spec *spec = codec->spec;
1704
1705 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1706 spec->adc_nid[0], &spec->cur_input);
1707
1708}
1709
1710static struct snd_kcontrol_new cs421x_capture_source = {
1711
1712 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1713 .name = "Capture Source",
1714 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1715 .info = cs421x_mux_enum_info,
1716 .get = cs421x_mux_enum_get,
1717 .put = cs421x_mux_enum_put,
1718};
1719
1720static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1721{
1722 struct cs_spec *spec = codec->spec;
1723 struct auto_pin_cfg *cfg = &spec->autocfg;
1724 const struct hda_input_mux *imux = &spec->input_mux;
1725 hda_nid_t pin = cfg->inputs[item].pin;
1726 struct snd_kcontrol *kctl;
1727 u32 caps;
1728
1729 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1730 return 0;
1731
1732 caps = query_amp_caps(codec, pin, HDA_INPUT);
1733 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1734 if (caps <= 1)
1735 return 0;
1736
1737 return add_volume(codec, imux->items[item].label, 0,
1738 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1739}
1740
1741/* add a (input-boost) volume control to the given input pin */
1742static int build_cs421x_input(struct hda_codec *codec)
1743{
1744 struct cs_spec *spec = codec->spec;
1745 struct auto_pin_cfg *cfg = &spec->autocfg;
1746 struct hda_input_mux *imux = &spec->input_mux;
1747 int i, err, type_idx;
1748 const char *label;
1749
1750 if (!spec->num_inputs)
1751 return 0;
1752
1753 /* make bind-capture */
1754 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1755 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1756 for (i = 0; i < 2; i++) {
1757 struct snd_kcontrol *kctl;
1758 int n;
1759 if (!spec->capture_bind[i])
1760 return -ENOMEM;
1761 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1762 if (!kctl)
1763 return -ENOMEM;
1764 kctl->private_value = (long)spec->capture_bind[i];
1765 err = snd_hda_ctl_add(codec, 0, kctl);
1766 if (err < 0)
1767 return err;
1768 for (n = 0; n < AUTO_PIN_LAST; n++) {
1769 if (!spec->adc_nid[n])
1770 continue;
1771 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1772 if (err < 0)
1773 return err;
1774 }
1775 }
1776
1777 /* Add Input MUX Items + Capture Volume/Switch */
1778 for (i = 0; i < spec->num_inputs; i++) {
1779 label = hda_get_autocfg_input_label(codec, cfg, i);
1780 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1781
1782 err = cs421x_add_input_volume_control(codec, i);
1783 if (err < 0)
1784 return err;
1785 }
1786
1787 /*
1788 Add 'Capture Source' Switch if
1789 * 2 inputs and no mic detec
1790 * 3 inputs
1791 */
1792 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1793 (spec->num_inputs == 3)) {
1794
1795 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1796 snd_ctl_new1(&cs421x_capture_source, codec));
1797 if (err < 0)
1798 return err;
1799 }
1800
1801 return 0;
1802}
1803
1804/* Single DAC (Mute/Gain) */
1805static int build_cs421x_output(struct hda_codec *codec)
1806{
1807 hda_nid_t dac = CS4210_DAC_NID;
1808 struct cs_spec *spec = codec->spec;
1809 struct auto_pin_cfg *cfg = &spec->autocfg;
1810 struct snd_kcontrol *kctl;
1811 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001812 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001813
1814 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001815
1816 err = add_mute(codec, name, 0,
1817 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1818 if (err < 0)
1819 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001820
1821 err = add_volume(codec, name, 0,
1822 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1823 if (err < 0)
1824 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001825
David Henningsson5660ffd2012-01-02 12:40:17 +01001826 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001827 err = snd_hda_ctl_add(codec, 0,
1828 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1829 if (err < 0)
1830 return err;
1831 }
1832 return err;
1833}
1834
1835static int cs421x_build_controls(struct hda_codec *codec)
1836{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001837 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001838 int err;
1839
1840 err = build_cs421x_output(codec);
1841 if (err < 0)
1842 return err;
1843 err = build_cs421x_input(codec);
1844 if (err < 0)
1845 return err;
1846 err = build_digital_output(codec);
1847 if (err < 0)
1848 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001849 err = cs421x_init(codec);
1850 if (err < 0)
1851 return err;
1852
1853 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1854 if (err < 0)
1855 return err;
1856
1857 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001858}
1859
1860static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1861{
Takashi Iwai3a938972011-10-28 01:16:55 +02001862 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Tim Howe56487c22011-07-22 16:41:00 -05001863 case HP_EVENT:
1864 case SPDIF_EVENT:
1865 cs_automute(codec);
1866 break;
1867
1868 case MIC_EVENT:
1869 cs_automic(codec);
1870 break;
1871 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001872 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001873}
1874
1875static int parse_cs421x_input(struct hda_codec *codec)
1876{
1877 struct cs_spec *spec = codec->spec;
1878 struct auto_pin_cfg *cfg = &spec->autocfg;
1879 int i;
1880
1881 for (i = 0; i < cfg->num_inputs; i++) {
1882 hda_nid_t pin = cfg->inputs[i].pin;
1883 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1884 spec->cur_input = spec->last_input = i;
1885 spec->num_inputs++;
1886
1887 /* check whether the automatic mic switch is available */
1888 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1889 spec->mic_detect = 1;
1890 spec->automic_idx = i;
1891 }
1892 }
1893 return 0;
1894}
1895
1896static int cs421x_parse_auto_config(struct hda_codec *codec)
1897{
1898 struct cs_spec *spec = codec->spec;
1899 int err;
1900
1901 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1902 if (err < 0)
1903 return err;
1904 err = parse_output(codec);
1905 if (err < 0)
1906 return err;
1907 err = parse_cs421x_input(codec);
1908 if (err < 0)
1909 return err;
1910 err = parse_digital_output(codec);
1911 if (err < 0)
1912 return err;
1913 return 0;
1914}
1915
1916#ifdef CONFIG_PM
1917/*
1918 Manage PDREF, when transitioning to D3hot
1919 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1920*/
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001921static int cs421x_suspend(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001922{
David Henningsson5660ffd2012-01-02 12:40:17 +01001923 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001924 unsigned int coef;
1925
1926 snd_hda_shutup_pins(codec);
1927
1928 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1929 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1930 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1931 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1932
David Henningsson5660ffd2012-01-02 12:40:17 +01001933 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1934 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1935 coef |= 0x0004; /* PDREF */
1936 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1937 }
Tim Howe56487c22011-07-22 16:41:00 -05001938
1939 return 0;
1940}
1941#endif
1942
David Henningsson5660ffd2012-01-02 12:40:17 +01001943static struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001944 .build_controls = cs421x_build_controls,
1945 .build_pcms = cs_build_pcms,
1946 .init = cs421x_init,
1947 .free = cs_free,
1948 .unsol_event = cs421x_unsol_event,
1949#ifdef CONFIG_PM
1950 .suspend = cs421x_suspend,
1951#endif
1952};
1953
David Henningsson5660ffd2012-01-02 12:40:17 +01001954static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001955{
1956 struct cs_spec *spec;
1957 int err;
1958
1959 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1960 if (!spec)
1961 return -ENOMEM;
1962 codec->spec = spec;
1963
David Henningsson5660ffd2012-01-02 12:40:17 +01001964 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001965
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001966 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1967 cs421x_fixups);
1968 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Tim Howe56487c22011-07-22 16:41:00 -05001969
1970 /*
1971 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1972 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1973 is disabled.
1974 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001975 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001976
1977 err = cs421x_parse_auto_config(codec);
1978 if (err < 0)
1979 goto error;
1980
David Henningsson5660ffd2012-01-02 12:40:17 +01001981 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05001982
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001983 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1984
Tim Howe56487c22011-07-22 16:41:00 -05001985 return 0;
1986
1987 error:
1988 kfree(codec->spec);
1989 codec->spec = NULL;
1990 return err;
1991}
1992
David Henningsson5660ffd2012-01-02 12:40:17 +01001993static int patch_cs4213(struct hda_codec *codec)
1994{
1995 struct cs_spec *spec;
1996 int err;
1997
1998 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1999 if (!spec)
2000 return -ENOMEM;
2001 codec->spec = spec;
2002
2003 spec->vendor_nid = CS4213_VENDOR_NID;
2004
2005 err = cs421x_parse_auto_config(codec);
2006 if (err < 0)
2007 goto error;
2008
2009 codec->patch_ops = cs421x_patch_ops;
2010 return 0;
2011
2012 error:
2013 kfree(codec->spec);
2014 codec->spec = NULL;
2015 return err;
2016}
2017
Takashi Iwaie5f14242009-07-01 18:11:44 +02002018
2019/*
2020 * patch entries
2021 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002022static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002023 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2024 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002025 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2026 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002027 {} /* terminator */
2028};
2029
2030MODULE_ALIAS("snd-hda-codec-id:10134206");
2031MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002032MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002033MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002034
2035MODULE_LICENSE("GPL");
2036MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2037
2038static struct hda_codec_preset_list cirrus_list = {
2039 .preset = snd_hda_preset_cirrus,
2040 .owner = THIS_MODULE,
2041};
2042
2043static int __init patch_cirrus_init(void)
2044{
2045 return snd_hda_add_codec_preset(&cirrus_list);
2046}
2047
2048static void __exit patch_cirrus_exit(void)
2049{
2050 snd_hda_delete_codec_preset(&cirrus_list);
2051}
2052
2053module_init(patch_cirrus_init)
2054module_exit(patch_cirrus_exit)