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 <linux/export.h>
15 #include <sound/core.h>
16 #include <sound/control.h>
17 #include <sound/jack.h>
18 #include "hda_codec.h"
19 #include "hda_local.h"
20 #include "hda_auto_parser.h"
23 bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
25 if (codec->no_jack_detect)
27 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
29 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
30 AC_DEFCFG_MISC_NO_PRESENCE)
32 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP))
36 EXPORT_SYMBOL_HDA(is_jack_detectable);
38 /* execute pin sense measurement */
39 static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
43 if (!codec->no_trigger_sense) {
44 pincap = snd_hda_query_pin_caps(codec, nid);
45 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
46 snd_hda_codec_read(codec, nid, 0,
47 AC_VERB_SET_PIN_SENSE, 0);
49 return snd_hda_codec_read(codec, nid, 0,
50 AC_VERB_GET_PIN_SENSE, 0);
54 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
57 snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
59 struct hda_jack_tbl *jack = codec->jacktbl.list;
64 for (i = 0; i < codec->jacktbl.used; i++, jack++)
69 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
72 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
75 snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
77 struct hda_jack_tbl *jack = codec->jacktbl.list;
82 for (i = 0; i < codec->jacktbl.used; i++, jack++)
87 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
90 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
93 snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
95 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
98 jack = snd_array_new(&codec->jacktbl);
102 jack->jack_dirty = 1;
103 jack->tag = codec->jacktbl.used;
106 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new);
108 void snd_hda_jack_tbl_clear(struct hda_codec *codec)
110 #ifdef CONFIG_SND_HDA_INPUT_JACK
111 /* free jack instances manually when clearing/reconfiguring */
112 if (!codec->bus->shutdown && codec->jacktbl.list) {
113 struct hda_jack_tbl *jack = codec->jacktbl.list;
115 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
117 snd_device_free(codec->bus->card, jack->jack);
121 snd_array_free(&codec->jacktbl);
124 #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
126 /* update the cached value and notification flag if needed */
127 static void jack_detect_update(struct hda_codec *codec,
128 struct hda_jack_tbl *jack)
130 if (!jack->jack_dirty)
133 if (jack->phantom_jack)
134 jack->pin_sense = AC_PINSENSE_PRESENCE;
136 jack->pin_sense = read_pin_sense(codec, jack->nid);
138 /* A gating jack indicates the jack is invalid if gating is unplugged */
139 if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack))
140 jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
142 jack->jack_dirty = 0;
144 /* If a jack is gated by this one update it. */
145 if (jack->gated_jack) {
146 struct hda_jack_tbl *gated =
147 snd_hda_jack_tbl_get(codec, jack->gated_jack);
149 gated->jack_dirty = 1;
150 jack_detect_update(codec, gated);
156 * snd_hda_set_dirty_all - Mark all the cached as dirty
158 * This function sets the dirty flag to all entries of jack table.
159 * It's called from the resume path in hda_codec.c.
161 void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
163 struct hda_jack_tbl *jack = codec->jacktbl.list;
166 for (i = 0; i < codec->jacktbl.used; i++, jack++)
168 jack->jack_dirty = 1;
170 EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
173 * snd_hda_pin_sense - execute pin sense measurement
174 * @codec: the CODEC to sense
175 * @nid: the pin NID to sense
177 * Execute necessary pin sense measurement and return its Presence Detect,
178 * Impedance, ELD Valid etc. status bits.
180 u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
182 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
184 jack_detect_update(codec, jack);
185 return jack->pin_sense;
187 return read_pin_sense(codec, nid);
189 EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
192 * snd_hda_jack_detect - query pin Presence Detect status
193 * @codec: the CODEC to sense
194 * @nid: the pin NID to sense
196 * Query and return the pin's Presence Detect status.
198 int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
200 u32 sense = snd_hda_pin_sense(codec, nid);
201 return get_jack_plug_state(sense);
203 EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
206 * snd_hda_jack_detect_enable - enable the jack-detection
208 int snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
209 unsigned char action,
210 hda_jack_callback cb)
212 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
215 if (jack->jack_detect)
216 return 0; /* already registered */
217 jack->jack_detect = 1;
219 jack->action = action;
222 if (codec->jackpoll_interval > 0)
223 return 0; /* No unsol if we're polling instead */
224 return snd_hda_codec_write_cache(codec, nid, 0,
225 AC_VERB_SET_UNSOLICITED_ENABLE,
226 AC_USRSP_EN | jack->tag);
228 EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable_callback);
230 int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
231 unsigned char action)
233 return snd_hda_jack_detect_enable_callback(codec, nid, action, NULL);
235 EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
238 * snd_hda_jack_set_gating_jack - Set gating jack.
240 * Indicates the gated jack is only valid when the gating jack is plugged.
242 int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
243 hda_nid_t gating_nid)
245 struct hda_jack_tbl *gated = snd_hda_jack_tbl_get(codec, gated_nid);
246 struct hda_jack_tbl *gating = snd_hda_jack_tbl_get(codec, gating_nid);
248 if (!gated || !gating)
251 gated->gating_jack = gating_nid;
252 gating->gated_jack = gated_nid;
256 EXPORT_SYMBOL_HDA(snd_hda_jack_set_gating_jack);
259 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
261 void snd_hda_jack_report_sync(struct hda_codec *codec)
263 struct hda_jack_tbl *jack;
266 /* update all jacks at first */
267 jack = codec->jacktbl.list;
268 for (i = 0; i < codec->jacktbl.used; i++, jack++)
270 jack_detect_update(codec, jack);
272 /* report the updated jacks; it's done after updating all jacks
273 * to make sure that all gating jacks properly have been set
275 jack = codec->jacktbl.list;
276 for (i = 0; i < codec->jacktbl.used; i++, jack++)
280 state = get_jack_plug_state(jack->pin_sense);
281 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
282 #ifdef CONFIG_SND_HDA_INPUT_JACK
284 snd_jack_report(jack->jack,
285 state ? jack->type : 0);
289 EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
291 #ifdef CONFIG_SND_HDA_INPUT_JACK
292 /* guess the jack type from the pin-config */
293 static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
295 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
296 switch (get_defcfg_device(def_conf)) {
297 case AC_JACK_LINE_OUT:
298 case AC_JACK_SPEAKER:
299 return SND_JACK_LINEOUT;
301 return SND_JACK_HEADPHONE;
302 case AC_JACK_SPDIF_OUT:
303 case AC_JACK_DIG_OTHER_OUT:
304 return SND_JACK_AVOUT;
306 return SND_JACK_MICROPHONE;
308 return SND_JACK_LINEIN;
312 static void hda_free_jack_priv(struct snd_jack *jack)
314 struct hda_jack_tbl *jacks = jack->private_data;
321 * snd_hda_jack_add_kctl - Add a kctl for the given pin
323 * This assigns a jack-detection kctl to the given pin. The kcontrol
324 * will have the given name and index.
326 static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
327 const char *name, int idx, bool phantom_jack)
329 struct hda_jack_tbl *jack;
330 struct snd_kcontrol *kctl;
333 jack = snd_hda_jack_tbl_new(codec, nid);
337 return 0; /* already created */
338 kctl = snd_kctl_jack_new(name, idx, codec);
341 err = snd_hda_ctl_add(codec, nid, kctl);
345 jack->phantom_jack = !!phantom_jack;
347 state = snd_hda_jack_detect(codec, nid);
348 snd_kctl_jack_report(codec->bus->card, kctl, state);
349 #ifdef CONFIG_SND_HDA_INPUT_JACK
351 jack->type = get_input_jack_type(codec, nid);
352 err = snd_jack_new(codec->bus->card, name, jack->type,
356 jack->jack->private_data = jack;
357 jack->jack->private_free = hda_free_jack_priv;
358 snd_jack_report(jack->jack, state ? jack->type : 0);
364 int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
365 const char *name, int idx)
367 return __snd_hda_jack_add_kctl(codec, nid, name, idx, false);
369 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
371 /* get the unique index number for the given kctl name */
372 static int get_unique_index(struct hda_codec *codec, const char *name, int idx)
374 struct hda_jack_tbl *jack;
375 int i, len = strlen(name);
377 jack = codec->jacktbl.list;
378 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
379 /* jack->kctl.id contains "XXX Jack" name string with index */
381 !strncmp(name, jack->kctl->id.name, len) &&
382 !strcmp(" Jack", jack->kctl->id.name + len) &&
383 jack->kctl->id.index == idx) {
391 static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
392 const struct auto_pin_cfg *cfg)
394 unsigned int def_conf, conn;
401 def_conf = snd_hda_codec_get_pincfg(codec, nid);
402 conn = get_defcfg_connect(def_conf);
403 if (conn == AC_JACK_PORT_NONE)
405 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
406 !is_jack_detectable(codec, nid);
408 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
410 /* Example final name: "Internal Mic Phantom Jack" */
411 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
412 idx = get_unique_index(codec, name, idx);
413 err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack);
418 return snd_hda_jack_detect_enable(codec, nid, 0);
423 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
425 int snd_hda_jack_add_kctls(struct hda_codec *codec,
426 const struct auto_pin_cfg *cfg)
431 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
432 err = add_jack_kctl(codec, *p, cfg);
436 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
437 if (*p == *cfg->line_out_pins) /* might be duplicated */
439 err = add_jack_kctl(codec, *p, cfg);
443 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
444 if (*p == *cfg->line_out_pins) /* might be duplicated */
446 err = add_jack_kctl(codec, *p, cfg);
450 for (i = 0; i < cfg->num_inputs; i++) {
451 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg);
455 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
456 err = add_jack_kctl(codec, *p, cfg);
460 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg);
463 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg);
468 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);
470 static void call_jack_callback(struct hda_codec *codec,
471 struct hda_jack_tbl *jack)
474 jack->callback(codec, jack);
475 if (jack->gated_jack) {
476 struct hda_jack_tbl *gated =
477 snd_hda_jack_tbl_get(codec, jack->gated_jack);
478 if (gated && gated->callback)
479 gated->callback(codec, gated);
483 void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
485 struct hda_jack_tbl *event;
486 int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f;
488 event = snd_hda_jack_tbl_get_from_tag(codec, tag);
491 event->jack_dirty = 1;
493 call_jack_callback(codec, event);
494 snd_hda_jack_report_sync(codec);
496 EXPORT_SYMBOL_HDA(snd_hda_jack_unsol_event);
498 void snd_hda_jack_poll_all(struct hda_codec *codec)
500 struct hda_jack_tbl *jack = codec->jacktbl.list;
503 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
504 unsigned int old_sense;
505 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
507 old_sense = get_jack_plug_state(jack->pin_sense);
508 jack_detect_update(codec, jack);
509 if (old_sense == get_jack_plug_state(jack->pin_sense))
512 call_jack_callback(codec, jack);
515 snd_hda_jack_report_sync(codec);
517 EXPORT_SYMBOL_HDA(snd_hda_jack_poll_all);