blob: e014562edc09a58b1b03d2857e1293961b298053 [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 Iwaiaad37db2011-11-02 08:54:51 +010016#include <sound/jack.h>
Takashi Iwai1835a0f2011-10-27 22:12:46 +020017#include "hda_codec.h"
18#include "hda_local.h"
19#include "hda_jack.h"
20
21/* execute pin sense measurement */
22static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
23{
24 u32 pincap;
25
26 if (!codec->no_trigger_sense) {
27 pincap = snd_hda_query_pin_caps(codec, nid);
28 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
29 snd_hda_codec_read(codec, nid, 0,
30 AC_VERB_SET_PIN_SENSE, 0);
31 }
32 return snd_hda_codec_read(codec, nid, 0,
33 AC_VERB_GET_PIN_SENSE, 0);
34}
35
36/**
37 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
38 */
39struct hda_jack_tbl *
40snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
41{
42 struct hda_jack_tbl *jack = codec->jacktbl.list;
43 int i;
44
45 if (!nid || !jack)
46 return NULL;
47 for (i = 0; i < codec->jacktbl.used; i++, jack++)
48 if (jack->nid == nid)
49 return jack;
50 return NULL;
51}
52EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
53
54/**
Takashi Iwai3a938972011-10-28 01:16:55 +020055 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
56 */
57struct hda_jack_tbl *
58snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
59{
60 struct hda_jack_tbl *jack = codec->jacktbl.list;
61 int i;
62
63 if (!tag || !jack)
64 return NULL;
65 for (i = 0; i < codec->jacktbl.used; i++, jack++)
66 if (jack->tag == tag)
67 return jack;
68 return NULL;
69}
70EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
71
72/**
Takashi Iwai1835a0f2011-10-27 22:12:46 +020073 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
74 */
75struct hda_jack_tbl *
76snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
77{
78 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
79 if (jack)
80 return jack;
81 snd_array_init(&codec->jacktbl, sizeof(*jack), 16);
82 jack = snd_array_new(&codec->jacktbl);
83 if (!jack)
84 return NULL;
85 jack->nid = nid;
86 jack->jack_dirty = 1;
Takashi Iwai3a938972011-10-28 01:16:55 +020087 jack->tag = codec->jacktbl.used;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020088 return jack;
89}
David Henningssond1cb6202011-11-11 17:13:15 +010090EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new);
Takashi Iwai1835a0f2011-10-27 22:12:46 +020091
Takashi Iwaiaad37db2011-11-02 08:54:51 +010092#ifdef CONFIG_SND_HDA_INPUT_JACK
93static void snd_hda_input_jack_free(struct hda_codec *codec);
94#else
95#define snd_hda_input_jack_free(codec)
96#endif
97
Takashi Iwai1835a0f2011-10-27 22:12:46 +020098void snd_hda_jack_tbl_clear(struct hda_codec *codec)
99{
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100100 snd_hda_input_jack_free(codec);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200101 snd_array_free(&codec->jacktbl);
102}
103
104/* update the cached value and notification flag if needed */
105static void jack_detect_update(struct hda_codec *codec,
106 struct hda_jack_tbl *jack)
107{
Takashi Iwai3a938972011-10-28 01:16:55 +0200108 if (jack->jack_dirty || !jack->jack_detect) {
Takashi Iwai35be5442011-11-02 08:36:06 +0100109 jack->pin_sense = read_pin_sense(codec, jack->nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200110 jack->jack_dirty = 0;
111 }
112}
113
114/**
115 * snd_hda_set_dirty_all - Mark all the cached as dirty
116 *
117 * This function sets the dirty flag to all entries of jack table.
118 * It's called from the resume path in hda_codec.c.
119 */
120void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
121{
122 struct hda_jack_tbl *jack = codec->jacktbl.list;
123 int i;
124
125 for (i = 0; i < codec->jacktbl.used; i++, jack++)
126 if (jack->nid)
127 jack->jack_dirty = 1;
128}
129EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
130
131/**
132 * snd_hda_pin_sense - execute pin sense measurement
133 * @codec: the CODEC to sense
134 * @nid: the pin NID to sense
135 *
136 * Execute necessary pin sense measurement and return its Presence Detect,
137 * Impedance, ELD Valid etc. status bits.
138 */
139u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
140{
141 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
142 if (jack) {
143 jack_detect_update(codec, jack);
144 return jack->pin_sense;
145 }
146 return read_pin_sense(codec, nid);
147}
148EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
149
Takashi Iwai35be5442011-11-02 08:36:06 +0100150#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
151
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200152/**
153 * snd_hda_jack_detect - query pin Presence Detect status
154 * @codec: the CODEC to sense
155 * @nid: the pin NID to sense
156 *
157 * Query and return the pin's Presence Detect status.
158 */
159int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
160{
161 u32 sense = snd_hda_pin_sense(codec, nid);
Takashi Iwai35be5442011-11-02 08:36:06 +0100162 return get_jack_plug_state(sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200163}
164EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
165
166/**
167 * snd_hda_jack_detect_enable - enable the jack-detection
168 */
169int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3a938972011-10-28 01:16:55 +0200170 unsigned char action)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200171{
172 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
173 if (!jack)
174 return -ENOMEM;
Takashi Iwai3a938972011-10-28 01:16:55 +0200175 if (jack->jack_detect)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200176 return 0; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200177 jack->jack_detect = 1;
178 if (action)
179 jack->action = action;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200180 return snd_hda_codec_write_cache(codec, nid, 0,
181 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200182 AC_USRSP_EN | jack->tag);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200183}
184EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200185
Takashi Iwai01a61e12011-10-28 00:03:22 +0200186/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200187 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
188 */
189void snd_hda_jack_report_sync(struct hda_codec *codec)
190{
191 struct hda_jack_tbl *jack = codec->jacktbl.list;
Takashi Iwai35be5442011-11-02 08:36:06 +0100192 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200193
194 for (i = 0; i < codec->jacktbl.used; i++, jack++)
195 if (jack->nid) {
196 jack_detect_update(codec, jack);
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100197 if (!jack->kctl)
198 continue;
Takashi Iwai35be5442011-11-02 08:36:06 +0100199 state = get_jack_plug_state(jack->pin_sense);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100200 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200201 }
202}
203EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
204
Takashi Iwai01a61e12011-10-28 00:03:22 +0200205/**
206 * snd_hda_jack_add_kctl - Add a kctl for the given pin
207 *
208 * This assigns a jack-detection kctl to the given pin. The kcontrol
209 * will have the given name and index.
210 */
211int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
212 const char *name, int idx)
213{
214 struct hda_jack_tbl *jack;
215 struct snd_kcontrol *kctl;
216
Takashi Iwai3a938972011-10-28 01:16:55 +0200217 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200218 if (!jack)
219 return 0;
220 if (jack->kctl)
221 return 0; /* already created */
Takashi Iwai35be5442011-11-02 08:36:06 +0100222 kctl = snd_kctl_jack_new(name, idx, codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200223 if (!kctl)
224 return -ENOMEM;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200225 if (snd_hda_ctl_add(codec, nid, kctl) < 0)
226 return -ENOMEM;
227 jack->kctl = kctl;
Takashi Iwaifc5b15f2011-11-14 10:32:21 +0100228 snd_kctl_jack_report(codec->bus->card, kctl,
229 snd_hda_jack_detect(codec, nid));
Takashi Iwai01a61e12011-10-28 00:03:22 +0200230 return 0;
231}
David Henningssond1cb6202011-11-11 17:13:15 +0100232EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200233
234static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, int idx,
235 const struct auto_pin_cfg *cfg)
236{
Takashi Iwai3a938972011-10-28 01:16:55 +0200237 unsigned int def_conf, conn;
238 int err;
239
Takashi Iwai01a61e12011-10-28 00:03:22 +0200240 if (!nid)
241 return 0;
242 if (!is_jack_detectable(codec, nid))
243 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200244 def_conf = snd_hda_codec_get_pincfg(codec, nid);
245 conn = get_defcfg_connect(def_conf);
246 if (conn != AC_JACK_PORT_COMPLEX)
247 return 0;
248
249 err = snd_hda_jack_add_kctl(codec, nid,
Takashi Iwai01a61e12011-10-28 00:03:22 +0200250 snd_hda_get_pin_label(codec, nid, cfg),
251 idx);
Takashi Iwai3a938972011-10-28 01:16:55 +0200252 if (err < 0)
253 return err;
254 return snd_hda_jack_detect_enable(codec, nid, 0);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200255}
256
257/**
258 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwai01a61e12011-10-28 00:03:22 +0200259 */
260int snd_hda_jack_add_kctls(struct hda_codec *codec,
261 const struct auto_pin_cfg *cfg)
262{
263 const hda_nid_t *p;
264 int i, err;
265
266 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
267 err = add_jack_kctl(codec, *p, i, cfg);
268 if (err < 0)
269 return err;
270 }
271 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
272 if (*p == *cfg->line_out_pins) /* might be duplicated */
273 break;
274 err = add_jack_kctl(codec, *p, i, cfg);
275 if (err < 0)
276 return err;
277 }
278 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
279 if (*p == *cfg->line_out_pins) /* might be duplicated */
280 break;
281 err = add_jack_kctl(codec, *p, i, cfg);
282 if (err < 0)
283 return err;
284 }
285 for (i = 0; i < cfg->num_inputs; i++) {
286 err = add_jack_kctl(codec, cfg->inputs[i].pin, 0, cfg);
287 if (err < 0)
288 return err;
289 }
290 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
291 err = add_jack_kctl(codec, *p, i, cfg);
292 if (err < 0)
293 return err;
294 }
295 err = add_jack_kctl(codec, cfg->dig_in_pin, 0, cfg);
296 if (err < 0)
297 return err;
298 err = add_jack_kctl(codec, cfg->mono_out_pin, 0, cfg);
299 if (err < 0)
300 return err;
301 return 0;
302}
303EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100304
305#ifdef CONFIG_SND_HDA_INPUT_JACK
306/*
307 * Input-jack notification support
308 */
309static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
310 int type)
311{
312 switch (type) {
313 case SND_JACK_HEADPHONE:
314 return "Headphone";
315 case SND_JACK_MICROPHONE:
316 return "Mic";
317 case SND_JACK_LINEOUT:
318 return "Line-out";
319 case SND_JACK_LINEIN:
320 return "Line-in";
321 case SND_JACK_HEADSET:
322 return "Headset";
323 case SND_JACK_VIDEOOUT:
324 return "HDMI/DP";
325 default:
326 return "Misc";
327 }
328}
329
330static void hda_free_jack_priv(struct snd_jack *jack)
331{
332 struct hda_jack_tbl *jacks = jack->private_data;
333 jacks->nid = 0;
334 jacks->jack = NULL;
335}
336
337int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
338 const char *name)
339{
340 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
341 int err;
342
343 if (!jack)
344 return -ENOMEM;
345 if (!name)
346 name = get_jack_default_name(codec, nid, type);
347 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
348 if (err < 0)
349 return err;
Takashi Iwai344b01a2011-11-11 17:54:19 +0100350 jack->type = type;
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100351 jack->jack->private_data = jack;
352 jack->jack->private_free = hda_free_jack_priv;
353 return 0;
354}
355EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
356
357void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
358{
359 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
360 unsigned int pin_ctl;
361 unsigned int present;
362 int type;
363
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100364 if (!jack || !jack->jack)
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100365 return;
366
367 present = snd_hda_jack_detect(codec, nid);
368 type = jack->type;
369 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
370 pin_ctl = snd_hda_codec_read(codec, nid, 0,
371 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
372 type = (pin_ctl & AC_PINCTL_HP_EN) ?
373 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
374 }
375 snd_jack_report(jack->jack, present ? type : 0);
376}
377EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
378
379/* free jack instances manually when clearing/reconfiguring */
380static void snd_hda_input_jack_free(struct hda_codec *codec)
381{
382 if (!codec->bus->shutdown && codec->jacktbl.list) {
383 struct hda_jack_tbl *jack = codec->jacktbl.list;
384 int i;
385 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
386 if (jack->jack)
387 snd_device_free(codec->bus->card, jack->jack);
388 }
389 }
390}
391#endif /* CONFIG_SND_HDA_INPUT_JACK */