blob: 1fb7b06457ae9168027a884c55a3b6c43cd3988a [file] [log] [blame]
Thomas Gleixner62810db2019-05-20 19:08:11 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002/*
3 * Jack-detection handling for HD-audio
4 *
5 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
Takashi Iwai1835a0f2011-10-27 22:12:46 +02006 */
7
8#include <linux/init.h>
9#include <linux/slab.h>
Takashi Iwaibf815bf2011-11-16 14:28:33 +010010#include <linux/export.h>
Takashi Iwai1835a0f2011-10-27 22:12:46 +020011#include <sound/core.h>
Takashi Iwai01a61e12011-10-28 00:03:22 +020012#include <sound/control.h>
Takashi Iwaiaad37db2011-11-02 08:54:51 +010013#include <sound/jack.h>
Pierre-Louis Bossartbe57bff2018-08-22 15:24:57 -050014#include <sound/hda_codec.h>
Takashi Iwai1835a0f2011-10-27 22:12:46 +020015#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020016#include "hda_auto_parser.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020017#include "hda_jack.h"
18
Takashi Iwai95a962c2014-10-29 16:03:58 +010019/**
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 */
Takashi Iwaia9c74172012-02-13 11:41:42 +010029bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
30{
Takashi Iwai71b1e9e42012-02-13 11:55:02 +010031 if (codec->no_jack_detect)
32 return false;
Takashi Iwaia9c74172012-02-13 11:41:42 +010033 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
34 return false;
David Henningsson5fe8e1e2012-09-07 07:25:44 +020035 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
36 AC_DEFCFG_MISC_NO_PRESENCE)
Takashi Iwaia9c74172012-02-13 11:41:42 +010037 return false;
Takashi Iwai76a19c62013-01-07 12:11:33 +010038 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) &&
39 !codec->jackpoll_interval)
Takashi Iwaia9c74172012-02-13 11:41:42 +010040 return false;
41 return true;
42}
Takashi Iwai2698ea92013-12-18 07:45:52 +010043EXPORT_SYMBOL_GPL(is_jack_detectable);
Takashi Iwaia9c74172012-02-13 11:41:42 +010044
Takashi Iwai1835a0f2011-10-27 22:12:46 +020045/* execute pin sense measurement */
46static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
47{
48 u32 pincap;
Takashi Iwai9cc159c2012-12-21 15:31:41 +010049 u32 val;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020050
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 }
Takashi Iwai9cc159c2012-12-21 15:31:41 +010057 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai1835a0f2011-10-27 22:12:46 +020058 AC_VERB_GET_PIN_SENSE, 0);
Takashi Iwai9cc159c2012-12-21 15:31:41 +010059 if (codec->inv_jack_detect)
60 val ^= AC_PINSENSE_PRESENCE;
61 return val;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020062}
63
64/**
65 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
Takashi Iwaia11e9b12014-10-29 15:06:01 +010066 * @codec: the HDA codec
67 * @nid: pin NID to refer to
Takashi Iwai1835a0f2011-10-27 22:12:46 +020068 */
69struct hda_jack_tbl *
70snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
71{
72 struct hda_jack_tbl *jack = codec->jacktbl.list;
73 int i;
74
75 if (!nid || !jack)
76 return NULL;
77 for (i = 0; i < codec->jacktbl.used; i++, jack++)
78 if (jack->nid == nid)
79 return jack;
80 return NULL;
81}
Takashi Iwai2698ea92013-12-18 07:45:52 +010082EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get);
Takashi Iwai1835a0f2011-10-27 22:12:46 +020083
84/**
Takashi Iwai3a938972011-10-28 01:16:55 +020085 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
Takashi Iwaia11e9b12014-10-29 15:06:01 +010086 * @codec: the HDA codec
87 * @tag: tag value to refer to
Takashi Iwai3a938972011-10-28 01:16:55 +020088 */
89struct hda_jack_tbl *
90snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
91{
92 struct hda_jack_tbl *jack = codec->jacktbl.list;
93 int i;
94
95 if (!tag || !jack)
96 return NULL;
97 for (i = 0; i < codec->jacktbl.used; i++, jack++)
98 if (jack->tag == tag)
99 return jack;
100 return NULL;
101}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100102EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
Takashi Iwai3a938972011-10-28 01:16:55 +0200103
104/**
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200105 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100106 * @codec: the HDA codec
107 * @nid: pin NID to assign
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200108 */
Takashi Iwai81965f12014-09-11 14:22:03 +0200109static struct hda_jack_tbl *
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200110snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
111{
112 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
113 if (jack)
114 return jack;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200115 jack = snd_array_new(&codec->jacktbl);
116 if (!jack)
117 return NULL;
118 jack->nid = nid;
119 jack->jack_dirty = 1;
Takashi Iwai3a938972011-10-28 01:16:55 +0200120 jack->tag = codec->jacktbl.used;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200121 return jack;
122}
123
124void snd_hda_jack_tbl_clear(struct hda_codec *codec)
125{
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200126 struct hda_jack_tbl *jack = codec->jacktbl.list;
127 int i;
128
129 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
130 struct hda_jack_callback *cb, *next;
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800131
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200132 /* free jack instances manually when clearing/reconfiguring */
133 if (!codec->bus->shutdown && jack->jack)
Takashi Iwai6efdd852015-02-27 16:09:22 +0100134 snd_device_free(codec->card, jack->jack);
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800135
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200136 for (cb = jack->callback; cb; cb = next) {
137 next = cb->next;
138 kfree(cb);
Takashi Iwai31ef2252011-12-01 17:41:36 +0100139 }
140 }
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200141 snd_array_free(&codec->jacktbl);
142}
143
Dylan Reid0619ba82012-11-19 19:48:07 +0100144#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
145
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200146/* update the cached value and notification flag if needed */
147static void jack_detect_update(struct hda_codec *codec,
148 struct hda_jack_tbl *jack)
149{
David Henningsson80c8bfb2012-06-04 09:33:51 +0200150 if (!jack->jack_dirty)
151 return;
152
153 if (jack->phantom_jack)
154 jack->pin_sense = AC_PINSENSE_PRESENCE;
155 else
Takashi Iwai35be5442011-11-02 08:36:06 +0100156 jack->pin_sense = read_pin_sense(codec, jack->nid);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200157
Dylan Reid0619ba82012-11-19 19:48:07 +0100158 /* A gating jack indicates the jack is invalid if gating is unplugged */
159 if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack))
160 jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
161
David Henningsson80c8bfb2012-06-04 09:33:51 +0200162 jack->jack_dirty = 0;
Dylan Reid0619ba82012-11-19 19:48:07 +0100163
164 /* If a jack is gated by this one update it. */
165 if (jack->gated_jack) {
166 struct hda_jack_tbl *gated =
167 snd_hda_jack_tbl_get(codec, jack->gated_jack);
168 if (gated) {
169 gated->jack_dirty = 1;
170 jack_detect_update(codec, gated);
171 }
172 }
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200173}
174
175/**
176 * snd_hda_set_dirty_all - Mark all the cached as dirty
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100177 * @codec: the HDA codec
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200178 *
179 * This function sets the dirty flag to all entries of jack table.
180 * It's called from the resume path in hda_codec.c.
181 */
182void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
183{
184 struct hda_jack_tbl *jack = codec->jacktbl.list;
185 int i;
186
187 for (i = 0; i < codec->jacktbl.used; i++, jack++)
188 if (jack->nid)
189 jack->jack_dirty = 1;
190}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100191EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200192
193/**
194 * snd_hda_pin_sense - execute pin sense measurement
195 * @codec: the CODEC to sense
196 * @nid: the pin NID to sense
197 *
198 * Execute necessary pin sense measurement and return its Presence Detect,
199 * Impedance, ELD Valid etc. status bits.
200 */
201u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
202{
203 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
204 if (jack) {
205 jack_detect_update(codec, jack);
206 return jack->pin_sense;
207 }
208 return read_pin_sense(codec, nid);
209}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100210EXPORT_SYMBOL_GPL(snd_hda_pin_sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200211
212/**
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200213 * snd_hda_jack_detect_state - query pin Presence Detect status
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200214 * @codec: the CODEC to sense
215 * @nid: the pin NID to sense
216 *
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200217 * Query and return the pin's Presence Detect status, as either
218 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200219 */
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200220int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200221{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200222 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
223 if (jack && jack->phantom_jack)
224 return HDA_JACK_PHANTOM;
225 else if (snd_hda_pin_sense(codec, nid) & AC_PINSENSE_PRESENCE)
226 return HDA_JACK_PRESENT;
227 else
228 return HDA_JACK_NOT_PRESENT;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200229}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100230EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200231
232/**
233 * snd_hda_jack_detect_enable - enable the jack-detection
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100234 * @codec: the HDA codec
235 * @nid: pin NID to enable
236 * @func: callback function to register
Takashi Iwaibda17b822014-09-11 14:39:09 +0200237 *
238 * In the case of error, the return value will be a pointer embedded with
239 * errno. Check and handle the return value appropriately with standard
240 * macros such as @IS_ERR() and @PTR_ERR().
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200241 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200242struct hda_jack_callback *
Takashi Iwaibda17b822014-09-11 14:39:09 +0200243snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200244 hda_jack_callback_fn func)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200245{
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200246 struct hda_jack_tbl *jack;
247 struct hda_jack_callback *callback = NULL;
Takashi Iwaibda17b822014-09-11 14:39:09 +0200248 int err;
249
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200250 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200251 if (!jack)
Takashi Iwaibda17b822014-09-11 14:39:09 +0200252 return ERR_PTR(-ENOMEM);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200253 if (func) {
254 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
255 if (!callback)
256 return ERR_PTR(-ENOMEM);
257 callback->func = func;
Takashi Iwai2ebab402016-02-09 10:23:52 +0100258 callback->nid = jack->nid;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200259 callback->next = jack->callback;
260 jack->callback = callback;
261 }
262
Takashi Iwai3a938972011-10-28 01:16:55 +0200263 if (jack->jack_detect)
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200264 return callback; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200265 jack->jack_detect = 1;
David Henningsson34a93182012-10-17 14:45:05 +0200266 if (codec->jackpoll_interval > 0)
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200267 return callback; /* No unsol if we're polling instead */
Takashi Iwaibda17b822014-09-11 14:39:09 +0200268 err = snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200269 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200270 AC_USRSP_EN | jack->tag);
Takashi Iwaibda17b822014-09-11 14:39:09 +0200271 if (err < 0)
272 return ERR_PTR(err);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200273 return callback;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200274}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100275EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback);
David Henningsson954df2a2012-09-25 11:30:59 +0200276
Takashi Iwai95a962c2014-10-29 16:03:58 +0100277/**
278 * snd_hda_jack_detect_enable - Enable the jack detection on the given pin
279 * @codec: the HDA codec
280 * @nid: pin NID to enable jack detection
281 *
282 * Enable the jack detection with the default callback. Returns zero if
283 * successful or a negative error code.
284 */
Takashi Iwai62f949b2014-09-11 14:06:53 +0200285int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid)
David Henningsson954df2a2012-09-25 11:30:59 +0200286{
Takashi Iwaibda17b822014-09-11 14:39:09 +0200287 return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback(codec, nid, NULL));
David Henningsson954df2a2012-09-25 11:30:59 +0200288}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100289EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200290
Takashi Iwai01a61e12011-10-28 00:03:22 +0200291/**
Dylan Reid0619ba82012-11-19 19:48:07 +0100292 * snd_hda_jack_set_gating_jack - Set gating jack.
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100293 * @codec: the HDA codec
294 * @gated_nid: gated pin NID
295 * @gating_nid: gating pin NID
Dylan Reid0619ba82012-11-19 19:48:07 +0100296 *
297 * Indicates the gated jack is only valid when the gating jack is plugged.
298 */
299int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
300 hda_nid_t gating_nid)
301{
Chih-Chung Changbde7bc62013-08-05 16:38:42 +0800302 struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid);
303 struct hda_jack_tbl *gating = snd_hda_jack_tbl_new(codec, gating_nid);
Dylan Reid0619ba82012-11-19 19:48:07 +0100304
305 if (!gated || !gating)
306 return -EINVAL;
307
308 gated->gating_jack = gating_nid;
309 gating->gated_jack = gated_nid;
310
311 return 0;
312}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100313EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
Dylan Reid0619ba82012-11-19 19:48:07 +0100314
315/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200316 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100317 * @codec: the HDA codec
Takashi Iwai01a61e12011-10-28 00:03:22 +0200318 */
319void snd_hda_jack_report_sync(struct hda_codec *codec)
320{
Dylan Reid0619ba82012-11-19 19:48:07 +0100321 struct hda_jack_tbl *jack;
Takashi Iwai35be5442011-11-02 08:36:06 +0100322 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200323
Dylan Reid0619ba82012-11-19 19:48:07 +0100324 /* update all jacks at first */
325 jack = codec->jacktbl.list;
326 for (i = 0; i < codec->jacktbl.used; i++, jack++)
327 if (jack->nid)
328 jack_detect_update(codec, jack);
329
330 /* report the updated jacks; it's done after updating all jacks
331 * to make sure that all gating jacks properly have been set
332 */
333 jack = codec->jacktbl.list;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200334 for (i = 0; i < codec->jacktbl.used; i++, jack++)
335 if (jack->nid) {
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800336 if (!jack->jack || jack->block_report)
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100337 continue;
Takashi Iwai911761c2018-11-28 14:25:37 +0100338 state = jack->button_state;
339 if (get_jack_plug_state(jack->pin_sense))
340 state |= jack->type;
341 snd_jack_report(jack->jack, state);
342 if (jack->button_state) {
343 snd_jack_report(jack->jack,
344 state & ~jack->button_state);
345 jack->button_state = 0; /* button released */
346 }
Takashi Iwai01a61e12011-10-28 00:03:22 +0200347 }
348}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100349EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200350
Takashi Iwai31ef2252011-12-01 17:41:36 +0100351/* guess the jack type from the pin-config */
352static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
353{
354 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
355 switch (get_defcfg_device(def_conf)) {
356 case AC_JACK_LINE_OUT:
357 case AC_JACK_SPEAKER:
358 return SND_JACK_LINEOUT;
359 case AC_JACK_HP_OUT:
360 return SND_JACK_HEADPHONE;
361 case AC_JACK_SPDIF_OUT:
362 case AC_JACK_DIG_OTHER_OUT:
363 return SND_JACK_AVOUT;
364 case AC_JACK_MIC_IN:
365 return SND_JACK_MICROPHONE;
366 default:
367 return SND_JACK_LINEIN;
368 }
369}
370
371static void hda_free_jack_priv(struct snd_jack *jack)
372{
373 struct hda_jack_tbl *jacks = jack->private_data;
374 jacks->nid = 0;
375 jacks->jack = NULL;
376}
Takashi Iwai31ef2252011-12-01 17:41:36 +0100377
Takashi Iwai01a61e12011-10-28 00:03:22 +0200378/**
379 * snd_hda_jack_add_kctl - Add a kctl for the given pin
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100380 * @codec: the HDA codec
381 * @nid: pin NID to assign
382 * @name: string name for the jack
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100383 * @phantom_jack: flag to deal as a phantom jack
Takashi Iwai911761c2018-11-28 14:25:37 +0100384 * @type: jack type bits to be reported, 0 for guessing from pincfg
385 * @keymap: optional jack / key mapping
Takashi Iwai01a61e12011-10-28 00:03:22 +0200386 *
387 * This assigns a jack-detection kctl to the given pin. The kcontrol
388 * will have the given name and index.
389 */
Takashi Iwai909cadc2015-11-12 11:52:13 +0100390int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai911761c2018-11-28 14:25:37 +0100391 const char *name, bool phantom_jack,
392 int type, const struct hda_jack_keymap *keymap)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200393{
394 struct hda_jack_tbl *jack;
Takashi Iwai911761c2018-11-28 14:25:37 +0100395 const struct hda_jack_keymap *map;
396 int err, state, buttons;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200397
Takashi Iwai3a938972011-10-28 01:16:55 +0200398 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200399 if (!jack)
400 return 0;
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800401 if (jack->jack)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200402 return 0; /* already created */
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800403
Takashi Iwai911761c2018-11-28 14:25:37 +0100404 if (!type)
405 type = get_input_jack_type(codec, nid);
406
407 buttons = 0;
408 if (keymap) {
409 for (map = keymap; map->type; map++)
410 buttons |= map->type;
411 }
412
413 err = snd_jack_new(codec->card, name, type | buttons,
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800414 &jack->jack, true, phantom_jack);
Takashi Iwai31ef2252011-12-01 17:41:36 +0100415 if (err < 0)
416 return err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200417
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800418 jack->phantom_jack = !!phantom_jack;
419 jack->type = type;
Takashi Iwai911761c2018-11-28 14:25:37 +0100420 jack->button_state = 0;
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800421 jack->jack->private_data = jack;
422 jack->jack->private_free = hda_free_jack_priv;
Takashi Iwai911761c2018-11-28 14:25:37 +0100423 if (keymap) {
424 for (map = keymap; map->type; map++)
425 snd_jack_set_key(jack->jack, map->type, map->key);
426 }
427
Takashi Iwai31ef2252011-12-01 17:41:36 +0100428 state = snd_hda_jack_detect(codec, nid);
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800429 snd_jack_report(jack->jack, state ? jack->type : 0);
430
Takashi Iwai01a61e12011-10-28 00:03:22 +0200431 return 0;
432}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100433EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200434
Takashi Iwai201e06f2011-11-16 15:33:26 +0100435static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
David Henningssonb26b5112013-04-11 11:30:30 +0200436 const struct auto_pin_cfg *cfg,
437 const char *base_name)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200438{
Takashi Iwai3a938972011-10-28 01:16:55 +0200439 unsigned int def_conf, conn;
Takashi Iwai975cc022013-06-28 11:56:49 +0200440 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800441 int err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200442 bool phantom_jack;
Takashi Iwai3a938972011-10-28 01:16:55 +0200443
Takashi Iwai01a61e12011-10-28 00:03:22 +0200444 if (!nid)
445 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200446 def_conf = snd_hda_codec_get_pincfg(codec, nid);
447 conn = get_defcfg_connect(def_conf);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200448 if (conn == AC_JACK_PORT_NONE)
Takashi Iwai3a938972011-10-28 01:16:55 +0200449 return 0;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200450 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
451 !is_jack_detectable(codec, nid);
Takashi Iwai3a938972011-10-28 01:16:55 +0200452
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800453 if (base_name)
David Henningssonb26b5112013-04-11 11:30:30 +0200454 strlcpy(name, base_name, sizeof(name));
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800455 else
456 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200457 if (phantom_jack)
458 /* Example final name: "Internal Mic Phantom Jack" */
459 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
Takashi Iwai911761c2018-11-28 14:25:37 +0100460 err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack, 0, NULL);
Takashi Iwai3a938972011-10-28 01:16:55 +0200461 if (err < 0)
462 return err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200463
464 if (!phantom_jack)
Takashi Iwai62f949b2014-09-11 14:06:53 +0200465 return snd_hda_jack_detect_enable(codec, nid);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200466 return 0;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200467}
468
469/**
470 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100471 * @codec: the HDA codec
472 * @cfg: pin config table to parse
Takashi Iwai01a61e12011-10-28 00:03:22 +0200473 */
474int snd_hda_jack_add_kctls(struct hda_codec *codec,
475 const struct auto_pin_cfg *cfg)
476{
477 const hda_nid_t *p;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200478 int i, err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200479
David Henningssonb26b5112013-04-11 11:30:30 +0200480 for (i = 0; i < cfg->num_inputs; i++) {
481 /* If we have headphone mics; make sure they get the right name
482 before grabbed by output pins */
483 if (cfg->inputs[i].is_headphone_mic) {
484 if (auto_cfg_hp_outs(cfg) == 1)
485 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
486 cfg, "Headphone Mic");
487 else
488 err = add_jack_kctl(codec, cfg->inputs[i].pin,
489 cfg, "Headphone Mic");
490 } else
491 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
492 NULL);
493 if (err < 0)
494 return err;
495 }
496
Takashi Iwai01a61e12011-10-28 00:03:22 +0200497 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
David Henningssonb26b5112013-04-11 11:30:30 +0200498 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200499 if (err < 0)
500 return err;
501 }
502 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
503 if (*p == *cfg->line_out_pins) /* might be duplicated */
504 break;
David Henningssonb26b5112013-04-11 11:30:30 +0200505 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200506 if (err < 0)
507 return err;
508 }
509 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
510 if (*p == *cfg->line_out_pins) /* might be duplicated */
511 break;
David Henningssonb26b5112013-04-11 11:30:30 +0200512 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200513 if (err < 0)
514 return err;
515 }
516 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
David Henningssonb26b5112013-04-11 11:30:30 +0200517 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200518 if (err < 0)
519 return err;
520 }
David Henningssonb26b5112013-04-11 11:30:30 +0200521 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200522 if (err < 0)
523 return err;
David Henningssonb26b5112013-04-11 11:30:30 +0200524 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200525 if (err < 0)
526 return err;
527 return 0;
528}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100529EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
David Henningsson954df2a2012-09-25 11:30:59 +0200530
Takashi Iwaie6ce1802018-11-28 14:17:58 +0100531static void call_jack_callback(struct hda_codec *codec, unsigned int res,
Dylan Reid0619ba82012-11-19 19:48:07 +0100532 struct hda_jack_tbl *jack)
533{
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200534 struct hda_jack_callback *cb;
535
Takashi Iwaie6ce1802018-11-28 14:17:58 +0100536 for (cb = jack->callback; cb; cb = cb->next) {
537 cb->jack = jack;
538 cb->unsol_res = res;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200539 cb->func(codec, cb);
Takashi Iwaie6ce1802018-11-28 14:17:58 +0100540 }
Dylan Reid0619ba82012-11-19 19:48:07 +0100541 if (jack->gated_jack) {
542 struct hda_jack_tbl *gated =
543 snd_hda_jack_tbl_get(codec, jack->gated_jack);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200544 if (gated) {
Takashi Iwaie6ce1802018-11-28 14:17:58 +0100545 for (cb = gated->callback; cb; cb = cb->next) {
546 cb->jack = gated;
547 cb->unsol_res = res;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200548 cb->func(codec, cb);
Takashi Iwaie6ce1802018-11-28 14:17:58 +0100549 }
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200550 }
Dylan Reid0619ba82012-11-19 19:48:07 +0100551 }
552}
553
Takashi Iwai95a962c2014-10-29 16:03:58 +0100554/**
555 * snd_hda_jack_unsol_event - Handle an unsolicited event
556 * @codec: the HDA codec
557 * @res: the unsolicited event data
558 */
David Henningsson954df2a2012-09-25 11:30:59 +0200559void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
560{
561 struct hda_jack_tbl *event;
Guennadi Liakhovetskic7e68a62019-06-11 19:09:07 +0200562 int tag = (res & AC_UNSOL_RES_TAG) >> AC_UNSOL_RES_TAG_SHIFT;
David Henningsson954df2a2012-09-25 11:30:59 +0200563
564 event = snd_hda_jack_tbl_get_from_tag(codec, tag);
565 if (!event)
566 return;
567 event->jack_dirty = 1;
568
Takashi Iwaie6ce1802018-11-28 14:17:58 +0100569 call_jack_callback(codec, res, event);
David Henningsson954df2a2012-09-25 11:30:59 +0200570 snd_hda_jack_report_sync(codec);
571}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100572EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
David Henningsson954df2a2012-09-25 11:30:59 +0200573
Takashi Iwai95a962c2014-10-29 16:03:58 +0100574/**
575 * snd_hda_jack_poll_all - Poll all jacks
576 * @codec: the HDA codec
577 *
578 * Poll all detectable jacks with dirty flag, update the status, call
579 * callbacks and call snd_hda_jack_report_sync() if any changes are found.
580 */
David Henningsson26a6cb62012-10-09 15:04:21 +0200581void snd_hda_jack_poll_all(struct hda_codec *codec)
582{
583 struct hda_jack_tbl *jack = codec->jacktbl.list;
584 int i, changes = 0;
585
586 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
587 unsigned int old_sense;
588 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
589 continue;
590 old_sense = get_jack_plug_state(jack->pin_sense);
591 jack_detect_update(codec, jack);
592 if (old_sense == get_jack_plug_state(jack->pin_sense))
593 continue;
594 changes = 1;
Takashi Iwaie6ce1802018-11-28 14:17:58 +0100595 call_jack_callback(codec, 0, jack);
David Henningsson26a6cb62012-10-09 15:04:21 +0200596 }
597 if (changes)
598 snd_hda_jack_report_sync(codec);
599}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100600EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);
David Henningsson26a6cb62012-10-09 15:04:21 +0200601