blob: 6e1e711585ff8956348f4ada65a4c30a86867870 [file] [log] [blame]
Takashi Iwaie5f14242009-07-01 18:11:44 +02001/*
2 * HD audio interface patch for Cirrus Logic CS420x chip
3 *
4 * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
5 *
6 * This driver is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This driver is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/init.h>
22#include <linux/delay.h>
23#include <linux/slab.h>
24#include <linux/pci.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040025#include <linux/module.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020026#include <sound/core.h>
27#include "hda_codec.h"
28#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020029#include "hda_auto_parser.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020030#include "hda_jack.h"
Tim Howe56487c22011-07-22 16:41:00 -050031#include <sound/tlv.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020032
33/*
34 */
35
36struct cs_spec {
Takashi Iwaib35aabd2012-08-24 19:09:45 +020037 struct hda_gen_spec gen;
38
Takashi Iwaie5f14242009-07-01 18:11:44 +020039 struct auto_pin_cfg autocfg;
40 struct hda_multi_out multiout;
41 struct snd_kcontrol *vmaster_sw;
42 struct snd_kcontrol *vmaster_vol;
43
44 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
45 hda_nid_t slave_dig_outs[2];
46
47 unsigned int input_idx[AUTO_PIN_LAST];
48 unsigned int capsrc_idx[AUTO_PIN_LAST];
49 hda_nid_t adc_nid[AUTO_PIN_LAST];
50 unsigned int adc_idx[AUTO_PIN_LAST];
51 unsigned int num_inputs;
52 unsigned int cur_input;
53 unsigned int automic_idx;
54 hda_nid_t cur_adc;
55 unsigned int cur_adc_stream_tag;
56 unsigned int cur_adc_format;
57 hda_nid_t dig_in;
58
Takashi Iwaic42d4782011-05-02 11:36:09 +020059 const struct hda_bind_ctls *capture_bind[2];
Takashi Iwaie5f14242009-07-01 18:11:44 +020060
Takashi Iwaied208252009-07-07 09:04:26 +020061 unsigned int gpio_mask;
62 unsigned int gpio_dir;
63 unsigned int gpio_data;
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010064 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
65 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
Takashi Iwaied208252009-07-07 09:04:26 +020066
Takashi Iwaie5f14242009-07-01 18:11:44 +020067 struct hda_pcm pcm_rec[2]; /* PCM information */
68
69 unsigned int hp_detect:1;
70 unsigned int mic_detect:1;
Takashi Iwaif37bc7a2012-11-08 15:59:23 +010071 unsigned int speaker_2_1:1;
Tim Howe56487c22011-07-22 16:41:00 -050072 /* CS421x */
73 unsigned int spdif_detect:1;
74 unsigned int sense_b:1;
75 hda_nid_t vendor_nid;
76 struct hda_input_mux input_mux;
77 unsigned int last_input;
Takashi Iwaie5f14242009-07-01 18:11:44 +020078};
79
Tim Howe56487c22011-07-22 16:41:00 -050080/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020081enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040082 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020083 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010084 CS420X_IMAC27,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020085 CS420X_GPIO_13,
86 CS420X_GPIO_23,
Takashi Iwaief596a52012-09-11 16:53:08 +020087 CS420X_MBP101,
Takashi Iwaia6bae202009-07-06 15:15:22 +020088 CS420X_AUTO,
Takashi Iwai03efce72012-09-13 09:56:57 +020089 /* aliases */
90 CS420X_IMAC27_122 = CS420X_GPIO_23,
91 CS420X_APPLE = CS420X_GPIO_13,
Takashi Iwaia6bae202009-07-06 15:15:22 +020092};
93
Tim Howe56487c22011-07-22 16:41:00 -050094/* CS421x boards */
95enum {
96 CS421X_CDB4210,
Takashi Iwaib35aabd2012-08-24 19:09:45 +020097 CS421X_SENSE_B,
Tim Howe56487c22011-07-22 16:41:00 -050098};
99
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200100/* Vendor-specific processing widget */
101#define CS420X_VENDOR_NID 0x11
102#define CS_DIG_OUT1_PIN_NID 0x10
103#define CS_DIG_OUT2_PIN_NID 0x15
Daniel J Blueman16337e02012-11-04 13:19:03 +0800104#define CS_DMIC1_PIN_NID 0x0e
105#define CS_DMIC2_PIN_NID 0x12
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200106
107/* coef indices */
108#define IDX_SPDIF_STAT 0x0000
109#define IDX_SPDIF_CTL 0x0001
110#define IDX_ADC_CFG 0x0002
111/* SZC bitmask, 4 modes below:
112 * 0 = immediate,
113 * 1 = digital immediate, analog zero-cross
114 * 2 = digtail & analog soft-ramp
115 * 3 = digital soft-ramp, analog zero-cross
116 */
117#define CS_COEF_ADC_SZC_MASK (3 << 0)
118#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
119#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
120/* PGA mode: 0 = differential, 1 = signle-ended */
121#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
122#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
123#define IDX_DAC_CFG 0x0003
124/* SZC bitmask, 4 modes below:
125 * 0 = Immediate
126 * 1 = zero-cross
127 * 2 = soft-ramp
128 * 3 = soft-ramp on zero-cross
129 */
130#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
131#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
132#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
133
134#define IDX_BEEP_CFG 0x0004
135/* 0x0008 - test reg key */
136/* 0x0009 - 0x0014 -> 12 test regs */
137/* 0x0015 - visibility reg */
138
Tim Howe56487c22011-07-22 16:41:00 -0500139/*
140 * Cirrus Logic CS4210
141 *
142 * 1 DAC => HP(sense) / Speakers,
143 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
144 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
145*/
146#define CS4210_DAC_NID 0x02
147#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100148#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500149#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
150#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
151
152#define CS421X_IDX_DEV_CFG 0x01
153#define CS421X_IDX_ADC_CFG 0x02
154#define CS421X_IDX_DAC_CFG 0x03
155#define CS421X_IDX_SPK_CTL 0x04
156
157#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200158
David Henningsson5660ffd2012-01-02 12:40:17 +0100159/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
160#define CS4213_VENDOR_NID 0x09
161
162
Takashi Iwai277a57c2009-07-08 12:42:08 +0200163static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200164{
Tim Howe56487c22011-07-22 16:41:00 -0500165 struct cs_spec *spec = codec->spec;
166 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200167 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500168 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200169 AC_VERB_GET_PROC_COEF, 0);
170}
171
Takashi Iwai277a57c2009-07-08 12:42:08 +0200172static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
173 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200174{
Tim Howe56487c22011-07-22 16:41:00 -0500175 struct cs_spec *spec = codec->spec;
176 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200177 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500178 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200179 AC_VERB_SET_PROC_COEF, coef);
180}
181
182
Takashi Iwaie5f14242009-07-01 18:11:44 +0200183#define HP_EVENT 1
184#define MIC_EVENT 2
185
186/*
187 * PCM callbacks
188 */
189static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
190 struct hda_codec *codec,
191 struct snd_pcm_substream *substream)
192{
193 struct cs_spec *spec = codec->spec;
194 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
195 hinfo);
196}
197
198static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
199 struct hda_codec *codec,
200 unsigned int stream_tag,
201 unsigned int format,
202 struct snd_pcm_substream *substream)
203{
204 struct cs_spec *spec = codec->spec;
205 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
206 stream_tag, format, substream);
207}
208
209static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
210 struct hda_codec *codec,
211 struct snd_pcm_substream *substream)
212{
213 struct cs_spec *spec = codec->spec;
214 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
215}
216
217/*
218 * Digital out
219 */
220static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
221 struct hda_codec *codec,
222 struct snd_pcm_substream *substream)
223{
224 struct cs_spec *spec = codec->spec;
225 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
226}
227
228static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
229 struct hda_codec *codec,
230 struct snd_pcm_substream *substream)
231{
232 struct cs_spec *spec = codec->spec;
233 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
234}
235
236static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
237 struct hda_codec *codec,
238 unsigned int stream_tag,
239 unsigned int format,
240 struct snd_pcm_substream *substream)
241{
242 struct cs_spec *spec = codec->spec;
243 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
244 format, substream);
245}
246
247static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
248 struct hda_codec *codec,
249 struct snd_pcm_substream *substream)
250{
251 struct cs_spec *spec = codec->spec;
252 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
253}
254
Takashi Iwai05ee7962011-11-16 18:05:11 +0100255static void cs_update_input_select(struct hda_codec *codec)
256{
257 struct cs_spec *spec = codec->spec;
258 if (spec->cur_adc)
259 snd_hda_codec_write(codec, spec->cur_adc, 0,
260 AC_VERB_SET_CONNECT_SEL,
261 spec->adc_idx[spec->cur_input]);
262}
263
Takashi Iwaie5f14242009-07-01 18:11:44 +0200264/*
265 * Analog capture
266 */
267static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
268 struct hda_codec *codec,
269 unsigned int stream_tag,
270 unsigned int format,
271 struct snd_pcm_substream *substream)
272{
273 struct cs_spec *spec = codec->spec;
274 spec->cur_adc = spec->adc_nid[spec->cur_input];
275 spec->cur_adc_stream_tag = stream_tag;
276 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100277 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200278 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
279 return 0;
280}
281
282static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
283 struct hda_codec *codec,
284 struct snd_pcm_substream *substream)
285{
286 struct cs_spec *spec = codec->spec;
287 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
288 spec->cur_adc = 0;
289 return 0;
290}
291
292/*
293 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200294static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200295 .substreams = 1,
296 .channels_min = 2,
297 .channels_max = 2,
298 .ops = {
299 .open = cs_playback_pcm_open,
300 .prepare = cs_playback_pcm_prepare,
301 .cleanup = cs_playback_pcm_cleanup
302 },
303};
304
Takashi Iwaic42d4782011-05-02 11:36:09 +0200305static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200306 .substreams = 1,
307 .channels_min = 2,
308 .channels_max = 2,
309 .ops = {
310 .prepare = cs_capture_pcm_prepare,
311 .cleanup = cs_capture_pcm_cleanup
312 },
313};
314
Takashi Iwaic42d4782011-05-02 11:36:09 +0200315static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200316 .substreams = 1,
317 .channels_min = 2,
318 .channels_max = 2,
319 .ops = {
320 .open = cs_dig_playback_pcm_open,
321 .close = cs_dig_playback_pcm_close,
322 .prepare = cs_dig_playback_pcm_prepare,
323 .cleanup = cs_dig_playback_pcm_cleanup
324 },
325};
326
Takashi Iwaic42d4782011-05-02 11:36:09 +0200327static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200328 .substreams = 1,
329 .channels_min = 2,
330 .channels_max = 2,
331};
332
333static int cs_build_pcms(struct hda_codec *codec)
334{
335 struct cs_spec *spec = codec->spec;
336 struct hda_pcm *info = spec->pcm_rec;
337
338 codec->pcm_info = info;
339 codec->num_pcms = 0;
340
341 info->name = "Cirrus Analog";
342 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
343 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
344 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
345 spec->multiout.max_channels;
Takashi Iwaiee81abb2012-11-08 17:12:10 +0100346 if (spec->speaker_2_1)
347 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
348 snd_pcm_2_1_chmaps;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200349 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200350 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
351 spec->adc_nid[spec->cur_input];
352 codec->num_pcms++;
353
354 if (!spec->multiout.dig_out_nid && !spec->dig_in)
355 return 0;
356
357 info++;
358 info->name = "Cirrus Digital";
359 info->pcm_type = spec->autocfg.dig_out_type[0];
360 if (!info->pcm_type)
361 info->pcm_type = HDA_PCM_TYPE_SPDIF;
362 if (spec->multiout.dig_out_nid) {
363 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
364 cs_pcm_digital_playback;
365 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
366 spec->multiout.dig_out_nid;
367 }
368 if (spec->dig_in) {
369 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
370 cs_pcm_digital_capture;
371 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
372 }
373 codec->num_pcms++;
374
375 return 0;
376}
377
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200378/*
379 * parse codec topology
380 */
381
Takashi Iwaie5f14242009-07-01 18:11:44 +0200382static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
383{
384 hda_nid_t dac;
385 if (!pin)
386 return 0;
387 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
388 return 0;
389 return dac;
390}
391
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200392static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
393{
394 struct cs_spec *spec = codec->spec;
395 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200396 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200397 unsigned int val;
398 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200399 return 0;
400 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200401 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200402}
403
404static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
405 unsigned int *idxp)
406{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200407 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200408 hda_nid_t nid;
409
410 nid = codec->start_nid;
411 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200412 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200413 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200414 if (type != AC_WID_AUD_IN)
415 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200416 idx = snd_hda_get_conn_index(codec, nid, pin, false);
417 if (idx >= 0) {
418 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200419 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200420 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200421 }
422 return 0;
423}
424
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200425static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
426{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200427 unsigned int val;
428 val = snd_hda_codec_get_pincfg(codec, nid);
429 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
430}
431
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200432static int parse_output(struct hda_codec *codec)
433{
434 struct cs_spec *spec = codec->spec;
435 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200436 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200437 hda_nid_t dac;
438
439 for (i = 0; i < cfg->line_outs; i++) {
440 dac = get_dac(codec, cfg->line_out_pins[i]);
441 if (!dac)
442 break;
443 spec->dac_nid[i] = dac;
444 }
445 spec->multiout.num_dacs = i;
446 spec->multiout.dac_nids = spec->dac_nid;
447 spec->multiout.max_channels = i * 2;
448
Takashi Iwaif37bc7a2012-11-08 15:59:23 +0100449 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT && i == 2)
450 spec->speaker_2_1 = 1; /* assume 2.1 speakers */
451
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200452 /* add HP and speakers */
453 extra_nids = 0;
454 for (i = 0; i < cfg->hp_outs; i++) {
455 dac = get_dac(codec, cfg->hp_pins[i]);
456 if (!dac)
457 break;
458 if (!i)
459 spec->multiout.hp_nid = dac;
460 else
461 spec->multiout.extra_out_nid[extra_nids++] = dac;
462 }
463 for (i = 0; i < cfg->speaker_outs; i++) {
464 dac = get_dac(codec, cfg->speaker_pins[i]);
465 if (!dac)
466 break;
467 spec->multiout.extra_out_nid[extra_nids++] = dac;
468 }
469
470 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
471 cfg->speaker_outs = cfg->line_outs;
472 memcpy(cfg->speaker_pins, cfg->line_out_pins,
473 sizeof(cfg->speaker_pins));
474 cfg->line_outs = 0;
David Henningsson34c3d192012-11-21 10:03:10 +0100475 memset(cfg->line_out_pins, 0, sizeof(cfg->line_out_pins));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200476 }
477
478 return 0;
479}
480
481static int parse_input(struct hda_codec *codec)
482{
483 struct cs_spec *spec = codec->spec;
484 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200485 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200486
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200487 for (i = 0; i < cfg->num_inputs; i++) {
488 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200489 spec->input_idx[spec->num_inputs] = i;
490 spec->capsrc_idx[i] = spec->num_inputs++;
491 spec->cur_input = i;
492 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
493 }
494 if (!spec->num_inputs)
495 return 0;
496
497 /* check whether the automatic mic switch is available */
498 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200499 cfg->inputs[0].type == AUTO_PIN_MIC &&
500 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200501 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
502 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200503 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200504 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200505 }
506 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200507 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200508 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200509 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200510 }
511 }
512 }
513 return 0;
514}
515
516
517static int parse_digital_output(struct hda_codec *codec)
518{
519 struct cs_spec *spec = codec->spec;
520 struct auto_pin_cfg *cfg = &spec->autocfg;
521 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200522
523 if (!cfg->dig_outs)
524 return 0;
525 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
526 return 0;
527 spec->multiout.dig_out_nid = nid;
528 spec->multiout.share_spdif = 1;
529 if (cfg->dig_outs > 1 &&
530 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
531 spec->slave_dig_outs[0] = nid;
532 codec->slave_dig_outs = spec->slave_dig_outs;
533 }
534 return 0;
535}
536
537static int parse_digital_input(struct hda_codec *codec)
538{
539 struct cs_spec *spec = codec->spec;
540 struct auto_pin_cfg *cfg = &spec->autocfg;
541 int idx;
542
Takashi Iwai60e53882009-07-06 15:01:09 +0200543 if (cfg->dig_in_pin)
544 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
545 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200546}
547
548/*
549 * create mixer controls
550 */
551
Takashi Iwaiea734962011-01-17 11:29:34 +0100552static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200553
554static int add_mute(struct hda_codec *codec, const char *name, int index,
555 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
556{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200557 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200558 struct snd_kcontrol_new knew =
559 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
560 knew.private_value = pval;
561 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
562 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100563 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100564 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200565}
566
567static int add_volume(struct hda_codec *codec, const char *name,
568 int index, unsigned int pval, int dir,
569 struct snd_kcontrol **kctlp)
570{
David Henningsson2e1210b2011-09-14 13:22:54 +0200571 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200572 struct snd_kcontrol_new knew =
573 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
574 knew.private_value = pval;
575 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
576 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100577 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100578 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200579}
580
581static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
582{
583 unsigned int caps;
584
585 /* set the upper-limit for mixer amp to 0dB */
586 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
587 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
588 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
589 << AC_AMPCAP_NUM_STEPS_SHIFT;
590 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
591}
592
593static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
594{
595 struct cs_spec *spec = codec->spec;
596 unsigned int tlv[4];
597 int err;
598
599 spec->vmaster_sw =
600 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100601 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200602 if (err < 0)
603 return err;
604
605 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
606 spec->vmaster_vol =
607 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100608 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200609 if (err < 0)
610 return err;
611 return 0;
612}
613
614static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
615 int num_ctls, int type)
616{
617 struct cs_spec *spec = codec->spec;
618 const char *name;
619 int err, index;
620 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200621 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200622 "Front Speaker", "Surround Speaker", "Bass Speaker"
623 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200624 static const char * const line_outs[] = {
Takashi Iwaie49a3432012-03-01 18:14:41 +0100625 "Front Line Out", "Surround Line Out", "Bass Line Out"
Takashi Iwaie5f14242009-07-01 18:11:44 +0200626 };
627
628 fix_volume_caps(codec, dac);
629 if (!spec->vmaster_sw) {
630 err = add_vmaster(codec, dac);
631 if (err < 0)
632 return err;
633 }
634
635 index = 0;
636 switch (type) {
637 case AUTO_PIN_HP_OUT:
638 name = "Headphone";
639 index = idx;
640 break;
641 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwaif37bc7a2012-11-08 15:59:23 +0100642 if (spec->speaker_2_1)
643 name = idx ? "Bass Speaker" : "Speaker";
644 else if (num_ctls > 1)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200645 name = speakers[idx];
646 else
647 name = "Speaker";
648 break;
649 default:
650 if (num_ctls > 1)
651 name = line_outs[idx];
652 else
Takashi Iwaie49a3432012-03-01 18:14:41 +0100653 name = "Line Out";
Takashi Iwaie5f14242009-07-01 18:11:44 +0200654 break;
655 }
656
657 err = add_mute(codec, name, index,
658 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
659 if (err < 0)
660 return err;
661 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
662 if (err < 0)
663 return err;
664
665 err = add_volume(codec, name, index,
666 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
667 if (err < 0)
668 return err;
669 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
670 if (err < 0)
671 return err;
672
673 return 0;
674}
675
676static int build_output(struct hda_codec *codec)
677{
678 struct cs_spec *spec = codec->spec;
679 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200680 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200681
682 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200683 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
684 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200685 if (err < 0)
686 return err;
687 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200688 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200689 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
690 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200691 if (err < 0)
692 return err;
693 }
694 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200695 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
696 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200697 if (err < 0)
698 return err;
699 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200700 return 0;
701}
702
703/*
704 */
705
Takashi Iwaic42d4782011-05-02 11:36:09 +0200706static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200707 HDA_BIND_SW("Capture Switch", 0),
708 HDA_BIND_VOL("Capture Volume", 0),
709};
710
Takashi Iwaiea359292009-07-06 12:58:59 +0200711static int change_cur_input(struct hda_codec *codec, unsigned int idx,
712 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200713{
714 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200715
Takashi Iwaiea359292009-07-06 12:58:59 +0200716 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200717 return 0;
718 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
719 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200720 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200721 spec->cur_adc = spec->adc_nid[idx];
722 snd_hda_codec_setup_stream(codec, spec->cur_adc,
723 spec->cur_adc_stream_tag, 0,
724 spec->cur_adc_format);
725 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200726 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100727 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200728 return 1;
729}
730
731static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
732 struct snd_ctl_elem_info *uinfo)
733{
734 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
735 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200736 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200737 unsigned int idx;
738
739 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
740 uinfo->count = 1;
741 uinfo->value.enumerated.items = spec->num_inputs;
742 if (uinfo->value.enumerated.item >= spec->num_inputs)
743 uinfo->value.enumerated.item = spec->num_inputs - 1;
744 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100745 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
746 uinfo->value.enumerated.name,
747 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200748 return 0;
749}
750
751static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
752 struct snd_ctl_elem_value *ucontrol)
753{
754 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
755 struct cs_spec *spec = codec->spec;
756 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
757 return 0;
758}
759
760static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
761 struct snd_ctl_elem_value *ucontrol)
762{
763 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
764 struct cs_spec *spec = codec->spec;
765 unsigned int idx = ucontrol->value.enumerated.item[0];
766
767 if (idx >= spec->num_inputs)
768 return -EINVAL;
769 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200770 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200771}
772
Takashi Iwaic42d4782011-05-02 11:36:09 +0200773static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200774 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
775 .name = "Capture Source",
776 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
777 .info = cs_capture_source_info,
778 .get = cs_capture_source_get,
779 .put = cs_capture_source_put,
780};
781
Takashi Iwaic42d4782011-05-02 11:36:09 +0200782static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200783 struct hda_ctl_ops *ops)
784{
785 struct cs_spec *spec = codec->spec;
786 struct hda_bind_ctls *bind;
787 int i, n;
788
789 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
790 GFP_KERNEL);
791 if (!bind)
792 return NULL;
793 bind->ops = ops;
794 n = 0;
795 for (i = 0; i < AUTO_PIN_LAST; i++) {
796 if (!spec->adc_nid[i])
797 continue;
798 bind->values[n++] =
799 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
800 spec->adc_idx[i], HDA_INPUT);
801 }
802 return bind;
803}
804
Takashi Iwai6a929342010-10-11 15:16:20 +0200805/* add a (input-boost) volume control to the given input pin */
806static int add_input_volume_control(struct hda_codec *codec,
807 struct auto_pin_cfg *cfg,
808 int item)
809{
810 hda_nid_t pin = cfg->inputs[item].pin;
811 u32 caps;
812 const char *label;
813 struct snd_kcontrol *kctl;
814
815 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
816 return 0;
817 caps = query_amp_caps(codec, pin, HDA_INPUT);
818 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
819 if (caps <= 1)
820 return 0;
821 label = hda_get_autocfg_input_label(codec, cfg, item);
822 return add_volume(codec, label, 0,
823 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
824}
825
Takashi Iwaie5f14242009-07-01 18:11:44 +0200826static int build_input(struct hda_codec *codec)
827{
828 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200829 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200830
Takashi Iwaie5f14242009-07-01 18:11:44 +0200831 if (!spec->num_inputs)
832 return 0;
833
Takashi Iwaie5f14242009-07-01 18:11:44 +0200834 /* make bind-capture */
835 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
836 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
837 for (i = 0; i < 2; i++) {
838 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100839 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200840 if (!spec->capture_bind[i])
841 return -ENOMEM;
842 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
843 if (!kctl)
844 return -ENOMEM;
845 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100846 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200847 if (err < 0)
848 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100849 for (n = 0; n < AUTO_PIN_LAST; n++) {
850 if (!spec->adc_nid[n])
851 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700852 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100853 if (err < 0)
854 return err;
855 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200856 }
857
858 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100859 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200860 snd_ctl_new1(&cs_capture_source, codec));
861 if (err < 0)
862 return err;
863 }
864
Takashi Iwai6a929342010-10-11 15:16:20 +0200865 for (i = 0; i < spec->num_inputs; i++) {
866 err = add_input_volume_control(codec, &spec->autocfg, i);
867 if (err < 0)
868 return err;
869 }
870
Takashi Iwaie5f14242009-07-01 18:11:44 +0200871 return 0;
872}
873
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200874/*
875 */
876
Takashi Iwaie5f14242009-07-01 18:11:44 +0200877static int build_digital_output(struct hda_codec *codec)
878{
879 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200880 int err;
881
Takashi Iwai63b24132009-07-09 11:45:59 +0200882 if (!spec->multiout.dig_out_nid)
883 return 0;
884
Takashi Iwaidcda5802012-10-12 17:24:51 +0200885 err = snd_hda_create_dig_out_ctls(codec, spec->multiout.dig_out_nid,
886 spec->multiout.dig_out_nid,
887 spec->pcm_rec[1].pcm_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200888 if (err < 0)
889 return err;
890 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
891 if (err < 0)
892 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200893 return 0;
894}
895
896static int build_digital_input(struct hda_codec *codec)
897{
898 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200899 if (spec->dig_in)
900 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
901 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200902}
903
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200904/*
905 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500906 * CS421x auto-output redirecting
907 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200908 */
909
David Henningsson5c2e4e02012-10-08 15:44:15 +0200910static void cs_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200911{
912 struct cs_spec *spec = codec->spec;
913 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200914 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500915 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200916 hda_nid_t nid;
917 int i;
918
Tim Howe56487c22011-07-22 16:41:00 -0500919 spdif_present = 0;
920 if (cfg->dig_outs) {
921 nid = cfg->dig_out_pins[0];
922 if (is_jack_detectable(codec, nid)) {
923 /*
924 TODO: SPDIF output redirect when SENSE_B is enabled.
925 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
926 assumed.
927 */
928 if (snd_hda_jack_detect(codec, nid)
929 /* && spec->sense_b */)
930 spdif_present = 1;
931 }
932 }
933
Takashi Iwaie5f14242009-07-01 18:11:44 +0200934 hp_present = 0;
935 for (i = 0; i < cfg->hp_outs; i++) {
936 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200937 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200938 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800939 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200940 if (hp_present)
941 break;
942 }
Tim Howe56487c22011-07-22 16:41:00 -0500943
944 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200945 for (i = 0; i < cfg->speaker_outs; i++) {
David Henningsson78e2a922012-01-02 12:40:16 +0100946 int pin_ctl = hp_present ? 0 : PIN_OUT;
David Henningsson5660ffd2012-01-02 12:40:17 +0100947 /* detect on spdif is specific to CS4210 */
948 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
David Henningsson78e2a922012-01-02 12:40:16 +0100949 pin_ctl = 0;
950
Takashi Iwaie5f14242009-07-01 18:11:44 +0200951 nid = cfg->speaker_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200952 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200953 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100954 if (spec->gpio_eapd_hp) {
955 unsigned int gpio = hp_present ?
956 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200957 snd_hda_codec_write(codec, 0x01, 0,
958 AC_VERB_SET_GPIO_DATA, gpio);
959 }
Tim Howe56487c22011-07-22 16:41:00 -0500960
David Henningsson5660ffd2012-01-02 12:40:17 +0100961 /* specific to CS4210 */
962 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500963 /* mute HPs if spdif jack (SENSE_B) is present */
964 for (i = 0; i < cfg->hp_outs; i++) {
965 nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200966 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500967 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
968 }
969
970 /* SPDIF TX on/off */
971 if (cfg->dig_outs) {
972 nid = cfg->dig_out_pins[0];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200973 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500974 spdif_present ? PIN_OUT : 0);
975
976 }
977 /* Update board GPIOs if neccessary ... */
978 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200979}
980
Tim Howe56487c22011-07-22 16:41:00 -0500981/*
982 * Auto-input redirect for CS421x
983 * Switch max 3 inputs of a single ADC (nid 3)
984*/
985
David Henningsson5c2e4e02012-10-08 15:44:15 +0200986static void cs_automic(struct hda_codec *codec, struct hda_jack_tbl *tbl)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200987{
988 struct cs_spec *spec = codec->spec;
989 struct auto_pin_cfg *cfg = &spec->autocfg;
990 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800991 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500992
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200993 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800994 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500995
996 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100997 if (spec->vendor_nid == CS420X_VENDOR_NID) {
998 if (present)
999 change_cur_input(codec, spec->automic_idx, 0);
1000 else
1001 change_cur_input(codec, !spec->automic_idx, 0);
1002 } else {
Tim Howe56487c22011-07-22 16:41:00 -05001003 if (present) {
Dylan Reidf70eecd2012-01-31 13:04:41 -08001004 if (spec->cur_input != spec->automic_idx) {
1005 spec->last_input = spec->cur_input;
1006 spec->cur_input = spec->automic_idx;
1007 }
Tim Howe56487c22011-07-22 16:41:00 -05001008 } else {
1009 spec->cur_input = spec->last_input;
1010 }
Takashi Iwai05ee7962011-11-16 18:05:11 +01001011 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001012 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001013}
1014
1015/*
1016 */
1017
1018static void init_output(struct hda_codec *codec)
1019{
1020 struct cs_spec *spec = codec->spec;
1021 struct auto_pin_cfg *cfg = &spec->autocfg;
1022 int i;
1023
1024 /* mute first */
1025 for (i = 0; i < spec->multiout.num_dacs; i++)
1026 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1027 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1028 if (spec->multiout.hp_nid)
1029 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1030 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1031 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1032 if (!spec->multiout.extra_out_nid[i])
1033 break;
1034 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1035 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1036 }
1037
1038 /* set appropriate pin controls */
1039 for (i = 0; i < cfg->line_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001040 snd_hda_set_pin_ctl(codec, cfg->line_out_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001041 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001042 for (i = 0; i < cfg->hp_outs; i++) {
1043 hda_nid_t nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001044 snd_hda_set_pin_ctl(codec, nid, PIN_HP);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001045 if (!cfg->speaker_outs)
1046 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001047 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
David Henningsson5c2e4e02012-10-08 15:44:15 +02001048 snd_hda_jack_detect_enable_callback(codec, nid, HP_EVENT, cs_automute);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001049 spec->hp_detect = 1;
1050 }
1051 }
Tim Howe56487c22011-07-22 16:41:00 -05001052
1053 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001054 for (i = 0; i < cfg->speaker_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001055 snd_hda_set_pin_ctl(codec, cfg->speaker_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001056
1057 /* SPDIF is enabled on presence detect for CS421x */
1058 if (spec->hp_detect || spec->spdif_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001059 cs_automute(codec, NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001060}
1061
1062static void init_input(struct hda_codec *codec)
1063{
1064 struct cs_spec *spec = codec->spec;
1065 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001066 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001067 int i;
1068
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001069 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001070 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001071 hda_nid_t pin = cfg->inputs[i].pin;
1072 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001073 continue;
1074 /* set appropriate pin control and mute first */
1075 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +02001076 if (cfg->inputs[i].type == AUTO_PIN_MIC)
1077 ctl |= snd_hda_get_default_vref(codec, pin);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001078 snd_hda_set_pin_ctl(codec, pin, ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001079 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1080 AC_VERB_SET_AMP_GAIN_MUTE,
1081 AMP_IN_MUTE(spec->adc_idx[i]));
1082 if (spec->mic_detect && spec->automic_idx == i)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001083 snd_hda_jack_detect_enable_callback(codec, pin, MIC_EVENT, cs_automic);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001084 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001085 /* CS420x has multiple ADC, CS421x has single ADC */
1086 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001087 change_cur_input(codec, spec->cur_input, 1);
1088 if (spec->mic_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001089 cs_automic(codec, NULL);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001090
Daniel J Blueman16337e02012-11-04 13:19:03 +08001091 coef = cs_vendor_coef_get(codec, IDX_BEEP_CFG);
Tim Howe56487c22011-07-22 16:41:00 -05001092 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
Daniel J Blueman16337e02012-11-04 13:19:03 +08001093 coef |= 1 << 4; /* DMIC2 2 chan on, GPIO1 off */
Tim Howe56487c22011-07-22 16:41:00 -05001094 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
Daniel J Blueman16337e02012-11-04 13:19:03 +08001095 coef |= 1 << 3; /* DMIC1 2 chan on, GPIO0 off
Tim Howe56487c22011-07-22 16:41:00 -05001096 * No effect if SPDIF_OUT2 is
1097 * selected in IDX_SPDIF_CTL.
1098 */
Daniel J Blueman16337e02012-11-04 13:19:03 +08001099
1100 cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001101 } else {
1102 if (spec->mic_detect)
David Henningsson5c2e4e02012-10-08 15:44:15 +02001103 cs_automic(codec, NULL);
David Henningsson5660ffd2012-01-02 12:40:17 +01001104 else {
1105 spec->cur_adc = spec->adc_nid[spec->cur_input];
1106 cs_update_input_select(codec);
1107 }
Tim Howe56487c22011-07-22 16:41:00 -05001108 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001109}
1110
Takashi Iwaic42d4782011-05-02 11:36:09 +02001111static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001112 {0x11, AC_VERB_SET_PROC_STATE, 1},
1113 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1114 {0x11, AC_VERB_SET_PROC_COEF,
1115 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1116 | 0x0040 /* Mute DACs on FIFO error */
1117 | 0x1000 /* Enable DACs High Pass Filter */
1118 | 0x0400 /* Disable Coefficient Auto increment */
1119 )},
Takashi Iwai829e87e2012-11-23 08:25:25 +01001120 /* ADC1/2 - Digital and Analog Soft Ramp */
1121 {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
1122 {0x11, AC_VERB_SET_PROC_COEF, 0x000a},
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001123 /* Beep */
Alexander Stein5a83b4b2012-11-01 13:42:37 +01001124 {0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001125 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1126
1127 {} /* terminator */
1128};
1129
Brian Austina769cbc2010-09-07 14:36:22 -05001130/* Errata: CS4207 rev C0/C1/C2 Silicon
1131 *
1132 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1133 *
1134 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1135 * may be excessive (up to an additional 200 μA), which is most easily
1136 * observed while the part is being held in reset (RESET# active low).
1137 *
1138 * Root Cause: At initial powerup of the device, the logic that drives
1139 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1140 * initialized.
1141 * Certain random patterns will cause a steady leakage current in those
1142 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1143 *
1144 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1145 * blocks, which will alleviate the issue.
1146 */
1147
Takashi Iwaic42d4782011-05-02 11:36:09 +02001148static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001149 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1150 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1151
1152 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1153 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1154 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1155 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1156 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1157 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1158
1159 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1160 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1161
1162 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1163 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1164 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1165 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1166 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1167 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1168 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1169
Takashi Iwai38c07642011-03-03 14:54:19 +01001170#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001171 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1172 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1173 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001174#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001175
1176 {} /* terminator */
1177};
1178
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001179/* SPDIF setup */
1180static void init_digital(struct hda_codec *codec)
1181{
1182 unsigned int coef;
1183
1184 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1185 coef |= 0x0008; /* Replace with mute on error */
1186 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1187 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1188 * SPDIF_OUT2 is shared with GPIO1 and
1189 * DMIC_SDA2.
1190 */
1191 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001192}
1193
1194static int cs_init(struct hda_codec *codec)
1195{
1196 struct cs_spec *spec = codec->spec;
1197
Brian Austina769cbc2010-09-07 14:36:22 -05001198 /* init_verb sequence for C0/C1/C2 errata*/
1199 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1200
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001201 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001202
Takashi Iwai98415ea2012-11-23 08:26:58 +01001203 snd_hda_gen_apply_verbs(codec);
1204
Takashi Iwaied208252009-07-07 09:04:26 +02001205 if (spec->gpio_mask) {
1206 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1207 spec->gpio_mask);
1208 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1209 spec->gpio_dir);
1210 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1211 spec->gpio_data);
1212 }
1213
Takashi Iwaie5f14242009-07-01 18:11:44 +02001214 init_output(codec);
1215 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001216 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001217
Takashi Iwaie5f14242009-07-01 18:11:44 +02001218 return 0;
1219}
1220
1221static int cs_build_controls(struct hda_codec *codec)
1222{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001223 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001224 int err;
1225
1226 err = build_output(codec);
1227 if (err < 0)
1228 return err;
1229 err = build_input(codec);
1230 if (err < 0)
1231 return err;
1232 err = build_digital_output(codec);
1233 if (err < 0)
1234 return err;
1235 err = build_digital_input(codec);
1236 if (err < 0)
1237 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001238 err = cs_init(codec);
1239 if (err < 0)
1240 return err;
1241
1242 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1243 if (err < 0)
1244 return err;
1245
1246 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001247}
1248
1249static void cs_free(struct hda_codec *codec)
1250{
1251 struct cs_spec *spec = codec->spec;
1252 kfree(spec->capture_bind[0]);
1253 kfree(spec->capture_bind[1]);
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001254 snd_hda_gen_free(&spec->gen);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001255 kfree(codec->spec);
1256}
1257
Takashi Iwaic42d4782011-05-02 11:36:09 +02001258static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001259 .build_controls = cs_build_controls,
1260 .build_pcms = cs_build_pcms,
1261 .init = cs_init,
1262 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +02001263 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwaie5f14242009-07-01 18:11:44 +02001264};
1265
1266static int cs_parse_auto_config(struct hda_codec *codec)
1267{
1268 struct cs_spec *spec = codec->spec;
1269 int err;
1270
1271 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1272 if (err < 0)
1273 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001274
1275 err = parse_output(codec);
1276 if (err < 0)
1277 return err;
1278 err = parse_input(codec);
1279 if (err < 0)
1280 return err;
1281 err = parse_digital_output(codec);
1282 if (err < 0)
1283 return err;
1284 err = parse_digital_input(codec);
1285 if (err < 0)
1286 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001287 return 0;
1288}
1289
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001290static const struct hda_model_fixup cs420x_models[] = {
1291 { .id = CS420X_MBP53, .name = "mbp53" },
1292 { .id = CS420X_MBP55, .name = "mbp55" },
1293 { .id = CS420X_IMAC27, .name = "imac27" },
1294 { .id = CS420X_IMAC27_122, .name = "imac27_122" },
1295 { .id = CS420X_APPLE, .name = "apple" },
Takashi Iwaief596a52012-09-11 16:53:08 +02001296 { .id = CS420X_MBP101, .name = "mbp101" },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001297 {}
Takashi Iwaia6bae202009-07-06 15:15:22 +02001298};
1299
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001300static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001301 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001302 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001303 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001304 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001305 /* this conflicts with too many other models */
1306 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001307
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001308 /* codec SSID */
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001309 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwaief596a52012-09-11 16:53:08 +02001310 SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001311 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001312 {} /* terminator */
1313};
1314
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001315static const struct hda_pintbl mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001316 { 0x09, 0x012b4050 },
1317 { 0x0a, 0x90100141 },
1318 { 0x0b, 0x90100140 },
1319 { 0x0c, 0x018b3020 },
1320 { 0x0d, 0x90a00110 },
1321 { 0x0e, 0x400000f0 },
1322 { 0x0f, 0x01cbe030 },
1323 { 0x10, 0x014be060 },
1324 { 0x12, 0x400000f0 },
1325 { 0x15, 0x400000f0 },
1326 {} /* terminator */
1327};
1328
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001329static const struct hda_pintbl mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001330 { 0x09, 0x012b4030 },
1331 { 0x0a, 0x90100121 },
1332 { 0x0b, 0x90100120 },
1333 { 0x0c, 0x400000f0 },
1334 { 0x0d, 0x90a00110 },
1335 { 0x0e, 0x400000f0 },
1336 { 0x0f, 0x400000f0 },
1337 { 0x10, 0x014be040 },
1338 { 0x12, 0x400000f0 },
1339 { 0x15, 0x400000f0 },
1340 {} /* terminator */
1341};
1342
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001343static const struct hda_pintbl imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001344 { 0x09, 0x012b4050 },
1345 { 0x0a, 0x90100140 },
1346 { 0x0b, 0x90100142 },
1347 { 0x0c, 0x018b3020 },
1348 { 0x0d, 0x90a00110 },
1349 { 0x0e, 0x400000f0 },
1350 { 0x0f, 0x01cbe030 },
1351 { 0x10, 0x014be060 },
1352 { 0x12, 0x01ab9070 },
1353 { 0x15, 0x400000f0 },
1354 {} /* terminator */
1355};
1356
Takashi Iwaief596a52012-09-11 16:53:08 +02001357static const struct hda_pintbl mbp101_pincfgs[] = {
1358 { 0x0d, 0x40ab90f0 },
1359 { 0x0e, 0x90a600f0 },
1360 { 0x12, 0x50a600f0 },
1361 {} /* terminator */
1362};
1363
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001364static void cs420x_fixup_gpio_13(struct hda_codec *codec,
1365 const struct hda_fixup *fix, int action)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001366{
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001367 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1368 struct cs_spec *spec = codec->spec;
1369 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1370 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1371 spec->gpio_mask = spec->gpio_dir =
1372 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1373 }
Takashi Iwaia6bae202009-07-06 15:15:22 +02001374}
1375
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001376static void cs420x_fixup_gpio_23(struct hda_codec *codec,
1377 const struct hda_fixup *fix, int action)
1378{
1379 if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1380 struct cs_spec *spec = codec->spec;
1381 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1382 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1383 spec->gpio_mask = spec->gpio_dir =
1384 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1385 }
1386}
1387
1388static const struct hda_fixup cs420x_fixups[] = {
1389 [CS420X_MBP53] = {
1390 .type = HDA_FIXUP_PINS,
1391 .v.pins = mbp53_pincfgs,
1392 .chained = true,
1393 .chain_id = CS420X_APPLE,
1394 },
1395 [CS420X_MBP55] = {
1396 .type = HDA_FIXUP_PINS,
1397 .v.pins = mbp55_pincfgs,
1398 .chained = true,
1399 .chain_id = CS420X_GPIO_13,
1400 },
1401 [CS420X_IMAC27] = {
1402 .type = HDA_FIXUP_PINS,
1403 .v.pins = imac27_pincfgs,
1404 .chained = true,
1405 .chain_id = CS420X_GPIO_13,
1406 },
1407 [CS420X_GPIO_13] = {
1408 .type = HDA_FIXUP_FUNC,
1409 .v.func = cs420x_fixup_gpio_13,
1410 },
1411 [CS420X_GPIO_23] = {
1412 .type = HDA_FIXUP_FUNC,
1413 .v.func = cs420x_fixup_gpio_23,
1414 },
Takashi Iwaief596a52012-09-11 16:53:08 +02001415 [CS420X_MBP101] = {
1416 .type = HDA_FIXUP_PINS,
1417 .v.pins = mbp101_pincfgs,
1418 .chained = true,
Takashi Iwaief596a52012-09-11 16:53:08 +02001419 .chain_id = CS420X_GPIO_13,
1420 },
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001421};
1422
Takashi Iwaie5f14242009-07-01 18:11:44 +02001423static int patch_cs420x(struct hda_codec *codec)
1424{
1425 struct cs_spec *spec;
1426 int err;
1427
1428 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1429 if (!spec)
1430 return -ENOMEM;
1431 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001432 snd_hda_gen_init(&spec->gen);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001433
Tim Howe56487c22011-07-22 16:41:00 -05001434 spec->vendor_nid = CS420X_VENDOR_NID;
1435
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001436 snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
1437 cs420x_fixups);
1438 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001439
Takashi Iwaied208252009-07-07 09:04:26 +02001440 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001441 if (err < 0)
1442 goto error;
1443
Takashi Iwaie5f14242009-07-01 18:11:44 +02001444 codec->patch_ops = cs_patch_ops;
1445
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001446 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1447
Takashi Iwaie5f14242009-07-01 18:11:44 +02001448 return 0;
1449
1450 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02001451 cs_free(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001452 codec->spec = NULL;
1453 return err;
1454}
1455
Tim Howe56487c22011-07-22 16:41:00 -05001456/*
1457 * Cirrus Logic CS4210
1458 *
1459 * 1 DAC => HP(sense) / Speakers,
1460 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1461 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1462*/
1463
1464/* CS4210 board names */
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001465static const struct hda_model_fixup cs421x_models[] = {
1466 { .id = CS421X_CDB4210, .name = "cdb4210" },
1467 {}
Tim Howe56487c22011-07-22 16:41:00 -05001468};
1469
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001470static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001471 /* Test Intel board + CDB2410 */
1472 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1473 {} /* terminator */
1474};
1475
1476/* CS4210 board pinconfigs */
1477/* Default CS4210 (CDB4210)*/
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001478static const struct hda_pintbl cdb4210_pincfgs[] = {
Tim Howe56487c22011-07-22 16:41:00 -05001479 { 0x05, 0x0321401f },
1480 { 0x06, 0x90170010 },
1481 { 0x07, 0x03813031 },
1482 { 0x08, 0xb7a70037 },
1483 { 0x09, 0xb7a6003e },
1484 { 0x0a, 0x034510f0 },
1485 {} /* terminator */
1486};
1487
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001488/* Setup GPIO/SENSE for each board (if used) */
1489static void cs421x_fixup_sense_b(struct hda_codec *codec,
1490 const struct hda_fixup *fix, int action)
1491{
1492 struct cs_spec *spec = codec->spec;
1493 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1494 spec->sense_b = 1;
1495}
1496
1497static const struct hda_fixup cs421x_fixups[] = {
1498 [CS421X_CDB4210] = {
1499 .type = HDA_FIXUP_PINS,
1500 .v.pins = cdb4210_pincfgs,
1501 .chained = true,
1502 .chain_id = CS421X_SENSE_B,
1503 },
1504 [CS421X_SENSE_B] = {
1505 .type = HDA_FIXUP_FUNC,
1506 .v.func = cs421x_fixup_sense_b,
1507 }
Tim Howe56487c22011-07-22 16:41:00 -05001508};
1509
1510static const struct hda_verb cs421x_coef_init_verbs[] = {
1511 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1512 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1513 /*
1514 Disable Coefficient Index Auto-Increment(DAI)=1,
1515 PDREF=0
1516 */
1517 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1518
1519 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1520 /* ADC SZCMode = Digital Soft Ramp */
1521 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1522
1523 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1524 {0x0B, AC_VERB_SET_PROC_COEF,
1525 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1526 | 0x0004 /* Mute DAC on FIFO error */
1527 | 0x0008 /* Enable DAC High Pass Filter */
1528 )},
1529 {} /* terminator */
1530};
1531
1532/* Errata: CS4210 rev A1 Silicon
1533 *
1534 * http://www.cirrus.com/en/pubs/errata/
1535 *
1536 * Description:
1537 * 1. Performance degredation is present in the ADC.
1538 * 2. Speaker output is not completely muted upon HP detect.
1539 * 3. Noise is present when clipping occurs on the amplified
1540 * speaker outputs.
1541 *
1542 * Workaround:
1543 * The following verb sequence written to the registers during
1544 * initialization will correct the issues listed above.
1545 */
1546
1547static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1548 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1549
1550 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1551 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1552
1553 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1554 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1555
1556 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1557 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1558
1559 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1560 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1561
1562 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1563 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1564
1565 {} /* terminator */
1566};
1567
1568/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1569static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1570
1571static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1572 struct snd_ctl_elem_info *uinfo)
1573{
1574 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1575 uinfo->count = 1;
1576 uinfo->value.integer.min = 0;
1577 uinfo->value.integer.max = 3;
1578 return 0;
1579}
1580
1581static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1582 struct snd_ctl_elem_value *ucontrol)
1583{
1584 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1585
1586 ucontrol->value.integer.value[0] =
1587 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1588 return 0;
1589}
1590
1591static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1592 struct snd_ctl_elem_value *ucontrol)
1593{
1594 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1595
1596 unsigned int vol = ucontrol->value.integer.value[0];
1597 unsigned int coef =
1598 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1599 unsigned int original_coef = coef;
1600
1601 coef &= ~0x0003;
1602 coef |= (vol & 0x0003);
1603 if (original_coef == coef)
1604 return 0;
1605 else {
1606 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1607 return 1;
1608 }
1609}
1610
1611static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1612
1613 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1614 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1615 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1616 .name = "Speaker Boost Playback Volume",
1617 .info = cs421x_boost_vol_info,
1618 .get = cs421x_boost_vol_get,
1619 .put = cs421x_boost_vol_put,
1620 .tlv = { .p = cs421x_speaker_boost_db_scale },
1621};
1622
David Henningsson5660ffd2012-01-02 12:40:17 +01001623static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001624{
1625 struct cs_spec *spec = codec->spec;
1626 unsigned int def_conf, coef;
1627
1628 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1629 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1630
1631 if (spec->gpio_mask)
1632 coef |= 0x0008; /* B1,B2 are GPIOs */
1633 else
1634 coef &= ~0x0008;
1635
1636 if (spec->sense_b)
1637 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1638 else
1639 coef &= ~0x0010;
1640
1641 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1642
1643 if ((spec->gpio_mask || spec->sense_b) &&
1644 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1645
1646 /*
1647 GPIO or SENSE_B forced - disconnect the DMIC pin.
1648 */
1649 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1650 def_conf &= ~AC_DEFCFG_PORT_CONN;
1651 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1652 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1653 }
1654}
1655
1656static void init_cs421x_digital(struct hda_codec *codec)
1657{
1658 struct cs_spec *spec = codec->spec;
1659 struct auto_pin_cfg *cfg = &spec->autocfg;
1660 int i;
1661
1662
1663 for (i = 0; i < cfg->dig_outs; i++) {
1664 hda_nid_t nid = cfg->dig_out_pins[i];
1665 if (!cfg->speaker_outs)
1666 continue;
1667 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
David Henningsson5c2e4e02012-10-08 15:44:15 +02001668 snd_hda_jack_detect_enable_callback(codec, nid, SPDIF_EVENT, cs_automute);
Tim Howe56487c22011-07-22 16:41:00 -05001669 spec->spdif_detect = 1;
1670 }
1671 }
1672}
1673
1674static int cs421x_init(struct hda_codec *codec)
1675{
1676 struct cs_spec *spec = codec->spec;
1677
David Henningsson5660ffd2012-01-02 12:40:17 +01001678 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1679 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1680 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1681 cs4210_pinmux_init(codec);
1682 }
Tim Howe56487c22011-07-22 16:41:00 -05001683
1684 if (spec->gpio_mask) {
1685 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1686 spec->gpio_mask);
1687 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1688 spec->gpio_dir);
1689 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1690 spec->gpio_data);
1691 }
1692
1693 init_output(codec);
1694 init_input(codec);
1695 init_cs421x_digital(codec);
1696
1697 return 0;
1698}
1699
1700/*
1701 * CS4210 Input MUX (1 ADC)
1702 */
1703static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_info *uinfo)
1705{
1706 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1707 struct cs_spec *spec = codec->spec;
1708
1709 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1710}
1711
1712static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1713 struct snd_ctl_elem_value *ucontrol)
1714{
1715 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1716 struct cs_spec *spec = codec->spec;
1717
1718 ucontrol->value.enumerated.item[0] = spec->cur_input;
1719 return 0;
1720}
1721
1722static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1723 struct snd_ctl_elem_value *ucontrol)
1724{
1725 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1726 struct cs_spec *spec = codec->spec;
1727
1728 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1729 spec->adc_nid[0], &spec->cur_input);
1730
1731}
1732
Daniel J Blueman00e17f72012-11-04 13:19:04 +08001733static const struct snd_kcontrol_new cs421x_capture_source = {
Tim Howe56487c22011-07-22 16:41:00 -05001734 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1735 .name = "Capture Source",
1736 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1737 .info = cs421x_mux_enum_info,
1738 .get = cs421x_mux_enum_get,
1739 .put = cs421x_mux_enum_put,
1740};
1741
1742static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1743{
1744 struct cs_spec *spec = codec->spec;
1745 struct auto_pin_cfg *cfg = &spec->autocfg;
1746 const struct hda_input_mux *imux = &spec->input_mux;
1747 hda_nid_t pin = cfg->inputs[item].pin;
1748 struct snd_kcontrol *kctl;
1749 u32 caps;
1750
1751 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1752 return 0;
1753
1754 caps = query_amp_caps(codec, pin, HDA_INPUT);
1755 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1756 if (caps <= 1)
1757 return 0;
1758
1759 return add_volume(codec, imux->items[item].label, 0,
1760 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1761}
1762
1763/* add a (input-boost) volume control to the given input pin */
1764static int build_cs421x_input(struct hda_codec *codec)
1765{
1766 struct cs_spec *spec = codec->spec;
1767 struct auto_pin_cfg *cfg = &spec->autocfg;
1768 struct hda_input_mux *imux = &spec->input_mux;
1769 int i, err, type_idx;
1770 const char *label;
1771
1772 if (!spec->num_inputs)
1773 return 0;
1774
1775 /* make bind-capture */
1776 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1777 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1778 for (i = 0; i < 2; i++) {
1779 struct snd_kcontrol *kctl;
1780 int n;
1781 if (!spec->capture_bind[i])
1782 return -ENOMEM;
1783 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1784 if (!kctl)
1785 return -ENOMEM;
1786 kctl->private_value = (long)spec->capture_bind[i];
1787 err = snd_hda_ctl_add(codec, 0, kctl);
1788 if (err < 0)
1789 return err;
1790 for (n = 0; n < AUTO_PIN_LAST; n++) {
1791 if (!spec->adc_nid[n])
1792 continue;
1793 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1794 if (err < 0)
1795 return err;
1796 }
1797 }
1798
1799 /* Add Input MUX Items + Capture Volume/Switch */
1800 for (i = 0; i < spec->num_inputs; i++) {
1801 label = hda_get_autocfg_input_label(codec, cfg, i);
1802 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1803
1804 err = cs421x_add_input_volume_control(codec, i);
1805 if (err < 0)
1806 return err;
1807 }
1808
1809 /*
1810 Add 'Capture Source' Switch if
1811 * 2 inputs and no mic detec
1812 * 3 inputs
1813 */
1814 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1815 (spec->num_inputs == 3)) {
1816
1817 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1818 snd_ctl_new1(&cs421x_capture_source, codec));
1819 if (err < 0)
1820 return err;
1821 }
1822
1823 return 0;
1824}
1825
1826/* Single DAC (Mute/Gain) */
1827static int build_cs421x_output(struct hda_codec *codec)
1828{
1829 hda_nid_t dac = CS4210_DAC_NID;
1830 struct cs_spec *spec = codec->spec;
1831 struct auto_pin_cfg *cfg = &spec->autocfg;
1832 struct snd_kcontrol *kctl;
1833 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001834 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001835
1836 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001837
1838 err = add_mute(codec, name, 0,
1839 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1840 if (err < 0)
1841 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001842
1843 err = add_volume(codec, name, 0,
1844 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1845 if (err < 0)
1846 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001847
David Henningsson5660ffd2012-01-02 12:40:17 +01001848 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001849 err = snd_hda_ctl_add(codec, 0,
1850 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1851 if (err < 0)
1852 return err;
1853 }
1854 return err;
1855}
1856
1857static int cs421x_build_controls(struct hda_codec *codec)
1858{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001859 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001860 int err;
1861
1862 err = build_cs421x_output(codec);
1863 if (err < 0)
1864 return err;
1865 err = build_cs421x_input(codec);
1866 if (err < 0)
1867 return err;
1868 err = build_digital_output(codec);
1869 if (err < 0)
1870 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001871 err = cs421x_init(codec);
1872 if (err < 0)
1873 return err;
1874
1875 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1876 if (err < 0)
1877 return err;
1878
1879 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001880}
1881
Tim Howe56487c22011-07-22 16:41:00 -05001882static int parse_cs421x_input(struct hda_codec *codec)
1883{
1884 struct cs_spec *spec = codec->spec;
1885 struct auto_pin_cfg *cfg = &spec->autocfg;
1886 int i;
1887
1888 for (i = 0; i < cfg->num_inputs; i++) {
1889 hda_nid_t pin = cfg->inputs[i].pin;
1890 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1891 spec->cur_input = spec->last_input = i;
1892 spec->num_inputs++;
1893
1894 /* check whether the automatic mic switch is available */
1895 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1896 spec->mic_detect = 1;
1897 spec->automic_idx = i;
1898 }
1899 }
1900 return 0;
1901}
1902
1903static int cs421x_parse_auto_config(struct hda_codec *codec)
1904{
1905 struct cs_spec *spec = codec->spec;
1906 int err;
1907
1908 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1909 if (err < 0)
1910 return err;
1911 err = parse_output(codec);
1912 if (err < 0)
1913 return err;
1914 err = parse_cs421x_input(codec);
1915 if (err < 0)
1916 return err;
1917 err = parse_digital_output(codec);
1918 if (err < 0)
1919 return err;
1920 return 0;
1921}
1922
1923#ifdef CONFIG_PM
1924/*
1925 Manage PDREF, when transitioning to D3hot
1926 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1927*/
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001928static int cs421x_suspend(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001929{
David Henningsson5660ffd2012-01-02 12:40:17 +01001930 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001931 unsigned int coef;
1932
1933 snd_hda_shutup_pins(codec);
1934
1935 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1936 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1937 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1938 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1939
David Henningsson5660ffd2012-01-02 12:40:17 +01001940 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1941 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1942 coef |= 0x0004; /* PDREF */
1943 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1944 }
Tim Howe56487c22011-07-22 16:41:00 -05001945
1946 return 0;
1947}
1948#endif
1949
Daniel J Blueman00e17f72012-11-04 13:19:04 +08001950static const struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001951 .build_controls = cs421x_build_controls,
1952 .build_pcms = cs_build_pcms,
1953 .init = cs421x_init,
1954 .free = cs_free,
David Henningsson5c2e4e02012-10-08 15:44:15 +02001955 .unsol_event = snd_hda_jack_unsol_event,
Tim Howe56487c22011-07-22 16:41:00 -05001956#ifdef CONFIG_PM
1957 .suspend = cs421x_suspend,
1958#endif
1959};
1960
David Henningsson5660ffd2012-01-02 12:40:17 +01001961static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001962{
1963 struct cs_spec *spec;
1964 int err;
1965
1966 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1967 if (!spec)
1968 return -ENOMEM;
1969 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02001970 snd_hda_gen_init(&spec->gen);
Tim Howe56487c22011-07-22 16:41:00 -05001971
David Henningsson5660ffd2012-01-02 12:40:17 +01001972 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001973
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001974 snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
1975 cs421x_fixups);
1976 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
Tim Howe56487c22011-07-22 16:41:00 -05001977
1978 /*
1979 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1980 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1981 is disabled.
1982 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001983 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001984
1985 err = cs421x_parse_auto_config(codec);
1986 if (err < 0)
1987 goto error;
1988
David Henningsson5660ffd2012-01-02 12:40:17 +01001989 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05001990
Takashi Iwaib35aabd2012-08-24 19:09:45 +02001991 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1992
Tim Howe56487c22011-07-22 16:41:00 -05001993 return 0;
1994
1995 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02001996 cs_free(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001997 codec->spec = NULL;
1998 return err;
1999}
2000
David Henningsson5660ffd2012-01-02 12:40:17 +01002001static int patch_cs4213(struct hda_codec *codec)
2002{
2003 struct cs_spec *spec;
2004 int err;
2005
2006 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2007 if (!spec)
2008 return -ENOMEM;
2009 codec->spec = spec;
Takashi Iwaib2cbf3b2012-10-10 08:53:06 +02002010 snd_hda_gen_init(&spec->gen);
David Henningsson5660ffd2012-01-02 12:40:17 +01002011
2012 spec->vendor_nid = CS4213_VENDOR_NID;
2013
2014 err = cs421x_parse_auto_config(codec);
2015 if (err < 0)
2016 goto error;
2017
2018 codec->patch_ops = cs421x_patch_ops;
2019 return 0;
2020
2021 error:
Takashi Iwaic5e0b6d2012-10-10 08:50:35 +02002022 cs_free(codec);
David Henningsson5660ffd2012-01-02 12:40:17 +01002023 codec->spec = NULL;
2024 return err;
2025}
2026
Takashi Iwaie5f14242009-07-01 18:11:44 +02002027
2028/*
2029 * patch entries
2030 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002031static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002032 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2033 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002034 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2035 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002036 {} /* terminator */
2037};
2038
2039MODULE_ALIAS("snd-hda-codec-id:10134206");
2040MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002041MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002042MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002043
2044MODULE_LICENSE("GPL");
2045MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2046
2047static struct hda_codec_preset_list cirrus_list = {
2048 .preset = snd_hda_preset_cirrus,
2049 .owner = THIS_MODULE,
2050};
2051
2052static int __init patch_cirrus_init(void)
2053{
2054 return snd_hda_add_codec_preset(&cirrus_list);
2055}
2056
2057static void __exit patch_cirrus_exit(void)
2058{
2059 snd_hda_delete_codec_preset(&cirrus_list);
2060}
2061
2062module_init(patch_cirrus_init)
2063module_exit(patch_cirrus_exit)