blob: 3605fbb6ef154581caed85803b0ba236195a5141 [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,
Jérémy Lal7e5bea12012-01-09 17:19:45 +010082 CS420X_IMAC27_122,
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010083 CS420X_APPLE,
Takashi Iwaia6bae202009-07-06 15:15:22 +020084 CS420X_AUTO,
85 CS420X_MODELS
86};
87
Tim Howe56487c22011-07-22 16:41:00 -050088/* CS421x boards */
89enum {
90 CS421X_CDB4210,
91 CS421X_MODELS
92};
93
Takashi Iwai40c20fa2009-07-06 13:00:57 +020094/* Vendor-specific processing widget */
95#define CS420X_VENDOR_NID 0x11
96#define CS_DIG_OUT1_PIN_NID 0x10
97#define CS_DIG_OUT2_PIN_NID 0x15
Daniel J Blueman1289a3e2012-11-04 13:19:03 +080098#define CS_DMIC1_PIN_NID 0x0e
99#define CS_DMIC2_PIN_NID 0x12
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200100
101/* coef indices */
102#define IDX_SPDIF_STAT 0x0000
103#define IDX_SPDIF_CTL 0x0001
104#define IDX_ADC_CFG 0x0002
105/* SZC bitmask, 4 modes below:
106 * 0 = immediate,
107 * 1 = digital immediate, analog zero-cross
108 * 2 = digtail & analog soft-ramp
109 * 3 = digital soft-ramp, analog zero-cross
110 */
111#define CS_COEF_ADC_SZC_MASK (3 << 0)
112#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
113#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
114/* PGA mode: 0 = differential, 1 = signle-ended */
115#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
116#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
117#define IDX_DAC_CFG 0x0003
118/* SZC bitmask, 4 modes below:
119 * 0 = Immediate
120 * 1 = zero-cross
121 * 2 = soft-ramp
122 * 3 = soft-ramp on zero-cross
123 */
124#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
125#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
126#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
127
128#define IDX_BEEP_CFG 0x0004
129/* 0x0008 - test reg key */
130/* 0x0009 - 0x0014 -> 12 test regs */
131/* 0x0015 - visibility reg */
132
Tim Howe56487c22011-07-22 16:41:00 -0500133/*
134 * Cirrus Logic CS4210
135 *
136 * 1 DAC => HP(sense) / Speakers,
137 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
138 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
139*/
140#define CS4210_DAC_NID 0x02
141#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100142#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500143#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
144#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
145
146#define CS421X_IDX_DEV_CFG 0x01
147#define CS421X_IDX_ADC_CFG 0x02
148#define CS421X_IDX_DAC_CFG 0x03
149#define CS421X_IDX_SPK_CTL 0x04
150
151#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200152
David Henningsson5660ffd2012-01-02 12:40:17 +0100153/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
154#define CS4213_VENDOR_NID 0x09
155
156
Takashi Iwai277a57c2009-07-08 12:42:08 +0200157static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
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 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200163 AC_VERB_GET_PROC_COEF, 0);
164}
165
Takashi Iwai277a57c2009-07-08 12:42:08 +0200166static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
167 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200168{
Tim Howe56487c22011-07-22 16:41:00 -0500169 struct cs_spec *spec = codec->spec;
170 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200171 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500172 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200173 AC_VERB_SET_PROC_COEF, coef);
174}
175
176
Takashi Iwaie5f14242009-07-01 18:11:44 +0200177#define HP_EVENT 1
178#define MIC_EVENT 2
179
180/*
181 * PCM callbacks
182 */
183static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
184 struct hda_codec *codec,
185 struct snd_pcm_substream *substream)
186{
187 struct cs_spec *spec = codec->spec;
188 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
189 hinfo);
190}
191
192static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
193 struct hda_codec *codec,
194 unsigned int stream_tag,
195 unsigned int format,
196 struct snd_pcm_substream *substream)
197{
198 struct cs_spec *spec = codec->spec;
199 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
200 stream_tag, format, substream);
201}
202
203static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
204 struct hda_codec *codec,
205 struct snd_pcm_substream *substream)
206{
207 struct cs_spec *spec = codec->spec;
208 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
209}
210
211/*
212 * Digital out
213 */
214static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
215 struct hda_codec *codec,
216 struct snd_pcm_substream *substream)
217{
218 struct cs_spec *spec = codec->spec;
219 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
220}
221
222static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
223 struct hda_codec *codec,
224 struct snd_pcm_substream *substream)
225{
226 struct cs_spec *spec = codec->spec;
227 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
228}
229
230static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
231 struct hda_codec *codec,
232 unsigned int stream_tag,
233 unsigned int format,
234 struct snd_pcm_substream *substream)
235{
236 struct cs_spec *spec = codec->spec;
237 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
238 format, substream);
239}
240
241static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
242 struct hda_codec *codec,
243 struct snd_pcm_substream *substream)
244{
245 struct cs_spec *spec = codec->spec;
246 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
247}
248
Takashi Iwai05ee7962011-11-16 18:05:11 +0100249static void cs_update_input_select(struct hda_codec *codec)
250{
251 struct cs_spec *spec = codec->spec;
252 if (spec->cur_adc)
253 snd_hda_codec_write(codec, spec->cur_adc, 0,
254 AC_VERB_SET_CONNECT_SEL,
255 spec->adc_idx[spec->cur_input]);
256}
257
Takashi Iwaie5f14242009-07-01 18:11:44 +0200258/*
259 * Analog capture
260 */
261static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
262 struct hda_codec *codec,
263 unsigned int stream_tag,
264 unsigned int format,
265 struct snd_pcm_substream *substream)
266{
267 struct cs_spec *spec = codec->spec;
268 spec->cur_adc = spec->adc_nid[spec->cur_input];
269 spec->cur_adc_stream_tag = stream_tag;
270 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100271 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200272 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
273 return 0;
274}
275
276static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
277 struct hda_codec *codec,
278 struct snd_pcm_substream *substream)
279{
280 struct cs_spec *spec = codec->spec;
281 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
282 spec->cur_adc = 0;
283 return 0;
284}
285
286/*
287 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200288static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200289 .substreams = 1,
290 .channels_min = 2,
291 .channels_max = 2,
292 .ops = {
293 .open = cs_playback_pcm_open,
294 .prepare = cs_playback_pcm_prepare,
295 .cleanup = cs_playback_pcm_cleanup
296 },
297};
298
Takashi Iwaic42d4782011-05-02 11:36:09 +0200299static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200300 .substreams = 1,
301 .channels_min = 2,
302 .channels_max = 2,
303 .ops = {
304 .prepare = cs_capture_pcm_prepare,
305 .cleanup = cs_capture_pcm_cleanup
306 },
307};
308
Takashi Iwaic42d4782011-05-02 11:36:09 +0200309static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200310 .substreams = 1,
311 .channels_min = 2,
312 .channels_max = 2,
313 .ops = {
314 .open = cs_dig_playback_pcm_open,
315 .close = cs_dig_playback_pcm_close,
316 .prepare = cs_dig_playback_pcm_prepare,
317 .cleanup = cs_dig_playback_pcm_cleanup
318 },
319};
320
Takashi Iwaic42d4782011-05-02 11:36:09 +0200321static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200322 .substreams = 1,
323 .channels_min = 2,
324 .channels_max = 2,
325};
326
327static int cs_build_pcms(struct hda_codec *codec)
328{
329 struct cs_spec *spec = codec->spec;
330 struct hda_pcm *info = spec->pcm_rec;
331
332 codec->pcm_info = info;
333 codec->num_pcms = 0;
334
335 info->name = "Cirrus Analog";
336 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
337 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
338 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
339 spec->multiout.max_channels;
340 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200341 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
342 spec->adc_nid[spec->cur_input];
343 codec->num_pcms++;
344
345 if (!spec->multiout.dig_out_nid && !spec->dig_in)
346 return 0;
347
348 info++;
349 info->name = "Cirrus Digital";
350 info->pcm_type = spec->autocfg.dig_out_type[0];
351 if (!info->pcm_type)
352 info->pcm_type = HDA_PCM_TYPE_SPDIF;
353 if (spec->multiout.dig_out_nid) {
354 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
355 cs_pcm_digital_playback;
356 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
357 spec->multiout.dig_out_nid;
358 }
359 if (spec->dig_in) {
360 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
361 cs_pcm_digital_capture;
362 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
363 }
364 codec->num_pcms++;
365
366 return 0;
367}
368
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200369/*
370 * parse codec topology
371 */
372
Takashi Iwaie5f14242009-07-01 18:11:44 +0200373static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
374{
375 hda_nid_t dac;
376 if (!pin)
377 return 0;
378 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
379 return 0;
380 return dac;
381}
382
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200383static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
384{
385 struct cs_spec *spec = codec->spec;
386 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200387 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200388 unsigned int val;
389 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200390 return 0;
391 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200392 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200393}
394
395static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
396 unsigned int *idxp)
397{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200398 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200399 hda_nid_t nid;
400
401 nid = codec->start_nid;
402 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200403 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200404 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200405 if (type != AC_WID_AUD_IN)
406 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200407 idx = snd_hda_get_conn_index(codec, nid, pin, false);
408 if (idx >= 0) {
409 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200410 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200411 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200412 }
413 return 0;
414}
415
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200416static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
417{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200418 unsigned int val;
419 val = snd_hda_codec_get_pincfg(codec, nid);
420 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
421}
422
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200423static int parse_output(struct hda_codec *codec)
424{
425 struct cs_spec *spec = codec->spec;
426 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200427 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200428 hda_nid_t dac;
429
430 for (i = 0; i < cfg->line_outs; i++) {
431 dac = get_dac(codec, cfg->line_out_pins[i]);
432 if (!dac)
433 break;
434 spec->dac_nid[i] = dac;
435 }
436 spec->multiout.num_dacs = i;
437 spec->multiout.dac_nids = spec->dac_nid;
438 spec->multiout.max_channels = i * 2;
439
440 /* add HP and speakers */
441 extra_nids = 0;
442 for (i = 0; i < cfg->hp_outs; i++) {
443 dac = get_dac(codec, cfg->hp_pins[i]);
444 if (!dac)
445 break;
446 if (!i)
447 spec->multiout.hp_nid = dac;
448 else
449 spec->multiout.extra_out_nid[extra_nids++] = dac;
450 }
451 for (i = 0; i < cfg->speaker_outs; i++) {
452 dac = get_dac(codec, cfg->speaker_pins[i]);
453 if (!dac)
454 break;
455 spec->multiout.extra_out_nid[extra_nids++] = dac;
456 }
457
458 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
459 cfg->speaker_outs = cfg->line_outs;
460 memcpy(cfg->speaker_pins, cfg->line_out_pins,
461 sizeof(cfg->speaker_pins));
462 cfg->line_outs = 0;
David Henningssonde134052012-11-21 10:03:10 +0100463 memset(cfg->line_out_pins, 0, sizeof(cfg->line_out_pins));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200464 }
465
466 return 0;
467}
468
469static int parse_input(struct hda_codec *codec)
470{
471 struct cs_spec *spec = codec->spec;
472 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200473 int i;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200474
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200475 for (i = 0; i < cfg->num_inputs; i++) {
476 hda_nid_t pin = cfg->inputs[i].pin;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200477 spec->input_idx[spec->num_inputs] = i;
478 spec->capsrc_idx[i] = spec->num_inputs++;
479 spec->cur_input = i;
480 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
481 }
482 if (!spec->num_inputs)
483 return 0;
484
485 /* check whether the automatic mic switch is available */
486 if (spec->num_inputs == 2 &&
Takashi Iwai86e29592010-09-09 14:50:17 +0200487 cfg->inputs[0].type == AUTO_PIN_MIC &&
488 cfg->inputs[1].type == AUTO_PIN_MIC) {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200489 if (is_ext_mic(codec, cfg->inputs[0].pin)) {
490 if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200491 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200492 spec->automic_idx = 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200493 }
494 } else {
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200495 if (is_ext_mic(codec, cfg->inputs[1].pin)) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200496 spec->mic_detect = 1;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200497 spec->automic_idx = 1;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200498 }
499 }
500 }
501 return 0;
502}
503
504
505static int parse_digital_output(struct hda_codec *codec)
506{
507 struct cs_spec *spec = codec->spec;
508 struct auto_pin_cfg *cfg = &spec->autocfg;
509 hda_nid_t nid;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200510
511 if (!cfg->dig_outs)
512 return 0;
513 if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
514 return 0;
515 spec->multiout.dig_out_nid = nid;
516 spec->multiout.share_spdif = 1;
517 if (cfg->dig_outs > 1 &&
518 snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
519 spec->slave_dig_outs[0] = nid;
520 codec->slave_dig_outs = spec->slave_dig_outs;
521 }
522 return 0;
523}
524
525static int parse_digital_input(struct hda_codec *codec)
526{
527 struct cs_spec *spec = codec->spec;
528 struct auto_pin_cfg *cfg = &spec->autocfg;
529 int idx;
530
Takashi Iwai60e53882009-07-06 15:01:09 +0200531 if (cfg->dig_in_pin)
532 spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
533 return 0;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200534}
535
536/*
537 * create mixer controls
538 */
539
Takashi Iwaiea734962011-01-17 11:29:34 +0100540static const char * const dir_sfx[2] = { "Playback", "Capture" };
Takashi Iwaie5f14242009-07-01 18:11:44 +0200541
542static int add_mute(struct hda_codec *codec, const char *name, int index,
543 unsigned int pval, int dir, struct snd_kcontrol **kctlp)
544{
Takashi Iwaib4dabfc2009-07-07 09:05:07 +0200545 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200546 struct snd_kcontrol_new knew =
547 HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
548 knew.private_value = pval;
549 snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
550 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100551 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100552 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200553}
554
555static int add_volume(struct hda_codec *codec, const char *name,
556 int index, unsigned int pval, int dir,
557 struct snd_kcontrol **kctlp)
558{
David Henningsson2e1210b2011-09-14 13:22:54 +0200559 char tmp[44];
Takashi Iwaie5f14242009-07-01 18:11:44 +0200560 struct snd_kcontrol_new knew =
561 HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
562 knew.private_value = pval;
563 snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
564 *kctlp = snd_ctl_new1(&knew, codec);
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +0100565 (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +0100566 return snd_hda_ctl_add(codec, 0, *kctlp);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200567}
568
569static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
570{
571 unsigned int caps;
572
573 /* set the upper-limit for mixer amp to 0dB */
574 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
575 caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
576 caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
577 << AC_AMPCAP_NUM_STEPS_SHIFT;
578 snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
579}
580
581static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
582{
583 struct cs_spec *spec = codec->spec;
584 unsigned int tlv[4];
585 int err;
586
587 spec->vmaster_sw =
588 snd_ctl_make_virtual_master("Master Playback Switch", NULL);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100589 err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200590 if (err < 0)
591 return err;
592
593 snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
594 spec->vmaster_vol =
595 snd_ctl_make_virtual_master("Master Playback Volume", tlv);
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100596 err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200597 if (err < 0)
598 return err;
599 return 0;
600}
601
602static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
603 int num_ctls, int type)
604{
605 struct cs_spec *spec = codec->spec;
606 const char *name;
607 int err, index;
608 struct snd_kcontrol *kctl;
Takashi Iwaic42d4782011-05-02 11:36:09 +0200609 static const char * const speakers[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200610 "Front Speaker", "Surround Speaker", "Bass Speaker"
611 };
Takashi Iwaic42d4782011-05-02 11:36:09 +0200612 static const char * const line_outs[] = {
Takashi Iwaie49a3432012-03-01 18:14:41 +0100613 "Front Line Out", "Surround Line Out", "Bass Line Out"
Takashi Iwaie5f14242009-07-01 18:11:44 +0200614 };
615
616 fix_volume_caps(codec, dac);
617 if (!spec->vmaster_sw) {
618 err = add_vmaster(codec, dac);
619 if (err < 0)
620 return err;
621 }
622
623 index = 0;
624 switch (type) {
625 case AUTO_PIN_HP_OUT:
626 name = "Headphone";
627 index = idx;
628 break;
629 case AUTO_PIN_SPEAKER_OUT:
630 if (num_ctls > 1)
631 name = speakers[idx];
632 else
633 name = "Speaker";
634 break;
635 default:
636 if (num_ctls > 1)
637 name = line_outs[idx];
638 else
Takashi Iwaie49a3432012-03-01 18:14:41 +0100639 name = "Line Out";
Takashi Iwaie5f14242009-07-01 18:11:44 +0200640 break;
641 }
642
643 err = add_mute(codec, name, index,
644 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
645 if (err < 0)
646 return err;
647 err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
648 if (err < 0)
649 return err;
650
651 err = add_volume(codec, name, index,
652 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
653 if (err < 0)
654 return err;
655 err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
656 if (err < 0)
657 return err;
658
659 return 0;
660}
661
662static int build_output(struct hda_codec *codec)
663{
664 struct cs_spec *spec = codec->spec;
665 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200666 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200667
668 for (i = 0; i < cfg->line_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200669 err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
670 i, cfg->line_outs, cfg->line_out_type);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200671 if (err < 0)
672 return err;
673 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200674 for (i = 0; i < cfg->hp_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200675 err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
676 i, cfg->hp_outs, AUTO_PIN_HP_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200677 if (err < 0)
678 return err;
679 }
680 for (i = 0; i < cfg->speaker_outs; i++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200681 err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
682 i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200683 if (err < 0)
684 return err;
685 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200686 return 0;
687}
688
689/*
690 */
691
Takashi Iwaic42d4782011-05-02 11:36:09 +0200692static const struct snd_kcontrol_new cs_capture_ctls[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200693 HDA_BIND_SW("Capture Switch", 0),
694 HDA_BIND_VOL("Capture Volume", 0),
695};
696
Takashi Iwaiea359292009-07-06 12:58:59 +0200697static int change_cur_input(struct hda_codec *codec, unsigned int idx,
698 int force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200699{
700 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200701
Takashi Iwaiea359292009-07-06 12:58:59 +0200702 if (spec->cur_input == idx && !force)
Takashi Iwaie5f14242009-07-01 18:11:44 +0200703 return 0;
704 if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
705 /* stream is running, let's swap the current ADC */
Takashi Iwaif0cea792010-08-13 11:56:53 +0200706 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200707 spec->cur_adc = spec->adc_nid[idx];
708 snd_hda_codec_setup_stream(codec, spec->cur_adc,
709 spec->cur_adc_stream_tag, 0,
710 spec->cur_adc_format);
711 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200712 spec->cur_input = idx;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100713 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200714 return 1;
715}
716
717static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
718 struct snd_ctl_elem_info *uinfo)
719{
720 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
721 struct cs_spec *spec = codec->spec;
Takashi Iwai10a20af2010-09-09 16:28:02 +0200722 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200723 unsigned int idx;
724
725 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
726 uinfo->count = 1;
727 uinfo->value.enumerated.items = spec->num_inputs;
728 if (uinfo->value.enumerated.item >= spec->num_inputs)
729 uinfo->value.enumerated.item = spec->num_inputs - 1;
730 idx = spec->input_idx[uinfo->value.enumerated.item];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100731 snd_hda_get_pin_label(codec, cfg->inputs[idx].pin, cfg,
732 uinfo->value.enumerated.name,
733 sizeof(uinfo->value.enumerated.name), NULL);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200734 return 0;
735}
736
737static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
738 struct snd_ctl_elem_value *ucontrol)
739{
740 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
741 struct cs_spec *spec = codec->spec;
742 ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
743 return 0;
744}
745
746static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
747 struct snd_ctl_elem_value *ucontrol)
748{
749 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
750 struct cs_spec *spec = codec->spec;
751 unsigned int idx = ucontrol->value.enumerated.item[0];
752
753 if (idx >= spec->num_inputs)
754 return -EINVAL;
755 idx = spec->input_idx[idx];
Takashi Iwaiea359292009-07-06 12:58:59 +0200756 return change_cur_input(codec, idx, 0);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200757}
758
Takashi Iwaic42d4782011-05-02 11:36:09 +0200759static const struct snd_kcontrol_new cs_capture_source = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200760 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
761 .name = "Capture Source",
762 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
763 .info = cs_capture_source_info,
764 .get = cs_capture_source_get,
765 .put = cs_capture_source_put,
766};
767
Takashi Iwaic42d4782011-05-02 11:36:09 +0200768static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200769 struct hda_ctl_ops *ops)
770{
771 struct cs_spec *spec = codec->spec;
772 struct hda_bind_ctls *bind;
773 int i, n;
774
775 bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
776 GFP_KERNEL);
777 if (!bind)
778 return NULL;
779 bind->ops = ops;
780 n = 0;
781 for (i = 0; i < AUTO_PIN_LAST; i++) {
782 if (!spec->adc_nid[i])
783 continue;
784 bind->values[n++] =
785 HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
786 spec->adc_idx[i], HDA_INPUT);
787 }
788 return bind;
789}
790
Takashi Iwai6a929342010-10-11 15:16:20 +0200791/* add a (input-boost) volume control to the given input pin */
792static int add_input_volume_control(struct hda_codec *codec,
793 struct auto_pin_cfg *cfg,
794 int item)
795{
796 hda_nid_t pin = cfg->inputs[item].pin;
797 u32 caps;
798 const char *label;
799 struct snd_kcontrol *kctl;
800
801 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
802 return 0;
803 caps = query_amp_caps(codec, pin, HDA_INPUT);
804 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
805 if (caps <= 1)
806 return 0;
807 label = hda_get_autocfg_input_label(codec, cfg, item);
808 return add_volume(codec, label, 0,
809 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
810}
811
Takashi Iwaie5f14242009-07-01 18:11:44 +0200812static int build_input(struct hda_codec *codec)
813{
814 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200815 int i, err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200816
Takashi Iwaie5f14242009-07-01 18:11:44 +0200817 if (!spec->num_inputs)
818 return 0;
819
Takashi Iwaie5f14242009-07-01 18:11:44 +0200820 /* make bind-capture */
821 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
822 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
823 for (i = 0; i < 2; i++) {
824 struct snd_kcontrol *kctl;
Takashi Iwai21949f02009-12-23 08:31:59 +0100825 int n;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200826 if (!spec->capture_bind[i])
827 return -ENOMEM;
828 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
829 if (!kctl)
830 return -ENOMEM;
831 kctl->private_value = (long)spec->capture_bind[i];
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100832 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200833 if (err < 0)
834 return err;
Takashi Iwai21949f02009-12-23 08:31:59 +0100835 for (n = 0; n < AUTO_PIN_LAST; n++) {
836 if (!spec->adc_nid[n])
837 continue;
Brian J. Tarricone8dd34ab2010-05-02 17:32:10 -0700838 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
Takashi Iwai21949f02009-12-23 08:31:59 +0100839 if (err < 0)
840 return err;
841 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200842 }
843
844 if (spec->num_inputs > 1 && !spec->mic_detect) {
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +0100845 err = snd_hda_ctl_add(codec, 0,
Takashi Iwaie5f14242009-07-01 18:11:44 +0200846 snd_ctl_new1(&cs_capture_source, codec));
847 if (err < 0)
848 return err;
849 }
850
Takashi Iwai6a929342010-10-11 15:16:20 +0200851 for (i = 0; i < spec->num_inputs; i++) {
852 err = add_input_volume_control(codec, &spec->autocfg, i);
853 if (err < 0)
854 return err;
855 }
856
Takashi Iwaie5f14242009-07-01 18:11:44 +0200857 return 0;
858}
859
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200860/*
861 */
862
Takashi Iwaie5f14242009-07-01 18:11:44 +0200863static int build_digital_output(struct hda_codec *codec)
864{
865 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200866 int err;
867
Takashi Iwai63b24132009-07-09 11:45:59 +0200868 if (!spec->multiout.dig_out_nid)
869 return 0;
870
Stephen Warren74b654c2011-06-01 11:14:18 -0600871 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
872 spec->multiout.dig_out_nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200873 if (err < 0)
874 return err;
875 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
876 if (err < 0)
877 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200878 return 0;
879}
880
881static int build_digital_input(struct hda_codec *codec)
882{
883 struct cs_spec *spec = codec->spec;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200884 if (spec->dig_in)
885 return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
886 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200887}
888
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200889/*
890 * auto-mute and auto-mic switching
Tim Howe56487c22011-07-22 16:41:00 -0500891 * CS421x auto-output redirecting
892 * HP/SPK/SPDIF
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200893 */
894
Takashi Iwaie5f14242009-07-01 18:11:44 +0200895static void cs_automute(struct hda_codec *codec)
896{
897 struct cs_spec *spec = codec->spec;
898 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai06dec222011-05-17 10:00:16 +0200899 unsigned int hp_present;
Tim Howe56487c22011-07-22 16:41:00 -0500900 unsigned int spdif_present;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200901 hda_nid_t nid;
902 int i;
903
Tim Howe56487c22011-07-22 16:41:00 -0500904 spdif_present = 0;
905 if (cfg->dig_outs) {
906 nid = cfg->dig_out_pins[0];
907 if (is_jack_detectable(codec, nid)) {
908 /*
909 TODO: SPDIF output redirect when SENSE_B is enabled.
910 Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
911 assumed.
912 */
913 if (snd_hda_jack_detect(codec, nid)
914 /* && spec->sense_b */)
915 spdif_present = 1;
916 }
917 }
918
Takashi Iwaie5f14242009-07-01 18:11:44 +0200919 hp_present = 0;
920 for (i = 0; i < cfg->hp_outs; i++) {
921 nid = cfg->hp_pins[i];
Takashi Iwai06dec222011-05-17 10:00:16 +0200922 if (!is_jack_detectable(codec, nid))
Takashi Iwaie5f14242009-07-01 18:11:44 +0200923 continue;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800924 hp_present = snd_hda_jack_detect(codec, nid);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200925 if (hp_present)
926 break;
927 }
Tim Howe56487c22011-07-22 16:41:00 -0500928
929 /* mute speakers if spdif or hp jack is plugged in */
Takashi Iwaie5f14242009-07-01 18:11:44 +0200930 for (i = 0; i < cfg->speaker_outs; i++) {
David Henningsson78e2a922012-01-02 12:40:16 +0100931 int pin_ctl = hp_present ? 0 : PIN_OUT;
David Henningsson5660ffd2012-01-02 12:40:17 +0100932 /* detect on spdif is specific to CS4210 */
933 if (spdif_present && (spec->vendor_nid == CS4210_VENDOR_NID))
David Henningsson78e2a922012-01-02 12:40:16 +0100934 pin_ctl = 0;
935
Takashi Iwaie5f14242009-07-01 18:11:44 +0200936 nid = cfg->speaker_pins[i];
937 snd_hda_codec_write(codec, nid, 0,
David Henningsson78e2a922012-01-02 12:40:16 +0100938 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200939 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100940 if (spec->gpio_eapd_hp) {
941 unsigned int gpio = hp_present ?
942 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200943 snd_hda_codec_write(codec, 0x01, 0,
944 AC_VERB_SET_GPIO_DATA, gpio);
945 }
Tim Howe56487c22011-07-22 16:41:00 -0500946
David Henningsson5660ffd2012-01-02 12:40:17 +0100947 /* specific to CS4210 */
948 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500949 /* mute HPs if spdif jack (SENSE_B) is present */
950 for (i = 0; i < cfg->hp_outs; i++) {
951 nid = cfg->hp_pins[i];
952 snd_hda_codec_write(codec, nid, 0,
953 AC_VERB_SET_PIN_WIDGET_CONTROL,
954 (spdif_present && spec->sense_b) ? 0 : PIN_HP);
955 }
956
957 /* SPDIF TX on/off */
958 if (cfg->dig_outs) {
959 nid = cfg->dig_out_pins[0];
960 snd_hda_codec_write(codec, nid, 0,
961 AC_VERB_SET_PIN_WIDGET_CONTROL,
962 spdif_present ? PIN_OUT : 0);
963
964 }
965 /* Update board GPIOs if neccessary ... */
966 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200967}
968
Tim Howe56487c22011-07-22 16:41:00 -0500969/*
970 * Auto-input redirect for CS421x
971 * Switch max 3 inputs of a single ADC (nid 3)
972*/
973
Takashi Iwaie5f14242009-07-01 18:11:44 +0200974static void cs_automic(struct hda_codec *codec)
975{
976 struct cs_spec *spec = codec->spec;
977 struct auto_pin_cfg *cfg = &spec->autocfg;
978 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800979 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500980
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200981 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800982 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500983
984 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100985 if (spec->vendor_nid == CS420X_VENDOR_NID) {
986 if (present)
987 change_cur_input(codec, spec->automic_idx, 0);
988 else
989 change_cur_input(codec, !spec->automic_idx, 0);
990 } else {
Tim Howe56487c22011-07-22 16:41:00 -0500991 if (present) {
Dylan Reidf70eecd2012-01-31 13:04:41 -0800992 if (spec->cur_input != spec->automic_idx) {
993 spec->last_input = spec->cur_input;
994 spec->cur_input = spec->automic_idx;
995 }
Tim Howe56487c22011-07-22 16:41:00 -0500996 } else {
997 spec->cur_input = spec->last_input;
998 }
Takashi Iwai05ee7962011-11-16 18:05:11 +0100999 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001000 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001001}
1002
1003/*
1004 */
1005
1006static void init_output(struct hda_codec *codec)
1007{
1008 struct cs_spec *spec = codec->spec;
1009 struct auto_pin_cfg *cfg = &spec->autocfg;
1010 int i;
1011
1012 /* mute first */
1013 for (i = 0; i < spec->multiout.num_dacs; i++)
1014 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1015 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1016 if (spec->multiout.hp_nid)
1017 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1018 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1019 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1020 if (!spec->multiout.extra_out_nid[i])
1021 break;
1022 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1023 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1024 }
1025
1026 /* set appropriate pin controls */
1027 for (i = 0; i < cfg->line_outs; i++)
1028 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
1029 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001030 /* HP */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001031 for (i = 0; i < cfg->hp_outs; i++) {
1032 hda_nid_t nid = cfg->hp_pins[i];
1033 snd_hda_codec_write(codec, nid, 0,
1034 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1035 if (!cfg->speaker_outs)
1036 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001037 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001038 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001039 spec->hp_detect = 1;
1040 }
1041 }
Tim Howe56487c22011-07-22 16:41:00 -05001042
1043 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001044 for (i = 0; i < cfg->speaker_outs; i++)
1045 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
1046 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001047
1048 /* SPDIF is enabled on presence detect for CS421x */
1049 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001050 cs_automute(codec);
1051}
1052
1053static void init_input(struct hda_codec *codec)
1054{
1055 struct cs_spec *spec = codec->spec;
1056 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001057 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001058 int i;
1059
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001060 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001061 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001062 hda_nid_t pin = cfg->inputs[i].pin;
1063 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001064 continue;
1065 /* set appropriate pin control and mute first */
1066 ctl = PIN_IN;
Takashi Iwai86e29592010-09-09 14:50:17 +02001067 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001068 unsigned int caps = snd_hda_query_pin_caps(codec, pin);
1069 caps >>= AC_PINCAP_VREF_SHIFT;
1070 if (caps & AC_PINCAP_VREF_80)
1071 ctl = PIN_VREF80;
1072 }
1073 snd_hda_codec_write(codec, pin, 0,
1074 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
1075 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1076 AC_VERB_SET_AMP_GAIN_MUTE,
1077 AMP_IN_MUTE(spec->adc_idx[i]));
1078 if (spec->mic_detect && spec->automic_idx == i)
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001079 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001080 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001081 /* CS420x has multiple ADC, CS421x has single ADC */
1082 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001083 change_cur_input(codec, spec->cur_input, 1);
1084 if (spec->mic_detect)
1085 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001086
Tim Howe56487c22011-07-22 16:41:00 -05001087 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
Daniel J Blueman1289a3e2012-11-04 13:19:03 +08001088 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
1089
1090 coef = cs_vendor_coef_get(codec, IDX_BEEP_CFG);
Tim Howe56487c22011-07-22 16:41:00 -05001091 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
Daniel J Blueman1289a3e2012-11-04 13:19:03 +08001092 coef |= 1 << 4; /* DMIC2 2 chan on, GPIO1 off */
Tim Howe56487c22011-07-22 16:41:00 -05001093 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
Daniel J Blueman1289a3e2012-11-04 13:19:03 +08001094 coef |= 1 << 3; /* DMIC1 2 chan on, GPIO0 off
Tim Howe56487c22011-07-22 16:41:00 -05001095 * No effect if SPDIF_OUT2 is
1096 * selected in IDX_SPDIF_CTL.
1097 */
Daniel J Blueman1289a3e2012-11-04 13:19:03 +08001098
1099 cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001100 } else {
1101 if (spec->mic_detect)
1102 cs_automic(codec);
1103 else {
1104 spec->cur_adc = spec->adc_nid[spec->cur_input];
1105 cs_update_input_select(codec);
1106 }
Tim Howe56487c22011-07-22 16:41:00 -05001107 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001108}
1109
Takashi Iwaic42d4782011-05-02 11:36:09 +02001110static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001111 {0x11, AC_VERB_SET_PROC_STATE, 1},
1112 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1113 {0x11, AC_VERB_SET_PROC_COEF,
1114 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1115 | 0x0040 /* Mute DACs on FIFO error */
1116 | 0x1000 /* Enable DACs High Pass Filter */
1117 | 0x0400 /* Disable Coefficient Auto increment */
1118 )},
1119 /* Beep */
Alexander Stein2a1d20c2012-11-01 13:42:37 +01001120 {0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001121 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1122
1123 {} /* terminator */
1124};
1125
Brian Austina769cbc2010-09-07 14:36:22 -05001126/* Errata: CS4207 rev C0/C1/C2 Silicon
1127 *
1128 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1129 *
1130 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1131 * may be excessive (up to an additional 200 μA), which is most easily
1132 * observed while the part is being held in reset (RESET# active low).
1133 *
1134 * Root Cause: At initial powerup of the device, the logic that drives
1135 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1136 * initialized.
1137 * Certain random patterns will cause a steady leakage current in those
1138 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1139 *
1140 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1141 * blocks, which will alleviate the issue.
1142 */
1143
Takashi Iwaic42d4782011-05-02 11:36:09 +02001144static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001145 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1146 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1147
1148 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1149 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1150 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1151 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1152 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1153 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1154
1155 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1156 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1157
1158 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1159 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1160 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1161 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1162 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1163 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1164 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1165
Takashi Iwai38c07642011-03-03 14:54:19 +01001166#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001167 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1168 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1169 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001170#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001171
1172 {} /* terminator */
1173};
1174
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001175/* SPDIF setup */
1176static void init_digital(struct hda_codec *codec)
1177{
1178 unsigned int coef;
1179
1180 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1181 coef |= 0x0008; /* Replace with mute on error */
1182 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1183 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1184 * SPDIF_OUT2 is shared with GPIO1 and
1185 * DMIC_SDA2.
1186 */
1187 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001188}
1189
1190static int cs_init(struct hda_codec *codec)
1191{
1192 struct cs_spec *spec = codec->spec;
1193
Brian Austina769cbc2010-09-07 14:36:22 -05001194 /* init_verb sequence for C0/C1/C2 errata*/
1195 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1196
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001197 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001198
1199 if (spec->gpio_mask) {
1200 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1201 spec->gpio_mask);
1202 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1203 spec->gpio_dir);
1204 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1205 spec->gpio_data);
1206 }
1207
Takashi Iwaie5f14242009-07-01 18:11:44 +02001208 init_output(codec);
1209 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001210 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001211 snd_hda_jack_report_sync(codec);
1212
Takashi Iwaie5f14242009-07-01 18:11:44 +02001213 return 0;
1214}
1215
1216static int cs_build_controls(struct hda_codec *codec)
1217{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001218 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001219 int err;
1220
1221 err = build_output(codec);
1222 if (err < 0)
1223 return err;
1224 err = build_input(codec);
1225 if (err < 0)
1226 return err;
1227 err = build_digital_output(codec);
1228 if (err < 0)
1229 return err;
1230 err = build_digital_input(codec);
1231 if (err < 0)
1232 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001233 err = cs_init(codec);
1234 if (err < 0)
1235 return err;
1236
1237 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1238 if (err < 0)
1239 return err;
1240
1241 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001242}
1243
1244static void cs_free(struct hda_codec *codec)
1245{
1246 struct cs_spec *spec = codec->spec;
1247 kfree(spec->capture_bind[0]);
1248 kfree(spec->capture_bind[1]);
1249 kfree(codec->spec);
1250}
1251
1252static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1253{
Takashi Iwai3a938972011-10-28 01:16:55 +02001254 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001255 case HP_EVENT:
1256 cs_automute(codec);
1257 break;
1258 case MIC_EVENT:
1259 cs_automic(codec);
1260 break;
1261 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001262 snd_hda_jack_report_sync(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001263}
1264
Takashi Iwaic42d4782011-05-02 11:36:09 +02001265static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001266 .build_controls = cs_build_controls,
1267 .build_pcms = cs_build_pcms,
1268 .init = cs_init,
1269 .free = cs_free,
1270 .unsol_event = cs_unsol_event,
1271};
1272
1273static int cs_parse_auto_config(struct hda_codec *codec)
1274{
1275 struct cs_spec *spec = codec->spec;
1276 int err;
1277
1278 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1279 if (err < 0)
1280 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001281
1282 err = parse_output(codec);
1283 if (err < 0)
1284 return err;
1285 err = parse_input(codec);
1286 if (err < 0)
1287 return err;
1288 err = parse_digital_output(codec);
1289 if (err < 0)
1290 return err;
1291 err = parse_digital_input(codec);
1292 if (err < 0)
1293 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001294 return 0;
1295}
1296
Takashi Iwaiea734962011-01-17 11:29:34 +01001297static const char * const cs420x_models[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001298 [CS420X_MBP53] = "mbp53",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001299 [CS420X_MBP55] = "mbp55",
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001300 [CS420X_IMAC27] = "imac27",
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001301 [CS420X_IMAC27_122] = "imac27_122",
Dan Carpenter92bb43e2011-11-24 14:48:24 +03001302 [CS420X_APPLE] = "apple",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001303 [CS420X_AUTO] = "auto",
1304};
1305
1306
Takashi Iwaic42d4782011-05-02 11:36:09 +02001307static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001308 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001309 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001310 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001311 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001312 /* this conflicts with too many other models */
1313 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1314 {} /* terminator */
1315};
1316
1317static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001318 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001319 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001320 {} /* terminator */
1321};
1322
1323struct cs_pincfg {
1324 hda_nid_t nid;
1325 u32 val;
1326};
1327
Takashi Iwaic42d4782011-05-02 11:36:09 +02001328static const struct cs_pincfg mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001329 { 0x09, 0x012b4050 },
1330 { 0x0a, 0x90100141 },
1331 { 0x0b, 0x90100140 },
1332 { 0x0c, 0x018b3020 },
1333 { 0x0d, 0x90a00110 },
1334 { 0x0e, 0x400000f0 },
1335 { 0x0f, 0x01cbe030 },
1336 { 0x10, 0x014be060 },
1337 { 0x12, 0x400000f0 },
1338 { 0x15, 0x400000f0 },
1339 {} /* terminator */
1340};
1341
Takashi Iwaic42d4782011-05-02 11:36:09 +02001342static const struct cs_pincfg mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001343 { 0x09, 0x012b4030 },
1344 { 0x0a, 0x90100121 },
1345 { 0x0b, 0x90100120 },
1346 { 0x0c, 0x400000f0 },
1347 { 0x0d, 0x90a00110 },
1348 { 0x0e, 0x400000f0 },
1349 { 0x0f, 0x400000f0 },
1350 { 0x10, 0x014be040 },
1351 { 0x12, 0x400000f0 },
1352 { 0x15, 0x400000f0 },
1353 {} /* terminator */
1354};
1355
Takashi Iwaic42d4782011-05-02 11:36:09 +02001356static const struct cs_pincfg imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001357 { 0x09, 0x012b4050 },
1358 { 0x0a, 0x90100140 },
1359 { 0x0b, 0x90100142 },
1360 { 0x0c, 0x018b3020 },
1361 { 0x0d, 0x90a00110 },
1362 { 0x0e, 0x400000f0 },
1363 { 0x0f, 0x01cbe030 },
1364 { 0x10, 0x014be060 },
1365 { 0x12, 0x01ab9070 },
1366 { 0x15, 0x400000f0 },
1367 {} /* terminator */
1368};
1369
Takashi Iwaic42d4782011-05-02 11:36:09 +02001370static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001371 [CS420X_MBP53] = mbp53_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001372 [CS420X_MBP55] = mbp55_pincfgs,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001373 [CS420X_IMAC27] = imac27_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001374};
1375
Tim Howe56487c22011-07-22 16:41:00 -05001376static void fix_pincfg(struct hda_codec *codec, int model,
1377 const struct cs_pincfg **pin_configs)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001378{
Tim Howe56487c22011-07-22 16:41:00 -05001379 const struct cs_pincfg *cfg = pin_configs[model];
Takashi Iwaia6bae202009-07-06 15:15:22 +02001380 if (!cfg)
1381 return;
1382 for (; cfg->nid; cfg++)
1383 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1384}
1385
Takashi Iwaie5f14242009-07-01 18:11:44 +02001386static int patch_cs420x(struct hda_codec *codec)
1387{
1388 struct cs_spec *spec;
1389 int err;
1390
1391 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1392 if (!spec)
1393 return -ENOMEM;
1394 codec->spec = spec;
1395
Tim Howe56487c22011-07-22 16:41:00 -05001396 spec->vendor_nid = CS420X_VENDOR_NID;
1397
Takashi Iwaia6bae202009-07-06 15:15:22 +02001398 spec->board_config =
1399 snd_hda_check_board_config(codec, CS420X_MODELS,
1400 cs420x_models, cs420x_cfg_tbl);
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001401 if (spec->board_config < 0)
1402 spec->board_config =
1403 snd_hda_check_board_codec_sid_config(codec,
1404 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001405 if (spec->board_config >= 0)
Tim Howe56487c22011-07-22 16:41:00 -05001406 fix_pincfg(codec, spec->board_config, cs_pincfgs);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001407
Takashi Iwaied208252009-07-07 09:04:26 +02001408 switch (spec->board_config) {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001409 case CS420X_IMAC27:
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001410 case CS420X_MBP53:
Takashi Iwaied208252009-07-07 09:04:26 +02001411 case CS420X_MBP55:
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001412 case CS420X_APPLE:
1413 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1414 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1415 spec->gpio_mask = spec->gpio_dir =
1416 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
Takashi Iwaied208252009-07-07 09:04:26 +02001417 break;
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001418 case CS420X_IMAC27_122:
1419 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1420 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1421 spec->gpio_mask = spec->gpio_dir =
1422 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1423 break;
Takashi Iwaied208252009-07-07 09:04:26 +02001424 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001425
Takashi Iwaied208252009-07-07 09:04:26 +02001426 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001427 if (err < 0)
1428 goto error;
1429
Takashi Iwaie5f14242009-07-01 18:11:44 +02001430 codec->patch_ops = cs_patch_ops;
1431
1432 return 0;
1433
1434 error:
Takashi Iwai531db9f2012-10-10 08:50:35 +02001435 cs_free(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001436 codec->spec = NULL;
1437 return err;
1438}
1439
Tim Howe56487c22011-07-22 16:41:00 -05001440/*
1441 * Cirrus Logic CS4210
1442 *
1443 * 1 DAC => HP(sense) / Speakers,
1444 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1445 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1446*/
1447
1448/* CS4210 board names */
1449static const char *cs421x_models[CS421X_MODELS] = {
1450 [CS421X_CDB4210] = "cdb4210",
1451};
1452
1453static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1454 /* Test Intel board + CDB2410 */
1455 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1456 {} /* terminator */
1457};
1458
1459/* CS4210 board pinconfigs */
1460/* Default CS4210 (CDB4210)*/
1461static const struct cs_pincfg cdb4210_pincfgs[] = {
1462 { 0x05, 0x0321401f },
1463 { 0x06, 0x90170010 },
1464 { 0x07, 0x03813031 },
1465 { 0x08, 0xb7a70037 },
1466 { 0x09, 0xb7a6003e },
1467 { 0x0a, 0x034510f0 },
1468 {} /* terminator */
1469};
1470
1471static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1472 [CS421X_CDB4210] = cdb4210_pincfgs,
1473};
1474
1475static const struct hda_verb cs421x_coef_init_verbs[] = {
1476 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1477 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1478 /*
1479 Disable Coefficient Index Auto-Increment(DAI)=1,
1480 PDREF=0
1481 */
1482 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1483
1484 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1485 /* ADC SZCMode = Digital Soft Ramp */
1486 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1487
1488 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1489 {0x0B, AC_VERB_SET_PROC_COEF,
1490 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1491 | 0x0004 /* Mute DAC on FIFO error */
1492 | 0x0008 /* Enable DAC High Pass Filter */
1493 )},
1494 {} /* terminator */
1495};
1496
1497/* Errata: CS4210 rev A1 Silicon
1498 *
1499 * http://www.cirrus.com/en/pubs/errata/
1500 *
1501 * Description:
1502 * 1. Performance degredation is present in the ADC.
1503 * 2. Speaker output is not completely muted upon HP detect.
1504 * 3. Noise is present when clipping occurs on the amplified
1505 * speaker outputs.
1506 *
1507 * Workaround:
1508 * The following verb sequence written to the registers during
1509 * initialization will correct the issues listed above.
1510 */
1511
1512static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1513 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1514
1515 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1516 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1517
1518 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1519 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1520
1521 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1522 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1523
1524 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1525 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1526
1527 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1528 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1529
1530 {} /* terminator */
1531};
1532
1533/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1534static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1535
1536static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1537 struct snd_ctl_elem_info *uinfo)
1538{
1539 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1540 uinfo->count = 1;
1541 uinfo->value.integer.min = 0;
1542 uinfo->value.integer.max = 3;
1543 return 0;
1544}
1545
1546static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1547 struct snd_ctl_elem_value *ucontrol)
1548{
1549 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1550
1551 ucontrol->value.integer.value[0] =
1552 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1553 return 0;
1554}
1555
1556static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1557 struct snd_ctl_elem_value *ucontrol)
1558{
1559 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1560
1561 unsigned int vol = ucontrol->value.integer.value[0];
1562 unsigned int coef =
1563 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1564 unsigned int original_coef = coef;
1565
1566 coef &= ~0x0003;
1567 coef |= (vol & 0x0003);
1568 if (original_coef == coef)
1569 return 0;
1570 else {
1571 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1572 return 1;
1573 }
1574}
1575
1576static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1577
1578 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1579 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1580 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1581 .name = "Speaker Boost Playback Volume",
1582 .info = cs421x_boost_vol_info,
1583 .get = cs421x_boost_vol_get,
1584 .put = cs421x_boost_vol_put,
1585 .tlv = { .p = cs421x_speaker_boost_db_scale },
1586};
1587
David Henningsson5660ffd2012-01-02 12:40:17 +01001588static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001589{
1590 struct cs_spec *spec = codec->spec;
1591 unsigned int def_conf, coef;
1592
1593 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1594 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1595
1596 if (spec->gpio_mask)
1597 coef |= 0x0008; /* B1,B2 are GPIOs */
1598 else
1599 coef &= ~0x0008;
1600
1601 if (spec->sense_b)
1602 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1603 else
1604 coef &= ~0x0010;
1605
1606 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1607
1608 if ((spec->gpio_mask || spec->sense_b) &&
1609 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1610
1611 /*
1612 GPIO or SENSE_B forced - disconnect the DMIC pin.
1613 */
1614 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1615 def_conf &= ~AC_DEFCFG_PORT_CONN;
1616 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1617 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1618 }
1619}
1620
1621static void init_cs421x_digital(struct hda_codec *codec)
1622{
1623 struct cs_spec *spec = codec->spec;
1624 struct auto_pin_cfg *cfg = &spec->autocfg;
1625 int i;
1626
1627
1628 for (i = 0; i < cfg->dig_outs; i++) {
1629 hda_nid_t nid = cfg->dig_out_pins[i];
1630 if (!cfg->speaker_outs)
1631 continue;
1632 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001633 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
Tim Howe56487c22011-07-22 16:41:00 -05001634 spec->spdif_detect = 1;
1635 }
1636 }
1637}
1638
1639static int cs421x_init(struct hda_codec *codec)
1640{
1641 struct cs_spec *spec = codec->spec;
1642
David Henningsson5660ffd2012-01-02 12:40:17 +01001643 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1644 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1645 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1646 cs4210_pinmux_init(codec);
1647 }
Tim Howe56487c22011-07-22 16:41:00 -05001648
1649 if (spec->gpio_mask) {
1650 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1651 spec->gpio_mask);
1652 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1653 spec->gpio_dir);
1654 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1655 spec->gpio_data);
1656 }
1657
1658 init_output(codec);
1659 init_input(codec);
1660 init_cs421x_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001661 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001662
1663 return 0;
1664}
1665
1666/*
1667 * CS4210 Input MUX (1 ADC)
1668 */
1669static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1670 struct snd_ctl_elem_info *uinfo)
1671{
1672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1673 struct cs_spec *spec = codec->spec;
1674
1675 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1676}
1677
1678static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1679 struct snd_ctl_elem_value *ucontrol)
1680{
1681 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1682 struct cs_spec *spec = codec->spec;
1683
1684 ucontrol->value.enumerated.item[0] = spec->cur_input;
1685 return 0;
1686}
1687
1688static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1689 struct snd_ctl_elem_value *ucontrol)
1690{
1691 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1692 struct cs_spec *spec = codec->spec;
1693
1694 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1695 spec->adc_nid[0], &spec->cur_input);
1696
1697}
1698
1699static struct snd_kcontrol_new cs421x_capture_source = {
1700
1701 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1702 .name = "Capture Source",
1703 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1704 .info = cs421x_mux_enum_info,
1705 .get = cs421x_mux_enum_get,
1706 .put = cs421x_mux_enum_put,
1707};
1708
1709static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1710{
1711 struct cs_spec *spec = codec->spec;
1712 struct auto_pin_cfg *cfg = &spec->autocfg;
1713 const struct hda_input_mux *imux = &spec->input_mux;
1714 hda_nid_t pin = cfg->inputs[item].pin;
1715 struct snd_kcontrol *kctl;
1716 u32 caps;
1717
1718 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1719 return 0;
1720
1721 caps = query_amp_caps(codec, pin, HDA_INPUT);
1722 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1723 if (caps <= 1)
1724 return 0;
1725
1726 return add_volume(codec, imux->items[item].label, 0,
1727 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1728}
1729
1730/* add a (input-boost) volume control to the given input pin */
1731static int build_cs421x_input(struct hda_codec *codec)
1732{
1733 struct cs_spec *spec = codec->spec;
1734 struct auto_pin_cfg *cfg = &spec->autocfg;
1735 struct hda_input_mux *imux = &spec->input_mux;
1736 int i, err, type_idx;
1737 const char *label;
1738
1739 if (!spec->num_inputs)
1740 return 0;
1741
1742 /* make bind-capture */
1743 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1744 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1745 for (i = 0; i < 2; i++) {
1746 struct snd_kcontrol *kctl;
1747 int n;
1748 if (!spec->capture_bind[i])
1749 return -ENOMEM;
1750 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1751 if (!kctl)
1752 return -ENOMEM;
1753 kctl->private_value = (long)spec->capture_bind[i];
1754 err = snd_hda_ctl_add(codec, 0, kctl);
1755 if (err < 0)
1756 return err;
1757 for (n = 0; n < AUTO_PIN_LAST; n++) {
1758 if (!spec->adc_nid[n])
1759 continue;
1760 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1761 if (err < 0)
1762 return err;
1763 }
1764 }
1765
1766 /* Add Input MUX Items + Capture Volume/Switch */
1767 for (i = 0; i < spec->num_inputs; i++) {
1768 label = hda_get_autocfg_input_label(codec, cfg, i);
1769 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1770
1771 err = cs421x_add_input_volume_control(codec, i);
1772 if (err < 0)
1773 return err;
1774 }
1775
1776 /*
1777 Add 'Capture Source' Switch if
1778 * 2 inputs and no mic detec
1779 * 3 inputs
1780 */
1781 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1782 (spec->num_inputs == 3)) {
1783
1784 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1785 snd_ctl_new1(&cs421x_capture_source, codec));
1786 if (err < 0)
1787 return err;
1788 }
1789
1790 return 0;
1791}
1792
1793/* Single DAC (Mute/Gain) */
1794static int build_cs421x_output(struct hda_codec *codec)
1795{
1796 hda_nid_t dac = CS4210_DAC_NID;
1797 struct cs_spec *spec = codec->spec;
1798 struct auto_pin_cfg *cfg = &spec->autocfg;
1799 struct snd_kcontrol *kctl;
1800 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001801 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001802
1803 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001804
1805 err = add_mute(codec, name, 0,
1806 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1807 if (err < 0)
1808 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001809
1810 err = add_volume(codec, name, 0,
1811 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1812 if (err < 0)
1813 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001814
David Henningsson5660ffd2012-01-02 12:40:17 +01001815 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001816 err = snd_hda_ctl_add(codec, 0,
1817 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1818 if (err < 0)
1819 return err;
1820 }
1821 return err;
1822}
1823
1824static int cs421x_build_controls(struct hda_codec *codec)
1825{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001826 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001827 int err;
1828
1829 err = build_cs421x_output(codec);
1830 if (err < 0)
1831 return err;
1832 err = build_cs421x_input(codec);
1833 if (err < 0)
1834 return err;
1835 err = build_digital_output(codec);
1836 if (err < 0)
1837 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001838 err = cs421x_init(codec);
1839 if (err < 0)
1840 return err;
1841
1842 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1843 if (err < 0)
1844 return err;
1845
1846 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001847}
1848
1849static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1850{
Takashi Iwai3a938972011-10-28 01:16:55 +02001851 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Tim Howe56487c22011-07-22 16:41:00 -05001852 case HP_EVENT:
1853 case SPDIF_EVENT:
1854 cs_automute(codec);
1855 break;
1856
1857 case MIC_EVENT:
1858 cs_automic(codec);
1859 break;
1860 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001861 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001862}
1863
1864static int parse_cs421x_input(struct hda_codec *codec)
1865{
1866 struct cs_spec *spec = codec->spec;
1867 struct auto_pin_cfg *cfg = &spec->autocfg;
1868 int i;
1869
1870 for (i = 0; i < cfg->num_inputs; i++) {
1871 hda_nid_t pin = cfg->inputs[i].pin;
1872 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1873 spec->cur_input = spec->last_input = i;
1874 spec->num_inputs++;
1875
1876 /* check whether the automatic mic switch is available */
1877 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1878 spec->mic_detect = 1;
1879 spec->automic_idx = i;
1880 }
1881 }
1882 return 0;
1883}
1884
1885static int cs421x_parse_auto_config(struct hda_codec *codec)
1886{
1887 struct cs_spec *spec = codec->spec;
1888 int err;
1889
1890 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1891 if (err < 0)
1892 return err;
1893 err = parse_output(codec);
1894 if (err < 0)
1895 return err;
1896 err = parse_cs421x_input(codec);
1897 if (err < 0)
1898 return err;
1899 err = parse_digital_output(codec);
1900 if (err < 0)
1901 return err;
1902 return 0;
1903}
1904
1905#ifdef CONFIG_PM
1906/*
1907 Manage PDREF, when transitioning to D3hot
1908 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1909*/
1910static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1911{
David Henningsson5660ffd2012-01-02 12:40:17 +01001912 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001913 unsigned int coef;
1914
1915 snd_hda_shutup_pins(codec);
1916
1917 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1918 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1919 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1920 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1921
David Henningsson5660ffd2012-01-02 12:40:17 +01001922 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1923 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1924 coef |= 0x0004; /* PDREF */
1925 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1926 }
Tim Howe56487c22011-07-22 16:41:00 -05001927
1928 return 0;
1929}
1930#endif
1931
David Henningsson5660ffd2012-01-02 12:40:17 +01001932static struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001933 .build_controls = cs421x_build_controls,
1934 .build_pcms = cs_build_pcms,
1935 .init = cs421x_init,
1936 .free = cs_free,
1937 .unsol_event = cs421x_unsol_event,
1938#ifdef CONFIG_PM
1939 .suspend = cs421x_suspend,
1940#endif
1941};
1942
David Henningsson5660ffd2012-01-02 12:40:17 +01001943static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001944{
1945 struct cs_spec *spec;
1946 int err;
1947
1948 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1949 if (!spec)
1950 return -ENOMEM;
1951 codec->spec = spec;
1952
David Henningsson5660ffd2012-01-02 12:40:17 +01001953 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001954
1955 spec->board_config =
1956 snd_hda_check_board_config(codec, CS421X_MODELS,
1957 cs421x_models, cs421x_cfg_tbl);
1958 if (spec->board_config >= 0)
1959 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1960 /*
1961 Setup GPIO/SENSE for each board (if used)
1962 */
1963 switch (spec->board_config) {
1964 case CS421X_CDB4210:
1965 snd_printd("CS4210 board: %s\n",
1966 cs421x_models[spec->board_config]);
1967/* spec->gpio_mask = 3;
1968 spec->gpio_dir = 3;
1969 spec->gpio_data = 3;
1970*/
1971 spec->sense_b = 1;
1972
1973 break;
1974 }
1975
1976 /*
1977 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1978 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1979 is disabled.
1980 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001981 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001982
1983 err = cs421x_parse_auto_config(codec);
1984 if (err < 0)
1985 goto error;
1986
David Henningsson5660ffd2012-01-02 12:40:17 +01001987 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05001988
1989 return 0;
1990
1991 error:
Takashi Iwai531db9f2012-10-10 08:50:35 +02001992 cs_free(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001993 codec->spec = NULL;
1994 return err;
1995}
1996
David Henningsson5660ffd2012-01-02 12:40:17 +01001997static int patch_cs4213(struct hda_codec *codec)
1998{
1999 struct cs_spec *spec;
2000 int err;
2001
2002 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2003 if (!spec)
2004 return -ENOMEM;
2005 codec->spec = spec;
2006
2007 spec->vendor_nid = CS4213_VENDOR_NID;
2008
2009 err = cs421x_parse_auto_config(codec);
2010 if (err < 0)
2011 goto error;
2012
2013 codec->patch_ops = cs421x_patch_ops;
2014 return 0;
2015
2016 error:
Takashi Iwai531db9f2012-10-10 08:50:35 +02002017 cs_free(codec);
David Henningsson5660ffd2012-01-02 12:40:17 +01002018 codec->spec = NULL;
2019 return err;
2020}
2021
Takashi Iwaie5f14242009-07-01 18:11:44 +02002022
2023/*
2024 * patch entries
2025 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002026static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002027 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2028 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002029 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2030 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002031 {} /* terminator */
2032};
2033
2034MODULE_ALIAS("snd-hda-codec-id:10134206");
2035MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002036MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002037MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002038
2039MODULE_LICENSE("GPL");
2040MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2041
2042static struct hda_codec_preset_list cirrus_list = {
2043 .preset = snd_hda_preset_cirrus,
2044 .owner = THIS_MODULE,
2045};
2046
2047static int __init patch_cirrus_init(void)
2048{
2049 return snd_hda_add_codec_preset(&cirrus_list);
2050}
2051
2052static void __exit patch_cirrus_exit(void)
2053{
2054 snd_hda_delete_codec_preset(&cirrus_list);
2055}
2056
2057module_init(patch_cirrus_init)
2058module_exit(patch_cirrus_exit)