blob: 0ba03878c5e0eed4bdcd30acf7fc581bb81326a0 [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;
62
Takashi Iwaie5f14242009-07-01 18:11:44 +020063 struct hda_pcm pcm_rec[2]; /* PCM information */
64
65 unsigned int hp_detect:1;
66 unsigned int mic_detect:1;
Tim Howe56487c22011-07-22 16:41:00 -050067 /* CS421x */
68 unsigned int spdif_detect:1;
69 unsigned int sense_b:1;
70 hda_nid_t vendor_nid;
71 struct hda_input_mux input_mux;
72 unsigned int last_input;
Takashi Iwaie5f14242009-07-01 18:11:44 +020073};
74
Tim Howe56487c22011-07-22 16:41:00 -050075/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020076enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040077 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020078 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010079 CS420X_IMAC27,
Takashi Iwaia6bae202009-07-06 15:15:22 +020080 CS420X_AUTO,
81 CS420X_MODELS
82};
83
Tim Howe56487c22011-07-22 16:41:00 -050084/* CS421x boards */
85enum {
86 CS421X_CDB4210,
87 CS421X_MODELS
88};
89
Takashi Iwai40c20fa2009-07-06 13:00:57 +020090/* Vendor-specific processing widget */
91#define CS420X_VENDOR_NID 0x11
92#define CS_DIG_OUT1_PIN_NID 0x10
93#define CS_DIG_OUT2_PIN_NID 0x15
94#define CS_DMIC1_PIN_NID 0x12
95#define CS_DMIC2_PIN_NID 0x0e
96
97/* coef indices */
98#define IDX_SPDIF_STAT 0x0000
99#define IDX_SPDIF_CTL 0x0001
100#define IDX_ADC_CFG 0x0002
101/* SZC bitmask, 4 modes below:
102 * 0 = immediate,
103 * 1 = digital immediate, analog zero-cross
104 * 2 = digtail & analog soft-ramp
105 * 3 = digital soft-ramp, analog zero-cross
106 */
107#define CS_COEF_ADC_SZC_MASK (3 << 0)
108#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
109#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
110/* PGA mode: 0 = differential, 1 = signle-ended */
111#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
112#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
113#define IDX_DAC_CFG 0x0003
114/* SZC bitmask, 4 modes below:
115 * 0 = Immediate
116 * 1 = zero-cross
117 * 2 = soft-ramp
118 * 3 = soft-ramp on zero-cross
119 */
120#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
121#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
122#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
123
124#define IDX_BEEP_CFG 0x0004
125/* 0x0008 - test reg key */
126/* 0x0009 - 0x0014 -> 12 test regs */
127/* 0x0015 - visibility reg */
128
Tim Howe56487c22011-07-22 16:41:00 -0500129/*
130 * Cirrus Logic CS4210
131 *
132 * 1 DAC => HP(sense) / Speakers,
133 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
134 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
135*/
136#define CS4210_DAC_NID 0x02
137#define CS4210_ADC_NID 0x03
138#define CS421X_VENDOR_NID 0x0B
139#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
140#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
141
142#define CS421X_IDX_DEV_CFG 0x01
143#define CS421X_IDX_ADC_CFG 0x02
144#define CS421X_IDX_DAC_CFG 0x03
145#define CS421X_IDX_SPK_CTL 0x04
146
147#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200148
Takashi Iwai277a57c2009-07-08 12:42:08 +0200149static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200150{
Tim Howe56487c22011-07-22 16:41:00 -0500151 struct cs_spec *spec = codec->spec;
152 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200153 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500154 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200155 AC_VERB_GET_PROC_COEF, 0);
156}
157
Takashi Iwai277a57c2009-07-08 12:42:08 +0200158static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
159 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200160{
Tim Howe56487c22011-07-22 16:41:00 -0500161 struct cs_spec *spec = codec->spec;
162 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200163 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500164 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200165 AC_VERB_SET_PROC_COEF, coef);
166}
167
168
Takashi Iwaie5f14242009-07-01 18:11:44 +0200169#define HP_EVENT 1
170#define MIC_EVENT 2
171
172/*
173 * PCM callbacks
174 */
175static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
176 struct hda_codec *codec,
177 struct snd_pcm_substream *substream)
178{
179 struct cs_spec *spec = codec->spec;
180 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
181 hinfo);
182}
183
184static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
185 struct hda_codec *codec,
186 unsigned int stream_tag,
187 unsigned int format,
188 struct snd_pcm_substream *substream)
189{
190 struct cs_spec *spec = codec->spec;
191 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
192 stream_tag, format, substream);
193}
194
195static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
196 struct hda_codec *codec,
197 struct snd_pcm_substream *substream)
198{
199 struct cs_spec *spec = codec->spec;
200 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
201}
202
203/*
204 * Digital out
205 */
206static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
207 struct hda_codec *codec,
208 struct snd_pcm_substream *substream)
209{
210 struct cs_spec *spec = codec->spec;
211 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
212}
213
214static int cs_dig_playback_pcm_close(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_close(codec, &spec->multiout);
220}
221
222static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
223 struct hda_codec *codec,
224 unsigned int stream_tag,
225 unsigned int format,
226 struct snd_pcm_substream *substream)
227{
228 struct cs_spec *spec = codec->spec;
229 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
230 format, substream);
231}
232
233static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
234 struct hda_codec *codec,
235 struct snd_pcm_substream *substream)
236{
237 struct cs_spec *spec = codec->spec;
238 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
239}
240
241/*
242 * Analog capture
243 */
244static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
245 struct hda_codec *codec,
246 unsigned int stream_tag,
247 unsigned int format,
248 struct snd_pcm_substream *substream)
249{
250 struct cs_spec *spec = codec->spec;
251 spec->cur_adc = spec->adc_nid[spec->cur_input];
252 spec->cur_adc_stream_tag = stream_tag;
253 spec->cur_adc_format = format;
254 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
255 return 0;
256}
257
258static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
259 struct hda_codec *codec,
260 struct snd_pcm_substream *substream)
261{
262 struct cs_spec *spec = codec->spec;
263 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
264 spec->cur_adc = 0;
265 return 0;
266}
267
268/*
269 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200270static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200271 .substreams = 1,
272 .channels_min = 2,
273 .channels_max = 2,
274 .ops = {
275 .open = cs_playback_pcm_open,
276 .prepare = cs_playback_pcm_prepare,
277 .cleanup = cs_playback_pcm_cleanup
278 },
279};
280
Takashi Iwaic42d4782011-05-02 11:36:09 +0200281static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200282 .substreams = 1,
283 .channels_min = 2,
284 .channels_max = 2,
285 .ops = {
286 .prepare = cs_capture_pcm_prepare,
287 .cleanup = cs_capture_pcm_cleanup
288 },
289};
290
Takashi Iwaic42d4782011-05-02 11:36:09 +0200291static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200292 .substreams = 1,
293 .channels_min = 2,
294 .channels_max = 2,
295 .ops = {
296 .open = cs_dig_playback_pcm_open,
297 .close = cs_dig_playback_pcm_close,
298 .prepare = cs_dig_playback_pcm_prepare,
299 .cleanup = cs_dig_playback_pcm_cleanup
300 },
301};
302
Takashi Iwaic42d4782011-05-02 11:36:09 +0200303static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200304 .substreams = 1,
305 .channels_min = 2,
306 .channels_max = 2,
307};
308
309static int cs_build_pcms(struct hda_codec *codec)
310{
311 struct cs_spec *spec = codec->spec;
312 struct hda_pcm *info = spec->pcm_rec;
313
314 codec->pcm_info = info;
315 codec->num_pcms = 0;
316
317 info->name = "Cirrus Analog";
318 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
319 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
320 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
321 spec->multiout.max_channels;
322 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200323 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
324 spec->adc_nid[spec->cur_input];
325 codec->num_pcms++;
326
327 if (!spec->multiout.dig_out_nid && !spec->dig_in)
328 return 0;
329
330 info++;
331 info->name = "Cirrus Digital";
332 info->pcm_type = spec->autocfg.dig_out_type[0];
333 if (!info->pcm_type)
334 info->pcm_type = HDA_PCM_TYPE_SPDIF;
335 if (spec->multiout.dig_out_nid) {
336 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
337 cs_pcm_digital_playback;
338 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
339 spec->multiout.dig_out_nid;
340 }
341 if (spec->dig_in) {
342 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
343 cs_pcm_digital_capture;
344 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
345 }
346 codec->num_pcms++;
347
348 return 0;
349}
350
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200351/*
352 * parse codec topology
353 */
354
Takashi Iwaie5f14242009-07-01 18:11:44 +0200355static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
356{
357 hda_nid_t dac;
358 if (!pin)
359 return 0;
360 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
361 return 0;
362 return dac;
363}
364
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200365static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
366{
367 struct cs_spec *spec = codec->spec;
368 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200369 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200370 unsigned int val;
371 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200372 return 0;
373 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200374 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200375}
376
377static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
378 unsigned int *idxp)
379{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200380 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200381 hda_nid_t nid;
382
383 nid = codec->start_nid;
384 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200385 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200386 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200387 if (type != AC_WID_AUD_IN)
388 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200389 idx = snd_hda_get_conn_index(codec, nid, pin, false);
390 if (idx >= 0) {
391 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200392 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200393 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200394 }
395 return 0;
396}
397
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200398static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
399{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200400 unsigned int val;
401 val = snd_hda_codec_get_pincfg(codec, nid);
402 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
403}
404
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200405static int parse_output(struct hda_codec *codec)
406{
407 struct cs_spec *spec = codec->spec;
408 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200409 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200410 hda_nid_t dac;
411
412 for (i = 0; i < cfg->line_outs; i++) {
413 dac = get_dac(codec, cfg->line_out_pins[i]);
414 if (!dac)
415 break;
416 spec->dac_nid[i] = dac;
417 }
418 spec->multiout.num_dacs = i;
419 spec->multiout.dac_nids = spec->dac_nid;
420 spec->multiout.max_channels = i * 2;
421
422 /* add HP and speakers */
423 extra_nids = 0;
424 for (i = 0; i < cfg->hp_outs; i++) {
425 dac = get_dac(codec, cfg->hp_pins[i]);
426 if (!dac)
427 break;
428 if (!i)
429 spec->multiout.hp_nid = dac;
430 else
431 spec->multiout.extra_out_nid[extra_nids++] = dac;
432 }
433 for (i = 0; i < cfg->speaker_outs; i++) {
434 dac = get_dac(codec, cfg->speaker_pins[i]);
435 if (!dac)
436 break;
437 spec->multiout.extra_out_nid[extra_nids++] = dac;
438 }
439
440 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
441 cfg->speaker_outs = cfg->line_outs;
442 memcpy(cfg->speaker_pins, cfg->line_out_pins,
443 sizeof(cfg->speaker_pins));
444 cfg->line_outs = 0;
445 }
446
447 return 0;
448}
449
450static int parse_input(struct hda_codec *codec)
451{
452 struct cs_spec *spec = codec->spec;
453 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200454 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200455
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200456 for (i = 0; i < cfg->num_inputs; i++) {
457 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200458 spec->input_idx[spec->num_inputs] = i;
459 spec->capsrc_idx[i] = spec->num_inputs++;
460 spec->cur_input = i;
461 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
462 }
463 if (!spec->num_inputs)
464 return 0;
465
466 /* check whether the automatic mic switch is available */
467 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200468 cfg->inputs[0].type == AUTO_PIN_MIC &&
469 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200470 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
471 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200472 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200473 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200474 }
475 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200476 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200477 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200478 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200479 }
480 }
481 }
482 return 0;
483}
484
485
486static int parse_digital_output(struct hda_codec *codec)
487{
488 struct cs_spec *spec = codec->spec;
489 struct auto_pin_cfg *cfg = &spec->autocfg;
490 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200491
492 if (!cfg->dig_outs)
493 return 0;
494 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
495 return 0;
496 spec->multiout.dig_out_nid = nid;
497 spec->multiout.share_spdif = 1;
498 if (cfg->dig_outs > 1 &&
499 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
500 spec->slave_dig_outs[0] = nid;
501 codec->slave_dig_outs = spec->slave_dig_outs;
502 }
503 return 0;
504}
505
506static int parse_digital_input(struct hda_codec *codec)
507{
508 struct cs_spec *spec = codec->spec;
509 struct auto_pin_cfg *cfg = &spec->autocfg;
510 int idx;
511
Takashi Iwai60e53882009-07-06 15:01:09 +0200512 if (cfg->dig_in_pin)
513 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
514 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200515}
516
517/*
518 * create mixer controls
519 */
520
Takashi Iwaiea734962011-01-17 11:29:34 +0100521static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200522
523static int add_mute(struct hda_codec *codec, const char *name, int index,
524 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
525{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200526 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200527 struct snd_kcontrol_new knew =
528 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
529 knew.private_value = pval;
530 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
531 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100532 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100533 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200534}
535
536static int add_volume(struct hda_codec *codec, const char *name,
537 int index, unsigned int pval, int dir,
538 struct snd_kcontrol **kctlp)
539{
David Henningsson2e1210b2011-09-14 13:22:54 +0200540 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200541 struct snd_kcontrol_new knew =
542 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
543 knew.private_value = pval;
544 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
545 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100546 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100547 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200548}
549
550static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
551{
552 unsigned int caps;
553
554 /* set the upper-limit for mixer amp to 0dB */
555 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
556 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
557 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
558 << AC_AMPCAP_NUM_STEPS_SHIFT;
559 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
560}
561
562static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
563{
564 struct cs_spec *spec = codec->spec;
565 unsigned int tlv[4];
566 int err;
567
568 spec->vmaster_sw =
569 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100570 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200571 if (err < 0)
572 return err;
573
574 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
575 spec->vmaster_vol =
576 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100577 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200578 if (err < 0)
579 return err;
580 return 0;
581}
582
583static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
584 int num_ctls, int type)
585{
586 struct cs_spec *spec = codec->spec;
587 const char *name;
588 int err, index;
589 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200590 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200591 "Front Speaker", "Surround Speaker", "Bass Speaker"
592 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200593 static const char * const line_outs[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200594 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
595 };
596
597 fix_volume_caps(codec, dac);
598 if (!spec->vmaster_sw) {
599 err = add_vmaster(codec, dac);
600 if (err < 0)
601 return err;
602 }
603
604 index = 0;
605 switch (type) {
606 case AUTO_PIN_HP_OUT:
607 name = "Headphone";
608 index = idx;
609 break;
610 case AUTO_PIN_SPEAKER_OUT:
611 if (num_ctls > 1)
612 name = speakers[idx];
613 else
614 name = "Speaker";
615 break;
616 default:
617 if (num_ctls > 1)
618 name = line_outs[idx];
619 else
620 name = "Line-Out";
621 break;
622 }
623
624 err = add_mute(codec, name, index,
625 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
626 if (err < 0)
627 return err;
628 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
629 if (err < 0)
630 return err;
631
632 err = add_volume(codec, name, index,
633 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
634 if (err < 0)
635 return err;
636 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
637 if (err < 0)
638 return err;
639
640 return 0;
641}
642
643static int build_output(struct hda_codec *codec)
644{
645 struct cs_spec *spec = codec->spec;
646 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200647 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200648
649 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200650 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
651 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200652 if (err < 0)
653 return err;
654 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200655 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200656 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
657 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200658 if (err < 0)
659 return err;
660 }
661 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200662 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
663 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200664 if (err < 0)
665 return err;
666 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200667 return 0;
668}
669
670/*
671 */
672
Takashi Iwaic42d4782011-05-02 11:36:09 +0200673static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200674 HDA_BIND_SW("Capture Switch", 0),
675 HDA_BIND_VOL("Capture Volume", 0),
676};
677
Takashi Iwaiea359292009-07-06 12:58:59 +0200678static int change_cur_input(struct hda_codec *codec, unsigned int idx,
679 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200680{
681 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200682
Takashi Iwaiea359292009-07-06 12:58:59 +0200683 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200684 return 0;
685 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
686 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200687 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200688 spec->cur_adc = spec->adc_nid[idx];
689 snd_hda_codec_setup_stream(codec, spec->cur_adc,
690 spec->cur_adc_stream_tag, 0,
691 spec->cur_adc_format);
692 }
693 snd_hda_codec_write(codec, spec->cur_adc, 0,
694 AC_VERB_SET_CONNECT_SEL,
695 spec->adc_idx[idx]);
696 spec->cur_input = idx;
697 return 1;
698}
699
700static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
701 struct snd_ctl_elem_info *uinfo)
702{
703 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
704 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200705 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200706 unsigned int idx;
707
708 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
709 uinfo->count = 1;
710 uinfo->value.enumerated.items = spec->num_inputs;
711 if (uinfo->value.enumerated.item >= spec->num_inputs)
712 uinfo->value.enumerated.item = spec->num_inputs - 1;
713 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100714 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
715 uinfo->value.enumerated.name,
716 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200717 return 0;
718}
719
720static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
721 struct snd_ctl_elem_value *ucontrol)
722{
723 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
724 struct cs_spec *spec = codec->spec;
725 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
726 return 0;
727}
728
729static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
730 struct snd_ctl_elem_value *ucontrol)
731{
732 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
733 struct cs_spec *spec = codec->spec;
734 unsigned int idx = ucontrol->value.enumerated.item[0];
735
736 if (idx >= spec->num_inputs)
737 return -EINVAL;
738 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200739 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200740}
741
Takashi Iwaic42d4782011-05-02 11:36:09 +0200742static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
744 .name = "Capture Source",
745 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
746 .info = cs_capture_source_info,
747 .get = cs_capture_source_get,
748 .put = cs_capture_source_put,
749};
750
Takashi Iwaic42d4782011-05-02 11:36:09 +0200751static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200752 struct hda_ctl_ops *ops)
753{
754 struct cs_spec *spec = codec->spec;
755 struct hda_bind_ctls *bind;
756 int i, n;
757
758 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
759 GFP_KERNEL);
760 if (!bind)
761 return NULL;
762 bind->ops = ops;
763 n = 0;
764 for (i = 0; i < AUTO_PIN_LAST; i++) {
765 if (!spec->adc_nid[i])
766 continue;
767 bind->values[n++] =
768 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
769 spec->adc_idx[i], HDA_INPUT);
770 }
771 return bind;
772}
773
Takashi Iwai6a929342010-10-11 15:16:20 +0200774/* add a (input-boost) volume control to the given input pin */
775static int add_input_volume_control(struct hda_codec *codec,
776 struct auto_pin_cfg *cfg,
777 int item)
778{
779 hda_nid_t pin = cfg->inputs[item].pin;
780 u32 caps;
781 const char *label;
782 struct snd_kcontrol *kctl;
783
784 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
785 return 0;
786 caps = query_amp_caps(codec, pin, HDA_INPUT);
787 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
788 if (caps <= 1)
789 return 0;
790 label = hda_get_autocfg_input_label(codec, cfg, item);
791 return add_volume(codec, label, 0,
792 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
793}
794
Takashi Iwaie5f14242009-07-01 18:11:44 +0200795static int build_input(struct hda_codec *codec)
796{
797 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200798 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200799
Takashi Iwaie5f14242009-07-01 18:11:44 +0200800 if (!spec->num_inputs)
801 return 0;
802
Takashi Iwaie5f14242009-07-01 18:11:44 +0200803 /* make bind-capture */
804 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
805 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
806 for (i = 0; i < 2; i++) {
807 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100808 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200809 if (!spec->capture_bind[i])
810 return -ENOMEM;
811 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
812 if (!kctl)
813 return -ENOMEM;
814 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100815 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200816 if (err < 0)
817 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100818 for (n = 0; n < AUTO_PIN_LAST; n++) {
819 if (!spec->adc_nid[n])
820 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700821 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100822 if (err < 0)
823 return err;
824 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200825 }
826
827 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100828 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200829 snd_ctl_new1(&cs_capture_source, codec));
830 if (err < 0)
831 return err;
832 }
833
Takashi Iwai6a929342010-10-11 15:16:20 +0200834 for (i = 0; i < spec->num_inputs; i++) {
835 err = add_input_volume_control(codec, &spec->autocfg, i);
836 if (err < 0)
837 return err;
838 }
839
Takashi Iwaie5f14242009-07-01 18:11:44 +0200840 return 0;
841}
842
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200843/*
844 */
845
Takashi Iwaie5f14242009-07-01 18:11:44 +0200846static int build_digital_output(struct hda_codec *codec)
847{
848 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200849 int err;
850
Takashi Iwai63b24132009-07-09 11:45:59 +0200851 if (!spec->multiout.dig_out_nid)
852 return 0;
853
Stephen Warren74b654c2011-06-01 11:14:18 -0600854 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
855 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200856 if (err < 0)
857 return err;
858 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
859 if (err < 0)
860 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200861 return 0;
862}
863
864static int build_digital_input(struct hda_codec *codec)
865{
866 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200867 if (spec->dig_in)
868 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
869 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200870}
871
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200872/*
873 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500874 * CS421x auto-output redirecting
875 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200876 */
877
Takashi Iwaie5f14242009-07-01 18:11:44 +0200878static void cs_automute(struct hda_codec *codec)
879{
880 struct cs_spec *spec = codec->spec;
881 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200882 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500883 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200884 hda_nid_t nid;
885 int i;
886
Tim Howe56487c22011-07-22 16:41:00 -0500887 spdif_present = 0;
888 if (cfg->dig_outs) {
889 nid = cfg->dig_out_pins[0];
890 if (is_jack_detectable(codec, nid)) {
891 /*
892 TODO: SPDIF output redirect when SENSE_B is enabled.
893 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
894 assumed.
895 */
896 if (snd_hda_jack_detect(codec, nid)
897 /* && spec->sense_b */)
898 spdif_present = 1;
899 }
900 }
901
Takashi Iwaie5f14242009-07-01 18:11:44 +0200902 hp_present = 0;
903 for (i = 0; i < cfg->hp_outs; i++) {
904 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200905 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200906 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800907 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200908 if (hp_present)
909 break;
910 }
Tim Howe56487c22011-07-22 16:41:00 -0500911
912 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200913 for (i = 0; i < cfg->speaker_outs; i++) {
914 nid = cfg->speaker_pins[i];
915 snd_hda_codec_write(codec, nid, 0,
916 AC_VERB_SET_PIN_WIDGET_CONTROL,
917 hp_present ? 0 : PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -0500918 /* detect on spdif is specific to CS421x */
919 if (spec->vendor_nid == CS421X_VENDOR_NID) {
920 snd_hda_codec_write(codec, nid, 0,
921 AC_VERB_SET_PIN_WIDGET_CONTROL,
922 spdif_present ? 0 : PIN_OUT);
923 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200924 }
Vince Weaver4e7d7c62010-09-22 17:31:37 -0400925 if (spec->board_config == CS420X_MBP53 ||
926 spec->board_config == CS420X_MBP55 ||
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +0100927 spec->board_config == CS420X_IMAC27) {
Stelian Pop3a385162009-07-30 14:44:27 +0200928 unsigned int gpio = hp_present ? 0x02 : 0x08;
929 snd_hda_codec_write(codec, 0x01, 0,
930 AC_VERB_SET_GPIO_DATA, gpio);
931 }
Tim Howe56487c22011-07-22 16:41:00 -0500932
933 /* specific to CS421x */
934 if (spec->vendor_nid == CS421X_VENDOR_NID) {
935 /* mute HPs if spdif jack (SENSE_B) is present */
936 for (i = 0; i < cfg->hp_outs; i++) {
937 nid = cfg->hp_pins[i];
938 snd_hda_codec_write(codec, nid, 0,
939 AC_VERB_SET_PIN_WIDGET_CONTROL,
940 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
941 }
942
943 /* SPDIF TX on/off */
944 if (cfg->dig_outs) {
945 nid = cfg->dig_out_pins[0];
946 snd_hda_codec_write(codec, nid, 0,
947 AC_VERB_SET_PIN_WIDGET_CONTROL,
948 spdif_present ? PIN_OUT : 0);
949
950 }
951 /* Update board GPIOs if neccessary ... */
952 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200953}
954
Tim Howe56487c22011-07-22 16:41:00 -0500955/*
956 * Auto-input redirect for CS421x
957 * Switch max 3 inputs of a single ADC (nid 3)
958*/
959
Takashi Iwaie5f14242009-07-01 18:11:44 +0200960static void cs_automic(struct hda_codec *codec)
961{
962 struct cs_spec *spec = codec->spec;
963 struct auto_pin_cfg *cfg = &spec->autocfg;
964 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800965 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500966
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200967 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800968 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500969
970 /* specific to CS421x, single ADC */
971 if (spec->vendor_nid == CS421X_VENDOR_NID) {
972 if (present) {
973 spec->last_input = spec->cur_input;
974 spec->cur_input = spec->automic_idx;
975 } else {
976 spec->cur_input = spec->last_input;
977 }
978
979 snd_hda_codec_write_cache(codec, spec->cur_adc, 0,
980 AC_VERB_SET_CONNECT_SEL,
981 spec->adc_idx[spec->cur_input]);
982 } else {
983 if (present)
984 change_cur_input(codec, spec->automic_idx, 0);
985 else
986 change_cur_input(codec, !spec->automic_idx, 0);
987 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200988}
989
990/*
991 */
992
993static void init_output(struct hda_codec *codec)
994{
995 struct cs_spec *spec = codec->spec;
996 struct auto_pin_cfg *cfg = &spec->autocfg;
997 int i;
998
999 /* mute first */
1000 for (i = 0; i < spec->multiout.num_dacs; i++)
1001 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1002 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1003 if (spec->multiout.hp_nid)
1004 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1005 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1006 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1007 if (!spec->multiout.extra_out_nid[i])
1008 break;
1009 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1010 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1011 }
1012
1013 /* set appropriate pin controls */
1014 for (i = 0; i < cfg->line_outs; i++)
1015 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1016 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001017 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001018 for (i = 0; i < cfg->hp_outs; i++) {
1019 hda_nid_t nid = cfg->hp_pins[i];
1020 snd_hda_codec_write(codec, nid, 0,
1021 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1022 if (!cfg->speaker_outs)
1023 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001024 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001025 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001026 spec->hp_detect = 1;
1027 }
1028 }
Tim Howe56487c22011-07-22 16:41:00 -05001029
1030 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001031 for (i = 0; i < cfg->speaker_outs; i++)
1032 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1033 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001034
1035 /* SPDIF is enabled on presence detect for CS421x */
1036 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001037 cs_automute(codec);
1038}
1039
1040static void init_input(struct hda_codec *codec)
1041{
1042 struct cs_spec *spec = codec->spec;
1043 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001044 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001045 int i;
1046
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001047 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001048 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001049 hda_nid_t pin = cfg->inputs[i].pin;
1050 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001051 continue;
1052 /* set appropriate pin control and mute first */
1053 ctl = PIN_IN;
Takashi Iwai86e29592010-09-09 14:50:17 +02001054 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001055 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1056 caps >>= AC_PINCAP_VREF_SHIFT;
1057 if (caps & AC_PINCAP_VREF_80)
1058 ctl = PIN_VREF80;
1059 }
1060 snd_hda_codec_write(codec, pin, 0,
1061 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1062 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1063 AC_VERB_SET_AMP_GAIN_MUTE,
1064 AMP_IN_MUTE(spec->adc_idx[i]));
1065 if (spec->mic_detect && spec->automic_idx == i)
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001066 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001067 }
Tim Howe56487c22011-07-22 16:41:00 -05001068 /* specific to CS421x */
1069 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1070 if (spec->mic_detect)
1071 cs_automic(codec);
1072 else {
1073 spec->cur_adc = spec->adc_nid[spec->cur_input];
1074 snd_hda_codec_write(codec, spec->cur_adc, 0,
1075 AC_VERB_SET_CONNECT_SEL,
1076 spec->adc_idx[spec->cur_input]);
1077 }
1078 } else {
1079 change_cur_input(codec, spec->cur_input, 1);
1080 if (spec->mic_detect)
1081 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001082
Tim Howe56487c22011-07-22 16:41:00 -05001083 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1084 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1085 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1086 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1087 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1088 * No effect if SPDIF_OUT2 is
1089 * selected in IDX_SPDIF_CTL.
1090 */
1091 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1092 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001093}
1094
Takashi Iwaic42d4782011-05-02 11:36:09 +02001095static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001096 {0x11, AC_VERB_SET_PROC_STATE, 1},
1097 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1098 {0x11, AC_VERB_SET_PROC_COEF,
1099 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1100 | 0x0040 /* Mute DACs on FIFO error */
1101 | 0x1000 /* Enable DACs High Pass Filter */
1102 | 0x0400 /* Disable Coefficient Auto increment */
1103 )},
1104 /* Beep */
1105 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1106 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1107
1108 {} /* terminator */
1109};
1110
Brian Austina769cbc2010-09-07 14:36:22 -05001111/* Errata: CS4207 rev C0/C1/C2 Silicon
1112 *
1113 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1114 *
1115 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1116 * may be excessive (up to an additional 200 μA), which is most easily
1117 * observed while the part is being held in reset (RESET# active low).
1118 *
1119 * Root Cause: At initial powerup of the device, the logic that drives
1120 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1121 * initialized.
1122 * Certain random patterns will cause a steady leakage current in those
1123 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1124 *
1125 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1126 * blocks, which will alleviate the issue.
1127 */
1128
Takashi Iwaic42d4782011-05-02 11:36:09 +02001129static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001130 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1131 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1132
1133 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1134 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1135 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1136 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1137 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1138 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1139
1140 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1141 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1142
1143 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1144 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1145 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1146 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1147 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1148 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1149 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1150
Takashi Iwai38c07642011-03-03 14:54:19 +01001151#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001152 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1153 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1154 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001155#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001156
1157 {} /* terminator */
1158};
1159
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001160/* SPDIF setup */
1161static void init_digital(struct hda_codec *codec)
1162{
1163 unsigned int coef;
1164
1165 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1166 coef |= 0x0008; /* Replace with mute on error */
1167 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1168 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1169 * SPDIF_OUT2 is shared with GPIO1 and
1170 * DMIC_SDA2.
1171 */
1172 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001173}
1174
1175static int cs_init(struct hda_codec *codec)
1176{
1177 struct cs_spec *spec = codec->spec;
1178
Brian Austina769cbc2010-09-07 14:36:22 -05001179 /* init_verb sequence for C0/C1/C2 errata*/
1180 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1181
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001182 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001183
1184 if (spec->gpio_mask) {
1185 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1186 spec->gpio_mask);
1187 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1188 spec->gpio_dir);
1189 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1190 spec->gpio_data);
1191 }
1192
Takashi Iwaie5f14242009-07-01 18:11:44 +02001193 init_output(codec);
1194 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001195 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001196 snd_hda_jack_report_sync(codec);
1197
Takashi Iwaie5f14242009-07-01 18:11:44 +02001198 return 0;
1199}
1200
1201static int cs_build_controls(struct hda_codec *codec)
1202{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001203 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001204 int err;
1205
1206 err = build_output(codec);
1207 if (err < 0)
1208 return err;
1209 err = build_input(codec);
1210 if (err < 0)
1211 return err;
1212 err = build_digital_output(codec);
1213 if (err < 0)
1214 return err;
1215 err = build_digital_input(codec);
1216 if (err < 0)
1217 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001218 err = cs_init(codec);
1219 if (err < 0)
1220 return err;
1221
1222 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1223 if (err < 0)
1224 return err;
1225
1226 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001227}
1228
1229static void cs_free(struct hda_codec *codec)
1230{
1231 struct cs_spec *spec = codec->spec;
1232 kfree(spec->capture_bind[0]);
1233 kfree(spec->capture_bind[1]);
1234 kfree(codec->spec);
1235}
1236
1237static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1238{
Takashi Iwai3a938972011-10-28 01:16:55 +02001239 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001240 case HP_EVENT:
1241 cs_automute(codec);
1242 break;
1243 case MIC_EVENT:
1244 cs_automic(codec);
1245 break;
1246 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001247 snd_hda_jack_report_sync(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001248}
1249
Takashi Iwaic42d4782011-05-02 11:36:09 +02001250static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001251 .build_controls = cs_build_controls,
1252 .build_pcms = cs_build_pcms,
1253 .init = cs_init,
1254 .free = cs_free,
1255 .unsol_event = cs_unsol_event,
1256};
1257
1258static int cs_parse_auto_config(struct hda_codec *codec)
1259{
1260 struct cs_spec *spec = codec->spec;
1261 int err;
1262
1263 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1264 if (err < 0)
1265 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001266
1267 err = parse_output(codec);
1268 if (err < 0)
1269 return err;
1270 err = parse_input(codec);
1271 if (err < 0)
1272 return err;
1273 err = parse_digital_output(codec);
1274 if (err < 0)
1275 return err;
1276 err = parse_digital_input(codec);
1277 if (err < 0)
1278 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001279 return 0;
1280}
1281
Takashi Iwaiea734962011-01-17 11:29:34 +01001282static const char * const cs420x_models[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001283 [CS420X_MBP53] = "mbp53",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001284 [CS420X_MBP55] = "mbp55",
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001285 [CS420X_IMAC27] = "imac27",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001286 [CS420X_AUTO] = "auto",
1287};
1288
1289
Takashi Iwaic42d4782011-05-02 11:36:09 +02001290static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001291 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001292 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001293 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001294 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001295 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001296 {} /* terminator */
1297};
1298
1299struct cs_pincfg {
1300 hda_nid_t nid;
1301 u32 val;
1302};
1303
Takashi Iwaic42d4782011-05-02 11:36:09 +02001304static const struct cs_pincfg mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001305 { 0x09, 0x012b4050 },
1306 { 0x0a, 0x90100141 },
1307 { 0x0b, 0x90100140 },
1308 { 0x0c, 0x018b3020 },
1309 { 0x0d, 0x90a00110 },
1310 { 0x0e, 0x400000f0 },
1311 { 0x0f, 0x01cbe030 },
1312 { 0x10, 0x014be060 },
1313 { 0x12, 0x400000f0 },
1314 { 0x15, 0x400000f0 },
1315 {} /* terminator */
1316};
1317
Takashi Iwaic42d4782011-05-02 11:36:09 +02001318static const struct cs_pincfg mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001319 { 0x09, 0x012b4030 },
1320 { 0x0a, 0x90100121 },
1321 { 0x0b, 0x90100120 },
1322 { 0x0c, 0x400000f0 },
1323 { 0x0d, 0x90a00110 },
1324 { 0x0e, 0x400000f0 },
1325 { 0x0f, 0x400000f0 },
1326 { 0x10, 0x014be040 },
1327 { 0x12, 0x400000f0 },
1328 { 0x15, 0x400000f0 },
1329 {} /* terminator */
1330};
1331
Takashi Iwaic42d4782011-05-02 11:36:09 +02001332static const struct cs_pincfg imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001333 { 0x09, 0x012b4050 },
1334 { 0x0a, 0x90100140 },
1335 { 0x0b, 0x90100142 },
1336 { 0x0c, 0x018b3020 },
1337 { 0x0d, 0x90a00110 },
1338 { 0x0e, 0x400000f0 },
1339 { 0x0f, 0x01cbe030 },
1340 { 0x10, 0x014be060 },
1341 { 0x12, 0x01ab9070 },
1342 { 0x15, 0x400000f0 },
1343 {} /* terminator */
1344};
1345
Takashi Iwaic42d4782011-05-02 11:36:09 +02001346static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001347 [CS420X_MBP53] = mbp53_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001348 [CS420X_MBP55] = mbp55_pincfgs,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001349 [CS420X_IMAC27] = imac27_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001350};
1351
Tim Howe56487c22011-07-22 16:41:00 -05001352static void fix_pincfg(struct hda_codec *codec, int model,
1353 const struct cs_pincfg **pin_configs)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001354{
Tim Howe56487c22011-07-22 16:41:00 -05001355 const struct cs_pincfg *cfg = pin_configs[model];
Takashi Iwaia6bae202009-07-06 15:15:22 +02001356 if (!cfg)
1357 return;
1358 for (; cfg->nid; cfg++)
1359 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1360}
1361
Takashi Iwaie5f14242009-07-01 18:11:44 +02001362static int patch_cs420x(struct hda_codec *codec)
1363{
1364 struct cs_spec *spec;
1365 int err;
1366
1367 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1368 if (!spec)
1369 return -ENOMEM;
1370 codec->spec = spec;
1371
Tim Howe56487c22011-07-22 16:41:00 -05001372 spec->vendor_nid = CS420X_VENDOR_NID;
1373
Takashi Iwaia6bae202009-07-06 15:15:22 +02001374 spec->board_config =
1375 snd_hda_check_board_config(codec, CS420X_MODELS,
1376 cs420x_models, cs420x_cfg_tbl);
1377 if (spec->board_config >= 0)
Tim Howe56487c22011-07-22 16:41:00 -05001378 fix_pincfg(codec, spec->board_config, cs_pincfgs);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001379
Takashi Iwaied208252009-07-07 09:04:26 +02001380 switch (spec->board_config) {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001381 case CS420X_IMAC27:
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001382 case CS420X_MBP53:
Takashi Iwaied208252009-07-07 09:04:26 +02001383 case CS420X_MBP55:
Stelian Pop3a385162009-07-30 14:44:27 +02001384 /* GPIO1 = headphones */
1385 /* GPIO3 = speakers */
1386 spec->gpio_mask = 0x0a;
1387 spec->gpio_dir = 0x0a;
Takashi Iwaied208252009-07-07 09:04:26 +02001388 break;
1389 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001390
Takashi Iwaied208252009-07-07 09:04:26 +02001391 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001392 if (err < 0)
1393 goto error;
1394
Takashi Iwaie5f14242009-07-01 18:11:44 +02001395 codec->patch_ops = cs_patch_ops;
1396
1397 return 0;
1398
1399 error:
1400 kfree(codec->spec);
1401 codec->spec = NULL;
1402 return err;
1403}
1404
Tim Howe56487c22011-07-22 16:41:00 -05001405/*
1406 * Cirrus Logic CS4210
1407 *
1408 * 1 DAC => HP(sense) / Speakers,
1409 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1410 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1411*/
1412
1413/* CS4210 board names */
1414static const char *cs421x_models[CS421X_MODELS] = {
1415 [CS421X_CDB4210] = "cdb4210",
1416};
1417
1418static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1419 /* Test Intel board + CDB2410 */
1420 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1421 {} /* terminator */
1422};
1423
1424/* CS4210 board pinconfigs */
1425/* Default CS4210 (CDB4210)*/
1426static const struct cs_pincfg cdb4210_pincfgs[] = {
1427 { 0x05, 0x0321401f },
1428 { 0x06, 0x90170010 },
1429 { 0x07, 0x03813031 },
1430 { 0x08, 0xb7a70037 },
1431 { 0x09, 0xb7a6003e },
1432 { 0x0a, 0x034510f0 },
1433 {} /* terminator */
1434};
1435
1436static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1437 [CS421X_CDB4210] = cdb4210_pincfgs,
1438};
1439
1440static const struct hda_verb cs421x_coef_init_verbs[] = {
1441 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1442 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1443 /*
1444 Disable Coefficient Index Auto-Increment(DAI)=1,
1445 PDREF=0
1446 */
1447 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1448
1449 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1450 /* ADC SZCMode = Digital Soft Ramp */
1451 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1452
1453 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1454 {0x0B, AC_VERB_SET_PROC_COEF,
1455 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1456 | 0x0004 /* Mute DAC on FIFO error */
1457 | 0x0008 /* Enable DAC High Pass Filter */
1458 )},
1459 {} /* terminator */
1460};
1461
1462/* Errata: CS4210 rev A1 Silicon
1463 *
1464 * http://www.cirrus.com/en/pubs/errata/
1465 *
1466 * Description:
1467 * 1. Performance degredation is present in the ADC.
1468 * 2. Speaker output is not completely muted upon HP detect.
1469 * 3. Noise is present when clipping occurs on the amplified
1470 * speaker outputs.
1471 *
1472 * Workaround:
1473 * The following verb sequence written to the registers during
1474 * initialization will correct the issues listed above.
1475 */
1476
1477static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1478 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1479
1480 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1481 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1482
1483 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1484 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1485
1486 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1487 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1488
1489 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1490 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1491
1492 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1493 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1494
1495 {} /* terminator */
1496};
1497
1498/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1499static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1500
1501static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1502 struct snd_ctl_elem_info *uinfo)
1503{
1504 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1505 uinfo->count = 1;
1506 uinfo->value.integer.min = 0;
1507 uinfo->value.integer.max = 3;
1508 return 0;
1509}
1510
1511static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1512 struct snd_ctl_elem_value *ucontrol)
1513{
1514 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1515
1516 ucontrol->value.integer.value[0] =
1517 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1518 return 0;
1519}
1520
1521static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1522 struct snd_ctl_elem_value *ucontrol)
1523{
1524 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1525
1526 unsigned int vol = ucontrol->value.integer.value[0];
1527 unsigned int coef =
1528 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1529 unsigned int original_coef = coef;
1530
1531 coef &= ~0x0003;
1532 coef |= (vol & 0x0003);
1533 if (original_coef == coef)
1534 return 0;
1535 else {
1536 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1537 return 1;
1538 }
1539}
1540
1541static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1542
1543 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1544 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1545 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1546 .name = "Speaker Boost Playback Volume",
1547 .info = cs421x_boost_vol_info,
1548 .get = cs421x_boost_vol_get,
1549 .put = cs421x_boost_vol_put,
1550 .tlv = { .p = cs421x_speaker_boost_db_scale },
1551};
1552
1553static void cs421x_pinmux_init(struct hda_codec *codec)
1554{
1555 struct cs_spec *spec = codec->spec;
1556 unsigned int def_conf, coef;
1557
1558 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1559 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1560
1561 if (spec->gpio_mask)
1562 coef |= 0x0008; /* B1,B2 are GPIOs */
1563 else
1564 coef &= ~0x0008;
1565
1566 if (spec->sense_b)
1567 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1568 else
1569 coef &= ~0x0010;
1570
1571 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1572
1573 if ((spec->gpio_mask || spec->sense_b) &&
1574 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1575
1576 /*
1577 GPIO or SENSE_B forced - disconnect the DMIC pin.
1578 */
1579 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1580 def_conf &= ~AC_DEFCFG_PORT_CONN;
1581 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1582 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1583 }
1584}
1585
1586static void init_cs421x_digital(struct hda_codec *codec)
1587{
1588 struct cs_spec *spec = codec->spec;
1589 struct auto_pin_cfg *cfg = &spec->autocfg;
1590 int i;
1591
1592
1593 for (i = 0; i < cfg->dig_outs; i++) {
1594 hda_nid_t nid = cfg->dig_out_pins[i];
1595 if (!cfg->speaker_outs)
1596 continue;
1597 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001598 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
Tim Howe56487c22011-07-22 16:41:00 -05001599 spec->spdif_detect = 1;
1600 }
1601 }
1602}
1603
1604static int cs421x_init(struct hda_codec *codec)
1605{
1606 struct cs_spec *spec = codec->spec;
1607
1608 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1609 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1610
1611 cs421x_pinmux_init(codec);
1612
1613 if (spec->gpio_mask) {
1614 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1615 spec->gpio_mask);
1616 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1617 spec->gpio_dir);
1618 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1619 spec->gpio_data);
1620 }
1621
1622 init_output(codec);
1623 init_input(codec);
1624 init_cs421x_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001625 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001626
1627 return 0;
1628}
1629
1630/*
1631 * CS4210 Input MUX (1 ADC)
1632 */
1633static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1634 struct snd_ctl_elem_info *uinfo)
1635{
1636 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1637 struct cs_spec *spec = codec->spec;
1638
1639 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1640}
1641
1642static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1643 struct snd_ctl_elem_value *ucontrol)
1644{
1645 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1646 struct cs_spec *spec = codec->spec;
1647
1648 ucontrol->value.enumerated.item[0] = spec->cur_input;
1649 return 0;
1650}
1651
1652static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1653 struct snd_ctl_elem_value *ucontrol)
1654{
1655 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1656 struct cs_spec *spec = codec->spec;
1657
1658 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1659 spec->adc_nid[0], &spec->cur_input);
1660
1661}
1662
1663static struct snd_kcontrol_new cs421x_capture_source = {
1664
1665 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1666 .name = "Capture Source",
1667 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1668 .info = cs421x_mux_enum_info,
1669 .get = cs421x_mux_enum_get,
1670 .put = cs421x_mux_enum_put,
1671};
1672
1673static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1674{
1675 struct cs_spec *spec = codec->spec;
1676 struct auto_pin_cfg *cfg = &spec->autocfg;
1677 const struct hda_input_mux *imux = &spec->input_mux;
1678 hda_nid_t pin = cfg->inputs[item].pin;
1679 struct snd_kcontrol *kctl;
1680 u32 caps;
1681
1682 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1683 return 0;
1684
1685 caps = query_amp_caps(codec, pin, HDA_INPUT);
1686 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1687 if (caps <= 1)
1688 return 0;
1689
1690 return add_volume(codec, imux->items[item].label, 0,
1691 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1692}
1693
1694/* add a (input-boost) volume control to the given input pin */
1695static int build_cs421x_input(struct hda_codec *codec)
1696{
1697 struct cs_spec *spec = codec->spec;
1698 struct auto_pin_cfg *cfg = &spec->autocfg;
1699 struct hda_input_mux *imux = &spec->input_mux;
1700 int i, err, type_idx;
1701 const char *label;
1702
1703 if (!spec->num_inputs)
1704 return 0;
1705
1706 /* make bind-capture */
1707 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1708 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1709 for (i = 0; i < 2; i++) {
1710 struct snd_kcontrol *kctl;
1711 int n;
1712 if (!spec->capture_bind[i])
1713 return -ENOMEM;
1714 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1715 if (!kctl)
1716 return -ENOMEM;
1717 kctl->private_value = (long)spec->capture_bind[i];
1718 err = snd_hda_ctl_add(codec, 0, kctl);
1719 if (err < 0)
1720 return err;
1721 for (n = 0; n < AUTO_PIN_LAST; n++) {
1722 if (!spec->adc_nid[n])
1723 continue;
1724 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1725 if (err < 0)
1726 return err;
1727 }
1728 }
1729
1730 /* Add Input MUX Items + Capture Volume/Switch */
1731 for (i = 0; i < spec->num_inputs; i++) {
1732 label = hda_get_autocfg_input_label(codec, cfg, i);
1733 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1734
1735 err = cs421x_add_input_volume_control(codec, i);
1736 if (err < 0)
1737 return err;
1738 }
1739
1740 /*
1741 Add 'Capture Source' Switch if
1742 * 2 inputs and no mic detec
1743 * 3 inputs
1744 */
1745 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1746 (spec->num_inputs == 3)) {
1747
1748 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1749 snd_ctl_new1(&cs421x_capture_source, codec));
1750 if (err < 0)
1751 return err;
1752 }
1753
1754 return 0;
1755}
1756
1757/* Single DAC (Mute/Gain) */
1758static int build_cs421x_output(struct hda_codec *codec)
1759{
1760 hda_nid_t dac = CS4210_DAC_NID;
1761 struct cs_spec *spec = codec->spec;
1762 struct auto_pin_cfg *cfg = &spec->autocfg;
1763 struct snd_kcontrol *kctl;
1764 int err;
1765 char *name = "HP/Speakers";
1766
1767 fix_volume_caps(codec, dac);
1768 if (!spec->vmaster_sw) {
1769 err = add_vmaster(codec, dac);
1770 if (err < 0)
1771 return err;
1772 }
1773
1774 err = add_mute(codec, name, 0,
1775 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1776 if (err < 0)
1777 return err;
1778 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1779 if (err < 0)
1780 return err;
1781
1782 err = add_volume(codec, name, 0,
1783 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1784 if (err < 0)
1785 return err;
1786 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1787 if (err < 0)
1788 return err;
1789
1790 if (cfg->speaker_outs) {
1791 err = snd_hda_ctl_add(codec, 0,
1792 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1793 if (err < 0)
1794 return err;
1795 }
1796 return err;
1797}
1798
1799static int cs421x_build_controls(struct hda_codec *codec)
1800{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001801 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001802 int err;
1803
1804 err = build_cs421x_output(codec);
1805 if (err < 0)
1806 return err;
1807 err = build_cs421x_input(codec);
1808 if (err < 0)
1809 return err;
1810 err = build_digital_output(codec);
1811 if (err < 0)
1812 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001813 err = cs421x_init(codec);
1814 if (err < 0)
1815 return err;
1816
1817 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1818 if (err < 0)
1819 return err;
1820
1821 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001822}
1823
1824static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1825{
Takashi Iwai3a938972011-10-28 01:16:55 +02001826 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Tim Howe56487c22011-07-22 16:41:00 -05001827 case HP_EVENT:
1828 case SPDIF_EVENT:
1829 cs_automute(codec);
1830 break;
1831
1832 case MIC_EVENT:
1833 cs_automic(codec);
1834 break;
1835 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001836 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001837}
1838
1839static int parse_cs421x_input(struct hda_codec *codec)
1840{
1841 struct cs_spec *spec = codec->spec;
1842 struct auto_pin_cfg *cfg = &spec->autocfg;
1843 int i;
1844
1845 for (i = 0; i < cfg->num_inputs; i++) {
1846 hda_nid_t pin = cfg->inputs[i].pin;
1847 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1848 spec->cur_input = spec->last_input = i;
1849 spec->num_inputs++;
1850
1851 /* check whether the automatic mic switch is available */
1852 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1853 spec->mic_detect = 1;
1854 spec->automic_idx = i;
1855 }
1856 }
1857 return 0;
1858}
1859
1860static int cs421x_parse_auto_config(struct hda_codec *codec)
1861{
1862 struct cs_spec *spec = codec->spec;
1863 int err;
1864
1865 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1866 if (err < 0)
1867 return err;
1868 err = parse_output(codec);
1869 if (err < 0)
1870 return err;
1871 err = parse_cs421x_input(codec);
1872 if (err < 0)
1873 return err;
1874 err = parse_digital_output(codec);
1875 if (err < 0)
1876 return err;
1877 return 0;
1878}
1879
1880#ifdef CONFIG_PM
1881/*
1882 Manage PDREF, when transitioning to D3hot
1883 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1884*/
1885static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1886{
1887 unsigned int coef;
1888
1889 snd_hda_shutup_pins(codec);
1890
1891 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1892 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1893 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1894 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1895
1896 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1897 coef |= 0x0004; /* PDREF */
1898 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1899
1900 return 0;
1901}
1902#endif
1903
1904static struct hda_codec_ops cs4210_patch_ops = {
1905 .build_controls = cs421x_build_controls,
1906 .build_pcms = cs_build_pcms,
1907 .init = cs421x_init,
1908 .free = cs_free,
1909 .unsol_event = cs421x_unsol_event,
1910#ifdef CONFIG_PM
1911 .suspend = cs421x_suspend,
1912#endif
1913};
1914
1915static int patch_cs421x(struct hda_codec *codec)
1916{
1917 struct cs_spec *spec;
1918 int err;
1919
1920 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1921 if (!spec)
1922 return -ENOMEM;
1923 codec->spec = spec;
1924
1925 spec->vendor_nid = CS421X_VENDOR_NID;
1926
1927 spec->board_config =
1928 snd_hda_check_board_config(codec, CS421X_MODELS,
1929 cs421x_models, cs421x_cfg_tbl);
1930 if (spec->board_config >= 0)
1931 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1932 /*
1933 Setup GPIO/SENSE for each board (if used)
1934 */
1935 switch (spec->board_config) {
1936 case CS421X_CDB4210:
1937 snd_printd("CS4210 board: %s\n",
1938 cs421x_models[spec->board_config]);
1939/* spec->gpio_mask = 3;
1940 spec->gpio_dir = 3;
1941 spec->gpio_data = 3;
1942*/
1943 spec->sense_b = 1;
1944
1945 break;
1946 }
1947
1948 /*
1949 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1950 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1951 is disabled.
1952 */
1953 cs421x_pinmux_init(codec);
1954
1955 err = cs421x_parse_auto_config(codec);
1956 if (err < 0)
1957 goto error;
1958
1959 codec->patch_ops = cs4210_patch_ops;
1960
1961 return 0;
1962
1963 error:
1964 kfree(codec->spec);
1965 codec->spec = NULL;
1966 return err;
1967}
1968
Takashi Iwaie5f14242009-07-01 18:11:44 +02001969
1970/*
1971 * patch entries
1972 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02001973static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001974 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1975 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
Tim Howe56487c22011-07-22 16:41:00 -05001976 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
Takashi Iwaie5f14242009-07-01 18:11:44 +02001977 {} /* terminator */
1978};
1979
1980MODULE_ALIAS("snd-hda-codec-id:10134206");
1981MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05001982MODULE_ALIAS("snd-hda-codec-id:10134210");
Takashi Iwaie5f14242009-07-01 18:11:44 +02001983
1984MODULE_LICENSE("GPL");
1985MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1986
1987static struct hda_codec_preset_list cirrus_list = {
1988 .preset = snd_hda_preset_cirrus,
1989 .owner = THIS_MODULE,
1990};
1991
1992static int __init patch_cirrus_init(void)
1993{
1994 return snd_hda_add_codec_preset(&cirrus_list);
1995}
1996
1997static void __exit patch_cirrus_exit(void)
1998{
1999 snd_hda_delete_codec_preset(&cirrus_list);
2000}
2001
2002module_init(patch_cirrus_init)
2003module_exit(patch_cirrus_exit)