2 * HD audio interface patch for Cirrus Logic CS420x chip
4 * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
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.
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.
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
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/pci.h>
25 #include <sound/core.h>
26 #include "hda_codec.h"
27 #include "hda_local.h"
34 struct auto_pin_cfg autocfg
;
35 struct hda_multi_out multiout
;
36 struct snd_kcontrol
*vmaster_sw
;
37 struct snd_kcontrol
*vmaster_vol
;
39 hda_nid_t dac_nid
[AUTO_CFG_MAX_OUTS
];
40 hda_nid_t slave_dig_outs
[2];
42 unsigned int input_idx
[AUTO_PIN_LAST
];
43 unsigned int capsrc_idx
[AUTO_PIN_LAST
];
44 hda_nid_t adc_nid
[AUTO_PIN_LAST
];
45 unsigned int adc_idx
[AUTO_PIN_LAST
];
46 unsigned int num_inputs
;
47 unsigned int cur_input
;
48 unsigned int automic_idx
;
50 unsigned int cur_adc_stream_tag
;
51 unsigned int cur_adc_format
;
54 struct hda_bind_ctls
*capture_bind
[2];
56 unsigned int gpio_mask
;
57 unsigned int gpio_dir
;
58 unsigned int gpio_data
;
60 struct hda_pcm pcm_rec
[2]; /* PCM information */
62 unsigned int hp_detect
:1;
63 unsigned int mic_detect
:1;
66 /* available models */
73 /* Vendor-specific processing widget */
74 #define CS420X_VENDOR_NID 0x11
75 #define CS_DIG_OUT1_PIN_NID 0x10
76 #define CS_DIG_OUT2_PIN_NID 0x15
77 #define CS_DMIC1_PIN_NID 0x12
78 #define CS_DMIC2_PIN_NID 0x0e
81 #define IDX_SPDIF_STAT 0x0000
82 #define IDX_SPDIF_CTL 0x0001
83 #define IDX_ADC_CFG 0x0002
84 /* SZC bitmask, 4 modes below:
86 * 1 = digital immediate, analog zero-cross
87 * 2 = digtail & analog soft-ramp
88 * 3 = digital soft-ramp, analog zero-cross
90 #define CS_COEF_ADC_SZC_MASK (3 << 0)
91 #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
92 #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
93 /* PGA mode: 0 = differential, 1 = signle-ended */
94 #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
95 #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
96 #define IDX_DAC_CFG 0x0003
97 /* SZC bitmask, 4 modes below:
101 * 3 = soft-ramp on zero-cross
103 #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
104 #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
105 #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
107 #define IDX_BEEP_CFG 0x0004
108 /* 0x0008 - test reg key */
109 /* 0x0009 - 0x0014 -> 12 test regs */
110 /* 0x0015 - visibility reg */
113 static inline int cs_vendor_coef_get(struct hda_codec
*codec
, unsigned int idx
)
115 snd_hda_codec_write(codec
, CS420X_VENDOR_NID
, 0,
116 AC_VERB_SET_COEF_INDEX
, idx
);
117 return snd_hda_codec_read(codec
, CS420X_VENDOR_NID
, 0,
118 AC_VERB_GET_PROC_COEF
, 0);
121 static inline void cs_vendor_coef_set(struct hda_codec
*codec
, unsigned int idx
,
124 snd_hda_codec_write(codec
, CS420X_VENDOR_NID
, 0,
125 AC_VERB_SET_COEF_INDEX
, idx
);
126 snd_hda_codec_write(codec
, CS420X_VENDOR_NID
, 0,
127 AC_VERB_SET_PROC_COEF
, coef
);
137 static int cs_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
138 struct hda_codec
*codec
,
139 struct snd_pcm_substream
*substream
)
141 struct cs_spec
*spec
= codec
->spec
;
142 return snd_hda_multi_out_analog_open(codec
, &spec
->multiout
, substream
,
146 static int cs_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
147 struct hda_codec
*codec
,
148 unsigned int stream_tag
,
150 struct snd_pcm_substream
*substream
)
152 struct cs_spec
*spec
= codec
->spec
;
153 return snd_hda_multi_out_analog_prepare(codec
, &spec
->multiout
,
154 stream_tag
, format
, substream
);
157 static int cs_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
158 struct hda_codec
*codec
,
159 struct snd_pcm_substream
*substream
)
161 struct cs_spec
*spec
= codec
->spec
;
162 return snd_hda_multi_out_analog_cleanup(codec
, &spec
->multiout
);
168 static int cs_dig_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
169 struct hda_codec
*codec
,
170 struct snd_pcm_substream
*substream
)
172 struct cs_spec
*spec
= codec
->spec
;
173 return snd_hda_multi_out_dig_open(codec
, &spec
->multiout
);
176 static int cs_dig_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
177 struct hda_codec
*codec
,
178 struct snd_pcm_substream
*substream
)
180 struct cs_spec
*spec
= codec
->spec
;
181 return snd_hda_multi_out_dig_close(codec
, &spec
->multiout
);
184 static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
185 struct hda_codec
*codec
,
186 unsigned int stream_tag
,
188 struct snd_pcm_substream
*substream
)
190 struct cs_spec
*spec
= codec
->spec
;
191 return snd_hda_multi_out_dig_prepare(codec
, &spec
->multiout
, stream_tag
,
195 static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
196 struct hda_codec
*codec
,
197 struct snd_pcm_substream
*substream
)
199 struct cs_spec
*spec
= codec
->spec
;
200 return snd_hda_multi_out_dig_cleanup(codec
, &spec
->multiout
);
206 static int cs_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
207 struct hda_codec
*codec
,
208 unsigned int stream_tag
,
210 struct snd_pcm_substream
*substream
)
212 struct cs_spec
*spec
= codec
->spec
;
213 spec
->cur_adc
= spec
->adc_nid
[spec
->cur_input
];
214 spec
->cur_adc_stream_tag
= stream_tag
;
215 spec
->cur_adc_format
= format
;
216 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
, stream_tag
, 0, format
);
220 static int cs_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
221 struct hda_codec
*codec
,
222 struct snd_pcm_substream
*substream
)
224 struct cs_spec
*spec
= codec
->spec
;
225 snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
);
232 static struct hda_pcm_stream cs_pcm_analog_playback
= {
237 .open
= cs_playback_pcm_open
,
238 .prepare
= cs_playback_pcm_prepare
,
239 .cleanup
= cs_playback_pcm_cleanup
243 static struct hda_pcm_stream cs_pcm_analog_capture
= {
248 .prepare
= cs_capture_pcm_prepare
,
249 .cleanup
= cs_capture_pcm_cleanup
253 static struct hda_pcm_stream cs_pcm_digital_playback
= {
258 .open
= cs_dig_playback_pcm_open
,
259 .close
= cs_dig_playback_pcm_close
,
260 .prepare
= cs_dig_playback_pcm_prepare
,
261 .cleanup
= cs_dig_playback_pcm_cleanup
265 static struct hda_pcm_stream cs_pcm_digital_capture
= {
271 static int cs_build_pcms(struct hda_codec
*codec
)
273 struct cs_spec
*spec
= codec
->spec
;
274 struct hda_pcm
*info
= spec
->pcm_rec
;
276 codec
->pcm_info
= info
;
279 info
->name
= "Cirrus Analog";
280 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] = cs_pcm_analog_playback
;
281 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
= spec
->dac_nid
[0];
282 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].channels_max
=
283 spec
->multiout
.max_channels
;
284 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] = cs_pcm_analog_capture
;
285 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
=
286 spec
->adc_nid
[spec
->cur_input
];
289 if (!spec
->multiout
.dig_out_nid
&& !spec
->dig_in
)
293 info
->name
= "Cirrus Digital";
294 info
->pcm_type
= spec
->autocfg
.dig_out_type
[0];
296 info
->pcm_type
= HDA_PCM_TYPE_SPDIF
;
297 if (spec
->multiout
.dig_out_nid
) {
298 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] =
299 cs_pcm_digital_playback
;
300 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
=
301 spec
->multiout
.dig_out_nid
;
304 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] =
305 cs_pcm_digital_capture
;
306 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
= spec
->dig_in
;
314 * parse codec topology
317 static hda_nid_t
get_dac(struct hda_codec
*codec
, hda_nid_t pin
)
322 if (snd_hda_get_connections(codec
, pin
, &dac
, 1) != 1)
327 static int is_ext_mic(struct hda_codec
*codec
, unsigned int idx
)
329 struct cs_spec
*spec
= codec
->spec
;
330 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
331 hda_nid_t pin
= cfg
->input_pins
[idx
];
332 unsigned int val
= snd_hda_query_pin_caps(codec
, pin
);
333 if (!(val
& AC_PINCAP_PRES_DETECT
))
335 val
= snd_hda_codec_get_pincfg(codec
, pin
);
336 return (get_defcfg_connect(val
) == AC_JACK_PORT_COMPLEX
);
339 static hda_nid_t
get_adc(struct hda_codec
*codec
, hda_nid_t pin
,
345 nid
= codec
->start_nid
;
346 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
350 type
= (get_wcaps(codec
, nid
) & AC_WCAP_TYPE
)
351 >> AC_WCAP_TYPE_SHIFT
;
352 if (type
!= AC_WID_AUD_IN
)
354 nums
= snd_hda_get_connections(codec
, nid
, pins
,
358 for (j
= 0; j
< nums
; j
++) {
359 if (pins
[j
] == pin
) {
368 static int is_active_pin(struct hda_codec
*codec
, hda_nid_t nid
)
371 val
= snd_hda_codec_get_pincfg(codec
, nid
);
372 return (get_defcfg_connect(val
) != AC_JACK_PORT_NONE
);
375 static int parse_output(struct hda_codec
*codec
)
377 struct cs_spec
*spec
= codec
->spec
;
378 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
382 for (i
= 0; i
< cfg
->line_outs
; i
++) {
383 dac
= get_dac(codec
, cfg
->line_out_pins
[i
]);
386 spec
->dac_nid
[i
] = dac
;
388 spec
->multiout
.num_dacs
= i
;
389 spec
->multiout
.dac_nids
= spec
->dac_nid
;
390 spec
->multiout
.max_channels
= i
* 2;
392 /* add HP and speakers */
394 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
395 dac
= get_dac(codec
, cfg
->hp_pins
[i
]);
399 spec
->multiout
.hp_nid
= dac
;
401 spec
->multiout
.extra_out_nid
[extra_nids
++] = dac
;
403 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
404 dac
= get_dac(codec
, cfg
->speaker_pins
[i
]);
407 spec
->multiout
.extra_out_nid
[extra_nids
++] = dac
;
410 if (cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
411 cfg
->speaker_outs
= cfg
->line_outs
;
412 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
413 sizeof(cfg
->speaker_pins
));
420 static int parse_input(struct hda_codec
*codec
)
422 struct cs_spec
*spec
= codec
->spec
;
423 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
426 for (i
= 0; i
< AUTO_PIN_LAST
; i
++) {
427 hda_nid_t pin
= cfg
->input_pins
[i
];
430 spec
->input_idx
[spec
->num_inputs
] = i
;
431 spec
->capsrc_idx
[i
] = spec
->num_inputs
++;
433 spec
->adc_nid
[i
] = get_adc(codec
, pin
, &spec
->adc_idx
[i
]);
435 if (!spec
->num_inputs
)
438 /* check whether the automatic mic switch is available */
439 if (spec
->num_inputs
== 2 &&
440 spec
->adc_nid
[AUTO_PIN_MIC
] && spec
->adc_nid
[AUTO_PIN_FRONT_MIC
]) {
441 if (is_ext_mic(codec
, cfg
->input_pins
[AUTO_PIN_FRONT_MIC
])) {
442 if (!is_ext_mic(codec
, cfg
->input_pins
[AUTO_PIN_MIC
])) {
443 spec
->mic_detect
= 1;
444 spec
->automic_idx
= AUTO_PIN_FRONT_MIC
;
447 if (is_ext_mic(codec
, cfg
->input_pins
[AUTO_PIN_MIC
])) {
448 spec
->mic_detect
= 1;
449 spec
->automic_idx
= AUTO_PIN_MIC
;
457 static int parse_digital_output(struct hda_codec
*codec
)
459 struct cs_spec
*spec
= codec
->spec
;
460 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
465 if (snd_hda_get_connections(codec
, cfg
->dig_out_pins
[0], &nid
, 1) < 1)
467 spec
->multiout
.dig_out_nid
= nid
;
468 spec
->multiout
.share_spdif
= 1;
469 if (cfg
->dig_outs
> 1 &&
470 snd_hda_get_connections(codec
, cfg
->dig_out_pins
[1], &nid
, 1) > 0) {
471 spec
->slave_dig_outs
[0] = nid
;
472 codec
->slave_dig_outs
= spec
->slave_dig_outs
;
477 static int parse_digital_input(struct hda_codec
*codec
)
479 struct cs_spec
*spec
= codec
->spec
;
480 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
484 spec
->dig_in
= get_adc(codec
, cfg
->dig_in_pin
, &idx
);
489 * create mixer controls
492 static const char *dir_sfx
[2] = { "Playback", "Capture" };
494 static int add_mute(struct hda_codec
*codec
, const char *name
, int index
,
495 unsigned int pval
, int dir
, struct snd_kcontrol
**kctlp
)
498 struct snd_kcontrol_new knew
=
499 HDA_CODEC_MUTE_IDX(tmp
, index
, 0, 0, HDA_OUTPUT
);
500 knew
.private_value
= pval
;
501 snprintf(tmp
, sizeof(tmp
), "%s %s Switch", name
, dir_sfx
[dir
]);
502 *kctlp
= snd_ctl_new1(&knew
, codec
);
503 (*kctlp
)->id
.subdevice
= HDA_SUBDEV_AMP_FLAG
;
504 return snd_hda_ctl_add(codec
, get_amp_nid_(pval
), *kctlp
);
507 static int add_volume(struct hda_codec
*codec
, const char *name
,
508 int index
, unsigned int pval
, int dir
,
509 struct snd_kcontrol
**kctlp
)
512 struct snd_kcontrol_new knew
=
513 HDA_CODEC_VOLUME_IDX(tmp
, index
, 0, 0, HDA_OUTPUT
);
514 knew
.private_value
= pval
;
515 snprintf(tmp
, sizeof(tmp
), "%s %s Volume", name
, dir_sfx
[dir
]);
516 *kctlp
= snd_ctl_new1(&knew
, codec
);
517 (*kctlp
)->id
.subdevice
= HDA_SUBDEV_AMP_FLAG
;
518 return snd_hda_ctl_add(codec
, get_amp_nid_(pval
), *kctlp
);
521 static void fix_volume_caps(struct hda_codec
*codec
, hda_nid_t dac
)
525 /* set the upper-limit for mixer amp to 0dB */
526 caps
= query_amp_caps(codec
, dac
, HDA_OUTPUT
);
527 caps
&= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT
);
528 caps
|= ((caps
>> AC_AMPCAP_OFFSET_SHIFT
) & 0x7f)
529 << AC_AMPCAP_NUM_STEPS_SHIFT
;
530 snd_hda_override_amp_caps(codec
, dac
, HDA_OUTPUT
, caps
);
533 static int add_vmaster(struct hda_codec
*codec
, hda_nid_t dac
)
535 struct cs_spec
*spec
= codec
->spec
;
540 snd_ctl_make_virtual_master("Master Playback Switch", NULL
);
541 err
= snd_hda_ctl_add(codec
, dac
, spec
->vmaster_sw
);
545 snd_hda_set_vmaster_tlv(codec
, dac
, HDA_OUTPUT
, tlv
);
547 snd_ctl_make_virtual_master("Master Playback Volume", tlv
);
548 err
= snd_hda_ctl_add(codec
, dac
, spec
->vmaster_vol
);
554 static int add_output(struct hda_codec
*codec
, hda_nid_t dac
, int idx
,
555 int num_ctls
, int type
)
557 struct cs_spec
*spec
= codec
->spec
;
560 struct snd_kcontrol
*kctl
;
561 static char *speakers
[] = {
562 "Front Speaker", "Surround Speaker", "Bass Speaker"
564 static char *line_outs
[] = {
565 "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
568 fix_volume_caps(codec
, dac
);
569 if (!spec
->vmaster_sw
) {
570 err
= add_vmaster(codec
, dac
);
577 case AUTO_PIN_HP_OUT
:
581 case AUTO_PIN_SPEAKER_OUT
:
583 name
= speakers
[idx
];
589 name
= line_outs
[idx
];
595 err
= add_mute(codec
, name
, index
,
596 HDA_COMPOSE_AMP_VAL(dac
, 3, 0, HDA_OUTPUT
), 0, &kctl
);
599 err
= snd_ctl_add_slave(spec
->vmaster_sw
, kctl
);
603 err
= add_volume(codec
, name
, index
,
604 HDA_COMPOSE_AMP_VAL(dac
, 3, 0, HDA_OUTPUT
), 0, &kctl
);
607 err
= snd_ctl_add_slave(spec
->vmaster_vol
, kctl
);
614 static int build_output(struct hda_codec
*codec
)
616 struct cs_spec
*spec
= codec
->spec
;
617 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
620 for (i
= 0; i
< cfg
->line_outs
; i
++) {
621 err
= add_output(codec
, get_dac(codec
, cfg
->line_out_pins
[i
]),
622 i
, cfg
->line_outs
, cfg
->line_out_type
);
626 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
627 err
= add_output(codec
, get_dac(codec
, cfg
->hp_pins
[i
]),
628 i
, cfg
->hp_outs
, AUTO_PIN_HP_OUT
);
632 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
633 err
= add_output(codec
, get_dac(codec
, cfg
->speaker_pins
[i
]),
634 i
, cfg
->speaker_outs
, AUTO_PIN_SPEAKER_OUT
);
644 static struct snd_kcontrol_new cs_capture_ctls
[] = {
645 HDA_BIND_SW("Capture Switch", 0),
646 HDA_BIND_VOL("Capture Volume", 0),
649 static int change_cur_input(struct hda_codec
*codec
, unsigned int idx
,
652 struct cs_spec
*spec
= codec
->spec
;
654 if (spec
->cur_input
== idx
&& !force
)
656 if (spec
->cur_adc
&& spec
->cur_adc
!= spec
->adc_nid
[idx
]) {
657 /* stream is running, let's swap the current ADC */
658 snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
);
659 spec
->cur_adc
= spec
->adc_nid
[idx
];
660 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
,
661 spec
->cur_adc_stream_tag
, 0,
662 spec
->cur_adc_format
);
664 snd_hda_codec_write(codec
, spec
->cur_adc
, 0,
665 AC_VERB_SET_CONNECT_SEL
,
667 spec
->cur_input
= idx
;
671 static int cs_capture_source_info(struct snd_kcontrol
*kcontrol
,
672 struct snd_ctl_elem_info
*uinfo
)
674 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
675 struct cs_spec
*spec
= codec
->spec
;
678 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
680 uinfo
->value
.enumerated
.items
= spec
->num_inputs
;
681 if (uinfo
->value
.enumerated
.item
>= spec
->num_inputs
)
682 uinfo
->value
.enumerated
.item
= spec
->num_inputs
- 1;
683 idx
= spec
->input_idx
[uinfo
->value
.enumerated
.item
];
684 strcpy(uinfo
->value
.enumerated
.name
, auto_pin_cfg_labels
[idx
]);
688 static int cs_capture_source_get(struct snd_kcontrol
*kcontrol
,
689 struct snd_ctl_elem_value
*ucontrol
)
691 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
692 struct cs_spec
*spec
= codec
->spec
;
693 ucontrol
->value
.enumerated
.item
[0] = spec
->capsrc_idx
[spec
->cur_input
];
697 static int cs_capture_source_put(struct snd_kcontrol
*kcontrol
,
698 struct snd_ctl_elem_value
*ucontrol
)
700 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
701 struct cs_spec
*spec
= codec
->spec
;
702 unsigned int idx
= ucontrol
->value
.enumerated
.item
[0];
704 if (idx
>= spec
->num_inputs
)
706 idx
= spec
->input_idx
[idx
];
707 return change_cur_input(codec
, idx
, 0);
710 static struct snd_kcontrol_new cs_capture_source
= {
711 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
712 .name
= "Capture Source",
713 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
714 .info
= cs_capture_source_info
,
715 .get
= cs_capture_source_get
,
716 .put
= cs_capture_source_put
,
719 static struct hda_bind_ctls
*make_bind_capture(struct hda_codec
*codec
,
720 struct hda_ctl_ops
*ops
)
722 struct cs_spec
*spec
= codec
->spec
;
723 struct hda_bind_ctls
*bind
;
726 bind
= kzalloc(sizeof(*bind
) + sizeof(long) * (spec
->num_inputs
+ 1),
732 for (i
= 0; i
< AUTO_PIN_LAST
; i
++) {
733 if (!spec
->adc_nid
[i
])
736 HDA_COMPOSE_AMP_VAL(spec
->adc_nid
[i
], 3,
737 spec
->adc_idx
[i
], HDA_INPUT
);
742 static int build_input(struct hda_codec
*codec
)
744 struct cs_spec
*spec
= codec
->spec
;
747 if (!spec
->num_inputs
)
750 /* make bind-capture */
751 spec
->capture_bind
[0] = make_bind_capture(codec
, &snd_hda_bind_sw
);
752 spec
->capture_bind
[1] = make_bind_capture(codec
, &snd_hda_bind_vol
);
753 for (i
= 0; i
< 2; i
++) {
754 struct snd_kcontrol
*kctl
;
755 if (!spec
->capture_bind
[i
])
757 kctl
= snd_ctl_new1(&cs_capture_ctls
[i
], codec
);
760 kctl
->private_value
= (long)spec
->capture_bind
[i
];
761 err
= snd_hda_ctl_add(codec
, 0, kctl
);
764 err
= snd_hda_add_nids(codec
, kctl
, 0, spec
->adc_nid
,
770 if (spec
->num_inputs
> 1 && !spec
->mic_detect
) {
771 err
= snd_hda_ctl_add(codec
, 0,
772 snd_ctl_new1(&cs_capture_source
, codec
));
783 static int build_digital_output(struct hda_codec
*codec
)
785 struct cs_spec
*spec
= codec
->spec
;
788 if (!spec
->multiout
.dig_out_nid
)
791 err
= snd_hda_create_spdif_out_ctls(codec
, spec
->multiout
.dig_out_nid
);
794 err
= snd_hda_create_spdif_share_sw(codec
, &spec
->multiout
);
800 static int build_digital_input(struct hda_codec
*codec
)
802 struct cs_spec
*spec
= codec
->spec
;
804 return snd_hda_create_spdif_in_ctls(codec
, spec
->dig_in
);
809 * auto-mute and auto-mic switching
812 static void cs_automute(struct hda_codec
*codec
)
814 struct cs_spec
*spec
= codec
->spec
;
815 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
816 unsigned int caps
, hp_present
;
821 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
822 nid
= cfg
->hp_pins
[i
];
823 caps
= snd_hda_query_pin_caps(codec
, nid
);
824 if (!(caps
& AC_PINCAP_PRES_DETECT
))
826 hp_present
= snd_hda_jack_detect(codec
, nid
);
830 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
831 nid
= cfg
->speaker_pins
[i
];
832 snd_hda_codec_write(codec
, nid
, 0,
833 AC_VERB_SET_PIN_WIDGET_CONTROL
,
834 hp_present
? 0 : PIN_OUT
);
836 if (spec
->board_config
== CS420X_MBP55
) {
837 unsigned int gpio
= hp_present
? 0x02 : 0x08;
838 snd_hda_codec_write(codec
, 0x01, 0,
839 AC_VERB_SET_GPIO_DATA
, gpio
);
843 static void cs_automic(struct hda_codec
*codec
)
845 struct cs_spec
*spec
= codec
->spec
;
846 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
848 unsigned int present
;
850 nid
= cfg
->input_pins
[spec
->automic_idx
];
851 present
= snd_hda_jack_detect(codec
, nid
);
853 change_cur_input(codec
, spec
->automic_idx
, 0);
855 unsigned int imic
= (spec
->automic_idx
== AUTO_PIN_MIC
) ?
856 AUTO_PIN_FRONT_MIC
: AUTO_PIN_MIC
;
857 change_cur_input(codec
, imic
, 0);
864 static void init_output(struct hda_codec
*codec
)
866 struct cs_spec
*spec
= codec
->spec
;
867 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
871 for (i
= 0; i
< spec
->multiout
.num_dacs
; i
++)
872 snd_hda_codec_write(codec
, spec
->multiout
.dac_nids
[i
], 0,
873 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_MUTE
);
874 if (spec
->multiout
.hp_nid
)
875 snd_hda_codec_write(codec
, spec
->multiout
.hp_nid
, 0,
876 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_MUTE
);
877 for (i
= 0; i
< ARRAY_SIZE(spec
->multiout
.extra_out_nid
); i
++) {
878 if (!spec
->multiout
.extra_out_nid
[i
])
880 snd_hda_codec_write(codec
, spec
->multiout
.extra_out_nid
[i
], 0,
881 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_MUTE
);
884 /* set appropriate pin controls */
885 for (i
= 0; i
< cfg
->line_outs
; i
++)
886 snd_hda_codec_write(codec
, cfg
->line_out_pins
[i
], 0,
887 AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_OUT
);
888 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
889 hda_nid_t nid
= cfg
->hp_pins
[i
];
890 snd_hda_codec_write(codec
, nid
, 0,
891 AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_HP
);
892 if (!cfg
->speaker_outs
)
894 if (get_wcaps(codec
, nid
) & AC_WCAP_UNSOL_CAP
) {
895 snd_hda_codec_write(codec
, nid
, 0,
896 AC_VERB_SET_UNSOLICITED_ENABLE
,
897 AC_USRSP_EN
| HP_EVENT
);
901 for (i
= 0; i
< cfg
->speaker_outs
; i
++)
902 snd_hda_codec_write(codec
, cfg
->speaker_pins
[i
], 0,
903 AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_OUT
);
908 static void init_input(struct hda_codec
*codec
)
910 struct cs_spec
*spec
= codec
->spec
;
911 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
915 for (i
= 0; i
< AUTO_PIN_LAST
; i
++) {
917 hda_nid_t pin
= cfg
->input_pins
[i
];
918 if (!pin
|| !spec
->adc_nid
[i
])
920 /* set appropriate pin control and mute first */
922 if (i
<= AUTO_PIN_FRONT_MIC
) {
923 unsigned int caps
= snd_hda_query_pin_caps(codec
, pin
);
924 caps
>>= AC_PINCAP_VREF_SHIFT
;
925 if (caps
& AC_PINCAP_VREF_80
)
928 snd_hda_codec_write(codec
, pin
, 0,
929 AC_VERB_SET_PIN_WIDGET_CONTROL
, ctl
);
930 snd_hda_codec_write(codec
, spec
->adc_nid
[i
], 0,
931 AC_VERB_SET_AMP_GAIN_MUTE
,
932 AMP_IN_MUTE(spec
->adc_idx
[i
]));
933 if (spec
->mic_detect
&& spec
->automic_idx
== i
)
934 snd_hda_codec_write(codec
, pin
, 0,
935 AC_VERB_SET_UNSOLICITED_ENABLE
,
936 AC_USRSP_EN
| MIC_EVENT
);
938 change_cur_input(codec
, spec
->cur_input
, 1);
939 if (spec
->mic_detect
)
942 coef
= 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
943 if (is_active_pin(codec
, CS_DMIC2_PIN_NID
))
944 coef
|= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
945 if (is_active_pin(codec
, CS_DMIC1_PIN_NID
))
946 coef
|= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0
947 * No effect if SPDIF_OUT2 is selected in
950 cs_vendor_coef_set(codec
, IDX_ADC_CFG
, coef
);
953 static struct hda_verb cs_coef_init_verbs
[] = {
954 {0x11, AC_VERB_SET_PROC_STATE
, 1},
955 {0x11, AC_VERB_SET_COEF_INDEX
, IDX_DAC_CFG
},
956 {0x11, AC_VERB_SET_PROC_COEF
,
957 (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
958 | 0x0040 /* Mute DACs on FIFO error */
959 | 0x1000 /* Enable DACs High Pass Filter */
960 | 0x0400 /* Disable Coefficient Auto increment */
963 {0x11, AC_VERB_SET_COEF_INDEX
, IDX_DAC_CFG
},
964 {0x11, AC_VERB_SET_PROC_COEF
, 0x0007}, /* Enable Beep thru DAC1/2/3 */
970 static void init_digital(struct hda_codec
*codec
)
974 coef
= 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
975 coef
|= 0x0008; /* Replace with mute on error */
976 if (is_active_pin(codec
, CS_DIG_OUT2_PIN_NID
))
977 coef
|= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
978 * SPDIF_OUT2 is shared with GPIO1 and
981 cs_vendor_coef_set(codec
, IDX_SPDIF_CTL
, coef
);
984 static int cs_init(struct hda_codec
*codec
)
986 struct cs_spec
*spec
= codec
->spec
;
988 snd_hda_sequence_write(codec
, cs_coef_init_verbs
);
990 if (spec
->gpio_mask
) {
991 snd_hda_codec_write(codec
, 0x01, 0, AC_VERB_SET_GPIO_MASK
,
993 snd_hda_codec_write(codec
, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION
,
995 snd_hda_codec_write(codec
, 0x01, 0, AC_VERB_SET_GPIO_DATA
,
1001 init_digital(codec
);
1005 static int cs_build_controls(struct hda_codec
*codec
)
1009 err
= build_output(codec
);
1012 err
= build_input(codec
);
1015 err
= build_digital_output(codec
);
1018 err
= build_digital_input(codec
);
1021 return cs_init(codec
);
1024 static void cs_free(struct hda_codec
*codec
)
1026 struct cs_spec
*spec
= codec
->spec
;
1027 kfree(spec
->capture_bind
[0]);
1028 kfree(spec
->capture_bind
[1]);
1032 static void cs_unsol_event(struct hda_codec
*codec
, unsigned int res
)
1034 switch ((res
>> 26) & 0x7f) {
1044 static struct hda_codec_ops cs_patch_ops
= {
1045 .build_controls
= cs_build_controls
,
1046 .build_pcms
= cs_build_pcms
,
1049 .unsol_event
= cs_unsol_event
,
1052 static int cs_parse_auto_config(struct hda_codec
*codec
)
1054 struct cs_spec
*spec
= codec
->spec
;
1057 err
= snd_hda_parse_pin_def_config(codec
, &spec
->autocfg
, NULL
);
1061 err
= parse_output(codec
);
1064 err
= parse_input(codec
);
1067 err
= parse_digital_output(codec
);
1070 err
= parse_digital_input(codec
);
1076 static const char *cs420x_models
[CS420X_MODELS
] = {
1077 [CS420X_MBP55
] = "mbp55",
1078 [CS420X_AUTO
] = "auto",
1082 static struct snd_pci_quirk cs420x_cfg_tbl
[] = {
1083 SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55
),
1092 static struct cs_pincfg mbp55_pincfgs
[] = {
1093 { 0x09, 0x012b4030 },
1094 { 0x0a, 0x90100121 },
1095 { 0x0b, 0x90100120 },
1096 { 0x0c, 0x400000f0 },
1097 { 0x0d, 0x90a00110 },
1098 { 0x0e, 0x400000f0 },
1099 { 0x0f, 0x400000f0 },
1100 { 0x10, 0x014be040 },
1101 { 0x12, 0x400000f0 },
1102 { 0x15, 0x400000f0 },
1106 static struct cs_pincfg
*cs_pincfgs
[CS420X_MODELS
] = {
1107 [CS420X_MBP55
] = mbp55_pincfgs
,
1110 static void fix_pincfg(struct hda_codec
*codec
, int model
)
1112 const struct cs_pincfg
*cfg
= cs_pincfgs
[model
];
1115 for (; cfg
->nid
; cfg
++)
1116 snd_hda_codec_set_pincfg(codec
, cfg
->nid
, cfg
->val
);
1120 static int patch_cs420x(struct hda_codec
*codec
)
1122 struct cs_spec
*spec
;
1125 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
1130 spec
->board_config
=
1131 snd_hda_check_board_config(codec
, CS420X_MODELS
,
1132 cs420x_models
, cs420x_cfg_tbl
);
1133 if (spec
->board_config
>= 0)
1134 fix_pincfg(codec
, spec
->board_config
);
1136 switch (spec
->board_config
) {
1138 /* GPIO1 = headphones */
1139 /* GPIO3 = speakers */
1140 spec
->gpio_mask
= 0x0a;
1141 spec
->gpio_dir
= 0x0a;
1145 err
= cs_parse_auto_config(codec
);
1149 codec
->patch_ops
= cs_patch_ops
;
1163 static struct hda_codec_preset snd_hda_preset_cirrus
[] = {
1164 { .id
= 0x10134206, .name
= "CS4206", .patch
= patch_cs420x
},
1165 { .id
= 0x10134207, .name
= "CS4207", .patch
= patch_cs420x
},
1169 MODULE_ALIAS("snd-hda-codec-id:10134206");
1170 MODULE_ALIAS("snd-hda-codec-id:10134207");
1172 MODULE_LICENSE("GPL");
1173 MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
1175 static struct hda_codec_preset_list cirrus_list
= {
1176 .preset
= snd_hda_preset_cirrus
,
1177 .owner
= THIS_MODULE
,
1180 static int __init
patch_cirrus_init(void)
1182 return snd_hda_add_codec_preset(&cirrus_list
);
1185 static void __exit
patch_cirrus_exit(void)
1187 snd_hda_delete_codec_preset(&cirrus_list
);
1190 module_init(patch_cirrus_init
)
1191 module_exit(patch_cirrus_exit
)