]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/pci/hda/hda_jack.c
ALSA: hda - Get rid of action field from struct hda_jack_tbl
[mirror_ubuntu-bionic-kernel.git] / sound / pci / hda / hda_jack.c
CommitLineData
1835a0f9
TI
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>
bf815bf0 14#include <linux/export.h>
1835a0f9 15#include <sound/core.h>
01a61e12 16#include <sound/control.h>
aad37dbd 17#include <sound/jack.h>
1835a0f9
TI
18#include "hda_codec.h"
19#include "hda_local.h"
128bc4ba 20#include "hda_auto_parser.h"
1835a0f9
TI
21#include "hda_jack.h"
22
a9c74173
TI
23bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
24{
71b1e9e4
TI
25 if (codec->no_jack_detect)
26 return false;
a9c74173
TI
27 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
28 return false;
5fe8e1e6
DH
29 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
30 AC_DEFCFG_MISC_NO_PRESENCE)
a9c74173 31 return false;
76a19c69
TI
32 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) &&
33 !codec->jackpoll_interval)
a9c74173
TI
34 return false;
35 return true;
36}
2698ea98 37EXPORT_SYMBOL_GPL(is_jack_detectable);
a9c74173 38
1835a0f9
TI
39/* execute pin sense measurement */
40static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
41{
42 u32 pincap;
9cc159c6 43 u32 val;
1835a0f9
TI
44
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);
50 }
9cc159c6 51 val = snd_hda_codec_read(codec, nid, 0,
1835a0f9 52 AC_VERB_GET_PIN_SENSE, 0);
9cc159c6
TI
53 if (codec->inv_jack_detect)
54 val ^= AC_PINSENSE_PRESENCE;
55 return val;
1835a0f9
TI
56}
57
58/**
59 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
60 */
61struct hda_jack_tbl *
62snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
63{
64 struct hda_jack_tbl *jack = codec->jacktbl.list;
65 int i;
66
67 if (!nid || !jack)
68 return NULL;
69 for (i = 0; i < codec->jacktbl.used; i++, jack++)
70 if (jack->nid == nid)
71 return jack;
72 return NULL;
73}
2698ea98 74EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get);
1835a0f9 75
3a93897e
TI
76/**
77 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
78 */
79struct hda_jack_tbl *
80snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
81{
82 struct hda_jack_tbl *jack = codec->jacktbl.list;
83 int i;
84
85 if (!tag || !jack)
86 return NULL;
87 for (i = 0; i < codec->jacktbl.used; i++, jack++)
88 if (jack->tag == tag)
89 return jack;
90 return NULL;
91}
2698ea98 92EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
3a93897e 93
1835a0f9
TI
94/**
95 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
96 */
97struct hda_jack_tbl *
98snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
99{
100 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
101 if (jack)
102 return jack;
1835a0f9
TI
103 jack = snd_array_new(&codec->jacktbl);
104 if (!jack)
105 return NULL;
106 jack->nid = nid;
107 jack->jack_dirty = 1;
3a93897e 108 jack->tag = codec->jacktbl.used;
1835a0f9
TI
109 return jack;
110}
2698ea98 111EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_new);
1835a0f9
TI
112
113void snd_hda_jack_tbl_clear(struct hda_codec *codec)
114{
31ef2257
TI
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;
119 int i;
120 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
121 if (jack->jack)
122 snd_device_free(codec->bus->card, jack->jack);
123 }
124 }
125#endif
1835a0f9
TI
126 snd_array_free(&codec->jacktbl);
127}
128
0619ba8c
DR
129#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
130
1835a0f9
TI
131/* update the cached value and notification flag if needed */
132static void jack_detect_update(struct hda_codec *codec,
133 struct hda_jack_tbl *jack)
134{
80c8bfbe
DH
135 if (!jack->jack_dirty)
136 return;
137
138 if (jack->phantom_jack)
139 jack->pin_sense = AC_PINSENSE_PRESENCE;
140 else
35be544a 141 jack->pin_sense = read_pin_sense(codec, jack->nid);
80c8bfbe 142
0619ba8c
DR
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;
146
80c8bfbe 147 jack->jack_dirty = 0;
0619ba8c
DR
148
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);
153 if (gated) {
154 gated->jack_dirty = 1;
155 jack_detect_update(codec, gated);
156 }
157 }
1835a0f9
TI
158}
159
160/**
161 * snd_hda_set_dirty_all - Mark all the cached as dirty
162 *
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.
165 */
166void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
167{
168 struct hda_jack_tbl *jack = codec->jacktbl.list;
169 int i;
170
171 for (i = 0; i < codec->jacktbl.used; i++, jack++)
172 if (jack->nid)
173 jack->jack_dirty = 1;
174}
2698ea98 175EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
1835a0f9
TI
176
177/**
178 * snd_hda_pin_sense - execute pin sense measurement
179 * @codec: the CODEC to sense
180 * @nid: the pin NID to sense
181 *
182 * Execute necessary pin sense measurement and return its Presence Detect,
183 * Impedance, ELD Valid etc. status bits.
184 */
185u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
186{
187 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
188 if (jack) {
189 jack_detect_update(codec, jack);
190 return jack->pin_sense;
191 }
192 return read_pin_sense(codec, nid);
193}
2698ea98 194EXPORT_SYMBOL_GPL(snd_hda_pin_sense);
1835a0f9
TI
195
196/**
60ea8ca2 197 * snd_hda_jack_detect_state - query pin Presence Detect status
1835a0f9
TI
198 * @codec: the CODEC to sense
199 * @nid: the pin NID to sense
200 *
60ea8ca2
TI
201 * Query and return the pin's Presence Detect status, as either
202 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
1835a0f9 203 */
60ea8ca2 204int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid)
1835a0f9 205{
60ea8ca2
TI
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;
211 else
212 return HDA_JACK_NOT_PRESENT;
1835a0f9 213}
2698ea98 214EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state);
1835a0f9
TI
215
216/**
217 * snd_hda_jack_detect_enable - enable the jack-detection
218 */
954df2a9 219int snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
954df2a9 220 hda_jack_callback cb)
1835a0f9
TI
221{
222 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
223 if (!jack)
224 return -ENOMEM;
3a93897e 225 if (jack->jack_detect)
01a61e12 226 return 0; /* already registered */
3a93897e 227 jack->jack_detect = 1;
954df2a9
DH
228 if (cb)
229 jack->callback = cb;
34a93187
DH
230 if (codec->jackpoll_interval > 0)
231 return 0; /* No unsol if we're polling instead */
1835a0f9
TI
232 return snd_hda_codec_write_cache(codec, nid, 0,
233 AC_VERB_SET_UNSOLICITED_ENABLE,
3a93897e 234 AC_USRSP_EN | jack->tag);
1835a0f9 235}
2698ea98 236EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback);
954df2a9 237
62f949bf 238int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid)
954df2a9 239{
62f949bf 240 return snd_hda_jack_detect_enable_callback(codec, nid, NULL);
954df2a9 241}
2698ea98 242EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
01a61e12 243
0619ba8c
DR
244/**
245 * snd_hda_jack_set_gating_jack - Set gating jack.
246 *
247 * Indicates the gated jack is only valid when the gating jack is plugged.
248 */
249int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
250 hda_nid_t gating_nid)
251{
bde7bc60
CCC
252 struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid);
253 struct hda_jack_tbl *gating = snd_hda_jack_tbl_new(codec, gating_nid);
0619ba8c
DR
254
255 if (!gated || !gating)
256 return -EINVAL;
257
258 gated->gating_jack = gating_nid;
259 gating->gated_jack = gated_nid;
260
261 return 0;
262}
2698ea98 263EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
0619ba8c 264
01a61e12
TI
265/**
266 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
267 */
268void snd_hda_jack_report_sync(struct hda_codec *codec)
269{
0619ba8c 270 struct hda_jack_tbl *jack;
35be544a 271 int i, state;
01a61e12 272
0619ba8c
DR
273 /* update all jacks at first */
274 jack = codec->jacktbl.list;
01a61e12 275 for (i = 0; i < codec->jacktbl.used; i++, jack++)
0619ba8c 276 if (jack->nid)
01a61e12 277 jack_detect_update(codec, jack);
0619ba8c
DR
278
279 /* report the updated jacks; it's done after updating all jacks
280 * to make sure that all gating jacks properly have been set
281 */
282 jack = codec->jacktbl.list;
283 for (i = 0; i < codec->jacktbl.used; i++, jack++)
284 if (jack->nid) {
0f568959 285 if (!jack->kctl || jack->block_report)
cfc7c9d3 286 continue;
35be544a 287 state = get_jack_plug_state(jack->pin_sense);
aad37dbd 288 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
31ef2257
TI
289#ifdef CONFIG_SND_HDA_INPUT_JACK
290 if (jack->jack)
291 snd_jack_report(jack->jack,
292 state ? jack->type : 0);
293#endif
01a61e12
TI
294 }
295}
2698ea98 296EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
01a61e12 297
31ef2257
TI
298#ifdef CONFIG_SND_HDA_INPUT_JACK
299/* guess the jack type from the pin-config */
300static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
301{
302 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
303 switch (get_defcfg_device(def_conf)) {
304 case AC_JACK_LINE_OUT:
305 case AC_JACK_SPEAKER:
306 return SND_JACK_LINEOUT;
307 case AC_JACK_HP_OUT:
308 return SND_JACK_HEADPHONE;
309 case AC_JACK_SPDIF_OUT:
310 case AC_JACK_DIG_OTHER_OUT:
311 return SND_JACK_AVOUT;
312 case AC_JACK_MIC_IN:
313 return SND_JACK_MICROPHONE;
314 default:
315 return SND_JACK_LINEIN;
316 }
317}
318
319static void hda_free_jack_priv(struct snd_jack *jack)
320{
321 struct hda_jack_tbl *jacks = jack->private_data;
322 jacks->nid = 0;
323 jacks->jack = NULL;
324}
325#endif
326
01a61e12
TI
327/**
328 * snd_hda_jack_add_kctl - Add a kctl for the given pin
329 *
330 * This assigns a jack-detection kctl to the given pin. The kcontrol
331 * will have the given name and index.
332 */
80c8bfbe
DH
333static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
334 const char *name, int idx, bool phantom_jack)
01a61e12
TI
335{
336 struct hda_jack_tbl *jack;
337 struct snd_kcontrol *kctl;
31ef2257 338 int err, state;
01a61e12 339
3a93897e 340 jack = snd_hda_jack_tbl_new(codec, nid);
01a61e12
TI
341 if (!jack)
342 return 0;
343 if (jack->kctl)
344 return 0; /* already created */
35be544a 345 kctl = snd_kctl_jack_new(name, idx, codec);
01a61e12
TI
346 if (!kctl)
347 return -ENOMEM;
31ef2257
TI
348 err = snd_hda_ctl_add(codec, nid, kctl);
349 if (err < 0)
350 return err;
01a61e12 351 jack->kctl = kctl;
80c8bfbe
DH
352 jack->phantom_jack = !!phantom_jack;
353
31ef2257
TI
354 state = snd_hda_jack_detect(codec, nid);
355 snd_kctl_jack_report(codec->bus->card, kctl, state);
356#ifdef CONFIG_SND_HDA_INPUT_JACK
80c8bfbe
DH
357 if (!phantom_jack) {
358 jack->type = get_input_jack_type(codec, nid);
359 err = snd_jack_new(codec->bus->card, name, jack->type,
360 &jack->jack);
361 if (err < 0)
362 return err;
363 jack->jack->private_data = jack;
364 jack->jack->private_free = hda_free_jack_priv;
365 snd_jack_report(jack->jack, state ? jack->type : 0);
366 }
31ef2257 367#endif
01a61e12
TI
368 return 0;
369}
80c8bfbe
DH
370
371int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
372 const char *name, int idx)
373{
374 return __snd_hda_jack_add_kctl(codec, nid, name, idx, false);
375}
2698ea98 376EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl);
01a61e12 377
f46c3296
TI
378/* get the unique index number for the given kctl name */
379static int get_unique_index(struct hda_codec *codec, const char *name, int idx)
380{
381 struct hda_jack_tbl *jack;
382 int i, len = strlen(name);
383 again:
384 jack = codec->jacktbl.list;
385 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
386 /* jack->kctl.id contains "XXX Jack" name string with index */
387 if (jack->kctl &&
388 !strncmp(name, jack->kctl->id.name, len) &&
389 !strcmp(" Jack", jack->kctl->id.name + len) &&
390 jack->kctl->id.index == idx) {
391 idx++;
392 goto again;
393 }
394 }
395 return idx;
396}
397
201e06ff 398static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
b26b5116
DH
399 const struct auto_pin_cfg *cfg,
400 const char *base_name)
01a61e12 401{
3a93897e 402 unsigned int def_conf, conn;
975cc02a 403 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
201e06ff 404 int idx, err;
80c8bfbe 405 bool phantom_jack;
3a93897e 406
01a61e12
TI
407 if (!nid)
408 return 0;
3a93897e
TI
409 def_conf = snd_hda_codec_get_pincfg(codec, nid);
410 conn = get_defcfg_connect(def_conf);
80c8bfbe 411 if (conn == AC_JACK_PORT_NONE)
3a93897e 412 return 0;
80c8bfbe
DH
413 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
414 !is_jack_detectable(codec, nid);
3a93897e 415
b26b5116
DH
416 if (base_name) {
417 strlcpy(name, base_name, sizeof(name));
418 idx = 0;
419 } else
420 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
80c8bfbe
DH
421 if (phantom_jack)
422 /* Example final name: "Internal Mic Phantom Jack" */
423 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
f46c3296 424 idx = get_unique_index(codec, name, idx);
80c8bfbe 425 err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack);
3a93897e
TI
426 if (err < 0)
427 return err;
80c8bfbe
DH
428
429 if (!phantom_jack)
62f949bf 430 return snd_hda_jack_detect_enable(codec, nid);
80c8bfbe 431 return 0;
01a61e12
TI
432}
433
434/**
435 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
01a61e12
TI
436 */
437int snd_hda_jack_add_kctls(struct hda_codec *codec,
438 const struct auto_pin_cfg *cfg)
439{
440 const hda_nid_t *p;
f46c3296 441 int i, err;
01a61e12 442
b26b5116
DH
443 for (i = 0; i < cfg->num_inputs; i++) {
444 /* If we have headphone mics; make sure they get the right name
445 before grabbed by output pins */
446 if (cfg->inputs[i].is_headphone_mic) {
447 if (auto_cfg_hp_outs(cfg) == 1)
448 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
449 cfg, "Headphone Mic");
450 else
451 err = add_jack_kctl(codec, cfg->inputs[i].pin,
452 cfg, "Headphone Mic");
453 } else
454 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
455 NULL);
456 if (err < 0)
457 return err;
458 }
459
01a61e12 460 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
b26b5116 461 err = add_jack_kctl(codec, *p, cfg, NULL);
01a61e12
TI
462 if (err < 0)
463 return err;
464 }
465 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
466 if (*p == *cfg->line_out_pins) /* might be duplicated */
467 break;
b26b5116 468 err = add_jack_kctl(codec, *p, cfg, NULL);
01a61e12
TI
469 if (err < 0)
470 return err;
471 }
472 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
473 if (*p == *cfg->line_out_pins) /* might be duplicated */
474 break;
b26b5116 475 err = add_jack_kctl(codec, *p, cfg, NULL);
01a61e12
TI
476 if (err < 0)
477 return err;
478 }
479 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
b26b5116 480 err = add_jack_kctl(codec, *p, cfg, NULL);
01a61e12
TI
481 if (err < 0)
482 return err;
483 }
b26b5116 484 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
01a61e12
TI
485 if (err < 0)
486 return err;
b26b5116 487 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
01a61e12
TI
488 if (err < 0)
489 return err;
490 return 0;
491}
2698ea98 492EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
954df2a9 493
0619ba8c
DR
494static void call_jack_callback(struct hda_codec *codec,
495 struct hda_jack_tbl *jack)
496{
497 if (jack->callback)
498 jack->callback(codec, jack);
499 if (jack->gated_jack) {
500 struct hda_jack_tbl *gated =
501 snd_hda_jack_tbl_get(codec, jack->gated_jack);
502 if (gated && gated->callback)
503 gated->callback(codec, gated);
504 }
505}
506
954df2a9
DH
507void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
508{
509 struct hda_jack_tbl *event;
510 int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f;
511
512 event = snd_hda_jack_tbl_get_from_tag(codec, tag);
513 if (!event)
514 return;
515 event->jack_dirty = 1;
516
0619ba8c 517 call_jack_callback(codec, event);
954df2a9
DH
518 snd_hda_jack_report_sync(codec);
519}
2698ea98 520EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
954df2a9 521
26a6cb6c
DH
522void snd_hda_jack_poll_all(struct hda_codec *codec)
523{
524 struct hda_jack_tbl *jack = codec->jacktbl.list;
525 int i, changes = 0;
526
527 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
528 unsigned int old_sense;
529 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
530 continue;
531 old_sense = get_jack_plug_state(jack->pin_sense);
532 jack_detect_update(codec, jack);
533 if (old_sense == get_jack_plug_state(jack->pin_sense))
534 continue;
535 changes = 1;
0619ba8c 536 call_jack_callback(codec, jack);
26a6cb6c
DH
537 }
538 if (changes)
539 snd_hda_jack_report_sync(codec);
540}
2698ea98 541EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);
26a6cb6c 542