blob: c945e257d368890bcd3f72037568e566c0ba9221 [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 Iwai95a962c2014-10-29 16:03:58 +010023/**
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 */
Takashi Iwaia9c74172012-02-13 11:41:42 +010033bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
34{
Takashi Iwai71b1e9e2012-02-13 11:55:02 +010035 if (codec->no_jack_detect)
36 return false;
Takashi Iwaia9c74172012-02-13 11:41:42 +010037 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
38 return false;
David Henningsson5fe8e1e2012-09-07 07:25:44 +020039 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
40 AC_DEFCFG_MISC_NO_PRESENCE)
Takashi Iwaia9c74172012-02-13 11:41:42 +010041 return false;
Takashi Iwai76a19c62013-01-07 12:11:33 +010042 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) &&
43 !codec->jackpoll_interval)
Takashi Iwaia9c74172012-02-13 11:41:42 +010044 return false;
45 return true;
46}
Takashi Iwai2698ea92013-12-18 07:45:52 +010047EXPORT_SYMBOL_GPL(is_jack_detectable);
Takashi Iwaia9c74172012-02-13 11:41:42 +010048
Takashi Iwai1835a0f2011-10-27 22:12:46 +020049/* execute pin sense measurement */
50static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
51{
52 u32 pincap;
Takashi Iwai9cc159c2012-12-21 15:31:41 +010053 u32 val;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020054
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 }
Takashi Iwai9cc159c2012-12-21 15:31:41 +010061 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai1835a0f2011-10-27 22:12:46 +020062 AC_VERB_GET_PIN_SENSE, 0);
Takashi Iwai9cc159c2012-12-21 15:31:41 +010063 if (codec->inv_jack_detect)
64 val ^= AC_PINSENSE_PRESENCE;
65 return val;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020066}
67
68/**
69 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
Takashi Iwaia11e9b12014-10-29 15:06:01 +010070 * @codec: the HDA codec
71 * @nid: pin NID to refer to
Takashi Iwai1835a0f2011-10-27 22:12:46 +020072 */
73struct hda_jack_tbl *
74snd_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}
Takashi Iwai2698ea92013-12-18 07:45:52 +010086EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get);
Takashi Iwai1835a0f2011-10-27 22:12:46 +020087
88/**
Takashi Iwai3a938972011-10-28 01:16:55 +020089 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
Takashi Iwaia11e9b12014-10-29 15:06:01 +010090 * @codec: the HDA codec
91 * @tag: tag value to refer to
Takashi Iwai3a938972011-10-28 01:16:55 +020092 */
93struct hda_jack_tbl *
94snd_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}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100106EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
Takashi Iwai3a938972011-10-28 01:16:55 +0200107
108/**
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200109 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100110 * @codec: the HDA codec
111 * @nid: pin NID to assign
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200112 */
Takashi Iwai81965f12014-09-11 14:22:03 +0200113static struct hda_jack_tbl *
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200114snd_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;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200119 jack = snd_array_new(&codec->jacktbl);
120 if (!jack)
121 return NULL;
122 jack->nid = nid;
123 jack->jack_dirty = 1;
Takashi Iwai3a938972011-10-28 01:16:55 +0200124 jack->tag = codec->jacktbl.used;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200125 return jack;
126}
127
128void snd_hda_jack_tbl_clear(struct hda_codec *codec)
129{
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200130 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;
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800135
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200136 /* free jack instances manually when clearing/reconfiguring */
137 if (!codec->bus->shutdown && jack->jack)
Takashi Iwai6efdd852015-02-27 16:09:22 +0100138 snd_device_free(codec->card, jack->jack);
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800139
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200140 for (cb = jack->callback; cb; cb = next) {
141 next = cb->next;
142 kfree(cb);
Takashi Iwai31ef2252011-12-01 17:41:36 +0100143 }
144 }
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200145 snd_array_free(&codec->jacktbl);
146}
147
Dylan Reid0619ba82012-11-19 19:48:07 +0100148#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
149
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200150/* update the cached value and notification flag if needed */
151static void jack_detect_update(struct hda_codec *codec,
152 struct hda_jack_tbl *jack)
153{
David Henningsson80c8bfb2012-06-04 09:33:51 +0200154 if (!jack->jack_dirty)
155 return;
156
157 if (jack->phantom_jack)
158 jack->pin_sense = AC_PINSENSE_PRESENCE;
159 else
Takashi Iwai35be5442011-11-02 08:36:06 +0100160 jack->pin_sense = read_pin_sense(codec, jack->nid);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200161
Dylan Reid0619ba82012-11-19 19:48:07 +0100162 /* 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
David Henningsson80c8bfb2012-06-04 09:33:51 +0200166 jack->jack_dirty = 0;
Dylan Reid0619ba82012-11-19 19:48:07 +0100167
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 }
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200177}
178
179/**
180 * snd_hda_set_dirty_all - Mark all the cached as dirty
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100181 * @codec: the HDA codec
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200182 *
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 */
186void 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}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100195EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200196
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 */
205u32 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}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100214EXPORT_SYMBOL_GPL(snd_hda_pin_sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200215
216/**
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200217 * snd_hda_jack_detect_state - query pin Presence Detect status
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200218 * @codec: the CODEC to sense
219 * @nid: the pin NID to sense
220 *
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200221 * Query and return the pin's Presence Detect status, as either
222 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200223 */
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200224int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200225{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200226 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;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200233}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100234EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200235
236/**
237 * snd_hda_jack_detect_enable - enable the jack-detection
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100238 * @codec: the HDA codec
239 * @nid: pin NID to enable
240 * @func: callback function to register
Takashi Iwaibda17b822014-09-11 14:39:09 +0200241 *
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().
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200245 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200246struct hda_jack_callback *
Takashi Iwaibda17b822014-09-11 14:39:09 +0200247snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200248 hda_jack_callback_fn func)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200249{
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200250 struct hda_jack_tbl *jack;
251 struct hda_jack_callback *callback = NULL;
Takashi Iwaibda17b822014-09-11 14:39:09 +0200252 int err;
253
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200254 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200255 if (!jack)
Takashi Iwaibda17b822014-09-11 14:39:09 +0200256 return ERR_PTR(-ENOMEM);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200257 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
Takashi Iwai3a938972011-10-28 01:16:55 +0200267 if (jack->jack_detect)
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200268 return callback; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200269 jack->jack_detect = 1;
David Henningsson34a93182012-10-17 14:45:05 +0200270 if (codec->jackpoll_interval > 0)
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200271 return callback; /* No unsol if we're polling instead */
Takashi Iwaibda17b822014-09-11 14:39:09 +0200272 err = snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200273 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200274 AC_USRSP_EN | jack->tag);
Takashi Iwaibda17b822014-09-11 14:39:09 +0200275 if (err < 0)
276 return ERR_PTR(err);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200277 return callback;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200278}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100279EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback);
David Henningsson954df2a2012-09-25 11:30:59 +0200280
Takashi Iwai95a962c2014-10-29 16:03:58 +0100281/**
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 */
Takashi Iwai62f949b2014-09-11 14:06:53 +0200289int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid)
David Henningsson954df2a2012-09-25 11:30:59 +0200290{
Takashi Iwaibda17b822014-09-11 14:39:09 +0200291 return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback(codec, nid, NULL));
David Henningsson954df2a2012-09-25 11:30:59 +0200292}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100293EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200294
Takashi Iwai01a61e12011-10-28 00:03:22 +0200295/**
Dylan Reid0619ba82012-11-19 19:48:07 +0100296 * snd_hda_jack_set_gating_jack - Set gating jack.
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100297 * @codec: the HDA codec
298 * @gated_nid: gated pin NID
299 * @gating_nid: gating pin NID
Dylan Reid0619ba82012-11-19 19:48:07 +0100300 *
301 * Indicates the gated jack is only valid when the gating jack is plugged.
302 */
303int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
304 hda_nid_t gating_nid)
305{
Chih-Chung Changbde7bc62013-08-05 16:38:42 +0800306 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);
Dylan Reid0619ba82012-11-19 19:48:07 +0100308
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}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100317EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
Dylan Reid0619ba82012-11-19 19:48:07 +0100318
319/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200320 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100321 * @codec: the HDA codec
Takashi Iwai01a61e12011-10-28 00:03:22 +0200322 */
323void snd_hda_jack_report_sync(struct hda_codec *codec)
324{
Dylan Reid0619ba82012-11-19 19:48:07 +0100325 struct hda_jack_tbl *jack;
Takashi Iwai35be5442011-11-02 08:36:06 +0100326 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200327
Dylan Reid0619ba82012-11-19 19:48:07 +0100328 /* update all jacks at first */
329 jack = codec->jacktbl.list;
330 for (i = 0; i < codec->jacktbl.used; i++, jack++)
331 if (jack->nid)
332 jack_detect_update(codec, jack);
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;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200338 for (i = 0; i < codec->jacktbl.used; i++, jack++)
339 if (jack->nid) {
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800340 if (!jack->jack || jack->block_report)
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100341 continue;
Takashi Iwai35be5442011-11-02 08:36:06 +0100342 state = get_jack_plug_state(jack->pin_sense);
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800343 snd_jack_report(jack->jack,
344 state ? jack->type : 0);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200345 }
346}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100347EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200348
Takashi Iwai31ef2252011-12-01 17:41:36 +0100349/* guess the jack type from the pin-config */
350static 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
369static 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}
Takashi Iwai31ef2252011-12-01 17:41:36 +0100375
Takashi Iwai01a61e12011-10-28 00:03:22 +0200376/**
377 * snd_hda_jack_add_kctl - Add a kctl for the given pin
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100378 * @codec: the HDA codec
379 * @nid: pin NID to assign
380 * @name: string name for the jack
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100381 * @phantom_jack: flag to deal as a phantom jack
Takashi Iwai01a61e12011-10-28 00:03:22 +0200382 *
383 * This assigns a jack-detection kctl to the given pin. The kcontrol
384 * will have the given name and index.
385 */
Takashi Iwai909cadc2015-11-12 11:52:13 +0100386int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800387 const char *name, bool phantom_jack)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200388{
389 struct hda_jack_tbl *jack;
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800390 int err, state, type;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200391
Takashi Iwai3a938972011-10-28 01:16:55 +0200392 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200393 if (!jack)
394 return 0;
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800395 if (jack->jack)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200396 return 0; /* already created */
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800397
398 type = get_input_jack_type(codec, nid);
399 err = snd_jack_new(codec->card, name, type,
400 &jack->jack, true, phantom_jack);
Takashi Iwai31ef2252011-12-01 17:41:36 +0100401 if (err < 0)
402 return err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200403
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800404 jack->phantom_jack = !!phantom_jack;
405 jack->type = type;
406 jack->jack->private_data = jack;
407 jack->jack->private_free = hda_free_jack_priv;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100408 state = snd_hda_jack_detect(codec, nid);
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800409 snd_jack_report(jack->jack, state ? jack->type : 0);
410
Takashi Iwai01a61e12011-10-28 00:03:22 +0200411 return 0;
412}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100413EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200414
Takashi Iwai201e06f2011-11-16 15:33:26 +0100415static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
David Henningssonb26b5112013-04-11 11:30:30 +0200416 const struct auto_pin_cfg *cfg,
417 const char *base_name)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200418{
Takashi Iwai3a938972011-10-28 01:16:55 +0200419 unsigned int def_conf, conn;
Takashi Iwai975cc022013-06-28 11:56:49 +0200420 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800421 int err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200422 bool phantom_jack;
Takashi Iwai3a938972011-10-28 01:16:55 +0200423
Takashi Iwai01a61e12011-10-28 00:03:22 +0200424 if (!nid)
425 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200426 def_conf = snd_hda_codec_get_pincfg(codec, nid);
427 conn = get_defcfg_connect(def_conf);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200428 if (conn == AC_JACK_PORT_NONE)
Takashi Iwai3a938972011-10-28 01:16:55 +0200429 return 0;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200430 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
431 !is_jack_detectable(codec, nid);
Takashi Iwai3a938972011-10-28 01:16:55 +0200432
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800433 if (base_name)
David Henningssonb26b5112013-04-11 11:30:30 +0200434 strlcpy(name, base_name, sizeof(name));
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800435 else
436 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200437 if (phantom_jack)
438 /* Example final name: "Internal Mic Phantom Jack" */
439 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
Takashi Iwai909cadc2015-11-12 11:52:13 +0100440 err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack);
Takashi Iwai3a938972011-10-28 01:16:55 +0200441 if (err < 0)
442 return err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200443
444 if (!phantom_jack)
Takashi Iwai62f949b2014-09-11 14:06:53 +0200445 return snd_hda_jack_detect_enable(codec, nid);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200446 return 0;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200447}
448
449/**
450 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100451 * @codec: the HDA codec
452 * @cfg: pin config table to parse
Takashi Iwai01a61e12011-10-28 00:03:22 +0200453 */
454int snd_hda_jack_add_kctls(struct hda_codec *codec,
455 const struct auto_pin_cfg *cfg)
456{
457 const hda_nid_t *p;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200458 int i, err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200459
David Henningssonb26b5112013-04-11 11:30:30 +0200460 for (i = 0; i < cfg->num_inputs; i++) {
461 /* If we have headphone mics; make sure they get the right name
462 before grabbed by output pins */
463 if (cfg->inputs[i].is_headphone_mic) {
464 if (auto_cfg_hp_outs(cfg) == 1)
465 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
466 cfg, "Headphone Mic");
467 else
468 err = add_jack_kctl(codec, cfg->inputs[i].pin,
469 cfg, "Headphone Mic");
470 } else
471 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
472 NULL);
473 if (err < 0)
474 return err;
475 }
476
Takashi Iwai01a61e12011-10-28 00:03:22 +0200477 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
David Henningssonb26b5112013-04-11 11:30:30 +0200478 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200479 if (err < 0)
480 return err;
481 }
482 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
483 if (*p == *cfg->line_out_pins) /* might be duplicated */
484 break;
David Henningssonb26b5112013-04-11 11:30:30 +0200485 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200486 if (err < 0)
487 return err;
488 }
489 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
490 if (*p == *cfg->line_out_pins) /* might be duplicated */
491 break;
David Henningssonb26b5112013-04-11 11:30:30 +0200492 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200493 if (err < 0)
494 return err;
495 }
496 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
David Henningssonb26b5112013-04-11 11:30:30 +0200497 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200498 if (err < 0)
499 return err;
500 }
David Henningssonb26b5112013-04-11 11:30:30 +0200501 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200502 if (err < 0)
503 return err;
David Henningssonb26b5112013-04-11 11:30:30 +0200504 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200505 if (err < 0)
506 return err;
507 return 0;
508}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100509EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
David Henningsson954df2a2012-09-25 11:30:59 +0200510
Dylan Reid0619ba82012-11-19 19:48:07 +0100511static void call_jack_callback(struct hda_codec *codec,
512 struct hda_jack_tbl *jack)
513{
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200514 struct hda_jack_callback *cb;
515
516 for (cb = jack->callback; cb; cb = cb->next)
517 cb->func(codec, cb);
Dylan Reid0619ba82012-11-19 19:48:07 +0100518 if (jack->gated_jack) {
519 struct hda_jack_tbl *gated =
520 snd_hda_jack_tbl_get(codec, jack->gated_jack);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200521 if (gated) {
522 for (cb = gated->callback; cb; cb = cb->next)
523 cb->func(codec, cb);
524 }
Dylan Reid0619ba82012-11-19 19:48:07 +0100525 }
526}
527
Takashi Iwai95a962c2014-10-29 16:03:58 +0100528/**
529 * snd_hda_jack_unsol_event - Handle an unsolicited event
530 * @codec: the HDA codec
531 * @res: the unsolicited event data
532 */
David Henningsson954df2a2012-09-25 11:30:59 +0200533void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
534{
535 struct hda_jack_tbl *event;
536 int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f;
537
538 event = snd_hda_jack_tbl_get_from_tag(codec, tag);
539 if (!event)
540 return;
541 event->jack_dirty = 1;
542
Dylan Reid0619ba82012-11-19 19:48:07 +0100543 call_jack_callback(codec, event);
David Henningsson954df2a2012-09-25 11:30:59 +0200544 snd_hda_jack_report_sync(codec);
545}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100546EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
David Henningsson954df2a2012-09-25 11:30:59 +0200547
Takashi Iwai95a962c2014-10-29 16:03:58 +0100548/**
549 * snd_hda_jack_poll_all - Poll all jacks
550 * @codec: the HDA codec
551 *
552 * Poll all detectable jacks with dirty flag, update the status, call
553 * callbacks and call snd_hda_jack_report_sync() if any changes are found.
554 */
David Henningsson26a6cb62012-10-09 15:04:21 +0200555void snd_hda_jack_poll_all(struct hda_codec *codec)
556{
557 struct hda_jack_tbl *jack = codec->jacktbl.list;
558 int i, changes = 0;
559
560 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
561 unsigned int old_sense;
562 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
563 continue;
564 old_sense = get_jack_plug_state(jack->pin_sense);
565 jack_detect_update(codec, jack);
566 if (old_sense == get_jack_plug_state(jack->pin_sense))
567 continue;
568 changes = 1;
Dylan Reid0619ba82012-11-19 19:48:07 +0100569 call_jack_callback(codec, jack);
David Henningsson26a6cb62012-10-09 15:04:21 +0200570 }
571 if (changes)
572 snd_hda_jack_report_sync(codec);
573}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100574EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);
David Henningsson26a6cb62012-10-09 15:04:21 +0200575