blob: d8a35da0803ff853becddd8c899e2cddba8b6293 [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
93void snd_hda_jack_tbl_clear(struct hda_codec *codec)
94{
Takashi Iwai31ef2252011-12-01 17:41:36 +010095#ifdef CONFIG_SND_HDA_INPUT_JACK
96 /* free jack instances manually when clearing/reconfiguring */
97 if (!codec->bus->shutdown && codec->jacktbl.list) {
98 struct hda_jack_tbl *jack = codec->jacktbl.list;
99 int i;
100 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
101 if (jack->jack)
102 snd_device_free(codec->bus->card, jack->jack);
103 }
104 }
105#endif
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200106 snd_array_free(&codec->jacktbl);
107}
108
109/* update the cached value and notification flag if needed */
110static void jack_detect_update(struct hda_codec *codec,
111 struct hda_jack_tbl *jack)
112{
Takashi Iwai3a938972011-10-28 01:16:55 +0200113 if (jack->jack_dirty || !jack->jack_detect) {
Takashi Iwai35be5442011-11-02 08:36:06 +0100114 jack->pin_sense = read_pin_sense(codec, jack->nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200115 jack->jack_dirty = 0;
116 }
117}
118
119/**
120 * snd_hda_set_dirty_all - Mark all the cached as dirty
121 *
122 * This function sets the dirty flag to all entries of jack table.
123 * It's called from the resume path in hda_codec.c.
124 */
125void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
126{
127 struct hda_jack_tbl *jack = codec->jacktbl.list;
128 int i;
129
130 for (i = 0; i < codec->jacktbl.used; i++, jack++)
131 if (jack->nid)
132 jack->jack_dirty = 1;
133}
134EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
135
136/**
137 * snd_hda_pin_sense - execute pin sense measurement
138 * @codec: the CODEC to sense
139 * @nid: the pin NID to sense
140 *
141 * Execute necessary pin sense measurement and return its Presence Detect,
142 * Impedance, ELD Valid etc. status bits.
143 */
144u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
145{
146 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
147 if (jack) {
148 jack_detect_update(codec, jack);
149 return jack->pin_sense;
150 }
151 return read_pin_sense(codec, nid);
152}
153EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
154
Takashi Iwai35be5442011-11-02 08:36:06 +0100155#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
156
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200157/**
158 * snd_hda_jack_detect - query pin Presence Detect status
159 * @codec: the CODEC to sense
160 * @nid: the pin NID to sense
161 *
162 * Query and return the pin's Presence Detect status.
163 */
164int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
165{
166 u32 sense = snd_hda_pin_sense(codec, nid);
Takashi Iwai35be5442011-11-02 08:36:06 +0100167 return get_jack_plug_state(sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200168}
169EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
170
171/**
172 * snd_hda_jack_detect_enable - enable the jack-detection
173 */
174int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3a938972011-10-28 01:16:55 +0200175 unsigned char action)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200176{
177 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
178 if (!jack)
179 return -ENOMEM;
Takashi Iwai3a938972011-10-28 01:16:55 +0200180 if (jack->jack_detect)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200181 return 0; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200182 jack->jack_detect = 1;
183 if (action)
184 jack->action = action;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200185 return snd_hda_codec_write_cache(codec, nid, 0,
186 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200187 AC_USRSP_EN | jack->tag);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200188}
189EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200190
Takashi Iwai01a61e12011-10-28 00:03:22 +0200191/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200192 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
193 */
194void snd_hda_jack_report_sync(struct hda_codec *codec)
195{
196 struct hda_jack_tbl *jack = codec->jacktbl.list;
Takashi Iwai35be5442011-11-02 08:36:06 +0100197 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200198
199 for (i = 0; i < codec->jacktbl.used; i++, jack++)
200 if (jack->nid) {
201 jack_detect_update(codec, jack);
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100202 if (!jack->kctl)
203 continue;
Takashi Iwai35be5442011-11-02 08:36:06 +0100204 state = get_jack_plug_state(jack->pin_sense);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100205 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
Takashi Iwai31ef2252011-12-01 17:41:36 +0100206#ifdef CONFIG_SND_HDA_INPUT_JACK
207 if (jack->jack)
208 snd_jack_report(jack->jack,
209 state ? jack->type : 0);
210#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200211 }
212}
213EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
214
Takashi Iwai31ef2252011-12-01 17:41:36 +0100215#ifdef CONFIG_SND_HDA_INPUT_JACK
216/* guess the jack type from the pin-config */
217static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
218{
219 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
220 switch (get_defcfg_device(def_conf)) {
221 case AC_JACK_LINE_OUT:
222 case AC_JACK_SPEAKER:
223 return SND_JACK_LINEOUT;
224 case AC_JACK_HP_OUT:
225 return SND_JACK_HEADPHONE;
226 case AC_JACK_SPDIF_OUT:
227 case AC_JACK_DIG_OTHER_OUT:
228 return SND_JACK_AVOUT;
229 case AC_JACK_MIC_IN:
230 return SND_JACK_MICROPHONE;
231 default:
232 return SND_JACK_LINEIN;
233 }
234}
235
236static void hda_free_jack_priv(struct snd_jack *jack)
237{
238 struct hda_jack_tbl *jacks = jack->private_data;
239 jacks->nid = 0;
240 jacks->jack = NULL;
241}
242#endif
243
Takashi Iwai01a61e12011-10-28 00:03:22 +0200244/**
245 * snd_hda_jack_add_kctl - Add a kctl for the given pin
246 *
247 * This assigns a jack-detection kctl to the given pin. The kcontrol
248 * will have the given name and index.
249 */
250int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
251 const char *name, int idx)
252{
253 struct hda_jack_tbl *jack;
254 struct snd_kcontrol *kctl;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100255 int err, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200256
Takashi Iwai3a938972011-10-28 01:16:55 +0200257 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200258 if (!jack)
259 return 0;
260 if (jack->kctl)
261 return 0; /* already created */
Takashi Iwai35be5442011-11-02 08:36:06 +0100262 kctl = snd_kctl_jack_new(name, idx, codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200263 if (!kctl)
264 return -ENOMEM;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100265 err = snd_hda_ctl_add(codec, nid, kctl);
266 if (err < 0)
267 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200268 jack->kctl = kctl;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100269 state = snd_hda_jack_detect(codec, nid);
270 snd_kctl_jack_report(codec->bus->card, kctl, state);
271#ifdef CONFIG_SND_HDA_INPUT_JACK
272 jack->type = get_input_jack_type(codec, nid);
273 err = snd_jack_new(codec->bus->card, name, jack->type, &jack->jack);
274 if (err < 0)
275 return err;
276 jack->jack->private_data = jack;
277 jack->jack->private_free = hda_free_jack_priv;
278 snd_jack_report(jack->jack, state ? jack->type : 0);
279#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200280 return 0;
281}
David Henningssond1cb6202011-11-11 17:13:15 +0100282EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200283
Takashi Iwai201e06f2011-11-16 15:33:26 +0100284static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai01a61e12011-10-28 00:03:22 +0200285 const struct auto_pin_cfg *cfg)
286{
Takashi Iwai3a938972011-10-28 01:16:55 +0200287 unsigned int def_conf, conn;
Takashi Iwai201e06f2011-11-16 15:33:26 +0100288 char name[44];
289 int idx, err;
Takashi Iwai3a938972011-10-28 01:16:55 +0200290
Takashi Iwai01a61e12011-10-28 00:03:22 +0200291 if (!nid)
292 return 0;
293 if (!is_jack_detectable(codec, nid))
294 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200295 def_conf = snd_hda_codec_get_pincfg(codec, nid);
296 conn = get_defcfg_connect(def_conf);
297 if (conn != AC_JACK_PORT_COMPLEX)
298 return 0;
299
Takashi Iwai201e06f2011-11-16 15:33:26 +0100300 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
301 err = snd_hda_jack_add_kctl(codec, nid, name, idx);
Takashi Iwai3a938972011-10-28 01:16:55 +0200302 if (err < 0)
303 return err;
304 return snd_hda_jack_detect_enable(codec, nid, 0);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200305}
306
307/**
308 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwai01a61e12011-10-28 00:03:22 +0200309 */
310int snd_hda_jack_add_kctls(struct hda_codec *codec,
311 const struct auto_pin_cfg *cfg)
312{
313 const hda_nid_t *p;
314 int i, err;
315
316 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
Takashi Iwai201e06f2011-11-16 15:33:26 +0100317 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200318 if (err < 0)
319 return err;
320 }
321 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
322 if (*p == *cfg->line_out_pins) /* might be duplicated */
323 break;
Takashi Iwai201e06f2011-11-16 15:33:26 +0100324 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200325 if (err < 0)
326 return err;
327 }
328 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
329 if (*p == *cfg->line_out_pins) /* might be duplicated */
330 break;
Takashi Iwai201e06f2011-11-16 15:33:26 +0100331 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200332 if (err < 0)
333 return err;
334 }
335 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai201e06f2011-11-16 15:33:26 +0100336 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200337 if (err < 0)
338 return err;
339 }
340 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
Takashi Iwai201e06f2011-11-16 15:33:26 +0100341 err = add_jack_kctl(codec, *p, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200342 if (err < 0)
343 return err;
344 }
Takashi Iwai201e06f2011-11-16 15:33:26 +0100345 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200346 if (err < 0)
347 return err;
Takashi Iwai201e06f2011-11-16 15:33:26 +0100348 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200349 if (err < 0)
350 return err;
351 return 0;
352}
353EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);