blob: 6f158777f71ac0d6a9e3a8ab70072ad733d80a58 [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 Iwai10a20af2010-09-09 16:28:02 +0200714 strcpy(uinfo->value.enumerated.name,
Takashi Iwai04f5ade2011-10-27 20:47:07 +0200715 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, NULL));
Takashi Iwaie5f14242009-07-01 18:11:44 +0200716 return 0;
717}
718
719static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
720 struct snd_ctl_elem_value *ucontrol)
721{
722 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
723 struct cs_spec *spec = codec->spec;
724 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
725 return 0;
726}
727
728static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
729 struct snd_ctl_elem_value *ucontrol)
730{
731 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
732 struct cs_spec *spec = codec->spec;
733 unsigned int idx = ucontrol->value.enumerated.item[0];
734
735 if (idx >= spec->num_inputs)
736 return -EINVAL;
737 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200738 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200739}
740
Takashi Iwaic42d4782011-05-02 11:36:09 +0200741static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200742 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
743 .name = "Capture Source",
744 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
745 .info = cs_capture_source_info,
746 .get = cs_capture_source_get,
747 .put = cs_capture_source_put,
748};
749
Takashi Iwaic42d4782011-05-02 11:36:09 +0200750static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200751 struct hda_ctl_ops *ops)
752{
753 struct cs_spec *spec = codec->spec;
754 struct hda_bind_ctls *bind;
755 int i, n;
756
757 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
758 GFP_KERNEL);
759 if (!bind)
760 return NULL;
761 bind->ops = ops;
762 n = 0;
763 for (i = 0; i < AUTO_PIN_LAST; i++) {
764 if (!spec->adc_nid[i])
765 continue;
766 bind->values[n++] =
767 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
768 spec->adc_idx[i], HDA_INPUT);
769 }
770 return bind;
771}
772
Takashi Iwai6a929342010-10-11 15:16:20 +0200773/* add a (input-boost) volume control to the given input pin */
774static int add_input_volume_control(struct hda_codec *codec,
775 struct auto_pin_cfg *cfg,
776 int item)
777{
778 hda_nid_t pin = cfg->inputs[item].pin;
779 u32 caps;
780 const char *label;
781 struct snd_kcontrol *kctl;
782
783 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
784 return 0;
785 caps = query_amp_caps(codec, pin, HDA_INPUT);
786 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
787 if (caps <= 1)
788 return 0;
789 label = hda_get_autocfg_input_label(codec, cfg, item);
790 return add_volume(codec, label, 0,
791 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
792}
793
Takashi Iwaie5f14242009-07-01 18:11:44 +0200794static int build_input(struct hda_codec *codec)
795{
796 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200797 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200798
Takashi Iwaie5f14242009-07-01 18:11:44 +0200799 if (!spec->num_inputs)
800 return 0;
801
Takashi Iwaie5f14242009-07-01 18:11:44 +0200802 /* make bind-capture */
803 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
804 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
805 for (i = 0; i < 2; i++) {
806 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100807 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200808 if (!spec->capture_bind[i])
809 return -ENOMEM;
810 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
811 if (!kctl)
812 return -ENOMEM;
813 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100814 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200815 if (err < 0)
816 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100817 for (n = 0; n < AUTO_PIN_LAST; n++) {
818 if (!spec->adc_nid[n])
819 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700820 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100821 if (err < 0)
822 return err;
823 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200824 }
825
826 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100827 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200828 snd_ctl_new1(&cs_capture_source, codec));
829 if (err < 0)
830 return err;
831 }
832
Takashi Iwai6a929342010-10-11 15:16:20 +0200833 for (i = 0; i < spec->num_inputs; i++) {
834 err = add_input_volume_control(codec, &spec->autocfg, i);
835 if (err < 0)
836 return err;
837 }
838
Takashi Iwaie5f14242009-07-01 18:11:44 +0200839 return 0;
840}
841
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200842/*
843 */
844
Takashi Iwaie5f14242009-07-01 18:11:44 +0200845static int build_digital_output(struct hda_codec *codec)
846{
847 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200848 int err;
849
Takashi Iwai63b24132009-07-09 11:45:59 +0200850 if (!spec->multiout.dig_out_nid)
851 return 0;
852
Stephen Warren74b654c2011-06-01 11:14:18 -0600853 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
854 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200855 if (err < 0)
856 return err;
857 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
858 if (err < 0)
859 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200860 return 0;
861}
862
863static int build_digital_input(struct hda_codec *codec)
864{
865 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200866 if (spec->dig_in)
867 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
868 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200869}
870
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200871/*
872 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500873 * CS421x auto-output redirecting
874 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200875 */
876
Takashi Iwaie5f14242009-07-01 18:11:44 +0200877static void cs_automute(struct hda_codec *codec)
878{
879 struct cs_spec *spec = codec->spec;
880 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200881 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500882 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200883 hda_nid_t nid;
884 int i;
885
Tim Howe56487c22011-07-22 16:41:00 -0500886 spdif_present = 0;
887 if (cfg->dig_outs) {
888 nid = cfg->dig_out_pins[0];
889 if (is_jack_detectable(codec, nid)) {
890 /*
891 TODO: SPDIF output redirect when SENSE_B is enabled.
892 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
893 assumed.
894 */
895 if (snd_hda_jack_detect(codec, nid)
896 /* && spec->sense_b */)
897 spdif_present = 1;
898 }
899 }
900
Takashi Iwaie5f14242009-07-01 18:11:44 +0200901 hp_present = 0;
902 for (i = 0; i < cfg->hp_outs; i++) {
903 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200904 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200905 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800906 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200907 if (hp_present)
908 break;
909 }
Tim Howe56487c22011-07-22 16:41:00 -0500910
911 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200912 for (i = 0; i < cfg->speaker_outs; i++) {
913 nid = cfg->speaker_pins[i];
914 snd_hda_codec_write(codec, nid, 0,
915 AC_VERB_SET_PIN_WIDGET_CONTROL,
916 hp_present ? 0 : PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -0500917 /* detect on spdif is specific to CS421x */
918 if (spec->vendor_nid == CS421X_VENDOR_NID) {
919 snd_hda_codec_write(codec, nid, 0,
920 AC_VERB_SET_PIN_WIDGET_CONTROL,
921 spdif_present ? 0 : PIN_OUT);
922 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200923 }
Vince Weaver4e7d7c62010-09-22 17:31:37 -0400924 if (spec->board_config == CS420X_MBP53 ||
925 spec->board_config == CS420X_MBP55 ||
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +0100926 spec->board_config == CS420X_IMAC27) {
Stelian Pop3a385162009-07-30 14:44:27 +0200927 unsigned int gpio = hp_present ? 0x02 : 0x08;
928 snd_hda_codec_write(codec, 0x01, 0,
929 AC_VERB_SET_GPIO_DATA, gpio);
930 }
Tim Howe56487c22011-07-22 16:41:00 -0500931
932 /* specific to CS421x */
933 if (spec->vendor_nid == CS421X_VENDOR_NID) {
934 /* mute HPs if spdif jack (SENSE_B) is present */
935 for (i = 0; i < cfg->hp_outs; i++) {
936 nid = cfg->hp_pins[i];
937 snd_hda_codec_write(codec, nid, 0,
938 AC_VERB_SET_PIN_WIDGET_CONTROL,
939 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
940 }
941
942 /* SPDIF TX on/off */
943 if (cfg->dig_outs) {
944 nid = cfg->dig_out_pins[0];
945 snd_hda_codec_write(codec, nid, 0,
946 AC_VERB_SET_PIN_WIDGET_CONTROL,
947 spdif_present ? PIN_OUT : 0);
948
949 }
950 /* Update board GPIOs if neccessary ... */
951 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200952}
953
Tim Howe56487c22011-07-22 16:41:00 -0500954/*
955 * Auto-input redirect for CS421x
956 * Switch max 3 inputs of a single ADC (nid 3)
957*/
958
Takashi Iwaie5f14242009-07-01 18:11:44 +0200959static void cs_automic(struct hda_codec *codec)
960{
961 struct cs_spec *spec = codec->spec;
962 struct auto_pin_cfg *cfg = &spec->autocfg;
963 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800964 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500965
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200966 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800967 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500968
969 /* specific to CS421x, single ADC */
970 if (spec->vendor_nid == CS421X_VENDOR_NID) {
971 if (present) {
972 spec->last_input = spec->cur_input;
973 spec->cur_input = spec->automic_idx;
974 } else {
975 spec->cur_input = spec->last_input;
976 }
977
978 snd_hda_codec_write_cache(codec, spec->cur_adc, 0,
979 AC_VERB_SET_CONNECT_SEL,
980 spec->adc_idx[spec->cur_input]);
981 } else {
982 if (present)
983 change_cur_input(codec, spec->automic_idx, 0);
984 else
985 change_cur_input(codec, !spec->automic_idx, 0);
986 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200987}
988
989/*
990 */
991
992static void init_output(struct hda_codec *codec)
993{
994 struct cs_spec *spec = codec->spec;
995 struct auto_pin_cfg *cfg = &spec->autocfg;
996 int i;
997
998 /* mute first */
999 for (i = 0; i < spec->multiout.num_dacs; i++)
1000 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1001 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1002 if (spec->multiout.hp_nid)
1003 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1004 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1005 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1006 if (!spec->multiout.extra_out_nid[i])
1007 break;
1008 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1009 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1010 }
1011
1012 /* set appropriate pin controls */
1013 for (i = 0; i < cfg->line_outs; i++)
1014 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1015 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001016 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001017 for (i = 0; i < cfg->hp_outs; i++) {
1018 hda_nid_t nid = cfg->hp_pins[i];
1019 snd_hda_codec_write(codec, nid, 0,
1020 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1021 if (!cfg->speaker_outs)
1022 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001023 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001024 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001025 spec->hp_detect = 1;
1026 }
1027 }
Tim Howe56487c22011-07-22 16:41:00 -05001028
1029 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001030 for (i = 0; i < cfg->speaker_outs; i++)
1031 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1032 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001033
1034 /* SPDIF is enabled on presence detect for CS421x */
1035 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001036 cs_automute(codec);
1037}
1038
1039static void init_input(struct hda_codec *codec)
1040{
1041 struct cs_spec *spec = codec->spec;
1042 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001043 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001044 int i;
1045
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001046 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001047 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001048 hda_nid_t pin = cfg->inputs[i].pin;
1049 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001050 continue;
1051 /* set appropriate pin control and mute first */
1052 ctl = PIN_IN;
Takashi Iwai86e29592010-09-09 14:50:17 +02001053 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001054 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1055 caps >>= AC_PINCAP_VREF_SHIFT;
1056 if (caps & AC_PINCAP_VREF_80)
1057 ctl = PIN_VREF80;
1058 }
1059 snd_hda_codec_write(codec, pin, 0,
1060 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1061 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1062 AC_VERB_SET_AMP_GAIN_MUTE,
1063 AMP_IN_MUTE(spec->adc_idx[i]));
1064 if (spec->mic_detect && spec->automic_idx == i)
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001065 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001066 }
Tim Howe56487c22011-07-22 16:41:00 -05001067 /* specific to CS421x */
1068 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1069 if (spec->mic_detect)
1070 cs_automic(codec);
1071 else {
1072 spec->cur_adc = spec->adc_nid[spec->cur_input];
1073 snd_hda_codec_write(codec, spec->cur_adc, 0,
1074 AC_VERB_SET_CONNECT_SEL,
1075 spec->adc_idx[spec->cur_input]);
1076 }
1077 } else {
1078 change_cur_input(codec, spec->cur_input, 1);
1079 if (spec->mic_detect)
1080 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001081
Tim Howe56487c22011-07-22 16:41:00 -05001082 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1083 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1084 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1085 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1086 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1087 * No effect if SPDIF_OUT2 is
1088 * selected in IDX_SPDIF_CTL.
1089 */
1090 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1091 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001092}
1093
Takashi Iwaic42d4782011-05-02 11:36:09 +02001094static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001095 {0x11, AC_VERB_SET_PROC_STATE, 1},
1096 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1097 {0x11, AC_VERB_SET_PROC_COEF,
1098 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1099 | 0x0040 /* Mute DACs on FIFO error */
1100 | 0x1000 /* Enable DACs High Pass Filter */
1101 | 0x0400 /* Disable Coefficient Auto increment */
1102 )},
1103 /* Beep */
1104 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1105 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1106
1107 {} /* terminator */
1108};
1109
Brian Austina769cbc2010-09-07 14:36:22 -05001110/* Errata: CS4207 rev C0/C1/C2 Silicon
1111 *
1112 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1113 *
1114 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1115 * may be excessive (up to an additional 200 μA), which is most easily
1116 * observed while the part is being held in reset (RESET# active low).
1117 *
1118 * Root Cause: At initial powerup of the device, the logic that drives
1119 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1120 * initialized.
1121 * Certain random patterns will cause a steady leakage current in those
1122 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1123 *
1124 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1125 * blocks, which will alleviate the issue.
1126 */
1127
Takashi Iwaic42d4782011-05-02 11:36:09 +02001128static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001129 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1130 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1131
1132 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1133 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1134 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1135 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1136 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1137 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1138
1139 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1140 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1141
1142 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1143 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1144 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1145 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1146 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1147 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1148 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1149
Takashi Iwai38c07642011-03-03 14:54:19 +01001150#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001151 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1152 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1153 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001154#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001155
1156 {} /* terminator */
1157};
1158
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001159/* SPDIF setup */
1160static void init_digital(struct hda_codec *codec)
1161{
1162 unsigned int coef;
1163
1164 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1165 coef |= 0x0008; /* Replace with mute on error */
1166 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1167 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1168 * SPDIF_OUT2 is shared with GPIO1 and
1169 * DMIC_SDA2.
1170 */
1171 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001172}
1173
1174static int cs_init(struct hda_codec *codec)
1175{
1176 struct cs_spec *spec = codec->spec;
1177
Brian Austina769cbc2010-09-07 14:36:22 -05001178 /* init_verb sequence for C0/C1/C2 errata*/
1179 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1180
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001181 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001182
1183 if (spec->gpio_mask) {
1184 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1185 spec->gpio_mask);
1186 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1187 spec->gpio_dir);
1188 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1189 spec->gpio_data);
1190 }
1191
Takashi Iwaie5f14242009-07-01 18:11:44 +02001192 init_output(codec);
1193 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001194 init_digital(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001195 return 0;
1196}
1197
1198static int cs_build_controls(struct hda_codec *codec)
1199{
Takashi Iwaie5f14242009-07-01 18:11:44 +02001200 int err;
1201
1202 err = build_output(codec);
1203 if (err < 0)
1204 return err;
1205 err = build_input(codec);
1206 if (err < 0)
1207 return err;
1208 err = build_digital_output(codec);
1209 if (err < 0)
1210 return err;
1211 err = build_digital_input(codec);
1212 if (err < 0)
1213 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001214 return cs_init(codec);
1215}
1216
1217static void cs_free(struct hda_codec *codec)
1218{
1219 struct cs_spec *spec = codec->spec;
1220 kfree(spec->capture_bind[0]);
1221 kfree(spec->capture_bind[1]);
1222 kfree(codec->spec);
1223}
1224
1225static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1226{
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001227 snd_hda_jack_set_dirty_all(codec); /* FIXME: to be more fine-grained */
1228
Takashi Iwaie5f14242009-07-01 18:11:44 +02001229 switch ((res >> 26) & 0x7f) {
1230 case HP_EVENT:
1231 cs_automute(codec);
1232 break;
1233 case MIC_EVENT:
1234 cs_automic(codec);
1235 break;
1236 }
1237}
1238
Takashi Iwaic42d4782011-05-02 11:36:09 +02001239static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001240 .build_controls = cs_build_controls,
1241 .build_pcms = cs_build_pcms,
1242 .init = cs_init,
1243 .free = cs_free,
1244 .unsol_event = cs_unsol_event,
1245};
1246
1247static int cs_parse_auto_config(struct hda_codec *codec)
1248{
1249 struct cs_spec *spec = codec->spec;
1250 int err;
1251
1252 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1253 if (err < 0)
1254 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001255
1256 err = parse_output(codec);
1257 if (err < 0)
1258 return err;
1259 err = parse_input(codec);
1260 if (err < 0)
1261 return err;
1262 err = parse_digital_output(codec);
1263 if (err < 0)
1264 return err;
1265 err = parse_digital_input(codec);
1266 if (err < 0)
1267 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001268 return 0;
1269}
1270
Takashi Iwaiea734962011-01-17 11:29:34 +01001271static const char * const cs420x_models[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001272 [CS420X_MBP53] = "mbp53",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001273 [CS420X_MBP55] = "mbp55",
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001274 [CS420X_IMAC27] = "imac27",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001275 [CS420X_AUTO] = "auto",
1276};
1277
1278
Takashi Iwaic42d4782011-05-02 11:36:09 +02001279static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001280 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001281 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001282 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001283 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001284 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001285 {} /* terminator */
1286};
1287
1288struct cs_pincfg {
1289 hda_nid_t nid;
1290 u32 val;
1291};
1292
Takashi Iwaic42d4782011-05-02 11:36:09 +02001293static const struct cs_pincfg mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001294 { 0x09, 0x012b4050 },
1295 { 0x0a, 0x90100141 },
1296 { 0x0b, 0x90100140 },
1297 { 0x0c, 0x018b3020 },
1298 { 0x0d, 0x90a00110 },
1299 { 0x0e, 0x400000f0 },
1300 { 0x0f, 0x01cbe030 },
1301 { 0x10, 0x014be060 },
1302 { 0x12, 0x400000f0 },
1303 { 0x15, 0x400000f0 },
1304 {} /* terminator */
1305};
1306
Takashi Iwaic42d4782011-05-02 11:36:09 +02001307static const struct cs_pincfg mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001308 { 0x09, 0x012b4030 },
1309 { 0x0a, 0x90100121 },
1310 { 0x0b, 0x90100120 },
1311 { 0x0c, 0x400000f0 },
1312 { 0x0d, 0x90a00110 },
1313 { 0x0e, 0x400000f0 },
1314 { 0x0f, 0x400000f0 },
1315 { 0x10, 0x014be040 },
1316 { 0x12, 0x400000f0 },
1317 { 0x15, 0x400000f0 },
1318 {} /* terminator */
1319};
1320
Takashi Iwaic42d4782011-05-02 11:36:09 +02001321static const struct cs_pincfg imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001322 { 0x09, 0x012b4050 },
1323 { 0x0a, 0x90100140 },
1324 { 0x0b, 0x90100142 },
1325 { 0x0c, 0x018b3020 },
1326 { 0x0d, 0x90a00110 },
1327 { 0x0e, 0x400000f0 },
1328 { 0x0f, 0x01cbe030 },
1329 { 0x10, 0x014be060 },
1330 { 0x12, 0x01ab9070 },
1331 { 0x15, 0x400000f0 },
1332 {} /* terminator */
1333};
1334
Takashi Iwaic42d4782011-05-02 11:36:09 +02001335static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001336 [CS420X_MBP53] = mbp53_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001337 [CS420X_MBP55] = mbp55_pincfgs,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001338 [CS420X_IMAC27] = imac27_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001339};
1340
Tim Howe56487c22011-07-22 16:41:00 -05001341static void fix_pincfg(struct hda_codec *codec, int model,
1342 const struct cs_pincfg **pin_configs)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001343{
Tim Howe56487c22011-07-22 16:41:00 -05001344 const struct cs_pincfg *cfg = pin_configs[model];
Takashi Iwaia6bae202009-07-06 15:15:22 +02001345 if (!cfg)
1346 return;
1347 for (; cfg->nid; cfg++)
1348 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1349}
1350
Takashi Iwaie5f14242009-07-01 18:11:44 +02001351static int patch_cs420x(struct hda_codec *codec)
1352{
1353 struct cs_spec *spec;
1354 int err;
1355
1356 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1357 if (!spec)
1358 return -ENOMEM;
1359 codec->spec = spec;
1360
Tim Howe56487c22011-07-22 16:41:00 -05001361 spec->vendor_nid = CS420X_VENDOR_NID;
1362
Takashi Iwaia6bae202009-07-06 15:15:22 +02001363 spec->board_config =
1364 snd_hda_check_board_config(codec, CS420X_MODELS,
1365 cs420x_models, cs420x_cfg_tbl);
1366 if (spec->board_config >= 0)
Tim Howe56487c22011-07-22 16:41:00 -05001367 fix_pincfg(codec, spec->board_config, cs_pincfgs);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001368
Takashi Iwaied208252009-07-07 09:04:26 +02001369 switch (spec->board_config) {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001370 case CS420X_IMAC27:
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001371 case CS420X_MBP53:
Takashi Iwaied208252009-07-07 09:04:26 +02001372 case CS420X_MBP55:
Stelian Pop3a385162009-07-30 14:44:27 +02001373 /* GPIO1 = headphones */
1374 /* GPIO3 = speakers */
1375 spec->gpio_mask = 0x0a;
1376 spec->gpio_dir = 0x0a;
Takashi Iwaied208252009-07-07 09:04:26 +02001377 break;
1378 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001379
Takashi Iwaied208252009-07-07 09:04:26 +02001380 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001381 if (err < 0)
1382 goto error;
1383
Takashi Iwaie5f14242009-07-01 18:11:44 +02001384 codec->patch_ops = cs_patch_ops;
1385
1386 return 0;
1387
1388 error:
1389 kfree(codec->spec);
1390 codec->spec = NULL;
1391 return err;
1392}
1393
Tim Howe56487c22011-07-22 16:41:00 -05001394/*
1395 * Cirrus Logic CS4210
1396 *
1397 * 1 DAC => HP(sense) / Speakers,
1398 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1399 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1400*/
1401
1402/* CS4210 board names */
1403static const char *cs421x_models[CS421X_MODELS] = {
1404 [CS421X_CDB4210] = "cdb4210",
1405};
1406
1407static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1408 /* Test Intel board + CDB2410 */
1409 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1410 {} /* terminator */
1411};
1412
1413/* CS4210 board pinconfigs */
1414/* Default CS4210 (CDB4210)*/
1415static const struct cs_pincfg cdb4210_pincfgs[] = {
1416 { 0x05, 0x0321401f },
1417 { 0x06, 0x90170010 },
1418 { 0x07, 0x03813031 },
1419 { 0x08, 0xb7a70037 },
1420 { 0x09, 0xb7a6003e },
1421 { 0x0a, 0x034510f0 },
1422 {} /* terminator */
1423};
1424
1425static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1426 [CS421X_CDB4210] = cdb4210_pincfgs,
1427};
1428
1429static const struct hda_verb cs421x_coef_init_verbs[] = {
1430 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1431 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1432 /*
1433 Disable Coefficient Index Auto-Increment(DAI)=1,
1434 PDREF=0
1435 */
1436 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1437
1438 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1439 /* ADC SZCMode = Digital Soft Ramp */
1440 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1441
1442 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1443 {0x0B, AC_VERB_SET_PROC_COEF,
1444 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1445 | 0x0004 /* Mute DAC on FIFO error */
1446 | 0x0008 /* Enable DAC High Pass Filter */
1447 )},
1448 {} /* terminator */
1449};
1450
1451/* Errata: CS4210 rev A1 Silicon
1452 *
1453 * http://www.cirrus.com/en/pubs/errata/
1454 *
1455 * Description:
1456 * 1. Performance degredation is present in the ADC.
1457 * 2. Speaker output is not completely muted upon HP detect.
1458 * 3. Noise is present when clipping occurs on the amplified
1459 * speaker outputs.
1460 *
1461 * Workaround:
1462 * The following verb sequence written to the registers during
1463 * initialization will correct the issues listed above.
1464 */
1465
1466static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1467 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1468
1469 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1470 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1471
1472 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1473 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1474
1475 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1476 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1477
1478 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1479 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1480
1481 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1482 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1483
1484 {} /* terminator */
1485};
1486
1487/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1488static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1489
1490static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1491 struct snd_ctl_elem_info *uinfo)
1492{
1493 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1494 uinfo->count = 1;
1495 uinfo->value.integer.min = 0;
1496 uinfo->value.integer.max = 3;
1497 return 0;
1498}
1499
1500static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_value *ucontrol)
1502{
1503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1504
1505 ucontrol->value.integer.value[0] =
1506 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1507 return 0;
1508}
1509
1510static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1511 struct snd_ctl_elem_value *ucontrol)
1512{
1513 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1514
1515 unsigned int vol = ucontrol->value.integer.value[0];
1516 unsigned int coef =
1517 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1518 unsigned int original_coef = coef;
1519
1520 coef &= ~0x0003;
1521 coef |= (vol & 0x0003);
1522 if (original_coef == coef)
1523 return 0;
1524 else {
1525 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1526 return 1;
1527 }
1528}
1529
1530static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1531
1532 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1533 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1534 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1535 .name = "Speaker Boost Playback Volume",
1536 .info = cs421x_boost_vol_info,
1537 .get = cs421x_boost_vol_get,
1538 .put = cs421x_boost_vol_put,
1539 .tlv = { .p = cs421x_speaker_boost_db_scale },
1540};
1541
1542static void cs421x_pinmux_init(struct hda_codec *codec)
1543{
1544 struct cs_spec *spec = codec->spec;
1545 unsigned int def_conf, coef;
1546
1547 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1548 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1549
1550 if (spec->gpio_mask)
1551 coef |= 0x0008; /* B1,B2 are GPIOs */
1552 else
1553 coef &= ~0x0008;
1554
1555 if (spec->sense_b)
1556 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1557 else
1558 coef &= ~0x0010;
1559
1560 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1561
1562 if ((spec->gpio_mask || spec->sense_b) &&
1563 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1564
1565 /*
1566 GPIO or SENSE_B forced - disconnect the DMIC pin.
1567 */
1568 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1569 def_conf &= ~AC_DEFCFG_PORT_CONN;
1570 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1571 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1572 }
1573}
1574
1575static void init_cs421x_digital(struct hda_codec *codec)
1576{
1577 struct cs_spec *spec = codec->spec;
1578 struct auto_pin_cfg *cfg = &spec->autocfg;
1579 int i;
1580
1581
1582 for (i = 0; i < cfg->dig_outs; i++) {
1583 hda_nid_t nid = cfg->dig_out_pins[i];
1584 if (!cfg->speaker_outs)
1585 continue;
1586 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001587 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
Tim Howe56487c22011-07-22 16:41:00 -05001588 spec->spdif_detect = 1;
1589 }
1590 }
1591}
1592
1593static int cs421x_init(struct hda_codec *codec)
1594{
1595 struct cs_spec *spec = codec->spec;
1596
1597 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1598 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1599
1600 cs421x_pinmux_init(codec);
1601
1602 if (spec->gpio_mask) {
1603 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1604 spec->gpio_mask);
1605 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1606 spec->gpio_dir);
1607 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1608 spec->gpio_data);
1609 }
1610
1611 init_output(codec);
1612 init_input(codec);
1613 init_cs421x_digital(codec);
1614
1615 return 0;
1616}
1617
1618/*
1619 * CS4210 Input MUX (1 ADC)
1620 */
1621static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_info *uinfo)
1623{
1624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1625 struct cs_spec *spec = codec->spec;
1626
1627 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1628}
1629
1630static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1631 struct snd_ctl_elem_value *ucontrol)
1632{
1633 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1634 struct cs_spec *spec = codec->spec;
1635
1636 ucontrol->value.enumerated.item[0] = spec->cur_input;
1637 return 0;
1638}
1639
1640static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1641 struct snd_ctl_elem_value *ucontrol)
1642{
1643 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1644 struct cs_spec *spec = codec->spec;
1645
1646 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1647 spec->adc_nid[0], &spec->cur_input);
1648
1649}
1650
1651static struct snd_kcontrol_new cs421x_capture_source = {
1652
1653 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1654 .name = "Capture Source",
1655 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1656 .info = cs421x_mux_enum_info,
1657 .get = cs421x_mux_enum_get,
1658 .put = cs421x_mux_enum_put,
1659};
1660
1661static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1662{
1663 struct cs_spec *spec = codec->spec;
1664 struct auto_pin_cfg *cfg = &spec->autocfg;
1665 const struct hda_input_mux *imux = &spec->input_mux;
1666 hda_nid_t pin = cfg->inputs[item].pin;
1667 struct snd_kcontrol *kctl;
1668 u32 caps;
1669
1670 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1671 return 0;
1672
1673 caps = query_amp_caps(codec, pin, HDA_INPUT);
1674 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1675 if (caps <= 1)
1676 return 0;
1677
1678 return add_volume(codec, imux->items[item].label, 0,
1679 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1680}
1681
1682/* add a (input-boost) volume control to the given input pin */
1683static int build_cs421x_input(struct hda_codec *codec)
1684{
1685 struct cs_spec *spec = codec->spec;
1686 struct auto_pin_cfg *cfg = &spec->autocfg;
1687 struct hda_input_mux *imux = &spec->input_mux;
1688 int i, err, type_idx;
1689 const char *label;
1690
1691 if (!spec->num_inputs)
1692 return 0;
1693
1694 /* make bind-capture */
1695 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1696 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1697 for (i = 0; i < 2; i++) {
1698 struct snd_kcontrol *kctl;
1699 int n;
1700 if (!spec->capture_bind[i])
1701 return -ENOMEM;
1702 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1703 if (!kctl)
1704 return -ENOMEM;
1705 kctl->private_value = (long)spec->capture_bind[i];
1706 err = snd_hda_ctl_add(codec, 0, kctl);
1707 if (err < 0)
1708 return err;
1709 for (n = 0; n < AUTO_PIN_LAST; n++) {
1710 if (!spec->adc_nid[n])
1711 continue;
1712 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1713 if (err < 0)
1714 return err;
1715 }
1716 }
1717
1718 /* Add Input MUX Items + Capture Volume/Switch */
1719 for (i = 0; i < spec->num_inputs; i++) {
1720 label = hda_get_autocfg_input_label(codec, cfg, i);
1721 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1722
1723 err = cs421x_add_input_volume_control(codec, i);
1724 if (err < 0)
1725 return err;
1726 }
1727
1728 /*
1729 Add 'Capture Source' Switch if
1730 * 2 inputs and no mic detec
1731 * 3 inputs
1732 */
1733 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1734 (spec->num_inputs == 3)) {
1735
1736 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1737 snd_ctl_new1(&cs421x_capture_source, codec));
1738 if (err < 0)
1739 return err;
1740 }
1741
1742 return 0;
1743}
1744
1745/* Single DAC (Mute/Gain) */
1746static int build_cs421x_output(struct hda_codec *codec)
1747{
1748 hda_nid_t dac = CS4210_DAC_NID;
1749 struct cs_spec *spec = codec->spec;
1750 struct auto_pin_cfg *cfg = &spec->autocfg;
1751 struct snd_kcontrol *kctl;
1752 int err;
1753 char *name = "HP/Speakers";
1754
1755 fix_volume_caps(codec, dac);
1756 if (!spec->vmaster_sw) {
1757 err = add_vmaster(codec, dac);
1758 if (err < 0)
1759 return err;
1760 }
1761
1762 err = add_mute(codec, name, 0,
1763 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1764 if (err < 0)
1765 return err;
1766 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1767 if (err < 0)
1768 return err;
1769
1770 err = add_volume(codec, name, 0,
1771 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1772 if (err < 0)
1773 return err;
1774 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1775 if (err < 0)
1776 return err;
1777
1778 if (cfg->speaker_outs) {
1779 err = snd_hda_ctl_add(codec, 0,
1780 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1781 if (err < 0)
1782 return err;
1783 }
1784 return err;
1785}
1786
1787static int cs421x_build_controls(struct hda_codec *codec)
1788{
1789 int err;
1790
1791 err = build_cs421x_output(codec);
1792 if (err < 0)
1793 return err;
1794 err = build_cs421x_input(codec);
1795 if (err < 0)
1796 return err;
1797 err = build_digital_output(codec);
1798 if (err < 0)
1799 return err;
1800 return cs421x_init(codec);
1801}
1802
1803static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1804{
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001805 snd_hda_jack_set_dirty_all(codec); /* FIXME: to be more fine-grained */
1806
Tim Howe56487c22011-07-22 16:41:00 -05001807 switch ((res >> 26) & 0x3f) {
1808 case HP_EVENT:
1809 case SPDIF_EVENT:
1810 cs_automute(codec);
1811 break;
1812
1813 case MIC_EVENT:
1814 cs_automic(codec);
1815 break;
1816 }
1817}
1818
1819static int parse_cs421x_input(struct hda_codec *codec)
1820{
1821 struct cs_spec *spec = codec->spec;
1822 struct auto_pin_cfg *cfg = &spec->autocfg;
1823 int i;
1824
1825 for (i = 0; i < cfg->num_inputs; i++) {
1826 hda_nid_t pin = cfg->inputs[i].pin;
1827 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1828 spec->cur_input = spec->last_input = i;
1829 spec->num_inputs++;
1830
1831 /* check whether the automatic mic switch is available */
1832 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1833 spec->mic_detect = 1;
1834 spec->automic_idx = i;
1835 }
1836 }
1837 return 0;
1838}
1839
1840static int cs421x_parse_auto_config(struct hda_codec *codec)
1841{
1842 struct cs_spec *spec = codec->spec;
1843 int err;
1844
1845 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1846 if (err < 0)
1847 return err;
1848 err = parse_output(codec);
1849 if (err < 0)
1850 return err;
1851 err = parse_cs421x_input(codec);
1852 if (err < 0)
1853 return err;
1854 err = parse_digital_output(codec);
1855 if (err < 0)
1856 return err;
1857 return 0;
1858}
1859
1860#ifdef CONFIG_PM
1861/*
1862 Manage PDREF, when transitioning to D3hot
1863 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1864*/
1865static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1866{
1867 unsigned int coef;
1868
1869 snd_hda_shutup_pins(codec);
1870
1871 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1872 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1873 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1874 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1875
1876 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1877 coef |= 0x0004; /* PDREF */
1878 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1879
1880 return 0;
1881}
1882#endif
1883
1884static struct hda_codec_ops cs4210_patch_ops = {
1885 .build_controls = cs421x_build_controls,
1886 .build_pcms = cs_build_pcms,
1887 .init = cs421x_init,
1888 .free = cs_free,
1889 .unsol_event = cs421x_unsol_event,
1890#ifdef CONFIG_PM
1891 .suspend = cs421x_suspend,
1892#endif
1893};
1894
1895static int patch_cs421x(struct hda_codec *codec)
1896{
1897 struct cs_spec *spec;
1898 int err;
1899
1900 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1901 if (!spec)
1902 return -ENOMEM;
1903 codec->spec = spec;
1904
1905 spec->vendor_nid = CS421X_VENDOR_NID;
1906
1907 spec->board_config =
1908 snd_hda_check_board_config(codec, CS421X_MODELS,
1909 cs421x_models, cs421x_cfg_tbl);
1910 if (spec->board_config >= 0)
1911 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1912 /*
1913 Setup GPIO/SENSE for each board (if used)
1914 */
1915 switch (spec->board_config) {
1916 case CS421X_CDB4210:
1917 snd_printd("CS4210 board: %s\n",
1918 cs421x_models[spec->board_config]);
1919/* spec->gpio_mask = 3;
1920 spec->gpio_dir = 3;
1921 spec->gpio_data = 3;
1922*/
1923 spec->sense_b = 1;
1924
1925 break;
1926 }
1927
1928 /*
1929 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1930 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1931 is disabled.
1932 */
1933 cs421x_pinmux_init(codec);
1934
1935 err = cs421x_parse_auto_config(codec);
1936 if (err < 0)
1937 goto error;
1938
1939 codec->patch_ops = cs4210_patch_ops;
1940
1941 return 0;
1942
1943 error:
1944 kfree(codec->spec);
1945 codec->spec = NULL;
1946 return err;
1947}
1948
Takashi Iwaie5f14242009-07-01 18:11:44 +02001949
1950/*
1951 * patch entries
1952 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02001953static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001954 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1955 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
Tim Howe56487c22011-07-22 16:41:00 -05001956 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
Takashi Iwaie5f14242009-07-01 18:11:44 +02001957 {} /* terminator */
1958};
1959
1960MODULE_ALIAS("snd-hda-codec-id:10134206");
1961MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05001962MODULE_ALIAS("snd-hda-codec-id:10134210");
Takashi Iwaie5f14242009-07-01 18:11:44 +02001963
1964MODULE_LICENSE("GPL");
1965MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1966
1967static struct hda_codec_preset_list cirrus_list = {
1968 .preset = snd_hda_preset_cirrus,
1969 .owner = THIS_MODULE,
1970};
1971
1972static int __init patch_cirrus_init(void)
1973{
1974 return snd_hda_add_codec_preset(&cirrus_list);
1975}
1976
1977static void __exit patch_cirrus_exit(void)
1978{
1979 snd_hda_delete_codec_preset(&cirrus_list);
1980}
1981
1982module_init(patch_cirrus_init)
1983module_exit(patch_cirrus_exit)