i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / sound / pci / hda / hda_jack.c
blob9d819c4b4923d18c67447953b02395408e923afc
1 /*
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"
20 #include "hda_jack.h"
22 /* execute pin sense measurement */
23 static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
25 u32 pincap;
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);
37 /**
38 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
40 struct hda_jack_tbl *
41 snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
43 struct hda_jack_tbl *jack = codec->jacktbl.list;
44 int i;
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;
53 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
55 /**
56 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
58 struct hda_jack_tbl *
59 snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
61 struct hda_jack_tbl *jack = codec->jacktbl.list;
62 int i;
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;
71 EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
73 /**
74 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
76 struct hda_jack_tbl *
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);
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;
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;
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);
105 #endif
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;
128 int i;
130 for (i = 0; i < codec->jacktbl.used; i++, jack++)
131 if (jack->nid)
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);
147 if (jack) {
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);
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);
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;
197 int i, state;
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
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;
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;
236 static void hda_free_jack_priv(struct snd_jack *jack)
238 struct hda_jack_tbl *jacks = jack->private_data;
239 jacks->nid = 0;
240 jacks->jack = NULL;
242 #endif
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;
255 int err, state;
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;
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;
289 char name[44];
290 int idx, err;
292 if (!nid)
293 return 0;
294 if (!is_jack_detectable(codec, nid))
295 return 0;
296 def_conf = snd_hda_codec_get_pincfg(codec, nid);
297 conn = get_defcfg_connect(def_conf);
298 if (conn != AC_JACK_PORT_COMPLEX)
299 return 0;
301 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
302 if (!strcmp(name, lastname) && idx == *lastidx)
303 idx++;
304 strncpy(lastname, name, 44);
305 *lastidx = idx;
306 err = snd_hda_jack_add_kctl(codec, nid, name, idx);
307 if (err < 0)
308 return err;
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)
318 const hda_nid_t *p;
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);
324 if (err < 0)
325 return err;
327 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
328 if (*p == *cfg->line_out_pins) /* might be duplicated */
329 break;
330 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
331 if (err < 0)
332 return err;
334 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
335 if (*p == *cfg->line_out_pins) /* might be duplicated */
336 break;
337 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
338 if (err < 0)
339 return err;
341 for (i = 0; i < cfg->num_inputs; i++) {
342 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, lastname, &lastidx);
343 if (err < 0)
344 return err;
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);
348 if (err < 0)
349 return err;
351 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, lastname, &lastidx);
352 if (err < 0)
353 return err;
354 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, lastname, &lastidx);
355 if (err < 0)
356 return err;
357 return 0;
359 EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);