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 /* execute pin sense measurement */
23 static u32
read_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
27 if (!codec
->no_trigger_sense
) {
28 pincap
= snd_hda_query_pin_caps(codec
, nid
);
29 if (pincap
& AC_PINCAP_TRIG_REQ
) /* need trigger? */
30 snd_hda_codec_read(codec
, nid
, 0,
31 AC_VERB_SET_PIN_SENSE
, 0);
33 return snd_hda_codec_read(codec
, nid
, 0,
34 AC_VERB_GET_PIN_SENSE
, 0);
38 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
41 snd_hda_jack_tbl_get(struct hda_codec
*codec
, hda_nid_t nid
)
43 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
48 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
53 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get
);
56 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
59 snd_hda_jack_tbl_get_from_tag(struct hda_codec
*codec
, unsigned char tag
)
61 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
66 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
71 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag
);
74 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
77 snd_hda_jack_tbl_new(struct hda_codec
*codec
, hda_nid_t nid
)
79 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_get(codec
, nid
);
82 snd_array_init(&codec
->jacktbl
, sizeof(*jack
), 16);
83 jack
= snd_array_new(&codec
->jacktbl
);
88 jack
->tag
= codec
->jacktbl
.used
;
91 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new
);
93 void snd_hda_jack_tbl_clear(struct hda_codec
*codec
)
95 #ifdef CONFIG_SND_HDA_INPUT_JACK
96 /* free jack instances manually when clearing/reconfiguring */
97 if (!codec
->bus
->shutdown
&& codec
->jacktbl
.list
) {
98 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
100 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++) {
102 snd_device_free(codec
->bus
->card
, jack
->jack
);
106 snd_array_free(&codec
->jacktbl
);
109 /* update the cached value and notification flag if needed */
110 static void jack_detect_update(struct hda_codec
*codec
,
111 struct hda_jack_tbl
*jack
)
113 if (jack
->jack_dirty
|| !jack
->jack_detect
) {
114 jack
->pin_sense
= read_pin_sense(codec
, jack
->nid
);
115 jack
->jack_dirty
= 0;
120 * snd_hda_set_dirty_all - Mark all the cached as dirty
122 * This function sets the dirty flag to all entries of jack table.
123 * It's called from the resume path in hda_codec.c.
125 void snd_hda_jack_set_dirty_all(struct hda_codec
*codec
)
127 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
130 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
132 jack
->jack_dirty
= 1;
134 EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all
);
137 * snd_hda_pin_sense - execute pin sense measurement
138 * @codec: the CODEC to sense
139 * @nid: the pin NID to sense
141 * Execute necessary pin sense measurement and return its Presence Detect,
142 * Impedance, ELD Valid etc. status bits.
144 u32
snd_hda_pin_sense(struct hda_codec
*codec
, hda_nid_t nid
)
146 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_get(codec
, nid
);
148 jack_detect_update(codec
, jack
);
149 return jack
->pin_sense
;
151 return read_pin_sense(codec
, nid
);
153 EXPORT_SYMBOL_HDA(snd_hda_pin_sense
);
155 #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
158 * snd_hda_jack_detect - query pin Presence Detect status
159 * @codec: the CODEC to sense
160 * @nid: the pin NID to sense
162 * Query and return the pin's Presence Detect status.
164 int snd_hda_jack_detect(struct hda_codec
*codec
, hda_nid_t nid
)
166 u32 sense
= snd_hda_pin_sense(codec
, nid
);
167 return get_jack_plug_state(sense
);
169 EXPORT_SYMBOL_HDA(snd_hda_jack_detect
);
172 * snd_hda_jack_detect_enable - enable the jack-detection
174 int snd_hda_jack_detect_enable(struct hda_codec
*codec
, hda_nid_t nid
,
175 unsigned char action
)
177 struct hda_jack_tbl
*jack
= snd_hda_jack_tbl_new(codec
, nid
);
180 if (jack
->jack_detect
)
181 return 0; /* already registered */
182 jack
->jack_detect
= 1;
184 jack
->action
= action
;
185 return snd_hda_codec_write_cache(codec
, nid
, 0,
186 AC_VERB_SET_UNSOLICITED_ENABLE
,
187 AC_USRSP_EN
| jack
->tag
);
189 EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable
);
192 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
194 void snd_hda_jack_report_sync(struct hda_codec
*codec
)
196 struct hda_jack_tbl
*jack
= codec
->jacktbl
.list
;
199 for (i
= 0; i
< codec
->jacktbl
.used
; i
++, jack
++)
201 jack_detect_update(codec
, jack
);
204 state
= get_jack_plug_state(jack
->pin_sense
);
205 snd_kctl_jack_report(codec
->bus
->card
, jack
->kctl
, state
);
206 #ifdef CONFIG_SND_HDA_INPUT_JACK
208 snd_jack_report(jack
->jack
,
209 state
? jack
->type
: 0);
213 EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync
);
215 #ifdef CONFIG_SND_HDA_INPUT_JACK
216 /* guess the jack type from the pin-config */
217 static int get_input_jack_type(struct hda_codec
*codec
, hda_nid_t nid
)
219 unsigned int def_conf
= snd_hda_codec_get_pincfg(codec
, nid
);
220 switch (get_defcfg_device(def_conf
)) {
221 case AC_JACK_LINE_OUT
:
222 case AC_JACK_SPEAKER
:
223 return SND_JACK_LINEOUT
;
225 return SND_JACK_HEADPHONE
;
226 case AC_JACK_SPDIF_OUT
:
227 case AC_JACK_DIG_OTHER_OUT
:
228 return SND_JACK_AVOUT
;
230 return SND_JACK_MICROPHONE
;
232 return SND_JACK_LINEIN
;
236 static void hda_free_jack_priv(struct snd_jack
*jack
)
238 struct hda_jack_tbl
*jacks
= jack
->private_data
;
245 * snd_hda_jack_add_kctl - Add a kctl for the given pin
247 * This assigns a jack-detection kctl to the given pin. The kcontrol
248 * will have the given name and index.
250 int snd_hda_jack_add_kctl(struct hda_codec
*codec
, hda_nid_t nid
,
251 const char *name
, int idx
)
253 struct hda_jack_tbl
*jack
;
254 struct snd_kcontrol
*kctl
;
257 jack
= snd_hda_jack_tbl_new(codec
, nid
);
261 return 0; /* already created */
262 kctl
= snd_kctl_jack_new(name
, idx
, codec
);
265 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
269 state
= snd_hda_jack_detect(codec
, nid
);
270 snd_kctl_jack_report(codec
->bus
->card
, kctl
, state
);
271 #ifdef CONFIG_SND_HDA_INPUT_JACK
272 jack
->type
= get_input_jack_type(codec
, nid
);
273 err
= snd_jack_new(codec
->bus
->card
, name
, jack
->type
, &jack
->jack
);
276 jack
->jack
->private_data
= jack
;
277 jack
->jack
->private_free
= hda_free_jack_priv
;
278 snd_jack_report(jack
->jack
, state
? jack
->type
: 0);
282 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl
);
284 static int add_jack_kctl(struct hda_codec
*codec
, hda_nid_t nid
,
285 const struct auto_pin_cfg
*cfg
,
286 char *lastname
, int *lastidx
)
288 unsigned int def_conf
, conn
;
294 if (!is_jack_detectable(codec
, nid
))
296 def_conf
= snd_hda_codec_get_pincfg(codec
, nid
);
297 conn
= get_defcfg_connect(def_conf
);
298 if (conn
!= AC_JACK_PORT_COMPLEX
)
301 snd_hda_get_pin_label(codec
, nid
, cfg
, name
, sizeof(name
), &idx
);
302 if (!strcmp(name
, lastname
) && idx
== *lastidx
)
304 strncpy(lastname
, name
, 44);
306 err
= snd_hda_jack_add_kctl(codec
, nid
, name
, idx
);
309 return snd_hda_jack_detect_enable(codec
, nid
, 0);
313 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
315 int snd_hda_jack_add_kctls(struct hda_codec
*codec
,
316 const struct auto_pin_cfg
*cfg
)
319 int i
, err
, lastidx
= 0;
320 char lastname
[44] = "";
322 for (i
= 0, p
= cfg
->line_out_pins
; i
< cfg
->line_outs
; i
++, p
++) {
323 err
= add_jack_kctl(codec
, *p
, cfg
, lastname
, &lastidx
);
327 for (i
= 0, p
= cfg
->hp_pins
; i
< cfg
->hp_outs
; i
++, p
++) {
328 if (*p
== *cfg
->line_out_pins
) /* might be duplicated */
330 err
= add_jack_kctl(codec
, *p
, cfg
, lastname
, &lastidx
);
334 for (i
= 0, p
= cfg
->speaker_pins
; i
< cfg
->speaker_outs
; i
++, p
++) {
335 if (*p
== *cfg
->line_out_pins
) /* might be duplicated */
337 err
= add_jack_kctl(codec
, *p
, cfg
, lastname
, &lastidx
);
341 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
342 err
= add_jack_kctl(codec
, cfg
->inputs
[i
].pin
, cfg
, lastname
, &lastidx
);
346 for (i
= 0, p
= cfg
->dig_out_pins
; i
< cfg
->dig_outs
; i
++, p
++) {
347 err
= add_jack_kctl(codec
, *p
, cfg
, lastname
, &lastidx
);
351 err
= add_jack_kctl(codec
, cfg
->dig_in_pin
, cfg
, lastname
, &lastidx
);
354 err
= add_jack_kctl(codec
, cfg
->mono_out_pin
, cfg
, lastname
, &lastidx
);
359 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls
);