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
) &&
33 !codec
->jackpoll_interval
)
37 EXPORT_SYMBOL_GPL(is_jack_detectable
);
39 /* execute pin sense measurement */
40 static u32
read_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
45 if (!codec
->no_trigger_sense
) {
46 pincap
= snd_hda_query_pin_caps(codec
, nid
);
47 if (pincap
& AC_PINCAP_TRIG_REQ
) /* need trigger? */
48 snd_hda_codec_read(codec
, nid
, 0,
49 AC_VERB_SET_PIN_SENSE
, 0);
51 val
= snd_hda_codec_read(codec
, nid
, 0,
52 AC_VERB_GET_PIN_SENSE
, 0);
53 if (codec
->inv_jack_detect
)
54 val
^= AC_PINSENSE_PRESENCE
;
59 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
62 snd_hda_jack_tbl_get(struct hda_codec
*codec
, hda_nid_t nid
)
64 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
69 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
74 EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get
);
77 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
80 snd_hda_jack_tbl_get_from_tag(struct hda_codec
*codec
, unsigned char tag
)
82 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
87 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
92 EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag
);
95 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
98 snd_hda_jack_tbl_new(struct hda_codec
*codec
, hda_nid_t nid
)
100 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_get(codec
, nid
);
103 jack
= snd_array_new(&codec
->jacktbl
);
107 jack
->jack_dirty
= 1;
108 jack
->tag
= codec
->jacktbl
.used
;
111 EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_new
);
113 void snd_hda_jack_tbl_clear(struct hda_codec
*codec
)
115 #ifdef CONFIG_SND_HDA_INPUT_JACK
116 /* free jack instances manually when clearing/reconfiguring */
117 if (!codec
->bus
->shutdown
&& codec
->jacktbl
.list
) {
118 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
120 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++) {
122 snd_device_free(codec
->bus
->card
, jack
->jack
);
126 snd_array_free(&codec
->jacktbl
);
129 #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
131 /* update the cached value and notification flag if needed */
132 static void jack_detect_update(struct hda_codec
*codec
,
133 struct hda_jack_tbl
*jack
)
135 if (!jack
->jack_dirty
)
138 if (jack
->phantom_jack
)
139 jack
->pin_sense
= AC_PINSENSE_PRESENCE
;
141 jack
->pin_sense
= read_pin_sense(codec
, jack
->nid
);
143 /* A gating jack indicates the jack is invalid if gating is unplugged */
144 if (jack
->gating_jack
&& !snd_hda_jack_detect(codec
, jack
->gating_jack
))
145 jack
->pin_sense
&= ~AC_PINSENSE_PRESENCE
;
147 jack
->jack_dirty
= 0;
149 /* If a jack is gated by this one update it. */
150 if (jack
->gated_jack
) {
151 struct hda_jack_tbl
*gated
=
152 snd_hda_jack_tbl_get(codec
, jack
->gated_jack
);
154 gated
->jack_dirty
= 1;
155 jack_detect_update(codec
, gated
);
161 * snd_hda_set_dirty_all - Mark all the cached as dirty
163 * This function sets the dirty flag to all entries of jack table.
164 * It's called from the resume path in hda_codec.c.
166 void snd_hda_jack_set_dirty_all(struct hda_codec
*codec
)
168 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
171 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
173 jack
->jack_dirty
= 1;
175 EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all
);
178 * snd_hda_pin_sense - execute pin sense measurement
179 * @codec: the CODEC to sense
180 * @nid: the pin NID to sense
182 * Execute necessary pin sense measurement and return its Presence Detect,
183 * Impedance, ELD Valid etc. status bits.
185 u32
snd_hda_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
187 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_get(codec
, nid
);
189 jack_detect_update(codec
, jack
);
190 return jack
->pin_sense
;
192 return read_pin_sense(codec
, nid
);
194 EXPORT_SYMBOL_GPL(snd_hda_pin_sense
);
197 * snd_hda_jack_detect_state - query pin Presence Detect status
198 * @codec: the CODEC to sense
199 * @nid: the pin NID to sense
201 * Query and return the pin's Presence Detect status, as either
202 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
204 int snd_hda_jack_detect_state(struct hda_codec
*codec
, hda_nid_t nid
)
206 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_get(codec
, nid
);
207 if (jack
&& jack
->phantom_jack
)
208 return HDA_JACK_PHANTOM
;
209 else if (snd_hda_pin_sense(codec
, nid
) & AC_PINSENSE_PRESENCE
)
210 return HDA_JACK_PRESENT
;
212 return HDA_JACK_NOT_PRESENT
;
214 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state
);
217 * snd_hda_jack_detect_enable - enable the jack-detection
219 int snd_hda_jack_detect_enable_callback(struct hda_codec
*codec
, hda_nid_t nid
,
220 unsigned char action
,
221 hda_jack_callback cb
)
223 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_new(codec
, nid
);
226 if (jack
->jack_detect
)
227 return 0; /* already registered */
228 jack
->jack_detect
= 1;
230 jack
->action
= action
;
233 if (codec
->jackpoll_interval
> 0)
234 return 0; /* No unsol if we're polling instead */
235 return snd_hda_codec_write_cache(codec
, nid
, 0,
236 AC_VERB_SET_UNSOLICITED_ENABLE
,
237 AC_USRSP_EN
| jack
->tag
);
239 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback
);
241 int snd_hda_jack_detect_enable(struct hda_codec
*codec
, hda_nid_t nid
,
242 unsigned char action
)
244 return snd_hda_jack_detect_enable_callback(codec
, nid
, action
, NULL
);
246 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable
);
249 * snd_hda_jack_set_gating_jack - Set gating jack.
251 * Indicates the gated jack is only valid when the gating jack is plugged.
253 int snd_hda_jack_set_gating_jack(struct hda_codec
*codec
, hda_nid_t gated_nid
,
254 hda_nid_t gating_nid
)
256 struct hda_jack_tbl
*gated
= snd_hda_jack_tbl_new(codec
, gated_nid
);
257 struct hda_jack_tbl
*gating
= snd_hda_jack_tbl_new(codec
, gating_nid
);
259 if (!gated
|| !gating
)
262 gated
->gating_jack
= gating_nid
;
263 gating
->gated_jack
= gated_nid
;
267 EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack
);
270 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
272 void snd_hda_jack_report_sync(struct hda_codec
*codec
)
274 struct hda_jack_tbl
*jack
;
277 /* update all jacks at first */
278 jack
= codec
->jacktbl
.list
;
279 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
281 jack_detect_update(codec
, jack
);
283 /* report the updated jacks; it's done after updating all jacks
284 * to make sure that all gating jacks properly have been set
286 jack
= codec
->jacktbl
.list
;
287 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
289 if (!jack
->kctl
|| jack
->block_report
)
291 state
= get_jack_plug_state(jack
->pin_sense
);
292 snd_kctl_jack_report(codec
->bus
->card
, jack
->kctl
, state
);
293 #ifdef CONFIG_SND_HDA_INPUT_JACK
295 snd_jack_report(jack
->jack
,
296 state
? jack
->type
: 0);
300 EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync
);
302 #ifdef CONFIG_SND_HDA_INPUT_JACK
303 /* guess the jack type from the pin-config */
304 static int get_input_jack_type(struct hda_codec
*codec
, hda_nid_t nid
)
306 unsigned int def_conf
= snd_hda_codec_get_pincfg(codec
, nid
);
307 switch (get_defcfg_device(def_conf
)) {
308 case AC_JACK_LINE_OUT
:
309 case AC_JACK_SPEAKER
:
310 return SND_JACK_LINEOUT
;
312 return SND_JACK_HEADPHONE
;
313 case AC_JACK_SPDIF_OUT
:
314 case AC_JACK_DIG_OTHER_OUT
:
315 return SND_JACK_AVOUT
;
317 return SND_JACK_MICROPHONE
;
319 return SND_JACK_LINEIN
;
323 static void hda_free_jack_priv(struct snd_jack
*jack
)
325 struct hda_jack_tbl
*jacks
= jack
->private_data
;
332 * snd_hda_jack_add_kctl - Add a kctl for the given pin
334 * This assigns a jack-detection kctl to the given pin. The kcontrol
335 * will have the given name and index.
337 static int __snd_hda_jack_add_kctl(struct hda_codec
*codec
, hda_nid_t nid
,
338 const char *name
, int idx
, bool phantom_jack
)
340 struct hda_jack_tbl
*jack
;
341 struct snd_kcontrol
*kctl
;
344 jack
= snd_hda_jack_tbl_new(codec
, nid
);
348 return 0; /* already created */
349 kctl
= snd_kctl_jack_new(name
, idx
, codec
);
352 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
356 jack
->phantom_jack
= !!phantom_jack
;
358 state
= snd_hda_jack_detect(codec
, nid
);
359 snd_kctl_jack_report(codec
->bus
->card
, kctl
, state
);
360 #ifdef CONFIG_SND_HDA_INPUT_JACK
362 jack
->type
= get_input_jack_type(codec
, nid
);
363 err
= snd_jack_new(codec
->bus
->card
, name
, jack
->type
,
367 jack
->jack
->private_data
= jack
;
368 jack
->jack
->private_free
= hda_free_jack_priv
;
369 snd_jack_report(jack
->jack
, state
? jack
->type
: 0);
375 int snd_hda_jack_add_kctl(struct hda_codec
*codec
, hda_nid_t nid
,
376 const char *name
, int idx
)
378 return __snd_hda_jack_add_kctl(codec
, nid
, name
, idx
, false);
380 EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl
);
382 /* get the unique index number for the given kctl name */
383 static int get_unique_index(struct hda_codec
*codec
, const char *name
, int idx
)
385 struct hda_jack_tbl
*jack
;
386 int i
, len
= strlen(name
);
388 jack
= codec
->jacktbl
.list
;
389 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++) {
390 /* jack->kctl.id contains "XXX Jack" name string with index */
392 !strncmp(name
, jack
->kctl
->id
.name
, len
) &&
393 !strcmp(" Jack", jack
->kctl
->id
.name
+ len
) &&
394 jack
->kctl
->id
.index
== idx
) {
402 static int add_jack_kctl(struct hda_codec
*codec
, hda_nid_t nid
,
403 const struct auto_pin_cfg
*cfg
,
404 const char *base_name
)
406 unsigned int def_conf
, conn
;
407 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
413 def_conf
= snd_hda_codec_get_pincfg(codec
, nid
);
414 conn
= get_defcfg_connect(def_conf
);
415 if (conn
== AC_JACK_PORT_NONE
)
417 phantom_jack
= (conn
!= AC_JACK_PORT_COMPLEX
) ||
418 !is_jack_detectable(codec
, nid
);
421 strlcpy(name
, base_name
, sizeof(name
));
424 snd_hda_get_pin_label(codec
, nid
, cfg
, name
, sizeof(name
), &idx
);
426 /* Example final name: "Internal Mic Phantom Jack" */
427 strncat(name
, " Phantom", sizeof(name
) - strlen(name
) - 1);
428 idx
= get_unique_index(codec
, name
, idx
);
429 err
= __snd_hda_jack_add_kctl(codec
, nid
, name
, idx
, phantom_jack
);
434 return snd_hda_jack_detect_enable(codec
, nid
, 0);
439 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
441 int snd_hda_jack_add_kctls(struct hda_codec
*codec
,
442 const struct auto_pin_cfg
*cfg
)
447 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
448 /* If we have headphone mics; make sure they get the right name
449 before grabbed by output pins */
450 if (cfg
->inputs
[i
].is_headphone_mic
) {
451 if (auto_cfg_hp_outs(cfg
) == 1)
452 err
= add_jack_kctl(codec
, auto_cfg_hp_pins(cfg
)[0],
453 cfg
, "Headphone Mic");
455 err
= add_jack_kctl(codec
, cfg
->inputs
[i
].pin
,
456 cfg
, "Headphone Mic");
458 err
= add_jack_kctl(codec
, cfg
->inputs
[i
].pin
, cfg
,
464 for (i
= 0, p
= cfg
->line_out_pins
; i
< cfg
->line_outs
; i
++, p
++) {
465 err
= add_jack_kctl(codec
, *p
, cfg
, NULL
);
469 for (i
= 0, p
= cfg
->hp_pins
; i
< cfg
->hp_outs
; i
++, p
++) {
470 if (*p
== *cfg
->line_out_pins
) /* might be duplicated */
472 err
= add_jack_kctl(codec
, *p
, cfg
, NULL
);
476 for (i
= 0, p
= cfg
->speaker_pins
; i
< cfg
->speaker_outs
; i
++, p
++) {
477 if (*p
== *cfg
->line_out_pins
) /* might be duplicated */
479 err
= add_jack_kctl(codec
, *p
, cfg
, NULL
);
483 for (i
= 0, p
= cfg
->dig_out_pins
; i
< cfg
->dig_outs
; i
++, p
++) {
484 err
= add_jack_kctl(codec
, *p
, cfg
, NULL
);
488 err
= add_jack_kctl(codec
, cfg
->dig_in_pin
, cfg
, NULL
);
491 err
= add_jack_kctl(codec
, cfg
->mono_out_pin
, cfg
, NULL
);
496 EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls
);
498 static void call_jack_callback(struct hda_codec
*codec
,
499 struct hda_jack_tbl
*jack
)
502 jack
->callback(codec
, jack
);
503 if (jack
->gated_jack
) {
504 struct hda_jack_tbl
*gated
=
505 snd_hda_jack_tbl_get(codec
, jack
->gated_jack
);
506 if (gated
&& gated
->callback
)
507 gated
->callback(codec
, gated
);
511 void snd_hda_jack_unsol_event(struct hda_codec
*codec
, unsigned int res
)
513 struct hda_jack_tbl
*event
;
514 int tag
= (res
>> AC_UNSOL_RES_TAG_SHIFT
) & 0x7f;
516 event
= snd_hda_jack_tbl_get_from_tag(codec
, tag
);
519 event
->jack_dirty
= 1;
521 call_jack_callback(codec
, event
);
522 snd_hda_jack_report_sync(codec
);
524 EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event
);
526 void snd_hda_jack_poll_all(struct hda_codec
*codec
)
528 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
531 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++) {
532 unsigned int old_sense
;
533 if (!jack
->nid
|| !jack
->jack_dirty
|| jack
->phantom_jack
)
535 old_sense
= get_jack_plug_state(jack
->pin_sense
);
536 jack_detect_update(codec
, jack
);
537 if (old_sense
== get_jack_plug_state(jack
->pin_sense
))
540 call_jack_callback(codec
, jack
);
543 snd_hda_jack_report_sync(codec
);
545 EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all
);