2 * Jack-detection handling for HD-audio
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
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.
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"
22 bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
24 if (codec->no_jack_detect)
26 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
28 if (!codec->ignore_misc_bit &&
29 (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
30 AC_DEFCFG_MISC_NO_PRESENCE))
32 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP))
36 EXPORT_SYMBOL_HDA(is_jack_detectable);
38 /* execute pin sense measurement */
39 static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
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);
49 return snd_hda_codec_read(codec, nid, 0,
50 AC_VERB_GET_PIN_SENSE, 0);
54 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
57 snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
59 struct hda_jack_tbl *jack = codec->jacktbl.list;
64 for (i = 0; i < codec->jacktbl.used; i++, jack++)
69 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
72 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
75 snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
77 struct hda_jack_tbl *jack = codec->jacktbl.list;
82 for (i = 0; i < codec->jacktbl.used; i++, jack++)
87 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
90 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
93 snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
95 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
98 snd_array_init(&codec->jacktbl, sizeof(*jack), 16);
99 jack = snd_array_new(&codec->jacktbl);
103 jack->jack_dirty = 1;
104 jack->tag = codec->jacktbl.used;
107 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new);
109 void snd_hda_jack_tbl_clear(struct hda_codec *codec)
111 #ifdef CONFIG_SND_HDA_INPUT_JACK
112 /* free jack instances manually when clearing/reconfiguring */
113 if (!codec->bus->shutdown && codec->jacktbl.list) {
114 struct hda_jack_tbl *jack = codec->jacktbl.list;
116 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
118 snd_device_free(codec->bus->card, jack->jack);
122 snd_array_free(&codec->jacktbl);
125 /* update the cached value and notification flag if needed */
126 static void jack_detect_update(struct hda_codec *codec,
127 struct hda_jack_tbl *jack)
129 if (jack->jack_dirty || !jack->jack_detect) {
130 jack->pin_sense = read_pin_sense(codec, jack->nid);
131 jack->jack_dirty = 0;
136 * snd_hda_set_dirty_all - Mark all the cached as dirty
138 * This function sets the dirty flag to all entries of jack table.
139 * It's called from the resume path in hda_codec.c.
141 void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
143 struct hda_jack_tbl *jack = codec->jacktbl.list;
146 for (i = 0; i < codec->jacktbl.used; i++, jack++)
148 jack->jack_dirty = 1;
150 EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
153 * snd_hda_pin_sense - execute pin sense measurement
154 * @codec: the CODEC to sense
155 * @nid: the pin NID to sense
157 * Execute necessary pin sense measurement and return its Presence Detect,
158 * Impedance, ELD Valid etc. status bits.
160 u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
162 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
164 jack_detect_update(codec, jack);
165 return jack->pin_sense;
167 return read_pin_sense(codec, nid);
169 EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
171 #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
174 * snd_hda_jack_detect - query pin Presence Detect status
175 * @codec: the CODEC to sense
176 * @nid: the pin NID to sense
178 * Query and return the pin's Presence Detect status.
180 int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
182 u32 sense = snd_hda_pin_sense(codec, nid);
183 return get_jack_plug_state(sense);
185 EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
188 * snd_hda_jack_detect_enable - enable the jack-detection
190 int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
191 unsigned char action)
193 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
196 if (jack->jack_detect)
197 return 0; /* already registered */
198 jack->jack_detect = 1;
200 jack->action = action;
201 return snd_hda_codec_write_cache(codec, nid, 0,
202 AC_VERB_SET_UNSOLICITED_ENABLE,
203 AC_USRSP_EN | jack->tag);
205 EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
208 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
210 void snd_hda_jack_report_sync(struct hda_codec *codec)
212 struct hda_jack_tbl *jack = codec->jacktbl.list;
215 for (i = 0; i < codec->jacktbl.used; i++, jack++)
217 jack_detect_update(codec, jack);
220 state = get_jack_plug_state(jack->pin_sense);
221 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
222 #ifdef CONFIG_SND_HDA_INPUT_JACK
224 snd_jack_report(jack->jack,
225 state ? jack->type : 0);
229 EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
231 #ifdef CONFIG_SND_HDA_INPUT_JACK
232 /* guess the jack type from the pin-config */
233 static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
235 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
236 switch (get_defcfg_device(def_conf)) {
237 case AC_JACK_LINE_OUT:
238 case AC_JACK_SPEAKER:
239 return SND_JACK_LINEOUT;
241 return SND_JACK_HEADPHONE;
242 case AC_JACK_SPDIF_OUT:
243 case AC_JACK_DIG_OTHER_OUT:
244 return SND_JACK_AVOUT;
246 return SND_JACK_MICROPHONE;
248 return SND_JACK_LINEIN;
252 static void hda_free_jack_priv(struct snd_jack *jack)
254 struct hda_jack_tbl *jacks = jack->private_data;
261 * snd_hda_jack_add_kctl - Add a kctl for the given pin
263 * This assigns a jack-detection kctl to the given pin. The kcontrol
264 * will have the given name and index.
266 int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
267 const char *name, int idx)
269 struct hda_jack_tbl *jack;
270 struct snd_kcontrol *kctl;
273 jack = snd_hda_jack_tbl_new(codec, nid);
277 return 0; /* already created */
278 kctl = snd_kctl_jack_new(name, idx, codec);
281 err = snd_hda_ctl_add(codec, nid, kctl);
285 state = snd_hda_jack_detect(codec, nid);
286 snd_kctl_jack_report(codec->bus->card, kctl, state);
287 #ifdef CONFIG_SND_HDA_INPUT_JACK
288 jack->type = get_input_jack_type(codec, nid);
289 err = snd_jack_new(codec->bus->card, name, jack->type, &jack->jack);
292 jack->jack->private_data = jack;
293 jack->jack->private_free = hda_free_jack_priv;
294 snd_jack_report(jack->jack, state ? jack->type : 0);
298 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
300 static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
301 const struct auto_pin_cfg *cfg,
302 char *lastname, int *lastidx)
304 unsigned int def_conf, conn;
310 if (!is_jack_detectable(codec, nid))
312 def_conf = snd_hda_codec_get_pincfg(codec, nid);
313 conn = get_defcfg_connect(def_conf);
314 if (conn != AC_JACK_PORT_COMPLEX)
317 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
318 if (!strcmp(name, lastname) && idx == *lastidx)
320 strncpy(lastname, name, 44);
322 err = snd_hda_jack_add_kctl(codec, nid, name, idx);
325 return snd_hda_jack_detect_enable(codec, nid, 0);
329 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
331 int snd_hda_jack_add_kctls(struct hda_codec *codec,
332 const struct auto_pin_cfg *cfg)
335 int i, err, lastidx = 0;
336 char lastname[44] = "";
338 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
339 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
343 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
344 if (*p == *cfg->line_out_pins) /* might be duplicated */
346 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
350 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
351 if (*p == *cfg->line_out_pins) /* might be duplicated */
353 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
357 for (i = 0; i < cfg->num_inputs; i++) {
358 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, lastname, &lastidx);
362 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
363 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
367 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, lastname, &lastidx);
370 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, lastname, &lastidx);
375 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);