xref: /linux/sound/pci/hda/hda_jack.c (revision 95e9fd10f06cb5642028b6b851e32b8c8afb4571)
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>
14 #include <linux/export.h>
15 #include <sound/core.h>
16 #include <sound/control.h>
17 #include <sound/jack.h>
18 #include "hda_codec.h"
19 #include "hda_local.h"
20 #include "hda_auto_parser.h"
21 #include "hda_jack.h"
22 
23 bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
24 {
25 	if (codec->no_jack_detect)
26 		return false;
27 	if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
28 		return false;
29 	if (!codec->ignore_misc_bit &&
30 	    (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
31 	     AC_DEFCFG_MISC_NO_PRESENCE))
32 		return false;
33 	if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP))
34 		return false;
35 	return true;
36 }
37 EXPORT_SYMBOL_HDA(is_jack_detectable);
38 
39 /* execute pin sense measurement */
40 static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
41 {
42 	u32 pincap;
43 
44 	if (!codec->no_trigger_sense) {
45 		pincap = snd_hda_query_pin_caps(codec, nid);
46 		if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
47 			snd_hda_codec_read(codec, nid, 0,
48 					AC_VERB_SET_PIN_SENSE, 0);
49 	}
50 	return snd_hda_codec_read(codec, nid, 0,
51 				  AC_VERB_GET_PIN_SENSE, 0);
52 }
53 
54 /**
55  * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
56  */
57 struct hda_jack_tbl *
58 snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
59 {
60 	struct hda_jack_tbl *jack = codec->jacktbl.list;
61 	int i;
62 
63 	if (!nid || !jack)
64 		return NULL;
65 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
66 		if (jack->nid == nid)
67 			return jack;
68 	return NULL;
69 }
70 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
71 
72 /**
73  * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
74  */
75 struct hda_jack_tbl *
76 snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
77 {
78 	struct hda_jack_tbl *jack = codec->jacktbl.list;
79 	int i;
80 
81 	if (!tag || !jack)
82 		return NULL;
83 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
84 		if (jack->tag == tag)
85 			return jack;
86 	return NULL;
87 }
88 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
89 
90 /**
91  * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
92  */
93 struct hda_jack_tbl *
94 snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
95 {
96 	struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
97 	if (jack)
98 		return jack;
99 	snd_array_init(&codec->jacktbl, sizeof(*jack), 16);
100 	jack = snd_array_new(&codec->jacktbl);
101 	if (!jack)
102 		return NULL;
103 	jack->nid = nid;
104 	jack->jack_dirty = 1;
105 	jack->tag = codec->jacktbl.used;
106 	return jack;
107 }
108 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new);
109 
110 void snd_hda_jack_tbl_clear(struct hda_codec *codec)
111 {
112 #ifdef CONFIG_SND_HDA_INPUT_JACK
113 	/* free jack instances manually when clearing/reconfiguring */
114 	if (!codec->bus->shutdown && codec->jacktbl.list) {
115 		struct hda_jack_tbl *jack = codec->jacktbl.list;
116 		int i;
117 		for (i = 0; i < codec->jacktbl.used; i++, jack++) {
118 			if (jack->jack)
119 				snd_device_free(codec->bus->card, jack->jack);
120 		}
121 	}
122 #endif
123 	snd_array_free(&codec->jacktbl);
124 }
125 
126 /* update the cached value and notification flag if needed */
127 static void jack_detect_update(struct hda_codec *codec,
128 			       struct hda_jack_tbl *jack)
129 {
130 	if (!jack->jack_dirty)
131 		return;
132 
133 	if (jack->phantom_jack)
134 		jack->pin_sense = AC_PINSENSE_PRESENCE;
135 	else
136 		jack->pin_sense = read_pin_sense(codec, jack->nid);
137 
138 	jack->jack_dirty = 0;
139 }
140 
141 /**
142  * snd_hda_set_dirty_all - Mark all the cached as dirty
143  *
144  * This function sets the dirty flag to all entries of jack table.
145  * It's called from the resume path in hda_codec.c.
146  */
147 void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
148 {
149 	struct hda_jack_tbl *jack = codec->jacktbl.list;
150 	int i;
151 
152 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
153 		if (jack->nid)
154 			jack->jack_dirty = 1;
155 }
156 EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
157 
158 /**
159  * snd_hda_pin_sense - execute pin sense measurement
160  * @codec: the CODEC to sense
161  * @nid: the pin NID to sense
162  *
163  * Execute necessary pin sense measurement and return its Presence Detect,
164  * Impedance, ELD Valid etc. status bits.
165  */
166 u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
167 {
168 	struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
169 	if (jack) {
170 		jack_detect_update(codec, jack);
171 		return jack->pin_sense;
172 	}
173 	return read_pin_sense(codec, nid);
174 }
175 EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
176 
177 #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
178 
179 /**
180  * snd_hda_jack_detect - query pin Presence Detect status
181  * @codec: the CODEC to sense
182  * @nid: the pin NID to sense
183  *
184  * Query and return the pin's Presence Detect status.
185  */
186 int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
187 {
188 	u32 sense = snd_hda_pin_sense(codec, nid);
189 	return get_jack_plug_state(sense);
190 }
191 EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
192 
193 /**
194  * snd_hda_jack_detect_enable - enable the jack-detection
195  */
196 int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
197 			       unsigned char action)
198 {
199 	struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
200 	if (!jack)
201 		return -ENOMEM;
202 	if (jack->jack_detect)
203 		return 0; /* already registered */
204 	jack->jack_detect = 1;
205 	if (action)
206 		jack->action = action;
207 	return snd_hda_codec_write_cache(codec, nid, 0,
208 					 AC_VERB_SET_UNSOLICITED_ENABLE,
209 					 AC_USRSP_EN | jack->tag);
210 }
211 EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
212 
213 /**
214  * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
215  */
216 void snd_hda_jack_report_sync(struct hda_codec *codec)
217 {
218 	struct hda_jack_tbl *jack = codec->jacktbl.list;
219 	int i, state;
220 
221 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
222 		if (jack->nid) {
223 			jack_detect_update(codec, jack);
224 			if (!jack->kctl)
225 				continue;
226 			state = get_jack_plug_state(jack->pin_sense);
227 			snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
228 #ifdef CONFIG_SND_HDA_INPUT_JACK
229 			if (jack->jack)
230 				snd_jack_report(jack->jack,
231 						state ? jack->type : 0);
232 #endif
233 		}
234 }
235 EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
236 
237 #ifdef CONFIG_SND_HDA_INPUT_JACK
238 /* guess the jack type from the pin-config */
239 static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
240 {
241 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
242 	switch (get_defcfg_device(def_conf)) {
243 	case AC_JACK_LINE_OUT:
244 	case AC_JACK_SPEAKER:
245 		return SND_JACK_LINEOUT;
246 	case AC_JACK_HP_OUT:
247 		return SND_JACK_HEADPHONE;
248 	case AC_JACK_SPDIF_OUT:
249 	case AC_JACK_DIG_OTHER_OUT:
250 		return SND_JACK_AVOUT;
251 	case AC_JACK_MIC_IN:
252 		return SND_JACK_MICROPHONE;
253 	default:
254 		return SND_JACK_LINEIN;
255 	}
256 }
257 
258 static void hda_free_jack_priv(struct snd_jack *jack)
259 {
260 	struct hda_jack_tbl *jacks = jack->private_data;
261 	jacks->nid = 0;
262 	jacks->jack = NULL;
263 }
264 #endif
265 
266 /**
267  * snd_hda_jack_add_kctl - Add a kctl for the given pin
268  *
269  * This assigns a jack-detection kctl to the given pin.  The kcontrol
270  * will have the given name and index.
271  */
272 static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
273 			  const char *name, int idx, bool phantom_jack)
274 {
275 	struct hda_jack_tbl *jack;
276 	struct snd_kcontrol *kctl;
277 	int err, state;
278 
279 	jack = snd_hda_jack_tbl_new(codec, nid);
280 	if (!jack)
281 		return 0;
282 	if (jack->kctl)
283 		return 0; /* already created */
284 	kctl = snd_kctl_jack_new(name, idx, codec);
285 	if (!kctl)
286 		return -ENOMEM;
287 	err = snd_hda_ctl_add(codec, nid, kctl);
288 	if (err < 0)
289 		return err;
290 	jack->kctl = kctl;
291 	jack->phantom_jack = !!phantom_jack;
292 
293 	state = snd_hda_jack_detect(codec, nid);
294 	snd_kctl_jack_report(codec->bus->card, kctl, state);
295 #ifdef CONFIG_SND_HDA_INPUT_JACK
296 	if (!phantom_jack) {
297 		jack->type = get_input_jack_type(codec, nid);
298 		err = snd_jack_new(codec->bus->card, name, jack->type,
299 				   &jack->jack);
300 		if (err < 0)
301 			return err;
302 		jack->jack->private_data = jack;
303 		jack->jack->private_free = hda_free_jack_priv;
304 		snd_jack_report(jack->jack, state ? jack->type : 0);
305 	}
306 #endif
307 	return 0;
308 }
309 
310 int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
311 			  const char *name, int idx)
312 {
313 	return __snd_hda_jack_add_kctl(codec, nid, name, idx, false);
314 }
315 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
316 
317 /* get the unique index number for the given kctl name */
318 static int get_unique_index(struct hda_codec *codec, const char *name, int idx)
319 {
320 	struct hda_jack_tbl *jack;
321 	int i, len = strlen(name);
322  again:
323 	jack = codec->jacktbl.list;
324 	for (i = 0; i < codec->jacktbl.used; i++, jack++) {
325 		/* jack->kctl.id contains "XXX Jack" name string with index */
326 		if (jack->kctl &&
327 		    !strncmp(name, jack->kctl->id.name, len) &&
328 		    !strcmp(" Jack", jack->kctl->id.name + len) &&
329 		    jack->kctl->id.index == idx) {
330 			idx++;
331 			goto again;
332 		}
333 	}
334 	return idx;
335 }
336 
337 static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
338 			 const struct auto_pin_cfg *cfg)
339 {
340 	unsigned int def_conf, conn;
341 	char name[44];
342 	int idx, err;
343 	bool phantom_jack;
344 
345 	if (!nid)
346 		return 0;
347 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
348 	conn = get_defcfg_connect(def_conf);
349 	if (conn == AC_JACK_PORT_NONE)
350 		return 0;
351 	phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
352 		       !is_jack_detectable(codec, nid);
353 
354 	snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
355 	if (phantom_jack)
356 		/* Example final name: "Internal Mic Phantom Jack" */
357 		strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
358 	idx = get_unique_index(codec, name, idx);
359 	err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack);
360 	if (err < 0)
361 		return err;
362 
363 	if (!phantom_jack)
364 		return snd_hda_jack_detect_enable(codec, nid, 0);
365 	return 0;
366 }
367 
368 /**
369  * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
370  */
371 int snd_hda_jack_add_kctls(struct hda_codec *codec,
372 			   const struct auto_pin_cfg *cfg)
373 {
374 	const hda_nid_t *p;
375 	int i, err;
376 
377 	for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
378 		err = add_jack_kctl(codec, *p, cfg);
379 		if (err < 0)
380 			return err;
381 	}
382 	for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
383 		if (*p == *cfg->line_out_pins) /* might be duplicated */
384 			break;
385 		err = add_jack_kctl(codec, *p, cfg);
386 		if (err < 0)
387 			return err;
388 	}
389 	for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
390 		if (*p == *cfg->line_out_pins) /* might be duplicated */
391 			break;
392 		err = add_jack_kctl(codec, *p, cfg);
393 		if (err < 0)
394 			return err;
395 	}
396 	for (i = 0; i < cfg->num_inputs; i++) {
397 		err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg);
398 		if (err < 0)
399 			return err;
400 	}
401 	for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
402 		err = add_jack_kctl(codec, *p, cfg);
403 		if (err < 0)
404 			return err;
405 	}
406 	err = add_jack_kctl(codec, cfg->dig_in_pin, cfg);
407 	if (err < 0)
408 		return err;
409 	err = add_jack_kctl(codec, cfg->mono_out_pin, cfg);
410 	if (err < 0)
411 		return err;
412 	return 0;
413 }
414 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);
415