blob: d68948499fbc15d7342ba0c9a63675d769b95a92 [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
Takashi Iwaia9c74172012-02-13 11:41:42 +010022bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
23{
Takashi Iwai71b1e9e2012-02-13 11:55:02 +010024 if (codec->no_jack_detect)
25 return false;
Takashi Iwaia9c74172012-02-13 11:41:42 +010026 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
27 return false;
28 if (!codec->ignore_misc_bit &&
29 (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
30 AC_DEFCFG_MISC_NO_PRESENCE))
31 return false;
32 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP))
33 return false;
34 return true;
35}
36EXPORT_SYMBOL_HDA(is_jack_detectable);
37
Takashi Iwai1835a0f2011-10-27 22:12:46 +020038/* execute pin sense measurement */
39static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
40{
41 u32 pincap;
42
43 if (!codec->no_trigger_sense) {
44 pincap = snd_hda_query_pin_caps(codec, nid);
45 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
46 snd_hda_codec_read(codec, nid, 0,
47 AC_VERB_SET_PIN_SENSE, 0);
48 }
49 return snd_hda_codec_read(codec, nid, 0,
50 AC_VERB_GET_PIN_SENSE, 0);
51}
52
53/**
54 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
55 */
56struct hda_jack_tbl *
57snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
58{
59 struct hda_jack_tbl *jack = codec->jacktbl.list;
60 int i;
61
62 if (!nid || !jack)
63 return NULL;
64 for (i = 0; i < codec->jacktbl.used; i++, jack++)
65 if (jack->nid == nid)
66 return jack;
67 return NULL;
68}
69EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
70
71/**
Takashi Iwai3a938972011-10-28 01:16:55 +020072 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
73 */
74struct hda_jack_tbl *
75snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
76{
77 struct hda_jack_tbl *jack = codec->jacktbl.list;
78 int i;
79
80 if (!tag || !jack)
81 return NULL;
82 for (i = 0; i < codec->jacktbl.used; i++, jack++)
83 if (jack->tag == tag)
84 return jack;
85 return NULL;
86}
87EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
88
89/**
Takashi Iwai1835a0f2011-10-27 22:12:46 +020090 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
91 */
92struct hda_jack_tbl *
93snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
94{
95 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
96 if (jack)
97 return jack;
98 snd_array_init(&codec->jacktbl, sizeof(*jack), 16);
99 jack = snd_array_new(&codec->jacktbl);
100 if (!jack)
101 return NULL;
102 jack->nid = nid;
103 jack->jack_dirty = 1;
Takashi Iwai3a938972011-10-28 01:16:55 +0200104 jack->tag = codec->jacktbl.used;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200105 return jack;
106}
David Henningssond1cb6202011-11-11 17:13:15 +0100107EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200108
109void snd_hda_jack_tbl_clear(struct hda_codec *codec)
110{
Takashi Iwai31ef2252011-12-01 17:41:36 +0100111#ifdef CONFIG_SND_HDA_INPUT_JACK
112 /* free jack instances manually when clearing/reconfiguring */
113 if (!codec->bus->shutdown && codec->jacktbl.list) {
114 struct hda_jack_tbl *jack = codec->jacktbl.list;
115 int i;
116 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
117 if (jack->jack)
118 snd_device_free(codec->bus->card, jack->jack);
119 }
120 }
121#endif
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200122 snd_array_free(&codec->jacktbl);
123}
124
125/* update the cached value and notification flag if needed */
126static void jack_detect_update(struct hda_codec *codec,
127 struct hda_jack_tbl *jack)
128{
Takashi Iwai3a938972011-10-28 01:16:55 +0200129 if (jack->jack_dirty || !jack->jack_detect) {
Takashi Iwai35be5442011-11-02 08:36:06 +0100130 jack->pin_sense = read_pin_sense(codec, jack->nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200131 jack->jack_dirty = 0;
132 }
133}
134
135/**
136 * snd_hda_set_dirty_all - Mark all the cached as dirty
137 *
138 * This function sets the dirty flag to all entries of jack table.
139 * It's called from the resume path in hda_codec.c.
140 */
141void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
142{
143 struct hda_jack_tbl *jack = codec->jacktbl.list;
144 int i;
145
146 for (i = 0; i < codec->jacktbl.used; i++, jack++)
147 if (jack->nid)
148 jack->jack_dirty = 1;
149}
150EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
151
152/**
153 * snd_hda_pin_sense - execute pin sense measurement
154 * @codec: the CODEC to sense
155 * @nid: the pin NID to sense
156 *
157 * Execute necessary pin sense measurement and return its Presence Detect,
158 * Impedance, ELD Valid etc. status bits.
159 */
160u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
161{
162 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
163 if (jack) {
164 jack_detect_update(codec, jack);
165 return jack->pin_sense;
166 }
167 return read_pin_sense(codec, nid);
168}
169EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
170
Takashi Iwai35be5442011-11-02 08:36:06 +0100171#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
172
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200173/**
174 * snd_hda_jack_detect - query pin Presence Detect status
175 * @codec: the CODEC to sense
176 * @nid: the pin NID to sense
177 *
178 * Query and return the pin's Presence Detect status.
179 */
180int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
181{
182 u32 sense = snd_hda_pin_sense(codec, nid);
Takashi Iwai35be5442011-11-02 08:36:06 +0100183 return get_jack_plug_state(sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200184}
185EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
186
187/**
188 * snd_hda_jack_detect_enable - enable the jack-detection
189 */
190int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3a938972011-10-28 01:16:55 +0200191 unsigned char action)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200192{
193 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
194 if (!jack)
195 return -ENOMEM;
Takashi Iwai3a938972011-10-28 01:16:55 +0200196 if (jack->jack_detect)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200197 return 0; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200198 jack->jack_detect = 1;
199 if (action)
200 jack->action = action;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200201 return snd_hda_codec_write_cache(codec, nid, 0,
202 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200203 AC_USRSP_EN | jack->tag);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200204}
205EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200206
Takashi Iwai01a61e12011-10-28 00:03:22 +0200207/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200208 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
209 */
210void snd_hda_jack_report_sync(struct hda_codec *codec)
211{
212 struct hda_jack_tbl *jack = codec->jacktbl.list;
Takashi Iwai35be5442011-11-02 08:36:06 +0100213 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200214
215 for (i = 0; i < codec->jacktbl.used; i++, jack++)
216 if (jack->nid) {
217 jack_detect_update(codec, jack);
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100218 if (!jack->kctl)
219 continue;
Takashi Iwai35be5442011-11-02 08:36:06 +0100220 state = get_jack_plug_state(jack->pin_sense);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100221 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
Takashi Iwai31ef2252011-12-01 17:41:36 +0100222#ifdef CONFIG_SND_HDA_INPUT_JACK
223 if (jack->jack)
224 snd_jack_report(jack->jack,
225 state ? jack->type : 0);
226#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200227 }
228}
229EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
230
Takashi Iwai31ef2252011-12-01 17:41:36 +0100231#ifdef CONFIG_SND_HDA_INPUT_JACK
232/* guess the jack type from the pin-config */
233static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
234{
235 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
236 switch (get_defcfg_device(def_conf)) {
237 case AC_JACK_LINE_OUT:
238 case AC_JACK_SPEAKER:
239 return SND_JACK_LINEOUT;
240 case AC_JACK_HP_OUT:
241 return SND_JACK_HEADPHONE;
242 case AC_JACK_SPDIF_OUT:
243 case AC_JACK_DIG_OTHER_OUT:
244 return SND_JACK_AVOUT;
245 case AC_JACK_MIC_IN:
246 return SND_JACK_MICROPHONE;
247 default:
248 return SND_JACK_LINEIN;
249 }
250}
251
252static void hda_free_jack_priv(struct snd_jack *jack)
253{
254 struct hda_jack_tbl *jacks = jack->private_data;
255 jacks->nid = 0;
256 jacks->jack = NULL;
257}
258#endif
259
Takashi Iwai01a61e12011-10-28 00:03:22 +0200260/**
261 * snd_hda_jack_add_kctl - Add a kctl for the given pin
262 *
263 * This assigns a jack-detection kctl to the given pin. The kcontrol
264 * will have the given name and index.
265 */
266int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
267 const char *name, int idx)
268{
269 struct hda_jack_tbl *jack;
270 struct snd_kcontrol *kctl;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100271 int err, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200272
Takashi Iwai3a938972011-10-28 01:16:55 +0200273 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200274 if (!jack)
275 return 0;
276 if (jack->kctl)
277 return 0; /* already created */
Takashi Iwai35be5442011-11-02 08:36:06 +0100278 kctl = snd_kctl_jack_new(name, idx, codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200279 if (!kctl)
280 return -ENOMEM;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100281 err = snd_hda_ctl_add(codec, nid, kctl);
282 if (err < 0)
283 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200284 jack->kctl = kctl;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100285 state = snd_hda_jack_detect(codec, nid);
286 snd_kctl_jack_report(codec->bus->card, kctl, state);
287#ifdef CONFIG_SND_HDA_INPUT_JACK
288 jack->type = get_input_jack_type(codec, nid);
289 err = snd_jack_new(codec->bus->card, name, jack->type, &jack->jack);
290 if (err < 0)
291 return err;
292 jack->jack->private_data = jack;
293 jack->jack->private_free = hda_free_jack_priv;
294 snd_jack_report(jack->jack, state ? jack->type : 0);
295#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200296 return 0;
297}
David Henningssond1cb6202011-11-11 17:13:15 +0100298EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200299
Takashi Iwai201e06f2011-11-16 15:33:26 +0100300static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
David Henningsson05c3b362012-01-31 09:04:15 +0100301 const struct auto_pin_cfg *cfg,
302 char *lastname, int *lastidx)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200303{
Takashi Iwai3a938972011-10-28 01:16:55 +0200304 unsigned int def_conf, conn;
Takashi Iwai201e06f2011-11-16 15:33:26 +0100305 char name[44];
306 int idx, err;
Takashi Iwai3a938972011-10-28 01:16:55 +0200307
Takashi Iwai01a61e12011-10-28 00:03:22 +0200308 if (!nid)
309 return 0;
310 if (!is_jack_detectable(codec, nid))
311 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200312 def_conf = snd_hda_codec_get_pincfg(codec, nid);
313 conn = get_defcfg_connect(def_conf);
314 if (conn != AC_JACK_PORT_COMPLEX)
315 return 0;
316
Takashi Iwai201e06f2011-11-16 15:33:26 +0100317 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
David Henningsson05c3b362012-01-31 09:04:15 +0100318 if (!strcmp(name, lastname) && idx == *lastidx)
319 idx++;
320 strncpy(lastname, name, 44);
321 *lastidx = idx;
Takashi Iwai201e06f2011-11-16 15:33:26 +0100322 err = snd_hda_jack_add_kctl(codec, nid, name, idx);
Takashi Iwai3a938972011-10-28 01:16:55 +0200323 if (err < 0)
324 return err;
325 return snd_hda_jack_detect_enable(codec, nid, 0);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200326}
327
328/**
329 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwai01a61e12011-10-28 00:03:22 +0200330 */
331int snd_hda_jack_add_kctls(struct hda_codec *codec,
332 const struct auto_pin_cfg *cfg)
333{
334 const hda_nid_t *p;
David Henningsson05c3b362012-01-31 09:04:15 +0100335 int i, err, lastidx = 0;
336 char lastname[44] = "";
Takashi Iwai01a61e12011-10-28 00:03:22 +0200337
338 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
David Henningsson05c3b362012-01-31 09:04:15 +0100339 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200340 if (err < 0)
341 return err;
342 }
343 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
344 if (*p == *cfg->line_out_pins) /* might be duplicated */
345 break;
David Henningsson05c3b362012-01-31 09:04:15 +0100346 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200347 if (err < 0)
348 return err;
349 }
350 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
351 if (*p == *cfg->line_out_pins) /* might be duplicated */
352 break;
David Henningsson05c3b362012-01-31 09:04:15 +0100353 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200354 if (err < 0)
355 return err;
356 }
357 for (i = 0; i < cfg->num_inputs; i++) {
David Henningsson05c3b362012-01-31 09:04:15 +0100358 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, lastname, &lastidx);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200359 if (err < 0)
360 return err;
361 }
362 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
David Henningsson05c3b362012-01-31 09:04:15 +0100363 err = add_jack_kctl(codec, *p, cfg, lastname, &lastidx);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200364 if (err < 0)
365 return err;
366 }
David Henningsson05c3b362012-01-31 09:04:15 +0100367 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, lastname, &lastidx);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200368 if (err < 0)
369 return err;
David Henningsson05c3b362012-01-31 09:04:15 +0100370 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, lastname, &lastidx);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200371 if (err < 0)
372 return err;
373 return 0;
374}
375EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);