blob: 25f756533bec749d49b689cb3b13dfd058e64dba [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>
Takashi Iwaibf815bf2011-11-16 14:28:33 +010014#include <linux/export.h>
Takashi Iwai1835a0f2011-10-27 22:12:46 +020015#include <sound/core.h>
Takashi Iwai01a61e12011-10-28 00:03:22 +020016#include <sound/control.h>
Takashi Iwaiaad37db2011-11-02 08:54:51 +010017#include <sound/jack.h>
Takashi Iwai1835a0f2011-10-27 22:12:46 +020018#include "hda_codec.h"
19#include "hda_local.h"
20#include "hda_jack.h"
21
22/* execute pin sense measurement */
23static 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 */
40struct hda_jack_tbl *
41snd_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}
53EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
54
55/**
Takashi Iwai3a938972011-10-28 01:16:55 +020056 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
57 */
58struct hda_jack_tbl *
59snd_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}
71EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
72
73/**
Takashi Iwai1835a0f2011-10-27 22:12:46 +020074 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
75 */
76struct hda_jack_tbl *
77snd_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;
Takashi Iwai3a938972011-10-28 01:16:55 +020088 jack->tag = codec->jacktbl.used;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020089 return jack;
90}
David Henningssond1cb6202011-11-11 17:13:15 +010091EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new);
Takashi Iwai1835a0f2011-10-27 22:12:46 +020092
Takashi Iwaiaad37db2011-11-02 08:54:51 +010093#ifdef CONFIG_SND_HDA_INPUT_JACK
94static void snd_hda_input_jack_free(struct hda_codec *codec);
95#else
96#define snd_hda_input_jack_free(codec)
97#endif
98
Takashi Iwai1835a0f2011-10-27 22:12:46 +020099void snd_hda_jack_tbl_clear(struct hda_codec *codec)
100{
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100101 snd_hda_input_jack_free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200102 snd_array_free(&codec->jacktbl);
103}
104
105/* update the cached value and notification flag if needed */
106static void jack_detect_update(struct hda_codec *codec,
107 struct hda_jack_tbl *jack)
108{
Takashi Iwai3a938972011-10-28 01:16:55 +0200109 if (jack->jack_dirty || !jack->jack_detect) {
Takashi Iwai35be5442011-11-02 08:36:06 +0100110 jack->pin_sense = read_pin_sense(codec, jack->nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200111 jack->jack_dirty = 0;
112 }
113}
114
115/**
116 * snd_hda_set_dirty_all - Mark all the cached as dirty
117 *
118 * This function sets the dirty flag to all entries of jack table.
119 * It's called from the resume path in hda_codec.c.
120 */
121void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
122{
123 struct hda_jack_tbl *jack = codec->jacktbl.list;
124 int i;
125
126 for (i = 0; i < codec->jacktbl.used; i++, jack++)
127 if (jack->nid)
128 jack->jack_dirty = 1;
129}
130EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
131
132/**
133 * snd_hda_pin_sense - execute pin sense measurement
134 * @codec: the CODEC to sense
135 * @nid: the pin NID to sense
136 *
137 * Execute necessary pin sense measurement and return its Presence Detect,
138 * Impedance, ELD Valid etc. status bits.
139 */
140u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
141{
142 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
143 if (jack) {
144 jack_detect_update(codec, jack);
145 return jack->pin_sense;
146 }
147 return read_pin_sense(codec, nid);
148}
149EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
150
Takashi Iwai35be5442011-11-02 08:36:06 +0100151#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
152
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200153/**
154 * snd_hda_jack_detect - query pin Presence Detect status
155 * @codec: the CODEC to sense
156 * @nid: the pin NID to sense
157 *
158 * Query and return the pin's Presence Detect status.
159 */
160int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
161{
162 u32 sense = snd_hda_pin_sense(codec, nid);
Takashi Iwai35be5442011-11-02 08:36:06 +0100163 return get_jack_plug_state(sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200164}
165EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
166
167/**
168 * snd_hda_jack_detect_enable - enable the jack-detection
169 */
170int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3a938972011-10-28 01:16:55 +0200171 unsigned char action)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200172{
173 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
174 if (!jack)
175 return -ENOMEM;
Takashi Iwai3a938972011-10-28 01:16:55 +0200176 if (jack->jack_detect)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200177 return 0; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200178 jack->jack_detect = 1;
179 if (action)
180 jack->action = action;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200181 return snd_hda_codec_write_cache(codec, nid, 0,
182 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200183 AC_USRSP_EN | jack->tag);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200184}
185EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200186
Takashi Iwai01a61e12011-10-28 00:03:22 +0200187/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200188 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
189 */
190void snd_hda_jack_report_sync(struct hda_codec *codec)
191{
192 struct hda_jack_tbl *jack = codec->jacktbl.list;
Takashi Iwai35be5442011-11-02 08:36:06 +0100193 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200194
195 for (i = 0; i < codec->jacktbl.used; i++, jack++)
196 if (jack->nid) {
197 jack_detect_update(codec, jack);
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100198 if (!jack->kctl)
199 continue;
Takashi Iwai35be5442011-11-02 08:36:06 +0100200 state = get_jack_plug_state(jack->pin_sense);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100201 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200202 }
203}
204EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
205
Takashi Iwai01a61e12011-10-28 00:03:22 +0200206/**
207 * snd_hda_jack_add_kctl - Add a kctl for the given pin
208 *
209 * This assigns a jack-detection kctl to the given pin. The kcontrol
210 * will have the given name and index.
211 */
212int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
213 const char *name, int idx)
214{
215 struct hda_jack_tbl *jack;
216 struct snd_kcontrol *kctl;
217
Takashi Iwai3a938972011-10-28 01:16:55 +0200218 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200219 if (!jack)
220 return 0;
221 if (jack->kctl)
222 return 0; /* already created */
Takashi Iwai35be5442011-11-02 08:36:06 +0100223 kctl = snd_kctl_jack_new(name, idx, codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200224 if (!kctl)
225 return -ENOMEM;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200226 if (snd_hda_ctl_add(codec, nid, kctl) < 0)
227 return -ENOMEM;
228 jack->kctl = kctl;
Takashi Iwaifc5b15f2011-11-14 10:32:21 +0100229 snd_kctl_jack_report(codec->bus->card, kctl,
230 snd_hda_jack_detect(codec, nid));
Takashi Iwai01a61e12011-10-28 00:03:22 +0200231 return 0;
232}
David Henningssond1cb6202011-11-11 17:13:15 +0100233EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200234
235static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, int idx,
236 const struct auto_pin_cfg *cfg)
237{
Takashi Iwai3a938972011-10-28 01:16:55 +0200238 unsigned int def_conf, conn;
239 int err;
240
Takashi Iwai01a61e12011-10-28 00:03:22 +0200241 if (!nid)
242 return 0;
243 if (!is_jack_detectable(codec, nid))
244 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200245 def_conf = snd_hda_codec_get_pincfg(codec, nid);
246 conn = get_defcfg_connect(def_conf);
247 if (conn != AC_JACK_PORT_COMPLEX)
248 return 0;
249
250 err = snd_hda_jack_add_kctl(codec, nid,
Takashi Iwai01a61e12011-10-28 00:03:22 +0200251 snd_hda_get_pin_label(codec, nid, cfg),
252 idx);
Takashi Iwai3a938972011-10-28 01:16:55 +0200253 if (err < 0)
254 return err;
255 return snd_hda_jack_detect_enable(codec, nid, 0);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200256}
257
258/**
259 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwai01a61e12011-10-28 00:03:22 +0200260 */
261int snd_hda_jack_add_kctls(struct hda_codec *codec,
262 const struct auto_pin_cfg *cfg)
263{
264 const hda_nid_t *p;
265 int i, err;
266
267 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
268 err = add_jack_kctl(codec, *p, i, cfg);
269 if (err < 0)
270 return err;
271 }
272 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
273 if (*p == *cfg->line_out_pins) /* might be duplicated */
274 break;
275 err = add_jack_kctl(codec, *p, i, cfg);
276 if (err < 0)
277 return err;
278 }
279 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
280 if (*p == *cfg->line_out_pins) /* might be duplicated */
281 break;
282 err = add_jack_kctl(codec, *p, i, cfg);
283 if (err < 0)
284 return err;
285 }
286 for (i = 0; i < cfg->num_inputs; i++) {
287 err = add_jack_kctl(codec, cfg->inputs[i].pin, 0, cfg);
288 if (err < 0)
289 return err;
290 }
291 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
292 err = add_jack_kctl(codec, *p, i, cfg);
293 if (err < 0)
294 return err;
295 }
296 err = add_jack_kctl(codec, cfg->dig_in_pin, 0, cfg);
297 if (err < 0)
298 return err;
299 err = add_jack_kctl(codec, cfg->mono_out_pin, 0, cfg);
300 if (err < 0)
301 return err;
302 return 0;
303}
304EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100305
306#ifdef CONFIG_SND_HDA_INPUT_JACK
307/*
308 * Input-jack notification support
309 */
310static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
311 int type)
312{
313 switch (type) {
314 case SND_JACK_HEADPHONE:
315 return "Headphone";
316 case SND_JACK_MICROPHONE:
317 return "Mic";
318 case SND_JACK_LINEOUT:
319 return "Line-out";
320 case SND_JACK_LINEIN:
321 return "Line-in";
322 case SND_JACK_HEADSET:
323 return "Headset";
324 case SND_JACK_VIDEOOUT:
325 return "HDMI/DP";
326 default:
327 return "Misc";
328 }
329}
330
331static void hda_free_jack_priv(struct snd_jack *jack)
332{
333 struct hda_jack_tbl *jacks = jack->private_data;
334 jacks->nid = 0;
335 jacks->jack = NULL;
336}
337
338int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
339 const char *name)
340{
341 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
342 int err;
343
344 if (!jack)
345 return -ENOMEM;
346 if (!name)
347 name = get_jack_default_name(codec, nid, type);
348 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
349 if (err < 0)
350 return err;
Takashi Iwai344b01a2011-11-11 17:54:19 +0100351 jack->type = type;
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100352 jack->jack->private_data = jack;
353 jack->jack->private_free = hda_free_jack_priv;
354 return 0;
355}
356EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
357
358void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
359{
360 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
361 unsigned int pin_ctl;
362 unsigned int present;
363 int type;
364
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100365 if (!jack || !jack->jack)
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100366 return;
367
368 present = snd_hda_jack_detect(codec, nid);
369 type = jack->type;
370 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
371 pin_ctl = snd_hda_codec_read(codec, nid, 0,
372 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
373 type = (pin_ctl & AC_PINCTL_HP_EN) ?
374 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
375 }
376 snd_jack_report(jack->jack, present ? type : 0);
377}
378EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
379
380/* free jack instances manually when clearing/reconfiguring */
381static void snd_hda_input_jack_free(struct hda_codec *codec)
382{
383 if (!codec->bus->shutdown && codec->jacktbl.list) {
384 struct hda_jack_tbl *jack = codec->jacktbl.list;
385 int i;
386 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
387 if (jack->jack)
388 snd_device_free(codec->bus->card, jack->jack);
389 }
390 }
391}
392#endif /* CONFIG_SND_HDA_INPUT_JACK */