blob: a2ab52b2726596e1ab500b47b1469409a3761eeb [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 Iwai1835a0f2011-10-27 22:12:46 +020016#include "hda_codec.h"
17#include "hda_local.h"
18#include "hda_jack.h"
19
20/* execute pin sense measurement */
21static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
22{
23 u32 pincap;
24
25 if (!codec->no_trigger_sense) {
26 pincap = snd_hda_query_pin_caps(codec, nid);
27 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
28 snd_hda_codec_read(codec, nid, 0,
29 AC_VERB_SET_PIN_SENSE, 0);
30 }
31 return snd_hda_codec_read(codec, nid, 0,
32 AC_VERB_GET_PIN_SENSE, 0);
33}
34
35/**
36 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
37 */
38struct hda_jack_tbl *
39snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
40{
41 struct hda_jack_tbl *jack = codec->jacktbl.list;
42 int i;
43
44 if (!nid || !jack)
45 return NULL;
46 for (i = 0; i < codec->jacktbl.used; i++, jack++)
47 if (jack->nid == nid)
48 return jack;
49 return NULL;
50}
51EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get);
52
53/**
Takashi Iwai3a938972011-10-28 01:16:55 +020054 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
55 */
56struct hda_jack_tbl *
57snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
58{
59 struct hda_jack_tbl *jack = codec->jacktbl.list;
60 int i;
61
62 if (!tag || !jack)
63 return NULL;
64 for (i = 0; i < codec->jacktbl.used; i++, jack++)
65 if (jack->tag == tag)
66 return jack;
67 return NULL;
68}
69EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag);
70
71/**
Takashi Iwai1835a0f2011-10-27 22:12:46 +020072 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
73 */
74struct hda_jack_tbl *
75snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
76{
77 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
78 if (jack)
79 return jack;
80 snd_array_init(&codec->jacktbl, sizeof(*jack), 16);
81 jack = snd_array_new(&codec->jacktbl);
82 if (!jack)
83 return NULL;
84 jack->nid = nid;
85 jack->jack_dirty = 1;
Takashi Iwai3a938972011-10-28 01:16:55 +020086 jack->tag = codec->jacktbl.used;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020087 return jack;
88}
89
90void snd_hda_jack_tbl_clear(struct hda_codec *codec)
91{
92 snd_array_free(&codec->jacktbl);
93}
94
95/* update the cached value and notification flag if needed */
96static void jack_detect_update(struct hda_codec *codec,
97 struct hda_jack_tbl *jack)
98{
Takashi Iwai3a938972011-10-28 01:16:55 +020099 if (jack->jack_dirty || !jack->jack_detect) {
Takashi Iwai35be5442011-11-02 08:36:06 +0100100 jack->pin_sense = read_pin_sense(codec, jack->nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200101 jack->jack_dirty = 0;
102 }
103}
104
105/**
106 * snd_hda_set_dirty_all - Mark all the cached as dirty
107 *
108 * This function sets the dirty flag to all entries of jack table.
109 * It's called from the resume path in hda_codec.c.
110 */
111void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
112{
113 struct hda_jack_tbl *jack = codec->jacktbl.list;
114 int i;
115
116 for (i = 0; i < codec->jacktbl.used; i++, jack++)
117 if (jack->nid)
118 jack->jack_dirty = 1;
119}
120EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all);
121
122/**
123 * snd_hda_pin_sense - execute pin sense measurement
124 * @codec: the CODEC to sense
125 * @nid: the pin NID to sense
126 *
127 * Execute necessary pin sense measurement and return its Presence Detect,
128 * Impedance, ELD Valid etc. status bits.
129 */
130u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
131{
132 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
133 if (jack) {
134 jack_detect_update(codec, jack);
135 return jack->pin_sense;
136 }
137 return read_pin_sense(codec, nid);
138}
139EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
140
Takashi Iwai35be5442011-11-02 08:36:06 +0100141#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
142
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200143/**
144 * snd_hda_jack_detect - query pin Presence Detect status
145 * @codec: the CODEC to sense
146 * @nid: the pin NID to sense
147 *
148 * Query and return the pin's Presence Detect status.
149 */
150int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
151{
152 u32 sense = snd_hda_pin_sense(codec, nid);
Takashi Iwai35be5442011-11-02 08:36:06 +0100153 return get_jack_plug_state(sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200154}
155EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
156
157/**
158 * snd_hda_jack_detect_enable - enable the jack-detection
159 */
160int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai3a938972011-10-28 01:16:55 +0200161 unsigned char action)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200162{
163 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
164 if (!jack)
165 return -ENOMEM;
Takashi Iwai3a938972011-10-28 01:16:55 +0200166 if (jack->jack_detect)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200167 return 0; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200168 jack->jack_detect = 1;
169 if (action)
170 jack->action = action;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200171 return snd_hda_codec_write_cache(codec, nid, 0,
172 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200173 AC_USRSP_EN | jack->tag);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200174}
175EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200176
Takashi Iwai01a61e12011-10-28 00:03:22 +0200177/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200178 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
179 */
180void snd_hda_jack_report_sync(struct hda_codec *codec)
181{
182 struct hda_jack_tbl *jack = codec->jacktbl.list;
Takashi Iwai35be5442011-11-02 08:36:06 +0100183 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200184
185 for (i = 0; i < codec->jacktbl.used; i++, jack++)
186 if (jack->nid) {
187 jack_detect_update(codec, jack);
Takashi Iwai35be5442011-11-02 08:36:06 +0100188 state = get_jack_plug_state(jack->pin_sense);
189 snd_kctl_jack_notify(codec->bus->card, jack->kctl, state);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200190 }
191}
192EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync);
193
Takashi Iwai01a61e12011-10-28 00:03:22 +0200194/**
195 * snd_hda_jack_add_kctl - Add a kctl for the given pin
196 *
197 * This assigns a jack-detection kctl to the given pin. The kcontrol
198 * will have the given name and index.
199 */
200int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
201 const char *name, int idx)
202{
203 struct hda_jack_tbl *jack;
204 struct snd_kcontrol *kctl;
205
Takashi Iwai3a938972011-10-28 01:16:55 +0200206 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200207 if (!jack)
208 return 0;
209 if (jack->kctl)
210 return 0; /* already created */
Takashi Iwai35be5442011-11-02 08:36:06 +0100211 kctl = snd_kctl_jack_new(name, idx, codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200212 if (!kctl)
213 return -ENOMEM;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200214 if (snd_hda_ctl_add(codec, nid, kctl) < 0)
215 return -ENOMEM;
216 jack->kctl = kctl;
217 return 0;
218}
219
220static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, int idx,
221 const struct auto_pin_cfg *cfg)
222{
Takashi Iwai3a938972011-10-28 01:16:55 +0200223 unsigned int def_conf, conn;
224 int err;
225
Takashi Iwai01a61e12011-10-28 00:03:22 +0200226 if (!nid)
227 return 0;
228 if (!is_jack_detectable(codec, nid))
229 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200230 def_conf = snd_hda_codec_get_pincfg(codec, nid);
231 conn = get_defcfg_connect(def_conf);
232 if (conn != AC_JACK_PORT_COMPLEX)
233 return 0;
234
235 err = snd_hda_jack_add_kctl(codec, nid,
Takashi Iwai01a61e12011-10-28 00:03:22 +0200236 snd_hda_get_pin_label(codec, nid, cfg),
237 idx);
Takashi Iwai3a938972011-10-28 01:16:55 +0200238 if (err < 0)
239 return err;
240 return snd_hda_jack_detect_enable(codec, nid, 0);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200241}
242
243/**
244 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwai01a61e12011-10-28 00:03:22 +0200245 */
246int snd_hda_jack_add_kctls(struct hda_codec *codec,
247 const struct auto_pin_cfg *cfg)
248{
249 const hda_nid_t *p;
250 int i, err;
251
252 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
253 err = add_jack_kctl(codec, *p, i, cfg);
254 if (err < 0)
255 return err;
256 }
257 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
258 if (*p == *cfg->line_out_pins) /* might be duplicated */
259 break;
260 err = add_jack_kctl(codec, *p, i, cfg);
261 if (err < 0)
262 return err;
263 }
264 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
265 if (*p == *cfg->line_out_pins) /* might be duplicated */
266 break;
267 err = add_jack_kctl(codec, *p, i, cfg);
268 if (err < 0)
269 return err;
270 }
271 for (i = 0; i < cfg->num_inputs; i++) {
272 err = add_jack_kctl(codec, cfg->inputs[i].pin, 0, cfg);
273 if (err < 0)
274 return err;
275 }
276 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
277 err = add_jack_kctl(codec, *p, i, cfg);
278 if (err < 0)
279 return err;
280 }
281 err = add_jack_kctl(codec, cfg->dig_in_pin, 0, cfg);
282 if (err < 0)
283 return err;
284 err = add_jack_kctl(codec, cfg->mono_out_pin, 0, cfg);
285 if (err < 0)
286 return err;
287 return 0;
288}
289EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls);