Commit | Line | Data |
---|---|---|
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 | ||
95a962c3 TI |
23 | /** |
24 | * is_jack_detectable - Check whether the given pin is jack-detectable | |
25 | * @codec: the HDA codec | |
26 | * @nid: pin NID | |
27 | * | |
28 | * Check whether the given pin is capable to report the jack detection. | |
29 | * The jack detection might not work by various reasons, e.g. the jack | |
30 | * detection is prohibited in the codec level, the pin config has | |
31 | * AC_DEFCFG_MISC_NO_PRESENCE bit, no unsol support, etc. | |
32 | */ | |
a9c74173 TI |
33 | bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid) |
34 | { | |
71b1e9e4 TI |
35 | if (codec->no_jack_detect) |
36 | return false; | |
a9c74173 TI |
37 | if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT)) |
38 | return false; | |
5fe8e1e6 DH |
39 | if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) & |
40 | AC_DEFCFG_MISC_NO_PRESENCE) | |
a9c74173 | 41 | return false; |
76a19c69 TI |
42 | if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) && |
43 | !codec->jackpoll_interval) | |
a9c74173 TI |
44 | return false; |
45 | return true; | |
46 | } | |
2698ea98 | 47 | EXPORT_SYMBOL_GPL(is_jack_detectable); |
a9c74173 | 48 | |
1835a0f9 TI |
49 | /* execute pin sense measurement */ |
50 | static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid) | |
51 | { | |
52 | u32 pincap; | |
9cc159c6 | 53 | u32 val; |
1835a0f9 TI |
54 | |
55 | if (!codec->no_trigger_sense) { | |
56 | pincap = snd_hda_query_pin_caps(codec, nid); | |
57 | if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */ | |
58 | snd_hda_codec_read(codec, nid, 0, | |
59 | AC_VERB_SET_PIN_SENSE, 0); | |
60 | } | |
9cc159c6 | 61 | val = snd_hda_codec_read(codec, nid, 0, |
1835a0f9 | 62 | AC_VERB_GET_PIN_SENSE, 0); |
9cc159c6 TI |
63 | if (codec->inv_jack_detect) |
64 | val ^= AC_PINSENSE_PRESENCE; | |
65 | return val; | |
1835a0f9 TI |
66 | } |
67 | ||
68 | /** | |
69 | * snd_hda_jack_tbl_get - query the jack-table entry for the given NID | |
a11e9b16 TI |
70 | * @codec: the HDA codec |
71 | * @nid: pin NID to refer to | |
1835a0f9 TI |
72 | */ |
73 | struct hda_jack_tbl * | |
74 | snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid) | |
75 | { | |
76 | struct hda_jack_tbl *jack = codec->jacktbl.list; | |
77 | int i; | |
78 | ||
79 | if (!nid || !jack) | |
80 | return NULL; | |
81 | for (i = 0; i < codec->jacktbl.used; i++, jack++) | |
82 | if (jack->nid == nid) | |
83 | return jack; | |
84 | return NULL; | |
85 | } | |
2698ea98 | 86 | EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get); |
1835a0f9 | 87 | |
3a93897e TI |
88 | /** |
89 | * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag | |
a11e9b16 TI |
90 | * @codec: the HDA codec |
91 | * @tag: tag value to refer to | |
3a93897e TI |
92 | */ |
93 | struct hda_jack_tbl * | |
94 | snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag) | |
95 | { | |
96 | struct hda_jack_tbl *jack = codec->jacktbl.list; | |
97 | int i; | |
98 | ||
99 | if (!tag || !jack) | |
100 | return NULL; | |
101 | for (i = 0; i < codec->jacktbl.used; i++, jack++) | |
102 | if (jack->tag == tag) | |
103 | return jack; | |
104 | return NULL; | |
105 | } | |
2698ea98 | 106 | EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag); |
3a93897e | 107 | |
1835a0f9 TI |
108 | /** |
109 | * snd_hda_jack_tbl_new - create a jack-table entry for the given NID | |
a11e9b16 TI |
110 | * @codec: the HDA codec |
111 | * @nid: pin NID to assign | |
1835a0f9 | 112 | */ |
81965f1f | 113 | static struct hda_jack_tbl * |
1835a0f9 TI |
114 | snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid) |
115 | { | |
116 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); | |
117 | if (jack) | |
118 | return jack; | |
1835a0f9 TI |
119 | jack = snd_array_new(&codec->jacktbl); |
120 | if (!jack) | |
121 | return NULL; | |
122 | jack->nid = nid; | |
123 | jack->jack_dirty = 1; | |
3a93897e | 124 | jack->tag = codec->jacktbl.used; |
1835a0f9 TI |
125 | return jack; |
126 | } | |
127 | ||
128 | void snd_hda_jack_tbl_clear(struct hda_codec *codec) | |
129 | { | |
1a4f69d5 TI |
130 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
131 | int i; | |
132 | ||
133 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { | |
134 | struct hda_jack_callback *cb, *next; | |
2ba2dfa1 | 135 | |
1a4f69d5 TI |
136 | /* free jack instances manually when clearing/reconfiguring */ |
137 | if (!codec->bus->shutdown && jack->jack) | |
6efdd851 | 138 | snd_device_free(codec->card, jack->jack); |
2ba2dfa1 | 139 | |
1a4f69d5 TI |
140 | for (cb = jack->callback; cb; cb = next) { |
141 | next = cb->next; | |
142 | kfree(cb); | |
31ef2257 TI |
143 | } |
144 | } | |
1835a0f9 TI |
145 | snd_array_free(&codec->jacktbl); |
146 | } | |
147 | ||
0619ba8c DR |
148 | #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE) |
149 | ||
1835a0f9 TI |
150 | /* update the cached value and notification flag if needed */ |
151 | static void jack_detect_update(struct hda_codec *codec, | |
152 | struct hda_jack_tbl *jack) | |
153 | { | |
80c8bfbe DH |
154 | if (!jack->jack_dirty) |
155 | return; | |
156 | ||
157 | if (jack->phantom_jack) | |
158 | jack->pin_sense = AC_PINSENSE_PRESENCE; | |
159 | else | |
35be544a | 160 | jack->pin_sense = read_pin_sense(codec, jack->nid); |
80c8bfbe | 161 | |
0619ba8c DR |
162 | /* A gating jack indicates the jack is invalid if gating is unplugged */ |
163 | if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack)) | |
164 | jack->pin_sense &= ~AC_PINSENSE_PRESENCE; | |
165 | ||
80c8bfbe | 166 | jack->jack_dirty = 0; |
0619ba8c DR |
167 | |
168 | /* If a jack is gated by this one update it. */ | |
169 | if (jack->gated_jack) { | |
170 | struct hda_jack_tbl *gated = | |
171 | snd_hda_jack_tbl_get(codec, jack->gated_jack); | |
172 | if (gated) { | |
173 | gated->jack_dirty = 1; | |
174 | jack_detect_update(codec, gated); | |
175 | } | |
176 | } | |
1835a0f9 TI |
177 | } |
178 | ||
179 | /** | |
180 | * snd_hda_set_dirty_all - Mark all the cached as dirty | |
a11e9b16 | 181 | * @codec: the HDA codec |
1835a0f9 TI |
182 | * |
183 | * This function sets the dirty flag to all entries of jack table. | |
184 | * It's called from the resume path in hda_codec.c. | |
185 | */ | |
186 | void snd_hda_jack_set_dirty_all(struct hda_codec *codec) | |
187 | { | |
188 | struct hda_jack_tbl *jack = codec->jacktbl.list; | |
189 | int i; | |
190 | ||
191 | for (i = 0; i < codec->jacktbl.used; i++, jack++) | |
192 | if (jack->nid) | |
193 | jack->jack_dirty = 1; | |
194 | } | |
2698ea98 | 195 | EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all); |
1835a0f9 TI |
196 | |
197 | /** | |
198 | * snd_hda_pin_sense - execute pin sense measurement | |
199 | * @codec: the CODEC to sense | |
200 | * @nid: the pin NID to sense | |
201 | * | |
202 | * Execute necessary pin sense measurement and return its Presence Detect, | |
203 | * Impedance, ELD Valid etc. status bits. | |
204 | */ | |
205 | u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid) | |
206 | { | |
207 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); | |
208 | if (jack) { | |
209 | jack_detect_update(codec, jack); | |
210 | return jack->pin_sense; | |
211 | } | |
212 | return read_pin_sense(codec, nid); | |
213 | } | |
2698ea98 | 214 | EXPORT_SYMBOL_GPL(snd_hda_pin_sense); |
1835a0f9 TI |
215 | |
216 | /** | |
60ea8ca2 | 217 | * snd_hda_jack_detect_state - query pin Presence Detect status |
1835a0f9 TI |
218 | * @codec: the CODEC to sense |
219 | * @nid: the pin NID to sense | |
220 | * | |
60ea8ca2 TI |
221 | * Query and return the pin's Presence Detect status, as either |
222 | * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM. | |
1835a0f9 | 223 | */ |
60ea8ca2 | 224 | int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid) |
1835a0f9 | 225 | { |
60ea8ca2 TI |
226 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
227 | if (jack && jack->phantom_jack) | |
228 | return HDA_JACK_PHANTOM; | |
229 | else if (snd_hda_pin_sense(codec, nid) & AC_PINSENSE_PRESENCE) | |
230 | return HDA_JACK_PRESENT; | |
231 | else | |
232 | return HDA_JACK_NOT_PRESENT; | |
1835a0f9 | 233 | } |
2698ea98 | 234 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state); |
1835a0f9 TI |
235 | |
236 | /** | |
237 | * snd_hda_jack_detect_enable - enable the jack-detection | |
a11e9b16 TI |
238 | * @codec: the HDA codec |
239 | * @nid: pin NID to enable | |
240 | * @func: callback function to register | |
bda17b82 TI |
241 | * |
242 | * In the case of error, the return value will be a pointer embedded with | |
243 | * errno. Check and handle the return value appropriately with standard | |
244 | * macros such as @IS_ERR() and @PTR_ERR(). | |
1835a0f9 | 245 | */ |
1a4f69d5 | 246 | struct hda_jack_callback * |
bda17b82 | 247 | snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid, |
1a4f69d5 | 248 | hda_jack_callback_fn func) |
1835a0f9 | 249 | { |
1a4f69d5 TI |
250 | struct hda_jack_tbl *jack; |
251 | struct hda_jack_callback *callback = NULL; | |
bda17b82 TI |
252 | int err; |
253 | ||
1a4f69d5 | 254 | jack = snd_hda_jack_tbl_new(codec, nid); |
1835a0f9 | 255 | if (!jack) |
bda17b82 | 256 | return ERR_PTR(-ENOMEM); |
1a4f69d5 TI |
257 | if (func) { |
258 | callback = kzalloc(sizeof(*callback), GFP_KERNEL); | |
259 | if (!callback) | |
260 | return ERR_PTR(-ENOMEM); | |
261 | callback->func = func; | |
262 | callback->tbl = jack; | |
263 | callback->next = jack->callback; | |
264 | jack->callback = callback; | |
265 | } | |
266 | ||
3a93897e | 267 | if (jack->jack_detect) |
1a4f69d5 | 268 | return callback; /* already registered */ |
3a93897e | 269 | jack->jack_detect = 1; |
34a93187 | 270 | if (codec->jackpoll_interval > 0) |
1a4f69d5 | 271 | return callback; /* No unsol if we're polling instead */ |
bda17b82 | 272 | err = snd_hda_codec_write_cache(codec, nid, 0, |
1835a0f9 | 273 | AC_VERB_SET_UNSOLICITED_ENABLE, |
3a93897e | 274 | AC_USRSP_EN | jack->tag); |
bda17b82 TI |
275 | if (err < 0) |
276 | return ERR_PTR(err); | |
1a4f69d5 | 277 | return callback; |
1835a0f9 | 278 | } |
2698ea98 | 279 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback); |
954df2a9 | 280 | |
95a962c3 TI |
281 | /** |
282 | * snd_hda_jack_detect_enable - Enable the jack detection on the given pin | |
283 | * @codec: the HDA codec | |
284 | * @nid: pin NID to enable jack detection | |
285 | * | |
286 | * Enable the jack detection with the default callback. Returns zero if | |
287 | * successful or a negative error code. | |
288 | */ | |
62f949bf | 289 | int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid) |
954df2a9 | 290 | { |
bda17b82 | 291 | return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback(codec, nid, NULL)); |
954df2a9 | 292 | } |
2698ea98 | 293 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable); |
01a61e12 | 294 | |
0619ba8c DR |
295 | /** |
296 | * snd_hda_jack_set_gating_jack - Set gating jack. | |
a11e9b16 TI |
297 | * @codec: the HDA codec |
298 | * @gated_nid: gated pin NID | |
299 | * @gating_nid: gating pin NID | |
0619ba8c DR |
300 | * |
301 | * Indicates the gated jack is only valid when the gating jack is plugged. | |
302 | */ | |
303 | int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid, | |
304 | hda_nid_t gating_nid) | |
305 | { | |
bde7bc60 CCC |
306 | struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid); |
307 | struct hda_jack_tbl *gating = snd_hda_jack_tbl_new(codec, gating_nid); | |
0619ba8c DR |
308 | |
309 | if (!gated || !gating) | |
310 | return -EINVAL; | |
311 | ||
312 | gated->gating_jack = gating_nid; | |
313 | gating->gated_jack = gated_nid; | |
314 | ||
315 | return 0; | |
316 | } | |
2698ea98 | 317 | EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack); |
0619ba8c | 318 | |
01a61e12 TI |
319 | /** |
320 | * snd_hda_jack_report_sync - sync the states of all jacks and report if changed | |
a11e9b16 | 321 | * @codec: the HDA codec |
01a61e12 TI |
322 | */ |
323 | void snd_hda_jack_report_sync(struct hda_codec *codec) | |
324 | { | |
0619ba8c | 325 | struct hda_jack_tbl *jack; |
35be544a | 326 | int i, state; |
01a61e12 | 327 | |
0619ba8c DR |
328 | /* update all jacks at first */ |
329 | jack = codec->jacktbl.list; | |
01a61e12 | 330 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
0619ba8c | 331 | if (jack->nid) |
01a61e12 | 332 | jack_detect_update(codec, jack); |
0619ba8c DR |
333 | |
334 | /* report the updated jacks; it's done after updating all jacks | |
335 | * to make sure that all gating jacks properly have been set | |
336 | */ | |
337 | jack = codec->jacktbl.list; | |
338 | for (i = 0; i < codec->jacktbl.used; i++, jack++) | |
339 | if (jack->nid) { | |
2ba2dfa1 | 340 | if (!jack->jack || jack->block_report) |
cfc7c9d3 | 341 | continue; |
35be544a | 342 | state = get_jack_plug_state(jack->pin_sense); |
2ba2dfa1 JY |
343 | snd_jack_report(jack->jack, |
344 | state ? jack->type : 0); | |
01a61e12 TI |
345 | } |
346 | } | |
2698ea98 | 347 | EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync); |
01a61e12 | 348 | |
31ef2257 TI |
349 | /* guess the jack type from the pin-config */ |
350 | static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid) | |
351 | { | |
352 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); | |
353 | switch (get_defcfg_device(def_conf)) { | |
354 | case AC_JACK_LINE_OUT: | |
355 | case AC_JACK_SPEAKER: | |
356 | return SND_JACK_LINEOUT; | |
357 | case AC_JACK_HP_OUT: | |
358 | return SND_JACK_HEADPHONE; | |
359 | case AC_JACK_SPDIF_OUT: | |
360 | case AC_JACK_DIG_OTHER_OUT: | |
361 | return SND_JACK_AVOUT; | |
362 | case AC_JACK_MIC_IN: | |
363 | return SND_JACK_MICROPHONE; | |
364 | default: | |
365 | return SND_JACK_LINEIN; | |
366 | } | |
367 | } | |
368 | ||
369 | static void hda_free_jack_priv(struct snd_jack *jack) | |
370 | { | |
371 | struct hda_jack_tbl *jacks = jack->private_data; | |
372 | jacks->nid = 0; | |
373 | jacks->jack = NULL; | |
374 | } | |
31ef2257 | 375 | |
01a61e12 TI |
376 | /** |
377 | * snd_hda_jack_add_kctl - Add a kctl for the given pin | |
a11e9b16 TI |
378 | * @codec: the HDA codec |
379 | * @nid: pin NID to assign | |
380 | * @name: string name for the jack | |
a11e9b16 | 381 | * @phantom_jack: flag to deal as a phantom jack |
01a61e12 TI |
382 | * |
383 | * This assigns a jack-detection kctl to the given pin. The kcontrol | |
384 | * will have the given name and index. | |
385 | */ | |
80c8bfbe | 386 | static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
2ba2dfa1 | 387 | const char *name, bool phantom_jack) |
01a61e12 TI |
388 | { |
389 | struct hda_jack_tbl *jack; | |
2ba2dfa1 | 390 | int err, state, type; |
01a61e12 | 391 | |
3a93897e | 392 | jack = snd_hda_jack_tbl_new(codec, nid); |
01a61e12 TI |
393 | if (!jack) |
394 | return 0; | |
2ba2dfa1 | 395 | if (jack->jack) |
01a61e12 | 396 | return 0; /* already created */ |
2ba2dfa1 JY |
397 | |
398 | type = get_input_jack_type(codec, nid); | |
399 | err = snd_jack_new(codec->card, name, type, | |
400 | &jack->jack, true, phantom_jack); | |
31ef2257 TI |
401 | if (err < 0) |
402 | return err; | |
80c8bfbe | 403 | |
2ba2dfa1 JY |
404 | jack->phantom_jack = !!phantom_jack; |
405 | jack->type = type; | |
406 | jack->jack->private_data = jack; | |
407 | jack->jack->private_free = hda_free_jack_priv; | |
31ef2257 | 408 | state = snd_hda_jack_detect(codec, nid); |
2ba2dfa1 JY |
409 | snd_jack_report(jack->jack, state ? jack->type : 0); |
410 | ||
01a61e12 TI |
411 | return 0; |
412 | } | |
80c8bfbe | 413 | |
95a962c3 TI |
414 | /** |
415 | * snd_hda_jack_add_kctl - Add a jack kctl for the given pin | |
416 | * @codec: the HDA codec | |
417 | * @nid: pin NID | |
418 | * @name: the name string for the jack ctl | |
95a962c3 TI |
419 | * |
420 | * This is a simple helper calling __snd_hda_jack_add_kctl(). | |
421 | */ | |
80c8bfbe | 422 | int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
2ba2dfa1 | 423 | const char *name) |
80c8bfbe | 424 | { |
2ba2dfa1 | 425 | return __snd_hda_jack_add_kctl(codec, nid, name, false); |
80c8bfbe | 426 | } |
2698ea98 | 427 | EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl); |
01a61e12 | 428 | |
201e06ff | 429 | static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, |
b26b5116 DH |
430 | const struct auto_pin_cfg *cfg, |
431 | const char *base_name) | |
01a61e12 | 432 | { |
3a93897e | 433 | unsigned int def_conf, conn; |
975cc02a | 434 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
2ba2dfa1 | 435 | int err; |
80c8bfbe | 436 | bool phantom_jack; |
3a93897e | 437 | |
01a61e12 TI |
438 | if (!nid) |
439 | return 0; | |
3a93897e TI |
440 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
441 | conn = get_defcfg_connect(def_conf); | |
80c8bfbe | 442 | if (conn == AC_JACK_PORT_NONE) |
3a93897e | 443 | return 0; |
80c8bfbe DH |
444 | phantom_jack = (conn != AC_JACK_PORT_COMPLEX) || |
445 | !is_jack_detectable(codec, nid); | |
3a93897e | 446 | |
2ba2dfa1 | 447 | if (base_name) |
b26b5116 | 448 | strlcpy(name, base_name, sizeof(name)); |
2ba2dfa1 JY |
449 | else |
450 | snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL); | |
80c8bfbe DH |
451 | if (phantom_jack) |
452 | /* Example final name: "Internal Mic Phantom Jack" */ | |
453 | strncat(name, " Phantom", sizeof(name) - strlen(name) - 1); | |
2ba2dfa1 | 454 | err = __snd_hda_jack_add_kctl(codec, nid, name, phantom_jack); |
3a93897e TI |
455 | if (err < 0) |
456 | return err; | |
80c8bfbe DH |
457 | |
458 | if (!phantom_jack) | |
62f949bf | 459 | return snd_hda_jack_detect_enable(codec, nid); |
80c8bfbe | 460 | return 0; |
01a61e12 TI |
461 | } |
462 | ||
463 | /** | |
464 | * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg | |
a11e9b16 TI |
465 | * @codec: the HDA codec |
466 | * @cfg: pin config table to parse | |
01a61e12 TI |
467 | */ |
468 | int snd_hda_jack_add_kctls(struct hda_codec *codec, | |
469 | const struct auto_pin_cfg *cfg) | |
470 | { | |
471 | const hda_nid_t *p; | |
f46c3296 | 472 | int i, err; |
01a61e12 | 473 | |
b26b5116 DH |
474 | for (i = 0; i < cfg->num_inputs; i++) { |
475 | /* If we have headphone mics; make sure they get the right name | |
476 | before grabbed by output pins */ | |
477 | if (cfg->inputs[i].is_headphone_mic) { | |
478 | if (auto_cfg_hp_outs(cfg) == 1) | |
479 | err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0], | |
480 | cfg, "Headphone Mic"); | |
481 | else | |
482 | err = add_jack_kctl(codec, cfg->inputs[i].pin, | |
483 | cfg, "Headphone Mic"); | |
484 | } else | |
485 | err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, | |
486 | NULL); | |
487 | if (err < 0) | |
488 | return err; | |
489 | } | |
490 | ||
01a61e12 | 491 | for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) { |
b26b5116 | 492 | err = add_jack_kctl(codec, *p, cfg, NULL); |
01a61e12 TI |
493 | if (err < 0) |
494 | return err; | |
495 | } | |
496 | for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { | |
497 | if (*p == *cfg->line_out_pins) /* might be duplicated */ | |
498 | break; | |
b26b5116 | 499 | err = add_jack_kctl(codec, *p, cfg, NULL); |
01a61e12 TI |
500 | if (err < 0) |
501 | return err; | |
502 | } | |
503 | for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { | |
504 | if (*p == *cfg->line_out_pins) /* might be duplicated */ | |
505 | break; | |
b26b5116 | 506 | err = add_jack_kctl(codec, *p, cfg, NULL); |
01a61e12 TI |
507 | if (err < 0) |
508 | return err; | |
509 | } | |
510 | for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) { | |
b26b5116 | 511 | err = add_jack_kctl(codec, *p, cfg, NULL); |
01a61e12 TI |
512 | if (err < 0) |
513 | return err; | |
514 | } | |
b26b5116 | 515 | err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL); |
01a61e12 TI |
516 | if (err < 0) |
517 | return err; | |
b26b5116 | 518 | err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL); |
01a61e12 TI |
519 | if (err < 0) |
520 | return err; | |
521 | return 0; | |
522 | } | |
2698ea98 | 523 | EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls); |
954df2a9 | 524 | |
0619ba8c DR |
525 | static void call_jack_callback(struct hda_codec *codec, |
526 | struct hda_jack_tbl *jack) | |
527 | { | |
1a4f69d5 TI |
528 | struct hda_jack_callback *cb; |
529 | ||
530 | for (cb = jack->callback; cb; cb = cb->next) | |
531 | cb->func(codec, cb); | |
0619ba8c DR |
532 | if (jack->gated_jack) { |
533 | struct hda_jack_tbl *gated = | |
534 | snd_hda_jack_tbl_get(codec, jack->gated_jack); | |
1a4f69d5 TI |
535 | if (gated) { |
536 | for (cb = gated->callback; cb; cb = cb->next) | |
537 | cb->func(codec, cb); | |
538 | } | |
0619ba8c DR |
539 | } |
540 | } | |
541 | ||
95a962c3 TI |
542 | /** |
543 | * snd_hda_jack_unsol_event - Handle an unsolicited event | |
544 | * @codec: the HDA codec | |
545 | * @res: the unsolicited event data | |
546 | */ | |
954df2a9 DH |
547 | void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res) |
548 | { | |
549 | struct hda_jack_tbl *event; | |
550 | int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f; | |
551 | ||
552 | event = snd_hda_jack_tbl_get_from_tag(codec, tag); | |
553 | if (!event) | |
554 | return; | |
555 | event->jack_dirty = 1; | |
556 | ||
0619ba8c | 557 | call_jack_callback(codec, event); |
954df2a9 DH |
558 | snd_hda_jack_report_sync(codec); |
559 | } | |
2698ea98 | 560 | EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event); |
954df2a9 | 561 | |
95a962c3 TI |
562 | /** |
563 | * snd_hda_jack_poll_all - Poll all jacks | |
564 | * @codec: the HDA codec | |
565 | * | |
566 | * Poll all detectable jacks with dirty flag, update the status, call | |
567 | * callbacks and call snd_hda_jack_report_sync() if any changes are found. | |
568 | */ | |
26a6cb6c DH |
569 | void snd_hda_jack_poll_all(struct hda_codec *codec) |
570 | { | |
571 | struct hda_jack_tbl *jack = codec->jacktbl.list; | |
572 | int i, changes = 0; | |
573 | ||
574 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { | |
575 | unsigned int old_sense; | |
576 | if (!jack->nid || !jack->jack_dirty || jack->phantom_jack) | |
577 | continue; | |
578 | old_sense = get_jack_plug_state(jack->pin_sense); | |
579 | jack_detect_update(codec, jack); | |
580 | if (old_sense == get_jack_plug_state(jack->pin_sense)) | |
581 | continue; | |
582 | changes = 1; | |
0619ba8c | 583 | call_jack_callback(codec, jack); |
26a6cb6c DH |
584 | } |
585 | if (changes) | |
586 | snd_hda_jack_report_sync(codec); | |
587 | } | |
2698ea98 | 588 | EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all); |
26a6cb6c | 589 |