blob: 6e9f57bbe6673d3c01371783428287956f8814eb [file] [log] [blame]
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001/*
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>
Takashi Iwaibf815bf2011-11-16 14:28:33 +010014#include <linux/export.h>
Takashi Iwai1835a0f2011-10-27 22:12:46 +020015#include <sound/core.h>
Takashi Iwai01a61e12011-10-28 00:03:22 +020016#include <sound/control.h>
Takashi Iwaiaad37db2011-11-02 08:54:51 +010017#include <sound/jack.h>
Takashi Iwai1835a0f2011-10-27 22:12:46 +020018#include "hda_codec.h"
19#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020020#include "hda_auto_parser.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020021#include "hda_jack.h"
22
Takashi Iwaia9c74172012-02-13 11:41:42 +010023bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
24{
Takashi Iwai71b1e9e2012-02-13 11:55:02 +010025 if (codec->no_jack_detect)
26 return false;
Takashi Iwaia9c74172012-02-13 11:41:42 +010027 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
28 return false;
David Henningsson5fe8e1e2012-09-07 07:25:44 +020029 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
30 AC_DEFCFG_MISC_NO_PRESENCE)
Takashi Iwaia9c74172012-02-13 11:41:42 +010031 return false;
32 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP))
33 return false;
34 return true;
35}
36EXPORT_SYMBOL_HDA(is_jack_detectable);
37
Takashi Iwai1835a0f2011-10-27 22:12:46 +020038/* execute pin sense measurement */
39static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
40{
41 u32 pincap;
42
43 if (!codec->no_trigger_sense) {
44 pincap = snd_hda_query_pin_caps(codec, nid);
45 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
46 snd_hda_codec_read(codec, nid, 0,
47 AC_VERB_SET_PIN_SENSE, 0);
48 }
49 return snd_hda_codec_read(codec, nid, 0,
50 AC_VERB_GET_PIN_SENSE, 0);
51}
52
53/**
54 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
55 */
56struct hda_jack_tbl *
57snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
58{
59 struct hda_jack_tbl *jack = codec->jacktbl.list;
60 int i;
61
62 if (!nid || !jack)
63 return NULL;
64 for (i = 0; i < codec->jacktbl.used; i++, jack++)
65 if (jack->nid == nid)
66 return jack;
67 return NULL;
68}
69EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
70
71/**
Takashi Iwai3a938972011-10-28 01:16:55 +020072 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
73 */
74struct hda_jack_tbl *
75snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
76{
77 struct hda_jack_tbl *jack = codec->jacktbl.list;
78 int i;
79
80 if (!tag || !jack)
81 return NULL;
82 for (i = 0; i < codec->jacktbl.used; i++, jack++)
83 if (jack->tag == tag)
84 return jack;
85 return NULL;
86}
87EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
88
89/**
Takashi Iwai1835a0f2011-10-27 22:12:46 +020090 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
91 */
92struct hda_jack_tbl *
93snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
94{
95 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
96 if (jack)
97 return jack;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020098 jack = snd_array_new(&codec->jacktbl);
99 if (!jack)
100 return NULL;
101 jack->nid = nid;
102 jack->jack_dirty = 1;
Takashi Iwai3a938972011-10-28 01:16:55 +0200103 jack->tag = codec->jacktbl.used;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200104 return jack;
105}
David Henningssond1cb6202011-11-11 17:13:15 +0100106EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200107
108void snd_hda_jack_tbl_clear(struct hda_codec *codec)
109{
Takashi Iwai31ef2252011-12-01 17:41:36 +0100110#ifdef CONFIG_SND_HDA_INPUT_JACK
111 /* free jack instances manually when clearing/reconfiguring */
112 if (!codec->bus->shutdown && codec->jacktbl.list) {
113 struct hda_jack_tbl *jack = codec->jacktbl.list;
114 int i;
115 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
116 if (jack->jack)
117 snd_device_free(codec->bus->card, jack->jack);
118 }
119 }
120#endif
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200121 snd_array_free(&codec->jacktbl);
122}
123
Dylan Reid0619ba82012-11-19 19:48:07 +0100124#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
125
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200126/* 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{
David Henningsson80c8bfb2012-06-04 09:33:51 +0200130 if (!jack->jack_dirty)
131 return;
132
133 if (jack->phantom_jack)
134 jack->pin_sense = AC_PINSENSE_PRESENCE;
135 else
Takashi Iwai35be5442011-11-02 08:36:06 +0100136 jack->pin_sense = read_pin_sense(codec, jack->nid);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200137
Dylan Reid0619ba82012-11-19 19:48:07 +0100138 /* A gating jack indicates the jack is invalid if gating is unplugged */
139 if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack))
140 jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
141
David Henningsson80c8bfb2012-06-04 09:33:51 +0200142 jack->jack_dirty = 0;
Dylan Reid0619ba82012-11-19 19:48:07 +0100143
144 /* If a jack is gated by this one update it. */
145 if (jack->gated_jack) {
146 struct hda_jack_tbl *gated =
147 snd_hda_jack_tbl_get(codec, jack->gated_jack);
148 if (gated) {
149 gated->jack_dirty = 1;
150 jack_detect_update(codec, gated);
151 }
152 }
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200153}
154
155/**
156 * snd_hda_set_dirty_all - Mark all the cached as dirty
157 *
158 * This function sets the dirty flag to all entries of jack table.
159 * It's called from the resume path in hda_codec.c.
160 */
161void snd_hda_jack_set_dirty_all(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 if (jack->nid)
168 jack->jack_dirty = 1;
169}
170EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
171
172/**
173 * snd_hda_pin_sense - execute pin sense measurement
174 * @codec: the CODEC to sense
175 * @nid: the pin NID to sense
176 *
177 * Execute necessary pin sense measurement and return its Presence Detect,
178 * Impedance, ELD Valid etc. status bits.
179 */
180u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
181{
182 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
183 if (jack) {
184 jack_detect_update(codec, jack);
185 return jack->pin_sense;
186 }
187 return read_pin_sense(codec, nid);
188}
189EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
190
191/**
192 * snd_hda_jack_detect - query pin Presence Detect status
193 * @codec: the CODEC to sense
194 * @nid: the pin NID to sense
195 *
196 * Query and return the pin's Presence Detect status.
197 */
198int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
199{
200 u32 sense = snd_hda_pin_sense(codec, nid);
Takashi Iwai35be5442011-11-02 08:36:06 +0100201 return get_jack_plug_state(sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200202}
203EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
204
205/**
206 * snd_hda_jack_detect_enable - enable the jack-detection
207 */
David Henningsson954df2a2012-09-25 11:30:59 +0200208int snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
209 unsigned char action,
210 hda_jack_callback cb)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200211{
212 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
213 if (!jack)
214 return -ENOMEM;
Takashi Iwai3a938972011-10-28 01:16:55 +0200215 if (jack->jack_detect)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200216 return 0; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200217 jack->jack_detect = 1;
218 if (action)
219 jack->action = action;
David Henningsson954df2a2012-09-25 11:30:59 +0200220 if (cb)
221 jack->callback = cb;
David Henningsson34a93182012-10-17 14:45:05 +0200222 if (codec->jackpoll_interval > 0)
223 return 0; /* No unsol if we're polling instead */
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200224 return snd_hda_codec_write_cache(codec, nid, 0,
225 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200226 AC_USRSP_EN | jack->tag);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200227}
David Henningsson954df2a2012-09-25 11:30:59 +0200228EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable_callback);
229
230int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
231 unsigned char action)
232{
233 return snd_hda_jack_detect_enable_callback(codec, nid, action, NULL);
234}
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200235EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200236
Takashi Iwai01a61e12011-10-28 00:03:22 +0200237/**
Dylan Reid0619ba82012-11-19 19:48:07 +0100238 * snd_hda_jack_set_gating_jack - Set gating jack.
239 *
240 * Indicates the gated jack is only valid when the gating jack is plugged.
241 */
242int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
243 hda_nid_t gating_nid)
244{
245 struct hda_jack_tbl *gated = snd_hda_jack_tbl_get(codec, gated_nid);
246 struct hda_jack_tbl *gating = snd_hda_jack_tbl_get(codec, gating_nid);
247
248 if (!gated || !gating)
249 return -EINVAL;
250
251 gated->gating_jack = gating_nid;
252 gating->gated_jack = gated_nid;
253
254 return 0;
255}
256EXPORT_SYMBOL_HDA(snd_hda_jack_set_gating_jack);
257
258/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200259 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
260 */
261void snd_hda_jack_report_sync(struct hda_codec *codec)
262{
Dylan Reid0619ba82012-11-19 19:48:07 +0100263 struct hda_jack_tbl *jack;
Takashi Iwai35be5442011-11-02 08:36:06 +0100264 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200265
Dylan Reid0619ba82012-11-19 19:48:07 +0100266 /* update all jacks at first */
267 jack = codec->jacktbl.list;
268 for (i = 0; i < codec->jacktbl.used; i++, jack++)
269 if (jack->nid)
270 jack_detect_update(codec, jack);
271
272 /* report the updated jacks; it's done after updating all jacks
273 * to make sure that all gating jacks properly have been set
274 */
275 jack = codec->jacktbl.list;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200276 for (i = 0; i < codec->jacktbl.used; i++, jack++)
277 if (jack->nid) {
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100278 if (!jack->kctl)
279 continue;
Takashi Iwai35be5442011-11-02 08:36:06 +0100280 state = get_jack_plug_state(jack->pin_sense);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100281 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
Takashi Iwai31ef2252011-12-01 17:41:36 +0100282#ifdef CONFIG_SND_HDA_INPUT_JACK
283 if (jack->jack)
284 snd_jack_report(jack->jack,
285 state ? jack->type : 0);
286#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200287 }
288}
289EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
290
Takashi Iwai31ef2252011-12-01 17:41:36 +0100291#ifdef CONFIG_SND_HDA_INPUT_JACK
292/* guess the jack type from the pin-config */
293static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
294{
295 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
296 switch (get_defcfg_device(def_conf)) {
297 case AC_JACK_LINE_OUT:
298 case AC_JACK_SPEAKER:
299 return SND_JACK_LINEOUT;
300 case AC_JACK_HP_OUT:
301 return SND_JACK_HEADPHONE;
302 case AC_JACK_SPDIF_OUT:
303 case AC_JACK_DIG_OTHER_OUT:
304 return SND_JACK_AVOUT;
305 case AC_JACK_MIC_IN:
306 return SND_JACK_MICROPHONE;
307 default:
308 return SND_JACK_LINEIN;
309 }
310}
311
312static void hda_free_jack_priv(struct snd_jack *jack)
313{
314 struct hda_jack_tbl *jacks = jack->private_data;
315 jacks->nid = 0;
316 jacks->jack = NULL;
317}
318#endif
319
Takashi Iwai01a61e12011-10-28 00:03:22 +0200320/**
321 * snd_hda_jack_add_kctl - Add a kctl for the given pin
322 *
323 * This assigns a jack-detection kctl to the given pin. The kcontrol
324 * will have the given name and index.
325 */
David Henningsson80c8bfb2012-06-04 09:33:51 +0200326static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
327 const char *name, int idx, bool phantom_jack)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200328{
329 struct hda_jack_tbl *jack;
330 struct snd_kcontrol *kctl;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100331 int err, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200332
Takashi Iwai3a938972011-10-28 01:16:55 +0200333 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200334 if (!jack)
335 return 0;
336 if (jack->kctl)
337 return 0; /* already created */
Takashi Iwai35be5442011-11-02 08:36:06 +0100338 kctl = snd_kctl_jack_new(name, idx, codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200339 if (!kctl)
340 return -ENOMEM;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100341 err = snd_hda_ctl_add(codec, nid, kctl);
342 if (err < 0)
343 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200344 jack->kctl = kctl;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200345 jack->phantom_jack = !!phantom_jack;
346
Takashi Iwai31ef2252011-12-01 17:41:36 +0100347 state = snd_hda_jack_detect(codec, nid);
348 snd_kctl_jack_report(codec->bus->card, kctl, state);
349#ifdef CONFIG_SND_HDA_INPUT_JACK
David Henningsson80c8bfb2012-06-04 09:33:51 +0200350 if (!phantom_jack) {
351 jack->type = get_input_jack_type(codec, nid);
352 err = snd_jack_new(codec->bus->card, name, jack->type,
353 &jack->jack);
354 if (err < 0)
355 return err;
356 jack->jack->private_data = jack;
357 jack->jack->private_free = hda_free_jack_priv;
358 snd_jack_report(jack->jack, state ? jack->type : 0);
359 }
Takashi Iwai31ef2252011-12-01 17:41:36 +0100360#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200361 return 0;
362}
David Henningsson80c8bfb2012-06-04 09:33:51 +0200363
364int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
365 const char *name, int idx)
366{
367 return __snd_hda_jack_add_kctl(codec, nid, name, idx, false);
368}
David Henningssond1cb6202011-11-11 17:13:15 +0100369EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200370
Takashi Iwaif46c3292012-07-17 16:48:27 +0200371/* get the unique index number for the given kctl name */
372static int get_unique_index(struct hda_codec *codec, const char *name, int idx)
373{
374 struct hda_jack_tbl *jack;
375 int i, len = strlen(name);
376 again:
377 jack = codec->jacktbl.list;
378 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
379 /* jack->kctl.id contains "XXX Jack" name string with index */
380 if (jack->kctl &&
381 !strncmp(name, jack->kctl->id.name, len) &&
382 !strcmp(" Jack", jack->kctl->id.name + len) &&
383 jack->kctl->id.index == idx) {
384 idx++;
385 goto again;
386 }
387 }
388 return idx;
389}
390
Takashi Iwai201e06f2011-11-16 15:33:26 +0100391static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaif46c3292012-07-17 16:48:27 +0200392 const struct auto_pin_cfg *cfg)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200393{
Takashi Iwai3a938972011-10-28 01:16:55 +0200394 unsigned int def_conf, conn;
Takashi Iwai201e06f2011-11-16 15:33:26 +0100395 char name[44];
396 int idx, err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200397 bool phantom_jack;
Takashi Iwai3a938972011-10-28 01:16:55 +0200398
Takashi Iwai01a61e12011-10-28 00:03:22 +0200399 if (!nid)
400 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200401 def_conf = snd_hda_codec_get_pincfg(codec, nid);
402 conn = get_defcfg_connect(def_conf);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200403 if (conn == AC_JACK_PORT_NONE)
Takashi Iwai3a938972011-10-28 01:16:55 +0200404 return 0;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200405 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
406 !is_jack_detectable(codec, nid);
Takashi Iwai3a938972011-10-28 01:16:55 +0200407
Takashi Iwai201e06f2011-11-16 15:33:26 +0100408 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200409 if (phantom_jack)
410 /* Example final name: "Internal Mic Phantom Jack" */
411 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
Takashi Iwaif46c3292012-07-17 16:48:27 +0200412 idx = get_unique_index(codec, name, idx);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200413 err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack);
Takashi Iwai3a938972011-10-28 01:16:55 +0200414 if (err < 0)
415 return err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200416
417 if (!phantom_jack)
418 return snd_hda_jack_detect_enable(codec, nid, 0);
419 return 0;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200420}
421
422/**
423 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwai01a61e12011-10-28 00:03:22 +0200424 */
425int snd_hda_jack_add_kctls(struct hda_codec *codec,
426 const struct auto_pin_cfg *cfg)
427{
428 const hda_nid_t *p;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200429 int i, err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200430
431 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
Takashi Iwaif46c3292012-07-17 16:48:27 +0200432 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200433 if (err < 0)
434 return err;
435 }
436 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
437 if (*p == *cfg->line_out_pins) /* might be duplicated */
438 break;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200439 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200440 if (err < 0)
441 return err;
442 }
443 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
444 if (*p == *cfg->line_out_pins) /* might be duplicated */
445 break;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200446 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200447 if (err < 0)
448 return err;
449 }
450 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaif46c3292012-07-17 16:48:27 +0200451 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200452 if (err < 0)
453 return err;
454 }
455 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
Takashi Iwaif46c3292012-07-17 16:48:27 +0200456 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200457 if (err < 0)
458 return err;
459 }
Takashi Iwaif46c3292012-07-17 16:48:27 +0200460 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200461 if (err < 0)
462 return err;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200463 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200464 if (err < 0)
465 return err;
466 return 0;
467}
468EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);
David Henningsson954df2a2012-09-25 11:30:59 +0200469
Dylan Reid0619ba82012-11-19 19:48:07 +0100470static void call_jack_callback(struct hda_codec *codec,
471 struct hda_jack_tbl *jack)
472{
473 if (jack->callback)
474 jack->callback(codec, jack);
475 if (jack->gated_jack) {
476 struct hda_jack_tbl *gated =
477 snd_hda_jack_tbl_get(codec, jack->gated_jack);
478 if (gated && gated->callback)
479 gated->callback(codec, gated);
480 }
481}
482
David Henningsson954df2a2012-09-25 11:30:59 +0200483void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
484{
485 struct hda_jack_tbl *event;
486 int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f;
487
488 event = snd_hda_jack_tbl_get_from_tag(codec, tag);
489 if (!event)
490 return;
491 event->jack_dirty = 1;
492
Dylan Reid0619ba82012-11-19 19:48:07 +0100493 call_jack_callback(codec, event);
David Henningsson954df2a2012-09-25 11:30:59 +0200494 snd_hda_jack_report_sync(codec);
495}
496EXPORT_SYMBOL_HDA(snd_hda_jack_unsol_event);
497
David Henningsson26a6cb62012-10-09 15:04:21 +0200498void snd_hda_jack_poll_all(struct hda_codec *codec)
499{
500 struct hda_jack_tbl *jack = codec->jacktbl.list;
501 int i, changes = 0;
502
503 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
504 unsigned int old_sense;
505 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
506 continue;
507 old_sense = get_jack_plug_state(jack->pin_sense);
508 jack_detect_update(codec, jack);
509 if (old_sense == get_jack_plug_state(jack->pin_sense))
510 continue;
511 changes = 1;
Dylan Reid0619ba82012-11-19 19:48:07 +0100512 call_jack_callback(codec, jack);
David Henningsson26a6cb62012-10-09 15:04:21 +0200513 }
514 if (changes)
515 snd_hda_jack_report_sync(codec);
516}
517EXPORT_SYMBOL_HDA(snd_hda_jack_poll_all);
518