2 * Jack-detection handling for HD-audio
4 * Copyright (c) 2011 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.
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <sound/core.h>
15 #include <sound/control.h>
16 #include "hda_codec.h"
17 #include "hda_local.h"
20 /* execute pin sense measurement */
21 static u32
read_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
25 if (!codec
->no_trigger_sense
) {
26 pincap
= snd_hda_query_pin_caps(codec
, nid
);
27 if (pincap
& AC_PINCAP_TRIG_REQ
) /* need trigger? */
28 snd_hda_codec_read(codec
, nid
, 0,
29 AC_VERB_SET_PIN_SENSE
, 0);
31 return snd_hda_codec_read(codec
, nid
, 0,
32 AC_VERB_GET_PIN_SENSE
, 0);
36 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
39 snd_hda_jack_tbl_get(struct hda_codec
*codec
, hda_nid_t nid
)
41 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
46 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
51 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get
);
54 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
57 snd_hda_jack_tbl_new(struct hda_codec
*codec
, hda_nid_t nid
)
59 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_get(codec
, nid
);
62 snd_array_init(&codec
->jacktbl
, sizeof(*jack
), 16);
63 jack
= snd_array_new(&codec
->jacktbl
);
71 void snd_hda_jack_tbl_clear(struct hda_codec
*codec
)
73 snd_array_free(&codec
->jacktbl
);
76 /* update the cached value and notification flag if needed */
77 static void jack_detect_update(struct hda_codec
*codec
,
78 struct hda_jack_tbl
*jack
)
80 if (jack
->jack_dirty
|| !jack
->jack_cachable
) {
81 unsigned int val
= read_pin_sense(codec
, jack
->nid
);
83 if (val
!= jack
->pin_sense
) {
84 jack
->need_notify
= 1;
85 jack
->pin_sense
= val
;
91 * snd_hda_set_dirty_all - Mark all the cached as dirty
93 * This function sets the dirty flag to all entries of jack table.
94 * It's called from the resume path in hda_codec.c.
96 void snd_hda_jack_set_dirty_all(struct hda_codec
*codec
)
98 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
101 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
103 jack
->jack_dirty
= 1;
105 EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all
);
108 * snd_hda_pin_sense - execute pin sense measurement
109 * @codec: the CODEC to sense
110 * @nid: the pin NID to sense
112 * Execute necessary pin sense measurement and return its Presence Detect,
113 * Impedance, ELD Valid etc. status bits.
115 u32
snd_hda_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
117 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_get(codec
, nid
);
119 jack_detect_update(codec
, jack
);
120 return jack
->pin_sense
;
122 return read_pin_sense(codec
, nid
);
124 EXPORT_SYMBOL_HDA(snd_hda_pin_sense
);
127 * snd_hda_jack_detect - query pin Presence Detect status
128 * @codec: the CODEC to sense
129 * @nid: the pin NID to sense
131 * Query and return the pin's Presence Detect status.
133 int snd_hda_jack_detect(struct hda_codec
*codec
, hda_nid_t nid
)
135 u32 sense
= snd_hda_pin_sense(codec
, nid
);
136 return !!(sense
& AC_PINSENSE_PRESENCE
);
138 EXPORT_SYMBOL_HDA(snd_hda_jack_detect
);
141 * snd_hda_jack_detect_enable - enable the jack-detection
143 int snd_hda_jack_detect_enable(struct hda_codec
*codec
, hda_nid_t nid
,
146 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_new(codec
, nid
);
149 if (jack
->jack_cachable
)
150 return 0; /* already registered */
151 jack
->jack_cachable
= 1;
152 return snd_hda_codec_write_cache(codec
, nid
, 0,
153 AC_VERB_SET_UNSOLICITED_ENABLE
,
156 EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable
);
158 /* queue the notification when needed */
159 static void jack_detect_report(struct hda_codec
*codec
,
160 struct hda_jack_tbl
*jack
)
162 jack_detect_update(codec
, jack
);
163 if (jack
->need_notify
) {
164 snd_ctl_notify(codec
->bus
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
166 jack
->need_notify
= 0;
171 * snd_hda_jack_report - notify kctl when the jack state was changed
173 void snd_hda_jack_report(struct hda_codec
*codec
, hda_nid_t nid
)
175 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_get(codec
, nid
);
178 jack_detect_report(codec
, jack
);
180 EXPORT_SYMBOL_HDA(snd_hda_jack_report
);
183 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
185 void snd_hda_jack_report_sync(struct hda_codec
*codec
)
187 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
190 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
192 jack_detect_update(codec
, jack
);
193 jack_detect_report(codec
, jack
);
196 EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync
);
199 * jack-detection kcontrols
202 #define jack_detect_kctl_info snd_ctl_boolean_mono_info
204 static int jack_detect_kctl_get(struct snd_kcontrol
*kcontrol
,
205 struct snd_ctl_elem_value
*ucontrol
)
207 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
208 hda_nid_t nid
= kcontrol
->private_value
;
210 ucontrol
->value
.integer
.value
[0] = snd_hda_jack_detect(codec
, nid
);
214 static struct snd_kcontrol_new jack_detect_kctl
= {
215 /* name is filled later */
216 .iface
= SNDRV_CTL_ELEM_IFACE_CARD
,
217 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
218 .info
= jack_detect_kctl_info
,
219 .get
= jack_detect_kctl_get
,
223 * snd_hda_jack_add_kctl - Add a kctl for the given pin
225 * This assigns a jack-detection kctl to the given pin. The kcontrol
226 * will have the given name and index.
228 int snd_hda_jack_add_kctl(struct hda_codec
*codec
, hda_nid_t nid
,
229 const char *name
, int idx
)
231 struct hda_jack_tbl
*jack
;
232 struct snd_kcontrol
*kctl
;
234 jack
= snd_hda_jack_tbl_get(codec
, nid
);
238 return 0; /* already created */
239 kctl
= snd_ctl_new1(&jack_detect_kctl
, codec
);
242 snprintf(kctl
->id
.name
, sizeof(kctl
->id
.name
), "%s Jack", name
);
243 kctl
->id
.index
= idx
;
244 kctl
->private_value
= nid
;
245 if (snd_hda_ctl_add(codec
, nid
, kctl
) < 0)
251 static int add_jack_kctl(struct hda_codec
*codec
, hda_nid_t nid
, int idx
,
252 const struct auto_pin_cfg
*cfg
)
256 if (!is_jack_detectable(codec
, nid
))
258 return snd_hda_jack_add_kctl(codec
, nid
,
259 snd_hda_get_pin_label(codec
, nid
, cfg
),
264 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
266 * As of now, it assigns only to the pins that enabled the detection.
267 * Usually this is called at the end of build_controls callback.
269 int snd_hda_jack_add_kctls(struct hda_codec
*codec
,
270 const struct auto_pin_cfg
*cfg
)
275 for (i
= 0, p
= cfg
->line_out_pins
; i
< cfg
->line_outs
; i
++, p
++) {
276 err
= add_jack_kctl(codec
, *p
, i
, cfg
);
280 for (i
= 0, p
= cfg
->hp_pins
; i
< cfg
->hp_outs
; i
++, p
++) {
281 if (*p
== *cfg
->line_out_pins
) /* might be duplicated */
283 err
= add_jack_kctl(codec
, *p
, i
, cfg
);
287 for (i
= 0, p
= cfg
->speaker_pins
; i
< cfg
->speaker_outs
; i
++, p
++) {
288 if (*p
== *cfg
->line_out_pins
) /* might be duplicated */
290 err
= add_jack_kctl(codec
, *p
, i
, cfg
);
294 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
295 err
= add_jack_kctl(codec
, cfg
->inputs
[i
].pin
, 0, cfg
);
299 for (i
= 0, p
= cfg
->dig_out_pins
; i
< cfg
->dig_outs
; i
++, p
++) {
300 err
= add_jack_kctl(codec
, *p
, i
, cfg
);
304 err
= add_jack_kctl(codec
, cfg
->dig_in_pin
, 0, cfg
);
307 err
= add_jack_kctl(codec
, cfg
->mono_out_pin
, 0, cfg
);
312 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls
);