xref: /openbmc/linux/sound/pci/hda/hda_jack.c (revision 0619ba8c17b121ef0273be181198659b17d84247)
11835a0f9STakashi Iwai /*
21835a0f9STakashi Iwai  * Jack-detection handling for HD-audio
31835a0f9STakashi Iwai  *
41835a0f9STakashi Iwai  * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
51835a0f9STakashi Iwai  *
61835a0f9STakashi Iwai  * This driver is free software; you can redistribute it and/or modify
71835a0f9STakashi Iwai  * it under the terms of the GNU General Public License as published by
81835a0f9STakashi Iwai  * the Free Software Foundation; either version 2 of the License, or
91835a0f9STakashi Iwai  * (at your option) any later version.
101835a0f9STakashi Iwai  */
111835a0f9STakashi Iwai 
121835a0f9STakashi Iwai #include <linux/init.h>
131835a0f9STakashi Iwai #include <linux/slab.h>
14bf815bf0STakashi Iwai #include <linux/export.h>
151835a0f9STakashi Iwai #include <sound/core.h>
1601a61e12STakashi Iwai #include <sound/control.h>
17aad37dbdSTakashi Iwai #include <sound/jack.h>
181835a0f9STakashi Iwai #include "hda_codec.h"
191835a0f9STakashi Iwai #include "hda_local.h"
20128bc4baSTakashi Iwai #include "hda_auto_parser.h"
211835a0f9STakashi Iwai #include "hda_jack.h"
221835a0f9STakashi Iwai 
23a9c74173STakashi Iwai bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
24a9c74173STakashi Iwai {
2571b1e9e4STakashi Iwai 	if (codec->no_jack_detect)
2671b1e9e4STakashi Iwai 		return false;
27a9c74173STakashi Iwai 	if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
28a9c74173STakashi Iwai 		return false;
295fe8e1e6SDavid Henningsson 	if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
305fe8e1e6SDavid Henningsson 	     AC_DEFCFG_MISC_NO_PRESENCE)
31a9c74173STakashi Iwai 		return false;
32a9c74173STakashi Iwai 	if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP))
33a9c74173STakashi Iwai 		return false;
34a9c74173STakashi Iwai 	return true;
35a9c74173STakashi Iwai }
36a9c74173STakashi Iwai EXPORT_SYMBOL_HDA(is_jack_detectable);
37a9c74173STakashi Iwai 
381835a0f9STakashi Iwai /* execute pin sense measurement */
391835a0f9STakashi Iwai static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
401835a0f9STakashi Iwai {
411835a0f9STakashi Iwai 	u32 pincap;
421835a0f9STakashi Iwai 
431835a0f9STakashi Iwai 	if (!codec->no_trigger_sense) {
441835a0f9STakashi Iwai 		pincap = snd_hda_query_pin_caps(codec, nid);
451835a0f9STakashi Iwai 		if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
461835a0f9STakashi Iwai 			snd_hda_codec_read(codec, nid, 0,
471835a0f9STakashi Iwai 					AC_VERB_SET_PIN_SENSE, 0);
481835a0f9STakashi Iwai 	}
491835a0f9STakashi Iwai 	return snd_hda_codec_read(codec, nid, 0,
501835a0f9STakashi Iwai 				  AC_VERB_GET_PIN_SENSE, 0);
511835a0f9STakashi Iwai }
521835a0f9STakashi Iwai 
531835a0f9STakashi Iwai /**
541835a0f9STakashi Iwai  * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
551835a0f9STakashi Iwai  */
561835a0f9STakashi Iwai struct hda_jack_tbl *
571835a0f9STakashi Iwai snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
581835a0f9STakashi Iwai {
591835a0f9STakashi Iwai 	struct hda_jack_tbl *jack = codec->jacktbl.list;
601835a0f9STakashi Iwai 	int i;
611835a0f9STakashi Iwai 
621835a0f9STakashi Iwai 	if (!nid || !jack)
631835a0f9STakashi Iwai 		return NULL;
641835a0f9STakashi Iwai 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
651835a0f9STakashi Iwai 		if (jack->nid == nid)
661835a0f9STakashi Iwai 			return jack;
671835a0f9STakashi Iwai 	return NULL;
681835a0f9STakashi Iwai }
691835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
701835a0f9STakashi Iwai 
711835a0f9STakashi Iwai /**
723a93897eSTakashi Iwai  * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
733a93897eSTakashi Iwai  */
743a93897eSTakashi Iwai struct hda_jack_tbl *
753a93897eSTakashi Iwai snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
763a93897eSTakashi Iwai {
773a93897eSTakashi Iwai 	struct hda_jack_tbl *jack = codec->jacktbl.list;
783a93897eSTakashi Iwai 	int i;
793a93897eSTakashi Iwai 
803a93897eSTakashi Iwai 	if (!tag || !jack)
813a93897eSTakashi Iwai 		return NULL;
823a93897eSTakashi Iwai 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
833a93897eSTakashi Iwai 		if (jack->tag == tag)
843a93897eSTakashi Iwai 			return jack;
853a93897eSTakashi Iwai 	return NULL;
863a93897eSTakashi Iwai }
873a93897eSTakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
883a93897eSTakashi Iwai 
893a93897eSTakashi Iwai /**
901835a0f9STakashi Iwai  * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
911835a0f9STakashi Iwai  */
921835a0f9STakashi Iwai struct hda_jack_tbl *
931835a0f9STakashi Iwai snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
941835a0f9STakashi Iwai {
951835a0f9STakashi Iwai 	struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
961835a0f9STakashi Iwai 	if (jack)
971835a0f9STakashi Iwai 		return jack;
981835a0f9STakashi Iwai 	snd_array_init(&codec->jacktbl, sizeof(*jack), 16);
991835a0f9STakashi Iwai 	jack = snd_array_new(&codec->jacktbl);
1001835a0f9STakashi Iwai 	if (!jack)
1011835a0f9STakashi Iwai 		return NULL;
1021835a0f9STakashi Iwai 	jack->nid = nid;
1031835a0f9STakashi Iwai 	jack->jack_dirty = 1;
1043a93897eSTakashi Iwai 	jack->tag = codec->jacktbl.used;
1051835a0f9STakashi Iwai 	return jack;
1061835a0f9STakashi Iwai }
107d1cb6200SDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new);
1081835a0f9STakashi Iwai 
1091835a0f9STakashi Iwai void snd_hda_jack_tbl_clear(struct hda_codec *codec)
1101835a0f9STakashi Iwai {
11131ef2257STakashi Iwai #ifdef CONFIG_SND_HDA_INPUT_JACK
11231ef2257STakashi Iwai 	/* free jack instances manually when clearing/reconfiguring */
11331ef2257STakashi Iwai 	if (!codec->bus->shutdown && codec->jacktbl.list) {
11431ef2257STakashi Iwai 		struct hda_jack_tbl *jack = codec->jacktbl.list;
11531ef2257STakashi Iwai 		int i;
11631ef2257STakashi Iwai 		for (i = 0; i < codec->jacktbl.used; i++, jack++) {
11731ef2257STakashi Iwai 			if (jack->jack)
11831ef2257STakashi Iwai 				snd_device_free(codec->bus->card, jack->jack);
11931ef2257STakashi Iwai 		}
12031ef2257STakashi Iwai 	}
12131ef2257STakashi Iwai #endif
1221835a0f9STakashi Iwai 	snd_array_free(&codec->jacktbl);
1231835a0f9STakashi Iwai }
1241835a0f9STakashi Iwai 
125*0619ba8cSDylan Reid #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
126*0619ba8cSDylan Reid 
1271835a0f9STakashi Iwai /* update the cached value and notification flag if needed */
1281835a0f9STakashi Iwai static void jack_detect_update(struct hda_codec *codec,
1291835a0f9STakashi Iwai 			       struct hda_jack_tbl *jack)
1301835a0f9STakashi Iwai {
13180c8bfbeSDavid Henningsson 	if (!jack->jack_dirty)
13280c8bfbeSDavid Henningsson 		return;
13380c8bfbeSDavid Henningsson 
13480c8bfbeSDavid Henningsson 	if (jack->phantom_jack)
13580c8bfbeSDavid Henningsson 		jack->pin_sense = AC_PINSENSE_PRESENCE;
13680c8bfbeSDavid Henningsson 	else
13735be544aSTakashi Iwai 		jack->pin_sense = read_pin_sense(codec, jack->nid);
13880c8bfbeSDavid Henningsson 
139*0619ba8cSDylan Reid 	/* A gating jack indicates the jack is invalid if gating is unplugged */
140*0619ba8cSDylan Reid 	if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack))
141*0619ba8cSDylan Reid 		jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
142*0619ba8cSDylan Reid 
1431835a0f9STakashi Iwai 	jack->jack_dirty = 0;
144*0619ba8cSDylan Reid 
145*0619ba8cSDylan Reid 	/* If a jack is gated by this one update it. */
146*0619ba8cSDylan Reid 	if (jack->gated_jack) {
147*0619ba8cSDylan Reid 		struct hda_jack_tbl *gated =
148*0619ba8cSDylan Reid 			snd_hda_jack_tbl_get(codec, jack->gated_jack);
149*0619ba8cSDylan Reid 		if (gated) {
150*0619ba8cSDylan Reid 			gated->jack_dirty = 1;
151*0619ba8cSDylan Reid 			jack_detect_update(codec, gated);
152*0619ba8cSDylan Reid 		}
153*0619ba8cSDylan Reid 	}
1541835a0f9STakashi Iwai }
1551835a0f9STakashi Iwai 
1561835a0f9STakashi Iwai /**
1571835a0f9STakashi Iwai  * snd_hda_set_dirty_all - Mark all the cached as dirty
1581835a0f9STakashi Iwai  *
1591835a0f9STakashi Iwai  * This function sets the dirty flag to all entries of jack table.
1601835a0f9STakashi Iwai  * It's called from the resume path in hda_codec.c.
1611835a0f9STakashi Iwai  */
1621835a0f9STakashi Iwai void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
1631835a0f9STakashi Iwai {
1641835a0f9STakashi Iwai 	struct hda_jack_tbl *jack = codec->jacktbl.list;
1651835a0f9STakashi Iwai 	int i;
1661835a0f9STakashi Iwai 
1671835a0f9STakashi Iwai 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
1681835a0f9STakashi Iwai 		if (jack->nid)
1691835a0f9STakashi Iwai 			jack->jack_dirty = 1;
1701835a0f9STakashi Iwai }
1711835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
1721835a0f9STakashi Iwai 
1731835a0f9STakashi Iwai /**
1741835a0f9STakashi Iwai  * snd_hda_pin_sense - execute pin sense measurement
1751835a0f9STakashi Iwai  * @codec: the CODEC to sense
1761835a0f9STakashi Iwai  * @nid: the pin NID to sense
1771835a0f9STakashi Iwai  *
1781835a0f9STakashi Iwai  * Execute necessary pin sense measurement and return its Presence Detect,
1791835a0f9STakashi Iwai  * Impedance, ELD Valid etc. status bits.
1801835a0f9STakashi Iwai  */
1811835a0f9STakashi Iwai u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1821835a0f9STakashi Iwai {
1831835a0f9STakashi Iwai 	struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
1841835a0f9STakashi Iwai 	if (jack) {
1851835a0f9STakashi Iwai 		jack_detect_update(codec, jack);
1861835a0f9STakashi Iwai 		return jack->pin_sense;
1871835a0f9STakashi Iwai 	}
1881835a0f9STakashi Iwai 	return read_pin_sense(codec, nid);
1891835a0f9STakashi Iwai }
1901835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1911835a0f9STakashi Iwai 
1921835a0f9STakashi Iwai /**
1931835a0f9STakashi Iwai  * snd_hda_jack_detect - query pin Presence Detect status
1941835a0f9STakashi Iwai  * @codec: the CODEC to sense
1951835a0f9STakashi Iwai  * @nid: the pin NID to sense
1961835a0f9STakashi Iwai  *
1971835a0f9STakashi Iwai  * Query and return the pin's Presence Detect status.
1981835a0f9STakashi Iwai  */
1991835a0f9STakashi Iwai int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
2001835a0f9STakashi Iwai {
2011835a0f9STakashi Iwai 	u32 sense = snd_hda_pin_sense(codec, nid);
20235be544aSTakashi Iwai 	return get_jack_plug_state(sense);
2031835a0f9STakashi Iwai }
2041835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
2051835a0f9STakashi Iwai 
2061835a0f9STakashi Iwai /**
2071835a0f9STakashi Iwai  * snd_hda_jack_detect_enable - enable the jack-detection
2081835a0f9STakashi Iwai  */
209954df2a9SDavid Henningsson int snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
210954df2a9SDavid Henningsson 					unsigned char action,
211954df2a9SDavid Henningsson 					hda_jack_callback cb)
2121835a0f9STakashi Iwai {
2131835a0f9STakashi Iwai 	struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
2141835a0f9STakashi Iwai 	if (!jack)
2151835a0f9STakashi Iwai 		return -ENOMEM;
2163a93897eSTakashi Iwai 	if (jack->jack_detect)
21701a61e12STakashi Iwai 		return 0; /* already registered */
2183a93897eSTakashi Iwai 	jack->jack_detect = 1;
2193a93897eSTakashi Iwai 	if (action)
2203a93897eSTakashi Iwai 		jack->action = action;
221954df2a9SDavid Henningsson 	if (cb)
222954df2a9SDavid Henningsson 		jack->callback = cb;
22334a93187SDavid Henningsson 	if (codec->jackpoll_interval > 0)
22434a93187SDavid Henningsson 		return 0; /* No unsol if we're polling instead */
2251835a0f9STakashi Iwai 	return snd_hda_codec_write_cache(codec, nid, 0,
2261835a0f9STakashi Iwai 					 AC_VERB_SET_UNSOLICITED_ENABLE,
2273a93897eSTakashi Iwai 					 AC_USRSP_EN | jack->tag);
2281835a0f9STakashi Iwai }
229954df2a9SDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable_callback);
230954df2a9SDavid Henningsson 
231954df2a9SDavid Henningsson int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
232954df2a9SDavid Henningsson 			       unsigned char action)
233954df2a9SDavid Henningsson {
234954df2a9SDavid Henningsson 	return snd_hda_jack_detect_enable_callback(codec, nid, action, NULL);
235954df2a9SDavid Henningsson }
2361835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
23701a61e12STakashi Iwai 
23801a61e12STakashi Iwai /**
239*0619ba8cSDylan Reid  * snd_hda_jack_set_gating_jack - Set gating jack.
240*0619ba8cSDylan Reid  *
241*0619ba8cSDylan Reid  * Indicates the gated jack is only valid when the gating jack is plugged.
242*0619ba8cSDylan Reid  */
243*0619ba8cSDylan Reid int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
244*0619ba8cSDylan Reid 				 hda_nid_t gating_nid)
245*0619ba8cSDylan Reid {
246*0619ba8cSDylan Reid 	struct hda_jack_tbl *gated = snd_hda_jack_tbl_get(codec, gated_nid);
247*0619ba8cSDylan Reid 	struct hda_jack_tbl *gating = snd_hda_jack_tbl_get(codec, gating_nid);
248*0619ba8cSDylan Reid 
249*0619ba8cSDylan Reid 	if (!gated || !gating)
250*0619ba8cSDylan Reid 		return -EINVAL;
251*0619ba8cSDylan Reid 
252*0619ba8cSDylan Reid 	gated->gating_jack = gating_nid;
253*0619ba8cSDylan Reid 	gating->gated_jack = gated_nid;
254*0619ba8cSDylan Reid 
255*0619ba8cSDylan Reid 	return 0;
256*0619ba8cSDylan Reid }
257*0619ba8cSDylan Reid EXPORT_SYMBOL_HDA(snd_hda_jack_set_gating_jack);
258*0619ba8cSDylan Reid 
259*0619ba8cSDylan Reid /**
26001a61e12STakashi Iwai  * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
26101a61e12STakashi Iwai  */
26201a61e12STakashi Iwai void snd_hda_jack_report_sync(struct hda_codec *codec)
26301a61e12STakashi Iwai {
264*0619ba8cSDylan Reid 	struct hda_jack_tbl *jack;
26535be544aSTakashi Iwai 	int i, state;
26601a61e12STakashi Iwai 
267*0619ba8cSDylan Reid 	/* update all jacks at first */
268*0619ba8cSDylan Reid 	jack = codec->jacktbl.list;
269*0619ba8cSDylan Reid 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
270*0619ba8cSDylan Reid 		if (jack->nid)
271*0619ba8cSDylan Reid 			jack_detect_update(codec, jack);
272*0619ba8cSDylan Reid 
273*0619ba8cSDylan Reid 	/* report the updated jacks; it's done after updating all jacks
274*0619ba8cSDylan Reid 	 * to make sure that all gating jacks properly have been set
275*0619ba8cSDylan Reid 	 */
276*0619ba8cSDylan Reid 	jack = codec->jacktbl.list;
27701a61e12STakashi Iwai 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
27801a61e12STakashi Iwai 		if (jack->nid) {
279cfc7c9d3STakashi Iwai 			if (!jack->kctl)
280cfc7c9d3STakashi Iwai 				continue;
28135be544aSTakashi Iwai 			state = get_jack_plug_state(jack->pin_sense);
282aad37dbdSTakashi Iwai 			snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
28331ef2257STakashi Iwai #ifdef CONFIG_SND_HDA_INPUT_JACK
28431ef2257STakashi Iwai 			if (jack->jack)
28531ef2257STakashi Iwai 				snd_jack_report(jack->jack,
28631ef2257STakashi Iwai 						state ? jack->type : 0);
28731ef2257STakashi Iwai #endif
28801a61e12STakashi Iwai 		}
28901a61e12STakashi Iwai }
29001a61e12STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
29101a61e12STakashi Iwai 
29231ef2257STakashi Iwai #ifdef CONFIG_SND_HDA_INPUT_JACK
29331ef2257STakashi Iwai /* guess the jack type from the pin-config */
29431ef2257STakashi Iwai static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
29531ef2257STakashi Iwai {
29631ef2257STakashi Iwai 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
29731ef2257STakashi Iwai 	switch (get_defcfg_device(def_conf)) {
29831ef2257STakashi Iwai 	case AC_JACK_LINE_OUT:
29931ef2257STakashi Iwai 	case AC_JACK_SPEAKER:
30031ef2257STakashi Iwai 		return SND_JACK_LINEOUT;
30131ef2257STakashi Iwai 	case AC_JACK_HP_OUT:
30231ef2257STakashi Iwai 		return SND_JACK_HEADPHONE;
30331ef2257STakashi Iwai 	case AC_JACK_SPDIF_OUT:
30431ef2257STakashi Iwai 	case AC_JACK_DIG_OTHER_OUT:
30531ef2257STakashi Iwai 		return SND_JACK_AVOUT;
30631ef2257STakashi Iwai 	case AC_JACK_MIC_IN:
30731ef2257STakashi Iwai 		return SND_JACK_MICROPHONE;
30831ef2257STakashi Iwai 	default:
30931ef2257STakashi Iwai 		return SND_JACK_LINEIN;
31031ef2257STakashi Iwai 	}
31131ef2257STakashi Iwai }
31231ef2257STakashi Iwai 
31331ef2257STakashi Iwai static void hda_free_jack_priv(struct snd_jack *jack)
31431ef2257STakashi Iwai {
31531ef2257STakashi Iwai 	struct hda_jack_tbl *jacks = jack->private_data;
31631ef2257STakashi Iwai 	jacks->nid = 0;
31731ef2257STakashi Iwai 	jacks->jack = NULL;
31831ef2257STakashi Iwai }
31931ef2257STakashi Iwai #endif
32031ef2257STakashi Iwai 
32101a61e12STakashi Iwai /**
32201a61e12STakashi Iwai  * snd_hda_jack_add_kctl - Add a kctl for the given pin
32301a61e12STakashi Iwai  *
32401a61e12STakashi Iwai  * This assigns a jack-detection kctl to the given pin.  The kcontrol
32501a61e12STakashi Iwai  * will have the given name and index.
32601a61e12STakashi Iwai  */
32780c8bfbeSDavid Henningsson static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
32880c8bfbeSDavid Henningsson 			  const char *name, int idx, bool phantom_jack)
32901a61e12STakashi Iwai {
33001a61e12STakashi Iwai 	struct hda_jack_tbl *jack;
33101a61e12STakashi Iwai 	struct snd_kcontrol *kctl;
33231ef2257STakashi Iwai 	int err, state;
33301a61e12STakashi Iwai 
3343a93897eSTakashi Iwai 	jack = snd_hda_jack_tbl_new(codec, nid);
33501a61e12STakashi Iwai 	if (!jack)
33601a61e12STakashi Iwai 		return 0;
33701a61e12STakashi Iwai 	if (jack->kctl)
33801a61e12STakashi Iwai 		return 0; /* already created */
33935be544aSTakashi Iwai 	kctl = snd_kctl_jack_new(name, idx, codec);
34001a61e12STakashi Iwai 	if (!kctl)
34101a61e12STakashi Iwai 		return -ENOMEM;
34231ef2257STakashi Iwai 	err = snd_hda_ctl_add(codec, nid, kctl);
34331ef2257STakashi Iwai 	if (err < 0)
34431ef2257STakashi Iwai 		return err;
34501a61e12STakashi Iwai 	jack->kctl = kctl;
34680c8bfbeSDavid Henningsson 	jack->phantom_jack = !!phantom_jack;
34780c8bfbeSDavid Henningsson 
34831ef2257STakashi Iwai 	state = snd_hda_jack_detect(codec, nid);
34931ef2257STakashi Iwai 	snd_kctl_jack_report(codec->bus->card, kctl, state);
35031ef2257STakashi Iwai #ifdef CONFIG_SND_HDA_INPUT_JACK
35180c8bfbeSDavid Henningsson 	if (!phantom_jack) {
35231ef2257STakashi Iwai 		jack->type = get_input_jack_type(codec, nid);
35380c8bfbeSDavid Henningsson 		err = snd_jack_new(codec->bus->card, name, jack->type,
35480c8bfbeSDavid Henningsson 				   &jack->jack);
35531ef2257STakashi Iwai 		if (err < 0)
35631ef2257STakashi Iwai 			return err;
35731ef2257STakashi Iwai 		jack->jack->private_data = jack;
35831ef2257STakashi Iwai 		jack->jack->private_free = hda_free_jack_priv;
35931ef2257STakashi Iwai 		snd_jack_report(jack->jack, state ? jack->type : 0);
36080c8bfbeSDavid Henningsson 	}
36131ef2257STakashi Iwai #endif
36201a61e12STakashi Iwai 	return 0;
36301a61e12STakashi Iwai }
36480c8bfbeSDavid Henningsson 
36580c8bfbeSDavid Henningsson int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
36680c8bfbeSDavid Henningsson 			  const char *name, int idx)
36780c8bfbeSDavid Henningsson {
36880c8bfbeSDavid Henningsson 	return __snd_hda_jack_add_kctl(codec, nid, name, idx, false);
36980c8bfbeSDavid Henningsson }
370d1cb6200SDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
37101a61e12STakashi Iwai 
372f46c3296STakashi Iwai /* get the unique index number for the given kctl name */
373f46c3296STakashi Iwai static int get_unique_index(struct hda_codec *codec, const char *name, int idx)
374f46c3296STakashi Iwai {
375f46c3296STakashi Iwai 	struct hda_jack_tbl *jack;
376f46c3296STakashi Iwai 	int i, len = strlen(name);
377f46c3296STakashi Iwai  again:
378f46c3296STakashi Iwai 	jack = codec->jacktbl.list;
379f46c3296STakashi Iwai 	for (i = 0; i < codec->jacktbl.used; i++, jack++) {
380f46c3296STakashi Iwai 		/* jack->kctl.id contains "XXX Jack" name string with index */
381f46c3296STakashi Iwai 		if (jack->kctl &&
382f46c3296STakashi Iwai 		    !strncmp(name, jack->kctl->id.name, len) &&
383f46c3296STakashi Iwai 		    !strcmp(" Jack", jack->kctl->id.name + len) &&
384f46c3296STakashi Iwai 		    jack->kctl->id.index == idx) {
385f46c3296STakashi Iwai 			idx++;
386f46c3296STakashi Iwai 			goto again;
387f46c3296STakashi Iwai 		}
388f46c3296STakashi Iwai 	}
389f46c3296STakashi Iwai 	return idx;
390f46c3296STakashi Iwai }
391f46c3296STakashi Iwai 
392201e06ffSTakashi Iwai static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
393f46c3296STakashi Iwai 			 const struct auto_pin_cfg *cfg)
39401a61e12STakashi Iwai {
3953a93897eSTakashi Iwai 	unsigned int def_conf, conn;
396201e06ffSTakashi Iwai 	char name[44];
397201e06ffSTakashi Iwai 	int idx, err;
39880c8bfbeSDavid Henningsson 	bool phantom_jack;
3993a93897eSTakashi Iwai 
40001a61e12STakashi Iwai 	if (!nid)
40101a61e12STakashi Iwai 		return 0;
4023a93897eSTakashi Iwai 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
4033a93897eSTakashi Iwai 	conn = get_defcfg_connect(def_conf);
40480c8bfbeSDavid Henningsson 	if (conn == AC_JACK_PORT_NONE)
4053a93897eSTakashi Iwai 		return 0;
40680c8bfbeSDavid Henningsson 	phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
40780c8bfbeSDavid Henningsson 		       !is_jack_detectable(codec, nid);
4083a93897eSTakashi Iwai 
409201e06ffSTakashi Iwai 	snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
41080c8bfbeSDavid Henningsson 	if (phantom_jack)
41180c8bfbeSDavid Henningsson 		/* Example final name: "Internal Mic Phantom Jack" */
41280c8bfbeSDavid Henningsson 		strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
413f46c3296STakashi Iwai 	idx = get_unique_index(codec, name, idx);
41480c8bfbeSDavid Henningsson 	err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack);
4153a93897eSTakashi Iwai 	if (err < 0)
4163a93897eSTakashi Iwai 		return err;
41780c8bfbeSDavid Henningsson 
41880c8bfbeSDavid Henningsson 	if (!phantom_jack)
4193a93897eSTakashi Iwai 		return snd_hda_jack_detect_enable(codec, nid, 0);
42080c8bfbeSDavid Henningsson 	return 0;
42101a61e12STakashi Iwai }
42201a61e12STakashi Iwai 
42301a61e12STakashi Iwai /**
42401a61e12STakashi Iwai  * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
42501a61e12STakashi Iwai  */
42601a61e12STakashi Iwai int snd_hda_jack_add_kctls(struct hda_codec *codec,
42701a61e12STakashi Iwai 			   const struct auto_pin_cfg *cfg)
42801a61e12STakashi Iwai {
42901a61e12STakashi Iwai 	const hda_nid_t *p;
430f46c3296STakashi Iwai 	int i, err;
43101a61e12STakashi Iwai 
43201a61e12STakashi Iwai 	for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
433f46c3296STakashi Iwai 		err = add_jack_kctl(codec, *p, cfg);
43401a61e12STakashi Iwai 		if (err < 0)
43501a61e12STakashi Iwai 			return err;
43601a61e12STakashi Iwai 	}
43701a61e12STakashi Iwai 	for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
43801a61e12STakashi Iwai 		if (*p == *cfg->line_out_pins) /* might be duplicated */
43901a61e12STakashi Iwai 			break;
440f46c3296STakashi Iwai 		err = add_jack_kctl(codec, *p, cfg);
44101a61e12STakashi Iwai 		if (err < 0)
44201a61e12STakashi Iwai 			return err;
44301a61e12STakashi Iwai 	}
44401a61e12STakashi Iwai 	for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
44501a61e12STakashi Iwai 		if (*p == *cfg->line_out_pins) /* might be duplicated */
44601a61e12STakashi Iwai 			break;
447f46c3296STakashi Iwai 		err = add_jack_kctl(codec, *p, cfg);
44801a61e12STakashi Iwai 		if (err < 0)
44901a61e12STakashi Iwai 			return err;
45001a61e12STakashi Iwai 	}
45101a61e12STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
452f46c3296STakashi Iwai 		err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg);
45301a61e12STakashi Iwai 		if (err < 0)
45401a61e12STakashi Iwai 			return err;
45501a61e12STakashi Iwai 	}
45601a61e12STakashi Iwai 	for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
457f46c3296STakashi Iwai 		err = add_jack_kctl(codec, *p, cfg);
45801a61e12STakashi Iwai 		if (err < 0)
45901a61e12STakashi Iwai 			return err;
46001a61e12STakashi Iwai 	}
461f46c3296STakashi Iwai 	err = add_jack_kctl(codec, cfg->dig_in_pin, cfg);
46201a61e12STakashi Iwai 	if (err < 0)
46301a61e12STakashi Iwai 		return err;
464f46c3296STakashi Iwai 	err = add_jack_kctl(codec, cfg->mono_out_pin, cfg);
46501a61e12STakashi Iwai 	if (err < 0)
46601a61e12STakashi Iwai 		return err;
46701a61e12STakashi Iwai 	return 0;
46801a61e12STakashi Iwai }
46901a61e12STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);
470954df2a9SDavid Henningsson 
471*0619ba8cSDylan Reid static void call_jack_callback(struct hda_codec *codec,
472*0619ba8cSDylan Reid 			       struct hda_jack_tbl *jack)
473*0619ba8cSDylan Reid {
474*0619ba8cSDylan Reid 	if (jack->callback)
475*0619ba8cSDylan Reid 		jack->callback(codec, jack);
476*0619ba8cSDylan Reid 	if (jack->gated_jack) {
477*0619ba8cSDylan Reid 		struct hda_jack_tbl *gated =
478*0619ba8cSDylan Reid 			snd_hda_jack_tbl_get(codec, jack->gated_jack);
479*0619ba8cSDylan Reid 		if (gated && gated->callback)
480*0619ba8cSDylan Reid 			gated->callback(codec, gated);
481*0619ba8cSDylan Reid 	}
482*0619ba8cSDylan Reid }
483*0619ba8cSDylan Reid 
484954df2a9SDavid Henningsson void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
485954df2a9SDavid Henningsson {
486954df2a9SDavid Henningsson 	struct hda_jack_tbl *event;
487954df2a9SDavid Henningsson 	int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f;
488954df2a9SDavid Henningsson 
489954df2a9SDavid Henningsson 	event = snd_hda_jack_tbl_get_from_tag(codec, tag);
490954df2a9SDavid Henningsson 	if (!event)
491954df2a9SDavid Henningsson 		return;
492954df2a9SDavid Henningsson 	event->jack_dirty = 1;
493954df2a9SDavid Henningsson 
494*0619ba8cSDylan Reid 	call_jack_callback(codec, event);
495954df2a9SDavid Henningsson 	snd_hda_jack_report_sync(codec);
496954df2a9SDavid Henningsson }
497954df2a9SDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_unsol_event);
498954df2a9SDavid Henningsson 
49926a6cb6cSDavid Henningsson void snd_hda_jack_poll_all(struct hda_codec *codec)
50026a6cb6cSDavid Henningsson {
50126a6cb6cSDavid Henningsson 	struct hda_jack_tbl *jack = codec->jacktbl.list;
50226a6cb6cSDavid Henningsson 	int i, changes = 0;
50326a6cb6cSDavid Henningsson 
50426a6cb6cSDavid Henningsson 	for (i = 0; i < codec->jacktbl.used; i++, jack++) {
50526a6cb6cSDavid Henningsson 		unsigned int old_sense;
50626a6cb6cSDavid Henningsson 		if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
50726a6cb6cSDavid Henningsson 			continue;
50826a6cb6cSDavid Henningsson 		old_sense = get_jack_plug_state(jack->pin_sense);
50926a6cb6cSDavid Henningsson 		jack_detect_update(codec, jack);
51026a6cb6cSDavid Henningsson 		if (old_sense == get_jack_plug_state(jack->pin_sense))
51126a6cb6cSDavid Henningsson 			continue;
51226a6cb6cSDavid Henningsson 		changes = 1;
513*0619ba8cSDylan Reid 		call_jack_callback(codec, jack);
51426a6cb6cSDavid Henningsson 	}
51526a6cb6cSDavid Henningsson 	if (changes)
51626a6cb6cSDavid Henningsson 		snd_hda_jack_report_sync(codec);
51726a6cb6cSDavid Henningsson }
51826a6cb6cSDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_poll_all);
51926a6cb6cSDavid Henningsson 
520