blob: 5c690cb873d469af2b419476fb790e893c2433a2 [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"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020020#include "hda_auto_parser.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020021#include "hda_jack.h"
22
Takashi Iwaia9c74172012-02-13 11:41:42 +010023bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
24{
Takashi Iwai71b1e9e2012-02-13 11:55:02 +010025 if (codec->no_jack_detect)
26 return false;
Takashi Iwaia9c74172012-02-13 11:41:42 +010027 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
28 return false;
David Henningsson5fe8e1e2012-09-07 07:25:44 +020029 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
30 AC_DEFCFG_MISC_NO_PRESENCE)
Takashi Iwaia9c74172012-02-13 11:41:42 +010031 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{
David Henningsson80c8bfb2012-06-04 09:33:51 +0200129 if (!jack->jack_dirty)
130 return;
131
132 if (jack->phantom_jack)
133 jack->pin_sense = AC_PINSENSE_PRESENCE;
134 else
Takashi Iwai35be5442011-11-02 08:36:06 +0100135 jack->pin_sense = read_pin_sense(codec, jack->nid);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200136
137 jack->jack_dirty = 0;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200138}
139
140/**
141 * snd_hda_set_dirty_all - Mark all the cached as dirty
142 *
143 * This function sets the dirty flag to all entries of jack table.
144 * It's called from the resume path in hda_codec.c.
145 */
146void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
147{
148 struct hda_jack_tbl *jack = codec->jacktbl.list;
149 int i;
150
151 for (i = 0; i < codec->jacktbl.used; i++, jack++)
152 if (jack->nid)
153 jack->jack_dirty = 1;
154}
155EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
156
157/**
158 * snd_hda_pin_sense - execute pin sense measurement
159 * @codec: the CODEC to sense
160 * @nid: the pin NID to sense
161 *
162 * Execute necessary pin sense measurement and return its Presence Detect,
163 * Impedance, ELD Valid etc. status bits.
164 */
165u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
166{
167 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
168 if (jack) {
169 jack_detect_update(codec, jack);
170 return jack->pin_sense;
171 }
172 return read_pin_sense(codec, nid);
173}
174EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
175
Takashi Iwai35be5442011-11-02 08:36:06 +0100176#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
177
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200178/**
179 * snd_hda_jack_detect - query pin Presence Detect status
180 * @codec: the CODEC to sense
181 * @nid: the pin NID to sense
182 *
183 * Query and return the pin's Presence Detect status.
184 */
185int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
186{
187 u32 sense = snd_hda_pin_sense(codec, nid);
Takashi Iwai35be5442011-11-02 08:36:06 +0100188 return get_jack_plug_state(sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200189}
190EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
191
192/**
193 * snd_hda_jack_detect_enable - enable the jack-detection
194 */
David Henningsson954df2a2012-09-25 11:30:59 +0200195int snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
196 unsigned char action,
197 hda_jack_callback cb)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200198{
199 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
200 if (!jack)
201 return -ENOMEM;
Takashi Iwai3a938972011-10-28 01:16:55 +0200202 if (jack->jack_detect)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200203 return 0; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200204 jack->jack_detect = 1;
205 if (action)
206 jack->action = action;
David Henningsson954df2a2012-09-25 11:30:59 +0200207 if (cb)
208 jack->callback = cb;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200209 return snd_hda_codec_write_cache(codec, nid, 0,
210 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200211 AC_USRSP_EN | jack->tag);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200212}
David Henningsson954df2a2012-09-25 11:30:59 +0200213EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable_callback);
214
215int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
216 unsigned char action)
217{
218 return snd_hda_jack_detect_enable_callback(codec, nid, action, NULL);
219}
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200220EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200221
Takashi Iwai01a61e12011-10-28 00:03:22 +0200222/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200223 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
224 */
225void snd_hda_jack_report_sync(struct hda_codec *codec)
226{
227 struct hda_jack_tbl *jack = codec->jacktbl.list;
Takashi Iwai35be5442011-11-02 08:36:06 +0100228 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200229
230 for (i = 0; i < codec->jacktbl.used; i++, jack++)
231 if (jack->nid) {
232 jack_detect_update(codec, jack);
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100233 if (!jack->kctl)
234 continue;
Takashi Iwai35be5442011-11-02 08:36:06 +0100235 state = get_jack_plug_state(jack->pin_sense);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100236 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
Takashi Iwai31ef2252011-12-01 17:41:36 +0100237#ifdef CONFIG_SND_HDA_INPUT_JACK
238 if (jack->jack)
239 snd_jack_report(jack->jack,
240 state ? jack->type : 0);
241#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200242 }
243}
244EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
245
Takashi Iwai31ef2252011-12-01 17:41:36 +0100246#ifdef CONFIG_SND_HDA_INPUT_JACK
247/* guess the jack type from the pin-config */
248static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
249{
250 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
251 switch (get_defcfg_device(def_conf)) {
252 case AC_JACK_LINE_OUT:
253 case AC_JACK_SPEAKER:
254 return SND_JACK_LINEOUT;
255 case AC_JACK_HP_OUT:
256 return SND_JACK_HEADPHONE;
257 case AC_JACK_SPDIF_OUT:
258 case AC_JACK_DIG_OTHER_OUT:
259 return SND_JACK_AVOUT;
260 case AC_JACK_MIC_IN:
261 return SND_JACK_MICROPHONE;
262 default:
263 return SND_JACK_LINEIN;
264 }
265}
266
267static void hda_free_jack_priv(struct snd_jack *jack)
268{
269 struct hda_jack_tbl *jacks = jack->private_data;
270 jacks->nid = 0;
271 jacks->jack = NULL;
272}
273#endif
274
Takashi Iwai01a61e12011-10-28 00:03:22 +0200275/**
276 * snd_hda_jack_add_kctl - Add a kctl for the given pin
277 *
278 * This assigns a jack-detection kctl to the given pin. The kcontrol
279 * will have the given name and index.
280 */
David Henningsson80c8bfb2012-06-04 09:33:51 +0200281static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
282 const char *name, int idx, bool phantom_jack)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200283{
284 struct hda_jack_tbl *jack;
285 struct snd_kcontrol *kctl;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100286 int err, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200287
Takashi Iwai3a938972011-10-28 01:16:55 +0200288 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200289 if (!jack)
290 return 0;
291 if (jack->kctl)
292 return 0; /* already created */
Takashi Iwai35be5442011-11-02 08:36:06 +0100293 kctl = snd_kctl_jack_new(name, idx, codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200294 if (!kctl)
295 return -ENOMEM;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100296 err = snd_hda_ctl_add(codec, nid, kctl);
297 if (err < 0)
298 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200299 jack->kctl = kctl;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200300 jack->phantom_jack = !!phantom_jack;
301
Takashi Iwai31ef2252011-12-01 17:41:36 +0100302 state = snd_hda_jack_detect(codec, nid);
303 snd_kctl_jack_report(codec->bus->card, kctl, state);
304#ifdef CONFIG_SND_HDA_INPUT_JACK
David Henningsson80c8bfb2012-06-04 09:33:51 +0200305 if (!phantom_jack) {
306 jack->type = get_input_jack_type(codec, nid);
307 err = snd_jack_new(codec->bus->card, name, jack->type,
308 &jack->jack);
309 if (err < 0)
310 return err;
311 jack->jack->private_data = jack;
312 jack->jack->private_free = hda_free_jack_priv;
313 snd_jack_report(jack->jack, state ? jack->type : 0);
314 }
Takashi Iwai31ef2252011-12-01 17:41:36 +0100315#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200316 return 0;
317}
David Henningsson80c8bfb2012-06-04 09:33:51 +0200318
319int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
320 const char *name, int idx)
321{
322 return __snd_hda_jack_add_kctl(codec, nid, name, idx, false);
323}
David Henningssond1cb6202011-11-11 17:13:15 +0100324EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200325
Takashi Iwaif46c3292012-07-17 16:48:27 +0200326/* get the unique index number for the given kctl name */
327static int get_unique_index(struct hda_codec *codec, const char *name, int idx)
328{
329 struct hda_jack_tbl *jack;
330 int i, len = strlen(name);
331 again:
332 jack = codec->jacktbl.list;
333 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
334 /* jack->kctl.id contains "XXX Jack" name string with index */
335 if (jack->kctl &&
336 !strncmp(name, jack->kctl->id.name, len) &&
337 !strcmp(" Jack", jack->kctl->id.name + len) &&
338 jack->kctl->id.index == idx) {
339 idx++;
340 goto again;
341 }
342 }
343 return idx;
344}
345
Takashi Iwai201e06f2011-11-16 15:33:26 +0100346static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaif46c3292012-07-17 16:48:27 +0200347 const struct auto_pin_cfg *cfg)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200348{
Takashi Iwai3a938972011-10-28 01:16:55 +0200349 unsigned int def_conf, conn;
Takashi Iwai201e06f2011-11-16 15:33:26 +0100350 char name[44];
351 int idx, err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200352 bool phantom_jack;
Takashi Iwai3a938972011-10-28 01:16:55 +0200353
Takashi Iwai01a61e12011-10-28 00:03:22 +0200354 if (!nid)
355 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200356 def_conf = snd_hda_codec_get_pincfg(codec, nid);
357 conn = get_defcfg_connect(def_conf);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200358 if (conn == AC_JACK_PORT_NONE)
Takashi Iwai3a938972011-10-28 01:16:55 +0200359 return 0;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200360 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
361 !is_jack_detectable(codec, nid);
Takashi Iwai3a938972011-10-28 01:16:55 +0200362
Takashi Iwai201e06f2011-11-16 15:33:26 +0100363 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200364 if (phantom_jack)
365 /* Example final name: "Internal Mic Phantom Jack" */
366 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
Takashi Iwaif46c3292012-07-17 16:48:27 +0200367 idx = get_unique_index(codec, name, idx);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200368 err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack);
Takashi Iwai3a938972011-10-28 01:16:55 +0200369 if (err < 0)
370 return err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200371
372 if (!phantom_jack)
373 return snd_hda_jack_detect_enable(codec, nid, 0);
374 return 0;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200375}
376
377/**
378 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwai01a61e12011-10-28 00:03:22 +0200379 */
380int snd_hda_jack_add_kctls(struct hda_codec *codec,
381 const struct auto_pin_cfg *cfg)
382{
383 const hda_nid_t *p;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200384 int i, err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200385
386 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
Takashi Iwaif46c3292012-07-17 16:48:27 +0200387 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200388 if (err < 0)
389 return err;
390 }
391 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
392 if (*p == *cfg->line_out_pins) /* might be duplicated */
393 break;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200394 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200395 if (err < 0)
396 return err;
397 }
398 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
399 if (*p == *cfg->line_out_pins) /* might be duplicated */
400 break;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200401 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200402 if (err < 0)
403 return err;
404 }
405 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwaif46c3292012-07-17 16:48:27 +0200406 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200407 if (err < 0)
408 return err;
409 }
410 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
Takashi Iwaif46c3292012-07-17 16:48:27 +0200411 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200412 if (err < 0)
413 return err;
414 }
Takashi Iwaif46c3292012-07-17 16:48:27 +0200415 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200416 if (err < 0)
417 return err;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200418 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200419 if (err < 0)
420 return err;
421 return 0;
422}
423EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);
David Henningsson954df2a2012-09-25 11:30:59 +0200424
425void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
426{
427 struct hda_jack_tbl *event;
428 int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f;
429
430 event = snd_hda_jack_tbl_get_from_tag(codec, tag);
431 if (!event)
432 return;
433 event->jack_dirty = 1;
434
435 if (event->callback)
436 event->callback(codec, event);
437
438 snd_hda_jack_report_sync(codec);
439}
440EXPORT_SYMBOL_HDA(snd_hda_jack_unsol_event);
441