blob: 7f93739b1e3339cf131d13f5372c9f5da9abf401 [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>
25#include <sound/core.h>
26#include "hda_codec.h"
27#include "hda_local.h"
28
29/*
30 */
31
32struct cs_spec {
Takashi Iwaia6bae202009-07-06 15:15:22 +020033 int board_config;
Takashi Iwaie5f14242009-07-01 18:11:44 +020034 struct auto_pin_cfg autocfg;
35 struct hda_multi_out multiout;
36 struct snd_kcontrol *vmaster_sw;
37 struct snd_kcontrol *vmaster_vol;
38
39 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
40 hda_nid_t slave_dig_outs[2];
41
42 unsigned int input_idx[AUTO_PIN_LAST];
43 unsigned int capsrc_idx[AUTO_PIN_LAST];
44 hda_nid_t adc_nid[AUTO_PIN_LAST];
45 unsigned int adc_idx[AUTO_PIN_LAST];
46 unsigned int num_inputs;
47 unsigned int cur_input;
48 unsigned int automic_idx;
49 hda_nid_t cur_adc;
50 unsigned int cur_adc_stream_tag;
51 unsigned int cur_adc_format;
52 hda_nid_t dig_in;
53
Takashi Iwaic42d4782011-05-02 11:36:09 +020054 const struct hda_bind_ctls *capture_bind[2];
Takashi Iwaie5f14242009-07-01 18:11:44 +020055
Takashi Iwaied208252009-07-07 09:04:26 +020056 unsigned int gpio_mask;
57 unsigned int gpio_dir;
58 unsigned int gpio_data;
59
Takashi Iwaie5f14242009-07-01 18:11:44 +020060 struct hda_pcm pcm_rec[2]; /* PCM information */
61
62 unsigned int hp_detect:1;
63 unsigned int mic_detect:1;
Takashi Iwaie5f14242009-07-01 18:11:44 +020064};
65
Takashi Iwaia6bae202009-07-06 15:15:22 +020066/* available models */
67enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040068 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020069 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010070 CS420X_IMAC27,
Takashi Iwaia6bae202009-07-06 15:15:22 +020071 CS420X_AUTO,
72 CS420X_MODELS
73};
74
Takashi Iwai40c20fa2009-07-06 13:00:57 +020075/* Vendor-specific processing widget */
76#define CS420X_VENDOR_NID 0x11
77#define CS_DIG_OUT1_PIN_NID 0x10
78#define CS_DIG_OUT2_PIN_NID 0x15
79#define CS_DMIC1_PIN_NID 0x12
80#define CS_DMIC2_PIN_NID 0x0e
81
82/* coef indices */
83#define IDX_SPDIF_STAT 0x0000
84#define IDX_SPDIF_CTL 0x0001
85#define IDX_ADC_CFG 0x0002
86/* SZC bitmask, 4 modes below:
87 * 0 = immediate,
88 * 1 = digital immediate, analog zero-cross
89 * 2 = digtail & analog soft-ramp
90 * 3 = digital soft-ramp, analog zero-cross
91 */
92#define CS_COEF_ADC_SZC_MASK (3 << 0)
93#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
94#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
95/* PGA mode: 0 = differential, 1 = signle-ended */
96#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
97#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
98#define IDX_DAC_CFG 0x0003
99/* SZC bitmask, 4 modes below:
100 * 0 = Immediate
101 * 1 = zero-cross
102 * 2 = soft-ramp
103 * 3 = soft-ramp on zero-cross
104 */
105#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
106#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
107#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
108
109#define IDX_BEEP_CFG 0x0004
110/* 0x0008 - test reg key */
111/* 0x0009 - 0x0014 -> 12 test regs */
112/* 0x0015 - visibility reg */
113
114
Takashi Iwai277a57c2009-07-08 12:42:08 +0200115static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200116{
117 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
118 AC_VERB_SET_COEF_INDEX, idx);
119 return snd_hda_codec_read(codec, CS420X_VENDOR_NID, 0,
120 AC_VERB_GET_PROC_COEF, 0);
121}
122
Takashi Iwai277a57c2009-07-08 12:42:08 +0200123static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
124 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200125{
126 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
127 AC_VERB_SET_COEF_INDEX, idx);
128 snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
129 AC_VERB_SET_PROC_COEF, coef);
130}
131
132
Takashi Iwaie5f14242009-07-01 18:11:44 +0200133#define HP_EVENT 1
134#define MIC_EVENT 2
135
136/*
137 * PCM callbacks
138 */
139static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
140 struct hda_codec *codec,
141 struct snd_pcm_substream *substream)
142{
143 struct cs_spec *spec = codec->spec;
144 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
145 hinfo);
146}
147
148static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
149 struct hda_codec *codec,
150 unsigned int stream_tag,
151 unsigned int format,
152 struct snd_pcm_substream *substream)
153{
154 struct cs_spec *spec = codec->spec;
155 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
156 stream_tag, format, substream);
157}
158
159static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
160 struct hda_codec *codec,
161 struct snd_pcm_substream *substream)
162{
163 struct cs_spec *spec = codec->spec;
164 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
165}
166
167/*
168 * Digital out
169 */
170static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
171 struct hda_codec *codec,
172 struct snd_pcm_substream *substream)
173{
174 struct cs_spec *spec = codec->spec;
175 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
176}
177
178static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
179 struct hda_codec *codec,
180 struct snd_pcm_substream *substream)
181{
182 struct cs_spec *spec = codec->spec;
183 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
184}
185
186static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
187 struct hda_codec *codec,
188 unsigned int stream_tag,
189 unsigned int format,
190 struct snd_pcm_substream *substream)
191{
192 struct cs_spec *spec = codec->spec;
193 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
194 format, substream);
195}
196
197static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
198 struct hda_codec *codec,
199 struct snd_pcm_substream *substream)
200{
201 struct cs_spec *spec = codec->spec;
202 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
203}
204
205/*
206 * Analog capture
207 */
208static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
209 struct hda_codec *codec,
210 unsigned int stream_tag,
211 unsigned int format,
212 struct snd_pcm_substream *substream)
213{
214 struct cs_spec *spec = codec->spec;
215 spec->cur_adc = spec->adc_nid[spec->cur_input];
216 spec->cur_adc_stream_tag = stream_tag;
217 spec->cur_adc_format = format;
218 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
219 return 0;
220}
221
222static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
223 struct hda_codec *codec,
224 struct snd_pcm_substream *substream)
225{
226 struct cs_spec *spec = codec->spec;
227 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
228 spec->cur_adc = 0;
229 return 0;
230}
231
232/*
233 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200234static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200235 .substreams = 1,
236 .channels_min = 2,
237 .channels_max = 2,
238 .ops = {
239 .open = cs_playback_pcm_open,
240 .prepare = cs_playback_pcm_prepare,
241 .cleanup = cs_playback_pcm_cleanup
242 },
243};
244
Takashi Iwaic42d4782011-05-02 11:36:09 +0200245static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200246 .substreams = 1,
247 .channels_min = 2,
248 .channels_max = 2,
249 .ops = {
250 .prepare = cs_capture_pcm_prepare,
251 .cleanup = cs_capture_pcm_cleanup
252 },
253};
254
Takashi Iwaic42d4782011-05-02 11:36:09 +0200255static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200256 .substreams = 1,
257 .channels_min = 2,
258 .channels_max = 2,
259 .ops = {
260 .open = cs_dig_playback_pcm_open,
261 .close = cs_dig_playback_pcm_close,
262 .prepare = cs_dig_playback_pcm_prepare,
263 .cleanup = cs_dig_playback_pcm_cleanup
264 },
265};
266
Takashi Iwaic42d4782011-05-02 11:36:09 +0200267static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200268 .substreams = 1,
269 .channels_min = 2,
270 .channels_max = 2,
271};
272
273static int cs_build_pcms(struct hda_codec *codec)
274{
275 struct cs_spec *spec = codec->spec;
276 struct hda_pcm *info = spec->pcm_rec;
277
278 codec->pcm_info = info;
279 codec->num_pcms = 0;
280
281 info->name = "Cirrus Analog";
282 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
283 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
284 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
285 spec->multiout.max_channels;
286 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200287 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
288 spec->adc_nid[spec->cur_input];
289 codec->num_pcms++;
290
291 if (!spec->multiout.dig_out_nid && !spec->dig_in)
292 return 0;
293
294 info++;
295 info->name = "Cirrus Digital";
296 info->pcm_type = spec->autocfg.dig_out_type[0];
297 if (!info->pcm_type)
298 info->pcm_type = HDA_PCM_TYPE_SPDIF;
299 if (spec->multiout.dig_out_nid) {
300 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
301 cs_pcm_digital_playback;
302 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
303 spec->multiout.dig_out_nid;
304 }
305 if (spec->dig_in) {
306 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
307 cs_pcm_digital_capture;
308 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
309 }
310 codec->num_pcms++;
311
312 return 0;
313}
314
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200315/*
316 * parse codec topology
317 */
318
Takashi Iwaie5f14242009-07-01 18:11:44 +0200319static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
320{
321 hda_nid_t dac;
322 if (!pin)
323 return 0;
324 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
325 return 0;
326 return dac;
327}
328
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200329static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
330{
331 struct cs_spec *spec = codec->spec;
332 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200333 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200334 unsigned int val;
335 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200336 return 0;
337 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200338 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200339}
340
341static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
342 unsigned int *idxp)
343{
344 int i;
345 hda_nid_t nid;
346
347 nid = codec->start_nid;
348 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200349 unsigned int type;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200350 int idx;
Takashi Iwai1682c812011-05-17 10:02:27 +0200351 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200352 if (type != AC_WID_AUD_IN)
353 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200354 idx = snd_hda_get_conn_index(codec, nid, pin, 0);
355 if (idx >= 0) {
356 *idxp = idx;
357 return nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200358 }
359 }
360 return 0;
361}
362
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200363static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
364{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200365 unsigned int val;
366 val = snd_hda_codec_get_pincfg(codec, nid);
367 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
368}
369
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200370static int parse_output(struct hda_codec *codec)
371{
372 struct cs_spec *spec = codec->spec;
373 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200374 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200375 hda_nid_t dac;
376
377 for (i = 0; i < cfg->line_outs; i++) {
378 dac = get_dac(codec, cfg->line_out_pins[i]);
379 if (!dac)
380 break;
381 spec->dac_nid[i] = dac;
382 }
383 spec->multiout.num_dacs = i;
384 spec->multiout.dac_nids = spec->dac_nid;
385 spec->multiout.max_channels = i * 2;
386
387 /* add HP and speakers */
388 extra_nids = 0;
389 for (i = 0; i < cfg->hp_outs; i++) {
390 dac = get_dac(codec, cfg->hp_pins[i]);
391 if (!dac)
392 break;
393 if (!i)
394 spec->multiout.hp_nid = dac;
395 else
396 spec->multiout.extra_out_nid[extra_nids++] = dac;
397 }
398 for (i = 0; i < cfg->speaker_outs; i++) {
399 dac = get_dac(codec, cfg->speaker_pins[i]);
400 if (!dac)
401 break;
402 spec->multiout.extra_out_nid[extra_nids++] = dac;
403 }
404
405 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
406 cfg->speaker_outs = cfg->line_outs;
407 memcpy(cfg->speaker_pins, cfg->line_out_pins,
408 sizeof(cfg->speaker_pins));
409 cfg->line_outs = 0;
410 }
411
412 return 0;
413}
414
415static int parse_input(struct hda_codec *codec)
416{
417 struct cs_spec *spec = codec->spec;
418 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200419 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200420
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200421 for (i = 0; i < cfg->num_inputs; i++) {
422 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200423 spec->input_idx[spec->num_inputs] = i;
424 spec->capsrc_idx[i] = spec->num_inputs++;
425 spec->cur_input = i;
426 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
427 }
428 if (!spec->num_inputs)
429 return 0;
430
431 /* check whether the automatic mic switch is available */
432 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200433 cfg->inputs[0].type == AUTO_PIN_MIC &&
434 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200435 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
436 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200437 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200438 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200439 }
440 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200441 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200442 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200443 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200444 }
445 }
446 }
447 return 0;
448}
449
450
451static int parse_digital_output(struct hda_codec *codec)
452{
453 struct cs_spec *spec = codec->spec;
454 struct auto_pin_cfg *cfg = &spec->autocfg;
455 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200456
457 if (!cfg->dig_outs)
458 return 0;
459 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
460 return 0;
461 spec->multiout.dig_out_nid = nid;
462 spec->multiout.share_spdif = 1;
463 if (cfg->dig_outs > 1 &&
464 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
465 spec->slave_dig_outs[0] = nid;
466 codec->slave_dig_outs = spec->slave_dig_outs;
467 }
468 return 0;
469}
470
471static int parse_digital_input(struct hda_codec *codec)
472{
473 struct cs_spec *spec = codec->spec;
474 struct auto_pin_cfg *cfg = &spec->autocfg;
475 int idx;
476
Takashi Iwai60e53882009-07-06 15:01:09 +0200477 if (cfg->dig_in_pin)
478 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
479 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200480}
481
482/*
483 * create mixer controls
484 */
485
Takashi Iwaiea734962011-01-17 11:29:34 +0100486static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200487
488static int add_mute(struct hda_codec *codec, const char *name, int index,
489 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
490{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200491 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200492 struct snd_kcontrol_new knew =
493 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
494 knew.private_value = pval;
495 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
496 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100497 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100498 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200499}
500
501static int add_volume(struct hda_codec *codec, const char *name,
502 int index, unsigned int pval, int dir,
503 struct snd_kcontrol **kctlp)
504{
505 char tmp[32];
506 struct snd_kcontrol_new knew =
507 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
508 knew.private_value = pval;
509 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
510 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100511 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100512 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200513}
514
515static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
516{
517 unsigned int caps;
518
519 /* set the upper-limit for mixer amp to 0dB */
520 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
521 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
522 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
523 << AC_AMPCAP_NUM_STEPS_SHIFT;
524 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
525}
526
527static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
528{
529 struct cs_spec *spec = codec->spec;
530 unsigned int tlv[4];
531 int err;
532
533 spec->vmaster_sw =
534 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100535 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200536 if (err < 0)
537 return err;
538
539 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
540 spec->vmaster_vol =
541 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100542 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200543 if (err < 0)
544 return err;
545 return 0;
546}
547
548static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
549 int num_ctls, int type)
550{
551 struct cs_spec *spec = codec->spec;
552 const char *name;
553 int err, index;
554 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200555 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200556 "Front Speaker", "Surround Speaker", "Bass Speaker"
557 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200558 static const char * const line_outs[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200559 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
560 };
561
562 fix_volume_caps(codec, dac);
563 if (!spec->vmaster_sw) {
564 err = add_vmaster(codec, dac);
565 if (err < 0)
566 return err;
567 }
568
569 index = 0;
570 switch (type) {
571 case AUTO_PIN_HP_OUT:
572 name = "Headphone";
573 index = idx;
574 break;
575 case AUTO_PIN_SPEAKER_OUT:
576 if (num_ctls > 1)
577 name = speakers[idx];
578 else
579 name = "Speaker";
580 break;
581 default:
582 if (num_ctls > 1)
583 name = line_outs[idx];
584 else
585 name = "Line-Out";
586 break;
587 }
588
589 err = add_mute(codec, name, index,
590 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
591 if (err < 0)
592 return err;
593 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
594 if (err < 0)
595 return err;
596
597 err = add_volume(codec, name, index,
598 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
599 if (err < 0)
600 return err;
601 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
602 if (err < 0)
603 return err;
604
605 return 0;
606}
607
608static int build_output(struct hda_codec *codec)
609{
610 struct cs_spec *spec = codec->spec;
611 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200612 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200613
614 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200615 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
616 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200617 if (err < 0)
618 return err;
619 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200620 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200621 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
622 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200623 if (err < 0)
624 return err;
625 }
626 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200627 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
628 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200629 if (err < 0)
630 return err;
631 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200632 return 0;
633}
634
635/*
636 */
637
Takashi Iwaic42d4782011-05-02 11:36:09 +0200638static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200639 HDA_BIND_SW("Capture Switch", 0),
640 HDA_BIND_VOL("Capture Volume", 0),
641};
642
Takashi Iwaiea359292009-07-06 12:58:59 +0200643static int change_cur_input(struct hda_codec *codec, unsigned int idx,
644 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200645{
646 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200647
Takashi Iwaiea359292009-07-06 12:58:59 +0200648 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200649 return 0;
650 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
651 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200652 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200653 spec->cur_adc = spec->adc_nid[idx];
654 snd_hda_codec_setup_stream(codec, spec->cur_adc,
655 spec->cur_adc_stream_tag, 0,
656 spec->cur_adc_format);
657 }
658 snd_hda_codec_write(codec, spec->cur_adc, 0,
659 AC_VERB_SET_CONNECT_SEL,
660 spec->adc_idx[idx]);
661 spec->cur_input = idx;
662 return 1;
663}
664
665static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
666 struct snd_ctl_elem_info *uinfo)
667{
668 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
669 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200670 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200671 unsigned int idx;
672
673 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
674 uinfo->count = 1;
675 uinfo->value.enumerated.items = spec->num_inputs;
676 if (uinfo->value.enumerated.item >= spec->num_inputs)
677 uinfo->value.enumerated.item = spec->num_inputs - 1;
678 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai10a20af2010-09-09 16:28:02 +0200679 strcpy(uinfo->value.enumerated.name,
680 hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
Takashi Iwaie5f14242009-07-01 18:11:44 +0200681 return 0;
682}
683
684static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
685 struct snd_ctl_elem_value *ucontrol)
686{
687 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
688 struct cs_spec *spec = codec->spec;
689 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
690 return 0;
691}
692
693static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
694 struct snd_ctl_elem_value *ucontrol)
695{
696 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
697 struct cs_spec *spec = codec->spec;
698 unsigned int idx = ucontrol->value.enumerated.item[0];
699
700 if (idx >= spec->num_inputs)
701 return -EINVAL;
702 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200703 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200704}
705
Takashi Iwaic42d4782011-05-02 11:36:09 +0200706static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200707 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
708 .name = "Capture Source",
709 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
710 .info = cs_capture_source_info,
711 .get = cs_capture_source_get,
712 .put = cs_capture_source_put,
713};
714
Takashi Iwaic42d4782011-05-02 11:36:09 +0200715static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200716 struct hda_ctl_ops *ops)
717{
718 struct cs_spec *spec = codec->spec;
719 struct hda_bind_ctls *bind;
720 int i, n;
721
722 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
723 GFP_KERNEL);
724 if (!bind)
725 return NULL;
726 bind->ops = ops;
727 n = 0;
728 for (i = 0; i < AUTO_PIN_LAST; i++) {
729 if (!spec->adc_nid[i])
730 continue;
731 bind->values[n++] =
732 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
733 spec->adc_idx[i], HDA_INPUT);
734 }
735 return bind;
736}
737
Takashi Iwai6a929342010-10-11 15:16:20 +0200738/* add a (input-boost) volume control to the given input pin */
739static int add_input_volume_control(struct hda_codec *codec,
740 struct auto_pin_cfg *cfg,
741 int item)
742{
743 hda_nid_t pin = cfg->inputs[item].pin;
744 u32 caps;
745 const char *label;
746 struct snd_kcontrol *kctl;
747
748 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
749 return 0;
750 caps = query_amp_caps(codec, pin, HDA_INPUT);
751 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
752 if (caps <= 1)
753 return 0;
754 label = hda_get_autocfg_input_label(codec, cfg, item);
755 return add_volume(codec, label, 0,
756 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
757}
758
Takashi Iwaie5f14242009-07-01 18:11:44 +0200759static int build_input(struct hda_codec *codec)
760{
761 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200762 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200763
Takashi Iwaie5f14242009-07-01 18:11:44 +0200764 if (!spec->num_inputs)
765 return 0;
766
Takashi Iwaie5f14242009-07-01 18:11:44 +0200767 /* make bind-capture */
768 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
769 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
770 for (i = 0; i < 2; i++) {
771 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100772 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200773 if (!spec->capture_bind[i])
774 return -ENOMEM;
775 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
776 if (!kctl)
777 return -ENOMEM;
778 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100779 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200780 if (err < 0)
781 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100782 for (n = 0; n < AUTO_PIN_LAST; n++) {
783 if (!spec->adc_nid[n])
784 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700785 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100786 if (err < 0)
787 return err;
788 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200789 }
790
791 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100792 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200793 snd_ctl_new1(&cs_capture_source, codec));
794 if (err < 0)
795 return err;
796 }
797
Takashi Iwai6a929342010-10-11 15:16:20 +0200798 for (i = 0; i < spec->num_inputs; i++) {
799 err = add_input_volume_control(codec, &spec->autocfg, i);
800 if (err < 0)
801 return err;
802 }
803
Takashi Iwaie5f14242009-07-01 18:11:44 +0200804 return 0;
805}
806
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200807/*
808 */
809
Takashi Iwaie5f14242009-07-01 18:11:44 +0200810static int build_digital_output(struct hda_codec *codec)
811{
812 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200813 int err;
814
Takashi Iwai63b24132009-07-09 11:45:59 +0200815 if (!spec->multiout.dig_out_nid)
816 return 0;
817
Stephen Warren74b654c2011-06-01 11:14:18 -0600818 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
819 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200820 if (err < 0)
821 return err;
822 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
823 if (err < 0)
824 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200825 return 0;
826}
827
828static int build_digital_input(struct hda_codec *codec)
829{
830 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200831 if (spec->dig_in)
832 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
833 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200834}
835
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200836/*
837 * auto-mute and auto-mic switching
838 */
839
Takashi Iwaie5f14242009-07-01 18:11:44 +0200840static void cs_automute(struct hda_codec *codec)
841{
842 struct cs_spec *spec = codec->spec;
843 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200844 unsigned int hp_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200845 hda_nid_t nid;
846 int i;
847
848 hp_present = 0;
849 for (i = 0; i < cfg->hp_outs; i++) {
850 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200851 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200852 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800853 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200854 if (hp_present)
855 break;
856 }
857 for (i = 0; i < cfg->speaker_outs; i++) {
858 nid = cfg->speaker_pins[i];
859 snd_hda_codec_write(codec, nid, 0,
860 AC_VERB_SET_PIN_WIDGET_CONTROL,
861 hp_present ? 0 : PIN_OUT);
862 }
Vince Weaver4e7d7c62010-09-22 17:31:37 -0400863 if (spec->board_config == CS420X_MBP53 ||
864 spec->board_config == CS420X_MBP55 ||
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +0100865 spec->board_config == CS420X_IMAC27) {
Stelian Pop3a385162009-07-30 14:44:27 +0200866 unsigned int gpio = hp_present ? 0x02 : 0x08;
867 snd_hda_codec_write(codec, 0x01, 0,
868 AC_VERB_SET_GPIO_DATA, gpio);
869 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200870}
871
872static void cs_automic(struct hda_codec *codec)
873{
874 struct cs_spec *spec = codec->spec;
875 struct auto_pin_cfg *cfg = &spec->autocfg;
876 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800877 unsigned int present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200878
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200879 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800880 present = snd_hda_jack_detect(codec, nid);
881 if (present)
Takashi Iwaiea359292009-07-06 12:58:59 +0200882 change_cur_input(codec, spec->automic_idx, 0);
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200883 else
884 change_cur_input(codec, !spec->automic_idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200885}
886
887/*
888 */
889
890static void init_output(struct hda_codec *codec)
891{
892 struct cs_spec *spec = codec->spec;
893 struct auto_pin_cfg *cfg = &spec->autocfg;
894 int i;
895
896 /* mute first */
897 for (i = 0; i < spec->multiout.num_dacs; i++)
898 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
899 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
900 if (spec->multiout.hp_nid)
901 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
902 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
903 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
904 if (!spec->multiout.extra_out_nid[i])
905 break;
906 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
907 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
908 }
909
910 /* set appropriate pin controls */
911 for (i = 0; i < cfg->line_outs; i++)
912 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
913 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
914 for (i = 0; i < cfg->hp_outs; i++) {
915 hda_nid_t nid = cfg->hp_pins[i];
916 snd_hda_codec_write(codec, nid, 0,
917 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
918 if (!cfg->speaker_outs)
919 continue;
Takashi Iwaie35d9d62011-05-17 11:28:16 +0200920 if (is_jack_detectable(codec, nid)) {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200921 snd_hda_codec_write(codec, nid, 0,
922 AC_VERB_SET_UNSOLICITED_ENABLE,
923 AC_USRSP_EN | HP_EVENT);
924 spec->hp_detect = 1;
925 }
926 }
927 for (i = 0; i < cfg->speaker_outs; i++)
928 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
929 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
930 if (spec->hp_detect)
931 cs_automute(codec);
932}
933
934static void init_input(struct hda_codec *codec)
935{
936 struct cs_spec *spec = codec->spec;
937 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200938 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200939 int i;
940
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200941 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200942 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200943 hda_nid_t pin = cfg->inputs[i].pin;
944 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +0200945 continue;
946 /* set appropriate pin control and mute first */
947 ctl = PIN_IN;
Takashi Iwai86e29592010-09-09 14:50:17 +0200948 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200949 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
950 caps >>= AC_PINCAP_VREF_SHIFT;
951 if (caps & AC_PINCAP_VREF_80)
952 ctl = PIN_VREF80;
953 }
954 snd_hda_codec_write(codec, pin, 0,
955 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
956 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
957 AC_VERB_SET_AMP_GAIN_MUTE,
958 AMP_IN_MUTE(spec->adc_idx[i]));
959 if (spec->mic_detect && spec->automic_idx == i)
960 snd_hda_codec_write(codec, pin, 0,
961 AC_VERB_SET_UNSOLICITED_ENABLE,
962 AC_USRSP_EN | MIC_EVENT);
963 }
Takashi Iwaiea359292009-07-06 12:58:59 +0200964 change_cur_input(codec, spec->cur_input, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200965 if (spec->mic_detect)
966 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200967
968 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
969 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
970 coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
971 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
972 coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +0200973 * No effect if SPDIF_OUT2 is selected in
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200974 * IDX_SPDIF_CTL.
975 */
976 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
977}
978
Takashi Iwaic42d4782011-05-02 11:36:09 +0200979static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200980 {0x11, AC_VERB_SET_PROC_STATE, 1},
981 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
982 {0x11, AC_VERB_SET_PROC_COEF,
983 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
984 | 0x0040 /* Mute DACs on FIFO error */
985 | 0x1000 /* Enable DACs High Pass Filter */
986 | 0x0400 /* Disable Coefficient Auto increment */
987 )},
988 /* Beep */
989 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
990 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
991
992 {} /* terminator */
993};
994
Brian Austina769cbc2010-09-07 14:36:22 -0500995/* Errata: CS4207 rev C0/C1/C2 Silicon
996 *
997 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
998 *
999 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1000 * may be excessive (up to an additional 200 μA), which is most easily
1001 * observed while the part is being held in reset (RESET# active low).
1002 *
1003 * Root Cause: At initial powerup of the device, the logic that drives
1004 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1005 * initialized.
1006 * Certain random patterns will cause a steady leakage current in those
1007 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1008 *
1009 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1010 * blocks, which will alleviate the issue.
1011 */
1012
Takashi Iwaic42d4782011-05-02 11:36:09 +02001013static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001014 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1015 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1016
1017 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1018 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1019 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1020 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1021 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1022 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1023
1024 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1025 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1026
1027 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1028 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1029 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1030 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1031 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1032 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1033 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1034
Takashi Iwai38c07642011-03-03 14:54:19 +01001035#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001036 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1037 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1038 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001039#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001040
1041 {} /* terminator */
1042};
1043
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001044/* SPDIF setup */
1045static void init_digital(struct hda_codec *codec)
1046{
1047 unsigned int coef;
1048
1049 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1050 coef |= 0x0008; /* Replace with mute on error */
1051 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1052 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1053 * SPDIF_OUT2 is shared with GPIO1 and
1054 * DMIC_SDA2.
1055 */
1056 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001057}
1058
1059static int cs_init(struct hda_codec *codec)
1060{
1061 struct cs_spec *spec = codec->spec;
1062
Brian Austina769cbc2010-09-07 14:36:22 -05001063 /* init_verb sequence for C0/C1/C2 errata*/
1064 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1065
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001066 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001067
1068 if (spec->gpio_mask) {
1069 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1070 spec->gpio_mask);
1071 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1072 spec->gpio_dir);
1073 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1074 spec->gpio_data);
1075 }
1076
Takashi Iwaie5f14242009-07-01 18:11:44 +02001077 init_output(codec);
1078 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001079 init_digital(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001080 return 0;
1081}
1082
1083static int cs_build_controls(struct hda_codec *codec)
1084{
Takashi Iwaie5f14242009-07-01 18:11:44 +02001085 int err;
1086
1087 err = build_output(codec);
1088 if (err < 0)
1089 return err;
1090 err = build_input(codec);
1091 if (err < 0)
1092 return err;
1093 err = build_digital_output(codec);
1094 if (err < 0)
1095 return err;
1096 err = build_digital_input(codec);
1097 if (err < 0)
1098 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001099 return cs_init(codec);
1100}
1101
1102static void cs_free(struct hda_codec *codec)
1103{
1104 struct cs_spec *spec = codec->spec;
1105 kfree(spec->capture_bind[0]);
1106 kfree(spec->capture_bind[1]);
1107 kfree(codec->spec);
1108}
1109
1110static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1111{
1112 switch ((res >> 26) & 0x7f) {
1113 case HP_EVENT:
1114 cs_automute(codec);
1115 break;
1116 case MIC_EVENT:
1117 cs_automic(codec);
1118 break;
1119 }
1120}
1121
Takashi Iwaic42d4782011-05-02 11:36:09 +02001122static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001123 .build_controls = cs_build_controls,
1124 .build_pcms = cs_build_pcms,
1125 .init = cs_init,
1126 .free = cs_free,
1127 .unsol_event = cs_unsol_event,
1128};
1129
1130static int cs_parse_auto_config(struct hda_codec *codec)
1131{
1132 struct cs_spec *spec = codec->spec;
1133 int err;
1134
1135 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1136 if (err < 0)
1137 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001138
1139 err = parse_output(codec);
1140 if (err < 0)
1141 return err;
1142 err = parse_input(codec);
1143 if (err < 0)
1144 return err;
1145 err = parse_digital_output(codec);
1146 if (err < 0)
1147 return err;
1148 err = parse_digital_input(codec);
1149 if (err < 0)
1150 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001151 return 0;
1152}
1153
Takashi Iwaiea734962011-01-17 11:29:34 +01001154static const char * const cs420x_models[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001155 [CS420X_MBP53] = "mbp53",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001156 [CS420X_MBP55] = "mbp55",
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001157 [CS420X_IMAC27] = "imac27",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001158 [CS420X_AUTO] = "auto",
1159};
1160
1161
Takashi Iwaic42d4782011-05-02 11:36:09 +02001162static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001163 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001164 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001165 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001166 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001167 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001168 {} /* terminator */
1169};
1170
1171struct cs_pincfg {
1172 hda_nid_t nid;
1173 u32 val;
1174};
1175
Takashi Iwaic42d4782011-05-02 11:36:09 +02001176static const struct cs_pincfg mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001177 { 0x09, 0x012b4050 },
1178 { 0x0a, 0x90100141 },
1179 { 0x0b, 0x90100140 },
1180 { 0x0c, 0x018b3020 },
1181 { 0x0d, 0x90a00110 },
1182 { 0x0e, 0x400000f0 },
1183 { 0x0f, 0x01cbe030 },
1184 { 0x10, 0x014be060 },
1185 { 0x12, 0x400000f0 },
1186 { 0x15, 0x400000f0 },
1187 {} /* terminator */
1188};
1189
Takashi Iwaic42d4782011-05-02 11:36:09 +02001190static const struct cs_pincfg mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001191 { 0x09, 0x012b4030 },
1192 { 0x0a, 0x90100121 },
1193 { 0x0b, 0x90100120 },
1194 { 0x0c, 0x400000f0 },
1195 { 0x0d, 0x90a00110 },
1196 { 0x0e, 0x400000f0 },
1197 { 0x0f, 0x400000f0 },
1198 { 0x10, 0x014be040 },
1199 { 0x12, 0x400000f0 },
1200 { 0x15, 0x400000f0 },
1201 {} /* terminator */
1202};
1203
Takashi Iwaic42d4782011-05-02 11:36:09 +02001204static const struct cs_pincfg imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001205 { 0x09, 0x012b4050 },
1206 { 0x0a, 0x90100140 },
1207 { 0x0b, 0x90100142 },
1208 { 0x0c, 0x018b3020 },
1209 { 0x0d, 0x90a00110 },
1210 { 0x0e, 0x400000f0 },
1211 { 0x0f, 0x01cbe030 },
1212 { 0x10, 0x014be060 },
1213 { 0x12, 0x01ab9070 },
1214 { 0x15, 0x400000f0 },
1215 {} /* terminator */
1216};
1217
Takashi Iwaic42d4782011-05-02 11:36:09 +02001218static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001219 [CS420X_MBP53] = mbp53_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001220 [CS420X_MBP55] = mbp55_pincfgs,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001221 [CS420X_IMAC27] = imac27_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001222};
1223
1224static void fix_pincfg(struct hda_codec *codec, int model)
1225{
1226 const struct cs_pincfg *cfg = cs_pincfgs[model];
1227 if (!cfg)
1228 return;
1229 for (; cfg->nid; cfg++)
1230 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1231}
1232
Takashi Iwaie5f14242009-07-01 18:11:44 +02001233
1234static int patch_cs420x(struct hda_codec *codec)
1235{
1236 struct cs_spec *spec;
1237 int err;
1238
1239 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1240 if (!spec)
1241 return -ENOMEM;
1242 codec->spec = spec;
1243
Takashi Iwaia6bae202009-07-06 15:15:22 +02001244 spec->board_config =
1245 snd_hda_check_board_config(codec, CS420X_MODELS,
1246 cs420x_models, cs420x_cfg_tbl);
1247 if (spec->board_config >= 0)
1248 fix_pincfg(codec, spec->board_config);
1249
Takashi Iwaied208252009-07-07 09:04:26 +02001250 switch (spec->board_config) {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001251 case CS420X_IMAC27:
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001252 case CS420X_MBP53:
Takashi Iwaied208252009-07-07 09:04:26 +02001253 case CS420X_MBP55:
Stelian Pop3a385162009-07-30 14:44:27 +02001254 /* GPIO1 = headphones */
1255 /* GPIO3 = speakers */
1256 spec->gpio_mask = 0x0a;
1257 spec->gpio_dir = 0x0a;
Takashi Iwaied208252009-07-07 09:04:26 +02001258 break;
1259 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001260
Takashi Iwaied208252009-07-07 09:04:26 +02001261 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001262 if (err < 0)
1263 goto error;
1264
Takashi Iwaie5f14242009-07-01 18:11:44 +02001265 codec->patch_ops = cs_patch_ops;
1266
1267 return 0;
1268
1269 error:
1270 kfree(codec->spec);
1271 codec->spec = NULL;
1272 return err;
1273}
1274
1275
1276/*
1277 * patch entries
1278 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02001279static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001280 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1281 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
1282 {} /* terminator */
1283};
1284
1285MODULE_ALIAS("snd-hda-codec-id:10134206");
1286MODULE_ALIAS("snd-hda-codec-id:10134207");
1287
1288MODULE_LICENSE("GPL");
1289MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1290
1291static struct hda_codec_preset_list cirrus_list = {
1292 .preset = snd_hda_preset_cirrus,
1293 .owner = THIS_MODULE,
1294};
1295
1296static int __init patch_cirrus_init(void)
1297{
1298 return snd_hda_add_codec_preset(&cirrus_list);
1299}
1300
1301static void __exit patch_cirrus_exit(void)
1302{
1303 snd_hda_delete_codec_preset(&cirrus_list);
1304}
1305
1306module_init(patch_cirrus_init)
1307module_exit(patch_cirrus_exit)