diff options
Diffstat (limited to 'sound/pci/hda/hda_jack.c')
-rw-r--r-- | sound/pci/hda/hda_jack.c | 353 |
1 files changed, 353 insertions, 0 deletions
diff --git a/sound/pci/hda/hda_jack.c b/sound/pci/hda/hda_jack.c new file mode 100644 index 000000000000..d8a35da0803f --- /dev/null +++ b/sound/pci/hda/hda_jack.c | |||
@@ -0,0 +1,353 @@ | |||
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_jack.h" | ||
21 | |||
22 | /* execute pin sense measurement */ | ||
23 | static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid) | ||
24 | { | ||
25 | u32 pincap; | ||
26 | |||
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); | ||
32 | } | ||
33 | return snd_hda_codec_read(codec, nid, 0, | ||
34 | AC_VERB_GET_PIN_SENSE, 0); | ||
35 | } | ||
36 | |||
37 | /** | ||
38 | * snd_hda_jack_tbl_get - query the jack-table entry for the given NID | ||
39 | */ | ||
40 | struct hda_jack_tbl * | ||
41 | snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid) | ||
42 | { | ||
43 | struct hda_jack_tbl *jack = codec->jacktbl.list; | ||
44 | int i; | ||
45 | |||
46 | if (!nid || !jack) | ||
47 | return NULL; | ||
48 | for (i = 0; i < codec->jacktbl.used; i++, jack++) | ||
49 | if (jack->nid == nid) | ||
50 | return jack; | ||
51 | return NULL; | ||
52 | } | ||
53 | EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get); | ||
54 | |||
55 | /** | ||
56 | * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag | ||
57 | */ | ||
58 | struct hda_jack_tbl * | ||
59 | snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag) | ||
60 | { | ||
61 | struct hda_jack_tbl *jack = codec->jacktbl.list; | ||
62 | int i; | ||
63 | |||
64 | if (!tag || !jack) | ||
65 | return NULL; | ||
66 | for (i = 0; i < codec->jacktbl.used; i++, jack++) | ||
67 | if (jack->tag == tag) | ||
68 | return jack; | ||
69 | return NULL; | ||
70 | } | ||
71 | EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag); | ||
72 | |||
73 | /** | ||
74 | * snd_hda_jack_tbl_new - create a jack-table entry for the given NID | ||
75 | */ | ||
76 | struct hda_jack_tbl * | ||
77 | snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid) | ||
78 | { | ||
79 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); | ||
80 | if (jack) | ||
81 | return jack; | ||
82 | snd_array_init(&codec->jacktbl, sizeof(*jack), 16); | ||
83 | jack = snd_array_new(&codec->jacktbl); | ||
84 | if (!jack) | ||
85 | return NULL; | ||
86 | jack->nid = nid; | ||
87 | jack->jack_dirty = 1; | ||
88 | jack->tag = codec->jacktbl.used; | ||
89 | return jack; | ||
90 | } | ||
91 | EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new); | ||
92 | |||
93 | void snd_hda_jack_tbl_clear(struct hda_codec *codec) | ||
94 | { | ||
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; | ||
99 | int i; | ||
100 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { | ||
101 | if (jack->jack) | ||
102 | snd_device_free(codec->bus->card, jack->jack); | ||
103 | } | ||
104 | } | ||
105 | #endif | ||
106 | snd_array_free(&codec->jacktbl); | ||
107 | } | ||
108 | |||
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) | ||
112 | { | ||
113 | if (jack->jack_dirty || !jack->jack_detect) { | ||
114 | jack->pin_sense = read_pin_sense(codec, jack->nid); | ||
115 | jack->jack_dirty = 0; | ||
116 | } | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * snd_hda_set_dirty_all - Mark all the cached as dirty | ||
121 | * | ||
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. | ||
124 | */ | ||
125 | void snd_hda_jack_set_dirty_all(struct hda_codec *codec) | ||
126 | { | ||
127 | struct hda_jack_tbl *jack = codec->jacktbl.list; | ||
128 | int i; | ||
129 | |||
130 | for (i = 0; i < codec->jacktbl.used; i++, jack++) | ||
131 | if (jack->nid) | ||
132 | jack->jack_dirty = 1; | ||
133 | } | ||
134 | EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all); | ||
135 | |||
136 | /** | ||
137 | * snd_hda_pin_sense - execute pin sense measurement | ||
138 | * @codec: the CODEC to sense | ||
139 | * @nid: the pin NID to sense | ||
140 | * | ||
141 | * Execute necessary pin sense measurement and return its Presence Detect, | ||
142 | * Impedance, ELD Valid etc. status bits. | ||
143 | */ | ||
144 | u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid) | ||
145 | { | ||
146 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); | ||
147 | if (jack) { | ||
148 | jack_detect_update(codec, jack); | ||
149 | return jack->pin_sense; | ||
150 | } | ||
151 | return read_pin_sense(codec, nid); | ||
152 | } | ||
153 | EXPORT_SYMBOL_HDA(snd_hda_pin_sense); | ||
154 | |||
155 | #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE) | ||
156 | |||
157 | /** | ||
158 | * snd_hda_jack_detect - query pin Presence Detect status | ||
159 | * @codec: the CODEC to sense | ||
160 | * @nid: the pin NID to sense | ||
161 | * | ||
162 | * Query and return the pin's Presence Detect status. | ||
163 | */ | ||
164 | int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid) | ||
165 | { | ||
166 | u32 sense = snd_hda_pin_sense(codec, nid); | ||
167 | return get_jack_plug_state(sense); | ||
168 | } | ||
169 | EXPORT_SYMBOL_HDA(snd_hda_jack_detect); | ||
170 | |||
171 | /** | ||
172 | * snd_hda_jack_detect_enable - enable the jack-detection | ||
173 | */ | ||
174 | int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid, | ||
175 | unsigned char action) | ||
176 | { | ||
177 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid); | ||
178 | if (!jack) | ||
179 | return -ENOMEM; | ||
180 | if (jack->jack_detect) | ||
181 | return 0; /* already registered */ | ||
182 | jack->jack_detect = 1; | ||
183 | if (action) | ||
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); | ||
188 | } | ||
189 | EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable); | ||
190 | |||
191 | /** | ||
192 | * snd_hda_jack_report_sync - sync the states of all jacks and report if changed | ||
193 | */ | ||
194 | void snd_hda_jack_report_sync(struct hda_codec *codec) | ||
195 | { | ||
196 | struct hda_jack_tbl *jack = codec->jacktbl.list; | ||
197 | int i, state; | ||
198 | |||
199 | for (i = 0; i < codec->jacktbl.used; i++, jack++) | ||
200 | if (jack->nid) { | ||
201 | jack_detect_update(codec, jack); | ||
202 | if (!jack->kctl) | ||
203 | continue; | ||
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 | ||
207 | if (jack->jack) | ||
208 | snd_jack_report(jack->jack, | ||
209 | state ? jack->type : 0); | ||
210 | #endif | ||
211 | } | ||
212 | } | ||
213 | EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync); | ||
214 | |||
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) | ||
218 | { | ||
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; | ||
224 | case AC_JACK_HP_OUT: | ||
225 | return SND_JACK_HEADPHONE; | ||
226 | case AC_JACK_SPDIF_OUT: | ||
227 | case AC_JACK_DIG_OTHER_OUT: | ||
228 | return SND_JACK_AVOUT; | ||
229 | case AC_JACK_MIC_IN: | ||
230 | return SND_JACK_MICROPHONE; | ||
231 | default: | ||
232 | return SND_JACK_LINEIN; | ||
233 | } | ||
234 | } | ||
235 | |||
236 | static void hda_free_jack_priv(struct snd_jack *jack) | ||
237 | { | ||
238 | struct hda_jack_tbl *jacks = jack->private_data; | ||
239 | jacks->nid = 0; | ||
240 | jacks->jack = NULL; | ||
241 | } | ||
242 | #endif | ||
243 | |||
244 | /** | ||
245 | * snd_hda_jack_add_kctl - Add a kctl for the given pin | ||
246 | * | ||
247 | * This assigns a jack-detection kctl to the given pin. The kcontrol | ||
248 | * will have the given name and index. | ||
249 | */ | ||
250 | int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, | ||
251 | const char *name, int idx) | ||
252 | { | ||
253 | struct hda_jack_tbl *jack; | ||
254 | struct snd_kcontrol *kctl; | ||
255 | int err, state; | ||
256 | |||
257 | jack = snd_hda_jack_tbl_new(codec, nid); | ||
258 | if (!jack) | ||
259 | return 0; | ||
260 | if (jack->kctl) | ||
261 | return 0; /* already created */ | ||
262 | kctl = snd_kctl_jack_new(name, idx, codec); | ||
263 | if (!kctl) | ||
264 | return -ENOMEM; | ||
265 | err = snd_hda_ctl_add(codec, nid, kctl); | ||
266 | if (err < 0) | ||
267 | return err; | ||
268 | jack->kctl = 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); | ||
274 | if (err < 0) | ||
275 | return err; | ||
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); | ||
279 | #endif | ||
280 | return 0; | ||
281 | } | ||
282 | EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl); | ||
283 | |||
284 | static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, | ||
285 | const struct auto_pin_cfg *cfg) | ||
286 | { | ||
287 | unsigned int def_conf, conn; | ||
288 | char name[44]; | ||
289 | int idx, err; | ||
290 | |||
291 | if (!nid) | ||
292 | return 0; | ||
293 | if (!is_jack_detectable(codec, nid)) | ||
294 | return 0; | ||
295 | def_conf = snd_hda_codec_get_pincfg(codec, nid); | ||
296 | conn = get_defcfg_connect(def_conf); | ||
297 | if (conn != AC_JACK_PORT_COMPLEX) | ||
298 | return 0; | ||
299 | |||
300 | snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx); | ||
301 | err = snd_hda_jack_add_kctl(codec, nid, name, idx); | ||
302 | if (err < 0) | ||
303 | return err; | ||
304 | return snd_hda_jack_detect_enable(codec, nid, 0); | ||
305 | } | ||
306 | |||
307 | /** | ||
308 | * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg | ||
309 | */ | ||
310 | int snd_hda_jack_add_kctls(struct hda_codec *codec, | ||
311 | const struct auto_pin_cfg *cfg) | ||
312 | { | ||
313 | const hda_nid_t *p; | ||
314 | int i, err; | ||
315 | |||
316 | for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) { | ||
317 | err = add_jack_kctl(codec, *p, cfg); | ||
318 | if (err < 0) | ||
319 | return err; | ||
320 | } | ||
321 | for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { | ||
322 | if (*p == *cfg->line_out_pins) /* might be duplicated */ | ||
323 | break; | ||
324 | err = add_jack_kctl(codec, *p, cfg); | ||
325 | if (err < 0) | ||
326 | return err; | ||
327 | } | ||
328 | for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { | ||
329 | if (*p == *cfg->line_out_pins) /* might be duplicated */ | ||
330 | break; | ||
331 | err = add_jack_kctl(codec, *p, cfg); | ||
332 | if (err < 0) | ||
333 | return err; | ||
334 | } | ||
335 | for (i = 0; i < cfg->num_inputs; i++) { | ||
336 | err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg); | ||
337 | if (err < 0) | ||
338 | return err; | ||
339 | } | ||
340 | for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) { | ||
341 | err = add_jack_kctl(codec, *p, cfg); | ||
342 | if (err < 0) | ||
343 | return err; | ||
344 | } | ||
345 | err = add_jack_kctl(codec, cfg->dig_in_pin, cfg); | ||
346 | if (err < 0) | ||
347 | return err; | ||
348 | err = add_jack_kctl(codec, cfg->mono_out_pin, cfg); | ||
349 | if (err < 0) | ||
350 | return err; | ||
351 | return 0; | ||
352 | } | ||
353 | EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls); | ||