Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Jack-detection handling for HD-audio
4 *
5 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
6 */
7
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"
17#include "hda_jack.h"
18
19/**
20 * is_jack_detectable - Check whether the given pin is jack-detectable
21 * @codec: the HDA codec
22 * @nid: pin NID
23 *
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.
28 */
29bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
30{
31 if (codec->no_jack_detect)
32 return false;
33 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
34 return false;
35 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
36 AC_DEFCFG_MISC_NO_PRESENCE)
37 return false;
38 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) &&
39 !codec->jackpoll_interval)
40 return false;
41 return true;
42}
43EXPORT_SYMBOL_GPL(is_jack_detectable);
44
45/* execute pin sense measurement */
46static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id)
47{
48 u32 pincap;
49 u32 val;
50
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);
56 }
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;
61 return val;
62}
63
64/**
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
69 */
70struct hda_jack_tbl *
71snd_hda_jack_tbl_get_mst(struct hda_codec *codec, hda_nid_t nid, int dev_id)
72{
73 struct hda_jack_tbl *jack = codec->jacktbl.list;
74 int i;
75
76 if (!nid || !jack)
77 return NULL;
78 for (i = 0; i < codec->jacktbl.used; i++, jack++)
79 if (jack->nid == nid && jack->dev_id == dev_id)
80 return jack;
81 return NULL;
82}
83EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_mst);
84
85/**
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
90 */
91struct hda_jack_tbl *
92snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec,
93 unsigned char tag, int dev_id)
94{
95 struct hda_jack_tbl *jack = codec->jacktbl.list;
96 int i;
97
98 if (!tag || !jack)
99 return NULL;
100 for (i = 0; i < codec->jacktbl.used; i++, jack++)
101 if (jack->tag == tag && jack->dev_id == dev_id)
102 return jack;
103 return NULL;
104}
105EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
106
107static struct hda_jack_tbl *
108any_jack_tbl_get_from_nid(struct hda_codec *codec, hda_nid_t nid)
109{
110 struct hda_jack_tbl *jack = codec->jacktbl.list;
111 int i;
112
113 if (!nid || !jack)
114 return NULL;
115 for (i = 0; i < codec->jacktbl.used; i++, jack++)
116 if (jack->nid == nid)
117 return jack;
118 return NULL;
119}
120
121/**
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
126 */
127static struct hda_jack_tbl *
128snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid, int dev_id)
129{
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);
134
135 WARN_ON(dev_id != 0 && !codec->dp_mst);
136
137 if (jack)
138 return jack;
139 jack = snd_array_new(&codec->jacktbl);
140 if (!jack)
141 return NULL;
142 jack->nid = nid;
143 jack->dev_id = dev_id;
144 jack->jack_dirty = 1;
145 if (existing_nid_jack) {
146 jack->tag = existing_nid_jack->tag;
147
148 /*
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.
152 */
153 jack->jack_detect = existing_nid_jack->jack_detect;
154 } else {
155 jack->tag = codec->jacktbl.used;
156 }
157
158 return jack;
159}
160
161void snd_hda_jack_tbl_clear(struct hda_codec *codec)
162{
163 struct hda_jack_tbl *jack = codec->jacktbl.list;
164 int i;
165
166 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
167 struct hda_jack_callback *cb, *next;
168
169 /* free jack instances manually when clearing/reconfiguring */
170 if (!codec->bus->shutdown && jack->jack)
171 snd_device_free(codec->card, jack->jack);
172
173 for (cb = jack->callback; cb; cb = next) {
174 next = cb->next;
175 kfree(cb);
176 }
177 }
178 snd_array_free(&codec->jacktbl);
179}
180
181#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
182
183/* update the cached value and notification flag if needed */
184static void jack_detect_update(struct hda_codec *codec,
185 struct hda_jack_tbl *jack)
186{
187 if (!jack->jack_dirty)
188 return;
189
190 if (jack->phantom_jack)
191 jack->pin_sense = AC_PINSENSE_PRESENCE;
192 else
193 jack->pin_sense = read_pin_sense(codec, jack->nid,
194 jack->dev_id);
195
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;
200
201 jack->jack_dirty = 0;
202
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,
207 jack->dev_id);
208 if (gated) {
209 gated->jack_dirty = 1;
210 jack_detect_update(codec, gated);
211 }
212 }
213}
214
215/**
216 * snd_hda_set_dirty_all - Mark all the cached as dirty
217 * @codec: the HDA codec
218 *
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.
221 */
222void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
223{
224 struct hda_jack_tbl *jack = codec->jacktbl.list;
225 int i;
226
227 for (i = 0; i < codec->jacktbl.used; i++, jack++)
228 if (jack->nid)
229 jack->jack_dirty = 1;
230}
231EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
232
233/**
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
238 *
239 * Execute necessary pin sense measurement and return its Presence Detect,
240 * Impedance, ELD Valid etc. status bits.
241 */
242u32 snd_hda_jack_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id)
243{
244 struct hda_jack_tbl *jack =
245 snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
246 if (jack) {
247 jack_detect_update(codec, jack);
248 return jack->pin_sense;
249 }
250 return read_pin_sense(codec, nid, dev_id);
251}
252EXPORT_SYMBOL_GPL(snd_hda_jack_pin_sense);
253
254/**
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
259 *
260 * Query and return the pin's Presence Detect status, as either
261 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
262 */
263int snd_hda_jack_detect_state_mst(struct hda_codec *codec,
264 hda_nid_t nid, int dev_id)
265{
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;
273 else
274 return HDA_JACK_NOT_PRESENT;
275}
276EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state_mst);
277
278/**
279 * snd_hda_jack_detect_enable_mst - enable the jack-detection
280 * @codec: the HDA codec
281 * @nid: pin NID to enable
282 * @func: callback function to register
283 * @dev_id: pin device entry id
284 *
285 * In the case of error, the return value will be a pointer embedded with
286 * errno. Check and handle the return value appropriately with standard
287 * macros such as @IS_ERR() and @PTR_ERR().
288 */
289struct hda_jack_callback *
290snd_hda_jack_detect_enable_callback_mst(struct hda_codec *codec, hda_nid_t nid,
291 int dev_id, hda_jack_callback_fn func)
292{
293 struct hda_jack_tbl *jack;
294 struct hda_jack_callback *callback = NULL;
295 int err;
296
297 jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
298 if (!jack)
299 return ERR_PTR(-ENOMEM);
300 if (func) {
301 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
302 if (!callback)
303 return ERR_PTR(-ENOMEM);
304 callback->func = func;
305 callback->nid = jack->nid;
306 callback->dev_id = jack->dev_id;
307 callback->next = jack->callback;
308 jack->callback = callback;
309 }
310
311 if (jack->jack_detect)
312 return callback; /* already registered */
313 jack->jack_detect = 1;
314 if (codec->jackpoll_interval > 0)
315 return callback; /* No unsol if we're polling instead */
316 err = snd_hda_codec_write_cache(codec, nid, 0,
317 AC_VERB_SET_UNSOLICITED_ENABLE,
318 AC_USRSP_EN | jack->tag);
319 if (err < 0)
320 return ERR_PTR(err);
321 return callback;
322}
323EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback_mst);
324
325/**
326 * snd_hda_jack_detect_enable - Enable the jack detection on the given pin
327 * @codec: the HDA codec
328 * @nid: pin NID to enable jack detection
329 * @dev_id: pin device entry id
330 *
331 * Enable the jack detection with the default callback. Returns zero if
332 * successful or a negative error code.
333 */
334int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
335 int dev_id)
336{
337 return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback_mst(codec,
338 nid,
339 dev_id,
340 NULL));
341}
342EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
343
344/**
345 * snd_hda_jack_set_gating_jack - Set gating jack.
346 * @codec: the HDA codec
347 * @gated_nid: gated pin NID
348 * @gating_nid: gating pin NID
349 *
350 * Indicates the gated jack is only valid when the gating jack is plugged.
351 */
352int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
353 hda_nid_t gating_nid)
354{
355 struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid, 0);
356 struct hda_jack_tbl *gating =
357 snd_hda_jack_tbl_new(codec, gating_nid, 0);
358
359 WARN_ON(codec->dp_mst);
360
361 if (!gated || !gating)
362 return -EINVAL;
363
364 gated->gating_jack = gating_nid;
365 gating->gated_jack = gated_nid;
366
367 return 0;
368}
369EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
370
371/**
372 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
373 * @codec: the HDA codec
374 */
375void snd_hda_jack_report_sync(struct hda_codec *codec)
376{
377 struct hda_jack_tbl *jack;
378 int i, state;
379
380 /* update all jacks at first */
381 jack = codec->jacktbl.list;
382 for (i = 0; i < codec->jacktbl.used; i++, jack++)
383 if (jack->nid)
384 jack_detect_update(codec, jack);
385
386 /* report the updated jacks; it's done after updating all jacks
387 * to make sure that all gating jacks properly have been set
388 */
389 jack = codec->jacktbl.list;
390 for (i = 0; i < codec->jacktbl.used; i++, jack++)
391 if (jack->nid) {
392 if (!jack->jack || jack->block_report)
393 continue;
394 state = jack->button_state;
395 if (get_jack_plug_state(jack->pin_sense))
396 state |= jack->type;
397 snd_jack_report(jack->jack, state);
398 if (jack->button_state) {
399 snd_jack_report(jack->jack,
400 state & ~jack->button_state);
401 jack->button_state = 0; /* button released */
402 }
403 }
404}
405EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
406
407/* guess the jack type from the pin-config */
408static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
409{
410 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
411 switch (get_defcfg_device(def_conf)) {
412 case AC_JACK_LINE_OUT:
413 case AC_JACK_SPEAKER:
414 return SND_JACK_LINEOUT;
415 case AC_JACK_HP_OUT:
416 return SND_JACK_HEADPHONE;
417 case AC_JACK_SPDIF_OUT:
418 case AC_JACK_DIG_OTHER_OUT:
419 return SND_JACK_AVOUT;
420 case AC_JACK_MIC_IN:
421 return SND_JACK_MICROPHONE;
422 default:
423 return SND_JACK_LINEIN;
424 }
425}
426
427static void hda_free_jack_priv(struct snd_jack *jack)
428{
429 struct hda_jack_tbl *jacks = jack->private_data;
430 jacks->nid = 0;
431 jacks->jack = NULL;
432}
433
434/**
435 * snd_hda_jack_add_kctl_mst - Add a kctl for the given pin
436 * @codec: the HDA codec
437 * @nid: pin NID to assign
438 * @dev_id : pin device entry id
439 * @name: string name for the jack
440 * @phantom_jack: flag to deal as a phantom jack
441 * @type: jack type bits to be reported, 0 for guessing from pincfg
442 * @keymap: optional jack / key mapping
443 *
444 * This assigns a jack-detection kctl to the given pin. The kcontrol
445 * will have the given name and index.
446 */
447int snd_hda_jack_add_kctl_mst(struct hda_codec *codec, hda_nid_t nid,
448 int dev_id, const char *name, bool phantom_jack,
449 int type, const struct hda_jack_keymap *keymap)
450{
451 struct hda_jack_tbl *jack;
452 const struct hda_jack_keymap *map;
453 int err, state, buttons;
454
455 jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
456 if (!jack)
457 return 0;
458 if (jack->jack)
459 return 0; /* already created */
460
461 if (!type)
462 type = get_input_jack_type(codec, nid);
463
464 buttons = 0;
465 if (keymap) {
466 for (map = keymap; map->type; map++)
467 buttons |= map->type;
468 }
469
470 err = snd_jack_new(codec->card, name, type | buttons,
471 &jack->jack, true, phantom_jack);
472 if (err < 0)
473 return err;
474
475 jack->phantom_jack = !!phantom_jack;
476 jack->type = type;
477 jack->button_state = 0;
478 jack->jack->private_data = jack;
479 jack->jack->private_free = hda_free_jack_priv;
480 if (keymap) {
481 for (map = keymap; map->type; map++)
482 snd_jack_set_key(jack->jack, map->type, map->key);
483 }
484
485 state = snd_hda_jack_detect_mst(codec, nid, dev_id);
486 snd_jack_report(jack->jack, state ? jack->type : 0);
487
488 return 0;
489}
490EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl_mst);
491
492static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
493 const struct auto_pin_cfg *cfg,
494 const char *base_name)
495{
496 unsigned int def_conf, conn;
497 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
498 int err;
499 bool phantom_jack;
500
501 WARN_ON(codec->dp_mst);
502
503 if (!nid)
504 return 0;
505 def_conf = snd_hda_codec_get_pincfg(codec, nid);
506 conn = get_defcfg_connect(def_conf);
507 if (conn == AC_JACK_PORT_NONE)
508 return 0;
509 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
510 !is_jack_detectable(codec, nid);
511
512 if (base_name)
513 strlcpy(name, base_name, sizeof(name));
514 else
515 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL);
516 if (phantom_jack)
517 /* Example final name: "Internal Mic Phantom Jack" */
518 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
519 err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack, 0, NULL);
520 if (err < 0)
521 return err;
522
523 if (!phantom_jack)
524 return snd_hda_jack_detect_enable(codec, nid, 0);
525 return 0;
526}
527
528/**
529 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
530 * @codec: the HDA codec
531 * @cfg: pin config table to parse
532 */
533int snd_hda_jack_add_kctls(struct hda_codec *codec,
534 const struct auto_pin_cfg *cfg)
535{
536 const hda_nid_t *p;
537 int i, err;
538
539 for (i = 0; i < cfg->num_inputs; i++) {
540 /* If we have headphone mics; make sure they get the right name
541 before grabbed by output pins */
542 if (cfg->inputs[i].is_headphone_mic) {
543 if (auto_cfg_hp_outs(cfg) == 1)
544 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
545 cfg, "Headphone Mic");
546 else
547 err = add_jack_kctl(codec, cfg->inputs[i].pin,
548 cfg, "Headphone Mic");
549 } else
550 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
551 NULL);
552 if (err < 0)
553 return err;
554 }
555
556 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
557 err = add_jack_kctl(codec, *p, cfg, NULL);
558 if (err < 0)
559 return err;
560 }
561 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
562 if (*p == *cfg->line_out_pins) /* might be duplicated */
563 break;
564 err = add_jack_kctl(codec, *p, cfg, NULL);
565 if (err < 0)
566 return err;
567 }
568 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
569 if (*p == *cfg->line_out_pins) /* might be duplicated */
570 break;
571 err = add_jack_kctl(codec, *p, cfg, NULL);
572 if (err < 0)
573 return err;
574 }
575 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
576 err = add_jack_kctl(codec, *p, cfg, NULL);
577 if (err < 0)
578 return err;
579 }
580 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
581 if (err < 0)
582 return err;
583 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
584 if (err < 0)
585 return err;
586 return 0;
587}
588EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
589
590static void call_jack_callback(struct hda_codec *codec, unsigned int res,
591 struct hda_jack_tbl *jack)
592{
593 struct hda_jack_callback *cb;
594
595 for (cb = jack->callback; cb; cb = cb->next) {
596 cb->jack = jack;
597 cb->unsol_res = res;
598 cb->func(codec, cb);
599 }
600 if (jack->gated_jack) {
601 struct hda_jack_tbl *gated =
602 snd_hda_jack_tbl_get_mst(codec, jack->gated_jack,
603 jack->dev_id);
604 if (gated) {
605 for (cb = gated->callback; cb; cb = cb->next) {
606 cb->jack = gated;
607 cb->unsol_res = res;
608 cb->func(codec, cb);
609 }
610 }
611 }
612}
613
614/**
615 * snd_hda_jack_unsol_event - Handle an unsolicited event
616 * @codec: the HDA codec
617 * @res: the unsolicited event data
618 */
619void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
620{
621 struct hda_jack_tbl *event;
622 int tag = (res & AC_UNSOL_RES_TAG) >> AC_UNSOL_RES_TAG_SHIFT;
623
624 if (codec->dp_mst) {
625 int dev_entry =
626 (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
627
628 event = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
629 } else {
630 event = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
631 }
632 if (!event)
633 return;
634 event->jack_dirty = 1;
635
636 call_jack_callback(codec, res, event);
637 snd_hda_jack_report_sync(codec);
638}
639EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
640
641/**
642 * snd_hda_jack_poll_all - Poll all jacks
643 * @codec: the HDA codec
644 *
645 * Poll all detectable jacks with dirty flag, update the status, call
646 * callbacks and call snd_hda_jack_report_sync() if any changes are found.
647 */
648void snd_hda_jack_poll_all(struct hda_codec *codec)
649{
650 struct hda_jack_tbl *jack = codec->jacktbl.list;
651 int i, changes = 0;
652
653 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
654 unsigned int old_sense;
655 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
656 continue;
657 old_sense = get_jack_plug_state(jack->pin_sense);
658 jack_detect_update(codec, jack);
659 if (old_sense == get_jack_plug_state(jack->pin_sense))
660 continue;
661 changes = 1;
662 call_jack_callback(codec, 0, jack);
663 }
664 if (changes)
665 snd_hda_jack_report_sync(codec);
666}
667EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);
668
1/*
2 * Jack-detection handling for HD-audio
3 *
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
5 *
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.
10 */
11
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"
21#include "hda_jack.h"
22
23bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
24{
25 if (codec->no_jack_detect)
26 return false;
27 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
28 return false;
29 if (!codec->ignore_misc_bit &&
30 (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
31 AC_DEFCFG_MISC_NO_PRESENCE))
32 return false;
33 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP))
34 return false;
35 return true;
36}
37EXPORT_SYMBOL_HDA(is_jack_detectable);
38
39/* execute pin sense measurement */
40static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
41{
42 u32 pincap;
43
44 if (!codec->no_trigger_sense) {
45 pincap = snd_hda_query_pin_caps(codec, nid);
46 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
47 snd_hda_codec_read(codec, nid, 0,
48 AC_VERB_SET_PIN_SENSE, 0);
49 }
50 return snd_hda_codec_read(codec, nid, 0,
51 AC_VERB_GET_PIN_SENSE, 0);
52}
53
54/**
55 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
56 */
57struct hda_jack_tbl *
58snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
59{
60 struct hda_jack_tbl *jack = codec->jacktbl.list;
61 int i;
62
63 if (!nid || !jack)
64 return NULL;
65 for (i = 0; i < codec->jacktbl.used; i++, jack++)
66 if (jack->nid == nid)
67 return jack;
68 return NULL;
69}
70EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
71
72/**
73 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
74 */
75struct hda_jack_tbl *
76snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
77{
78 struct hda_jack_tbl *jack = codec->jacktbl.list;
79 int i;
80
81 if (!tag || !jack)
82 return NULL;
83 for (i = 0; i < codec->jacktbl.used; i++, jack++)
84 if (jack->tag == tag)
85 return jack;
86 return NULL;
87}
88EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
89
90/**
91 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
92 */
93struct hda_jack_tbl *
94snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
95{
96 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
97 if (jack)
98 return jack;
99 snd_array_init(&codec->jacktbl, sizeof(*jack), 16);
100 jack = snd_array_new(&codec->jacktbl);
101 if (!jack)
102 return NULL;
103 jack->nid = nid;
104 jack->jack_dirty = 1;
105 jack->tag = codec->jacktbl.used;
106 return jack;
107}
108EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new);
109
110void snd_hda_jack_tbl_clear(struct hda_codec *codec)
111{
112#ifdef CONFIG_SND_HDA_INPUT_JACK
113 /* free jack instances manually when clearing/reconfiguring */
114 if (!codec->bus->shutdown && codec->jacktbl.list) {
115 struct hda_jack_tbl *jack = codec->jacktbl.list;
116 int i;
117 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
118 if (jack->jack)
119 snd_device_free(codec->bus->card, jack->jack);
120 }
121 }
122#endif
123 snd_array_free(&codec->jacktbl);
124}
125
126/* update the cached value and notification flag if needed */
127static void jack_detect_update(struct hda_codec *codec,
128 struct hda_jack_tbl *jack)
129{
130 if (jack->jack_dirty || !jack->jack_detect) {
131 jack->pin_sense = read_pin_sense(codec, jack->nid);
132 jack->jack_dirty = 0;
133 }
134}
135
136/**
137 * snd_hda_set_dirty_all - Mark all the cached as dirty
138 *
139 * This function sets the dirty flag to all entries of jack table.
140 * It's called from the resume path in hda_codec.c.
141 */
142void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
143{
144 struct hda_jack_tbl *jack = codec->jacktbl.list;
145 int i;
146
147 for (i = 0; i < codec->jacktbl.used; i++, jack++)
148 if (jack->nid)
149 jack->jack_dirty = 1;
150}
151EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
152
153/**
154 * snd_hda_pin_sense - execute pin sense measurement
155 * @codec: the CODEC to sense
156 * @nid: the pin NID to sense
157 *
158 * Execute necessary pin sense measurement and return its Presence Detect,
159 * Impedance, ELD Valid etc. status bits.
160 */
161u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
162{
163 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
164 if (jack) {
165 jack_detect_update(codec, jack);
166 return jack->pin_sense;
167 }
168 return read_pin_sense(codec, nid);
169}
170EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
171
172#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
173
174/**
175 * snd_hda_jack_detect - query pin Presence Detect status
176 * @codec: the CODEC to sense
177 * @nid: the pin NID to sense
178 *
179 * Query and return the pin's Presence Detect status.
180 */
181int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
182{
183 u32 sense = snd_hda_pin_sense(codec, nid);
184 return get_jack_plug_state(sense);
185}
186EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
187
188/**
189 * snd_hda_jack_detect_enable - enable the jack-detection
190 */
191int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
192 unsigned char action)
193{
194 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
195 if (!jack)
196 return -ENOMEM;
197 if (jack->jack_detect)
198 return 0; /* already registered */
199 jack->jack_detect = 1;
200 if (action)
201 jack->action = action;
202 return snd_hda_codec_write_cache(codec, nid, 0,
203 AC_VERB_SET_UNSOLICITED_ENABLE,
204 AC_USRSP_EN | jack->tag);
205}
206EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
207
208/**
209 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
210 */
211void snd_hda_jack_report_sync(struct hda_codec *codec)
212{
213 struct hda_jack_tbl *jack = codec->jacktbl.list;
214 int i, state;
215
216 for (i = 0; i < codec->jacktbl.used; i++, jack++)
217 if (jack->nid) {
218 jack_detect_update(codec, jack);
219 if (!jack->kctl)
220 continue;
221 state = get_jack_plug_state(jack->pin_sense);
222 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
223#ifdef CONFIG_SND_HDA_INPUT_JACK
224 if (jack->jack)
225 snd_jack_report(jack->jack,
226 state ? jack->type : 0);
227#endif
228 }
229}
230EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
231
232#ifdef CONFIG_SND_HDA_INPUT_JACK
233/* guess the jack type from the pin-config */
234static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
235{
236 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
237 switch (get_defcfg_device(def_conf)) {
238 case AC_JACK_LINE_OUT:
239 case AC_JACK_SPEAKER:
240 return SND_JACK_LINEOUT;
241 case AC_JACK_HP_OUT:
242 return SND_JACK_HEADPHONE;
243 case AC_JACK_SPDIF_OUT:
244 case AC_JACK_DIG_OTHER_OUT:
245 return SND_JACK_AVOUT;
246 case AC_JACK_MIC_IN:
247 return SND_JACK_MICROPHONE;
248 default:
249 return SND_JACK_LINEIN;
250 }
251}
252
253static void hda_free_jack_priv(struct snd_jack *jack)
254{
255 struct hda_jack_tbl *jacks = jack->private_data;
256 jacks->nid = 0;
257 jacks->jack = NULL;
258}
259#endif
260
261/**
262 * snd_hda_jack_add_kctl - Add a kctl for the given pin
263 *
264 * This assigns a jack-detection kctl to the given pin. The kcontrol
265 * will have the given name and index.
266 */
267int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
268 const char *name, int idx)
269{
270 struct hda_jack_tbl *jack;
271 struct snd_kcontrol *kctl;
272 int err, state;
273
274 jack = snd_hda_jack_tbl_new(codec, nid);
275 if (!jack)
276 return 0;
277 if (jack->kctl)
278 return 0; /* already created */
279 kctl = snd_kctl_jack_new(name, idx, codec);
280 if (!kctl)
281 return -ENOMEM;
282 err = snd_hda_ctl_add(codec, nid, kctl);
283 if (err < 0)
284 return err;
285 jack->kctl = kctl;
286 state = snd_hda_jack_detect(codec, nid);
287 snd_kctl_jack_report(codec->bus->card, kctl, state);
288#ifdef CONFIG_SND_HDA_INPUT_JACK
289 jack->type = get_input_jack_type(codec, nid);
290 err = snd_jack_new(codec->bus->card, name, jack->type, &jack->jack);
291 if (err < 0)
292 return err;
293 jack->jack->private_data = jack;
294 jack->jack->private_free = hda_free_jack_priv;
295 snd_jack_report(jack->jack, state ? jack->type : 0);
296#endif
297 return 0;
298}
299EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
300
301static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
302 const struct auto_pin_cfg *cfg,
303 char *lastname, int *lastidx)
304{
305 unsigned int def_conf, conn;
306 char name[44];
307 int idx, err;
308
309 if (!nid)
310 return 0;
311 if (!is_jack_detectable(codec, nid))
312 return 0;
313 def_conf = snd_hda_codec_get_pincfg(codec, nid);
314 conn = get_defcfg_connect(def_conf);
315 if (conn != AC_JACK_PORT_COMPLEX)
316 return 0;
317
318 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
319 if (!strcmp(name, lastname) && idx == *lastidx)
320 idx++;
321 strncpy(lastname, name, 44);
322 *lastidx = idx;
323 err = snd_hda_jack_add_kctl(codec, nid, name, idx);
324 if (err < 0)
325 return err;
326 return snd_hda_jack_detect_enable(codec, nid, 0);
327}
328
329/**
330 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
331 */
332int snd_hda_jack_add_kctls(struct hda_codec *codec,
333 const struct auto_pin_cfg *cfg)
334{
335 const hda_nid_t *p;
336 int i, err, lastidx = 0;
337 char lastname[44] = "";
338
339 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
340 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
341 if (err < 0)
342 return err;
343 }
344 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
345 if (*p == *cfg->line_out_pins) /* might be duplicated */
346 break;
347 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
348 if (err < 0)
349 return err;
350 }
351 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
352 if (*p == *cfg->line_out_pins) /* might be duplicated */
353 break;
354 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
355 if (err < 0)
356 return err;
357 }
358 for (i = 0; i < cfg->num_inputs; i++) {
359 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, lastname, &lastidx);
360 if (err < 0)
361 return err;
362 }
363 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
364 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
365 if (err < 0)
366 return err;
367 }
368 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, lastname, &lastidx);
369 if (err < 0)
370 return err;
371 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, lastname, &lastidx);
372 if (err < 0)
373 return err;
374 return 0;
375}
376EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);