blob: eac002d01fd66a89b00886aa218a7d727161e0d8 [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 Iwai35be5442011-11-02 08:36:06 +0100197 state = get_jack_plug_state(jack->pin_sense);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100198 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200199 }
200}
201EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
202
Takashi Iwai01a61e12011-10-28 00:03:22 +0200203/**
204 * snd_hda_jack_add_kctl - Add a kctl for the given pin
205 *
206 * This assigns a jack-detection kctl to the given pin. The kcontrol
207 * will have the given name and index.
208 */
209int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
210 const char *name, int idx)
211{
212 struct hda_jack_tbl *jack;
213 struct snd_kcontrol *kctl;
214
Takashi Iwai3a938972011-10-28 01:16:55 +0200215 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200216 if (!jack)
217 return 0;
218 if (jack->kctl)
219 return 0; /* already created */
Takashi Iwai35be5442011-11-02 08:36:06 +0100220 kctl = snd_kctl_jack_new(name, idx, codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200221 if (!kctl)
222 return -ENOMEM;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200223 if (snd_hda_ctl_add(codec, nid, kctl) < 0)
224 return -ENOMEM;
225 jack->kctl = kctl;
226 return 0;
227}
David Henningssond1cb6202011-11-11 17:13:15 +0100228EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200229
230static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, int idx,
231 const struct auto_pin_cfg *cfg)
232{
Takashi Iwai3a938972011-10-28 01:16:55 +0200233 unsigned int def_conf, conn;
234 int err;
235
Takashi Iwai01a61e12011-10-28 00:03:22 +0200236 if (!nid)
237 return 0;
238 if (!is_jack_detectable(codec, nid))
239 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200240 def_conf = snd_hda_codec_get_pincfg(codec, nid);
241 conn = get_defcfg_connect(def_conf);
242 if (conn != AC_JACK_PORT_COMPLEX)
243 return 0;
244
245 err = snd_hda_jack_add_kctl(codec, nid,
Takashi Iwai01a61e12011-10-28 00:03:22 +0200246 snd_hda_get_pin_label(codec, nid, cfg),
247 idx);
Takashi Iwai3a938972011-10-28 01:16:55 +0200248 if (err < 0)
249 return err;
250 return snd_hda_jack_detect_enable(codec, nid, 0);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200251}
252
253/**
254 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwai01a61e12011-10-28 00:03:22 +0200255 */
256int snd_hda_jack_add_kctls(struct hda_codec *codec,
257 const struct auto_pin_cfg *cfg)
258{
259 const hda_nid_t *p;
260 int i, err;
261
262 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
263 err = add_jack_kctl(codec, *p, i, cfg);
264 if (err < 0)
265 return err;
266 }
267 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
268 if (*p == *cfg->line_out_pins) /* might be duplicated */
269 break;
270 err = add_jack_kctl(codec, *p, i, cfg);
271 if (err < 0)
272 return err;
273 }
274 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
275 if (*p == *cfg->line_out_pins) /* might be duplicated */
276 break;
277 err = add_jack_kctl(codec, *p, i, cfg);
278 if (err < 0)
279 return err;
280 }
281 for (i = 0; i < cfg->num_inputs; i++) {
282 err = add_jack_kctl(codec, cfg->inputs[i].pin, 0, cfg);
283 if (err < 0)
284 return err;
285 }
286 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
287 err = add_jack_kctl(codec, *p, i, cfg);
288 if (err < 0)
289 return err;
290 }
291 err = add_jack_kctl(codec, cfg->dig_in_pin, 0, cfg);
292 if (err < 0)
293 return err;
294 err = add_jack_kctl(codec, cfg->mono_out_pin, 0, cfg);
295 if (err < 0)
296 return err;
297 return 0;
298}
299EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100300
301#ifdef CONFIG_SND_HDA_INPUT_JACK
302/*
303 * Input-jack notification support
304 */
305static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
306 int type)
307{
308 switch (type) {
309 case SND_JACK_HEADPHONE:
310 return "Headphone";
311 case SND_JACK_MICROPHONE:
312 return "Mic";
313 case SND_JACK_LINEOUT:
314 return "Line-out";
315 case SND_JACK_LINEIN:
316 return "Line-in";
317 case SND_JACK_HEADSET:
318 return "Headset";
319 case SND_JACK_VIDEOOUT:
320 return "HDMI/DP";
321 default:
322 return "Misc";
323 }
324}
325
326static void hda_free_jack_priv(struct snd_jack *jack)
327{
328 struct hda_jack_tbl *jacks = jack->private_data;
329 jacks->nid = 0;
330 jacks->jack = NULL;
331}
332
333int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
334 const char *name)
335{
336 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
337 int err;
338
339 if (!jack)
340 return -ENOMEM;
341 if (!name)
342 name = get_jack_default_name(codec, nid, type);
343 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
344 if (err < 0)
345 return err;
346 jack->jack->private_data = jack;
347 jack->jack->private_free = hda_free_jack_priv;
348 return 0;
349}
350EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
351
352void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
353{
354 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
355 unsigned int pin_ctl;
356 unsigned int present;
357 int type;
358
359 if (!jack)
360 return;
361
362 present = snd_hda_jack_detect(codec, nid);
363 type = jack->type;
364 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
365 pin_ctl = snd_hda_codec_read(codec, nid, 0,
366 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
367 type = (pin_ctl & AC_PINCTL_HP_EN) ?
368 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
369 }
370 snd_jack_report(jack->jack, present ? type : 0);
371}
372EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
373
374/* free jack instances manually when clearing/reconfiguring */
375static void snd_hda_input_jack_free(struct hda_codec *codec)
376{
377 if (!codec->bus->shutdown && codec->jacktbl.list) {
378 struct hda_jack_tbl *jack = codec->jacktbl.list;
379 int i;
380 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
381 if (jack->jack)
382 snd_device_free(codec->bus->card, jack->jack);
383 }
384 }
385}
386#endif /* CONFIG_SND_HDA_INPUT_JACK */