blob: 778e4b9dd88c999ef9d9761c5beaa0441b1f7427 [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 Iwai1835a0f2011-10-27 22:12:46 +020029#include "hda_jack.h"
Tim Howe56487c22011-07-22 16:41:00 -050030#include <sound/tlv.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020031
32/*
33 */
34
35struct cs_spec {
Takashi Iwaia6bae202009-07-06 15:15:22 +020036 int board_config;
Takashi Iwaie5f14242009-07-01 18:11:44 +020037 struct auto_pin_cfg autocfg;
38 struct hda_multi_out multiout;
39 struct snd_kcontrol *vmaster_sw;
40 struct snd_kcontrol *vmaster_vol;
41
42 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
43 hda_nid_t slave_dig_outs[2];
44
45 unsigned int input_idx[AUTO_PIN_LAST];
46 unsigned int capsrc_idx[AUTO_PIN_LAST];
47 hda_nid_t adc_nid[AUTO_PIN_LAST];
48 unsigned int adc_idx[AUTO_PIN_LAST];
49 unsigned int num_inputs;
50 unsigned int cur_input;
51 unsigned int automic_idx;
52 hda_nid_t cur_adc;
53 unsigned int cur_adc_stream_tag;
54 unsigned int cur_adc_format;
55 hda_nid_t dig_in;
56
Takashi Iwaic42d4782011-05-02 11:36:09 +020057 const struct hda_bind_ctls *capture_bind[2];
Takashi Iwaie5f14242009-07-01 18:11:44 +020058
Takashi Iwaied208252009-07-07 09:04:26 +020059 unsigned int gpio_mask;
60 unsigned int gpio_dir;
61 unsigned int gpio_data;
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010062 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
63 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
Takashi Iwaied208252009-07-07 09:04:26 +020064
Takashi Iwaie5f14242009-07-01 18:11:44 +020065 struct hda_pcm pcm_rec[2]; /* PCM information */
66
67 unsigned int hp_detect:1;
68 unsigned int mic_detect:1;
Tim Howe56487c22011-07-22 16:41:00 -050069 /* CS421x */
70 unsigned int spdif_detect:1;
71 unsigned int sense_b:1;
72 hda_nid_t vendor_nid;
73 struct hda_input_mux input_mux;
74 unsigned int last_input;
Takashi Iwaie5f14242009-07-01 18:11:44 +020075};
76
Tim Howe56487c22011-07-22 16:41:00 -050077/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020078enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040079 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020080 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010081 CS420X_IMAC27,
Jérémy Lal7e5bea12012-01-09 17:19:45 +010082 CS420X_IMAC27_122,
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010083 CS420X_APPLE,
Takashi Iwaia6bae202009-07-06 15:15:22 +020084 CS420X_AUTO,
85 CS420X_MODELS
86};
87
Tim Howe56487c22011-07-22 16:41:00 -050088/* CS421x boards */
89enum {
90 CS421X_CDB4210,
91 CS421X_MODELS
92};
93
Takashi Iwai40c20fa2009-07-06 13:00:57 +020094/* Vendor-specific processing widget */
95#define CS420X_VENDOR_NID 0x11
96#define CS_DIG_OUT1_PIN_NID 0x10
97#define CS_DIG_OUT2_PIN_NID 0x15
98#define CS_DMIC1_PIN_NID 0x12
99#define CS_DMIC2_PIN_NID 0x0e
100
101/* coef indices */
102#define IDX_SPDIF_STAT 0x0000
103#define IDX_SPDIF_CTL 0x0001
104#define IDX_ADC_CFG 0x0002
105/* SZC bitmask, 4 modes below:
106 * 0 = immediate,
107 * 1 = digital immediate, analog zero-cross
108 * 2 = digtail & analog soft-ramp
109 * 3 = digital soft-ramp, analog zero-cross
110 */
111#define CS_COEF_ADC_SZC_MASK (3 << 0)
112#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
113#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
114/* PGA mode: 0 = differential, 1 = signle-ended */
115#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
116#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
117#define IDX_DAC_CFG 0x0003
118/* SZC bitmask, 4 modes below:
119 * 0 = Immediate
120 * 1 = zero-cross
121 * 2 = soft-ramp
122 * 3 = soft-ramp on zero-cross
123 */
124#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
125#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
126#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
127
128#define IDX_BEEP_CFG 0x0004
129/* 0x0008 - test reg key */
130/* 0x0009 - 0x0014 -> 12 test regs */
131/* 0x0015 - visibility reg */
132
Tim Howe56487c22011-07-22 16:41:00 -0500133/*
134 * Cirrus Logic CS4210
135 *
136 * 1 DAC => HP(sense) / Speakers,
137 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
138 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
139*/
140#define CS4210_DAC_NID 0x02
141#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100142#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500143#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
144#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
145
146#define CS421X_IDX_DEV_CFG 0x01
147#define CS421X_IDX_ADC_CFG 0x02
148#define CS421X_IDX_DAC_CFG 0x03
149#define CS421X_IDX_SPK_CTL 0x04
150
151#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200152
David Henningsson5660ffd2012-01-02 12:40:17 +0100153/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
154#define CS4213_VENDOR_NID 0x09
155
156
Takashi Iwai277a57c2009-07-08 12:42:08 +0200157static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200158{
Tim Howe56487c22011-07-22 16:41:00 -0500159 struct cs_spec *spec = codec->spec;
160 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200161 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500162 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200163 AC_VERB_GET_PROC_COEF, 0);
164}
165
Takashi Iwai277a57c2009-07-08 12:42:08 +0200166static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
167 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200168{
Tim Howe56487c22011-07-22 16:41:00 -0500169 struct cs_spec *spec = codec->spec;
170 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200171 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500172 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200173 AC_VERB_SET_PROC_COEF, coef);
174}
175
176
Takashi Iwaie5f14242009-07-01 18:11:44 +0200177#define HP_EVENT 1
178#define MIC_EVENT 2
179
180/*
181 * PCM callbacks
182 */
183static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
184 struct hda_codec *codec,
185 struct snd_pcm_substream *substream)
186{
187 struct cs_spec *spec = codec->spec;
188 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
189 hinfo);
190}
191
192static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
193 struct hda_codec *codec,
194 unsigned int stream_tag,
195 unsigned int format,
196 struct snd_pcm_substream *substream)
197{
198 struct cs_spec *spec = codec->spec;
199 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
200 stream_tag, format, substream);
201}
202
203static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
204 struct hda_codec *codec,
205 struct snd_pcm_substream *substream)
206{
207 struct cs_spec *spec = codec->spec;
208 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
209}
210
211/*
212 * Digital out
213 */
214static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
215 struct hda_codec *codec,
216 struct snd_pcm_substream *substream)
217{
218 struct cs_spec *spec = codec->spec;
219 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
220}
221
222static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
223 struct hda_codec *codec,
224 struct snd_pcm_substream *substream)
225{
226 struct cs_spec *spec = codec->spec;
227 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
228}
229
230static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
231 struct hda_codec *codec,
232 unsigned int stream_tag,
233 unsigned int format,
234 struct snd_pcm_substream *substream)
235{
236 struct cs_spec *spec = codec->spec;
237 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
238 format, substream);
239}
240
241static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
242 struct hda_codec *codec,
243 struct snd_pcm_substream *substream)
244{
245 struct cs_spec *spec = codec->spec;
246 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
247}
248
Takashi Iwai05ee7962011-11-16 18:05:11 +0100249static void cs_update_input_select(struct hda_codec *codec)
250{
251 struct cs_spec *spec = codec->spec;
252 if (spec->cur_adc)
253 snd_hda_codec_write(codec, spec->cur_adc, 0,
254 AC_VERB_SET_CONNECT_SEL,
255 spec->adc_idx[spec->cur_input]);
256}
257
Takashi Iwaie5f14242009-07-01 18:11:44 +0200258/*
259 * Analog capture
260 */
261static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
262 struct hda_codec *codec,
263 unsigned int stream_tag,
264 unsigned int format,
265 struct snd_pcm_substream *substream)
266{
267 struct cs_spec *spec = codec->spec;
268 spec->cur_adc = spec->adc_nid[spec->cur_input];
269 spec->cur_adc_stream_tag = stream_tag;
270 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100271 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200272 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
273 return 0;
274}
275
276static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
277 struct hda_codec *codec,
278 struct snd_pcm_substream *substream)
279{
280 struct cs_spec *spec = codec->spec;
281 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
282 spec->cur_adc = 0;
283 return 0;
284}
285
286/*
287 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200288static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200289 .substreams = 1,
290 .channels_min = 2,
291 .channels_max = 2,
292 .ops = {
293 .open = cs_playback_pcm_open,
294 .prepare = cs_playback_pcm_prepare,
295 .cleanup = cs_playback_pcm_cleanup
296 },
297};
298
Takashi Iwaic42d4782011-05-02 11:36:09 +0200299static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200300 .substreams = 1,
301 .channels_min = 2,
302 .channels_max = 2,
303 .ops = {
304 .prepare = cs_capture_pcm_prepare,
305 .cleanup = cs_capture_pcm_cleanup
306 },
307};
308
Takashi Iwaic42d4782011-05-02 11:36:09 +0200309static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200310 .substreams = 1,
311 .channels_min = 2,
312 .channels_max = 2,
313 .ops = {
314 .open = cs_dig_playback_pcm_open,
315 .close = cs_dig_playback_pcm_close,
316 .prepare = cs_dig_playback_pcm_prepare,
317 .cleanup = cs_dig_playback_pcm_cleanup
318 },
319};
320
Takashi Iwaic42d4782011-05-02 11:36:09 +0200321static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200322 .substreams = 1,
323 .channels_min = 2,
324 .channels_max = 2,
325};
326
327static int cs_build_pcms(struct hda_codec *codec)
328{
329 struct cs_spec *spec = codec->spec;
330 struct hda_pcm *info = spec->pcm_rec;
331
332 codec->pcm_info = info;
333 codec->num_pcms = 0;
334
335 info->name = "Cirrus Analog";
336 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
337 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
338 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
339 spec->multiout.max_channels;
340 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200341 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
342 spec->adc_nid[spec->cur_input];
343 codec->num_pcms++;
344
345 if (!spec->multiout.dig_out_nid && !spec->dig_in)
346 return 0;
347
348 info++;
349 info->name = "Cirrus Digital";
350 info->pcm_type = spec->autocfg.dig_out_type[0];
351 if (!info->pcm_type)
352 info->pcm_type = HDA_PCM_TYPE_SPDIF;
353 if (spec->multiout.dig_out_nid) {
354 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
355 cs_pcm_digital_playback;
356 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
357 spec->multiout.dig_out_nid;
358 }
359 if (spec->dig_in) {
360 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
361 cs_pcm_digital_capture;
362 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
363 }
364 codec->num_pcms++;
365
366 return 0;
367}
368
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200369/*
370 * parse codec topology
371 */
372
Takashi Iwaie5f14242009-07-01 18:11:44 +0200373static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
374{
375 hda_nid_t dac;
376 if (!pin)
377 return 0;
378 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
379 return 0;
380 return dac;
381}
382
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200383static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
384{
385 struct cs_spec *spec = codec->spec;
386 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200387 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200388 unsigned int val;
389 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200390 return 0;
391 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200392 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200393}
394
395static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
396 unsigned int *idxp)
397{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200398 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200399 hda_nid_t nid;
400
401 nid = codec->start_nid;
402 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200403 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200404 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200405 if (type != AC_WID_AUD_IN)
406 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200407 idx = snd_hda_get_conn_index(codec, nid, pin, false);
408 if (idx >= 0) {
409 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200410 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200411 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200412 }
413 return 0;
414}
415
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200416static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
417{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200418 unsigned int val;
419 val = snd_hda_codec_get_pincfg(codec, nid);
420 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
421}
422
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200423static int parse_output(struct hda_codec *codec)
424{
425 struct cs_spec *spec = codec->spec;
426 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200427 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200428 hda_nid_t dac;
429
430 for (i = 0; i < cfg->line_outs; i++) {
431 dac = get_dac(codec, cfg->line_out_pins[i]);
432 if (!dac)
433 break;
434 spec->dac_nid[i] = dac;
435 }
436 spec->multiout.num_dacs = i;
437 spec->multiout.dac_nids = spec->dac_nid;
438 spec->multiout.max_channels = i * 2;
439
440 /* add HP and speakers */
441 extra_nids = 0;
442 for (i = 0; i < cfg->hp_outs; i++) {
443 dac = get_dac(codec, cfg->hp_pins[i]);
444 if (!dac)
445 break;
446 if (!i)
447 spec->multiout.hp_nid = dac;
448 else
449 spec->multiout.extra_out_nid[extra_nids++] = dac;
450 }
451 for (i = 0; i < cfg->speaker_outs; i++) {
452 dac = get_dac(codec, cfg->speaker_pins[i]);
453 if (!dac)
454 break;
455 spec->multiout.extra_out_nid[extra_nids++] = dac;
456 }
457
458 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
459 cfg->speaker_outs = cfg->line_outs;
460 memcpy(cfg->speaker_pins, cfg->line_out_pins,
461 sizeof(cfg->speaker_pins));
462 cfg->line_outs = 0;
463 }
464
465 return 0;
466}
467
468static int parse_input(struct hda_codec *codec)
469{
470 struct cs_spec *spec = codec->spec;
471 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200472 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200473
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200474 for (i = 0; i < cfg->num_inputs; i++) {
475 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200476 spec->input_idx[spec->num_inputs] = i;
477 spec->capsrc_idx[i] = spec->num_inputs++;
478 spec->cur_input = i;
479 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
480 }
481 if (!spec->num_inputs)
482 return 0;
483
484 /* check whether the automatic mic switch is available */
485 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200486 cfg->inputs[0].type == AUTO_PIN_MIC &&
487 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200488 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
489 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200490 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200491 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200492 }
493 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200494 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200495 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200496 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200497 }
498 }
499 }
500 return 0;
501}
502
503
504static int parse_digital_output(struct hda_codec *codec)
505{
506 struct cs_spec *spec = codec->spec;
507 struct auto_pin_cfg *cfg = &spec->autocfg;
508 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200509
510 if (!cfg->dig_outs)
511 return 0;
512 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
513 return 0;
514 spec->multiout.dig_out_nid = nid;
515 spec->multiout.share_spdif = 1;
516 if (cfg->dig_outs > 1 &&
517 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
518 spec->slave_dig_outs[0] = nid;
519 codec->slave_dig_outs = spec->slave_dig_outs;
520 }
521 return 0;
522}
523
524static int parse_digital_input(struct hda_codec *codec)
525{
526 struct cs_spec *spec = codec->spec;
527 struct auto_pin_cfg *cfg = &spec->autocfg;
528 int idx;
529
Takashi Iwai60e53882009-07-06 15:01:09 +0200530 if (cfg->dig_in_pin)
531 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
532 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200533}
534
535/*
536 * create mixer controls
537 */
538
Takashi Iwaiea734962011-01-17 11:29:34 +0100539static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200540
541static int add_mute(struct hda_codec *codec, const char *name, int index,
542 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
543{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200544 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200545 struct snd_kcontrol_new knew =
546 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
547 knew.private_value = pval;
548 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
549 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100550 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100551 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200552}
553
554static int add_volume(struct hda_codec *codec, const char *name,
555 int index, unsigned int pval, int dir,
556 struct snd_kcontrol **kctlp)
557{
David Henningsson2e1210b2011-09-14 13:22:54 +0200558 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200559 struct snd_kcontrol_new knew =
560 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
561 knew.private_value = pval;
562 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
563 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100564 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100565 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200566}
567
568static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
569{
570 unsigned int caps;
571
572 /* set the upper-limit for mixer amp to 0dB */
573 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
574 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
575 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
576 << AC_AMPCAP_NUM_STEPS_SHIFT;
577 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
578}
579
580static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
581{
582 struct cs_spec *spec = codec->spec;
583 unsigned int tlv[4];
584 int err;
585
586 spec->vmaster_sw =
587 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100588 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200589 if (err < 0)
590 return err;
591
592 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
593 spec->vmaster_vol =
594 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100595 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200596 if (err < 0)
597 return err;
598 return 0;
599}
600
601static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
602 int num_ctls, int type)
603{
604 struct cs_spec *spec = codec->spec;
605 const char *name;
606 int err, index;
607 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200608 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200609 "Front Speaker", "Surround Speaker", "Bass Speaker"
610 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200611 static const char * const line_outs[] = {
Takashi Iwaie49a3432012-03-01 18:14:41 +0100612 "Front Line Out", "Surround Line Out", "Bass Line Out"
Takashi Iwaie5f14242009-07-01 18:11:44 +0200613 };
614
615 fix_volume_caps(codec, dac);
616 if (!spec->vmaster_sw) {
617 err = add_vmaster(codec, dac);
618 if (err < 0)
619 return err;
620 }
621
622 index = 0;
623 switch (type) {
624 case AUTO_PIN_HP_OUT:
625 name = "Headphone";
626 index = idx;
627 break;
628 case AUTO_PIN_SPEAKER_OUT:
629 if (num_ctls > 1)
630 name = speakers[idx];
631 else
632 name = "Speaker";
633 break;
634 default:
635 if (num_ctls > 1)
636 name = line_outs[idx];
637 else
Takashi Iwaie49a3432012-03-01 18:14:41 +0100638 name = "Line Out";
Takashi Iwaie5f14242009-07-01 18:11:44 +0200639 break;
640 }
641
642 err = add_mute(codec, name, index,
643 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
644 if (err < 0)
645 return err;
646 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
647 if (err < 0)
648 return err;
649
650 err = add_volume(codec, name, index,
651 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
652 if (err < 0)
653 return err;
654 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
655 if (err < 0)
656 return err;
657
658 return 0;
659}
660
661static int build_output(struct hda_codec *codec)
662{
663 struct cs_spec *spec = codec->spec;
664 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200665 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200666
667 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200668 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
669 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200670 if (err < 0)
671 return err;
672 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200673 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200674 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
675 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200676 if (err < 0)
677 return err;
678 }
679 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200680 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
681 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200682 if (err < 0)
683 return err;
684 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200685 return 0;
686}
687
688/*
689 */
690
Takashi Iwaic42d4782011-05-02 11:36:09 +0200691static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200692 HDA_BIND_SW("Capture Switch", 0),
693 HDA_BIND_VOL("Capture Volume", 0),
694};
695
Takashi Iwaiea359292009-07-06 12:58:59 +0200696static int change_cur_input(struct hda_codec *codec, unsigned int idx,
697 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200698{
699 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200700
Takashi Iwaiea359292009-07-06 12:58:59 +0200701 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200702 return 0;
703 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
704 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200705 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200706 spec->cur_adc = spec->adc_nid[idx];
707 snd_hda_codec_setup_stream(codec, spec->cur_adc,
708 spec->cur_adc_stream_tag, 0,
709 spec->cur_adc_format);
710 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200711 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100712 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200713 return 1;
714}
715
716static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
717 struct snd_ctl_elem_info *uinfo)
718{
719 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
720 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200721 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200722 unsigned int idx;
723
724 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
725 uinfo->count = 1;
726 uinfo->value.enumerated.items = spec->num_inputs;
727 if (uinfo->value.enumerated.item >= spec->num_inputs)
728 uinfo->value.enumerated.item = spec->num_inputs - 1;
729 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100730 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
731 uinfo->value.enumerated.name,
732 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200733 return 0;
734}
735
736static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
737 struct snd_ctl_elem_value *ucontrol)
738{
739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
740 struct cs_spec *spec = codec->spec;
741 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
742 return 0;
743}
744
745static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
746 struct snd_ctl_elem_value *ucontrol)
747{
748 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
749 struct cs_spec *spec = codec->spec;
750 unsigned int idx = ucontrol->value.enumerated.item[0];
751
752 if (idx >= spec->num_inputs)
753 return -EINVAL;
754 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200755 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200756}
757
Takashi Iwaic42d4782011-05-02 11:36:09 +0200758static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200759 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
760 .name = "Capture Source",
761 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
762 .info = cs_capture_source_info,
763 .get = cs_capture_source_get,
764 .put = cs_capture_source_put,
765};
766
Takashi Iwaic42d4782011-05-02 11:36:09 +0200767static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200768 struct hda_ctl_ops *ops)
769{
770 struct cs_spec *spec = codec->spec;
771 struct hda_bind_ctls *bind;
772 int i, n;
773
774 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
775 GFP_KERNEL);
776 if (!bind)
777 return NULL;
778 bind->ops = ops;
779 n = 0;
780 for (i = 0; i < AUTO_PIN_LAST; i++) {
781 if (!spec->adc_nid[i])
782 continue;
783 bind->values[n++] =
784 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
785 spec->adc_idx[i], HDA_INPUT);
786 }
787 return bind;
788}
789
Takashi Iwai6a929342010-10-11 15:16:20 +0200790/* add a (input-boost) volume control to the given input pin */
791static int add_input_volume_control(struct hda_codec *codec,
792 struct auto_pin_cfg *cfg,
793 int item)
794{
795 hda_nid_t pin = cfg->inputs[item].pin;
796 u32 caps;
797 const char *label;
798 struct snd_kcontrol *kctl;
799
800 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
801 return 0;
802 caps = query_amp_caps(codec, pin, HDA_INPUT);
803 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
804 if (caps <= 1)
805 return 0;
806 label = hda_get_autocfg_input_label(codec, cfg, item);
807 return add_volume(codec, label, 0,
808 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
809}
810
Takashi Iwaie5f14242009-07-01 18:11:44 +0200811static int build_input(struct hda_codec *codec)
812{
813 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200814 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200815
Takashi Iwaie5f14242009-07-01 18:11:44 +0200816 if (!spec->num_inputs)
817 return 0;
818
Takashi Iwaie5f14242009-07-01 18:11:44 +0200819 /* make bind-capture */
820 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
821 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
822 for (i = 0; i < 2; i++) {
823 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100824 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200825 if (!spec->capture_bind[i])
826 return -ENOMEM;
827 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
828 if (!kctl)
829 return -ENOMEM;
830 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100831 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200832 if (err < 0)
833 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100834 for (n = 0; n < AUTO_PIN_LAST; n++) {
835 if (!spec->adc_nid[n])
836 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700837 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100838 if (err < 0)
839 return err;
840 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200841 }
842
843 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100844 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200845 snd_ctl_new1(&cs_capture_source, codec));
846 if (err < 0)
847 return err;
848 }
849
Takashi Iwai6a929342010-10-11 15:16:20 +0200850 for (i = 0; i < spec->num_inputs; i++) {
851 err = add_input_volume_control(codec, &spec->autocfg, i);
852 if (err < 0)
853 return err;
854 }
855
Takashi Iwaie5f14242009-07-01 18:11:44 +0200856 return 0;
857}
858
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200859/*
860 */
861
Takashi Iwaie5f14242009-07-01 18:11:44 +0200862static int build_digital_output(struct hda_codec *codec)
863{
864 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200865 int err;
866
Takashi Iwai63b24132009-07-09 11:45:59 +0200867 if (!spec->multiout.dig_out_nid)
868 return 0;
869
Stephen Warren74b654c2011-06-01 11:14:18 -0600870 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
871 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200872 if (err < 0)
873 return err;
874 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
875 if (err < 0)
876 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200877 return 0;
878}
879
880static int build_digital_input(struct hda_codec *codec)
881{
882 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200883 if (spec->dig_in)
884 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
885 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200886}
887
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200888/*
889 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500890 * CS421x auto-output redirecting
891 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200892 */
893
Takashi Iwaie5f14242009-07-01 18:11:44 +0200894static void cs_automute(struct hda_codec *codec)
895{
896 struct cs_spec *spec = codec->spec;
897 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200898 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500899 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200900 hda_nid_t nid;
901 int i;
902
Tim Howe56487c22011-07-22 16:41:00 -0500903 spdif_present = 0;
904 if (cfg->dig_outs) {
905 nid = cfg->dig_out_pins[0];
906 if (is_jack_detectable(codec, nid)) {
907 /*
908 TODO: SPDIF output redirect when SENSE_B is enabled.
909 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
910 assumed.
911 */
912 if (snd_hda_jack_detect(codec, nid)
913 /* && spec->sense_b */)
914 spdif_present = 1;
915 }
916 }
917
Takashi Iwaie5f14242009-07-01 18:11:44 +0200918 hp_present = 0;
919 for (i = 0; i < cfg->hp_outs; i++) {
920 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200921 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200922 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800923 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200924 if (hp_present)
925 break;
926 }
Tim Howe56487c22011-07-22 16:41:00 -0500927
928 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200929 for (i = 0; i < cfg->speaker_outs; i++) {
David Henningsson78e2a922012-01-02 12:40:16 +0100930 int pin_ctl = hp_present ? 0 : PIN_OUT;
David Henningsson5660ffd2012-01-02 12:40:17 +0100931 /* detect on spdif is specific to CS4210 */
932 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
David Henningsson78e2a922012-01-02 12:40:16 +0100933 pin_ctl = 0;
934
Takashi Iwaie5f14242009-07-01 18:11:44 +0200935 nid = cfg->speaker_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200936 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200937 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100938 if (spec->gpio_eapd_hp) {
939 unsigned int gpio = hp_present ?
940 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200941 snd_hda_codec_write(codec, 0x01, 0,
942 AC_VERB_SET_GPIO_DATA, gpio);
943 }
Tim Howe56487c22011-07-22 16:41:00 -0500944
David Henningsson5660ffd2012-01-02 12:40:17 +0100945 /* specific to CS4210 */
946 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500947 /* mute HPs if spdif jack (SENSE_B) is present */
948 for (i = 0; i < cfg->hp_outs; i++) {
949 nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200950 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500951 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
952 }
953
954 /* SPDIF TX on/off */
955 if (cfg->dig_outs) {
956 nid = cfg->dig_out_pins[0];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200957 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500958 spdif_present ? PIN_OUT : 0);
959
960 }
961 /* Update board GPIOs if neccessary ... */
962 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200963}
964
Tim Howe56487c22011-07-22 16:41:00 -0500965/*
966 * Auto-input redirect for CS421x
967 * Switch max 3 inputs of a single ADC (nid 3)
968*/
969
Takashi Iwaie5f14242009-07-01 18:11:44 +0200970static void cs_automic(struct hda_codec *codec)
971{
972 struct cs_spec *spec = codec->spec;
973 struct auto_pin_cfg *cfg = &spec->autocfg;
974 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800975 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500976
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200977 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800978 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500979
980 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100981 if (spec->vendor_nid == CS420X_VENDOR_NID) {
982 if (present)
983 change_cur_input(codec, spec->automic_idx, 0);
984 else
985 change_cur_input(codec, !spec->automic_idx, 0);
986 } else {
Tim Howe56487c22011-07-22 16:41:00 -0500987 if (present) {
Dylan Reidf70eecd2012-01-31 13:04:41 -0800988 if (spec->cur_input != spec->automic_idx) {
989 spec->last_input = spec->cur_input;
990 spec->cur_input = spec->automic_idx;
991 }
Tim Howe56487c22011-07-22 16:41:00 -0500992 } else {
993 spec->cur_input = spec->last_input;
994 }
Takashi Iwai05ee7962011-11-16 18:05:11 +0100995 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -0500996 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200997}
998
999/*
1000 */
1001
1002static void init_output(struct hda_codec *codec)
1003{
1004 struct cs_spec *spec = codec->spec;
1005 struct auto_pin_cfg *cfg = &spec->autocfg;
1006 int i;
1007
1008 /* mute first */
1009 for (i = 0; i < spec->multiout.num_dacs; i++)
1010 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1011 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1012 if (spec->multiout.hp_nid)
1013 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1014 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1015 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1016 if (!spec->multiout.extra_out_nid[i])
1017 break;
1018 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1019 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1020 }
1021
1022 /* set appropriate pin controls */
1023 for (i = 0; i < cfg->line_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001024 snd_hda_set_pin_ctl(codec, cfg->line_out_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001025 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001026 for (i = 0; i < cfg->hp_outs; i++) {
1027 hda_nid_t nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001028 snd_hda_set_pin_ctl(codec, nid, PIN_HP);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001029 if (!cfg->speaker_outs)
1030 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001031 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001032 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001033 spec->hp_detect = 1;
1034 }
1035 }
Tim Howe56487c22011-07-22 16:41:00 -05001036
1037 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001038 for (i = 0; i < cfg->speaker_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001039 snd_hda_set_pin_ctl(codec, cfg->speaker_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001040
1041 /* SPDIF is enabled on presence detect for CS421x */
1042 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001043 cs_automute(codec);
1044}
1045
1046static void init_input(struct hda_codec *codec)
1047{
1048 struct cs_spec *spec = codec->spec;
1049 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001050 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001051 int i;
1052
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001053 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001054 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001055 hda_nid_t pin = cfg->inputs[i].pin;
1056 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001057 continue;
1058 /* set appropriate pin control and mute first */
1059 ctl = PIN_IN;
Takashi Iwai86e29592010-09-09 14:50:17 +02001060 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001061 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1062 caps >>= AC_PINCAP_VREF_SHIFT;
1063 if (caps & AC_PINCAP_VREF_80)
1064 ctl = PIN_VREF80;
1065 }
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001066 snd_hda_set_pin_ctl(codec, pin, ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001067 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1068 AC_VERB_SET_AMP_GAIN_MUTE,
1069 AMP_IN_MUTE(spec->adc_idx[i]));
1070 if (spec->mic_detect && spec->automic_idx == i)
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001071 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001072 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001073 /* CS420x has multiple ADC, CS421x has single ADC */
1074 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001075 change_cur_input(codec, spec->cur_input, 1);
1076 if (spec->mic_detect)
1077 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001078
Tim Howe56487c22011-07-22 16:41:00 -05001079 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1080 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1081 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1082 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1083 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1084 * No effect if SPDIF_OUT2 is
1085 * selected in IDX_SPDIF_CTL.
1086 */
1087 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001088 } else {
1089 if (spec->mic_detect)
1090 cs_automic(codec);
1091 else {
1092 spec->cur_adc = spec->adc_nid[spec->cur_input];
1093 cs_update_input_select(codec);
1094 }
Tim Howe56487c22011-07-22 16:41:00 -05001095 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001096}
1097
Takashi Iwaic42d4782011-05-02 11:36:09 +02001098static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001099 {0x11, AC_VERB_SET_PROC_STATE, 1},
1100 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1101 {0x11, AC_VERB_SET_PROC_COEF,
1102 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1103 | 0x0040 /* Mute DACs on FIFO error */
1104 | 0x1000 /* Enable DACs High Pass Filter */
1105 | 0x0400 /* Disable Coefficient Auto increment */
1106 )},
1107 /* Beep */
1108 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1109 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1110
1111 {} /* terminator */
1112};
1113
Brian Austina769cbc2010-09-07 14:36:22 -05001114/* Errata: CS4207 rev C0/C1/C2 Silicon
1115 *
1116 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1117 *
1118 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1119 * may be excessive (up to an additional 200 μA), which is most easily
1120 * observed while the part is being held in reset (RESET# active low).
1121 *
1122 * Root Cause: At initial powerup of the device, the logic that drives
1123 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1124 * initialized.
1125 * Certain random patterns will cause a steady leakage current in those
1126 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1127 *
1128 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1129 * blocks, which will alleviate the issue.
1130 */
1131
Takashi Iwaic42d4782011-05-02 11:36:09 +02001132static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001133 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1134 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1135
1136 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1137 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1138 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1139 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1140 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1141 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1142
1143 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1144 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1145
1146 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1147 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1148 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1149 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1150 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1151 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1152 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1153
Takashi Iwai38c07642011-03-03 14:54:19 +01001154#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001155 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1156 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1157 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001158#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001159
1160 {} /* terminator */
1161};
1162
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001163/* SPDIF setup */
1164static void init_digital(struct hda_codec *codec)
1165{
1166 unsigned int coef;
1167
1168 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1169 coef |= 0x0008; /* Replace with mute on error */
1170 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1171 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1172 * SPDIF_OUT2 is shared with GPIO1 and
1173 * DMIC_SDA2.
1174 */
1175 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001176}
1177
1178static int cs_init(struct hda_codec *codec)
1179{
1180 struct cs_spec *spec = codec->spec;
1181
Brian Austina769cbc2010-09-07 14:36:22 -05001182 /* init_verb sequence for C0/C1/C2 errata*/
1183 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1184
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001185 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001186
1187 if (spec->gpio_mask) {
1188 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1189 spec->gpio_mask);
1190 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1191 spec->gpio_dir);
1192 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1193 spec->gpio_data);
1194 }
1195
Takashi Iwaie5f14242009-07-01 18:11:44 +02001196 init_output(codec);
1197 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001198 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001199 snd_hda_jack_report_sync(codec);
1200
Takashi Iwaie5f14242009-07-01 18:11:44 +02001201 return 0;
1202}
1203
1204static int cs_build_controls(struct hda_codec *codec)
1205{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001206 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001207 int err;
1208
1209 err = build_output(codec);
1210 if (err < 0)
1211 return err;
1212 err = build_input(codec);
1213 if (err < 0)
1214 return err;
1215 err = build_digital_output(codec);
1216 if (err < 0)
1217 return err;
1218 err = build_digital_input(codec);
1219 if (err < 0)
1220 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001221 err = cs_init(codec);
1222 if (err < 0)
1223 return err;
1224
1225 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1226 if (err < 0)
1227 return err;
1228
1229 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001230}
1231
1232static void cs_free(struct hda_codec *codec)
1233{
1234 struct cs_spec *spec = codec->spec;
1235 kfree(spec->capture_bind[0]);
1236 kfree(spec->capture_bind[1]);
1237 kfree(codec->spec);
1238}
1239
1240static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1241{
Takashi Iwai3a938972011-10-28 01:16:55 +02001242 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001243 case HP_EVENT:
1244 cs_automute(codec);
1245 break;
1246 case MIC_EVENT:
1247 cs_automic(codec);
1248 break;
1249 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001250 snd_hda_jack_report_sync(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001251}
1252
Takashi Iwaic42d4782011-05-02 11:36:09 +02001253static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001254 .build_controls = cs_build_controls,
1255 .build_pcms = cs_build_pcms,
1256 .init = cs_init,
1257 .free = cs_free,
1258 .unsol_event = cs_unsol_event,
1259};
1260
1261static int cs_parse_auto_config(struct hda_codec *codec)
1262{
1263 struct cs_spec *spec = codec->spec;
1264 int err;
1265
1266 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1267 if (err < 0)
1268 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001269
1270 err = parse_output(codec);
1271 if (err < 0)
1272 return err;
1273 err = parse_input(codec);
1274 if (err < 0)
1275 return err;
1276 err = parse_digital_output(codec);
1277 if (err < 0)
1278 return err;
1279 err = parse_digital_input(codec);
1280 if (err < 0)
1281 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001282 return 0;
1283}
1284
Takashi Iwaiea734962011-01-17 11:29:34 +01001285static const char * const cs420x_models[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001286 [CS420X_MBP53] = "mbp53",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001287 [CS420X_MBP55] = "mbp55",
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001288 [CS420X_IMAC27] = "imac27",
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001289 [CS420X_IMAC27_122] = "imac27_122",
Dan Carpenter92bb43e2011-11-24 14:48:24 +03001290 [CS420X_APPLE] = "apple",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001291 [CS420X_AUTO] = "auto",
1292};
1293
1294
Takashi Iwaic42d4782011-05-02 11:36:09 +02001295static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001296 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001297 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001298 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001299 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001300 /* this conflicts with too many other models */
1301 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1302 {} /* terminator */
1303};
1304
1305static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001306 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001307 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001308 {} /* terminator */
1309};
1310
1311struct cs_pincfg {
1312 hda_nid_t nid;
1313 u32 val;
1314};
1315
Takashi Iwaic42d4782011-05-02 11:36:09 +02001316static const struct cs_pincfg mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001317 { 0x09, 0x012b4050 },
1318 { 0x0a, 0x90100141 },
1319 { 0x0b, 0x90100140 },
1320 { 0x0c, 0x018b3020 },
1321 { 0x0d, 0x90a00110 },
1322 { 0x0e, 0x400000f0 },
1323 { 0x0f, 0x01cbe030 },
1324 { 0x10, 0x014be060 },
1325 { 0x12, 0x400000f0 },
1326 { 0x15, 0x400000f0 },
1327 {} /* terminator */
1328};
1329
Takashi Iwaic42d4782011-05-02 11:36:09 +02001330static const struct cs_pincfg mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001331 { 0x09, 0x012b4030 },
1332 { 0x0a, 0x90100121 },
1333 { 0x0b, 0x90100120 },
1334 { 0x0c, 0x400000f0 },
1335 { 0x0d, 0x90a00110 },
1336 { 0x0e, 0x400000f0 },
1337 { 0x0f, 0x400000f0 },
1338 { 0x10, 0x014be040 },
1339 { 0x12, 0x400000f0 },
1340 { 0x15, 0x400000f0 },
1341 {} /* terminator */
1342};
1343
Takashi Iwaic42d4782011-05-02 11:36:09 +02001344static const struct cs_pincfg imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001345 { 0x09, 0x012b4050 },
1346 { 0x0a, 0x90100140 },
1347 { 0x0b, 0x90100142 },
1348 { 0x0c, 0x018b3020 },
1349 { 0x0d, 0x90a00110 },
1350 { 0x0e, 0x400000f0 },
1351 { 0x0f, 0x01cbe030 },
1352 { 0x10, 0x014be060 },
1353 { 0x12, 0x01ab9070 },
1354 { 0x15, 0x400000f0 },
1355 {} /* terminator */
1356};
1357
Takashi Iwaic42d4782011-05-02 11:36:09 +02001358static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001359 [CS420X_MBP53] = mbp53_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001360 [CS420X_MBP55] = mbp55_pincfgs,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001361 [CS420X_IMAC27] = imac27_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001362};
1363
Tim Howe56487c22011-07-22 16:41:00 -05001364static void fix_pincfg(struct hda_codec *codec, int model,
1365 const struct cs_pincfg **pin_configs)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001366{
Tim Howe56487c22011-07-22 16:41:00 -05001367 const struct cs_pincfg *cfg = pin_configs[model];
Takashi Iwaia6bae202009-07-06 15:15:22 +02001368 if (!cfg)
1369 return;
1370 for (; cfg->nid; cfg++)
1371 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1372}
1373
Takashi Iwaie5f14242009-07-01 18:11:44 +02001374static int patch_cs420x(struct hda_codec *codec)
1375{
1376 struct cs_spec *spec;
1377 int err;
1378
1379 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1380 if (!spec)
1381 return -ENOMEM;
1382 codec->spec = spec;
1383
Tim Howe56487c22011-07-22 16:41:00 -05001384 spec->vendor_nid = CS420X_VENDOR_NID;
1385
Takashi Iwaia6bae202009-07-06 15:15:22 +02001386 spec->board_config =
1387 snd_hda_check_board_config(codec, CS420X_MODELS,
1388 cs420x_models, cs420x_cfg_tbl);
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001389 if (spec->board_config < 0)
1390 spec->board_config =
1391 snd_hda_check_board_codec_sid_config(codec,
1392 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001393 if (spec->board_config >= 0)
Tim Howe56487c22011-07-22 16:41:00 -05001394 fix_pincfg(codec, spec->board_config, cs_pincfgs);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001395
Takashi Iwaied208252009-07-07 09:04:26 +02001396 switch (spec->board_config) {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001397 case CS420X_IMAC27:
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001398 case CS420X_MBP53:
Takashi Iwaied208252009-07-07 09:04:26 +02001399 case CS420X_MBP55:
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001400 case CS420X_APPLE:
1401 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1402 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1403 spec->gpio_mask = spec->gpio_dir =
1404 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
Takashi Iwaied208252009-07-07 09:04:26 +02001405 break;
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001406 case CS420X_IMAC27_122:
1407 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1408 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1409 spec->gpio_mask = spec->gpio_dir =
1410 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1411 break;
Takashi Iwaied208252009-07-07 09:04:26 +02001412 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001413
Takashi Iwaied208252009-07-07 09:04:26 +02001414 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001415 if (err < 0)
1416 goto error;
1417
Takashi Iwaie5f14242009-07-01 18:11:44 +02001418 codec->patch_ops = cs_patch_ops;
1419
1420 return 0;
1421
1422 error:
1423 kfree(codec->spec);
1424 codec->spec = NULL;
1425 return err;
1426}
1427
Tim Howe56487c22011-07-22 16:41:00 -05001428/*
1429 * Cirrus Logic CS4210
1430 *
1431 * 1 DAC => HP(sense) / Speakers,
1432 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1433 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1434*/
1435
1436/* CS4210 board names */
1437static const char *cs421x_models[CS421X_MODELS] = {
1438 [CS421X_CDB4210] = "cdb4210",
1439};
1440
1441static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1442 /* Test Intel board + CDB2410 */
1443 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1444 {} /* terminator */
1445};
1446
1447/* CS4210 board pinconfigs */
1448/* Default CS4210 (CDB4210)*/
1449static const struct cs_pincfg cdb4210_pincfgs[] = {
1450 { 0x05, 0x0321401f },
1451 { 0x06, 0x90170010 },
1452 { 0x07, 0x03813031 },
1453 { 0x08, 0xb7a70037 },
1454 { 0x09, 0xb7a6003e },
1455 { 0x0a, 0x034510f0 },
1456 {} /* terminator */
1457};
1458
1459static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1460 [CS421X_CDB4210] = cdb4210_pincfgs,
1461};
1462
1463static const struct hda_verb cs421x_coef_init_verbs[] = {
1464 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1465 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1466 /*
1467 Disable Coefficient Index Auto-Increment(DAI)=1,
1468 PDREF=0
1469 */
1470 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1471
1472 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1473 /* ADC SZCMode = Digital Soft Ramp */
1474 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1475
1476 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1477 {0x0B, AC_VERB_SET_PROC_COEF,
1478 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1479 | 0x0004 /* Mute DAC on FIFO error */
1480 | 0x0008 /* Enable DAC High Pass Filter */
1481 )},
1482 {} /* terminator */
1483};
1484
1485/* Errata: CS4210 rev A1 Silicon
1486 *
1487 * http://www.cirrus.com/en/pubs/errata/
1488 *
1489 * Description:
1490 * 1. Performance degredation is present in the ADC.
1491 * 2. Speaker output is not completely muted upon HP detect.
1492 * 3. Noise is present when clipping occurs on the amplified
1493 * speaker outputs.
1494 *
1495 * Workaround:
1496 * The following verb sequence written to the registers during
1497 * initialization will correct the issues listed above.
1498 */
1499
1500static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1501 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1502
1503 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1504 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1505
1506 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1507 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1508
1509 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1510 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1511
1512 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1513 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1514
1515 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1516 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1517
1518 {} /* terminator */
1519};
1520
1521/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1522static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1523
1524static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1525 struct snd_ctl_elem_info *uinfo)
1526{
1527 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1528 uinfo->count = 1;
1529 uinfo->value.integer.min = 0;
1530 uinfo->value.integer.max = 3;
1531 return 0;
1532}
1533
1534static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1535 struct snd_ctl_elem_value *ucontrol)
1536{
1537 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1538
1539 ucontrol->value.integer.value[0] =
1540 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1541 return 0;
1542}
1543
1544static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1545 struct snd_ctl_elem_value *ucontrol)
1546{
1547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1548
1549 unsigned int vol = ucontrol->value.integer.value[0];
1550 unsigned int coef =
1551 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1552 unsigned int original_coef = coef;
1553
1554 coef &= ~0x0003;
1555 coef |= (vol & 0x0003);
1556 if (original_coef == coef)
1557 return 0;
1558 else {
1559 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1560 return 1;
1561 }
1562}
1563
1564static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1565
1566 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1567 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1568 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1569 .name = "Speaker Boost Playback Volume",
1570 .info = cs421x_boost_vol_info,
1571 .get = cs421x_boost_vol_get,
1572 .put = cs421x_boost_vol_put,
1573 .tlv = { .p = cs421x_speaker_boost_db_scale },
1574};
1575
David Henningsson5660ffd2012-01-02 12:40:17 +01001576static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001577{
1578 struct cs_spec *spec = codec->spec;
1579 unsigned int def_conf, coef;
1580
1581 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1582 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1583
1584 if (spec->gpio_mask)
1585 coef |= 0x0008; /* B1,B2 are GPIOs */
1586 else
1587 coef &= ~0x0008;
1588
1589 if (spec->sense_b)
1590 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1591 else
1592 coef &= ~0x0010;
1593
1594 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1595
1596 if ((spec->gpio_mask || spec->sense_b) &&
1597 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1598
1599 /*
1600 GPIO or SENSE_B forced - disconnect the DMIC pin.
1601 */
1602 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1603 def_conf &= ~AC_DEFCFG_PORT_CONN;
1604 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1605 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1606 }
1607}
1608
1609static void init_cs421x_digital(struct hda_codec *codec)
1610{
1611 struct cs_spec *spec = codec->spec;
1612 struct auto_pin_cfg *cfg = &spec->autocfg;
1613 int i;
1614
1615
1616 for (i = 0; i < cfg->dig_outs; i++) {
1617 hda_nid_t nid = cfg->dig_out_pins[i];
1618 if (!cfg->speaker_outs)
1619 continue;
1620 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001621 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
Tim Howe56487c22011-07-22 16:41:00 -05001622 spec->spdif_detect = 1;
1623 }
1624 }
1625}
1626
1627static int cs421x_init(struct hda_codec *codec)
1628{
1629 struct cs_spec *spec = codec->spec;
1630
David Henningsson5660ffd2012-01-02 12:40:17 +01001631 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1632 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1633 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1634 cs4210_pinmux_init(codec);
1635 }
Tim Howe56487c22011-07-22 16:41:00 -05001636
1637 if (spec->gpio_mask) {
1638 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1639 spec->gpio_mask);
1640 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1641 spec->gpio_dir);
1642 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1643 spec->gpio_data);
1644 }
1645
1646 init_output(codec);
1647 init_input(codec);
1648 init_cs421x_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001649 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001650
1651 return 0;
1652}
1653
1654/*
1655 * CS4210 Input MUX (1 ADC)
1656 */
1657static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1658 struct snd_ctl_elem_info *uinfo)
1659{
1660 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1661 struct cs_spec *spec = codec->spec;
1662
1663 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1664}
1665
1666static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1667 struct snd_ctl_elem_value *ucontrol)
1668{
1669 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1670 struct cs_spec *spec = codec->spec;
1671
1672 ucontrol->value.enumerated.item[0] = spec->cur_input;
1673 return 0;
1674}
1675
1676static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1677 struct snd_ctl_elem_value *ucontrol)
1678{
1679 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1680 struct cs_spec *spec = codec->spec;
1681
1682 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1683 spec->adc_nid[0], &spec->cur_input);
1684
1685}
1686
1687static struct snd_kcontrol_new cs421x_capture_source = {
1688
1689 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1690 .name = "Capture Source",
1691 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1692 .info = cs421x_mux_enum_info,
1693 .get = cs421x_mux_enum_get,
1694 .put = cs421x_mux_enum_put,
1695};
1696
1697static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1698{
1699 struct cs_spec *spec = codec->spec;
1700 struct auto_pin_cfg *cfg = &spec->autocfg;
1701 const struct hda_input_mux *imux = &spec->input_mux;
1702 hda_nid_t pin = cfg->inputs[item].pin;
1703 struct snd_kcontrol *kctl;
1704 u32 caps;
1705
1706 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1707 return 0;
1708
1709 caps = query_amp_caps(codec, pin, HDA_INPUT);
1710 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1711 if (caps <= 1)
1712 return 0;
1713
1714 return add_volume(codec, imux->items[item].label, 0,
1715 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1716}
1717
1718/* add a (input-boost) volume control to the given input pin */
1719static int build_cs421x_input(struct hda_codec *codec)
1720{
1721 struct cs_spec *spec = codec->spec;
1722 struct auto_pin_cfg *cfg = &spec->autocfg;
1723 struct hda_input_mux *imux = &spec->input_mux;
1724 int i, err, type_idx;
1725 const char *label;
1726
1727 if (!spec->num_inputs)
1728 return 0;
1729
1730 /* make bind-capture */
1731 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1732 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1733 for (i = 0; i < 2; i++) {
1734 struct snd_kcontrol *kctl;
1735 int n;
1736 if (!spec->capture_bind[i])
1737 return -ENOMEM;
1738 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1739 if (!kctl)
1740 return -ENOMEM;
1741 kctl->private_value = (long)spec->capture_bind[i];
1742 err = snd_hda_ctl_add(codec, 0, kctl);
1743 if (err < 0)
1744 return err;
1745 for (n = 0; n < AUTO_PIN_LAST; n++) {
1746 if (!spec->adc_nid[n])
1747 continue;
1748 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1749 if (err < 0)
1750 return err;
1751 }
1752 }
1753
1754 /* Add Input MUX Items + Capture Volume/Switch */
1755 for (i = 0; i < spec->num_inputs; i++) {
1756 label = hda_get_autocfg_input_label(codec, cfg, i);
1757 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1758
1759 err = cs421x_add_input_volume_control(codec, i);
1760 if (err < 0)
1761 return err;
1762 }
1763
1764 /*
1765 Add 'Capture Source' Switch if
1766 * 2 inputs and no mic detec
1767 * 3 inputs
1768 */
1769 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1770 (spec->num_inputs == 3)) {
1771
1772 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1773 snd_ctl_new1(&cs421x_capture_source, codec));
1774 if (err < 0)
1775 return err;
1776 }
1777
1778 return 0;
1779}
1780
1781/* Single DAC (Mute/Gain) */
1782static int build_cs421x_output(struct hda_codec *codec)
1783{
1784 hda_nid_t dac = CS4210_DAC_NID;
1785 struct cs_spec *spec = codec->spec;
1786 struct auto_pin_cfg *cfg = &spec->autocfg;
1787 struct snd_kcontrol *kctl;
1788 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001789 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001790
1791 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001792
1793 err = add_mute(codec, name, 0,
1794 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1795 if (err < 0)
1796 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001797
1798 err = add_volume(codec, name, 0,
1799 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1800 if (err < 0)
1801 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001802
David Henningsson5660ffd2012-01-02 12:40:17 +01001803 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001804 err = snd_hda_ctl_add(codec, 0,
1805 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1806 if (err < 0)
1807 return err;
1808 }
1809 return err;
1810}
1811
1812static int cs421x_build_controls(struct hda_codec *codec)
1813{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001814 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001815 int err;
1816
1817 err = build_cs421x_output(codec);
1818 if (err < 0)
1819 return err;
1820 err = build_cs421x_input(codec);
1821 if (err < 0)
1822 return err;
1823 err = build_digital_output(codec);
1824 if (err < 0)
1825 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001826 err = cs421x_init(codec);
1827 if (err < 0)
1828 return err;
1829
1830 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1831 if (err < 0)
1832 return err;
1833
1834 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001835}
1836
1837static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1838{
Takashi Iwai3a938972011-10-28 01:16:55 +02001839 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Tim Howe56487c22011-07-22 16:41:00 -05001840 case HP_EVENT:
1841 case SPDIF_EVENT:
1842 cs_automute(codec);
1843 break;
1844
1845 case MIC_EVENT:
1846 cs_automic(codec);
1847 break;
1848 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001849 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001850}
1851
1852static int parse_cs421x_input(struct hda_codec *codec)
1853{
1854 struct cs_spec *spec = codec->spec;
1855 struct auto_pin_cfg *cfg = &spec->autocfg;
1856 int i;
1857
1858 for (i = 0; i < cfg->num_inputs; i++) {
1859 hda_nid_t pin = cfg->inputs[i].pin;
1860 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1861 spec->cur_input = spec->last_input = i;
1862 spec->num_inputs++;
1863
1864 /* check whether the automatic mic switch is available */
1865 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1866 spec->mic_detect = 1;
1867 spec->automic_idx = i;
1868 }
1869 }
1870 return 0;
1871}
1872
1873static int cs421x_parse_auto_config(struct hda_codec *codec)
1874{
1875 struct cs_spec *spec = codec->spec;
1876 int err;
1877
1878 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1879 if (err < 0)
1880 return err;
1881 err = parse_output(codec);
1882 if (err < 0)
1883 return err;
1884 err = parse_cs421x_input(codec);
1885 if (err < 0)
1886 return err;
1887 err = parse_digital_output(codec);
1888 if (err < 0)
1889 return err;
1890 return 0;
1891}
1892
1893#ifdef CONFIG_PM
1894/*
1895 Manage PDREF, when transitioning to D3hot
1896 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1897*/
1898static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1899{
David Henningsson5660ffd2012-01-02 12:40:17 +01001900 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001901 unsigned int coef;
1902
1903 snd_hda_shutup_pins(codec);
1904
1905 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1906 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1907 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1908 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1909
David Henningsson5660ffd2012-01-02 12:40:17 +01001910 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1911 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1912 coef |= 0x0004; /* PDREF */
1913 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1914 }
Tim Howe56487c22011-07-22 16:41:00 -05001915
1916 return 0;
1917}
1918#endif
1919
David Henningsson5660ffd2012-01-02 12:40:17 +01001920static struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001921 .build_controls = cs421x_build_controls,
1922 .build_pcms = cs_build_pcms,
1923 .init = cs421x_init,
1924 .free = cs_free,
1925 .unsol_event = cs421x_unsol_event,
1926#ifdef CONFIG_PM
1927 .suspend = cs421x_suspend,
1928#endif
1929};
1930
David Henningsson5660ffd2012-01-02 12:40:17 +01001931static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001932{
1933 struct cs_spec *spec;
1934 int err;
1935
1936 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1937 if (!spec)
1938 return -ENOMEM;
1939 codec->spec = spec;
1940
David Henningsson5660ffd2012-01-02 12:40:17 +01001941 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001942
1943 spec->board_config =
1944 snd_hda_check_board_config(codec, CS421X_MODELS,
1945 cs421x_models, cs421x_cfg_tbl);
1946 if (spec->board_config >= 0)
1947 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1948 /*
1949 Setup GPIO/SENSE for each board (if used)
1950 */
1951 switch (spec->board_config) {
1952 case CS421X_CDB4210:
1953 snd_printd("CS4210 board: %s\n",
1954 cs421x_models[spec->board_config]);
1955/* spec->gpio_mask = 3;
1956 spec->gpio_dir = 3;
1957 spec->gpio_data = 3;
1958*/
1959 spec->sense_b = 1;
1960
1961 break;
1962 }
1963
1964 /*
1965 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1966 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1967 is disabled.
1968 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001969 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001970
1971 err = cs421x_parse_auto_config(codec);
1972 if (err < 0)
1973 goto error;
1974
David Henningsson5660ffd2012-01-02 12:40:17 +01001975 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05001976
1977 return 0;
1978
1979 error:
1980 kfree(codec->spec);
1981 codec->spec = NULL;
1982 return err;
1983}
1984
David Henningsson5660ffd2012-01-02 12:40:17 +01001985static int patch_cs4213(struct hda_codec *codec)
1986{
1987 struct cs_spec *spec;
1988 int err;
1989
1990 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1991 if (!spec)
1992 return -ENOMEM;
1993 codec->spec = spec;
1994
1995 spec->vendor_nid = CS4213_VENDOR_NID;
1996
1997 err = cs421x_parse_auto_config(codec);
1998 if (err < 0)
1999 goto error;
2000
2001 codec->patch_ops = cs421x_patch_ops;
2002 return 0;
2003
2004 error:
2005 kfree(codec->spec);
2006 codec->spec = NULL;
2007 return err;
2008}
2009
Takashi Iwaie5f14242009-07-01 18:11:44 +02002010
2011/*
2012 * patch entries
2013 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002014static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002015 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2016 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002017 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2018 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002019 {} /* terminator */
2020};
2021
2022MODULE_ALIAS("snd-hda-codec-id:10134206");
2023MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002024MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002025MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002026
2027MODULE_LICENSE("GPL");
2028MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2029
2030static struct hda_codec_preset_list cirrus_list = {
2031 .preset = snd_hda_preset_cirrus,
2032 .owner = THIS_MODULE,
2033};
2034
2035static int __init patch_cirrus_init(void)
2036{
2037 return snd_hda_add_codec_preset(&cirrus_list);
2038}
2039
2040static void __exit patch_cirrus_exit(void)
2041{
2042 snd_hda_delete_codec_preset(&cirrus_list);
2043}
2044
2045module_init(patch_cirrus_init)
2046module_exit(patch_cirrus_exit)