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, int dev_id)
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, dev_id);
59 if (codec->inv_jack_detect)
60 val ^= AC_PINSENSE_PRESENCE;
65 * snd_hda_jack_tbl_get_mst - query the jack-table entry for the given NID
66 * @codec: the HDA codec
67 * @nid: pin NID to refer to
68 * @dev_id: pin device entry id
71 snd_hda_jack_tbl_get_mst(struct hda_codec *codec, hda_nid_t nid, int dev_id)
73 struct hda_jack_tbl *jack = codec->jacktbl.list;
78 for (i = 0; i < codec->jacktbl.used; i++, jack++)
79 if (jack->nid == nid && jack->dev_id == dev_id)
83 EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_mst);
86 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
87 * @codec: the HDA codec
88 * @tag: tag value to refer to
89 * @dev_id: pin device entry id
92 snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec,
93 unsigned char tag, int dev_id)
95 struct hda_jack_tbl *jack = codec->jacktbl.list;
100 for (i = 0; i < codec->jacktbl.used; i++, jack++)
101 if (jack->tag == tag && jack->dev_id == dev_id)
105 EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
107 static struct hda_jack_tbl *
108 any_jack_tbl_get_from_nid(struct hda_codec *codec, hda_nid_t nid)
110 struct hda_jack_tbl *jack = codec->jacktbl.list;
115 for (i = 0; i < codec->jacktbl.used; i++, jack++)
116 if (jack->nid == nid)
122 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
123 * @codec: the HDA codec
124 * @nid: pin NID to assign
125 * @dev_id: pin device entry id
127 static struct hda_jack_tbl *
128 snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid, int dev_id)
130 struct hda_jack_tbl *jack =
131 snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
132 struct hda_jack_tbl *existing_nid_jack =
133 any_jack_tbl_get_from_nid(codec, nid);
135 WARN_ON(dev_id != 0 && !codec->dp_mst);
139 jack = snd_array_new(&codec->jacktbl);
143 jack->dev_id = dev_id;
144 jack->jack_dirty = 1;
145 if (existing_nid_jack) {
146 jack->tag = existing_nid_jack->tag;
149 * Copy jack_detect from existing_nid_jack to avoid
150 * snd_hda_jack_detect_enable_callback_mst() making multiple
151 * SET_UNSOLICITED_ENABLE calls on the same pin.
153 jack->jack_detect = existing_nid_jack->jack_detect;
155 jack->tag = codec->jacktbl.used;
161 void snd_hda_jack_tbl_clear(struct hda_codec *codec)
163 struct hda_jack_tbl *jack = codec->jacktbl.list;
166 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
167 struct hda_jack_callback *cb, *next;
169 /* free jack instances manually when clearing/reconfiguring */
170 if (!codec->bus->shutdown && jack->jack)
171 snd_device_free(codec->card, jack->jack);
173 for (cb = jack->callback; cb; cb = next) {
178 snd_array_free(&codec->jacktbl);
181 #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
183 /* update the cached value and notification flag if needed */
184 static void jack_detect_update(struct hda_codec *codec,
185 struct hda_jack_tbl *jack)
187 if (!jack->jack_dirty)
190 if (jack->phantom_jack)
191 jack->pin_sense = AC_PINSENSE_PRESENCE;
193 jack->pin_sense = read_pin_sense(codec, jack->nid,
196 /* A gating jack indicates the jack is invalid if gating is unplugged */
197 if (jack->gating_jack &&
198 !snd_hda_jack_detect_mst(codec, jack->gating_jack, jack->dev_id))
199 jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
201 jack->jack_dirty = 0;
203 /* If a jack is gated by this one update it. */
204 if (jack->gated_jack) {
205 struct hda_jack_tbl *gated =
206 snd_hda_jack_tbl_get_mst(codec, jack->gated_jack,
209 gated->jack_dirty = 1;
210 jack_detect_update(codec, gated);
216 * snd_hda_jack_set_dirty_all - Mark all the cached as dirty
217 * @codec: the HDA codec
219 * This function sets the dirty flag to all entries of jack table.
220 * It's called from the resume path in hda_codec.c.
222 void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
224 struct hda_jack_tbl *jack = codec->jacktbl.list;
227 for (i = 0; i < codec->jacktbl.used; i++, jack++)
229 jack->jack_dirty = 1;
231 EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
234 * snd_hda_jack_pin_sense - execute pin sense measurement
235 * @codec: the CODEC to sense
236 * @nid: the pin NID to sense
237 * @dev_id: pin device entry id
239 * Execute necessary pin sense measurement and return its Presence Detect,
240 * Impedance, ELD Valid etc. status bits.
242 u32 snd_hda_jack_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id)
244 struct hda_jack_tbl *jack =
245 snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
247 jack_detect_update(codec, jack);
248 return jack->pin_sense;
250 return read_pin_sense(codec, nid, dev_id);
252 EXPORT_SYMBOL_GPL(snd_hda_jack_pin_sense);
255 * snd_hda_jack_detect_state_mst - query pin Presence Detect status
256 * @codec: the CODEC to sense
257 * @nid: the pin NID to sense
258 * @dev_id: pin device entry id
260 * Query and return the pin's Presence Detect status, as either
261 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
263 int snd_hda_jack_detect_state_mst(struct hda_codec *codec,
264 hda_nid_t nid, int dev_id)
266 struct hda_jack_tbl *jack =
267 snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
268 if (jack && jack->phantom_jack)
269 return HDA_JACK_PHANTOM;
270 else if (snd_hda_jack_pin_sense(codec, nid, dev_id) &
271 AC_PINSENSE_PRESENCE)
272 return HDA_JACK_PRESENT;
274 return HDA_JACK_NOT_PRESENT;
276 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state_mst);
278 static struct hda_jack_callback *
279 find_callback_from_list(struct hda_jack_tbl *jack,
280 hda_jack_callback_fn func)
282 struct hda_jack_callback *cb;
287 for (cb = jack->callback; cb; cb = cb->next) {
288 if (cb->func == func)
296 * snd_hda_jack_detect_enable_callback_mst - enable the jack-detection
297 * @codec: the HDA codec
298 * @nid: pin NID to enable
299 * @func: callback function to register
300 * @dev_id: pin device entry id
302 * In the case of error, the return value will be a pointer embedded with
303 * errno. Check and handle the return value appropriately with standard
304 * macros such as @IS_ERR() and @PTR_ERR().
306 struct hda_jack_callback *
307 snd_hda_jack_detect_enable_callback_mst(struct hda_codec *codec, hda_nid_t nid,
308 int dev_id, hda_jack_callback_fn func)
310 struct hda_jack_tbl *jack;
311 struct hda_jack_callback *callback = NULL;
314 jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
316 return ERR_PTR(-ENOMEM);
318 callback = find_callback_from_list(jack, func);
320 if (func && !callback) {
321 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
323 return ERR_PTR(-ENOMEM);
324 callback->func = func;
325 callback->nid = jack->nid;
326 callback->dev_id = jack->dev_id;
327 callback->next = jack->callback;
328 jack->callback = callback;
331 if (jack->jack_detect)
332 return callback; /* already registered */
333 jack->jack_detect = 1;
334 if (codec->jackpoll_interval > 0)
335 return callback; /* No unsol if we're polling instead */
336 err = snd_hda_codec_write_cache(codec, nid, 0,
337 AC_VERB_SET_UNSOLICITED_ENABLE,
338 AC_USRSP_EN | jack->tag);
343 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback_mst);
346 * snd_hda_jack_detect_enable - Enable the jack detection on the given pin
347 * @codec: the HDA codec
348 * @nid: pin NID to enable jack detection
349 * @dev_id: pin device entry id
351 * Enable the jack detection with the default callback. Returns zero if
352 * successful or a negative error code.
354 int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
357 return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback_mst(codec,
362 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
365 * snd_hda_jack_set_gating_jack - Set gating jack.
366 * @codec: the HDA codec
367 * @gated_nid: gated pin NID
368 * @gating_nid: gating pin NID
370 * Indicates the gated jack is only valid when the gating jack is plugged.
372 int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
373 hda_nid_t gating_nid)
375 struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid, 0);
376 struct hda_jack_tbl *gating =
377 snd_hda_jack_tbl_new(codec, gating_nid, 0);
379 WARN_ON(codec->dp_mst);
381 if (!gated || !gating)
384 gated->gating_jack = gating_nid;
385 gating->gated_jack = gated_nid;
389 EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
392 * snd_hda_jack_bind_keymap - bind keys generated from one NID to another jack.
393 * @codec: the HDA codec
394 * @key_nid: key event is generated by this pin NID
395 * @keymap: map of key type and key code
396 * @jack_nid: key reports to the jack of this pin NID
398 * This function is used in the case of key is generated from one NID while is
399 * reported to the jack of another NID.
401 int snd_hda_jack_bind_keymap(struct hda_codec *codec, hda_nid_t key_nid,
402 const struct hda_jack_keymap *keymap,
405 const struct hda_jack_keymap *map;
406 struct hda_jack_tbl *key_gen = snd_hda_jack_tbl_get(codec, key_nid);
407 struct hda_jack_tbl *report_to = snd_hda_jack_tbl_get(codec, jack_nid);
409 WARN_ON(codec->dp_mst);
411 if (!key_gen || !report_to || !report_to->jack)
414 key_gen->key_report_jack = jack_nid;
417 for (map = keymap; map->type; map++)
418 snd_jack_set_key(report_to->jack, map->type, map->key);
422 EXPORT_SYMBOL_GPL(snd_hda_jack_bind_keymap);
425 * snd_hda_jack_set_button_state - report button event to the hda_jack_tbl button_state.
426 * @codec: the HDA codec
427 * @jack_nid: the button event reports to the jack_tbl of this NID
428 * @button_state: the button event captured by codec
430 * Codec driver calls this function to report the button event.
432 void snd_hda_jack_set_button_state(struct hda_codec *codec, hda_nid_t jack_nid,
435 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, jack_nid);
440 if (jack->key_report_jack) {
441 struct hda_jack_tbl *report_to =
442 snd_hda_jack_tbl_get(codec, jack->key_report_jack);
445 report_to->button_state = button_state;
450 jack->button_state = button_state;
452 EXPORT_SYMBOL_GPL(snd_hda_jack_set_button_state);
455 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
456 * @codec: the HDA codec
458 void snd_hda_jack_report_sync(struct hda_codec *codec)
460 struct hda_jack_tbl *jack;
463 /* update all jacks at first */
464 jack = codec->jacktbl.list;
465 for (i = 0; i < codec->jacktbl.used; i++, jack++)
467 jack_detect_update(codec, jack);
469 /* report the updated jacks; it's done after updating all jacks
470 * to make sure that all gating jacks properly have been set
472 jack = codec->jacktbl.list;
473 for (i = 0; i < codec->jacktbl.used; i++, jack++)
475 if (!jack->jack || jack->block_report)
477 state = jack->button_state;
478 if (get_jack_plug_state(jack->pin_sense))
480 snd_jack_report(jack->jack, state);
481 if (jack->button_state) {
482 snd_jack_report(jack->jack,
483 state & ~jack->button_state);
484 jack->button_state = 0; /* button released */
488 EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
490 /* guess the jack type from the pin-config */
491 static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
493 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
494 switch (get_defcfg_device(def_conf)) {
495 case AC_JACK_LINE_OUT:
496 case AC_JACK_SPEAKER:
497 return SND_JACK_LINEOUT;
499 return SND_JACK_HEADPHONE;
500 case AC_JACK_SPDIF_OUT:
501 case AC_JACK_DIG_OTHER_OUT:
502 return SND_JACK_AVOUT;
504 return SND_JACK_MICROPHONE;
506 return SND_JACK_LINEIN;
510 static void hda_free_jack_priv(struct snd_jack *jack)
512 struct hda_jack_tbl *jacks = jack->private_data;
518 * snd_hda_jack_add_kctl_mst - Add a kctl for the given pin
519 * @codec: the HDA codec
520 * @nid: pin NID to assign
521 * @dev_id : pin device entry id
522 * @name: string name for the jack
523 * @phantom_jack: flag to deal as a phantom jack
524 * @type: jack type bits to be reported, 0 for guessing from pincfg
525 * @keymap: optional jack / key mapping
527 * This assigns a jack-detection kctl to the given pin. The kcontrol
528 * will have the given name and index.
530 int snd_hda_jack_add_kctl_mst(struct hda_codec *codec, hda_nid_t nid,
531 int dev_id, const char *name, bool phantom_jack,
532 int type, const struct hda_jack_keymap *keymap)
534 struct hda_jack_tbl *jack;
535 const struct hda_jack_keymap *map;
536 int err, state, buttons;
538 jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
542 return 0; /* already created */
545 type = get_input_jack_type(codec, nid);
549 for (map = keymap; map->type; map++)
550 buttons |= map->type;
553 err = snd_jack_new(codec->card, name, type | buttons,
554 &jack->jack, true, phantom_jack);
558 jack->phantom_jack = !!phantom_jack;
560 jack->button_state = 0;
561 jack->jack->private_data = jack;
562 jack->jack->private_free = hda_free_jack_priv;
564 for (map = keymap; map->type; map++)
565 snd_jack_set_key(jack->jack, map->type, map->key);
568 state = snd_hda_jack_detect_mst(codec, nid, dev_id);
569 snd_jack_report(jack->jack, state ? jack->type : 0);
573 EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl_mst);
575 static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
576 const struct auto_pin_cfg *cfg,
577 const char *base_name)
579 unsigned int def_conf, conn;
580 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
584 WARN_ON(codec->dp_mst);
588 def_conf = snd_hda_codec_get_pincfg(codec, nid);
589 conn = get_defcfg_connect(def_conf);
590 if (conn == AC_JACK_PORT_NONE)
592 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
593 !is_jack_detectable(codec, nid);
596 strscpy(name, base_name, sizeof(name));
598 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL);
600 /* Example final name: "Internal Mic Phantom Jack" */
601 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
602 err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack, 0, NULL);
607 return snd_hda_jack_detect_enable(codec, nid, 0);
612 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
613 * @codec: the HDA codec
614 * @cfg: pin config table to parse
616 int snd_hda_jack_add_kctls(struct hda_codec *codec,
617 const struct auto_pin_cfg *cfg)
622 for (i = 0; i < cfg->num_inputs; i++) {
623 /* If we have headphone mics; make sure they get the right name
624 before grabbed by output pins */
625 if (cfg->inputs[i].is_headphone_mic) {
626 if (auto_cfg_hp_outs(cfg) == 1)
627 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
628 cfg, "Headphone Mic");
630 err = add_jack_kctl(codec, cfg->inputs[i].pin,
631 cfg, "Headphone Mic");
633 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
639 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
640 err = add_jack_kctl(codec, *p, cfg, NULL);
644 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
645 if (*p == *cfg->line_out_pins) /* might be duplicated */
647 err = add_jack_kctl(codec, *p, cfg, NULL);
651 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
652 if (*p == *cfg->line_out_pins) /* might be duplicated */
654 err = add_jack_kctl(codec, *p, cfg, NULL);
658 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
659 err = add_jack_kctl(codec, *p, cfg, NULL);
663 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
666 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
671 EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
673 static void call_jack_callback(struct hda_codec *codec, unsigned int res,
674 struct hda_jack_tbl *jack)
676 struct hda_jack_callback *cb;
678 for (cb = jack->callback; cb; cb = cb->next) {
683 if (jack->gated_jack) {
684 struct hda_jack_tbl *gated =
685 snd_hda_jack_tbl_get_mst(codec, jack->gated_jack,
688 for (cb = gated->callback; cb; cb = cb->next) {
698 * snd_hda_jack_unsol_event - Handle an unsolicited event
699 * @codec: the HDA codec
700 * @res: the unsolicited event data
702 void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
704 struct hda_jack_tbl *event;
705 int tag = (res & AC_UNSOL_RES_TAG) >> AC_UNSOL_RES_TAG_SHIFT;
709 (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
711 event = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
713 event = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
718 if (event->key_report_jack) {
719 struct hda_jack_tbl *report_to =
720 snd_hda_jack_tbl_get_mst(codec, event->key_report_jack,
723 report_to->jack_dirty = 1;
725 event->jack_dirty = 1;
727 call_jack_callback(codec, res, event);
728 snd_hda_jack_report_sync(codec);
730 EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
733 * snd_hda_jack_poll_all - Poll all jacks
734 * @codec: the HDA codec
736 * Poll all detectable jacks with dirty flag, update the status, call
737 * callbacks and call snd_hda_jack_report_sync() if any changes are found.
739 void snd_hda_jack_poll_all(struct hda_codec *codec)
741 struct hda_jack_tbl *jack = codec->jacktbl.list;
744 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
745 unsigned int old_sense;
746 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
748 old_sense = get_jack_plug_state(jack->pin_sense);
749 jack_detect_update(codec, jack);
750 if (old_sense == get_jack_plug_state(jack->pin_sense))
753 call_jack_callback(codec, 0, jack);
756 snd_hda_jack_report_sync(codec);
758 EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);