blob: 2fbab8e29576bb236ce51fd5f4050bc0283843b6 [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"
Tim Howe56487c22011-07-22 16:41:00 -050029#include <sound/tlv.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020030
31/*
32 */
33
34struct cs_spec {
Takashi Iwaia6bae202009-07-06 15:15:22 +020035 int board_config;
Takashi Iwaie5f14242009-07-01 18:11:44 +020036 struct auto_pin_cfg autocfg;
37 struct hda_multi_out multiout;
38 struct snd_kcontrol *vmaster_sw;
39 struct snd_kcontrol *vmaster_vol;
40
41 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
42 hda_nid_t slave_dig_outs[2];
43
44 unsigned int input_idx[AUTO_PIN_LAST];
45 unsigned int capsrc_idx[AUTO_PIN_LAST];
46 hda_nid_t adc_nid[AUTO_PIN_LAST];
47 unsigned int adc_idx[AUTO_PIN_LAST];
48 unsigned int num_inputs;
49 unsigned int cur_input;
50 unsigned int automic_idx;
51 hda_nid_t cur_adc;
52 unsigned int cur_adc_stream_tag;
53 unsigned int cur_adc_format;
54 hda_nid_t dig_in;
55
Takashi Iwaic42d4782011-05-02 11:36:09 +020056 const struct hda_bind_ctls *capture_bind[2];
Takashi Iwaie5f14242009-07-01 18:11:44 +020057
Takashi Iwaied208252009-07-07 09:04:26 +020058 unsigned int gpio_mask;
59 unsigned int gpio_dir;
60 unsigned int gpio_data;
61
Takashi Iwaie5f14242009-07-01 18:11:44 +020062 struct hda_pcm pcm_rec[2]; /* PCM information */
63
64 unsigned int hp_detect:1;
65 unsigned int mic_detect:1;
Tim Howe56487c22011-07-22 16:41:00 -050066 /* CS421x */
67 unsigned int spdif_detect:1;
68 unsigned int sense_b:1;
69 hda_nid_t vendor_nid;
70 struct hda_input_mux input_mux;
71 unsigned int last_input;
Takashi Iwaie5f14242009-07-01 18:11:44 +020072};
73
Tim Howe56487c22011-07-22 16:41:00 -050074/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020075enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040076 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020077 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010078 CS420X_IMAC27,
Takashi Iwaia6bae202009-07-06 15:15:22 +020079 CS420X_AUTO,
80 CS420X_MODELS
81};
82
Tim Howe56487c22011-07-22 16:41:00 -050083/* CS421x boards */
84enum {
85 CS421X_CDB4210,
86 CS421X_MODELS
87};
88
Takashi Iwai40c20fa2009-07-06 13:00:57 +020089/* Vendor-specific processing widget */
90#define CS420X_VENDOR_NID 0x11
91#define CS_DIG_OUT1_PIN_NID 0x10
92#define CS_DIG_OUT2_PIN_NID 0x15
93#define CS_DMIC1_PIN_NID 0x12
94#define CS_DMIC2_PIN_NID 0x0e
95
96/* coef indices */
97#define IDX_SPDIF_STAT 0x0000
98#define IDX_SPDIF_CTL 0x0001
99#define IDX_ADC_CFG 0x0002
100/* SZC bitmask, 4 modes below:
101 * 0 = immediate,
102 * 1 = digital immediate, analog zero-cross
103 * 2 = digtail & analog soft-ramp
104 * 3 = digital soft-ramp, analog zero-cross
105 */
106#define CS_COEF_ADC_SZC_MASK (3 << 0)
107#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
108#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
109/* PGA mode: 0 = differential, 1 = signle-ended */
110#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
111#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
112#define IDX_DAC_CFG 0x0003
113/* SZC bitmask, 4 modes below:
114 * 0 = Immediate
115 * 1 = zero-cross
116 * 2 = soft-ramp
117 * 3 = soft-ramp on zero-cross
118 */
119#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
120#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
121#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
122
123#define IDX_BEEP_CFG 0x0004
124/* 0x0008 - test reg key */
125/* 0x0009 - 0x0014 -> 12 test regs */
126/* 0x0015 - visibility reg */
127
Tim Howe56487c22011-07-22 16:41:00 -0500128/*
129 * Cirrus Logic CS4210
130 *
131 * 1 DAC => HP(sense) / Speakers,
132 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
133 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
134*/
135#define CS4210_DAC_NID 0x02
136#define CS4210_ADC_NID 0x03
137#define CS421X_VENDOR_NID 0x0B
138#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
139#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
140
141#define CS421X_IDX_DEV_CFG 0x01
142#define CS421X_IDX_ADC_CFG 0x02
143#define CS421X_IDX_DAC_CFG 0x03
144#define CS421X_IDX_SPK_CTL 0x04
145
146#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200147
Takashi Iwai277a57c2009-07-08 12:42:08 +0200148static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200149{
Tim Howe56487c22011-07-22 16:41:00 -0500150 struct cs_spec *spec = codec->spec;
151 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200152 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500153 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200154 AC_VERB_GET_PROC_COEF, 0);
155}
156
Takashi Iwai277a57c2009-07-08 12:42:08 +0200157static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
158 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200159{
Tim Howe56487c22011-07-22 16:41:00 -0500160 struct cs_spec *spec = codec->spec;
161 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200162 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500163 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200164 AC_VERB_SET_PROC_COEF, coef);
165}
166
167
Takashi Iwaie5f14242009-07-01 18:11:44 +0200168#define HP_EVENT 1
169#define MIC_EVENT 2
170
171/*
172 * PCM callbacks
173 */
174static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
175 struct hda_codec *codec,
176 struct snd_pcm_substream *substream)
177{
178 struct cs_spec *spec = codec->spec;
179 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
180 hinfo);
181}
182
183static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
184 struct hda_codec *codec,
185 unsigned int stream_tag,
186 unsigned int format,
187 struct snd_pcm_substream *substream)
188{
189 struct cs_spec *spec = codec->spec;
190 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
191 stream_tag, format, substream);
192}
193
194static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
195 struct hda_codec *codec,
196 struct snd_pcm_substream *substream)
197{
198 struct cs_spec *spec = codec->spec;
199 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
200}
201
202/*
203 * Digital out
204 */
205static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
206 struct hda_codec *codec,
207 struct snd_pcm_substream *substream)
208{
209 struct cs_spec *spec = codec->spec;
210 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
211}
212
213static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
214 struct hda_codec *codec,
215 struct snd_pcm_substream *substream)
216{
217 struct cs_spec *spec = codec->spec;
218 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
219}
220
221static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
222 struct hda_codec *codec,
223 unsigned int stream_tag,
224 unsigned int format,
225 struct snd_pcm_substream *substream)
226{
227 struct cs_spec *spec = codec->spec;
228 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
229 format, substream);
230}
231
232static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
233 struct hda_codec *codec,
234 struct snd_pcm_substream *substream)
235{
236 struct cs_spec *spec = codec->spec;
237 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
238}
239
Takashi Iwai05ee7962011-11-16 18:05:11 +0100240static void cs_update_input_select(struct hda_codec *codec)
241{
242 struct cs_spec *spec = codec->spec;
243 if (spec->cur_adc)
244 snd_hda_codec_write(codec, spec->cur_adc, 0,
245 AC_VERB_SET_CONNECT_SEL,
246 spec->adc_idx[spec->cur_input]);
247}
248
Takashi Iwaie5f14242009-07-01 18:11:44 +0200249/*
250 * Analog capture
251 */
252static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
253 struct hda_codec *codec,
254 unsigned int stream_tag,
255 unsigned int format,
256 struct snd_pcm_substream *substream)
257{
258 struct cs_spec *spec = codec->spec;
259 spec->cur_adc = spec->adc_nid[spec->cur_input];
260 spec->cur_adc_stream_tag = stream_tag;
261 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100262 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200263 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
264 return 0;
265}
266
267static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
268 struct hda_codec *codec,
269 struct snd_pcm_substream *substream)
270{
271 struct cs_spec *spec = codec->spec;
272 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
273 spec->cur_adc = 0;
274 return 0;
275}
276
277/*
278 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200279static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200280 .substreams = 1,
281 .channels_min = 2,
282 .channels_max = 2,
283 .ops = {
284 .open = cs_playback_pcm_open,
285 .prepare = cs_playback_pcm_prepare,
286 .cleanup = cs_playback_pcm_cleanup
287 },
288};
289
Takashi Iwaic42d4782011-05-02 11:36:09 +0200290static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200291 .substreams = 1,
292 .channels_min = 2,
293 .channels_max = 2,
294 .ops = {
295 .prepare = cs_capture_pcm_prepare,
296 .cleanup = cs_capture_pcm_cleanup
297 },
298};
299
Takashi Iwaic42d4782011-05-02 11:36:09 +0200300static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200301 .substreams = 1,
302 .channels_min = 2,
303 .channels_max = 2,
304 .ops = {
305 .open = cs_dig_playback_pcm_open,
306 .close = cs_dig_playback_pcm_close,
307 .prepare = cs_dig_playback_pcm_prepare,
308 .cleanup = cs_dig_playback_pcm_cleanup
309 },
310};
311
Takashi Iwaic42d4782011-05-02 11:36:09 +0200312static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200313 .substreams = 1,
314 .channels_min = 2,
315 .channels_max = 2,
316};
317
318static int cs_build_pcms(struct hda_codec *codec)
319{
320 struct cs_spec *spec = codec->spec;
321 struct hda_pcm *info = spec->pcm_rec;
322
323 codec->pcm_info = info;
324 codec->num_pcms = 0;
325
326 info->name = "Cirrus Analog";
327 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
328 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
329 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
330 spec->multiout.max_channels;
331 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200332 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
333 spec->adc_nid[spec->cur_input];
334 codec->num_pcms++;
335
336 if (!spec->multiout.dig_out_nid && !spec->dig_in)
337 return 0;
338
339 info++;
340 info->name = "Cirrus Digital";
341 info->pcm_type = spec->autocfg.dig_out_type[0];
342 if (!info->pcm_type)
343 info->pcm_type = HDA_PCM_TYPE_SPDIF;
344 if (spec->multiout.dig_out_nid) {
345 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
346 cs_pcm_digital_playback;
347 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
348 spec->multiout.dig_out_nid;
349 }
350 if (spec->dig_in) {
351 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
352 cs_pcm_digital_capture;
353 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
354 }
355 codec->num_pcms++;
356
357 return 0;
358}
359
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200360/*
361 * parse codec topology
362 */
363
Takashi Iwaie5f14242009-07-01 18:11:44 +0200364static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
365{
366 hda_nid_t dac;
367 if (!pin)
368 return 0;
369 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
370 return 0;
371 return dac;
372}
373
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200374static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
375{
376 struct cs_spec *spec = codec->spec;
377 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200378 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200379 unsigned int val;
380 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200381 return 0;
382 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200383 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200384}
385
386static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
387 unsigned int *idxp)
388{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200389 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200390 hda_nid_t nid;
391
392 nid = codec->start_nid;
393 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200394 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200395 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200396 if (type != AC_WID_AUD_IN)
397 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200398 idx = snd_hda_get_conn_index(codec, nid, pin, false);
399 if (idx >= 0) {
400 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200401 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200402 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200403 }
404 return 0;
405}
406
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200407static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
408{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200409 unsigned int val;
410 val = snd_hda_codec_get_pincfg(codec, nid);
411 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
412}
413
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200414static int parse_output(struct hda_codec *codec)
415{
416 struct cs_spec *spec = codec->spec;
417 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200418 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200419 hda_nid_t dac;
420
421 for (i = 0; i < cfg->line_outs; i++) {
422 dac = get_dac(codec, cfg->line_out_pins[i]);
423 if (!dac)
424 break;
425 spec->dac_nid[i] = dac;
426 }
427 spec->multiout.num_dacs = i;
428 spec->multiout.dac_nids = spec->dac_nid;
429 spec->multiout.max_channels = i * 2;
430
431 /* add HP and speakers */
432 extra_nids = 0;
433 for (i = 0; i < cfg->hp_outs; i++) {
434 dac = get_dac(codec, cfg->hp_pins[i]);
435 if (!dac)
436 break;
437 if (!i)
438 spec->multiout.hp_nid = dac;
439 else
440 spec->multiout.extra_out_nid[extra_nids++] = dac;
441 }
442 for (i = 0; i < cfg->speaker_outs; i++) {
443 dac = get_dac(codec, cfg->speaker_pins[i]);
444 if (!dac)
445 break;
446 spec->multiout.extra_out_nid[extra_nids++] = dac;
447 }
448
449 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
450 cfg->speaker_outs = cfg->line_outs;
451 memcpy(cfg->speaker_pins, cfg->line_out_pins,
452 sizeof(cfg->speaker_pins));
453 cfg->line_outs = 0;
454 }
455
456 return 0;
457}
458
459static int parse_input(struct hda_codec *codec)
460{
461 struct cs_spec *spec = codec->spec;
462 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200463 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200464
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200465 for (i = 0; i < cfg->num_inputs; i++) {
466 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200467 spec->input_idx[spec->num_inputs] = i;
468 spec->capsrc_idx[i] = spec->num_inputs++;
469 spec->cur_input = i;
470 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
471 }
472 if (!spec->num_inputs)
473 return 0;
474
475 /* check whether the automatic mic switch is available */
476 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200477 cfg->inputs[0].type == AUTO_PIN_MIC &&
478 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200479 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
480 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200481 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200482 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200483 }
484 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200485 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200486 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200487 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200488 }
489 }
490 }
491 return 0;
492}
493
494
495static int parse_digital_output(struct hda_codec *codec)
496{
497 struct cs_spec *spec = codec->spec;
498 struct auto_pin_cfg *cfg = &spec->autocfg;
499 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200500
501 if (!cfg->dig_outs)
502 return 0;
503 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
504 return 0;
505 spec->multiout.dig_out_nid = nid;
506 spec->multiout.share_spdif = 1;
507 if (cfg->dig_outs > 1 &&
508 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
509 spec->slave_dig_outs[0] = nid;
510 codec->slave_dig_outs = spec->slave_dig_outs;
511 }
512 return 0;
513}
514
515static int parse_digital_input(struct hda_codec *codec)
516{
517 struct cs_spec *spec = codec->spec;
518 struct auto_pin_cfg *cfg = &spec->autocfg;
519 int idx;
520
Takashi Iwai60e53882009-07-06 15:01:09 +0200521 if (cfg->dig_in_pin)
522 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
523 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200524}
525
526/*
527 * create mixer controls
528 */
529
Takashi Iwaiea734962011-01-17 11:29:34 +0100530static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200531
532static int add_mute(struct hda_codec *codec, const char *name, int index,
533 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
534{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200535 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200536 struct snd_kcontrol_new knew =
537 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
538 knew.private_value = pval;
539 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
540 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100541 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100542 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200543}
544
545static int add_volume(struct hda_codec *codec, const char *name,
546 int index, unsigned int pval, int dir,
547 struct snd_kcontrol **kctlp)
548{
David Henningsson2e1210b2011-09-14 13:22:54 +0200549 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200550 struct snd_kcontrol_new knew =
551 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
552 knew.private_value = pval;
553 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
554 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100555 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100556 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200557}
558
559static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
560{
561 unsigned int caps;
562
563 /* set the upper-limit for mixer amp to 0dB */
564 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
565 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
566 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
567 << AC_AMPCAP_NUM_STEPS_SHIFT;
568 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
569}
570
571static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
572{
573 struct cs_spec *spec = codec->spec;
574 unsigned int tlv[4];
575 int err;
576
577 spec->vmaster_sw =
578 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100579 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200580 if (err < 0)
581 return err;
582
583 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
584 spec->vmaster_vol =
585 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100586 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200587 if (err < 0)
588 return err;
589 return 0;
590}
591
592static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
593 int num_ctls, int type)
594{
595 struct cs_spec *spec = codec->spec;
596 const char *name;
597 int err, index;
598 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200599 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200600 "Front Speaker", "Surround Speaker", "Bass Speaker"
601 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200602 static const char * const line_outs[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200603 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
604 };
605
606 fix_volume_caps(codec, dac);
607 if (!spec->vmaster_sw) {
608 err = add_vmaster(codec, dac);
609 if (err < 0)
610 return err;
611 }
612
613 index = 0;
614 switch (type) {
615 case AUTO_PIN_HP_OUT:
616 name = "Headphone";
617 index = idx;
618 break;
619 case AUTO_PIN_SPEAKER_OUT:
620 if (num_ctls > 1)
621 name = speakers[idx];
622 else
623 name = "Speaker";
624 break;
625 default:
626 if (num_ctls > 1)
627 name = line_outs[idx];
628 else
629 name = "Line-Out";
630 break;
631 }
632
633 err = add_mute(codec, name, index,
634 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
635 if (err < 0)
636 return err;
637 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
638 if (err < 0)
639 return err;
640
641 err = add_volume(codec, name, index,
642 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
643 if (err < 0)
644 return err;
645 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
646 if (err < 0)
647 return err;
648
649 return 0;
650}
651
652static int build_output(struct hda_codec *codec)
653{
654 struct cs_spec *spec = codec->spec;
655 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200656 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200657
658 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200659 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
660 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200661 if (err < 0)
662 return err;
663 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200664 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200665 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
666 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200667 if (err < 0)
668 return err;
669 }
670 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200671 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
672 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200673 if (err < 0)
674 return err;
675 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200676 return 0;
677}
678
679/*
680 */
681
Takashi Iwaic42d4782011-05-02 11:36:09 +0200682static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200683 HDA_BIND_SW("Capture Switch", 0),
684 HDA_BIND_VOL("Capture Volume", 0),
685};
686
Takashi Iwaiea359292009-07-06 12:58:59 +0200687static int change_cur_input(struct hda_codec *codec, unsigned int idx,
688 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200689{
690 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200691
Takashi Iwaiea359292009-07-06 12:58:59 +0200692 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200693 return 0;
694 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
695 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200696 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200697 spec->cur_adc = spec->adc_nid[idx];
698 snd_hda_codec_setup_stream(codec, spec->cur_adc,
699 spec->cur_adc_stream_tag, 0,
700 spec->cur_adc_format);
701 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200702 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100703 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200704 return 1;
705}
706
707static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
708 struct snd_ctl_elem_info *uinfo)
709{
710 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
711 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200712 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200713 unsigned int idx;
714
715 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
716 uinfo->count = 1;
717 uinfo->value.enumerated.items = spec->num_inputs;
718 if (uinfo->value.enumerated.item >= spec->num_inputs)
719 uinfo->value.enumerated.item = spec->num_inputs - 1;
720 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai10a20af2010-09-09 16:28:02 +0200721 strcpy(uinfo->value.enumerated.name,
722 hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
Takashi Iwaie5f14242009-07-01 18:11:44 +0200723 return 0;
724}
725
726static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
727 struct snd_ctl_elem_value *ucontrol)
728{
729 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
730 struct cs_spec *spec = codec->spec;
731 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
732 return 0;
733}
734
735static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
736 struct snd_ctl_elem_value *ucontrol)
737{
738 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
739 struct cs_spec *spec = codec->spec;
740 unsigned int idx = ucontrol->value.enumerated.item[0];
741
742 if (idx >= spec->num_inputs)
743 return -EINVAL;
744 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200745 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200746}
747
Takashi Iwaic42d4782011-05-02 11:36:09 +0200748static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200749 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
750 .name = "Capture Source",
751 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
752 .info = cs_capture_source_info,
753 .get = cs_capture_source_get,
754 .put = cs_capture_source_put,
755};
756
Takashi Iwaic42d4782011-05-02 11:36:09 +0200757static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200758 struct hda_ctl_ops *ops)
759{
760 struct cs_spec *spec = codec->spec;
761 struct hda_bind_ctls *bind;
762 int i, n;
763
764 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
765 GFP_KERNEL);
766 if (!bind)
767 return NULL;
768 bind->ops = ops;
769 n = 0;
770 for (i = 0; i < AUTO_PIN_LAST; i++) {
771 if (!spec->adc_nid[i])
772 continue;
773 bind->values[n++] =
774 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
775 spec->adc_idx[i], HDA_INPUT);
776 }
777 return bind;
778}
779
Takashi Iwai6a929342010-10-11 15:16:20 +0200780/* add a (input-boost) volume control to the given input pin */
781static int add_input_volume_control(struct hda_codec *codec,
782 struct auto_pin_cfg *cfg,
783 int item)
784{
785 hda_nid_t pin = cfg->inputs[item].pin;
786 u32 caps;
787 const char *label;
788 struct snd_kcontrol *kctl;
789
790 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
791 return 0;
792 caps = query_amp_caps(codec, pin, HDA_INPUT);
793 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
794 if (caps <= 1)
795 return 0;
796 label = hda_get_autocfg_input_label(codec, cfg, item);
797 return add_volume(codec, label, 0,
798 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
799}
800
Takashi Iwaie5f14242009-07-01 18:11:44 +0200801static int build_input(struct hda_codec *codec)
802{
803 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200804 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200805
Takashi Iwaie5f14242009-07-01 18:11:44 +0200806 if (!spec->num_inputs)
807 return 0;
808
Takashi Iwaie5f14242009-07-01 18:11:44 +0200809 /* make bind-capture */
810 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
811 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
812 for (i = 0; i < 2; i++) {
813 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100814 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200815 if (!spec->capture_bind[i])
816 return -ENOMEM;
817 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
818 if (!kctl)
819 return -ENOMEM;
820 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100821 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200822 if (err < 0)
823 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100824 for (n = 0; n < AUTO_PIN_LAST; n++) {
825 if (!spec->adc_nid[n])
826 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700827 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100828 if (err < 0)
829 return err;
830 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200831 }
832
833 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100834 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200835 snd_ctl_new1(&cs_capture_source, codec));
836 if (err < 0)
837 return err;
838 }
839
Takashi Iwai6a929342010-10-11 15:16:20 +0200840 for (i = 0; i < spec->num_inputs; i++) {
841 err = add_input_volume_control(codec, &spec->autocfg, i);
842 if (err < 0)
843 return err;
844 }
845
Takashi Iwaie5f14242009-07-01 18:11:44 +0200846 return 0;
847}
848
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200849/*
850 */
851
Takashi Iwaie5f14242009-07-01 18:11:44 +0200852static int build_digital_output(struct hda_codec *codec)
853{
854 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200855 int err;
856
Takashi Iwai63b24132009-07-09 11:45:59 +0200857 if (!spec->multiout.dig_out_nid)
858 return 0;
859
Stephen Warren74b654c2011-06-01 11:14:18 -0600860 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
861 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200862 if (err < 0)
863 return err;
864 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
865 if (err < 0)
866 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200867 return 0;
868}
869
870static int build_digital_input(struct hda_codec *codec)
871{
872 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200873 if (spec->dig_in)
874 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
875 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200876}
877
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200878/*
879 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500880 * CS421x auto-output redirecting
881 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200882 */
883
Takashi Iwaie5f14242009-07-01 18:11:44 +0200884static void cs_automute(struct hda_codec *codec)
885{
886 struct cs_spec *spec = codec->spec;
887 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200888 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500889 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200890 hda_nid_t nid;
891 int i;
892
Tim Howe56487c22011-07-22 16:41:00 -0500893 spdif_present = 0;
894 if (cfg->dig_outs) {
895 nid = cfg->dig_out_pins[0];
896 if (is_jack_detectable(codec, nid)) {
897 /*
898 TODO: SPDIF output redirect when SENSE_B is enabled.
899 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
900 assumed.
901 */
902 if (snd_hda_jack_detect(codec, nid)
903 /* && spec->sense_b */)
904 spdif_present = 1;
905 }
906 }
907
Takashi Iwaie5f14242009-07-01 18:11:44 +0200908 hp_present = 0;
909 for (i = 0; i < cfg->hp_outs; i++) {
910 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200911 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200912 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800913 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200914 if (hp_present)
915 break;
916 }
Tim Howe56487c22011-07-22 16:41:00 -0500917
918 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200919 for (i = 0; i < cfg->speaker_outs; i++) {
920 nid = cfg->speaker_pins[i];
921 snd_hda_codec_write(codec, nid, 0,
922 AC_VERB_SET_PIN_WIDGET_CONTROL,
923 hp_present ? 0 : PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -0500924 /* detect on spdif is specific to CS421x */
925 if (spec->vendor_nid == CS421X_VENDOR_NID) {
926 snd_hda_codec_write(codec, nid, 0,
927 AC_VERB_SET_PIN_WIDGET_CONTROL,
928 spdif_present ? 0 : PIN_OUT);
929 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200930 }
Vince Weaver4e7d7c62010-09-22 17:31:37 -0400931 if (spec->board_config == CS420X_MBP53 ||
932 spec->board_config == CS420X_MBP55 ||
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +0100933 spec->board_config == CS420X_IMAC27) {
Stelian Pop3a385162009-07-30 14:44:27 +0200934 unsigned int gpio = hp_present ? 0x02 : 0x08;
935 snd_hda_codec_write(codec, 0x01, 0,
936 AC_VERB_SET_GPIO_DATA, gpio);
937 }
Tim Howe56487c22011-07-22 16:41:00 -0500938
939 /* specific to CS421x */
940 if (spec->vendor_nid == CS421X_VENDOR_NID) {
941 /* mute HPs if spdif jack (SENSE_B) is present */
942 for (i = 0; i < cfg->hp_outs; i++) {
943 nid = cfg->hp_pins[i];
944 snd_hda_codec_write(codec, nid, 0,
945 AC_VERB_SET_PIN_WIDGET_CONTROL,
946 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
947 }
948
949 /* SPDIF TX on/off */
950 if (cfg->dig_outs) {
951 nid = cfg->dig_out_pins[0];
952 snd_hda_codec_write(codec, nid, 0,
953 AC_VERB_SET_PIN_WIDGET_CONTROL,
954 spdif_present ? PIN_OUT : 0);
955
956 }
957 /* Update board GPIOs if neccessary ... */
958 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200959}
960
Tim Howe56487c22011-07-22 16:41:00 -0500961/*
962 * Auto-input redirect for CS421x
963 * Switch max 3 inputs of a single ADC (nid 3)
964*/
965
Takashi Iwaie5f14242009-07-01 18:11:44 +0200966static void cs_automic(struct hda_codec *codec)
967{
968 struct cs_spec *spec = codec->spec;
969 struct auto_pin_cfg *cfg = &spec->autocfg;
970 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800971 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500972
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200973 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800974 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500975
976 /* specific to CS421x, single ADC */
977 if (spec->vendor_nid == CS421X_VENDOR_NID) {
978 if (present) {
979 spec->last_input = spec->cur_input;
980 spec->cur_input = spec->automic_idx;
981 } else {
982 spec->cur_input = spec->last_input;
983 }
Takashi Iwai05ee7962011-11-16 18:05:11 +0100984 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -0500985 } else {
986 if (present)
987 change_cur_input(codec, spec->automic_idx, 0);
988 else
989 change_cur_input(codec, !spec->automic_idx, 0);
990 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200991}
992
993/*
994 */
995
996static void init_output(struct hda_codec *codec)
997{
998 struct cs_spec *spec = codec->spec;
999 struct auto_pin_cfg *cfg = &spec->autocfg;
1000 int i;
1001
1002 /* mute first */
1003 for (i = 0; i < spec->multiout.num_dacs; i++)
1004 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1005 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1006 if (spec->multiout.hp_nid)
1007 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1008 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1009 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1010 if (!spec->multiout.extra_out_nid[i])
1011 break;
1012 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1013 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1014 }
1015
1016 /* set appropriate pin controls */
1017 for (i = 0; i < cfg->line_outs; i++)
1018 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1019 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001020 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001021 for (i = 0; i < cfg->hp_outs; i++) {
1022 hda_nid_t nid = cfg->hp_pins[i];
1023 snd_hda_codec_write(codec, nid, 0,
1024 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1025 if (!cfg->speaker_outs)
1026 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001027 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001028 snd_hda_codec_write(codec, nid, 0,
1029 AC_VERB_SET_UNSOLICITED_ENABLE,
1030 AC_USRSP_EN | HP_EVENT);
1031 spec->hp_detect = 1;
1032 }
1033 }
Tim Howe56487c22011-07-22 16:41:00 -05001034
1035 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001036 for (i = 0; i < cfg->speaker_outs; i++)
1037 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1038 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001039
1040 /* SPDIF is enabled on presence detect for CS421x */
1041 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001042 cs_automute(codec);
1043}
1044
1045static void init_input(struct hda_codec *codec)
1046{
1047 struct cs_spec *spec = codec->spec;
1048 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001049 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001050 int i;
1051
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001052 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001053 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001054 hda_nid_t pin = cfg->inputs[i].pin;
1055 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001056 continue;
1057 /* set appropriate pin control and mute first */
1058 ctl = PIN_IN;
Takashi Iwai86e29592010-09-09 14:50:17 +02001059 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001060 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1061 caps >>= AC_PINCAP_VREF_SHIFT;
1062 if (caps & AC_PINCAP_VREF_80)
1063 ctl = PIN_VREF80;
1064 }
1065 snd_hda_codec_write(codec, pin, 0,
1066 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1067 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1068 AC_VERB_SET_AMP_GAIN_MUTE,
1069 AMP_IN_MUTE(spec->adc_idx[i]));
1070 if (spec->mic_detect && spec->automic_idx == i)
1071 snd_hda_codec_write(codec, pin, 0,
1072 AC_VERB_SET_UNSOLICITED_ENABLE,
1073 AC_USRSP_EN | MIC_EVENT);
1074 }
Tim Howe56487c22011-07-22 16:41:00 -05001075 /* specific to CS421x */
1076 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1077 if (spec->mic_detect)
1078 cs_automic(codec);
1079 else {
1080 spec->cur_adc = spec->adc_nid[spec->cur_input];
Takashi Iwai05ee7962011-11-16 18:05:11 +01001081 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001082 }
1083 } else {
1084 change_cur_input(codec, spec->cur_input, 1);
1085 if (spec->mic_detect)
1086 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001087
Tim Howe56487c22011-07-22 16:41:00 -05001088 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1089 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1090 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1091 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1092 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1093 * No effect if SPDIF_OUT2 is
1094 * selected in IDX_SPDIF_CTL.
1095 */
1096 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1097 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001098}
1099
Takashi Iwaic42d4782011-05-02 11:36:09 +02001100static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001101 {0x11, AC_VERB_SET_PROC_STATE, 1},
1102 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1103 {0x11, AC_VERB_SET_PROC_COEF,
1104 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1105 | 0x0040 /* Mute DACs on FIFO error */
1106 | 0x1000 /* Enable DACs High Pass Filter */
1107 | 0x0400 /* Disable Coefficient Auto increment */
1108 )},
1109 /* Beep */
1110 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1111 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1112
1113 {} /* terminator */
1114};
1115
Brian Austina769cbc2010-09-07 14:36:22 -05001116/* Errata: CS4207 rev C0/C1/C2 Silicon
1117 *
1118 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1119 *
1120 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1121 * may be excessive (up to an additional 200 μA), which is most easily
1122 * observed while the part is being held in reset (RESET# active low).
1123 *
1124 * Root Cause: At initial powerup of the device, the logic that drives
1125 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1126 * initialized.
1127 * Certain random patterns will cause a steady leakage current in those
1128 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1129 *
1130 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1131 * blocks, which will alleviate the issue.
1132 */
1133
Takashi Iwaic42d4782011-05-02 11:36:09 +02001134static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001135 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1136 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1137
1138 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1139 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1140 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1141 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1142 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1143 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1144
1145 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1146 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1147
1148 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1149 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1150 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1151 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1152 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1153 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1154 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1155
Takashi Iwai38c07642011-03-03 14:54:19 +01001156#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001157 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1158 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1159 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001160#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001161
1162 {} /* terminator */
1163};
1164
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001165/* SPDIF setup */
1166static void init_digital(struct hda_codec *codec)
1167{
1168 unsigned int coef;
1169
1170 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1171 coef |= 0x0008; /* Replace with mute on error */
1172 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1173 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1174 * SPDIF_OUT2 is shared with GPIO1 and
1175 * DMIC_SDA2.
1176 */
1177 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001178}
1179
1180static int cs_init(struct hda_codec *codec)
1181{
1182 struct cs_spec *spec = codec->spec;
1183
Brian Austina769cbc2010-09-07 14:36:22 -05001184 /* init_verb sequence for C0/C1/C2 errata*/
1185 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1186
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001187 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001188
1189 if (spec->gpio_mask) {
1190 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1191 spec->gpio_mask);
1192 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1193 spec->gpio_dir);
1194 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1195 spec->gpio_data);
1196 }
1197
Takashi Iwaie5f14242009-07-01 18:11:44 +02001198 init_output(codec);
1199 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001200 init_digital(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001201 return 0;
1202}
1203
1204static int cs_build_controls(struct hda_codec *codec)
1205{
Takashi Iwaie5f14242009-07-01 18:11:44 +02001206 int err;
1207
1208 err = build_output(codec);
1209 if (err < 0)
1210 return err;
1211 err = build_input(codec);
1212 if (err < 0)
1213 return err;
1214 err = build_digital_output(codec);
1215 if (err < 0)
1216 return err;
1217 err = build_digital_input(codec);
1218 if (err < 0)
1219 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001220 return cs_init(codec);
1221}
1222
1223static void cs_free(struct hda_codec *codec)
1224{
1225 struct cs_spec *spec = codec->spec;
1226 kfree(spec->capture_bind[0]);
1227 kfree(spec->capture_bind[1]);
1228 kfree(codec->spec);
1229}
1230
1231static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1232{
1233 switch ((res >> 26) & 0x7f) {
1234 case HP_EVENT:
1235 cs_automute(codec);
1236 break;
1237 case MIC_EVENT:
1238 cs_automic(codec);
1239 break;
1240 }
1241}
1242
Takashi Iwaic42d4782011-05-02 11:36:09 +02001243static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001244 .build_controls = cs_build_controls,
1245 .build_pcms = cs_build_pcms,
1246 .init = cs_init,
1247 .free = cs_free,
1248 .unsol_event = cs_unsol_event,
1249};
1250
1251static int cs_parse_auto_config(struct hda_codec *codec)
1252{
1253 struct cs_spec *spec = codec->spec;
1254 int err;
1255
1256 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1257 if (err < 0)
1258 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001259
1260 err = parse_output(codec);
1261 if (err < 0)
1262 return err;
1263 err = parse_input(codec);
1264 if (err < 0)
1265 return err;
1266 err = parse_digital_output(codec);
1267 if (err < 0)
1268 return err;
1269 err = parse_digital_input(codec);
1270 if (err < 0)
1271 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001272 return 0;
1273}
1274
Takashi Iwaiea734962011-01-17 11:29:34 +01001275static const char * const cs420x_models[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001276 [CS420X_MBP53] = "mbp53",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001277 [CS420X_MBP55] = "mbp55",
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001278 [CS420X_IMAC27] = "imac27",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001279 [CS420X_AUTO] = "auto",
1280};
1281
1282
Takashi Iwaic42d4782011-05-02 11:36:09 +02001283static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001284 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001285 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001286 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001287 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001288 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001289 {} /* terminator */
1290};
1291
1292struct cs_pincfg {
1293 hda_nid_t nid;
1294 u32 val;
1295};
1296
Takashi Iwaic42d4782011-05-02 11:36:09 +02001297static const struct cs_pincfg mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001298 { 0x09, 0x012b4050 },
1299 { 0x0a, 0x90100141 },
1300 { 0x0b, 0x90100140 },
1301 { 0x0c, 0x018b3020 },
1302 { 0x0d, 0x90a00110 },
1303 { 0x0e, 0x400000f0 },
1304 { 0x0f, 0x01cbe030 },
1305 { 0x10, 0x014be060 },
1306 { 0x12, 0x400000f0 },
1307 { 0x15, 0x400000f0 },
1308 {} /* terminator */
1309};
1310
Takashi Iwaic42d4782011-05-02 11:36:09 +02001311static const struct cs_pincfg mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001312 { 0x09, 0x012b4030 },
1313 { 0x0a, 0x90100121 },
1314 { 0x0b, 0x90100120 },
1315 { 0x0c, 0x400000f0 },
1316 { 0x0d, 0x90a00110 },
1317 { 0x0e, 0x400000f0 },
1318 { 0x0f, 0x400000f0 },
1319 { 0x10, 0x014be040 },
1320 { 0x12, 0x400000f0 },
1321 { 0x15, 0x400000f0 },
1322 {} /* terminator */
1323};
1324
Takashi Iwaic42d4782011-05-02 11:36:09 +02001325static const struct cs_pincfg imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001326 { 0x09, 0x012b4050 },
1327 { 0x0a, 0x90100140 },
1328 { 0x0b, 0x90100142 },
1329 { 0x0c, 0x018b3020 },
1330 { 0x0d, 0x90a00110 },
1331 { 0x0e, 0x400000f0 },
1332 { 0x0f, 0x01cbe030 },
1333 { 0x10, 0x014be060 },
1334 { 0x12, 0x01ab9070 },
1335 { 0x15, 0x400000f0 },
1336 {} /* terminator */
1337};
1338
Takashi Iwaic42d4782011-05-02 11:36:09 +02001339static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001340 [CS420X_MBP53] = mbp53_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001341 [CS420X_MBP55] = mbp55_pincfgs,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001342 [CS420X_IMAC27] = imac27_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001343};
1344
Tim Howe56487c22011-07-22 16:41:00 -05001345static void fix_pincfg(struct hda_codec *codec, int model,
1346 const struct cs_pincfg **pin_configs)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001347{
Tim Howe56487c22011-07-22 16:41:00 -05001348 const struct cs_pincfg *cfg = pin_configs[model];
Takashi Iwaia6bae202009-07-06 15:15:22 +02001349 if (!cfg)
1350 return;
1351 for (; cfg->nid; cfg++)
1352 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1353}
1354
Takashi Iwaie5f14242009-07-01 18:11:44 +02001355static int patch_cs420x(struct hda_codec *codec)
1356{
1357 struct cs_spec *spec;
1358 int err;
1359
1360 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1361 if (!spec)
1362 return -ENOMEM;
1363 codec->spec = spec;
1364
Tim Howe56487c22011-07-22 16:41:00 -05001365 spec->vendor_nid = CS420X_VENDOR_NID;
1366
Takashi Iwaia6bae202009-07-06 15:15:22 +02001367 spec->board_config =
1368 snd_hda_check_board_config(codec, CS420X_MODELS,
1369 cs420x_models, cs420x_cfg_tbl);
1370 if (spec->board_config >= 0)
Tim Howe56487c22011-07-22 16:41:00 -05001371 fix_pincfg(codec, spec->board_config, cs_pincfgs);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001372
Takashi Iwaied208252009-07-07 09:04:26 +02001373 switch (spec->board_config) {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001374 case CS420X_IMAC27:
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001375 case CS420X_MBP53:
Takashi Iwaied208252009-07-07 09:04:26 +02001376 case CS420X_MBP55:
Stelian Pop3a385162009-07-30 14:44:27 +02001377 /* GPIO1 = headphones */
1378 /* GPIO3 = speakers */
1379 spec->gpio_mask = 0x0a;
1380 spec->gpio_dir = 0x0a;
Takashi Iwaied208252009-07-07 09:04:26 +02001381 break;
1382 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001383
Takashi Iwaied208252009-07-07 09:04:26 +02001384 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001385 if (err < 0)
1386 goto error;
1387
Takashi Iwaie5f14242009-07-01 18:11:44 +02001388 codec->patch_ops = cs_patch_ops;
1389
1390 return 0;
1391
1392 error:
1393 kfree(codec->spec);
1394 codec->spec = NULL;
1395 return err;
1396}
1397
Tim Howe56487c22011-07-22 16:41:00 -05001398/*
1399 * Cirrus Logic CS4210
1400 *
1401 * 1 DAC => HP(sense) / Speakers,
1402 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1403 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1404*/
1405
1406/* CS4210 board names */
1407static const char *cs421x_models[CS421X_MODELS] = {
1408 [CS421X_CDB4210] = "cdb4210",
1409};
1410
1411static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1412 /* Test Intel board + CDB2410 */
1413 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1414 {} /* terminator */
1415};
1416
1417/* CS4210 board pinconfigs */
1418/* Default CS4210 (CDB4210)*/
1419static const struct cs_pincfg cdb4210_pincfgs[] = {
1420 { 0x05, 0x0321401f },
1421 { 0x06, 0x90170010 },
1422 { 0x07, 0x03813031 },
1423 { 0x08, 0xb7a70037 },
1424 { 0x09, 0xb7a6003e },
1425 { 0x0a, 0x034510f0 },
1426 {} /* terminator */
1427};
1428
1429static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1430 [CS421X_CDB4210] = cdb4210_pincfgs,
1431};
1432
1433static const struct hda_verb cs421x_coef_init_verbs[] = {
1434 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1435 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1436 /*
1437 Disable Coefficient Index Auto-Increment(DAI)=1,
1438 PDREF=0
1439 */
1440 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1441
1442 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1443 /* ADC SZCMode = Digital Soft Ramp */
1444 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1445
1446 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1447 {0x0B, AC_VERB_SET_PROC_COEF,
1448 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1449 | 0x0004 /* Mute DAC on FIFO error */
1450 | 0x0008 /* Enable DAC High Pass Filter */
1451 )},
1452 {} /* terminator */
1453};
1454
1455/* Errata: CS4210 rev A1 Silicon
1456 *
1457 * http://www.cirrus.com/en/pubs/errata/
1458 *
1459 * Description:
1460 * 1. Performance degredation is present in the ADC.
1461 * 2. Speaker output is not completely muted upon HP detect.
1462 * 3. Noise is present when clipping occurs on the amplified
1463 * speaker outputs.
1464 *
1465 * Workaround:
1466 * The following verb sequence written to the registers during
1467 * initialization will correct the issues listed above.
1468 */
1469
1470static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1471 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1472
1473 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1474 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1475
1476 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1477 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1478
1479 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1480 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1481
1482 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1483 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1484
1485 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1486 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1487
1488 {} /* terminator */
1489};
1490
1491/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1492static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1493
1494static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1495 struct snd_ctl_elem_info *uinfo)
1496{
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1498 uinfo->count = 1;
1499 uinfo->value.integer.min = 0;
1500 uinfo->value.integer.max = 3;
1501 return 0;
1502}
1503
1504static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1505 struct snd_ctl_elem_value *ucontrol)
1506{
1507 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1508
1509 ucontrol->value.integer.value[0] =
1510 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1511 return 0;
1512}
1513
1514static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1515 struct snd_ctl_elem_value *ucontrol)
1516{
1517 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1518
1519 unsigned int vol = ucontrol->value.integer.value[0];
1520 unsigned int coef =
1521 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1522 unsigned int original_coef = coef;
1523
1524 coef &= ~0x0003;
1525 coef |= (vol & 0x0003);
1526 if (original_coef == coef)
1527 return 0;
1528 else {
1529 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1530 return 1;
1531 }
1532}
1533
1534static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1535
1536 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1537 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1538 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1539 .name = "Speaker Boost Playback Volume",
1540 .info = cs421x_boost_vol_info,
1541 .get = cs421x_boost_vol_get,
1542 .put = cs421x_boost_vol_put,
1543 .tlv = { .p = cs421x_speaker_boost_db_scale },
1544};
1545
1546static void cs421x_pinmux_init(struct hda_codec *codec)
1547{
1548 struct cs_spec *spec = codec->spec;
1549 unsigned int def_conf, coef;
1550
1551 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1552 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1553
1554 if (spec->gpio_mask)
1555 coef |= 0x0008; /* B1,B2 are GPIOs */
1556 else
1557 coef &= ~0x0008;
1558
1559 if (spec->sense_b)
1560 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1561 else
1562 coef &= ~0x0010;
1563
1564 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1565
1566 if ((spec->gpio_mask || spec->sense_b) &&
1567 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1568
1569 /*
1570 GPIO or SENSE_B forced - disconnect the DMIC pin.
1571 */
1572 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1573 def_conf &= ~AC_DEFCFG_PORT_CONN;
1574 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1575 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1576 }
1577}
1578
1579static void init_cs421x_digital(struct hda_codec *codec)
1580{
1581 struct cs_spec *spec = codec->spec;
1582 struct auto_pin_cfg *cfg = &spec->autocfg;
1583 int i;
1584
1585
1586 for (i = 0; i < cfg->dig_outs; i++) {
1587 hda_nid_t nid = cfg->dig_out_pins[i];
1588 if (!cfg->speaker_outs)
1589 continue;
1590 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1591
1592 snd_hda_codec_write(codec, nid, 0,
1593 AC_VERB_SET_UNSOLICITED_ENABLE,
1594 AC_USRSP_EN | SPDIF_EVENT);
1595 spec->spdif_detect = 1;
1596 }
1597 }
1598}
1599
1600static int cs421x_init(struct hda_codec *codec)
1601{
1602 struct cs_spec *spec = codec->spec;
1603
1604 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1605 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1606
1607 cs421x_pinmux_init(codec);
1608
1609 if (spec->gpio_mask) {
1610 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1611 spec->gpio_mask);
1612 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1613 spec->gpio_dir);
1614 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1615 spec->gpio_data);
1616 }
1617
1618 init_output(codec);
1619 init_input(codec);
1620 init_cs421x_digital(codec);
1621
1622 return 0;
1623}
1624
1625/*
1626 * CS4210 Input MUX (1 ADC)
1627 */
1628static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1629 struct snd_ctl_elem_info *uinfo)
1630{
1631 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1632 struct cs_spec *spec = codec->spec;
1633
1634 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1635}
1636
1637static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1638 struct snd_ctl_elem_value *ucontrol)
1639{
1640 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1641 struct cs_spec *spec = codec->spec;
1642
1643 ucontrol->value.enumerated.item[0] = spec->cur_input;
1644 return 0;
1645}
1646
1647static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1648 struct snd_ctl_elem_value *ucontrol)
1649{
1650 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1651 struct cs_spec *spec = codec->spec;
1652
1653 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1654 spec->adc_nid[0], &spec->cur_input);
1655
1656}
1657
1658static struct snd_kcontrol_new cs421x_capture_source = {
1659
1660 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1661 .name = "Capture Source",
1662 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1663 .info = cs421x_mux_enum_info,
1664 .get = cs421x_mux_enum_get,
1665 .put = cs421x_mux_enum_put,
1666};
1667
1668static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1669{
1670 struct cs_spec *spec = codec->spec;
1671 struct auto_pin_cfg *cfg = &spec->autocfg;
1672 const struct hda_input_mux *imux = &spec->input_mux;
1673 hda_nid_t pin = cfg->inputs[item].pin;
1674 struct snd_kcontrol *kctl;
1675 u32 caps;
1676
1677 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1678 return 0;
1679
1680 caps = query_amp_caps(codec, pin, HDA_INPUT);
1681 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1682 if (caps <= 1)
1683 return 0;
1684
1685 return add_volume(codec, imux->items[item].label, 0,
1686 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1687}
1688
1689/* add a (input-boost) volume control to the given input pin */
1690static int build_cs421x_input(struct hda_codec *codec)
1691{
1692 struct cs_spec *spec = codec->spec;
1693 struct auto_pin_cfg *cfg = &spec->autocfg;
1694 struct hda_input_mux *imux = &spec->input_mux;
1695 int i, err, type_idx;
1696 const char *label;
1697
1698 if (!spec->num_inputs)
1699 return 0;
1700
1701 /* make bind-capture */
1702 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1703 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1704 for (i = 0; i < 2; i++) {
1705 struct snd_kcontrol *kctl;
1706 int n;
1707 if (!spec->capture_bind[i])
1708 return -ENOMEM;
1709 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1710 if (!kctl)
1711 return -ENOMEM;
1712 kctl->private_value = (long)spec->capture_bind[i];
1713 err = snd_hda_ctl_add(codec, 0, kctl);
1714 if (err < 0)
1715 return err;
1716 for (n = 0; n < AUTO_PIN_LAST; n++) {
1717 if (!spec->adc_nid[n])
1718 continue;
1719 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1720 if (err < 0)
1721 return err;
1722 }
1723 }
1724
1725 /* Add Input MUX Items + Capture Volume/Switch */
1726 for (i = 0; i < spec->num_inputs; i++) {
1727 label = hda_get_autocfg_input_label(codec, cfg, i);
1728 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1729
1730 err = cs421x_add_input_volume_control(codec, i);
1731 if (err < 0)
1732 return err;
1733 }
1734
1735 /*
1736 Add 'Capture Source' Switch if
1737 * 2 inputs and no mic detec
1738 * 3 inputs
1739 */
1740 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1741 (spec->num_inputs == 3)) {
1742
1743 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1744 snd_ctl_new1(&cs421x_capture_source, codec));
1745 if (err < 0)
1746 return err;
1747 }
1748
1749 return 0;
1750}
1751
1752/* Single DAC (Mute/Gain) */
1753static int build_cs421x_output(struct hda_codec *codec)
1754{
1755 hda_nid_t dac = CS4210_DAC_NID;
1756 struct cs_spec *spec = codec->spec;
1757 struct auto_pin_cfg *cfg = &spec->autocfg;
1758 struct snd_kcontrol *kctl;
1759 int err;
1760 char *name = "HP/Speakers";
1761
1762 fix_volume_caps(codec, dac);
1763 if (!spec->vmaster_sw) {
1764 err = add_vmaster(codec, dac);
1765 if (err < 0)
1766 return err;
1767 }
1768
1769 err = add_mute(codec, name, 0,
1770 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1771 if (err < 0)
1772 return err;
1773 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1774 if (err < 0)
1775 return err;
1776
1777 err = add_volume(codec, name, 0,
1778 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1779 if (err < 0)
1780 return err;
1781 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1782 if (err < 0)
1783 return err;
1784
1785 if (cfg->speaker_outs) {
1786 err = snd_hda_ctl_add(codec, 0,
1787 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1788 if (err < 0)
1789 return err;
1790 }
1791 return err;
1792}
1793
1794static int cs421x_build_controls(struct hda_codec *codec)
1795{
1796 int err;
1797
1798 err = build_cs421x_output(codec);
1799 if (err < 0)
1800 return err;
1801 err = build_cs421x_input(codec);
1802 if (err < 0)
1803 return err;
1804 err = build_digital_output(codec);
1805 if (err < 0)
1806 return err;
1807 return cs421x_init(codec);
1808}
1809
1810static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1811{
1812 switch ((res >> 26) & 0x3f) {
1813 case HP_EVENT:
1814 case SPDIF_EVENT:
1815 cs_automute(codec);
1816 break;
1817
1818 case MIC_EVENT:
1819 cs_automic(codec);
1820 break;
1821 }
1822}
1823
1824static int parse_cs421x_input(struct hda_codec *codec)
1825{
1826 struct cs_spec *spec = codec->spec;
1827 struct auto_pin_cfg *cfg = &spec->autocfg;
1828 int i;
1829
1830 for (i = 0; i < cfg->num_inputs; i++) {
1831 hda_nid_t pin = cfg->inputs[i].pin;
1832 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1833 spec->cur_input = spec->last_input = i;
1834 spec->num_inputs++;
1835
1836 /* check whether the automatic mic switch is available */
1837 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1838 spec->mic_detect = 1;
1839 spec->automic_idx = i;
1840 }
1841 }
1842 return 0;
1843}
1844
1845static int cs421x_parse_auto_config(struct hda_codec *codec)
1846{
1847 struct cs_spec *spec = codec->spec;
1848 int err;
1849
1850 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1851 if (err < 0)
1852 return err;
1853 err = parse_output(codec);
1854 if (err < 0)
1855 return err;
1856 err = parse_cs421x_input(codec);
1857 if (err < 0)
1858 return err;
1859 err = parse_digital_output(codec);
1860 if (err < 0)
1861 return err;
1862 return 0;
1863}
1864
1865#ifdef CONFIG_PM
1866/*
1867 Manage PDREF, when transitioning to D3hot
1868 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1869*/
1870static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1871{
1872 unsigned int coef;
1873
1874 snd_hda_shutup_pins(codec);
1875
1876 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1877 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1878 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1879 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1880
1881 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1882 coef |= 0x0004; /* PDREF */
1883 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1884
1885 return 0;
1886}
1887#endif
1888
1889static struct hda_codec_ops cs4210_patch_ops = {
1890 .build_controls = cs421x_build_controls,
1891 .build_pcms = cs_build_pcms,
1892 .init = cs421x_init,
1893 .free = cs_free,
1894 .unsol_event = cs421x_unsol_event,
1895#ifdef CONFIG_PM
1896 .suspend = cs421x_suspend,
1897#endif
1898};
1899
1900static int patch_cs421x(struct hda_codec *codec)
1901{
1902 struct cs_spec *spec;
1903 int err;
1904
1905 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1906 if (!spec)
1907 return -ENOMEM;
1908 codec->spec = spec;
1909
1910 spec->vendor_nid = CS421X_VENDOR_NID;
1911
1912 spec->board_config =
1913 snd_hda_check_board_config(codec, CS421X_MODELS,
1914 cs421x_models, cs421x_cfg_tbl);
1915 if (spec->board_config >= 0)
1916 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1917 /*
1918 Setup GPIO/SENSE for each board (if used)
1919 */
1920 switch (spec->board_config) {
1921 case CS421X_CDB4210:
1922 snd_printd("CS4210 board: %s\n",
1923 cs421x_models[spec->board_config]);
1924/* spec->gpio_mask = 3;
1925 spec->gpio_dir = 3;
1926 spec->gpio_data = 3;
1927*/
1928 spec->sense_b = 1;
1929
1930 break;
1931 }
1932
1933 /*
1934 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1935 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1936 is disabled.
1937 */
1938 cs421x_pinmux_init(codec);
1939
1940 err = cs421x_parse_auto_config(codec);
1941 if (err < 0)
1942 goto error;
1943
1944 codec->patch_ops = cs4210_patch_ops;
1945
1946 return 0;
1947
1948 error:
1949 kfree(codec->spec);
1950 codec->spec = NULL;
1951 return err;
1952}
1953
Takashi Iwaie5f14242009-07-01 18:11:44 +02001954
1955/*
1956 * patch entries
1957 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02001958static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001959 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1960 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
Tim Howe56487c22011-07-22 16:41:00 -05001961 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
Takashi Iwaie5f14242009-07-01 18:11:44 +02001962 {} /* terminator */
1963};
1964
1965MODULE_ALIAS("snd-hda-codec-id:10134206");
1966MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05001967MODULE_ALIAS("snd-hda-codec-id:10134210");
Takashi Iwaie5f14242009-07-01 18:11:44 +02001968
1969MODULE_LICENSE("GPL");
1970MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1971
1972static struct hda_codec_preset_list cirrus_list = {
1973 .preset = snd_hda_preset_cirrus,
1974 .owner = THIS_MODULE,
1975};
1976
1977static int __init patch_cirrus_init(void)
1978{
1979 return snd_hda_add_codec_preset(&cirrus_list);
1980}
1981
1982static void __exit patch_cirrus_exit(void)
1983{
1984 snd_hda_delete_codec_preset(&cirrus_list);
1985}
1986
1987module_init(patch_cirrus_init)
1988module_exit(patch_cirrus_exit)