1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Jack-detection handling for HD-audio
5 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/export.h>
11 #include <sound/core.h>
12 #include <sound/control.h>
13 #include <sound/jack.h>
14 #include <sound/hda_codec.h>
15 #include "hda_local.h"
16 #include "hda_auto_parser.h"
20 * is_jack_detectable - Check whether the given pin is jack-detectable
21 * @codec: the HDA codec
24 * Check whether the given pin is capable to report the jack detection.
25 * The jack detection might not work by various reasons, e.g. the jack
26 * detection is prohibited in the codec level, the pin config has
27 * AC_DEFCFG_MISC_NO_PRESENCE bit, no unsol support, etc.
29 bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
31 if (codec->no_jack_detect)
33 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
35 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
36 AC_DEFCFG_MISC_NO_PRESENCE)
38 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) &&
39 !codec->jackpoll_interval)
43 EXPORT_SYMBOL_GPL(is_jack_detectable);
45 /* execute pin sense measurement */
46 static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
51 if (!codec->no_trigger_sense) {
52 pincap = snd_hda_query_pin_caps(codec, nid);
53 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
54 snd_hda_codec_read(codec, nid, 0,
55 AC_VERB_SET_PIN_SENSE, 0);
57 val = snd_hda_codec_read(codec, nid, 0,
58 AC_VERB_GET_PIN_SENSE, 0);
59 if (codec->inv_jack_detect)
60 val ^= AC_PINSENSE_PRESENCE;
65 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
66 * @codec: the HDA codec
67 * @nid: pin NID to refer to
70 snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
72 struct hda_jack_tbl *jack = codec->jacktbl.list;
77 for (i = 0; i < codec->jacktbl.used; i++, jack++)
82 EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get);
85 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
86 * @codec: the HDA codec
87 * @tag: tag value to refer to
90 snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
92 struct hda_jack_tbl *jack = codec->jacktbl.list;
97 for (i = 0; i < codec->jacktbl.used; i++, jack++)
102 EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
105 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
106 * @codec: the HDA codec
107 * @nid: pin NID to assign
109 static struct hda_jack_tbl *
110 snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
112 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
115 jack = snd_array_new(&codec->jacktbl);
119 jack->jack_dirty = 1;
120 jack->tag = codec->jacktbl.used;
124 void snd_hda_jack_tbl_clear(struct hda_codec *codec)
126 struct hda_jack_tbl *jack = codec->jacktbl.list;
129 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
130 struct hda_jack_callback *cb, *next;
132 /* free jack instances manually when clearing/reconfiguring */
133 if (!codec->bus->shutdown && jack->jack)
134 snd_device_free(codec->card, jack->jack);
136 for (cb = jack->callback; cb; cb = next) {
141 snd_array_free(&codec->jacktbl);
144 #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
146 /* update the cached value and notification flag if needed */
147 static void jack_detect_update(struct hda_codec *codec,
148 struct hda_jack_tbl *jack)
150 if (!jack->jack_dirty)
153 if (jack->phantom_jack)
154 jack->pin_sense = AC_PINSENSE_PRESENCE;
156 jack->pin_sense = read_pin_sense(codec, jack->nid);
158 /* A gating jack indicates the jack is invalid if gating is unplugged */
159 if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack))
160 jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
162 jack->jack_dirty = 0;
164 /* If a jack is gated by this one update it. */
165 if (jack->gated_jack) {
166 struct hda_jack_tbl *gated =
167 snd_hda_jack_tbl_get(codec, jack->gated_jack);
169 gated->jack_dirty = 1;
170 jack_detect_update(codec, gated);
176 * snd_hda_set_dirty_all - Mark all the cached as dirty
177 * @codec: the HDA codec
179 * This function sets the dirty flag to all entries of jack table.
180 * It's called from the resume path in hda_codec.c.
182 void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
184 struct hda_jack_tbl *jack = codec->jacktbl.list;
187 for (i = 0; i < codec->jacktbl.used; i++, jack++)
189 jack->jack_dirty = 1;
191 EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
194 * snd_hda_pin_sense - execute pin sense measurement
195 * @codec: the CODEC to sense
196 * @nid: the pin NID to sense
198 * Execute necessary pin sense measurement and return its Presence Detect,
199 * Impedance, ELD Valid etc. status bits.
201 u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
203 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
205 jack_detect_update(codec, jack);
206 return jack->pin_sense;
208 return read_pin_sense(codec, nid);
210 EXPORT_SYMBOL_GPL(snd_hda_pin_sense);
213 * snd_hda_jack_detect_state - query pin Presence Detect status
214 * @codec: the CODEC to sense
215 * @nid: the pin NID to sense
217 * Query and return the pin's Presence Detect status, as either
218 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
220 int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid)
222 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
223 if (jack && jack->phantom_jack)
224 return HDA_JACK_PHANTOM;
225 else if (snd_hda_pin_sense(codec, nid) & AC_PINSENSE_PRESENCE)
226 return HDA_JACK_PRESENT;
228 return HDA_JACK_NOT_PRESENT;
230 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state);
233 * snd_hda_jack_detect_enable - enable the jack-detection
234 * @codec: the HDA codec
235 * @nid: pin NID to enable
236 * @func: callback function to register
238 * In the case of error, the return value will be a pointer embedded with
239 * errno. Check and handle the return value appropriately with standard
240 * macros such as @IS_ERR() and @PTR_ERR().
242 struct hda_jack_callback *
243 snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
244 hda_jack_callback_fn func)
246 struct hda_jack_tbl *jack;
247 struct hda_jack_callback *callback = NULL;
250 jack = snd_hda_jack_tbl_new(codec, nid);
252 return ERR_PTR(-ENOMEM);
254 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
256 return ERR_PTR(-ENOMEM);
257 callback->func = func;
258 callback->nid = jack->nid;
259 callback->next = jack->callback;
260 jack->callback = callback;
263 if (jack->jack_detect)
264 return callback; /* already registered */
265 jack->jack_detect = 1;
266 if (codec->jackpoll_interval > 0)
267 return callback; /* No unsol if we're polling instead */
268 err = snd_hda_codec_write_cache(codec, nid, 0,
269 AC_VERB_SET_UNSOLICITED_ENABLE,
270 AC_USRSP_EN | jack->tag);
275 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback);
278 * snd_hda_jack_detect_enable - Enable the jack detection on the given pin
279 * @codec: the HDA codec
280 * @nid: pin NID to enable jack detection
282 * Enable the jack detection with the default callback. Returns zero if
283 * successful or a negative error code.
285 int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid)
287 return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback(codec, nid, NULL));
289 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
292 * snd_hda_jack_set_gating_jack - Set gating jack.
293 * @codec: the HDA codec
294 * @gated_nid: gated pin NID
295 * @gating_nid: gating pin NID
297 * Indicates the gated jack is only valid when the gating jack is plugged.
299 int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
300 hda_nid_t gating_nid)
302 struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid);
303 struct hda_jack_tbl *gating = snd_hda_jack_tbl_new(codec, gating_nid);
305 if (!gated || !gating)
308 gated->gating_jack = gating_nid;
309 gating->gated_jack = gated_nid;
313 EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
316 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
317 * @codec: the HDA codec
319 void snd_hda_jack_report_sync(struct hda_codec *codec)
321 struct hda_jack_tbl *jack;
324 /* update all jacks at first */
325 jack = codec->jacktbl.list;
326 for (i = 0; i < codec->jacktbl.used; i++, jack++)
328 jack_detect_update(codec, jack);
330 /* report the updated jacks; it's done after updating all jacks
331 * to make sure that all gating jacks properly have been set
333 jack = codec->jacktbl.list;
334 for (i = 0; i < codec->jacktbl.used; i++, jack++)
336 if (!jack->jack || jack->block_report)
338 state = jack->button_state;
339 if (get_jack_plug_state(jack->pin_sense))
341 snd_jack_report(jack->jack, state);
342 if (jack->button_state) {
343 snd_jack_report(jack->jack,
344 state & ~jack->button_state);
345 jack->button_state = 0; /* button released */
349 EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
351 /* guess the jack type from the pin-config */
352 static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
354 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
355 switch (get_defcfg_device(def_conf)) {
356 case AC_JACK_LINE_OUT:
357 case AC_JACK_SPEAKER:
358 return SND_JACK_LINEOUT;
360 return SND_JACK_HEADPHONE;
361 case AC_JACK_SPDIF_OUT:
362 case AC_JACK_DIG_OTHER_OUT:
363 return SND_JACK_AVOUT;
365 return SND_JACK_MICROPHONE;
367 return SND_JACK_LINEIN;
371 static void hda_free_jack_priv(struct snd_jack *jack)
373 struct hda_jack_tbl *jacks = jack->private_data;
379 * snd_hda_jack_add_kctl - Add a kctl for the given pin
380 * @codec: the HDA codec
381 * @nid: pin NID to assign
382 * @name: string name for the jack
383 * @phantom_jack: flag to deal as a phantom jack
384 * @type: jack type bits to be reported, 0 for guessing from pincfg
385 * @keymap: optional jack / key mapping
387 * This assigns a jack-detection kctl to the given pin. The kcontrol
388 * will have the given name and index.
390 int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
391 const char *name, bool phantom_jack,
392 int type, const struct hda_jack_keymap *keymap)
394 struct hda_jack_tbl *jack;
395 const struct hda_jack_keymap *map;
396 int err, state, buttons;
398 jack = snd_hda_jack_tbl_new(codec, nid);
402 return 0; /* already created */
405 type = get_input_jack_type(codec, nid);
409 for (map = keymap; map->type; map++)
410 buttons |= map->type;
413 err = snd_jack_new(codec->card, name, type | buttons,
414 &jack->jack, true, phantom_jack);
418 jack->phantom_jack = !!phantom_jack;
420 jack->button_state = 0;
421 jack->jack->private_data = jack;
422 jack->jack->private_free = hda_free_jack_priv;
424 for (map = keymap; map->type; map++)
425 snd_jack_set_key(jack->jack, map->type, map->key);
428 state = snd_hda_jack_detect(codec, nid);
429 snd_jack_report(jack->jack, state ? jack->type : 0);
433 EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl);
435 static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
436 const struct auto_pin_cfg *cfg,
437 const char *base_name)
439 unsigned int def_conf, conn;
440 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
446 def_conf = snd_hda_codec_get_pincfg(codec, nid);
447 conn = get_defcfg_connect(def_conf);
448 if (conn == AC_JACK_PORT_NONE)
450 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
451 !is_jack_detectable(codec, nid);
454 strlcpy(name, base_name, sizeof(name));
456 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL);
458 /* Example final name: "Internal Mic Phantom Jack" */
459 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
460 err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack, 0, NULL);
465 return snd_hda_jack_detect_enable(codec, nid);
470 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
471 * @codec: the HDA codec
472 * @cfg: pin config table to parse
474 int snd_hda_jack_add_kctls(struct hda_codec *codec,
475 const struct auto_pin_cfg *cfg)
480 for (i = 0; i < cfg->num_inputs; i++) {
481 /* If we have headphone mics; make sure they get the right name
482 before grabbed by output pins */
483 if (cfg->inputs[i].is_headphone_mic) {
484 if (auto_cfg_hp_outs(cfg) == 1)
485 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
486 cfg, "Headphone Mic");
488 err = add_jack_kctl(codec, cfg->inputs[i].pin,
489 cfg, "Headphone Mic");
491 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
497 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
498 err = add_jack_kctl(codec, *p, cfg, NULL);
502 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
503 if (*p == *cfg->line_out_pins) /* might be duplicated */
505 err = add_jack_kctl(codec, *p, cfg, NULL);
509 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
510 if (*p == *cfg->line_out_pins) /* might be duplicated */
512 err = add_jack_kctl(codec, *p, cfg, NULL);
516 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
517 err = add_jack_kctl(codec, *p, cfg, NULL);
521 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
524 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
529 EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
531 static void call_jack_callback(struct hda_codec *codec, unsigned int res,
532 struct hda_jack_tbl *jack)
534 struct hda_jack_callback *cb;
536 for (cb = jack->callback; cb; cb = cb->next) {
541 if (jack->gated_jack) {
542 struct hda_jack_tbl *gated =
543 snd_hda_jack_tbl_get(codec, jack->gated_jack);
545 for (cb = gated->callback; cb; cb = cb->next) {
555 * snd_hda_jack_unsol_event - Handle an unsolicited event
556 * @codec: the HDA codec
557 * @res: the unsolicited event data
559 void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
561 struct hda_jack_tbl *event;
562 int tag = (res & AC_UNSOL_RES_TAG) >> AC_UNSOL_RES_TAG_SHIFT;
564 event = snd_hda_jack_tbl_get_from_tag(codec, tag);
567 event->jack_dirty = 1;
569 call_jack_callback(codec, res, event);
570 snd_hda_jack_report_sync(codec);
572 EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
575 * snd_hda_jack_poll_all - Poll all jacks
576 * @codec: the HDA codec
578 * Poll all detectable jacks with dirty flag, update the status, call
579 * callbacks and call snd_hda_jack_report_sync() if any changes are found.
581 void snd_hda_jack_poll_all(struct hda_codec *codec)
583 struct hda_jack_tbl *jack = codec->jacktbl.list;
586 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
587 unsigned int old_sense;
588 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
590 old_sense = get_jack_plug_state(jack->pin_sense);
591 jack_detect_update(codec, jack);
592 if (old_sense == get_jack_plug_state(jack->pin_sense))
595 call_jack_callback(codec, 0, jack);
598 snd_hda_jack_report_sync(codec);
600 EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);