blob: cee6a00bd85a2311681433196f26da22baf38174 [file] [log] [blame]
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001/*
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 <sound/core.h>
Takashi Iwai01a61e12011-10-28 00:03:22 +020015#include <sound/control.h>
Takashi Iwai1835a0f2011-10-27 22:12:46 +020016#include "hda_codec.h"
17#include "hda_local.h"
18#include "hda_jack.h"
19
20/* execute pin sense measurement */
21static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
22{
23 u32 pincap;
24
25 if (!codec->no_trigger_sense) {
26 pincap = snd_hda_query_pin_caps(codec, nid);
27 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
28 snd_hda_codec_read(codec, nid, 0,
29 AC_VERB_SET_PIN_SENSE, 0);
30 }
31 return snd_hda_codec_read(codec, nid, 0,
32 AC_VERB_GET_PIN_SENSE, 0);
33}
34
35/**
36 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
37 */
38struct hda_jack_tbl *
39snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
40{
41 struct hda_jack_tbl *jack = codec->jacktbl.list;
42 int i;
43
44 if (!nid || !jack)
45 return NULL;
46 for (i = 0; i < codec->jacktbl.used; i++, jack++)
47 if (jack->nid == nid)
48 return jack;
49 return NULL;
50}
51EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
52
53/**
54 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
55 */
56struct hda_jack_tbl *
57snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
58{
59 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
60 if (jack)
61 return jack;
62 snd_array_init(&codec->jacktbl, sizeof(*jack), 16);
63 jack = snd_array_new(&codec->jacktbl);
64 if (!jack)
65 return NULL;
66 jack->nid = nid;
67 jack->jack_dirty = 1;
68 return jack;
69}
70
71void snd_hda_jack_tbl_clear(struct hda_codec *codec)
72{
73 snd_array_free(&codec->jacktbl);
74}
75
76/* update the cached value and notification flag if needed */
77static void jack_detect_update(struct hda_codec *codec,
78 struct hda_jack_tbl *jack)
79{
Takashi Iwai01a61e12011-10-28 00:03:22 +020080 if (jack->jack_dirty || !jack->jack_cachable) {
81 unsigned int val = read_pin_sense(codec, jack->nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +020082 jack->jack_dirty = 0;
Takashi Iwai01a61e12011-10-28 00:03:22 +020083 if (val != jack->pin_sense) {
84 jack->need_notify = 1;
85 jack->pin_sense = val;
86 }
Takashi Iwai1835a0f2011-10-27 22:12:46 +020087 }
88}
89
90/**
91 * snd_hda_set_dirty_all - Mark all the cached as dirty
92 *
93 * This function sets the dirty flag to all entries of jack table.
94 * It's called from the resume path in hda_codec.c.
95 */
96void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
97{
98 struct hda_jack_tbl *jack = codec->jacktbl.list;
99 int i;
100
101 for (i = 0; i < codec->jacktbl.used; i++, jack++)
102 if (jack->nid)
103 jack->jack_dirty = 1;
104}
105EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
106
107/**
108 * snd_hda_pin_sense - execute pin sense measurement
109 * @codec: the CODEC to sense
110 * @nid: the pin NID to sense
111 *
112 * Execute necessary pin sense measurement and return its Presence Detect,
113 * Impedance, ELD Valid etc. status bits.
114 */
115u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
116{
117 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
118 if (jack) {
119 jack_detect_update(codec, jack);
120 return jack->pin_sense;
121 }
122 return read_pin_sense(codec, nid);
123}
124EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
125
126/**
127 * snd_hda_jack_detect - query pin Presence Detect status
128 * @codec: the CODEC to sense
129 * @nid: the pin NID to sense
130 *
131 * Query and return the pin's Presence Detect status.
132 */
133int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
134{
135 u32 sense = snd_hda_pin_sense(codec, nid);
136 return !!(sense & AC_PINSENSE_PRESENCE);
137}
138EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
139
140/**
141 * snd_hda_jack_detect_enable - enable the jack-detection
142 */
143int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
144 unsigned int tag)
145{
146 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
147 if (!jack)
148 return -ENOMEM;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200149 if (jack->jack_cachable)
150 return 0; /* already registered */
151 jack->jack_cachable = 1;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200152 return snd_hda_codec_write_cache(codec, nid, 0,
153 AC_VERB_SET_UNSOLICITED_ENABLE,
154 AC_USRSP_EN | tag);
155}
156EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200157
158/* queue the notification when needed */
159static void jack_detect_report(struct hda_codec *codec,
160 struct hda_jack_tbl *jack)
161{
162 jack_detect_update(codec, jack);
163 if (jack->need_notify) {
164 snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
165 &jack->kctl->id);
166 jack->need_notify = 0;
167 }
168}
169
170/**
171 * snd_hda_jack_report - notify kctl when the jack state was changed
172 */
173void snd_hda_jack_report(struct hda_codec *codec, hda_nid_t nid)
174{
175 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
176
177 if (jack)
178 jack_detect_report(codec, jack);
179}
180EXPORT_SYMBOL_HDA(snd_hda_jack_report);
181
182/**
183 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
184 */
185void snd_hda_jack_report_sync(struct hda_codec *codec)
186{
187 struct hda_jack_tbl *jack = codec->jacktbl.list;
188 int i;
189
190 for (i = 0; i < codec->jacktbl.used; i++, jack++)
191 if (jack->nid) {
192 jack_detect_update(codec, jack);
193 jack_detect_report(codec, jack);
194 }
195}
196EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
197
198/*
199 * jack-detection kcontrols
200 */
201
202#define jack_detect_kctl_info snd_ctl_boolean_mono_info
203
204static int jack_detect_kctl_get(struct snd_kcontrol *kcontrol,
205 struct snd_ctl_elem_value *ucontrol)
206{
207 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
208 hda_nid_t nid = kcontrol->private_value;
209
210 ucontrol->value.integer.value[0] = snd_hda_jack_detect(codec, nid);
211 return 0;
212}
213
214static struct snd_kcontrol_new jack_detect_kctl = {
215 /* name is filled later */
216 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
217 .access = SNDRV_CTL_ELEM_ACCESS_READ,
218 .info = jack_detect_kctl_info,
219 .get = jack_detect_kctl_get,
220};
221
222/**
223 * snd_hda_jack_add_kctl - Add a kctl for the given pin
224 *
225 * This assigns a jack-detection kctl to the given pin. The kcontrol
226 * will have the given name and index.
227 */
228int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
229 const char *name, int idx)
230{
231 struct hda_jack_tbl *jack;
232 struct snd_kcontrol *kctl;
233
234 jack = snd_hda_jack_tbl_get(codec, nid);
235 if (!jack)
236 return 0;
237 if (jack->kctl)
238 return 0; /* already created */
239 kctl = snd_ctl_new1(&jack_detect_kctl, codec);
240 if (!kctl)
241 return -ENOMEM;
242 snprintf(kctl->id.name, sizeof(kctl->id.name), "%s Jack", name);
243 kctl->id.index = idx;
244 kctl->private_value = nid;
245 if (snd_hda_ctl_add(codec, nid, kctl) < 0)
246 return -ENOMEM;
247 jack->kctl = kctl;
248 return 0;
249}
250
251static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, int idx,
252 const struct auto_pin_cfg *cfg)
253{
254 if (!nid)
255 return 0;
256 if (!is_jack_detectable(codec, nid))
257 return 0;
258 return snd_hda_jack_add_kctl(codec, nid,
259 snd_hda_get_pin_label(codec, nid, cfg),
260 idx);
261}
262
263/**
264 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
265 *
266 * As of now, it assigns only to the pins that enabled the detection.
267 * Usually this is called at the end of build_controls callback.
268 */
269int snd_hda_jack_add_kctls(struct hda_codec *codec,
270 const struct auto_pin_cfg *cfg)
271{
272 const hda_nid_t *p;
273 int i, err;
274
275 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
276 err = add_jack_kctl(codec, *p, i, cfg);
277 if (err < 0)
278 return err;
279 }
280 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
281 if (*p == *cfg->line_out_pins) /* might be duplicated */
282 break;
283 err = add_jack_kctl(codec, *p, i, cfg);
284 if (err < 0)
285 return err;
286 }
287 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
288 if (*p == *cfg->line_out_pins) /* might be duplicated */
289 break;
290 err = add_jack_kctl(codec, *p, i, cfg);
291 if (err < 0)
292 return err;
293 }
294 for (i = 0; i < cfg->num_inputs; i++) {
295 err = add_jack_kctl(codec, cfg->inputs[i].pin, 0, cfg);
296 if (err < 0)
297 return err;
298 }
299 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
300 err = add_jack_kctl(codec, *p, i, cfg);
301 if (err < 0)
302 return err;
303 }
304 err = add_jack_kctl(codec, cfg->dig_in_pin, 0, cfg);
305 if (err < 0)
306 return err;
307 err = add_jack_kctl(codec, cfg->mono_out_pin, 0, cfg);
308 if (err < 0)
309 return err;
310 return 0;
311}
312EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);