blob: c45f3e69bcf0483ab559b377e657638edb36798e [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"
Tim Howe56487c22011-07-22 16:41:00 -050028#include <sound/tlv.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020029
30/*
31 */
32
33struct cs_spec {
Takashi Iwaia6bae202009-07-06 15:15:22 +020034 int board_config;
Takashi Iwaie5f14242009-07-01 18:11:44 +020035 struct auto_pin_cfg autocfg;
36 struct hda_multi_out multiout;
37 struct snd_kcontrol *vmaster_sw;
38 struct snd_kcontrol *vmaster_vol;
39
40 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
41 hda_nid_t slave_dig_outs[2];
42
43 unsigned int input_idx[AUTO_PIN_LAST];
44 unsigned int capsrc_idx[AUTO_PIN_LAST];
45 hda_nid_t adc_nid[AUTO_PIN_LAST];
46 unsigned int adc_idx[AUTO_PIN_LAST];
47 unsigned int num_inputs;
48 unsigned int cur_input;
49 unsigned int automic_idx;
50 hda_nid_t cur_adc;
51 unsigned int cur_adc_stream_tag;
52 unsigned int cur_adc_format;
53 hda_nid_t dig_in;
54
Takashi Iwaic42d4782011-05-02 11:36:09 +020055 const struct hda_bind_ctls *capture_bind[2];
Takashi Iwaie5f14242009-07-01 18:11:44 +020056
Takashi Iwaied208252009-07-07 09:04:26 +020057 unsigned int gpio_mask;
58 unsigned int gpio_dir;
59 unsigned int gpio_data;
60
Takashi Iwaie5f14242009-07-01 18:11:44 +020061 struct hda_pcm pcm_rec[2]; /* PCM information */
62
63 unsigned int hp_detect:1;
64 unsigned int mic_detect:1;
Tim Howe56487c22011-07-22 16:41:00 -050065 /* CS421x */
66 unsigned int spdif_detect:1;
67 unsigned int sense_b:1;
68 hda_nid_t vendor_nid;
69 struct hda_input_mux input_mux;
70 unsigned int last_input;
Takashi Iwaie5f14242009-07-01 18:11:44 +020071};
72
Tim Howe56487c22011-07-22 16:41:00 -050073/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020074enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040075 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020076 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010077 CS420X_IMAC27,
Takashi Iwaia6bae202009-07-06 15:15:22 +020078 CS420X_AUTO,
79 CS420X_MODELS
80};
81
Tim Howe56487c22011-07-22 16:41:00 -050082/* CS421x boards */
83enum {
84 CS421X_CDB4210,
85 CS421X_MODELS
86};
87
Takashi Iwai40c20fa2009-07-06 13:00:57 +020088/* Vendor-specific processing widget */
89#define CS420X_VENDOR_NID 0x11
90#define CS_DIG_OUT1_PIN_NID 0x10
91#define CS_DIG_OUT2_PIN_NID 0x15
92#define CS_DMIC1_PIN_NID 0x12
93#define CS_DMIC2_PIN_NID 0x0e
94
95/* coef indices */
96#define IDX_SPDIF_STAT 0x0000
97#define IDX_SPDIF_CTL 0x0001
98#define IDX_ADC_CFG 0x0002
99/* SZC bitmask, 4 modes below:
100 * 0 = immediate,
101 * 1 = digital immediate, analog zero-cross
102 * 2 = digtail & analog soft-ramp
103 * 3 = digital soft-ramp, analog zero-cross
104 */
105#define CS_COEF_ADC_SZC_MASK (3 << 0)
106#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
107#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
108/* PGA mode: 0 = differential, 1 = signle-ended */
109#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
110#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
111#define IDX_DAC_CFG 0x0003
112/* SZC bitmask, 4 modes below:
113 * 0 = Immediate
114 * 1 = zero-cross
115 * 2 = soft-ramp
116 * 3 = soft-ramp on zero-cross
117 */
118#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
119#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
120#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
121
122#define IDX_BEEP_CFG 0x0004
123/* 0x0008 - test reg key */
124/* 0x0009 - 0x0014 -> 12 test regs */
125/* 0x0015 - visibility reg */
126
Tim Howe56487c22011-07-22 16:41:00 -0500127/*
128 * Cirrus Logic CS4210
129 *
130 * 1 DAC => HP(sense) / Speakers,
131 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
132 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
133*/
134#define CS4210_DAC_NID 0x02
135#define CS4210_ADC_NID 0x03
136#define CS421X_VENDOR_NID 0x0B
137#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
138#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
139
140#define CS421X_IDX_DEV_CFG 0x01
141#define CS421X_IDX_ADC_CFG 0x02
142#define CS421X_IDX_DAC_CFG 0x03
143#define CS421X_IDX_SPK_CTL 0x04
144
145#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200146
Takashi Iwai277a57c2009-07-08 12:42:08 +0200147static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200148{
Tim Howe56487c22011-07-22 16:41:00 -0500149 struct cs_spec *spec = codec->spec;
150 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200151 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500152 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200153 AC_VERB_GET_PROC_COEF, 0);
154}
155
Takashi Iwai277a57c2009-07-08 12:42:08 +0200156static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
157 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200158{
Tim Howe56487c22011-07-22 16:41:00 -0500159 struct cs_spec *spec = codec->spec;
160 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200161 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500162 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200163 AC_VERB_SET_PROC_COEF, coef);
164}
165
166
Takashi Iwaie5f14242009-07-01 18:11:44 +0200167#define HP_EVENT 1
168#define MIC_EVENT 2
169
170/*
171 * PCM callbacks
172 */
173static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
174 struct hda_codec *codec,
175 struct snd_pcm_substream *substream)
176{
177 struct cs_spec *spec = codec->spec;
178 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
179 hinfo);
180}
181
182static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
183 struct hda_codec *codec,
184 unsigned int stream_tag,
185 unsigned int format,
186 struct snd_pcm_substream *substream)
187{
188 struct cs_spec *spec = codec->spec;
189 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
190 stream_tag, format, substream);
191}
192
193static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
194 struct hda_codec *codec,
195 struct snd_pcm_substream *substream)
196{
197 struct cs_spec *spec = codec->spec;
198 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
199}
200
201/*
202 * Digital out
203 */
204static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
205 struct hda_codec *codec,
206 struct snd_pcm_substream *substream)
207{
208 struct cs_spec *spec = codec->spec;
209 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
210}
211
212static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
213 struct hda_codec *codec,
214 struct snd_pcm_substream *substream)
215{
216 struct cs_spec *spec = codec->spec;
217 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
218}
219
220static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
221 struct hda_codec *codec,
222 unsigned int stream_tag,
223 unsigned int format,
224 struct snd_pcm_substream *substream)
225{
226 struct cs_spec *spec = codec->spec;
227 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
228 format, substream);
229}
230
231static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
232 struct hda_codec *codec,
233 struct snd_pcm_substream *substream)
234{
235 struct cs_spec *spec = codec->spec;
236 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
237}
238
239/*
240 * Analog capture
241 */
242static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
243 struct hda_codec *codec,
244 unsigned int stream_tag,
245 unsigned int format,
246 struct snd_pcm_substream *substream)
247{
248 struct cs_spec *spec = codec->spec;
249 spec->cur_adc = spec->adc_nid[spec->cur_input];
250 spec->cur_adc_stream_tag = stream_tag;
251 spec->cur_adc_format = format;
252 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
253 return 0;
254}
255
256static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
257 struct hda_codec *codec,
258 struct snd_pcm_substream *substream)
259{
260 struct cs_spec *spec = codec->spec;
261 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
262 spec->cur_adc = 0;
263 return 0;
264}
265
266/*
267 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200268static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200269 .substreams = 1,
270 .channels_min = 2,
271 .channels_max = 2,
272 .ops = {
273 .open = cs_playback_pcm_open,
274 .prepare = cs_playback_pcm_prepare,
275 .cleanup = cs_playback_pcm_cleanup
276 },
277};
278
Takashi Iwaic42d4782011-05-02 11:36:09 +0200279static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200280 .substreams = 1,
281 .channels_min = 2,
282 .channels_max = 2,
283 .ops = {
284 .prepare = cs_capture_pcm_prepare,
285 .cleanup = cs_capture_pcm_cleanup
286 },
287};
288
Takashi Iwaic42d4782011-05-02 11:36:09 +0200289static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200290 .substreams = 1,
291 .channels_min = 2,
292 .channels_max = 2,
293 .ops = {
294 .open = cs_dig_playback_pcm_open,
295 .close = cs_dig_playback_pcm_close,
296 .prepare = cs_dig_playback_pcm_prepare,
297 .cleanup = cs_dig_playback_pcm_cleanup
298 },
299};
300
Takashi Iwaic42d4782011-05-02 11:36:09 +0200301static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200302 .substreams = 1,
303 .channels_min = 2,
304 .channels_max = 2,
305};
306
307static int cs_build_pcms(struct hda_codec *codec)
308{
309 struct cs_spec *spec = codec->spec;
310 struct hda_pcm *info = spec->pcm_rec;
311
312 codec->pcm_info = info;
313 codec->num_pcms = 0;
314
315 info->name = "Cirrus Analog";
316 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
317 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
318 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
319 spec->multiout.max_channels;
320 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200321 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
322 spec->adc_nid[spec->cur_input];
323 codec->num_pcms++;
324
325 if (!spec->multiout.dig_out_nid && !spec->dig_in)
326 return 0;
327
328 info++;
329 info->name = "Cirrus Digital";
330 info->pcm_type = spec->autocfg.dig_out_type[0];
331 if (!info->pcm_type)
332 info->pcm_type = HDA_PCM_TYPE_SPDIF;
333 if (spec->multiout.dig_out_nid) {
334 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
335 cs_pcm_digital_playback;
336 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
337 spec->multiout.dig_out_nid;
338 }
339 if (spec->dig_in) {
340 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
341 cs_pcm_digital_capture;
342 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
343 }
344 codec->num_pcms++;
345
346 return 0;
347}
348
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200349/*
350 * parse codec topology
351 */
352
Takashi Iwaie5f14242009-07-01 18:11:44 +0200353static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
354{
355 hda_nid_t dac;
356 if (!pin)
357 return 0;
358 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
359 return 0;
360 return dac;
361}
362
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200363static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
364{
365 struct cs_spec *spec = codec->spec;
366 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200367 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200368 unsigned int val;
369 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200370 return 0;
371 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200372 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200373}
374
375static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
376 unsigned int *idxp)
377{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200378 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200379 hda_nid_t nid;
380
381 nid = codec->start_nid;
382 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200383 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200384 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200385 if (type != AC_WID_AUD_IN)
386 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200387 idx = snd_hda_get_conn_index(codec, nid, pin, false);
388 if (idx >= 0) {
389 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200390 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200391 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200392 }
393 return 0;
394}
395
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200396static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
397{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200398 unsigned int val;
399 val = snd_hda_codec_get_pincfg(codec, nid);
400 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
401}
402
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200403static int parse_output(struct hda_codec *codec)
404{
405 struct cs_spec *spec = codec->spec;
406 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200407 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200408 hda_nid_t dac;
409
410 for (i = 0; i < cfg->line_outs; i++) {
411 dac = get_dac(codec, cfg->line_out_pins[i]);
412 if (!dac)
413 break;
414 spec->dac_nid[i] = dac;
415 }
416 spec->multiout.num_dacs = i;
417 spec->multiout.dac_nids = spec->dac_nid;
418 spec->multiout.max_channels = i * 2;
419
420 /* add HP and speakers */
421 extra_nids = 0;
422 for (i = 0; i < cfg->hp_outs; i++) {
423 dac = get_dac(codec, cfg->hp_pins[i]);
424 if (!dac)
425 break;
426 if (!i)
427 spec->multiout.hp_nid = dac;
428 else
429 spec->multiout.extra_out_nid[extra_nids++] = dac;
430 }
431 for (i = 0; i < cfg->speaker_outs; i++) {
432 dac = get_dac(codec, cfg->speaker_pins[i]);
433 if (!dac)
434 break;
435 spec->multiout.extra_out_nid[extra_nids++] = dac;
436 }
437
438 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
439 cfg->speaker_outs = cfg->line_outs;
440 memcpy(cfg->speaker_pins, cfg->line_out_pins,
441 sizeof(cfg->speaker_pins));
442 cfg->line_outs = 0;
443 }
444
445 return 0;
446}
447
448static int parse_input(struct hda_codec *codec)
449{
450 struct cs_spec *spec = codec->spec;
451 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200452 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200453
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200454 for (i = 0; i < cfg->num_inputs; i++) {
455 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200456 spec->input_idx[spec->num_inputs] = i;
457 spec->capsrc_idx[i] = spec->num_inputs++;
458 spec->cur_input = i;
459 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
460 }
461 if (!spec->num_inputs)
462 return 0;
463
464 /* check whether the automatic mic switch is available */
465 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200466 cfg->inputs[0].type == AUTO_PIN_MIC &&
467 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200468 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
469 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200470 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200471 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200472 }
473 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200474 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200475 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200476 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200477 }
478 }
479 }
480 return 0;
481}
482
483
484static int parse_digital_output(struct hda_codec *codec)
485{
486 struct cs_spec *spec = codec->spec;
487 struct auto_pin_cfg *cfg = &spec->autocfg;
488 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200489
490 if (!cfg->dig_outs)
491 return 0;
492 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
493 return 0;
494 spec->multiout.dig_out_nid = nid;
495 spec->multiout.share_spdif = 1;
496 if (cfg->dig_outs > 1 &&
497 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
498 spec->slave_dig_outs[0] = nid;
499 codec->slave_dig_outs = spec->slave_dig_outs;
500 }
501 return 0;
502}
503
504static int parse_digital_input(struct hda_codec *codec)
505{
506 struct cs_spec *spec = codec->spec;
507 struct auto_pin_cfg *cfg = &spec->autocfg;
508 int idx;
509
Takashi Iwai60e53882009-07-06 15:01:09 +0200510 if (cfg->dig_in_pin)
511 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
512 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200513}
514
515/*
516 * create mixer controls
517 */
518
Takashi Iwaiea734962011-01-17 11:29:34 +0100519static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200520
521static int add_mute(struct hda_codec *codec, const char *name, int index,
522 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
523{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200524 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200525 struct snd_kcontrol_new knew =
526 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
527 knew.private_value = pval;
528 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
529 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100530 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100531 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200532}
533
534static int add_volume(struct hda_codec *codec, const char *name,
535 int index, unsigned int pval, int dir,
536 struct snd_kcontrol **kctlp)
537{
David Henningsson2e1210b2011-09-14 13:22:54 +0200538 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200539 struct snd_kcontrol_new knew =
540 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
541 knew.private_value = pval;
542 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
543 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100544 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100545 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200546}
547
548static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
549{
550 unsigned int caps;
551
552 /* set the upper-limit for mixer amp to 0dB */
553 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
554 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
555 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
556 << AC_AMPCAP_NUM_STEPS_SHIFT;
557 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
558}
559
560static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
561{
562 struct cs_spec *spec = codec->spec;
563 unsigned int tlv[4];
564 int err;
565
566 spec->vmaster_sw =
567 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100568 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200569 if (err < 0)
570 return err;
571
572 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
573 spec->vmaster_vol =
574 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100575 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200576 if (err < 0)
577 return err;
578 return 0;
579}
580
581static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
582 int num_ctls, int type)
583{
584 struct cs_spec *spec = codec->spec;
585 const char *name;
586 int err, index;
587 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200588 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200589 "Front Speaker", "Surround Speaker", "Bass Speaker"
590 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200591 static const char * const line_outs[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200592 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
593 };
594
595 fix_volume_caps(codec, dac);
596 if (!spec->vmaster_sw) {
597 err = add_vmaster(codec, dac);
598 if (err < 0)
599 return err;
600 }
601
602 index = 0;
603 switch (type) {
604 case AUTO_PIN_HP_OUT:
605 name = "Headphone";
606 index = idx;
607 break;
608 case AUTO_PIN_SPEAKER_OUT:
609 if (num_ctls > 1)
610 name = speakers[idx];
611 else
612 name = "Speaker";
613 break;
614 default:
615 if (num_ctls > 1)
616 name = line_outs[idx];
617 else
618 name = "Line-Out";
619 break;
620 }
621
622 err = add_mute(codec, name, index,
623 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
624 if (err < 0)
625 return err;
626 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
627 if (err < 0)
628 return err;
629
630 err = add_volume(codec, name, index,
631 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
632 if (err < 0)
633 return err;
634 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
635 if (err < 0)
636 return err;
637
638 return 0;
639}
640
641static int build_output(struct hda_codec *codec)
642{
643 struct cs_spec *spec = codec->spec;
644 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200645 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200646
647 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200648 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
649 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200650 if (err < 0)
651 return err;
652 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200653 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200654 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
655 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200656 if (err < 0)
657 return err;
658 }
659 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200660 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
661 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200662 if (err < 0)
663 return err;
664 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200665 return 0;
666}
667
668/*
669 */
670
Takashi Iwaic42d4782011-05-02 11:36:09 +0200671static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200672 HDA_BIND_SW("Capture Switch", 0),
673 HDA_BIND_VOL("Capture Volume", 0),
674};
675
Takashi Iwaiea359292009-07-06 12:58:59 +0200676static int change_cur_input(struct hda_codec *codec, unsigned int idx,
677 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200678{
679 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200680
Takashi Iwaiea359292009-07-06 12:58:59 +0200681 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200682 return 0;
683 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
684 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200685 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200686 spec->cur_adc = spec->adc_nid[idx];
687 snd_hda_codec_setup_stream(codec, spec->cur_adc,
688 spec->cur_adc_stream_tag, 0,
689 spec->cur_adc_format);
690 }
691 snd_hda_codec_write(codec, spec->cur_adc, 0,
692 AC_VERB_SET_CONNECT_SEL,
693 spec->adc_idx[idx]);
694 spec->cur_input = idx;
695 return 1;
696}
697
698static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
699 struct snd_ctl_elem_info *uinfo)
700{
701 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
702 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200703 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200704 unsigned int idx;
705
706 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
707 uinfo->count = 1;
708 uinfo->value.enumerated.items = spec->num_inputs;
709 if (uinfo->value.enumerated.item >= spec->num_inputs)
710 uinfo->value.enumerated.item = spec->num_inputs - 1;
711 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai10a20af2010-09-09 16:28:02 +0200712 strcpy(uinfo->value.enumerated.name,
713 hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
Takashi Iwaie5f14242009-07-01 18:11:44 +0200714 return 0;
715}
716
717static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
718 struct snd_ctl_elem_value *ucontrol)
719{
720 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
721 struct cs_spec *spec = codec->spec;
722 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
723 return 0;
724}
725
726static int cs_capture_source_put(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 unsigned int idx = ucontrol->value.enumerated.item[0];
732
733 if (idx >= spec->num_inputs)
734 return -EINVAL;
735 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200736 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200737}
738
Takashi Iwaic42d4782011-05-02 11:36:09 +0200739static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200740 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
741 .name = "Capture Source",
742 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
743 .info = cs_capture_source_info,
744 .get = cs_capture_source_get,
745 .put = cs_capture_source_put,
746};
747
Takashi Iwaic42d4782011-05-02 11:36:09 +0200748static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200749 struct hda_ctl_ops *ops)
750{
751 struct cs_spec *spec = codec->spec;
752 struct hda_bind_ctls *bind;
753 int i, n;
754
755 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
756 GFP_KERNEL);
757 if (!bind)
758 return NULL;
759 bind->ops = ops;
760 n = 0;
761 for (i = 0; i < AUTO_PIN_LAST; i++) {
762 if (!spec->adc_nid[i])
763 continue;
764 bind->values[n++] =
765 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
766 spec->adc_idx[i], HDA_INPUT);
767 }
768 return bind;
769}
770
Takashi Iwai6a929342010-10-11 15:16:20 +0200771/* add a (input-boost) volume control to the given input pin */
772static int add_input_volume_control(struct hda_codec *codec,
773 struct auto_pin_cfg *cfg,
774 int item)
775{
776 hda_nid_t pin = cfg->inputs[item].pin;
777 u32 caps;
778 const char *label;
779 struct snd_kcontrol *kctl;
780
781 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
782 return 0;
783 caps = query_amp_caps(codec, pin, HDA_INPUT);
784 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
785 if (caps <= 1)
786 return 0;
787 label = hda_get_autocfg_input_label(codec, cfg, item);
788 return add_volume(codec, label, 0,
789 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
790}
791
Takashi Iwaie5f14242009-07-01 18:11:44 +0200792static int build_input(struct hda_codec *codec)
793{
794 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200795 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200796
Takashi Iwaie5f14242009-07-01 18:11:44 +0200797 if (!spec->num_inputs)
798 return 0;
799
Takashi Iwaie5f14242009-07-01 18:11:44 +0200800 /* make bind-capture */
801 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
802 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
803 for (i = 0; i < 2; i++) {
804 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100805 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200806 if (!spec->capture_bind[i])
807 return -ENOMEM;
808 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
809 if (!kctl)
810 return -ENOMEM;
811 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100812 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200813 if (err < 0)
814 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100815 for (n = 0; n < AUTO_PIN_LAST; n++) {
816 if (!spec->adc_nid[n])
817 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700818 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100819 if (err < 0)
820 return err;
821 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200822 }
823
824 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100825 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200826 snd_ctl_new1(&cs_capture_source, codec));
827 if (err < 0)
828 return err;
829 }
830
Takashi Iwai6a929342010-10-11 15:16:20 +0200831 for (i = 0; i < spec->num_inputs; i++) {
832 err = add_input_volume_control(codec, &spec->autocfg, i);
833 if (err < 0)
834 return err;
835 }
836
Takashi Iwaie5f14242009-07-01 18:11:44 +0200837 return 0;
838}
839
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200840/*
841 */
842
Takashi Iwaie5f14242009-07-01 18:11:44 +0200843static int build_digital_output(struct hda_codec *codec)
844{
845 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200846 int err;
847
Takashi Iwai63b24132009-07-09 11:45:59 +0200848 if (!spec->multiout.dig_out_nid)
849 return 0;
850
Stephen Warren74b654c2011-06-01 11:14:18 -0600851 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
852 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200853 if (err < 0)
854 return err;
855 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
856 if (err < 0)
857 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200858 return 0;
859}
860
861static int build_digital_input(struct hda_codec *codec)
862{
863 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200864 if (spec->dig_in)
865 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
866 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200867}
868
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200869/*
870 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500871 * CS421x auto-output redirecting
872 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200873 */
874
Takashi Iwaie5f14242009-07-01 18:11:44 +0200875static void cs_automute(struct hda_codec *codec)
876{
877 struct cs_spec *spec = codec->spec;
878 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200879 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500880 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200881 hda_nid_t nid;
882 int i;
883
Tim Howe56487c22011-07-22 16:41:00 -0500884 spdif_present = 0;
885 if (cfg->dig_outs) {
886 nid = cfg->dig_out_pins[0];
887 if (is_jack_detectable(codec, nid)) {
888 /*
889 TODO: SPDIF output redirect when SENSE_B is enabled.
890 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
891 assumed.
892 */
893 if (snd_hda_jack_detect(codec, nid)
894 /* && spec->sense_b */)
895 spdif_present = 1;
896 }
897 }
898
Takashi Iwaie5f14242009-07-01 18:11:44 +0200899 hp_present = 0;
900 for (i = 0; i < cfg->hp_outs; i++) {
901 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200902 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200903 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800904 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200905 if (hp_present)
906 break;
907 }
Tim Howe56487c22011-07-22 16:41:00 -0500908
909 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200910 for (i = 0; i < cfg->speaker_outs; i++) {
911 nid = cfg->speaker_pins[i];
912 snd_hda_codec_write(codec, nid, 0,
913 AC_VERB_SET_PIN_WIDGET_CONTROL,
914 hp_present ? 0 : PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -0500915 /* detect on spdif is specific to CS421x */
916 if (spec->vendor_nid == CS421X_VENDOR_NID) {
917 snd_hda_codec_write(codec, nid, 0,
918 AC_VERB_SET_PIN_WIDGET_CONTROL,
919 spdif_present ? 0 : PIN_OUT);
920 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200921 }
Vince Weaver4e7d7c62010-09-22 17:31:37 -0400922 if (spec->board_config == CS420X_MBP53 ||
923 spec->board_config == CS420X_MBP55 ||
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +0100924 spec->board_config == CS420X_IMAC27) {
Stelian Pop3a385162009-07-30 14:44:27 +0200925 unsigned int gpio = hp_present ? 0x02 : 0x08;
926 snd_hda_codec_write(codec, 0x01, 0,
927 AC_VERB_SET_GPIO_DATA, gpio);
928 }
Tim Howe56487c22011-07-22 16:41:00 -0500929
930 /* specific to CS421x */
931 if (spec->vendor_nid == CS421X_VENDOR_NID) {
932 /* mute HPs if spdif jack (SENSE_B) is present */
933 for (i = 0; i < cfg->hp_outs; i++) {
934 nid = cfg->hp_pins[i];
935 snd_hda_codec_write(codec, nid, 0,
936 AC_VERB_SET_PIN_WIDGET_CONTROL,
937 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
938 }
939
940 /* SPDIF TX on/off */
941 if (cfg->dig_outs) {
942 nid = cfg->dig_out_pins[0];
943 snd_hda_codec_write(codec, nid, 0,
944 AC_VERB_SET_PIN_WIDGET_CONTROL,
945 spdif_present ? PIN_OUT : 0);
946
947 }
948 /* Update board GPIOs if neccessary ... */
949 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200950}
951
Tim Howe56487c22011-07-22 16:41:00 -0500952/*
953 * Auto-input redirect for CS421x
954 * Switch max 3 inputs of a single ADC (nid 3)
955*/
956
Takashi Iwaie5f14242009-07-01 18:11:44 +0200957static void cs_automic(struct hda_codec *codec)
958{
959 struct cs_spec *spec = codec->spec;
960 struct auto_pin_cfg *cfg = &spec->autocfg;
961 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800962 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500963
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200964 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800965 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500966
967 /* specific to CS421x, single ADC */
968 if (spec->vendor_nid == CS421X_VENDOR_NID) {
969 if (present) {
970 spec->last_input = spec->cur_input;
971 spec->cur_input = spec->automic_idx;
972 } else {
973 spec->cur_input = spec->last_input;
974 }
975
976 snd_hda_codec_write_cache(codec, spec->cur_adc, 0,
977 AC_VERB_SET_CONNECT_SEL,
978 spec->adc_idx[spec->cur_input]);
979 } else {
980 if (present)
981 change_cur_input(codec, spec->automic_idx, 0);
982 else
983 change_cur_input(codec, !spec->automic_idx, 0);
984 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200985}
986
987/*
988 */
989
990static void init_output(struct hda_codec *codec)
991{
992 struct cs_spec *spec = codec->spec;
993 struct auto_pin_cfg *cfg = &spec->autocfg;
994 int i;
995
996 /* mute first */
997 for (i = 0; i < spec->multiout.num_dacs; i++)
998 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
999 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1000 if (spec->multiout.hp_nid)
1001 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1002 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1003 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1004 if (!spec->multiout.extra_out_nid[i])
1005 break;
1006 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1007 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1008 }
1009
1010 /* set appropriate pin controls */
1011 for (i = 0; i < cfg->line_outs; i++)
1012 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1013 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001014 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001015 for (i = 0; i < cfg->hp_outs; i++) {
1016 hda_nid_t nid = cfg->hp_pins[i];
1017 snd_hda_codec_write(codec, nid, 0,
1018 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1019 if (!cfg->speaker_outs)
1020 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001021 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001022 snd_hda_codec_write(codec, nid, 0,
1023 AC_VERB_SET_UNSOLICITED_ENABLE,
1024 AC_USRSP_EN | HP_EVENT);
1025 spec->hp_detect = 1;
1026 }
1027 }
Tim Howe56487c22011-07-22 16:41:00 -05001028
1029 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001030 for (i = 0; i < cfg->speaker_outs; i++)
1031 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1032 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001033
1034 /* SPDIF is enabled on presence detect for CS421x */
1035 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001036 cs_automute(codec);
1037}
1038
1039static void init_input(struct hda_codec *codec)
1040{
1041 struct cs_spec *spec = codec->spec;
1042 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001043 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001044 int i;
1045
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001046 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001047 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001048 hda_nid_t pin = cfg->inputs[i].pin;
1049 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001050 continue;
1051 /* set appropriate pin control and mute first */
1052 ctl = PIN_IN;
Takashi Iwai86e29592010-09-09 14:50:17 +02001053 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001054 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1055 caps >>= AC_PINCAP_VREF_SHIFT;
1056 if (caps & AC_PINCAP_VREF_80)
1057 ctl = PIN_VREF80;
1058 }
1059 snd_hda_codec_write(codec, pin, 0,
1060 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1061 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1062 AC_VERB_SET_AMP_GAIN_MUTE,
1063 AMP_IN_MUTE(spec->adc_idx[i]));
1064 if (spec->mic_detect && spec->automic_idx == i)
1065 snd_hda_codec_write(codec, pin, 0,
1066 AC_VERB_SET_UNSOLICITED_ENABLE,
1067 AC_USRSP_EN | MIC_EVENT);
1068 }
Tim Howe56487c22011-07-22 16:41:00 -05001069 /* specific to CS421x */
1070 if (spec->vendor_nid == CS421X_VENDOR_NID) {
1071 if (spec->mic_detect)
1072 cs_automic(codec);
1073 else {
1074 spec->cur_adc = spec->adc_nid[spec->cur_input];
1075 snd_hda_codec_write(codec, spec->cur_adc, 0,
1076 AC_VERB_SET_CONNECT_SEL,
1077 spec->adc_idx[spec->cur_input]);
1078 }
1079 } else {
1080 change_cur_input(codec, spec->cur_input, 1);
1081 if (spec->mic_detect)
1082 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001083
Tim Howe56487c22011-07-22 16:41:00 -05001084 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1085 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1086 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1087 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1088 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1089 * No effect if SPDIF_OUT2 is
1090 * selected in IDX_SPDIF_CTL.
1091 */
1092 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1093 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001094}
1095
Takashi Iwaic42d4782011-05-02 11:36:09 +02001096static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001097 {0x11, AC_VERB_SET_PROC_STATE, 1},
1098 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1099 {0x11, AC_VERB_SET_PROC_COEF,
1100 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1101 | 0x0040 /* Mute DACs on FIFO error */
1102 | 0x1000 /* Enable DACs High Pass Filter */
1103 | 0x0400 /* Disable Coefficient Auto increment */
1104 )},
1105 /* Beep */
1106 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1107 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1108
1109 {} /* terminator */
1110};
1111
Brian Austina769cbc2010-09-07 14:36:22 -05001112/* Errata: CS4207 rev C0/C1/C2 Silicon
1113 *
1114 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1115 *
1116 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1117 * may be excessive (up to an additional 200 μA), which is most easily
1118 * observed while the part is being held in reset (RESET# active low).
1119 *
1120 * Root Cause: At initial powerup of the device, the logic that drives
1121 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1122 * initialized.
1123 * Certain random patterns will cause a steady leakage current in those
1124 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1125 *
1126 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1127 * blocks, which will alleviate the issue.
1128 */
1129
Takashi Iwaic42d4782011-05-02 11:36:09 +02001130static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001131 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1132 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1133
1134 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1135 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1136 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1137 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1138 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1139 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1140
1141 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1142 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1143
1144 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1145 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1146 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1147 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1148 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1149 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1150 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1151
Takashi Iwai38c07642011-03-03 14:54:19 +01001152#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001153 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1154 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1155 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001156#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001157
1158 {} /* terminator */
1159};
1160
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001161/* SPDIF setup */
1162static void init_digital(struct hda_codec *codec)
1163{
1164 unsigned int coef;
1165
1166 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1167 coef |= 0x0008; /* Replace with mute on error */
1168 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1169 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1170 * SPDIF_OUT2 is shared with GPIO1 and
1171 * DMIC_SDA2.
1172 */
1173 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001174}
1175
1176static int cs_init(struct hda_codec *codec)
1177{
1178 struct cs_spec *spec = codec->spec;
1179
Brian Austina769cbc2010-09-07 14:36:22 -05001180 /* init_verb sequence for C0/C1/C2 errata*/
1181 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1182
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001183 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001184
1185 if (spec->gpio_mask) {
1186 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1187 spec->gpio_mask);
1188 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1189 spec->gpio_dir);
1190 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1191 spec->gpio_data);
1192 }
1193
Takashi Iwaie5f14242009-07-01 18:11:44 +02001194 init_output(codec);
1195 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001196 init_digital(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001197 return 0;
1198}
1199
1200static int cs_build_controls(struct hda_codec *codec)
1201{
Takashi Iwaie5f14242009-07-01 18:11:44 +02001202 int err;
1203
1204 err = build_output(codec);
1205 if (err < 0)
1206 return err;
1207 err = build_input(codec);
1208 if (err < 0)
1209 return err;
1210 err = build_digital_output(codec);
1211 if (err < 0)
1212 return err;
1213 err = build_digital_input(codec);
1214 if (err < 0)
1215 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001216 return cs_init(codec);
1217}
1218
1219static void cs_free(struct hda_codec *codec)
1220{
1221 struct cs_spec *spec = codec->spec;
1222 kfree(spec->capture_bind[0]);
1223 kfree(spec->capture_bind[1]);
1224 kfree(codec->spec);
1225}
1226
1227static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1228{
1229 switch ((res >> 26) & 0x7f) {
1230 case HP_EVENT:
1231 cs_automute(codec);
1232 break;
1233 case MIC_EVENT:
1234 cs_automic(codec);
1235 break;
1236 }
1237}
1238
Takashi Iwaic42d4782011-05-02 11:36:09 +02001239static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001240 .build_controls = cs_build_controls,
1241 .build_pcms = cs_build_pcms,
1242 .init = cs_init,
1243 .free = cs_free,
1244 .unsol_event = cs_unsol_event,
1245};
1246
1247static int cs_parse_auto_config(struct hda_codec *codec)
1248{
1249 struct cs_spec *spec = codec->spec;
1250 int err;
1251
1252 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1253 if (err < 0)
1254 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001255
1256 err = parse_output(codec);
1257 if (err < 0)
1258 return err;
1259 err = parse_input(codec);
1260 if (err < 0)
1261 return err;
1262 err = parse_digital_output(codec);
1263 if (err < 0)
1264 return err;
1265 err = parse_digital_input(codec);
1266 if (err < 0)
1267 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001268 return 0;
1269}
1270
Takashi Iwaiea734962011-01-17 11:29:34 +01001271static const char * const cs420x_models[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001272 [CS420X_MBP53] = "mbp53",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001273 [CS420X_MBP55] = "mbp55",
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001274 [CS420X_IMAC27] = "imac27",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001275 [CS420X_AUTO] = "auto",
1276};
1277
1278
Takashi Iwaic42d4782011-05-02 11:36:09 +02001279static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001280 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001281 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001282 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001283 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001284 SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001285 {} /* terminator */
1286};
1287
1288struct cs_pincfg {
1289 hda_nid_t nid;
1290 u32 val;
1291};
1292
Takashi Iwaic42d4782011-05-02 11:36:09 +02001293static const struct cs_pincfg mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001294 { 0x09, 0x012b4050 },
1295 { 0x0a, 0x90100141 },
1296 { 0x0b, 0x90100140 },
1297 { 0x0c, 0x018b3020 },
1298 { 0x0d, 0x90a00110 },
1299 { 0x0e, 0x400000f0 },
1300 { 0x0f, 0x01cbe030 },
1301 { 0x10, 0x014be060 },
1302 { 0x12, 0x400000f0 },
1303 { 0x15, 0x400000f0 },
1304 {} /* terminator */
1305};
1306
Takashi Iwaic42d4782011-05-02 11:36:09 +02001307static const struct cs_pincfg mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001308 { 0x09, 0x012b4030 },
1309 { 0x0a, 0x90100121 },
1310 { 0x0b, 0x90100120 },
1311 { 0x0c, 0x400000f0 },
1312 { 0x0d, 0x90a00110 },
1313 { 0x0e, 0x400000f0 },
1314 { 0x0f, 0x400000f0 },
1315 { 0x10, 0x014be040 },
1316 { 0x12, 0x400000f0 },
1317 { 0x15, 0x400000f0 },
1318 {} /* terminator */
1319};
1320
Takashi Iwaic42d4782011-05-02 11:36:09 +02001321static const struct cs_pincfg imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001322 { 0x09, 0x012b4050 },
1323 { 0x0a, 0x90100140 },
1324 { 0x0b, 0x90100142 },
1325 { 0x0c, 0x018b3020 },
1326 { 0x0d, 0x90a00110 },
1327 { 0x0e, 0x400000f0 },
1328 { 0x0f, 0x01cbe030 },
1329 { 0x10, 0x014be060 },
1330 { 0x12, 0x01ab9070 },
1331 { 0x15, 0x400000f0 },
1332 {} /* terminator */
1333};
1334
Takashi Iwaic42d4782011-05-02 11:36:09 +02001335static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001336 [CS420X_MBP53] = mbp53_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001337 [CS420X_MBP55] = mbp55_pincfgs,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001338 [CS420X_IMAC27] = imac27_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001339};
1340
Tim Howe56487c22011-07-22 16:41:00 -05001341static void fix_pincfg(struct hda_codec *codec, int model,
1342 const struct cs_pincfg **pin_configs)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001343{
Tim Howe56487c22011-07-22 16:41:00 -05001344 const struct cs_pincfg *cfg = pin_configs[model];
Takashi Iwaia6bae202009-07-06 15:15:22 +02001345 if (!cfg)
1346 return;
1347 for (; cfg->nid; cfg++)
1348 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1349}
1350
Takashi Iwaie5f14242009-07-01 18:11:44 +02001351static int patch_cs420x(struct hda_codec *codec)
1352{
1353 struct cs_spec *spec;
1354 int err;
1355
1356 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1357 if (!spec)
1358 return -ENOMEM;
1359 codec->spec = spec;
1360
Tim Howe56487c22011-07-22 16:41:00 -05001361 spec->vendor_nid = CS420X_VENDOR_NID;
1362
Takashi Iwaia6bae202009-07-06 15:15:22 +02001363 spec->board_config =
1364 snd_hda_check_board_config(codec, CS420X_MODELS,
1365 cs420x_models, cs420x_cfg_tbl);
1366 if (spec->board_config >= 0)
Tim Howe56487c22011-07-22 16:41:00 -05001367 fix_pincfg(codec, spec->board_config, cs_pincfgs);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001368
Takashi Iwaied208252009-07-07 09:04:26 +02001369 switch (spec->board_config) {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001370 case CS420X_IMAC27:
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001371 case CS420X_MBP53:
Takashi Iwaied208252009-07-07 09:04:26 +02001372 case CS420X_MBP55:
Stelian Pop3a385162009-07-30 14:44:27 +02001373 /* GPIO1 = headphones */
1374 /* GPIO3 = speakers */
1375 spec->gpio_mask = 0x0a;
1376 spec->gpio_dir = 0x0a;
Takashi Iwaied208252009-07-07 09:04:26 +02001377 break;
1378 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001379
Takashi Iwaied208252009-07-07 09:04:26 +02001380 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001381 if (err < 0)
1382 goto error;
1383
Takashi Iwaie5f14242009-07-01 18:11:44 +02001384 codec->patch_ops = cs_patch_ops;
1385
1386 return 0;
1387
1388 error:
1389 kfree(codec->spec);
1390 codec->spec = NULL;
1391 return err;
1392}
1393
Tim Howe56487c22011-07-22 16:41:00 -05001394/*
1395 * Cirrus Logic CS4210
1396 *
1397 * 1 DAC => HP(sense) / Speakers,
1398 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1399 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1400*/
1401
1402/* CS4210 board names */
1403static const char *cs421x_models[CS421X_MODELS] = {
1404 [CS421X_CDB4210] = "cdb4210",
1405};
1406
1407static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1408 /* Test Intel board + CDB2410 */
1409 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1410 {} /* terminator */
1411};
1412
1413/* CS4210 board pinconfigs */
1414/* Default CS4210 (CDB4210)*/
1415static const struct cs_pincfg cdb4210_pincfgs[] = {
1416 { 0x05, 0x0321401f },
1417 { 0x06, 0x90170010 },
1418 { 0x07, 0x03813031 },
1419 { 0x08, 0xb7a70037 },
1420 { 0x09, 0xb7a6003e },
1421 { 0x0a, 0x034510f0 },
1422 {} /* terminator */
1423};
1424
1425static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1426 [CS421X_CDB4210] = cdb4210_pincfgs,
1427};
1428
1429static const struct hda_verb cs421x_coef_init_verbs[] = {
1430 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1431 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1432 /*
1433 Disable Coefficient Index Auto-Increment(DAI)=1,
1434 PDREF=0
1435 */
1436 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1437
1438 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1439 /* ADC SZCMode = Digital Soft Ramp */
1440 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1441
1442 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1443 {0x0B, AC_VERB_SET_PROC_COEF,
1444 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1445 | 0x0004 /* Mute DAC on FIFO error */
1446 | 0x0008 /* Enable DAC High Pass Filter */
1447 )},
1448 {} /* terminator */
1449};
1450
1451/* Errata: CS4210 rev A1 Silicon
1452 *
1453 * http://www.cirrus.com/en/pubs/errata/
1454 *
1455 * Description:
1456 * 1. Performance degredation is present in the ADC.
1457 * 2. Speaker output is not completely muted upon HP detect.
1458 * 3. Noise is present when clipping occurs on the amplified
1459 * speaker outputs.
1460 *
1461 * Workaround:
1462 * The following verb sequence written to the registers during
1463 * initialization will correct the issues listed above.
1464 */
1465
1466static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1467 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1468
1469 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1470 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1471
1472 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1473 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1474
1475 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1476 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1477
1478 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1479 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1480
1481 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1482 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1483
1484 {} /* terminator */
1485};
1486
1487/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1488static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1489
1490static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1491 struct snd_ctl_elem_info *uinfo)
1492{
1493 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1494 uinfo->count = 1;
1495 uinfo->value.integer.min = 0;
1496 uinfo->value.integer.max = 3;
1497 return 0;
1498}
1499
1500static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_value *ucontrol)
1502{
1503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1504
1505 ucontrol->value.integer.value[0] =
1506 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1507 return 0;
1508}
1509
1510static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1511 struct snd_ctl_elem_value *ucontrol)
1512{
1513 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1514
1515 unsigned int vol = ucontrol->value.integer.value[0];
1516 unsigned int coef =
1517 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1518 unsigned int original_coef = coef;
1519
1520 coef &= ~0x0003;
1521 coef |= (vol & 0x0003);
1522 if (original_coef == coef)
1523 return 0;
1524 else {
1525 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1526 return 1;
1527 }
1528}
1529
1530static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1531
1532 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1533 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1534 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1535 .name = "Speaker Boost Playback Volume",
1536 .info = cs421x_boost_vol_info,
1537 .get = cs421x_boost_vol_get,
1538 .put = cs421x_boost_vol_put,
1539 .tlv = { .p = cs421x_speaker_boost_db_scale },
1540};
1541
1542static void cs421x_pinmux_init(struct hda_codec *codec)
1543{
1544 struct cs_spec *spec = codec->spec;
1545 unsigned int def_conf, coef;
1546
1547 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1548 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1549
1550 if (spec->gpio_mask)
1551 coef |= 0x0008; /* B1,B2 are GPIOs */
1552 else
1553 coef &= ~0x0008;
1554
1555 if (spec->sense_b)
1556 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1557 else
1558 coef &= ~0x0010;
1559
1560 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1561
1562 if ((spec->gpio_mask || spec->sense_b) &&
1563 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1564
1565 /*
1566 GPIO or SENSE_B forced - disconnect the DMIC pin.
1567 */
1568 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1569 def_conf &= ~AC_DEFCFG_PORT_CONN;
1570 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1571 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1572 }
1573}
1574
1575static void init_cs421x_digital(struct hda_codec *codec)
1576{
1577 struct cs_spec *spec = codec->spec;
1578 struct auto_pin_cfg *cfg = &spec->autocfg;
1579 int i;
1580
1581
1582 for (i = 0; i < cfg->dig_outs; i++) {
1583 hda_nid_t nid = cfg->dig_out_pins[i];
1584 if (!cfg->speaker_outs)
1585 continue;
1586 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
1587
1588 snd_hda_codec_write(codec, nid, 0,
1589 AC_VERB_SET_UNSOLICITED_ENABLE,
1590 AC_USRSP_EN | SPDIF_EVENT);
1591 spec->spdif_detect = 1;
1592 }
1593 }
1594}
1595
1596static int cs421x_init(struct hda_codec *codec)
1597{
1598 struct cs_spec *spec = codec->spec;
1599
1600 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1601 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1602
1603 cs421x_pinmux_init(codec);
1604
1605 if (spec->gpio_mask) {
1606 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1607 spec->gpio_mask);
1608 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1609 spec->gpio_dir);
1610 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1611 spec->gpio_data);
1612 }
1613
1614 init_output(codec);
1615 init_input(codec);
1616 init_cs421x_digital(codec);
1617
1618 return 0;
1619}
1620
1621/*
1622 * CS4210 Input MUX (1 ADC)
1623 */
1624static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1625 struct snd_ctl_elem_info *uinfo)
1626{
1627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1628 struct cs_spec *spec = codec->spec;
1629
1630 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1631}
1632
1633static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1634 struct snd_ctl_elem_value *ucontrol)
1635{
1636 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1637 struct cs_spec *spec = codec->spec;
1638
1639 ucontrol->value.enumerated.item[0] = spec->cur_input;
1640 return 0;
1641}
1642
1643static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1644 struct snd_ctl_elem_value *ucontrol)
1645{
1646 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1647 struct cs_spec *spec = codec->spec;
1648
1649 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1650 spec->adc_nid[0], &spec->cur_input);
1651
1652}
1653
1654static struct snd_kcontrol_new cs421x_capture_source = {
1655
1656 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1657 .name = "Capture Source",
1658 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1659 .info = cs421x_mux_enum_info,
1660 .get = cs421x_mux_enum_get,
1661 .put = cs421x_mux_enum_put,
1662};
1663
1664static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1665{
1666 struct cs_spec *spec = codec->spec;
1667 struct auto_pin_cfg *cfg = &spec->autocfg;
1668 const struct hda_input_mux *imux = &spec->input_mux;
1669 hda_nid_t pin = cfg->inputs[item].pin;
1670 struct snd_kcontrol *kctl;
1671 u32 caps;
1672
1673 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1674 return 0;
1675
1676 caps = query_amp_caps(codec, pin, HDA_INPUT);
1677 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1678 if (caps <= 1)
1679 return 0;
1680
1681 return add_volume(codec, imux->items[item].label, 0,
1682 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1683}
1684
1685/* add a (input-boost) volume control to the given input pin */
1686static int build_cs421x_input(struct hda_codec *codec)
1687{
1688 struct cs_spec *spec = codec->spec;
1689 struct auto_pin_cfg *cfg = &spec->autocfg;
1690 struct hda_input_mux *imux = &spec->input_mux;
1691 int i, err, type_idx;
1692 const char *label;
1693
1694 if (!spec->num_inputs)
1695 return 0;
1696
1697 /* make bind-capture */
1698 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1699 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1700 for (i = 0; i < 2; i++) {
1701 struct snd_kcontrol *kctl;
1702 int n;
1703 if (!spec->capture_bind[i])
1704 return -ENOMEM;
1705 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1706 if (!kctl)
1707 return -ENOMEM;
1708 kctl->private_value = (long)spec->capture_bind[i];
1709 err = snd_hda_ctl_add(codec, 0, kctl);
1710 if (err < 0)
1711 return err;
1712 for (n = 0; n < AUTO_PIN_LAST; n++) {
1713 if (!spec->adc_nid[n])
1714 continue;
1715 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1716 if (err < 0)
1717 return err;
1718 }
1719 }
1720
1721 /* Add Input MUX Items + Capture Volume/Switch */
1722 for (i = 0; i < spec->num_inputs; i++) {
1723 label = hda_get_autocfg_input_label(codec, cfg, i);
1724 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1725
1726 err = cs421x_add_input_volume_control(codec, i);
1727 if (err < 0)
1728 return err;
1729 }
1730
1731 /*
1732 Add 'Capture Source' Switch if
1733 * 2 inputs and no mic detec
1734 * 3 inputs
1735 */
1736 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1737 (spec->num_inputs == 3)) {
1738
1739 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1740 snd_ctl_new1(&cs421x_capture_source, codec));
1741 if (err < 0)
1742 return err;
1743 }
1744
1745 return 0;
1746}
1747
1748/* Single DAC (Mute/Gain) */
1749static int build_cs421x_output(struct hda_codec *codec)
1750{
1751 hda_nid_t dac = CS4210_DAC_NID;
1752 struct cs_spec *spec = codec->spec;
1753 struct auto_pin_cfg *cfg = &spec->autocfg;
1754 struct snd_kcontrol *kctl;
1755 int err;
1756 char *name = "HP/Speakers";
1757
1758 fix_volume_caps(codec, dac);
1759 if (!spec->vmaster_sw) {
1760 err = add_vmaster(codec, dac);
1761 if (err < 0)
1762 return err;
1763 }
1764
1765 err = add_mute(codec, name, 0,
1766 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1767 if (err < 0)
1768 return err;
1769 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
1770 if (err < 0)
1771 return err;
1772
1773 err = add_volume(codec, name, 0,
1774 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1775 if (err < 0)
1776 return err;
1777 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
1778 if (err < 0)
1779 return err;
1780
1781 if (cfg->speaker_outs) {
1782 err = snd_hda_ctl_add(codec, 0,
1783 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1784 if (err < 0)
1785 return err;
1786 }
1787 return err;
1788}
1789
1790static int cs421x_build_controls(struct hda_codec *codec)
1791{
1792 int err;
1793
1794 err = build_cs421x_output(codec);
1795 if (err < 0)
1796 return err;
1797 err = build_cs421x_input(codec);
1798 if (err < 0)
1799 return err;
1800 err = build_digital_output(codec);
1801 if (err < 0)
1802 return err;
1803 return cs421x_init(codec);
1804}
1805
1806static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1807{
1808 switch ((res >> 26) & 0x3f) {
1809 case HP_EVENT:
1810 case SPDIF_EVENT:
1811 cs_automute(codec);
1812 break;
1813
1814 case MIC_EVENT:
1815 cs_automic(codec);
1816 break;
1817 }
1818}
1819
1820static int parse_cs421x_input(struct hda_codec *codec)
1821{
1822 struct cs_spec *spec = codec->spec;
1823 struct auto_pin_cfg *cfg = &spec->autocfg;
1824 int i;
1825
1826 for (i = 0; i < cfg->num_inputs; i++) {
1827 hda_nid_t pin = cfg->inputs[i].pin;
1828 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1829 spec->cur_input = spec->last_input = i;
1830 spec->num_inputs++;
1831
1832 /* check whether the automatic mic switch is available */
1833 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1834 spec->mic_detect = 1;
1835 spec->automic_idx = i;
1836 }
1837 }
1838 return 0;
1839}
1840
1841static int cs421x_parse_auto_config(struct hda_codec *codec)
1842{
1843 struct cs_spec *spec = codec->spec;
1844 int err;
1845
1846 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1847 if (err < 0)
1848 return err;
1849 err = parse_output(codec);
1850 if (err < 0)
1851 return err;
1852 err = parse_cs421x_input(codec);
1853 if (err < 0)
1854 return err;
1855 err = parse_digital_output(codec);
1856 if (err < 0)
1857 return err;
1858 return 0;
1859}
1860
1861#ifdef CONFIG_PM
1862/*
1863 Manage PDREF, when transitioning to D3hot
1864 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1865*/
1866static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1867{
1868 unsigned int coef;
1869
1870 snd_hda_shutup_pins(codec);
1871
1872 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1873 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1874 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1875 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1876
1877 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1878 coef |= 0x0004; /* PDREF */
1879 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1880
1881 return 0;
1882}
1883#endif
1884
1885static struct hda_codec_ops cs4210_patch_ops = {
1886 .build_controls = cs421x_build_controls,
1887 .build_pcms = cs_build_pcms,
1888 .init = cs421x_init,
1889 .free = cs_free,
1890 .unsol_event = cs421x_unsol_event,
1891#ifdef CONFIG_PM
1892 .suspend = cs421x_suspend,
1893#endif
1894};
1895
1896static int patch_cs421x(struct hda_codec *codec)
1897{
1898 struct cs_spec *spec;
1899 int err;
1900
1901 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1902 if (!spec)
1903 return -ENOMEM;
1904 codec->spec = spec;
1905
1906 spec->vendor_nid = CS421X_VENDOR_NID;
1907
1908 spec->board_config =
1909 snd_hda_check_board_config(codec, CS421X_MODELS,
1910 cs421x_models, cs421x_cfg_tbl);
1911 if (spec->board_config >= 0)
1912 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1913 /*
1914 Setup GPIO/SENSE for each board (if used)
1915 */
1916 switch (spec->board_config) {
1917 case CS421X_CDB4210:
1918 snd_printd("CS4210 board: %s\n",
1919 cs421x_models[spec->board_config]);
1920/* spec->gpio_mask = 3;
1921 spec->gpio_dir = 3;
1922 spec->gpio_data = 3;
1923*/
1924 spec->sense_b = 1;
1925
1926 break;
1927 }
1928
1929 /*
1930 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1931 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1932 is disabled.
1933 */
1934 cs421x_pinmux_init(codec);
1935
1936 err = cs421x_parse_auto_config(codec);
1937 if (err < 0)
1938 goto error;
1939
1940 codec->patch_ops = cs4210_patch_ops;
1941
1942 return 0;
1943
1944 error:
1945 kfree(codec->spec);
1946 codec->spec = NULL;
1947 return err;
1948}
1949
Takashi Iwaie5f14242009-07-01 18:11:44 +02001950
1951/*
1952 * patch entries
1953 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02001954static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001955 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
1956 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
Tim Howe56487c22011-07-22 16:41:00 -05001957 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
Takashi Iwaie5f14242009-07-01 18:11:44 +02001958 {} /* terminator */
1959};
1960
1961MODULE_ALIAS("snd-hda-codec-id:10134206");
1962MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05001963MODULE_ALIAS("snd-hda-codec-id:10134210");
Takashi Iwaie5f14242009-07-01 18:11:44 +02001964
1965MODULE_LICENSE("GPL");
1966MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1967
1968static struct hda_codec_preset_list cirrus_list = {
1969 .preset = snd_hda_preset_cirrus,
1970 .owner = THIS_MODULE,
1971};
1972
1973static int __init patch_cirrus_init(void)
1974{
1975 return snd_hda_add_codec_preset(&cirrus_list);
1976}
1977
1978static void __exit patch_cirrus_exit(void)
1979{
1980 snd_hda_delete_codec_preset(&cirrus_list);
1981}
1982
1983module_init(patch_cirrus_init)
1984module_exit(patch_cirrus_exit)