blob: 9647ed4d7929175452ea77eee81677342264104f [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 Iwai128bc4b2012-05-07 17:42:31 +020029#include "hda_auto_parser.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020030#include "hda_jack.h"
Tim Howe56487c22011-07-22 16:41:00 -050031#include <sound/tlv.h>
Takashi Iwaie5f14242009-07-01 18:11:44 +020032
33/*
34 */
35
36struct cs_spec {
Takashi Iwaia6bae202009-07-06 15:15:22 +020037 int board_config;
Takashi Iwaie5f14242009-07-01 18:11:44 +020038 struct auto_pin_cfg autocfg;
39 struct hda_multi_out multiout;
40 struct snd_kcontrol *vmaster_sw;
41 struct snd_kcontrol *vmaster_vol;
42
43 hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
44 hda_nid_t slave_dig_outs[2];
45
46 unsigned int input_idx[AUTO_PIN_LAST];
47 unsigned int capsrc_idx[AUTO_PIN_LAST];
48 hda_nid_t adc_nid[AUTO_PIN_LAST];
49 unsigned int adc_idx[AUTO_PIN_LAST];
50 unsigned int num_inputs;
51 unsigned int cur_input;
52 unsigned int automic_idx;
53 hda_nid_t cur_adc;
54 unsigned int cur_adc_stream_tag;
55 unsigned int cur_adc_format;
56 hda_nid_t dig_in;
57
Takashi Iwaic42d4782011-05-02 11:36:09 +020058 const struct hda_bind_ctls *capture_bind[2];
Takashi Iwaie5f14242009-07-01 18:11:44 +020059
Takashi Iwaied208252009-07-07 09:04:26 +020060 unsigned int gpio_mask;
61 unsigned int gpio_dir;
62 unsigned int gpio_data;
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010063 unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
64 unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
Takashi Iwaied208252009-07-07 09:04:26 +020065
Takashi Iwaie5f14242009-07-01 18:11:44 +020066 struct hda_pcm pcm_rec[2]; /* PCM information */
67
68 unsigned int hp_detect:1;
69 unsigned int mic_detect:1;
Tim Howe56487c22011-07-22 16:41:00 -050070 /* CS421x */
71 unsigned int spdif_detect:1;
72 unsigned int sense_b:1;
73 hda_nid_t vendor_nid;
74 struct hda_input_mux input_mux;
75 unsigned int last_input;
Takashi Iwaie5f14242009-07-01 18:11:44 +020076};
77
Tim Howe56487c22011-07-22 16:41:00 -050078/* available models with CS420x */
Takashi Iwaia6bae202009-07-06 15:15:22 +020079enum {
Vince Weaver4e7d7c62010-09-22 17:31:37 -040080 CS420X_MBP53,
Takashi Iwaia6bae202009-07-06 15:15:22 +020081 CS420X_MBP55,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +010082 CS420X_IMAC27,
Jérémy Lal7e5bea12012-01-09 17:19:45 +010083 CS420X_IMAC27_122,
Takashi Iwai6dfeb7032011-11-22 20:00:31 +010084 CS420X_APPLE,
Takashi Iwaia6bae202009-07-06 15:15:22 +020085 CS420X_AUTO,
86 CS420X_MODELS
87};
88
Tim Howe56487c22011-07-22 16:41:00 -050089/* CS421x boards */
90enum {
91 CS421X_CDB4210,
92 CS421X_MODELS
93};
94
Takashi Iwai40c20fa2009-07-06 13:00:57 +020095/* Vendor-specific processing widget */
96#define CS420X_VENDOR_NID 0x11
97#define CS_DIG_OUT1_PIN_NID 0x10
98#define CS_DIG_OUT2_PIN_NID 0x15
99#define CS_DMIC1_PIN_NID 0x12
100#define CS_DMIC2_PIN_NID 0x0e
101
102/* coef indices */
103#define IDX_SPDIF_STAT 0x0000
104#define IDX_SPDIF_CTL 0x0001
105#define IDX_ADC_CFG 0x0002
106/* SZC bitmask, 4 modes below:
107 * 0 = immediate,
108 * 1 = digital immediate, analog zero-cross
109 * 2 = digtail & analog soft-ramp
110 * 3 = digital soft-ramp, analog zero-cross
111 */
112#define CS_COEF_ADC_SZC_MASK (3 << 0)
113#define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
114#define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
115/* PGA mode: 0 = differential, 1 = signle-ended */
116#define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
117#define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
118#define IDX_DAC_CFG 0x0003
119/* SZC bitmask, 4 modes below:
120 * 0 = Immediate
121 * 1 = zero-cross
122 * 2 = soft-ramp
123 * 3 = soft-ramp on zero-cross
124 */
125#define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
126#define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
127#define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
128
129#define IDX_BEEP_CFG 0x0004
130/* 0x0008 - test reg key */
131/* 0x0009 - 0x0014 -> 12 test regs */
132/* 0x0015 - visibility reg */
133
Tim Howe56487c22011-07-22 16:41:00 -0500134/*
135 * Cirrus Logic CS4210
136 *
137 * 1 DAC => HP(sense) / Speakers,
138 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
139 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
140*/
141#define CS4210_DAC_NID 0x02
142#define CS4210_ADC_NID 0x03
David Henningsson5660ffd2012-01-02 12:40:17 +0100143#define CS4210_VENDOR_NID 0x0B
Tim Howe56487c22011-07-22 16:41:00 -0500144#define CS421X_DMIC_PIN_NID 0x09 /* Port E */
145#define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
146
147#define CS421X_IDX_DEV_CFG 0x01
148#define CS421X_IDX_ADC_CFG 0x02
149#define CS421X_IDX_DAC_CFG 0x03
150#define CS421X_IDX_SPK_CTL 0x04
151
152#define SPDIF_EVENT 0x04
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200153
David Henningsson5660ffd2012-01-02 12:40:17 +0100154/* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
155#define CS4213_VENDOR_NID 0x09
156
157
Takashi Iwai277a57c2009-07-08 12:42:08 +0200158static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200159{
Tim Howe56487c22011-07-22 16:41:00 -0500160 struct cs_spec *spec = codec->spec;
161 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200162 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500163 return snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200164 AC_VERB_GET_PROC_COEF, 0);
165}
166
Takashi Iwai277a57c2009-07-08 12:42:08 +0200167static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
168 unsigned int coef)
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200169{
Tim Howe56487c22011-07-22 16:41:00 -0500170 struct cs_spec *spec = codec->spec;
171 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200172 AC_VERB_SET_COEF_INDEX, idx);
Tim Howe56487c22011-07-22 16:41:00 -0500173 snd_hda_codec_write(codec, spec->vendor_nid, 0,
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200174 AC_VERB_SET_PROC_COEF, coef);
175}
176
177
Takashi Iwaie5f14242009-07-01 18:11:44 +0200178#define HP_EVENT 1
179#define MIC_EVENT 2
180
181/*
182 * PCM callbacks
183 */
184static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
185 struct hda_codec *codec,
186 struct snd_pcm_substream *substream)
187{
188 struct cs_spec *spec = codec->spec;
189 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
190 hinfo);
191}
192
193static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
194 struct hda_codec *codec,
195 unsigned int stream_tag,
196 unsigned int format,
197 struct snd_pcm_substream *substream)
198{
199 struct cs_spec *spec = codec->spec;
200 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
201 stream_tag, format, substream);
202}
203
204static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
205 struct hda_codec *codec,
206 struct snd_pcm_substream *substream)
207{
208 struct cs_spec *spec = codec->spec;
209 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
210}
211
212/*
213 * Digital out
214 */
215static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
216 struct hda_codec *codec,
217 struct snd_pcm_substream *substream)
218{
219 struct cs_spec *spec = codec->spec;
220 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
221}
222
223static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
224 struct hda_codec *codec,
225 struct snd_pcm_substream *substream)
226{
227 struct cs_spec *spec = codec->spec;
228 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
229}
230
231static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
232 struct hda_codec *codec,
233 unsigned int stream_tag,
234 unsigned int format,
235 struct snd_pcm_substream *substream)
236{
237 struct cs_spec *spec = codec->spec;
238 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
239 format, substream);
240}
241
242static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
243 struct hda_codec *codec,
244 struct snd_pcm_substream *substream)
245{
246 struct cs_spec *spec = codec->spec;
247 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
248}
249
Takashi Iwai05ee7962011-11-16 18:05:11 +0100250static void cs_update_input_select(struct hda_codec *codec)
251{
252 struct cs_spec *spec = codec->spec;
253 if (spec->cur_adc)
254 snd_hda_codec_write(codec, spec->cur_adc, 0,
255 AC_VERB_SET_CONNECT_SEL,
256 spec->adc_idx[spec->cur_input]);
257}
258
Takashi Iwaie5f14242009-07-01 18:11:44 +0200259/*
260 * Analog capture
261 */
262static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
263 struct hda_codec *codec,
264 unsigned int stream_tag,
265 unsigned int format,
266 struct snd_pcm_substream *substream)
267{
268 struct cs_spec *spec = codec->spec;
269 spec->cur_adc = spec->adc_nid[spec->cur_input];
270 spec->cur_adc_stream_tag = stream_tag;
271 spec->cur_adc_format = format;
Takashi Iwai05ee7962011-11-16 18:05:11 +0100272 cs_update_input_select(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200273 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
274 return 0;
275}
276
277static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
278 struct hda_codec *codec,
279 struct snd_pcm_substream *substream)
280{
281 struct cs_spec *spec = codec->spec;
282 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
283 spec->cur_adc = 0;
284 return 0;
285}
286
287/*
288 */
Takashi Iwaic42d4782011-05-02 11:36:09 +0200289static const struct hda_pcm_stream cs_pcm_analog_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200290 .substreams = 1,
291 .channels_min = 2,
292 .channels_max = 2,
293 .ops = {
294 .open = cs_playback_pcm_open,
295 .prepare = cs_playback_pcm_prepare,
296 .cleanup = cs_playback_pcm_cleanup
297 },
298};
299
Takashi Iwaic42d4782011-05-02 11:36:09 +0200300static const struct hda_pcm_stream cs_pcm_analog_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200301 .substreams = 1,
302 .channels_min = 2,
303 .channels_max = 2,
304 .ops = {
305 .prepare = cs_capture_pcm_prepare,
306 .cleanup = cs_capture_pcm_cleanup
307 },
308};
309
Takashi Iwaic42d4782011-05-02 11:36:09 +0200310static const struct hda_pcm_stream cs_pcm_digital_playback = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200311 .substreams = 1,
312 .channels_min = 2,
313 .channels_max = 2,
314 .ops = {
315 .open = cs_dig_playback_pcm_open,
316 .close = cs_dig_playback_pcm_close,
317 .prepare = cs_dig_playback_pcm_prepare,
318 .cleanup = cs_dig_playback_pcm_cleanup
319 },
320};
321
Takashi Iwaic42d4782011-05-02 11:36:09 +0200322static const struct hda_pcm_stream cs_pcm_digital_capture = {
Takashi Iwaie5f14242009-07-01 18:11:44 +0200323 .substreams = 1,
324 .channels_min = 2,
325 .channels_max = 2,
326};
327
328static int cs_build_pcms(struct hda_codec *codec)
329{
330 struct cs_spec *spec = codec->spec;
331 struct hda_pcm *info = spec->pcm_rec;
332
333 codec->pcm_info = info;
334 codec->num_pcms = 0;
335
336 info->name = "Cirrus Analog";
337 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
338 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
339 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
340 spec->multiout.max_channels;
341 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
Takashi Iwaie5f14242009-07-01 18:11:44 +0200342 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
343 spec->adc_nid[spec->cur_input];
344 codec->num_pcms++;
345
346 if (!spec->multiout.dig_out_nid && !spec->dig_in)
347 return 0;
348
349 info++;
350 info->name = "Cirrus Digital";
351 info->pcm_type = spec->autocfg.dig_out_type[0];
352 if (!info->pcm_type)
353 info->pcm_type = HDA_PCM_TYPE_SPDIF;
354 if (spec->multiout.dig_out_nid) {
355 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
356 cs_pcm_digital_playback;
357 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
358 spec->multiout.dig_out_nid;
359 }
360 if (spec->dig_in) {
361 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
362 cs_pcm_digital_capture;
363 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
364 }
365 codec->num_pcms++;
366
367 return 0;
368}
369
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200370/*
371 * parse codec topology
372 */
373
Takashi Iwaie5f14242009-07-01 18:11:44 +0200374static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
375{
376 hda_nid_t dac;
377 if (!pin)
378 return 0;
379 if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
380 return 0;
381 return dac;
382}
383
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200384static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
385{
386 struct cs_spec *spec = codec->spec;
387 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200388 hda_nid_t pin = cfg->inputs[idx].pin;
Takashi Iwai06dec222011-05-17 10:00:16 +0200389 unsigned int val;
390 if (!is_jack_detectable(codec, pin))
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200391 return 0;
392 val = snd_hda_codec_get_pincfg(codec, pin);
Takashi Iwai99ae28b2010-09-17 14:42:34 +0200393 return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200394}
395
396static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
397 unsigned int *idxp)
398{
Takashi Iwai1b004d02011-08-20 09:19:59 +0200399 int i, idx;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200400 hda_nid_t nid;
401
402 nid = codec->start_nid;
403 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200404 unsigned int type;
Takashi Iwai1682c812011-05-17 10:02:27 +0200405 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200406 if (type != AC_WID_AUD_IN)
407 continue;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200408 idx = snd_hda_get_conn_index(codec, nid, pin, false);
409 if (idx >= 0) {
410 *idxp = idx;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200411 return nid;
Takashi Iwai1b004d02011-08-20 09:19:59 +0200412 }
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200413 }
414 return 0;
415}
416
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200417static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
418{
Takashi Iwai40c20fa2009-07-06 13:00:57 +0200419 unsigned int val;
420 val = snd_hda_codec_get_pincfg(codec, nid);
421 return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
422}
423
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200424static int parse_output(struct hda_codec *codec)
425{
426 struct cs_spec *spec = codec->spec;
427 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai277a57c2009-07-08 12:42:08 +0200428 int i, extra_nids;
Takashi Iwai21a4dc42009-07-06 12:55:46 +0200429 hda_nid_t dac;
430
431 for (i = 0; i < cfg->line_outs; i++) {
432 dac = get_dac(codec, cfg->line_out_pins[i]);
433 if (!dac)
434 break;
435 spec->dac_nid[i] = dac;
436 }
437 spec->multiout.num_dacs = i;
438 spec->multiout.dac_nids = spec->dac_nid;
439 spec->multiout.max_channels = i * 2;
440
441 /* add HP and speakers */
442 extra_nids = 0;
443 for (i = 0; i < cfg->hp_outs; i++) {
444 dac = get_dac(codec, cfg->hp_pins[i]);
445 if (!dac)
446 break;
447 if (!i)
448 spec->multiout.hp_nid = dac;
449 else
450 spec->multiout.extra_out_nid[extra_nids++] = dac;
451 }
452 for (i = 0; i < cfg->speaker_outs; i++) {
453 dac = get_dac(codec, cfg->speaker_pins[i]);
454 if (!dac)
455 break;
456 spec->multiout.extra_out_nid[extra_nids++] = dac;
457 }
458
459 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
460 cfg->speaker_outs = cfg->line_outs;
461 memcpy(cfg->speaker_pins, cfg->line_out_pins,
462 sizeof(cfg->speaker_pins));
463 cfg->line_outs = 0;
464 }
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];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200937 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +0200938 }
Takashi Iwai6dfeb7032011-11-22 20:00:31 +0100939 if (spec->gpio_eapd_hp) {
940 unsigned int gpio = hp_present ?
941 spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
Stelian Pop3a385162009-07-30 14:44:27 +0200942 snd_hda_codec_write(codec, 0x01, 0,
943 AC_VERB_SET_GPIO_DATA, gpio);
944 }
Tim Howe56487c22011-07-22 16:41:00 -0500945
David Henningsson5660ffd2012-01-02 12:40:17 +0100946 /* specific to CS4210 */
947 if (spec->vendor_nid == CS4210_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -0500948 /* mute HPs if spdif jack (SENSE_B) is present */
949 for (i = 0; i < cfg->hp_outs; i++) {
950 nid = cfg->hp_pins[i];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200951 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500952 (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];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200958 snd_hda_set_pin_ctl(codec, nid,
Tim Howe56487c22011-07-22 16:41:00 -0500959 spdif_present ? PIN_OUT : 0);
960
961 }
962 /* Update board GPIOs if neccessary ... */
963 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200964}
965
Tim Howe56487c22011-07-22 16:41:00 -0500966/*
967 * Auto-input redirect for CS421x
968 * Switch max 3 inputs of a single ADC (nid 3)
969*/
970
Takashi Iwaie5f14242009-07-01 18:11:44 +0200971static void cs_automic(struct hda_codec *codec)
972{
973 struct cs_spec *spec = codec->spec;
974 struct auto_pin_cfg *cfg = &spec->autocfg;
975 hda_nid_t nid;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800976 unsigned int present;
Tim Howe56487c22011-07-22 16:41:00 -0500977
Takashi Iwaic1e0bb92010-08-30 13:05:30 +0200978 nid = cfg->inputs[spec->automic_idx].pin;
Wu Fengguang864f92b2009-11-18 12:38:02 +0800979 present = snd_hda_jack_detect(codec, nid);
Tim Howe56487c22011-07-22 16:41:00 -0500980
981 /* specific to CS421x, single ADC */
David Henningsson5660ffd2012-01-02 12:40:17 +0100982 if (spec->vendor_nid == CS420X_VENDOR_NID) {
983 if (present)
984 change_cur_input(codec, spec->automic_idx, 0);
985 else
986 change_cur_input(codec, !spec->automic_idx, 0);
987 } else {
Tim Howe56487c22011-07-22 16:41:00 -0500988 if (present) {
Dylan Reidf70eecd2012-01-31 13:04:41 -0800989 if (spec->cur_input != spec->automic_idx) {
990 spec->last_input = spec->cur_input;
991 spec->cur_input = spec->automic_idx;
992 }
Tim Howe56487c22011-07-22 16:41:00 -0500993 } else {
994 spec->cur_input = spec->last_input;
995 }
Takashi Iwai05ee7962011-11-16 18:05:11 +0100996 cs_update_input_select(codec);
Tim Howe56487c22011-07-22 16:41:00 -0500997 }
Takashi Iwaie5f14242009-07-01 18:11:44 +0200998}
999
1000/*
1001 */
1002
1003static void init_output(struct hda_codec *codec)
1004{
1005 struct cs_spec *spec = codec->spec;
1006 struct auto_pin_cfg *cfg = &spec->autocfg;
1007 int i;
1008
1009 /* mute first */
1010 for (i = 0; i < spec->multiout.num_dacs; i++)
1011 snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
1012 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1013 if (spec->multiout.hp_nid)
1014 snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
1015 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1016 for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
1017 if (!spec->multiout.extra_out_nid[i])
1018 break;
1019 snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
1020 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1021 }
1022
1023 /* set appropriate pin controls */
1024 for (i = 0; i < cfg->line_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001025 snd_hda_set_pin_ctl(codec, cfg->line_out_pins[i], 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];
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001029 snd_hda_set_pin_ctl(codec, nid, PIN_HP);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001030 if (!cfg->speaker_outs)
1031 continue;
Tim Howe56487c22011-07-22 16:41:00 -05001032 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001033 snd_hda_jack_detect_enable(codec, nid, HP_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001034 spec->hp_detect = 1;
1035 }
1036 }
Tim Howe56487c22011-07-22 16:41:00 -05001037
1038 /* Speaker */
Takashi Iwaie5f14242009-07-01 18:11:44 +02001039 for (i = 0; i < cfg->speaker_outs; i++)
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001040 snd_hda_set_pin_ctl(codec, cfg->speaker_pins[i], PIN_OUT);
Tim Howe56487c22011-07-22 16:41:00 -05001041
1042 /* SPDIF is enabled on presence detect for CS421x */
1043 if (spec->hp_detect || spec->spdif_detect)
Takashi Iwaie5f14242009-07-01 18:11:44 +02001044 cs_automute(codec);
1045}
1046
1047static void init_input(struct hda_codec *codec)
1048{
1049 struct cs_spec *spec = codec->spec;
1050 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001051 unsigned int coef;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001052 int i;
1053
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001054 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001055 unsigned int ctl;
Takashi Iwaic1e0bb92010-08-30 13:05:30 +02001056 hda_nid_t pin = cfg->inputs[i].pin;
1057 if (!spec->adc_nid[i])
Takashi Iwaie5f14242009-07-01 18:11:44 +02001058 continue;
1059 /* set appropriate pin control and mute first */
1060 ctl = PIN_IN;
Takashi Iwai47408602012-04-20 13:06:53 +02001061 if (cfg->inputs[i].type == AUTO_PIN_MIC)
1062 ctl |= snd_hda_get_default_vref(codec, pin);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02001063 snd_hda_set_pin_ctl(codec, pin, ctl);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001064 snd_hda_codec_write(codec, spec->adc_nid[i], 0,
1065 AC_VERB_SET_AMP_GAIN_MUTE,
1066 AMP_IN_MUTE(spec->adc_idx[i]));
1067 if (spec->mic_detect && spec->automic_idx == i)
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001068 snd_hda_jack_detect_enable(codec, pin, MIC_EVENT);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001069 }
David Henningsson5660ffd2012-01-02 12:40:17 +01001070 /* CS420x has multiple ADC, CS421x has single ADC */
1071 if (spec->vendor_nid == CS420X_VENDOR_NID) {
Tim Howe56487c22011-07-22 16:41:00 -05001072 change_cur_input(codec, spec->cur_input, 1);
1073 if (spec->mic_detect)
1074 cs_automic(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001075
Tim Howe56487c22011-07-22 16:41:00 -05001076 coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
1077 if (is_active_pin(codec, CS_DMIC2_PIN_NID))
1078 coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
1079 if (is_active_pin(codec, CS_DMIC1_PIN_NID))
1080 coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
1081 * No effect if SPDIF_OUT2 is
1082 * selected in IDX_SPDIF_CTL.
1083 */
1084 cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
David Henningsson5660ffd2012-01-02 12:40:17 +01001085 } else {
1086 if (spec->mic_detect)
1087 cs_automic(codec);
1088 else {
1089 spec->cur_adc = spec->adc_nid[spec->cur_input];
1090 cs_update_input_select(codec);
1091 }
Tim Howe56487c22011-07-22 16:41:00 -05001092 }
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001093}
1094
Takashi Iwaic42d4782011-05-02 11:36:09 +02001095static const struct hda_verb cs_coef_init_verbs[] = {
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001096 {0x11, AC_VERB_SET_PROC_STATE, 1},
1097 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1098 {0x11, AC_VERB_SET_PROC_COEF,
1099 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
1100 | 0x0040 /* Mute DACs on FIFO error */
1101 | 0x1000 /* Enable DACs High Pass Filter */
1102 | 0x0400 /* Disable Coefficient Auto increment */
1103 )},
1104 /* Beep */
1105 {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
1106 {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
1107
1108 {} /* terminator */
1109};
1110
Brian Austina769cbc2010-09-07 14:36:22 -05001111/* Errata: CS4207 rev C0/C1/C2 Silicon
1112 *
1113 * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
1114 *
1115 * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
1116 * may be excessive (up to an additional 200 μA), which is most easily
1117 * observed while the part is being held in reset (RESET# active low).
1118 *
1119 * Root Cause: At initial powerup of the device, the logic that drives
1120 * the clock and write enable to the S/PDIF SRC RAMs is not properly
1121 * initialized.
1122 * Certain random patterns will cause a steady leakage current in those
1123 * RAM cells. The issue will resolve once the SRCs are used (turned on).
1124 *
1125 * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
1126 * blocks, which will alleviate the issue.
1127 */
1128
Takashi Iwaic42d4782011-05-02 11:36:09 +02001129static const struct hda_verb cs_errata_init_verbs[] = {
Brian Austina769cbc2010-09-07 14:36:22 -05001130 {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
1131 {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1132
1133 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1134 {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
1135 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1136 {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
1137 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1138 {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
1139
1140 {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
1141 {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
1142
1143 {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
1144 {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
1145 {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
1146 {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
1147 {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
1148 {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
1149 {0x11, AC_VERB_SET_PROC_STATE, 0x00},
1150
Takashi Iwai38c07642011-03-03 14:54:19 +01001151#if 0 /* Don't to set to D3 as we are in power-up sequence */
Brian Austina769cbc2010-09-07 14:36:22 -05001152 {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
1153 {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
1154 /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
Takashi Iwai38c07642011-03-03 14:54:19 +01001155#endif
Brian Austina769cbc2010-09-07 14:36:22 -05001156
1157 {} /* terminator */
1158};
1159
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001160/* SPDIF setup */
1161static void init_digital(struct hda_codec *codec)
1162{
1163 unsigned int coef;
1164
1165 coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
1166 coef |= 0x0008; /* Replace with mute on error */
1167 if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
1168 coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
1169 * SPDIF_OUT2 is shared with GPIO1 and
1170 * DMIC_SDA2.
1171 */
1172 cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001173}
1174
1175static int cs_init(struct hda_codec *codec)
1176{
1177 struct cs_spec *spec = codec->spec;
1178
Brian Austina769cbc2010-09-07 14:36:22 -05001179 /* init_verb sequence for C0/C1/C2 errata*/
1180 snd_hda_sequence_write(codec, cs_errata_init_verbs);
1181
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001182 snd_hda_sequence_write(codec, cs_coef_init_verbs);
Takashi Iwaied208252009-07-07 09:04:26 +02001183
1184 if (spec->gpio_mask) {
1185 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1186 spec->gpio_mask);
1187 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1188 spec->gpio_dir);
1189 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1190 spec->gpio_data);
1191 }
1192
Takashi Iwaie5f14242009-07-01 18:11:44 +02001193 init_output(codec);
1194 init_input(codec);
Takashi Iwai40c20fa2009-07-06 13:00:57 +02001195 init_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001196 snd_hda_jack_report_sync(codec);
1197
Takashi Iwaie5f14242009-07-01 18:11:44 +02001198 return 0;
1199}
1200
1201static int cs_build_controls(struct hda_codec *codec)
1202{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001203 struct cs_spec *spec = codec->spec;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001204 int err;
1205
1206 err = build_output(codec);
1207 if (err < 0)
1208 return err;
1209 err = build_input(codec);
1210 if (err < 0)
1211 return err;
1212 err = build_digital_output(codec);
1213 if (err < 0)
1214 return err;
1215 err = build_digital_input(codec);
1216 if (err < 0)
1217 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001218 err = cs_init(codec);
1219 if (err < 0)
1220 return err;
1221
1222 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1223 if (err < 0)
1224 return err;
1225
1226 return 0;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001227}
1228
1229static void cs_free(struct hda_codec *codec)
1230{
1231 struct cs_spec *spec = codec->spec;
1232 kfree(spec->capture_bind[0]);
1233 kfree(spec->capture_bind[1]);
1234 kfree(codec->spec);
1235}
1236
1237static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
1238{
Takashi Iwai3a938972011-10-28 01:16:55 +02001239 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001240 case HP_EVENT:
1241 cs_automute(codec);
1242 break;
1243 case MIC_EVENT:
1244 cs_automic(codec);
1245 break;
1246 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001247 snd_hda_jack_report_sync(codec);
Takashi Iwaie5f14242009-07-01 18:11:44 +02001248}
1249
Takashi Iwaic42d4782011-05-02 11:36:09 +02001250static const struct hda_codec_ops cs_patch_ops = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02001251 .build_controls = cs_build_controls,
1252 .build_pcms = cs_build_pcms,
1253 .init = cs_init,
1254 .free = cs_free,
1255 .unsol_event = cs_unsol_event,
1256};
1257
1258static int cs_parse_auto_config(struct hda_codec *codec)
1259{
1260 struct cs_spec *spec = codec->spec;
1261 int err;
1262
1263 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1264 if (err < 0)
1265 return err;
Takashi Iwaied208252009-07-07 09:04:26 +02001266
1267 err = parse_output(codec);
1268 if (err < 0)
1269 return err;
1270 err = parse_input(codec);
1271 if (err < 0)
1272 return err;
1273 err = parse_digital_output(codec);
1274 if (err < 0)
1275 return err;
1276 err = parse_digital_input(codec);
1277 if (err < 0)
1278 return err;
Takashi Iwaie5f14242009-07-01 18:11:44 +02001279 return 0;
1280}
1281
Takashi Iwaiea734962011-01-17 11:29:34 +01001282static const char * const cs420x_models[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001283 [CS420X_MBP53] = "mbp53",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001284 [CS420X_MBP55] = "mbp55",
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001285 [CS420X_IMAC27] = "imac27",
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001286 [CS420X_IMAC27_122] = "imac27_122",
Dan Carpenter92bb43e2011-11-24 14:48:24 +03001287 [CS420X_APPLE] = "apple",
Takashi Iwaia6bae202009-07-06 15:15:22 +02001288 [CS420X_AUTO] = "auto",
1289};
1290
1291
Takashi Iwaic42d4782011-05-02 11:36:09 +02001292static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001293 SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
Edgar (gimli) Hucek87232dd2010-11-03 08:14:10 +01001294 SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001295 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
Takashi Iwaif46119b2010-10-11 14:46:35 +02001296 SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001297 /* this conflicts with too many other models */
1298 /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
1299 {} /* terminator */
1300};
1301
1302static const struct snd_pci_quirk cs420x_codec_cfg_tbl[] = {
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001303 SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001304 SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
Takashi Iwaia6bae202009-07-06 15:15:22 +02001305 {} /* terminator */
1306};
1307
1308struct cs_pincfg {
1309 hda_nid_t nid;
1310 u32 val;
1311};
1312
Takashi Iwaic42d4782011-05-02 11:36:09 +02001313static const struct cs_pincfg mbp53_pincfgs[] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001314 { 0x09, 0x012b4050 },
1315 { 0x0a, 0x90100141 },
1316 { 0x0b, 0x90100140 },
1317 { 0x0c, 0x018b3020 },
1318 { 0x0d, 0x90a00110 },
1319 { 0x0e, 0x400000f0 },
1320 { 0x0f, 0x01cbe030 },
1321 { 0x10, 0x014be060 },
1322 { 0x12, 0x400000f0 },
1323 { 0x15, 0x400000f0 },
1324 {} /* terminator */
1325};
1326
Takashi Iwaic42d4782011-05-02 11:36:09 +02001327static const struct cs_pincfg mbp55_pincfgs[] = {
Takashi Iwaia6bae202009-07-06 15:15:22 +02001328 { 0x09, 0x012b4030 },
1329 { 0x0a, 0x90100121 },
1330 { 0x0b, 0x90100120 },
1331 { 0x0c, 0x400000f0 },
1332 { 0x0d, 0x90a00110 },
1333 { 0x0e, 0x400000f0 },
1334 { 0x0f, 0x400000f0 },
1335 { 0x10, 0x014be040 },
1336 { 0x12, 0x400000f0 },
1337 { 0x15, 0x400000f0 },
1338 {} /* terminator */
1339};
1340
Takashi Iwaic42d4782011-05-02 11:36:09 +02001341static const struct cs_pincfg imac27_pincfgs[] = {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001342 { 0x09, 0x012b4050 },
1343 { 0x0a, 0x90100140 },
1344 { 0x0b, 0x90100142 },
1345 { 0x0c, 0x018b3020 },
1346 { 0x0d, 0x90a00110 },
1347 { 0x0e, 0x400000f0 },
1348 { 0x0f, 0x01cbe030 },
1349 { 0x10, 0x014be060 },
1350 { 0x12, 0x01ab9070 },
1351 { 0x15, 0x400000f0 },
1352 {} /* terminator */
1353};
1354
Takashi Iwaic42d4782011-05-02 11:36:09 +02001355static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001356 [CS420X_MBP53] = mbp53_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001357 [CS420X_MBP55] = mbp55_pincfgs,
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001358 [CS420X_IMAC27] = imac27_pincfgs,
Takashi Iwaia6bae202009-07-06 15:15:22 +02001359};
1360
Tim Howe56487c22011-07-22 16:41:00 -05001361static void fix_pincfg(struct hda_codec *codec, int model,
1362 const struct cs_pincfg **pin_configs)
Takashi Iwaia6bae202009-07-06 15:15:22 +02001363{
Tim Howe56487c22011-07-22 16:41:00 -05001364 const struct cs_pincfg *cfg = pin_configs[model];
Takashi Iwaia6bae202009-07-06 15:15:22 +02001365 if (!cfg)
1366 return;
1367 for (; cfg->nid; cfg++)
1368 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1369}
1370
Takashi Iwaie5f14242009-07-01 18:11:44 +02001371static int patch_cs420x(struct hda_codec *codec)
1372{
1373 struct cs_spec *spec;
1374 int err;
1375
1376 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1377 if (!spec)
1378 return -ENOMEM;
1379 codec->spec = spec;
1380
Tim Howe56487c22011-07-22 16:41:00 -05001381 spec->vendor_nid = CS420X_VENDOR_NID;
1382
Takashi Iwaia6bae202009-07-06 15:15:22 +02001383 spec->board_config =
1384 snd_hda_check_board_config(codec, CS420X_MODELS,
1385 cs420x_models, cs420x_cfg_tbl);
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001386 if (spec->board_config < 0)
1387 spec->board_config =
1388 snd_hda_check_board_codec_sid_config(codec,
1389 CS420X_MODELS, NULL, cs420x_codec_cfg_tbl);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001390 if (spec->board_config >= 0)
Tim Howe56487c22011-07-22 16:41:00 -05001391 fix_pincfg(codec, spec->board_config, cs_pincfgs);
Takashi Iwaia6bae202009-07-06 15:15:22 +02001392
Takashi Iwaied208252009-07-07 09:04:26 +02001393 switch (spec->board_config) {
Rafael Avila de Espindola1a5ba2e2009-12-22 07:59:37 +01001394 case CS420X_IMAC27:
Vince Weaver4e7d7c62010-09-22 17:31:37 -04001395 case CS420X_MBP53:
Takashi Iwaied208252009-07-07 09:04:26 +02001396 case CS420X_MBP55:
Takashi Iwai6dfeb7032011-11-22 20:00:31 +01001397 case CS420X_APPLE:
1398 spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
1399 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1400 spec->gpio_mask = spec->gpio_dir =
1401 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
Takashi Iwaied208252009-07-07 09:04:26 +02001402 break;
Jérémy Lal7e5bea12012-01-09 17:19:45 +01001403 case CS420X_IMAC27_122:
1404 spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
1405 spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
1406 spec->gpio_mask = spec->gpio_dir =
1407 spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
1408 break;
Takashi Iwaied208252009-07-07 09:04:26 +02001409 }
Takashi Iwaie5f14242009-07-01 18:11:44 +02001410
Takashi Iwaied208252009-07-07 09:04:26 +02001411 err = cs_parse_auto_config(codec);
Takashi Iwai21a4dc42009-07-06 12:55:46 +02001412 if (err < 0)
1413 goto error;
1414
Takashi Iwaie5f14242009-07-01 18:11:44 +02001415 codec->patch_ops = cs_patch_ops;
1416
1417 return 0;
1418
1419 error:
1420 kfree(codec->spec);
1421 codec->spec = NULL;
1422 return err;
1423}
1424
Tim Howe56487c22011-07-22 16:41:00 -05001425/*
1426 * Cirrus Logic CS4210
1427 *
1428 * 1 DAC => HP(sense) / Speakers,
1429 * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
1430 * 1 SPDIF OUT => SPDIF Trasmitter(sense)
1431*/
1432
1433/* CS4210 board names */
1434static const char *cs421x_models[CS421X_MODELS] = {
1435 [CS421X_CDB4210] = "cdb4210",
1436};
1437
1438static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
1439 /* Test Intel board + CDB2410 */
1440 SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
1441 {} /* terminator */
1442};
1443
1444/* CS4210 board pinconfigs */
1445/* Default CS4210 (CDB4210)*/
1446static const struct cs_pincfg cdb4210_pincfgs[] = {
1447 { 0x05, 0x0321401f },
1448 { 0x06, 0x90170010 },
1449 { 0x07, 0x03813031 },
1450 { 0x08, 0xb7a70037 },
1451 { 0x09, 0xb7a6003e },
1452 { 0x0a, 0x034510f0 },
1453 {} /* terminator */
1454};
1455
1456static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
1457 [CS421X_CDB4210] = cdb4210_pincfgs,
1458};
1459
1460static const struct hda_verb cs421x_coef_init_verbs[] = {
1461 {0x0B, AC_VERB_SET_PROC_STATE, 1},
1462 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
1463 /*
1464 Disable Coefficient Index Auto-Increment(DAI)=1,
1465 PDREF=0
1466 */
1467 {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
1468
1469 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
1470 /* ADC SZCMode = Digital Soft Ramp */
1471 {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
1472
1473 {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
1474 {0x0B, AC_VERB_SET_PROC_COEF,
1475 (0x0002 /* DAC SZCMode = Digital Soft Ramp */
1476 | 0x0004 /* Mute DAC on FIFO error */
1477 | 0x0008 /* Enable DAC High Pass Filter */
1478 )},
1479 {} /* terminator */
1480};
1481
1482/* Errata: CS4210 rev A1 Silicon
1483 *
1484 * http://www.cirrus.com/en/pubs/errata/
1485 *
1486 * Description:
1487 * 1. Performance degredation is present in the ADC.
1488 * 2. Speaker output is not completely muted upon HP detect.
1489 * 3. Noise is present when clipping occurs on the amplified
1490 * speaker outputs.
1491 *
1492 * Workaround:
1493 * The following verb sequence written to the registers during
1494 * initialization will correct the issues listed above.
1495 */
1496
1497static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
1498 {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
1499
1500 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
1501 {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
1502
1503 {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
1504 {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
1505
1506 {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
1507 {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
1508
1509 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
1510 {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
1511
1512 {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
1513 {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
1514
1515 {} /* terminator */
1516};
1517
1518/* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
1519static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
1520
1521static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
1522 struct snd_ctl_elem_info *uinfo)
1523{
1524 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1525 uinfo->count = 1;
1526 uinfo->value.integer.min = 0;
1527 uinfo->value.integer.max = 3;
1528 return 0;
1529}
1530
1531static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
1532 struct snd_ctl_elem_value *ucontrol)
1533{
1534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1535
1536 ucontrol->value.integer.value[0] =
1537 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
1538 return 0;
1539}
1540
1541static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
1542 struct snd_ctl_elem_value *ucontrol)
1543{
1544 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1545
1546 unsigned int vol = ucontrol->value.integer.value[0];
1547 unsigned int coef =
1548 cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
1549 unsigned int original_coef = coef;
1550
1551 coef &= ~0x0003;
1552 coef |= (vol & 0x0003);
1553 if (original_coef == coef)
1554 return 0;
1555 else {
1556 cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
1557 return 1;
1558 }
1559}
1560
1561static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
1562
1563 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1564 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1565 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1566 .name = "Speaker Boost Playback Volume",
1567 .info = cs421x_boost_vol_info,
1568 .get = cs421x_boost_vol_get,
1569 .put = cs421x_boost_vol_put,
1570 .tlv = { .p = cs421x_speaker_boost_db_scale },
1571};
1572
David Henningsson5660ffd2012-01-02 12:40:17 +01001573static void cs4210_pinmux_init(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001574{
1575 struct cs_spec *spec = codec->spec;
1576 unsigned int def_conf, coef;
1577
1578 /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
1579 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1580
1581 if (spec->gpio_mask)
1582 coef |= 0x0008; /* B1,B2 are GPIOs */
1583 else
1584 coef &= ~0x0008;
1585
1586 if (spec->sense_b)
1587 coef |= 0x0010; /* B2 is SENSE_B, not inverted */
1588 else
1589 coef &= ~0x0010;
1590
1591 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1592
1593 if ((spec->gpio_mask || spec->sense_b) &&
1594 is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
1595
1596 /*
1597 GPIO or SENSE_B forced - disconnect the DMIC pin.
1598 */
1599 def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
1600 def_conf &= ~AC_DEFCFG_PORT_CONN;
1601 def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
1602 snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
1603 }
1604}
1605
1606static void init_cs421x_digital(struct hda_codec *codec)
1607{
1608 struct cs_spec *spec = codec->spec;
1609 struct auto_pin_cfg *cfg = &spec->autocfg;
1610 int i;
1611
1612
1613 for (i = 0; i < cfg->dig_outs; i++) {
1614 hda_nid_t nid = cfg->dig_out_pins[i];
1615 if (!cfg->speaker_outs)
1616 continue;
1617 if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001618 snd_hda_jack_detect_enable(codec, nid, SPDIF_EVENT);
Tim Howe56487c22011-07-22 16:41:00 -05001619 spec->spdif_detect = 1;
1620 }
1621 }
1622}
1623
1624static int cs421x_init(struct hda_codec *codec)
1625{
1626 struct cs_spec *spec = codec->spec;
1627
David Henningsson5660ffd2012-01-02 12:40:17 +01001628 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1629 snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
1630 snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
1631 cs4210_pinmux_init(codec);
1632 }
Tim Howe56487c22011-07-22 16:41:00 -05001633
1634 if (spec->gpio_mask) {
1635 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
1636 spec->gpio_mask);
1637 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
1638 spec->gpio_dir);
1639 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
1640 spec->gpio_data);
1641 }
1642
1643 init_output(codec);
1644 init_input(codec);
1645 init_cs421x_digital(codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001646 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001647
1648 return 0;
1649}
1650
1651/*
1652 * CS4210 Input MUX (1 ADC)
1653 */
1654static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
1655 struct snd_ctl_elem_info *uinfo)
1656{
1657 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1658 struct cs_spec *spec = codec->spec;
1659
1660 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1661}
1662
1663static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
1664 struct snd_ctl_elem_value *ucontrol)
1665{
1666 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1667 struct cs_spec *spec = codec->spec;
1668
1669 ucontrol->value.enumerated.item[0] = spec->cur_input;
1670 return 0;
1671}
1672
1673static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
1674 struct snd_ctl_elem_value *ucontrol)
1675{
1676 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1677 struct cs_spec *spec = codec->spec;
1678
1679 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
1680 spec->adc_nid[0], &spec->cur_input);
1681
1682}
1683
1684static struct snd_kcontrol_new cs421x_capture_source = {
1685
1686 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1687 .name = "Capture Source",
1688 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1689 .info = cs421x_mux_enum_info,
1690 .get = cs421x_mux_enum_get,
1691 .put = cs421x_mux_enum_put,
1692};
1693
1694static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
1695{
1696 struct cs_spec *spec = codec->spec;
1697 struct auto_pin_cfg *cfg = &spec->autocfg;
1698 const struct hda_input_mux *imux = &spec->input_mux;
1699 hda_nid_t pin = cfg->inputs[item].pin;
1700 struct snd_kcontrol *kctl;
1701 u32 caps;
1702
1703 if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
1704 return 0;
1705
1706 caps = query_amp_caps(codec, pin, HDA_INPUT);
1707 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1708 if (caps <= 1)
1709 return 0;
1710
1711 return add_volume(codec, imux->items[item].label, 0,
1712 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
1713}
1714
1715/* add a (input-boost) volume control to the given input pin */
1716static int build_cs421x_input(struct hda_codec *codec)
1717{
1718 struct cs_spec *spec = codec->spec;
1719 struct auto_pin_cfg *cfg = &spec->autocfg;
1720 struct hda_input_mux *imux = &spec->input_mux;
1721 int i, err, type_idx;
1722 const char *label;
1723
1724 if (!spec->num_inputs)
1725 return 0;
1726
1727 /* make bind-capture */
1728 spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
1729 spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
1730 for (i = 0; i < 2; i++) {
1731 struct snd_kcontrol *kctl;
1732 int n;
1733 if (!spec->capture_bind[i])
1734 return -ENOMEM;
1735 kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
1736 if (!kctl)
1737 return -ENOMEM;
1738 kctl->private_value = (long)spec->capture_bind[i];
1739 err = snd_hda_ctl_add(codec, 0, kctl);
1740 if (err < 0)
1741 return err;
1742 for (n = 0; n < AUTO_PIN_LAST; n++) {
1743 if (!spec->adc_nid[n])
1744 continue;
1745 err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
1746 if (err < 0)
1747 return err;
1748 }
1749 }
1750
1751 /* Add Input MUX Items + Capture Volume/Switch */
1752 for (i = 0; i < spec->num_inputs; i++) {
1753 label = hda_get_autocfg_input_label(codec, cfg, i);
1754 snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
1755
1756 err = cs421x_add_input_volume_control(codec, i);
1757 if (err < 0)
1758 return err;
1759 }
1760
1761 /*
1762 Add 'Capture Source' Switch if
1763 * 2 inputs and no mic detec
1764 * 3 inputs
1765 */
1766 if ((spec->num_inputs == 2 && !spec->mic_detect) ||
1767 (spec->num_inputs == 3)) {
1768
1769 err = snd_hda_ctl_add(codec, spec->adc_nid[0],
1770 snd_ctl_new1(&cs421x_capture_source, codec));
1771 if (err < 0)
1772 return err;
1773 }
1774
1775 return 0;
1776}
1777
1778/* Single DAC (Mute/Gain) */
1779static int build_cs421x_output(struct hda_codec *codec)
1780{
1781 hda_nid_t dac = CS4210_DAC_NID;
1782 struct cs_spec *spec = codec->spec;
1783 struct auto_pin_cfg *cfg = &spec->autocfg;
1784 struct snd_kcontrol *kctl;
1785 int err;
David Henningsson40d03e62012-01-02 12:40:15 +01001786 char *name = "Master";
Tim Howe56487c22011-07-22 16:41:00 -05001787
1788 fix_volume_caps(codec, dac);
Tim Howe56487c22011-07-22 16:41:00 -05001789
1790 err = add_mute(codec, name, 0,
1791 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1792 if (err < 0)
1793 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001794
1795 err = add_volume(codec, name, 0,
1796 HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
1797 if (err < 0)
1798 return err;
Tim Howe56487c22011-07-22 16:41:00 -05001799
David Henningsson5660ffd2012-01-02 12:40:17 +01001800 if (cfg->speaker_outs && (spec->vendor_nid == CS4210_VENDOR_NID)) {
Tim Howe56487c22011-07-22 16:41:00 -05001801 err = snd_hda_ctl_add(codec, 0,
1802 snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
1803 if (err < 0)
1804 return err;
1805 }
1806 return err;
1807}
1808
1809static int cs421x_build_controls(struct hda_codec *codec)
1810{
Takashi Iwai01a61e12011-10-28 00:03:22 +02001811 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001812 int err;
1813
1814 err = build_cs421x_output(codec);
1815 if (err < 0)
1816 return err;
1817 err = build_cs421x_input(codec);
1818 if (err < 0)
1819 return err;
1820 err = build_digital_output(codec);
1821 if (err < 0)
1822 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +02001823 err = cs421x_init(codec);
1824 if (err < 0)
1825 return err;
1826
1827 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1828 if (err < 0)
1829 return err;
1830
1831 return 0;
Tim Howe56487c22011-07-22 16:41:00 -05001832}
1833
1834static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
1835{
Takashi Iwai3a938972011-10-28 01:16:55 +02001836 switch (snd_hda_jack_get_action(codec, res >> 26)) {
Tim Howe56487c22011-07-22 16:41:00 -05001837 case HP_EVENT:
1838 case SPDIF_EVENT:
1839 cs_automute(codec);
1840 break;
1841
1842 case MIC_EVENT:
1843 cs_automic(codec);
1844 break;
1845 }
Takashi Iwai01a61e12011-10-28 00:03:22 +02001846 snd_hda_jack_report_sync(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001847}
1848
1849static int parse_cs421x_input(struct hda_codec *codec)
1850{
1851 struct cs_spec *spec = codec->spec;
1852 struct auto_pin_cfg *cfg = &spec->autocfg;
1853 int i;
1854
1855 for (i = 0; i < cfg->num_inputs; i++) {
1856 hda_nid_t pin = cfg->inputs[i].pin;
1857 spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
1858 spec->cur_input = spec->last_input = i;
1859 spec->num_inputs++;
1860
1861 /* check whether the automatic mic switch is available */
1862 if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
1863 spec->mic_detect = 1;
1864 spec->automic_idx = i;
1865 }
1866 }
1867 return 0;
1868}
1869
1870static int cs421x_parse_auto_config(struct hda_codec *codec)
1871{
1872 struct cs_spec *spec = codec->spec;
1873 int err;
1874
1875 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1876 if (err < 0)
1877 return err;
1878 err = parse_output(codec);
1879 if (err < 0)
1880 return err;
1881 err = parse_cs421x_input(codec);
1882 if (err < 0)
1883 return err;
1884 err = parse_digital_output(codec);
1885 if (err < 0)
1886 return err;
1887 return 0;
1888}
1889
1890#ifdef CONFIG_PM
1891/*
1892 Manage PDREF, when transitioning to D3hot
1893 (DAC,ADC) -> D3, PDREF=1, AFG->D3
1894*/
1895static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
1896{
David Henningsson5660ffd2012-01-02 12:40:17 +01001897 struct cs_spec *spec = codec->spec;
Tim Howe56487c22011-07-22 16:41:00 -05001898 unsigned int coef;
1899
1900 snd_hda_shutup_pins(codec);
1901
1902 snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
1903 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1904 snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
1905 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1906
David Henningsson5660ffd2012-01-02 12:40:17 +01001907 if (spec->vendor_nid == CS4210_VENDOR_NID) {
1908 coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
1909 coef |= 0x0004; /* PDREF */
1910 cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
1911 }
Tim Howe56487c22011-07-22 16:41:00 -05001912
1913 return 0;
1914}
1915#endif
1916
David Henningsson5660ffd2012-01-02 12:40:17 +01001917static struct hda_codec_ops cs421x_patch_ops = {
Tim Howe56487c22011-07-22 16:41:00 -05001918 .build_controls = cs421x_build_controls,
1919 .build_pcms = cs_build_pcms,
1920 .init = cs421x_init,
1921 .free = cs_free,
1922 .unsol_event = cs421x_unsol_event,
1923#ifdef CONFIG_PM
1924 .suspend = cs421x_suspend,
1925#endif
1926};
1927
David Henningsson5660ffd2012-01-02 12:40:17 +01001928static int patch_cs4210(struct hda_codec *codec)
Tim Howe56487c22011-07-22 16:41:00 -05001929{
1930 struct cs_spec *spec;
1931 int err;
1932
1933 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1934 if (!spec)
1935 return -ENOMEM;
1936 codec->spec = spec;
1937
David Henningsson5660ffd2012-01-02 12:40:17 +01001938 spec->vendor_nid = CS4210_VENDOR_NID;
Tim Howe56487c22011-07-22 16:41:00 -05001939
1940 spec->board_config =
1941 snd_hda_check_board_config(codec, CS421X_MODELS,
1942 cs421x_models, cs421x_cfg_tbl);
1943 if (spec->board_config >= 0)
1944 fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
1945 /*
1946 Setup GPIO/SENSE for each board (if used)
1947 */
1948 switch (spec->board_config) {
1949 case CS421X_CDB4210:
1950 snd_printd("CS4210 board: %s\n",
1951 cs421x_models[spec->board_config]);
1952/* spec->gpio_mask = 3;
1953 spec->gpio_dir = 3;
1954 spec->gpio_data = 3;
1955*/
1956 spec->sense_b = 1;
1957
1958 break;
1959 }
1960
1961 /*
1962 Update the GPIO/DMIC/SENSE_B pinmux before the configuration
1963 is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
1964 is disabled.
1965 */
David Henningsson5660ffd2012-01-02 12:40:17 +01001966 cs4210_pinmux_init(codec);
Tim Howe56487c22011-07-22 16:41:00 -05001967
1968 err = cs421x_parse_auto_config(codec);
1969 if (err < 0)
1970 goto error;
1971
David Henningsson5660ffd2012-01-02 12:40:17 +01001972 codec->patch_ops = cs421x_patch_ops;
Tim Howe56487c22011-07-22 16:41:00 -05001973
1974 return 0;
1975
1976 error:
1977 kfree(codec->spec);
1978 codec->spec = NULL;
1979 return err;
1980}
1981
David Henningsson5660ffd2012-01-02 12:40:17 +01001982static int patch_cs4213(struct hda_codec *codec)
1983{
1984 struct cs_spec *spec;
1985 int err;
1986
1987 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1988 if (!spec)
1989 return -ENOMEM;
1990 codec->spec = spec;
1991
1992 spec->vendor_nid = CS4213_VENDOR_NID;
1993
1994 err = cs421x_parse_auto_config(codec);
1995 if (err < 0)
1996 goto error;
1997
1998 codec->patch_ops = cs421x_patch_ops;
1999 return 0;
2000
2001 error:
2002 kfree(codec->spec);
2003 codec->spec = NULL;
2004 return err;
2005}
2006
Takashi Iwaie5f14242009-07-01 18:11:44 +02002007
2008/*
2009 * patch entries
2010 */
Takashi Iwaic42d4782011-05-02 11:36:09 +02002011static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
Takashi Iwaie5f14242009-07-01 18:11:44 +02002012 { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
2013 { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
David Henningsson5660ffd2012-01-02 12:40:17 +01002014 { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
2015 { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
Takashi Iwaie5f14242009-07-01 18:11:44 +02002016 {} /* terminator */
2017};
2018
2019MODULE_ALIAS("snd-hda-codec-id:10134206");
2020MODULE_ALIAS("snd-hda-codec-id:10134207");
Tim Howe56487c22011-07-22 16:41:00 -05002021MODULE_ALIAS("snd-hda-codec-id:10134210");
David Henningsson5660ffd2012-01-02 12:40:17 +01002022MODULE_ALIAS("snd-hda-codec-id:10134213");
Takashi Iwaie5f14242009-07-01 18:11:44 +02002023
2024MODULE_LICENSE("GPL");
2025MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
2026
2027static struct hda_codec_preset_list cirrus_list = {
2028 .preset = snd_hda_preset_cirrus,
2029 .owner = THIS_MODULE,
2030};
2031
2032static int __init patch_cirrus_init(void)
2033{
2034 return snd_hda_add_codec_preset(&cirrus_list);
2035}
2036
2037static void __exit patch_cirrus_exit(void)
2038{
2039 snd_hda_delete_codec_preset(&cirrus_list);
2040}
2041
2042module_init(patch_cirrus_init)
2043module_exit(patch_cirrus_exit)