blob: 036056c42c1376c601fe8013518cf90d59c48123 [file] [log] [blame]
Takashi Iwaie5f14242009-07-01 18:11:44 +02001/*
2 * HD audio interface patch for Cirrus Logic CS420x chip
3 *
4 * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
5 *
6 * This driver is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This driver is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/init.h>
22#include <linux/delay.h>
23#include <linux/slab.h>
24#include <linux/pci.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040025#include <linux/module.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020026#include <sound/core.h>
27#include "hda_codec.h"
28#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020029#include "hda_jack.h"
Tim Howe56487c22011-07-22 16:41:00 -050030#include <sound/tlv.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020031
32/*
33 */
34
35struct cs_spec {
Takashi Iwaia6bae202009-07-06 15:15:22 +020036 int board_config;
Takashi Iwaie5f14242009-07-01 18:11:44 +020037 struct auto_pin_cfg autocfg;
38 struct hda_multi_out multiout;
39 struct snd_kcontrol *vmaster_sw;
40 struct snd_kcontrol *vmaster_vol;
41
42 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
43 hda_nid_t slave_dig_outs[2];
44
45 unsigned int input_idx[AUTO_PIN_LAST];
46 unsigned int capsrc_idx[AUTO_PIN_LAST];
47 hda_nid_t adc_nid[AUTO_PIN_LAST];
48 unsigned int adc_idx[AUTO_PIN_LAST];
49 unsigned int num_inputs;
50 unsigned int cur_input;
51 unsigned int automic_idx;
52 hda_nid_t cur_adc;
53 unsigned int cur_adc_stream_tag;
54 unsigned int cur_adc_format;
55 hda_nid_t dig_in;
56
Takashi Iwaic42d4782011-05-02 11:36:09 +020057 const struct hda_bind_ctls *capture_bind[2];
Takashi Iwaie5f14242009-07-01 18:11:44 +020058
Takashi Iwaied208252009-07-07 09:04:26 +020059 unsigned int gpio_mask;
60 unsigned int gpio_dir;
61 unsigned int gpio_data;
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010062 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
63 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
Takashi Iwaied208252009-07-07 09:04:26 +020064
Takashi Iwaie5f14242009-07-01 18:11:44 +020065 struct hda_pcm pcm_rec[2]; /* PCM information */
66
67 unsigned int hp_detect:1;
68 unsigned int mic_detect:1;
Tim Howe56487c22011-07-22 16:41:00 -050069 /* CS421x */
70 unsigned int spdif_detect:1;
71 unsigned int sense_b:1;
72 hda_nid_t vendor_nid;
73 struct hda_input_mux input_mux;
74 unsigned int last_input;
Takashi Iwaie5f14242009-07-01 18:11:44 +020075};
76
Tim Howe56487c22011-07-22 16:41:00 -050077/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020078enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040079 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020080 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010081 CS420X_IMAC27,
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010082 CS420X_APPLE,
Takashi Iwaia6bae202009-07-06 15:15:22 +020083 CS420X_AUTO,
84 CS420X_MODELS
85};
86
Tim Howe56487c22011-07-22 16:41:00 -050087/* CS421x boards */
88enum {
89 CS421X_CDB4210,
90 CS421X_MODELS
91};
92
Takashi Iwai40c20fa2009-07-06 13:00:57 +020093/* Vendor-specific processing widget */
94#define CS420X_VENDOR_NID 0x11
95#define CS_DIG_OUT1_PIN_NID 0x10
96#define CS_DIG_OUT2_PIN_NID 0x15
97#define CS_DMIC1_PIN_NID 0x12
98#define CS_DMIC2_PIN_NID 0x0e
99
100/* coef indices */
101#define IDX_SPDIF_STAT 0x0000
102#define IDX_SPDIF_CTL 0x0001
103#define IDX_ADC_CFG 0x0002
104/* SZC bitmask, 4 modes below:
105 * 0 = immediate,
106 * 1 = digital immediate, analog zero-cross
107 * 2 = digtail & analog soft-ramp
108 * 3 = digital soft-ramp, analog zero-cross
109 */
110#define CS_COEF_ADC_SZC_MASK (3 << 0)
111#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
112#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
113/* PGA mode: 0 = differential, 1 = signle-ended */
114#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
115#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
116#define IDX_DAC_CFG 0x0003
117/* SZC bitmask, 4 modes below:
118 * 0 = Immediate
119 * 1 = zero-cross
120 * 2 = soft-ramp
121 * 3 = soft-ramp on zero-cross
122 */
123#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
124#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
125#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
126
127#define IDX_BEEP_CFG 0x0004
128/* 0x0008 - test reg key */
129/* 0x0009 - 0x0014 -> 12 test regs */
130/* 0x0015 - visibility reg */
131
Tim Howe56487c22011-07-22 16:41:00 -0500132/*
133 * Cirrus Logic CS4210
134 *
135 * 1 DAC => HP(sense) / Speakers,
136 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
137 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
138*/
139#define CS4210_DAC_NID 0x02
140#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100141#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500142#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
143#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
144
145#define CS421X_IDX_DEV_CFG 0x01
146#define CS421X_IDX_ADC_CFG 0x02
147#define CS421X_IDX_DAC_CFG 0x03
148#define CS421X_IDX_SPK_CTL 0x04
149
150#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200151
David Henningsson5660ffd2012-01-02 12:40:17 +0100152/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
153#define CS4213_VENDOR_NID 0x09
154
155
Takashi Iwai277a57c2009-07-08 12:42:08 +0200156static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200157{
Tim Howe56487c22011-07-22 16:41:00 -0500158 struct cs_spec *spec = codec->spec;
159 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200160 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500161 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200162 AC_VERB_GET_PROC_COEF, 0);
163}
164
Takashi Iwai277a57c2009-07-08 12:42:08 +0200165static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
166 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200167{
Tim Howe56487c22011-07-22 16:41:00 -0500168 struct cs_spec *spec = codec->spec;
169 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200170 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500171 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200172 AC_VERB_SET_PROC_COEF, coef);
173}
174
175
Takashi Iwaie5f14242009-07-01 18:11:44 +0200176#define HP_EVENT 1
177#define MIC_EVENT 2
178
179/*
180 * PCM callbacks
181 */
182static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
183 struct hda_codec *codec,
184 struct snd_pcm_substream *substream)
185{
186 struct cs_spec *spec = codec->spec;
187 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
188 hinfo);
189}
190
191static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
192 struct hda_codec *codec,
193 unsigned int stream_tag,
194 unsigned int format,
195 struct snd_pcm_substream *substream)
196{
197 struct cs_spec *spec = codec->spec;
198 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
199 stream_tag, format, substream);
200}
201
202static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
203 struct hda_codec *codec,
204 struct snd_pcm_substream *substream)
205{
206 struct cs_spec *spec = codec->spec;
207 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
208}
209
210/*
211 * Digital out
212 */
213static int cs_dig_playback_pcm_open(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_open(codec, &spec->multiout);
219}
220
221static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
222 struct hda_codec *codec,
223 struct snd_pcm_substream *substream)
224{
225 struct cs_spec *spec = codec->spec;
226 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
227}
228
229static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
230 struct hda_codec *codec,
231 unsigned int stream_tag,
232 unsigned int format,
233 struct snd_pcm_substream *substream)
234{
235 struct cs_spec *spec = codec->spec;
236 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
237 format, substream);
238}
239
240static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
241 struct hda_codec *codec,
242 struct snd_pcm_substream *substream)
243{
244 struct cs_spec *spec = codec->spec;
245 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
246}
247
Takashi Iwai05ee7962011-11-16 18:05:11 +0100248static void cs_update_input_select(struct hda_codec *codec)
249{
250 struct cs_spec *spec = codec->spec;
251 if (spec->cur_adc)
252 snd_hda_codec_write(codec, spec->cur_adc, 0,
253 AC_VERB_SET_CONNECT_SEL,
254 spec->adc_idx[spec->cur_input]);
255}
256
Takashi Iwaie5f14242009-07-01 18:11:44 +0200257/*
258 * Analog capture
259 */
260static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
261 struct hda_codec *codec,
262 unsigned int stream_tag,
263 unsigned int format,
264 struct snd_pcm_substream *substream)
265{
266 struct cs_spec *spec = codec->spec;
267 spec->cur_adc = spec->adc_nid[spec->cur_input];
268 spec->cur_adc_stream_tag = stream_tag;
269 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100270 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200271 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
272 return 0;
273}
274
275static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
276 struct hda_codec *codec,
277 struct snd_pcm_substream *substream)
278{
279 struct cs_spec *spec = codec->spec;
280 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
281 spec->cur_adc = 0;
282 return 0;
283}
284
285/*
286 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200287static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200288 .substreams = 1,
289 .channels_min = 2,
290 .channels_max = 2,
291 .ops = {
292 .open = cs_playback_pcm_open,
293 .prepare = cs_playback_pcm_prepare,
294 .cleanup = cs_playback_pcm_cleanup
295 },
296};
297
Takashi Iwaic42d4782011-05-02 11:36:09 +0200298static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200299 .substreams = 1,
300 .channels_min = 2,
301 .channels_max = 2,
302 .ops = {
303 .prepare = cs_capture_pcm_prepare,
304 .cleanup = cs_capture_pcm_cleanup
305 },
306};
307
Takashi Iwaic42d4782011-05-02 11:36:09 +0200308static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200309 .substreams = 1,
310 .channels_min = 2,
311 .channels_max = 2,
312 .ops = {
313 .open = cs_dig_playback_pcm_open,
314 .close = cs_dig_playback_pcm_close,
315 .prepare = cs_dig_playback_pcm_prepare,
316 .cleanup = cs_dig_playback_pcm_cleanup
317 },
318};
319
Takashi Iwaic42d4782011-05-02 11:36:09 +0200320static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200321 .substreams = 1,
322 .channels_min = 2,
323 .channels_max = 2,
324};
325
326static int cs_build_pcms(struct hda_codec *codec)
327{
328 struct cs_spec *spec = codec->spec;
329 struct hda_pcm *info = spec->pcm_rec;
330
331 codec->pcm_info = info;
332 codec->num_pcms = 0;
333
334 info->name = "Cirrus Analog";
335 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
336 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
337 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
338 spec->multiout.max_channels;
339 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200340 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
341 spec->adc_nid[spec->cur_input];
342 codec->num_pcms++;
343
344 if (!spec->multiout.dig_out_nid && !spec->dig_in)
345 return 0;
346
347 info++;
348 info->name = "Cirrus Digital";
349 info->pcm_type = spec->autocfg.dig_out_type[0];
350 if (!info->pcm_type)
351 info->pcm_type = HDA_PCM_TYPE_SPDIF;
352 if (spec->multiout.dig_out_nid) {
353 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
354 cs_pcm_digital_playback;
355 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
356 spec->multiout.dig_out_nid;
357 }
358 if (spec->dig_in) {
359 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
360 cs_pcm_digital_capture;
361 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
362 }
363 codec->num_pcms++;
364
365 return 0;
366}
367
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200368/*
369 * parse codec topology
370 */
371
Takashi Iwaie5f14242009-07-01 18:11:44 +0200372static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
373{
374 hda_nid_t dac;
375 if (!pin)
376 return 0;
377 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
378 return 0;
379 return dac;
380}
381
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200382static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
383{
384 struct cs_spec *spec = codec->spec;
385 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200386 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200387 unsigned int val;
388 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200389 return 0;
390 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200391 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200392}
393
394static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
395 unsigned int *idxp)
396{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200397 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200398 hda_nid_t nid;
399
400 nid = codec->start_nid;
401 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200402 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200403 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200404 if (type != AC_WID_AUD_IN)
405 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200406 idx = snd_hda_get_conn_index(codec, nid, pin, false);
407 if (idx >= 0) {
408 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200409 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200410 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200411 }
412 return 0;
413}
414
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200415static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
416{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200417 unsigned int val;
418 val = snd_hda_codec_get_pincfg(codec, nid);
419 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
420}
421
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200422static int parse_output(struct hda_codec *codec)
423{
424 struct cs_spec *spec = codec->spec;
425 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200426 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200427 hda_nid_t dac;
428
429 for (i = 0; i < cfg->line_outs; i++) {
430 dac = get_dac(codec, cfg->line_out_pins[i]);
431 if (!dac)
432 break;
433 spec->dac_nid[i] = dac;
434 }
435 spec->multiout.num_dacs = i;
436 spec->multiout.dac_nids = spec->dac_nid;
437 spec->multiout.max_channels = i * 2;
438
439 /* add HP and speakers */
440 extra_nids = 0;
441 for (i = 0; i < cfg->hp_outs; i++) {
442 dac = get_dac(codec, cfg->hp_pins[i]);
443 if (!dac)
444 break;
445 if (!i)
446 spec->multiout.hp_nid = dac;
447 else
448 spec->multiout.extra_out_nid[extra_nids++] = dac;
449 }
450 for (i = 0; i < cfg->speaker_outs; i++) {
451 dac = get_dac(codec, cfg->speaker_pins[i]);
452 if (!dac)
453 break;
454 spec->multiout.extra_out_nid[extra_nids++] = dac;
455 }
456
457 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
458 cfg->speaker_outs = cfg->line_outs;
459 memcpy(cfg->speaker_pins, cfg->line_out_pins,
460 sizeof(cfg->speaker_pins));
461 cfg->line_outs = 0;
462 }
463
464 return 0;
465}
466
467static int parse_input(struct hda_codec *codec)
468{
469 struct cs_spec *spec = codec->spec;
470 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200471 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200472
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200473 for (i = 0; i < cfg->num_inputs; i++) {
474 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200475 spec->input_idx[spec->num_inputs] = i;
476 spec->capsrc_idx[i] = spec->num_inputs++;
477 spec->cur_input = i;
478 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
479 }
480 if (!spec->num_inputs)
481 return 0;
482
483 /* check whether the automatic mic switch is available */
484 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200485 cfg->inputs[0].type == AUTO_PIN_MIC &&
486 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200487 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
488 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200489 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200490 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200491 }
492 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200493 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200494 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200495 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200496 }
497 }
498 }
499 return 0;
500}
501
502
503static int parse_digital_output(struct hda_codec *codec)
504{
505 struct cs_spec *spec = codec->spec;
506 struct auto_pin_cfg *cfg = &spec->autocfg;
507 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200508
509 if (!cfg->dig_outs)
510 return 0;
511 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
512 return 0;
513 spec->multiout.dig_out_nid = nid;
514 spec->multiout.share_spdif = 1;
515 if (cfg->dig_outs > 1 &&
516 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
517 spec->slave_dig_outs[0] = nid;
518 codec->slave_dig_outs = spec->slave_dig_outs;
519 }
520 return 0;
521}
522
523static int parse_digital_input(struct hda_codec *codec)
524{
525 struct cs_spec *spec = codec->spec;
526 struct auto_pin_cfg *cfg = &spec->autocfg;
527 int idx;
528
Takashi Iwai60e53882009-07-06 15:01:09 +0200529 if (cfg->dig_in_pin)
530 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
531 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200532}
533
534/*
535 * create mixer controls
536 */
537
Takashi Iwaiea734962011-01-17 11:29:34 +0100538static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200539
540static int add_mute(struct hda_codec *codec, const char *name, int index,
541 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
542{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200543 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200544 struct snd_kcontrol_new knew =
545 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
546 knew.private_value = pval;
547 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
548 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100549 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100550 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200551}
552
553static int add_volume(struct hda_codec *codec, const char *name,
554 int index, unsigned int pval, int dir,
555 struct snd_kcontrol **kctlp)
556{
David Henningsson2e1210b2011-09-14 13:22:54 +0200557 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200558 struct snd_kcontrol_new knew =
559 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
560 knew.private_value = pval;
561 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
562 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100563 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100564 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200565}
566
567static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
568{
569 unsigned int caps;
570
571 /* set the upper-limit for mixer amp to 0dB */
572 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
573 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
574 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
575 << AC_AMPCAP_NUM_STEPS_SHIFT;
576 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
577}
578
579static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
580{
581 struct cs_spec *spec = codec->spec;
582 unsigned int tlv[4];
583 int err;
584
585 spec->vmaster_sw =
586 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100587 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200588 if (err < 0)
589 return err;
590
591 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
592 spec->vmaster_vol =
593 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100594 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200595 if (err < 0)
596 return err;
597 return 0;
598}
599
600static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
601 int num_ctls, int type)
602{
603 struct cs_spec *spec = codec->spec;
604 const char *name;
605 int err, index;
606 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200607 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200608 "Front Speaker", "Surround Speaker", "Bass Speaker"
609 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200610 static const char * const line_outs[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200611 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
612 };
613
614 fix_volume_caps(codec, dac);
615 if (!spec->vmaster_sw) {
616 err = add_vmaster(codec, dac);
617 if (err < 0)
618 return err;
619 }
620
621 index = 0;
622 switch (type) {
623 case AUTO_PIN_HP_OUT:
624 name = "Headphone";
625 index = idx;
626 break;
627 case AUTO_PIN_SPEAKER_OUT:
628 if (num_ctls > 1)
629 name = speakers[idx];
630 else
631 name = "Speaker";
632 break;
633 default:
634 if (num_ctls > 1)
635 name = line_outs[idx];
636 else
637 name = "Line-Out";
638 break;
639 }
640
641 err = add_mute(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_sw, kctl);
646 if (err < 0)
647 return err;
648
649 err = add_volume(codec, name, index,
650 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
651 if (err < 0)
652 return err;
653 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
654 if (err < 0)
655 return err;
656
657 return 0;
658}
659
660static int build_output(struct hda_codec *codec)
661{
662 struct cs_spec *spec = codec->spec;
663 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200664 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200665
666 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200667 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
668 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200669 if (err < 0)
670 return err;
671 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200672 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200673 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
674 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200675 if (err < 0)
676 return err;
677 }
678 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200679 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
680 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200681 if (err < 0)
682 return err;
683 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200684 return 0;
685}
686
687/*
688 */
689
Takashi Iwaic42d4782011-05-02 11:36:09 +0200690static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200691 HDA_BIND_SW("Capture Switch", 0),
692 HDA_BIND_VOL("Capture Volume", 0),
693};
694
Takashi Iwaiea359292009-07-06 12:58:59 +0200695static int change_cur_input(struct hda_codec *codec, unsigned int idx,
696 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200697{
698 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200699
Takashi Iwaiea359292009-07-06 12:58:59 +0200700 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200701 return 0;
702 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
703 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200704 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200705 spec->cur_adc = spec->adc_nid[idx];
706 snd_hda_codec_setup_stream(codec, spec->cur_adc,
707 spec->cur_adc_stream_tag, 0,
708 spec->cur_adc_format);
709 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200710 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100711 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200712 return 1;
713}
714
715static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
716 struct snd_ctl_elem_info *uinfo)
717{
718 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
719 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200720 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200721 unsigned int idx;
722
723 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
724 uinfo->count = 1;
725 uinfo->value.enumerated.items = spec->num_inputs;
726 if (uinfo->value.enumerated.item >= spec->num_inputs)
727 uinfo->value.enumerated.item = spec->num_inputs - 1;
728 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100729 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
730 uinfo->value.enumerated.name,
731 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200732 return 0;
733}
734
735static int cs_capture_source_get(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 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
741 return 0;
742}
743
744static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
745 struct snd_ctl_elem_value *ucontrol)
746{
747 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
748 struct cs_spec *spec = codec->spec;
749 unsigned int idx = ucontrol->value.enumerated.item[0];
750
751 if (idx >= spec->num_inputs)
752 return -EINVAL;
753 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200754 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200755}
756
Takashi Iwaic42d4782011-05-02 11:36:09 +0200757static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200758 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
759 .name = "Capture Source",
760 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
761 .info = cs_capture_source_info,
762 .get = cs_capture_source_get,
763 .put = cs_capture_source_put,
764};
765
Takashi Iwaic42d4782011-05-02 11:36:09 +0200766static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200767 struct hda_ctl_ops *ops)
768{
769 struct cs_spec *spec = codec->spec;
770 struct hda_bind_ctls *bind;
771 int i, n;
772
773 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
774 GFP_KERNEL);
775 if (!bind)
776 return NULL;
777 bind->ops = ops;
778 n = 0;
779 for (i = 0; i < AUTO_PIN_LAST; i++) {
780 if (!spec->adc_nid[i])
781 continue;
782 bind->values[n++] =
783 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
784 spec->adc_idx[i], HDA_INPUT);
785 }
786 return bind;
787}
788
Takashi Iwai6a929342010-10-11 15:16:20 +0200789/* add a (input-boost) volume control to the given input pin */
790static int add_input_volume_control(struct hda_codec *codec,
791 struct auto_pin_cfg *cfg,
792 int item)
793{
794 hda_nid_t pin = cfg->inputs[item].pin;
795 u32 caps;
796 const char *label;
797 struct snd_kcontrol *kctl;
798
799 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
800 return 0;
801 caps = query_amp_caps(codec, pin, HDA_INPUT);
802 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
803 if (caps <= 1)
804 return 0;
805 label = hda_get_autocfg_input_label(codec, cfg, item);
806 return add_volume(codec, label, 0,
807 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
808}
809
Takashi Iwaie5f14242009-07-01 18:11:44 +0200810static int build_input(struct hda_codec *codec)
811{
812 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200813 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200814
Takashi Iwaie5f14242009-07-01 18:11:44 +0200815 if (!spec->num_inputs)
816 return 0;
817
Takashi Iwaie5f14242009-07-01 18:11:44 +0200818 /* make bind-capture */
819 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
820 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
821 for (i = 0; i < 2; i++) {
822 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100823 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200824 if (!spec->capture_bind[i])
825 return -ENOMEM;
826 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
827 if (!kctl)
828 return -ENOMEM;
829 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100830 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200831 if (err < 0)
832 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100833 for (n = 0; n < AUTO_PIN_LAST; n++) {
834 if (!spec->adc_nid[n])
835 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700836 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100837 if (err < 0)
838 return err;
839 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200840 }
841
842 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100843 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200844 snd_ctl_new1(&cs_capture_source, codec));
845 if (err < 0)
846 return err;
847 }
848
Takashi Iwai6a929342010-10-11 15:16:20 +0200849 for (i = 0; i < spec->num_inputs; i++) {
850 err = add_input_volume_control(codec, &spec->autocfg, i);
851 if (err < 0)
852 return err;
853 }
854
Takashi Iwaie5f14242009-07-01 18:11:44 +0200855 return 0;
856}
857
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200858/*
859 */
860
Takashi Iwaie5f14242009-07-01 18:11:44 +0200861static int build_digital_output(struct hda_codec *codec)
862{
863 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200864 int err;
865
Takashi Iwai63b24132009-07-09 11:45:59 +0200866 if (!spec->multiout.dig_out_nid)
867 return 0;
868
Stephen Warren74b654c2011-06-01 11:14:18 -0600869 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
870 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200871 if (err < 0)
872 return err;
873 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
874 if (err < 0)
875 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200876 return 0;
877}
878
879static int build_digital_input(struct hda_codec *codec)
880{
881 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200882 if (spec->dig_in)
883 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
884 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200885}
886
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200887/*
888 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500889 * CS421x auto-output redirecting
890 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200891 */
892
Takashi Iwaie5f14242009-07-01 18:11:44 +0200893static void cs_automute(struct hda_codec *codec)
894{
895 struct cs_spec *spec = codec->spec;
896 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200897 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500898 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200899 hda_nid_t nid;
900 int i;
901
Tim Howe56487c22011-07-22 16:41:00 -0500902 spdif_present = 0;
903 if (cfg->dig_outs) {
904 nid = cfg->dig_out_pins[0];
905 if (is_jack_detectable(codec, nid)) {
906 /*
907 TODO: SPDIF output redirect when SENSE_B is enabled.
908 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
909 assumed.
910 */
911 if (snd_hda_jack_detect(codec, nid)
912 /* && spec->sense_b */)
913 spdif_present = 1;
914 }
915 }
916
Takashi Iwaie5f14242009-07-01 18:11:44 +0200917 hp_present = 0;
918 for (i = 0; i < cfg->hp_outs; i++) {
919 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200920 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200921 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800922 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200923 if (hp_present)
924 break;
925 }
Tim Howe56487c22011-07-22 16:41:00 -0500926
927 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200928 for (i = 0; i < cfg->speaker_outs; i++) {
David Henningsson78e2a922012-01-02 12:40:16 +0100929 int pin_ctl = hp_present ? 0 : PIN_OUT;
David Henningsson5660ffd2012-01-02 12:40:17 +0100930 /* detect on spdif is specific to CS4210 */
931 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
David Henningsson78e2a922012-01-02 12:40:16 +0100932 pin_ctl = 0;
933
Takashi Iwaie5f14242009-07-01 18:11:44 +0200934 nid = cfg->speaker_pins[i];
935 snd_hda_codec_write(codec, nid, 0,
David Henningsson78e2a922012-01-02 12:40:16 +0100936 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200937 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100938 if (spec->gpio_eapd_hp) {
939 unsigned int gpio = hp_present ?
940 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200941 snd_hda_codec_write(codec, 0x01, 0,
942 AC_VERB_SET_GPIO_DATA, gpio);
943 }
Tim Howe56487c22011-07-22 16:41:00 -0500944
David Henningsson5660ffd2012-01-02 12:40:17 +0100945 /* specific to CS4210 */
946 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500947 /* mute HPs if spdif jack (SENSE_B) is present */
948 for (i = 0; i < cfg->hp_outs; i++) {
949 nid = cfg->hp_pins[i];
950 snd_hda_codec_write(codec, nid, 0,
951 AC_VERB_SET_PIN_WIDGET_CONTROL,
952 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
953 }
954
955 /* SPDIF TX on/off */
956 if (cfg->dig_outs) {
957 nid = cfg->dig_out_pins[0];
958 snd_hda_codec_write(codec, nid, 0,
959 AC_VERB_SET_PIN_WIDGET_CONTROL,
960 spdif_present ? PIN_OUT : 0);
961
962 }
963 /* Update board GPIOs if neccessary ... */
964 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200965}
966
Tim Howe56487c22011-07-22 16:41:00 -0500967/*
968 * Auto-input redirect for CS421x
969 * Switch max 3 inputs of a single ADC (nid 3)
970*/
971
Takashi Iwaie5f14242009-07-01 18:11:44 +0200972static void cs_automic(struct hda_codec *codec)
973{
974 struct cs_spec *spec = codec->spec;
975 struct auto_pin_cfg *cfg = &spec->autocfg;
976 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800977 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500978
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200979 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800980 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500981
982 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100983 if (spec->vendor_nid == CS420X_VENDOR_NID) {
984 if (present)
985 change_cur_input(codec, spec->automic_idx, 0);
986 else
987 change_cur_input(codec, !spec->automic_idx, 0);
988 } else {
Tim Howe56487c22011-07-22 16:41:00 -0500989 if (present) {
990 spec->last_input = spec->cur_input;
991 spec->cur_input = spec->automic_idx;
992 } else {
993 spec->cur_input = spec->last_input;
994 }
Takashi Iwai05ee7962011-11-16 18:05:11 +0100995 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -0500996 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200997}
998
999/*
1000 */
1001
1002static void init_output(struct hda_codec *codec)
1003{
1004 struct cs_spec *spec = codec->spec;
1005 struct auto_pin_cfg *cfg = &spec->autocfg;
1006 int i;
1007
1008 /* mute first */
1009 for (i = 0; i < spec->multiout.num_dacs; i++)
1010 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1011 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1012 if (spec->multiout.hp_nid)
1013 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1014 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1015 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1016 if (!spec->multiout.extra_out_nid[i])
1017 break;
1018 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1019 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1020 }
1021
1022 /* set appropriate pin controls */
1023 for (i = 0; i < cfg->line_outs; i++)
1024 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1025 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001026 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001027 for (i = 0; i < cfg->hp_outs; i++) {
1028 hda_nid_t nid = cfg->hp_pins[i];
1029 snd_hda_codec_write(codec, nid, 0,
1030 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1031 if (!cfg->speaker_outs)
1032 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001033 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001034 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001035 spec->hp_detect = 1;
1036 }
1037 }
Tim Howe56487c22011-07-22 16:41:00 -05001038
1039 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001040 for (i = 0; i < cfg->speaker_outs; i++)
1041 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1042 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001043
1044 /* SPDIF is enabled on presence detect for CS421x */
1045 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001046 cs_automute(codec);
1047}
1048
1049static void init_input(struct hda_codec *codec)
1050{
1051 struct cs_spec *spec = codec->spec;
1052 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001053 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001054 int i;
1055
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001056 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001057 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001058 hda_nid_t pin = cfg->inputs[i].pin;
1059 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001060 continue;
1061 /* set appropriate pin control and mute first */
1062 ctl = PIN_IN;
Takashi Iwai86e29592010-09-09 14:50:17 +02001063 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001064 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1065 caps >>= AC_PINCAP_VREF_SHIFT;
1066 if (caps & AC_PINCAP_VREF_80)
1067 ctl = PIN_VREF80;
1068 }
1069 snd_hda_codec_write(codec, pin, 0,
1070 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1071 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1072 AC_VERB_SET_AMP_GAIN_MUTE,
1073 AMP_IN_MUTE(spec->adc_idx[i]));
1074 if (spec->mic_detect && spec->automic_idx == i)
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001075 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001076 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001077 /* CS420x has multiple ADC, CS421x has single ADC */
1078 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001079 change_cur_input(codec, spec->cur_input, 1);
1080 if (spec->mic_detect)
1081 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001082
Tim Howe56487c22011-07-22 16:41:00 -05001083 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1084 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1085 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1086 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1087 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1088 * No effect if SPDIF_OUT2 is
1089 * selected in IDX_SPDIF_CTL.
1090 */
1091 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001092 } else {
1093 if (spec->mic_detect)
1094 cs_automic(codec);
1095 else {
1096 spec->cur_adc = spec->adc_nid[spec->cur_input];
1097 cs_update_input_select(codec);
1098 }
Tim Howe56487c22011-07-22 16:41:00 -05001099 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001100}
1101
Takashi Iwaic42d4782011-05-02 11:36:09 +02001102static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001103 {0x11, AC_VERB_SET_PROC_STATE, 1},
1104 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1105 {0x11, AC_VERB_SET_PROC_COEF,
1106 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1107 | 0x0040 /* Mute DACs on FIFO error */
1108 | 0x1000 /* Enable DACs High Pass Filter */
1109 | 0x0400 /* Disable Coefficient Auto increment */
1110 )},
1111 /* Beep */
1112 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1113 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1114
1115 {} /* terminator */
1116};
1117
Brian Austina769cbc2010-09-07 14:36:22 -05001118/* Errata: CS4207 rev C0/C1/C2 Silicon
1119 *
1120 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1121 *
1122 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1123 * may be excessive (up to an additional 200 μA), which is most easily
1124 * observed while the part is being held in reset (RESET# active low).
1125 *
1126 * Root Cause: At initial powerup of the device, the logic that drives
1127 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1128 * initialized.
1129 * Certain random patterns will cause a steady leakage current in those
1130 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1131 *
1132 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1133 * blocks, which will alleviate the issue.
1134 */
1135
Takashi Iwaic42d4782011-05-02 11:36:09 +02001136static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001137 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1138 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1139
1140 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1141 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1142 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1143 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1144 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1145 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1146
1147 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1148 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1149
1150 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1151 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1152 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1153 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1154 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1155 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1156 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1157
Takashi Iwai38c07642011-03-03 14:54:19 +01001158#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001159 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1160 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1161 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001162#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001163
1164 {} /* terminator */
1165};
1166
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001167/* SPDIF setup */
1168static void init_digital(struct hda_codec *codec)
1169{
1170 unsigned int coef;
1171
1172 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1173 coef |= 0x0008; /* Replace with mute on error */
1174 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1175 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1176 * SPDIF_OUT2 is shared with GPIO1 and
1177 * DMIC_SDA2.
1178 */
1179 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001180}
1181
1182static int cs_init(struct hda_codec *codec)
1183{
1184 struct cs_spec *spec = codec->spec;
1185
Brian Austina769cbc2010-09-07 14:36:22 -05001186 /* init_verb sequence for C0/C1/C2 errata*/
1187 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1188
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001189 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001190
1191 if (spec->gpio_mask) {
1192 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1193 spec->gpio_mask);
1194 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1195 spec->gpio_dir);
1196 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1197 spec->gpio_data);
1198 }
1199
Takashi Iwaie5f14242009-07-01 18:11:44 +02001200 init_output(codec);
1201 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001202 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001203 snd_hda_jack_report_sync(codec);
1204
Takashi Iwaie5f14242009-07-01 18:11:44 +02001205 return 0;
1206}
1207
1208static int cs_build_controls(struct hda_codec *codec)
1209{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001210 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001211 int err;
1212
1213 err = build_output(codec);
1214 if (err < 0)
1215 return err;
1216 err = build_input(codec);
1217 if (err < 0)
1218 return err;
1219 err = build_digital_output(codec);
1220 if (err < 0)
1221 return err;
1222 err = build_digital_input(codec);
1223 if (err < 0)
1224 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001225 err = cs_init(codec);
1226 if (err < 0)
1227 return err;
1228
1229 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1230 if (err < 0)
1231 return err;
1232
1233 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001234}
1235
1236static void cs_free(struct hda_codec *codec)
1237{
1238 struct cs_spec *spec = codec->spec;
1239 kfree(spec->capture_bind[0]);
1240 kfree(spec->capture_bind[1]);
1241 kfree(codec->spec);
1242}
1243
1244static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1245{
Takashi Iwai3a938972011-10-28 01:16:55 +02001246 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001247 case HP_EVENT:
1248 cs_automute(codec);
1249 break;
1250 case MIC_EVENT:
1251 cs_automic(codec);
1252 break;
1253 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001254 snd_hda_jack_report_sync(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001255}
1256
Takashi Iwaic42d4782011-05-02 11:36:09 +02001257static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001258 .build_controls = cs_build_controls,
1259 .build_pcms = cs_build_pcms,
1260 .init = cs_init,
1261 .free = cs_free,
1262 .unsol_event = cs_unsol_event,
1263};
1264
1265static int cs_parse_auto_config(struct hda_codec *codec)
1266{
1267 struct cs_spec *spec = codec->spec;
1268 int err;
1269
1270 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1271 if (err < 0)
1272 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001273
1274 err = parse_output(codec);
1275 if (err < 0)
1276 return err;
1277 err = parse_input(codec);
1278 if (err < 0)
1279 return err;
1280 err = parse_digital_output(codec);
1281 if (err < 0)
1282 return err;
1283 err = parse_digital_input(codec);
1284 if (err < 0)
1285 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001286 return 0;
1287}
1288
Takashi Iwaiea734962011-01-17 11:29:34 +01001289static const char * const cs420x_models[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001290 [CS420X_MBP53] = "mbp53",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001291 [CS420X_MBP55] = "mbp55",
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001292 [CS420X_IMAC27] = "imac27",
Dan Carpenter92bb43e2011-11-24 14:48:24 +03001293 [CS420X_APPLE] = "apple",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001294 [CS420X_AUTO] = "auto",
1295};
1296
1297
Takashi Iwaic42d4782011-05-02 11:36:09 +02001298static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001299 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001300 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001301 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001302 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001303 /* this conflicts with too many other models */
1304 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1305 {} /* terminator */
1306};
1307
1308static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
1309 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001310 {} /* terminator */
1311};
1312
1313struct cs_pincfg {
1314 hda_nid_t nid;
1315 u32 val;
1316};
1317
Takashi Iwaic42d4782011-05-02 11:36:09 +02001318static const struct cs_pincfg mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001319 { 0x09, 0x012b4050 },
1320 { 0x0a, 0x90100141 },
1321 { 0x0b, 0x90100140 },
1322 { 0x0c, 0x018b3020 },
1323 { 0x0d, 0x90a00110 },
1324 { 0x0e, 0x400000f0 },
1325 { 0x0f, 0x01cbe030 },
1326 { 0x10, 0x014be060 },
1327 { 0x12, 0x400000f0 },
1328 { 0x15, 0x400000f0 },
1329 {} /* terminator */
1330};
1331
Takashi Iwaic42d4782011-05-02 11:36:09 +02001332static const struct cs_pincfg mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001333 { 0x09, 0x012b4030 },
1334 { 0x0a, 0x90100121 },
1335 { 0x0b, 0x90100120 },
1336 { 0x0c, 0x400000f0 },
1337 { 0x0d, 0x90a00110 },
1338 { 0x0e, 0x400000f0 },
1339 { 0x0f, 0x400000f0 },
1340 { 0x10, 0x014be040 },
1341 { 0x12, 0x400000f0 },
1342 { 0x15, 0x400000f0 },
1343 {} /* terminator */
1344};
1345
Takashi Iwaic42d4782011-05-02 11:36:09 +02001346static const struct cs_pincfg imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001347 { 0x09, 0x012b4050 },
1348 { 0x0a, 0x90100140 },
1349 { 0x0b, 0x90100142 },
1350 { 0x0c, 0x018b3020 },
1351 { 0x0d, 0x90a00110 },
1352 { 0x0e, 0x400000f0 },
1353 { 0x0f, 0x01cbe030 },
1354 { 0x10, 0x014be060 },
1355 { 0x12, 0x01ab9070 },
1356 { 0x15, 0x400000f0 },
1357 {} /* terminator */
1358};
1359
Takashi Iwaic42d4782011-05-02 11:36:09 +02001360static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001361 [CS420X_MBP53] = mbp53_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001362 [CS420X_MBP55] = mbp55_pincfgs,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001363 [CS420X_IMAC27] = imac27_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001364};
1365
Tim Howe56487c22011-07-22 16:41:00 -05001366static void fix_pincfg(struct hda_codec *codec, int model,
1367 const struct cs_pincfg **pin_configs)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001368{
Tim Howe56487c22011-07-22 16:41:00 -05001369 const struct cs_pincfg *cfg = pin_configs[model];
Takashi Iwaia6bae202009-07-06 15:15:22 +02001370 if (!cfg)
1371 return;
1372 for (; cfg->nid; cfg++)
1373 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1374}
1375
Takashi Iwaie5f14242009-07-01 18:11:44 +02001376static int patch_cs420x(struct hda_codec *codec)
1377{
1378 struct cs_spec *spec;
1379 int err;
1380
1381 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1382 if (!spec)
1383 return -ENOMEM;
1384 codec->spec = spec;
1385
Tim Howe56487c22011-07-22 16:41:00 -05001386 spec->vendor_nid = CS420X_VENDOR_NID;
1387
Takashi Iwaia6bae202009-07-06 15:15:22 +02001388 spec->board_config =
1389 snd_hda_check_board_config(codec, CS420X_MODELS,
1390 cs420x_models, cs420x_cfg_tbl);
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001391 if (spec->board_config < 0)
1392 spec->board_config =
1393 snd_hda_check_board_codec_sid_config(codec,
1394 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001395 if (spec->board_config >= 0)
Tim Howe56487c22011-07-22 16:41:00 -05001396 fix_pincfg(codec, spec->board_config, cs_pincfgs);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001397
Takashi Iwaied208252009-07-07 09:04:26 +02001398 switch (spec->board_config) {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001399 case CS420X_IMAC27:
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001400 case CS420X_MBP53:
Takashi Iwaied208252009-07-07 09:04:26 +02001401 case CS420X_MBP55:
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001402 case CS420X_APPLE:
1403 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1404 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1405 spec->gpio_mask = spec->gpio_dir =
1406 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
Takashi Iwaied208252009-07-07 09:04:26 +02001407 break;
1408 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001409
Takashi Iwaied208252009-07-07 09:04:26 +02001410 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001411 if (err < 0)
1412 goto error;
1413
Takashi Iwaie5f14242009-07-01 18:11:44 +02001414 codec->patch_ops = cs_patch_ops;
1415
1416 return 0;
1417
1418 error:
1419 kfree(codec->spec);
1420 codec->spec = NULL;
1421 return err;
1422}
1423
Tim Howe56487c22011-07-22 16:41:00 -05001424/*
1425 * Cirrus Logic CS4210
1426 *
1427 * 1 DAC => HP(sense) / Speakers,
1428 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1429 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1430*/
1431
1432/* CS4210 board names */
1433static const char *cs421x_models[CS421X_MODELS] = {
1434 [CS421X_CDB4210] = "cdb4210",
1435};
1436
1437static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1438 /* Test Intel board + CDB2410 */
1439 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1440 {} /* terminator */
1441};
1442
1443/* CS4210 board pinconfigs */
1444/* Default CS4210 (CDB4210)*/
1445static const struct cs_pincfg cdb4210_pincfgs[] = {
1446 { 0x05, 0x0321401f },
1447 { 0x06, 0x90170010 },
1448 { 0x07, 0x03813031 },
1449 { 0x08, 0xb7a70037 },
1450 { 0x09, 0xb7a6003e },
1451 { 0x0a, 0x034510f0 },
1452 {} /* terminator */
1453};
1454
1455static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1456 [CS421X_CDB4210] = cdb4210_pincfgs,
1457};
1458
1459static const struct hda_verb cs421x_coef_init_verbs[] = {
1460 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1461 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1462 /*
1463 Disable Coefficient Index Auto-Increment(DAI)=1,
1464 PDREF=0
1465 */
1466 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1467
1468 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1469 /* ADC SZCMode = Digital Soft Ramp */
1470 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1471
1472 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1473 {0x0B, AC_VERB_SET_PROC_COEF,
1474 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1475 | 0x0004 /* Mute DAC on FIFO error */
1476 | 0x0008 /* Enable DAC High Pass Filter */
1477 )},
1478 {} /* terminator */
1479};
1480
1481/* Errata: CS4210 rev A1 Silicon
1482 *
1483 * http://www.cirrus.com/en/pubs/errata/
1484 *
1485 * Description:
1486 * 1. Performance degredation is present in the ADC.
1487 * 2. Speaker output is not completely muted upon HP detect.
1488 * 3. Noise is present when clipping occurs on the amplified
1489 * speaker outputs.
1490 *
1491 * Workaround:
1492 * The following verb sequence written to the registers during
1493 * initialization will correct the issues listed above.
1494 */
1495
1496static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1497 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1498
1499 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1500 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1501
1502 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1503 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1504
1505 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1506 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1507
1508 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1509 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1510
1511 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1512 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1513
1514 {} /* terminator */
1515};
1516
1517/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1518static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1519
1520static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1521 struct snd_ctl_elem_info *uinfo)
1522{
1523 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1524 uinfo->count = 1;
1525 uinfo->value.integer.min = 0;
1526 uinfo->value.integer.max = 3;
1527 return 0;
1528}
1529
1530static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1531 struct snd_ctl_elem_value *ucontrol)
1532{
1533 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1534
1535 ucontrol->value.integer.value[0] =
1536 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1537 return 0;
1538}
1539
1540static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_value *ucontrol)
1542{
1543 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1544
1545 unsigned int vol = ucontrol->value.integer.value[0];
1546 unsigned int coef =
1547 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1548 unsigned int original_coef = coef;
1549
1550 coef &= ~0x0003;
1551 coef |= (vol & 0x0003);
1552 if (original_coef == coef)
1553 return 0;
1554 else {
1555 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1556 return 1;
1557 }
1558}
1559
1560static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1561
1562 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1563 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1564 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1565 .name = "Speaker Boost Playback Volume",
1566 .info = cs421x_boost_vol_info,
1567 .get = cs421x_boost_vol_get,
1568 .put = cs421x_boost_vol_put,
1569 .tlv = { .p = cs421x_speaker_boost_db_scale },
1570};
1571
David Henningsson5660ffd2012-01-02 12:40:17 +01001572static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001573{
1574 struct cs_spec *spec = codec->spec;
1575 unsigned int def_conf, coef;
1576
1577 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1578 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1579
1580 if (spec->gpio_mask)
1581 coef |= 0x0008; /* B1,B2 are GPIOs */
1582 else
1583 coef &= ~0x0008;
1584
1585 if (spec->sense_b)
1586 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1587 else
1588 coef &= ~0x0010;
1589
1590 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1591
1592 if ((spec->gpio_mask || spec->sense_b) &&
1593 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1594
1595 /*
1596 GPIO or SENSE_B forced - disconnect the DMIC pin.
1597 */
1598 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1599 def_conf &= ~AC_DEFCFG_PORT_CONN;
1600 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1601 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1602 }
1603}
1604
1605static void init_cs421x_digital(struct hda_codec *codec)
1606{
1607 struct cs_spec *spec = codec->spec;
1608 struct auto_pin_cfg *cfg = &spec->autocfg;
1609 int i;
1610
1611
1612 for (i = 0; i < cfg->dig_outs; i++) {
1613 hda_nid_t nid = cfg->dig_out_pins[i];
1614 if (!cfg->speaker_outs)
1615 continue;
1616 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001617 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
Tim Howe56487c22011-07-22 16:41:00 -05001618 spec->spdif_detect = 1;
1619 }
1620 }
1621}
1622
1623static int cs421x_init(struct hda_codec *codec)
1624{
1625 struct cs_spec *spec = codec->spec;
1626
David Henningsson5660ffd2012-01-02 12:40:17 +01001627 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1628 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1629 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1630 cs4210_pinmux_init(codec);
1631 }
Tim Howe56487c22011-07-22 16:41:00 -05001632
1633 if (spec->gpio_mask) {
1634 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1635 spec->gpio_mask);
1636 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1637 spec->gpio_dir);
1638 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1639 spec->gpio_data);
1640 }
1641
1642 init_output(codec);
1643 init_input(codec);
1644 init_cs421x_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001645 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001646
1647 return 0;
1648}
1649
1650/*
1651 * CS4210 Input MUX (1 ADC)
1652 */
1653static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1654 struct snd_ctl_elem_info *uinfo)
1655{
1656 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1657 struct cs_spec *spec = codec->spec;
1658
1659 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1660}
1661
1662static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1663 struct snd_ctl_elem_value *ucontrol)
1664{
1665 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1666 struct cs_spec *spec = codec->spec;
1667
1668 ucontrol->value.enumerated.item[0] = spec->cur_input;
1669 return 0;
1670}
1671
1672static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1673 struct snd_ctl_elem_value *ucontrol)
1674{
1675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1676 struct cs_spec *spec = codec->spec;
1677
1678 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1679 spec->adc_nid[0], &spec->cur_input);
1680
1681}
1682
1683static struct snd_kcontrol_new cs421x_capture_source = {
1684
1685 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1686 .name = "Capture Source",
1687 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1688 .info = cs421x_mux_enum_info,
1689 .get = cs421x_mux_enum_get,
1690 .put = cs421x_mux_enum_put,
1691};
1692
1693static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1694{
1695 struct cs_spec *spec = codec->spec;
1696 struct auto_pin_cfg *cfg = &spec->autocfg;
1697 const struct hda_input_mux *imux = &spec->input_mux;
1698 hda_nid_t pin = cfg->inputs[item].pin;
1699 struct snd_kcontrol *kctl;
1700 u32 caps;
1701
1702 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1703 return 0;
1704
1705 caps = query_amp_caps(codec, pin, HDA_INPUT);
1706 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1707 if (caps <= 1)
1708 return 0;
1709
1710 return add_volume(codec, imux->items[item].label, 0,
1711 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1712}
1713
1714/* add a (input-boost) volume control to the given input pin */
1715static int build_cs421x_input(struct hda_codec *codec)
1716{
1717 struct cs_spec *spec = codec->spec;
1718 struct auto_pin_cfg *cfg = &spec->autocfg;
1719 struct hda_input_mux *imux = &spec->input_mux;
1720 int i, err, type_idx;
1721 const char *label;
1722
1723 if (!spec->num_inputs)
1724 return 0;
1725
1726 /* make bind-capture */
1727 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1728 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1729 for (i = 0; i < 2; i++) {
1730 struct snd_kcontrol *kctl;
1731 int n;
1732 if (!spec->capture_bind[i])
1733 return -ENOMEM;
1734 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1735 if (!kctl)
1736 return -ENOMEM;
1737 kctl->private_value = (long)spec->capture_bind[i];
1738 err = snd_hda_ctl_add(codec, 0, kctl);
1739 if (err < 0)
1740 return err;
1741 for (n = 0; n < AUTO_PIN_LAST; n++) {
1742 if (!spec->adc_nid[n])
1743 continue;
1744 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1745 if (err < 0)
1746 return err;
1747 }
1748 }
1749
1750 /* Add Input MUX Items + Capture Volume/Switch */
1751 for (i = 0; i < spec->num_inputs; i++) {
1752 label = hda_get_autocfg_input_label(codec, cfg, i);
1753 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1754
1755 err = cs421x_add_input_volume_control(codec, i);
1756 if (err < 0)
1757 return err;
1758 }
1759
1760 /*
1761 Add 'Capture Source' Switch if
1762 * 2 inputs and no mic detec
1763 * 3 inputs
1764 */
1765 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1766 (spec->num_inputs == 3)) {
1767
1768 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1769 snd_ctl_new1(&cs421x_capture_source, codec));
1770 if (err < 0)
1771 return err;
1772 }
1773
1774 return 0;
1775}
1776
1777/* Single DAC (Mute/Gain) */
1778static int build_cs421x_output(struct hda_codec *codec)
1779{
1780 hda_nid_t dac = CS4210_DAC_NID;
1781 struct cs_spec *spec = codec->spec;
1782 struct auto_pin_cfg *cfg = &spec->autocfg;
1783 struct snd_kcontrol *kctl;
1784 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001785 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001786
1787 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001788
1789 err = add_mute(codec, name, 0,
1790 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1791 if (err < 0)
1792 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001793
1794 err = add_volume(codec, name, 0,
1795 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1796 if (err < 0)
1797 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001798
David Henningsson5660ffd2012-01-02 12:40:17 +01001799 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001800 err = snd_hda_ctl_add(codec, 0,
1801 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1802 if (err < 0)
1803 return err;
1804 }
1805 return err;
1806}
1807
1808static int cs421x_build_controls(struct hda_codec *codec)
1809{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001810 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001811 int err;
1812
1813 err = build_cs421x_output(codec);
1814 if (err < 0)
1815 return err;
1816 err = build_cs421x_input(codec);
1817 if (err < 0)
1818 return err;
1819 err = build_digital_output(codec);
1820 if (err < 0)
1821 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001822 err = cs421x_init(codec);
1823 if (err < 0)
1824 return err;
1825
1826 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1827 if (err < 0)
1828 return err;
1829
1830 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001831}
1832
1833static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1834{
Takashi Iwai3a938972011-10-28 01:16:55 +02001835 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Tim Howe56487c22011-07-22 16:41:00 -05001836 case HP_EVENT:
1837 case SPDIF_EVENT:
1838 cs_automute(codec);
1839 break;
1840
1841 case MIC_EVENT:
1842 cs_automic(codec);
1843 break;
1844 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001845 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001846}
1847
1848static int parse_cs421x_input(struct hda_codec *codec)
1849{
1850 struct cs_spec *spec = codec->spec;
1851 struct auto_pin_cfg *cfg = &spec->autocfg;
1852 int i;
1853
1854 for (i = 0; i < cfg->num_inputs; i++) {
1855 hda_nid_t pin = cfg->inputs[i].pin;
1856 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1857 spec->cur_input = spec->last_input = i;
1858 spec->num_inputs++;
1859
1860 /* check whether the automatic mic switch is available */
1861 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1862 spec->mic_detect = 1;
1863 spec->automic_idx = i;
1864 }
1865 }
1866 return 0;
1867}
1868
1869static int cs421x_parse_auto_config(struct hda_codec *codec)
1870{
1871 struct cs_spec *spec = codec->spec;
1872 int err;
1873
1874 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1875 if (err < 0)
1876 return err;
1877 err = parse_output(codec);
1878 if (err < 0)
1879 return err;
1880 err = parse_cs421x_input(codec);
1881 if (err < 0)
1882 return err;
1883 err = parse_digital_output(codec);
1884 if (err < 0)
1885 return err;
1886 return 0;
1887}
1888
1889#ifdef CONFIG_PM
1890/*
1891 Manage PDREF, when transitioning to D3hot
1892 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1893*/
1894static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1895{
David Henningsson5660ffd2012-01-02 12:40:17 +01001896 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001897 unsigned int coef;
1898
1899 snd_hda_shutup_pins(codec);
1900
1901 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1902 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1903 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1904 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1905
David Henningsson5660ffd2012-01-02 12:40:17 +01001906 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1907 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1908 coef |= 0x0004; /* PDREF */
1909 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1910 }
Tim Howe56487c22011-07-22 16:41:00 -05001911
1912 return 0;
1913}
1914#endif
1915
David Henningsson5660ffd2012-01-02 12:40:17 +01001916static struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001917 .build_controls = cs421x_build_controls,
1918 .build_pcms = cs_build_pcms,
1919 .init = cs421x_init,
1920 .free = cs_free,
1921 .unsol_event = cs421x_unsol_event,
1922#ifdef CONFIG_PM
1923 .suspend = cs421x_suspend,
1924#endif
1925};
1926
David Henningsson5660ffd2012-01-02 12:40:17 +01001927static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001928{
1929 struct cs_spec *spec;
1930 int err;
1931
1932 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1933 if (!spec)
1934 return -ENOMEM;
1935 codec->spec = spec;
1936
David Henningsson5660ffd2012-01-02 12:40:17 +01001937 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001938
1939 spec->board_config =
1940 snd_hda_check_board_config(codec, CS421X_MODELS,
1941 cs421x_models, cs421x_cfg_tbl);
1942 if (spec->board_config >= 0)
1943 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1944 /*
1945 Setup GPIO/SENSE for each board (if used)
1946 */
1947 switch (spec->board_config) {
1948 case CS421X_CDB4210:
1949 snd_printd("CS4210 board: %s\n",
1950 cs421x_models[spec->board_config]);
1951/* spec->gpio_mask = 3;
1952 spec->gpio_dir = 3;
1953 spec->gpio_data = 3;
1954*/
1955 spec->sense_b = 1;
1956
1957 break;
1958 }
1959
1960 /*
1961 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1962 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1963 is disabled.
1964 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001965 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001966
1967 err = cs421x_parse_auto_config(codec);
1968 if (err < 0)
1969 goto error;
1970
David Henningsson5660ffd2012-01-02 12:40:17 +01001971 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05001972
1973 return 0;
1974
1975 error:
1976 kfree(codec->spec);
1977 codec->spec = NULL;
1978 return err;
1979}
1980
David Henningsson5660ffd2012-01-02 12:40:17 +01001981static int patch_cs4213(struct hda_codec *codec)
1982{
1983 struct cs_spec *spec;
1984 int err;
1985
1986 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1987 if (!spec)
1988 return -ENOMEM;
1989 codec->spec = spec;
1990
1991 spec->vendor_nid = CS4213_VENDOR_NID;
1992
1993 err = cs421x_parse_auto_config(codec);
1994 if (err < 0)
1995 goto error;
1996
1997 codec->patch_ops = cs421x_patch_ops;
1998 return 0;
1999
2000 error:
2001 kfree(codec->spec);
2002 codec->spec = NULL;
2003 return err;
2004}
2005
Takashi Iwaie5f14242009-07-01 18:11:44 +02002006
2007/*
2008 * patch entries
2009 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002010static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002011 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2012 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002013 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2014 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002015 {} /* terminator */
2016};
2017
2018MODULE_ALIAS("snd-hda-codec-id:10134206");
2019MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002020MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002021MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002022
2023MODULE_LICENSE("GPL");
2024MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2025
2026static struct hda_codec_preset_list cirrus_list = {
2027 .preset = snd_hda_preset_cirrus,
2028 .owner = THIS_MODULE,
2029};
2030
2031static int __init patch_cirrus_init(void)
2032{
2033 return snd_hda_add_codec_preset(&cirrus_list);
2034}
2035
2036static void __exit patch_cirrus_exit(void)
2037{
2038 snd_hda_delete_codec_preset(&cirrus_list);
2039}
2040
2041module_init(patch_cirrus_init)
2042module_exit(patch_cirrus_exit)