blob: 9746d73cec52b113b83e7e694f854cdac719e574 [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;
Takashi Iwai76a19c62013-01-07 12:11:33 +010032 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) &&
33 !codec->jackpoll_interval)
Takashi Iwaia9c74172012-02-13 11:41:42 +010034 return false;
35 return true;
36}
Takashi Iwai2698ea92013-12-18 07:45:52 +010037EXPORT_SYMBOL_GPL(is_jack_detectable);
Takashi Iwaia9c74172012-02-13 11:41:42 +010038
Takashi Iwai1835a0f2011-10-27 22:12:46 +020039/* execute pin sense measurement */
40static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
41{
42 u32 pincap;
Takashi Iwai9cc159c2012-12-21 15:31:41 +010043 u32 val;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020044
45 if (!codec->no_trigger_sense) {
46 pincap = snd_hda_query_pin_caps(codec, nid);
47 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
48 snd_hda_codec_read(codec, nid, 0,
49 AC_VERB_SET_PIN_SENSE, 0);
50 }
Takashi Iwai9cc159c2012-12-21 15:31:41 +010051 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai1835a0f2011-10-27 22:12:46 +020052 AC_VERB_GET_PIN_SENSE, 0);
Takashi Iwai9cc159c2012-12-21 15:31:41 +010053 if (codec->inv_jack_detect)
54 val ^= AC_PINSENSE_PRESENCE;
55 return val;
Takashi Iwai1835a0f2011-10-27 22:12:46 +020056}
57
58/**
59 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
60 */
61struct hda_jack_tbl *
62snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
63{
64 struct hda_jack_tbl *jack = codec->jacktbl.list;
65 int i;
66
67 if (!nid || !jack)
68 return NULL;
69 for (i = 0; i < codec->jacktbl.used; i++, jack++)
70 if (jack->nid == nid)
71 return jack;
72 return NULL;
73}
Takashi Iwai2698ea92013-12-18 07:45:52 +010074EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get);
Takashi Iwai1835a0f2011-10-27 22:12:46 +020075
76/**
Takashi Iwai3a938972011-10-28 01:16:55 +020077 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
78 */
79struct hda_jack_tbl *
80snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
81{
82 struct hda_jack_tbl *jack = codec->jacktbl.list;
83 int i;
84
85 if (!tag || !jack)
86 return NULL;
87 for (i = 0; i < codec->jacktbl.used; i++, jack++)
88 if (jack->tag == tag)
89 return jack;
90 return NULL;
91}
Takashi Iwai2698ea92013-12-18 07:45:52 +010092EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
Takashi Iwai3a938972011-10-28 01:16:55 +020093
94/**
Takashi Iwai1835a0f2011-10-27 22:12:46 +020095 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
96 */
97struct hda_jack_tbl *
98snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
99{
100 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
101 if (jack)
102 return jack;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200103 jack = snd_array_new(&codec->jacktbl);
104 if (!jack)
105 return NULL;
106 jack->nid = nid;
107 jack->jack_dirty = 1;
Takashi Iwai3a938972011-10-28 01:16:55 +0200108 jack->tag = codec->jacktbl.used;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200109 return jack;
110}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100111EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_new);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200112
113void snd_hda_jack_tbl_clear(struct hda_codec *codec)
114{
Takashi Iwai31ef2252011-12-01 17:41:36 +0100115#ifdef CONFIG_SND_HDA_INPUT_JACK
116 /* free jack instances manually when clearing/reconfiguring */
117 if (!codec->bus->shutdown && codec->jacktbl.list) {
118 struct hda_jack_tbl *jack = codec->jacktbl.list;
119 int i;
120 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
121 if (jack->jack)
122 snd_device_free(codec->bus->card, jack->jack);
123 }
124 }
125#endif
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200126 snd_array_free(&codec->jacktbl);
127}
128
Dylan Reid0619ba82012-11-19 19:48:07 +0100129#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
130
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200131/* update the cached value and notification flag if needed */
132static void jack_detect_update(struct hda_codec *codec,
133 struct hda_jack_tbl *jack)
134{
David Henningsson80c8bfb2012-06-04 09:33:51 +0200135 if (!jack->jack_dirty)
136 return;
137
138 if (jack->phantom_jack)
139 jack->pin_sense = AC_PINSENSE_PRESENCE;
140 else
Takashi Iwai35be5442011-11-02 08:36:06 +0100141 jack->pin_sense = read_pin_sense(codec, jack->nid);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200142
Dylan Reid0619ba82012-11-19 19:48:07 +0100143 /* A gating jack indicates the jack is invalid if gating is unplugged */
144 if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack))
145 jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
146
David Henningsson80c8bfb2012-06-04 09:33:51 +0200147 jack->jack_dirty = 0;
Dylan Reid0619ba82012-11-19 19:48:07 +0100148
149 /* If a jack is gated by this one update it. */
150 if (jack->gated_jack) {
151 struct hda_jack_tbl *gated =
152 snd_hda_jack_tbl_get(codec, jack->gated_jack);
153 if (gated) {
154 gated->jack_dirty = 1;
155 jack_detect_update(codec, gated);
156 }
157 }
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200158}
159
160/**
161 * snd_hda_set_dirty_all - Mark all the cached as dirty
162 *
163 * This function sets the dirty flag to all entries of jack table.
164 * It's called from the resume path in hda_codec.c.
165 */
166void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
167{
168 struct hda_jack_tbl *jack = codec->jacktbl.list;
169 int i;
170
171 for (i = 0; i < codec->jacktbl.used; i++, jack++)
172 if (jack->nid)
173 jack->jack_dirty = 1;
174}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100175EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200176
177/**
178 * snd_hda_pin_sense - execute pin sense measurement
179 * @codec: the CODEC to sense
180 * @nid: the pin NID to sense
181 *
182 * Execute necessary pin sense measurement and return its Presence Detect,
183 * Impedance, ELD Valid etc. status bits.
184 */
185u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
186{
187 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
188 if (jack) {
189 jack_detect_update(codec, jack);
190 return jack->pin_sense;
191 }
192 return read_pin_sense(codec, nid);
193}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100194EXPORT_SYMBOL_GPL(snd_hda_pin_sense);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200195
196/**
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200197 * snd_hda_jack_detect_state - query pin Presence Detect status
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200198 * @codec: the CODEC to sense
199 * @nid: the pin NID to sense
200 *
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200201 * Query and return the pin's Presence Detect status, as either
202 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200203 */
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200204int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200205{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +0200206 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
207 if (jack && jack->phantom_jack)
208 return HDA_JACK_PHANTOM;
209 else if (snd_hda_pin_sense(codec, nid) & AC_PINSENSE_PRESENCE)
210 return HDA_JACK_PRESENT;
211 else
212 return HDA_JACK_NOT_PRESENT;
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200213}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100214EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200215
216/**
217 * snd_hda_jack_detect_enable - enable the jack-detection
218 */
David Henningsson954df2a2012-09-25 11:30:59 +0200219int snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
220 unsigned char action,
221 hda_jack_callback cb)
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200222{
223 struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid);
224 if (!jack)
225 return -ENOMEM;
Takashi Iwai3a938972011-10-28 01:16:55 +0200226 if (jack->jack_detect)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200227 return 0; /* already registered */
Takashi Iwai3a938972011-10-28 01:16:55 +0200228 jack->jack_detect = 1;
229 if (action)
230 jack->action = action;
David Henningsson954df2a2012-09-25 11:30:59 +0200231 if (cb)
232 jack->callback = cb;
David Henningsson34a93182012-10-17 14:45:05 +0200233 if (codec->jackpoll_interval > 0)
234 return 0; /* No unsol if we're polling instead */
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200235 return snd_hda_codec_write_cache(codec, nid, 0,
236 AC_VERB_SET_UNSOLICITED_ENABLE,
Takashi Iwai3a938972011-10-28 01:16:55 +0200237 AC_USRSP_EN | jack->tag);
Takashi Iwai1835a0f2011-10-27 22:12:46 +0200238}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100239EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback);
David Henningsson954df2a2012-09-25 11:30:59 +0200240
241int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
242 unsigned char action)
243{
244 return snd_hda_jack_detect_enable_callback(codec, nid, action, NULL);
245}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100246EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200247
Takashi Iwai01a61e12011-10-28 00:03:22 +0200248/**
Dylan Reid0619ba82012-11-19 19:48:07 +0100249 * snd_hda_jack_set_gating_jack - Set gating jack.
250 *
251 * Indicates the gated jack is only valid when the gating jack is plugged.
252 */
253int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
254 hda_nid_t gating_nid)
255{
Chih-Chung Changbde7bc62013-08-05 16:38:42 +0800256 struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid);
257 struct hda_jack_tbl *gating = snd_hda_jack_tbl_new(codec, gating_nid);
Dylan Reid0619ba82012-11-19 19:48:07 +0100258
259 if (!gated || !gating)
260 return -EINVAL;
261
262 gated->gating_jack = gating_nid;
263 gating->gated_jack = gated_nid;
264
265 return 0;
266}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100267EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
Dylan Reid0619ba82012-11-19 19:48:07 +0100268
269/**
Takashi Iwai01a61e12011-10-28 00:03:22 +0200270 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
271 */
272void snd_hda_jack_report_sync(struct hda_codec *codec)
273{
Dylan Reid0619ba82012-11-19 19:48:07 +0100274 struct hda_jack_tbl *jack;
Takashi Iwai35be5442011-11-02 08:36:06 +0100275 int i, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200276
Dylan Reid0619ba82012-11-19 19:48:07 +0100277 /* update all jacks at first */
278 jack = codec->jacktbl.list;
279 for (i = 0; i < codec->jacktbl.used; i++, jack++)
280 if (jack->nid)
281 jack_detect_update(codec, jack);
282
283 /* report the updated jacks; it's done after updating all jacks
284 * to make sure that all gating jacks properly have been set
285 */
286 jack = codec->jacktbl.list;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200287 for (i = 0; i < codec->jacktbl.used; i++, jack++)
288 if (jack->nid) {
David Henningsson0f568952013-11-07 13:38:24 +0100289 if (!jack->kctl || jack->block_report)
Takashi Iwaicfc7c9d2011-11-11 17:53:03 +0100290 continue;
Takashi Iwai35be5442011-11-02 08:36:06 +0100291 state = get_jack_plug_state(jack->pin_sense);
Takashi Iwaiaad37db2011-11-02 08:54:51 +0100292 snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
Takashi Iwai31ef2252011-12-01 17:41:36 +0100293#ifdef CONFIG_SND_HDA_INPUT_JACK
294 if (jack->jack)
295 snd_jack_report(jack->jack,
296 state ? jack->type : 0);
297#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200298 }
299}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100300EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200301
Takashi Iwai31ef2252011-12-01 17:41:36 +0100302#ifdef CONFIG_SND_HDA_INPUT_JACK
303/* guess the jack type from the pin-config */
304static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
305{
306 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
307 switch (get_defcfg_device(def_conf)) {
308 case AC_JACK_LINE_OUT:
309 case AC_JACK_SPEAKER:
310 return SND_JACK_LINEOUT;
311 case AC_JACK_HP_OUT:
312 return SND_JACK_HEADPHONE;
313 case AC_JACK_SPDIF_OUT:
314 case AC_JACK_DIG_OTHER_OUT:
315 return SND_JACK_AVOUT;
316 case AC_JACK_MIC_IN:
317 return SND_JACK_MICROPHONE;
318 default:
319 return SND_JACK_LINEIN;
320 }
321}
322
323static void hda_free_jack_priv(struct snd_jack *jack)
324{
325 struct hda_jack_tbl *jacks = jack->private_data;
326 jacks->nid = 0;
327 jacks->jack = NULL;
328}
329#endif
330
Takashi Iwai01a61e12011-10-28 00:03:22 +0200331/**
332 * snd_hda_jack_add_kctl - Add a kctl for the given pin
333 *
334 * This assigns a jack-detection kctl to the given pin. The kcontrol
335 * will have the given name and index.
336 */
David Henningsson80c8bfb2012-06-04 09:33:51 +0200337static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
338 const char *name, int idx, bool phantom_jack)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200339{
340 struct hda_jack_tbl *jack;
341 struct snd_kcontrol *kctl;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100342 int err, state;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200343
Takashi Iwai3a938972011-10-28 01:16:55 +0200344 jack = snd_hda_jack_tbl_new(codec, nid);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200345 if (!jack)
346 return 0;
347 if (jack->kctl)
348 return 0; /* already created */
Takashi Iwai35be5442011-11-02 08:36:06 +0100349 kctl = snd_kctl_jack_new(name, idx, codec);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200350 if (!kctl)
351 return -ENOMEM;
Takashi Iwai31ef2252011-12-01 17:41:36 +0100352 err = snd_hda_ctl_add(codec, nid, kctl);
353 if (err < 0)
354 return err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200355 jack->kctl = kctl;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200356 jack->phantom_jack = !!phantom_jack;
357
Takashi Iwai31ef2252011-12-01 17:41:36 +0100358 state = snd_hda_jack_detect(codec, nid);
359 snd_kctl_jack_report(codec->bus->card, kctl, state);
360#ifdef CONFIG_SND_HDA_INPUT_JACK
David Henningsson80c8bfb2012-06-04 09:33:51 +0200361 if (!phantom_jack) {
362 jack->type = get_input_jack_type(codec, nid);
363 err = snd_jack_new(codec->bus->card, name, jack->type,
364 &jack->jack);
365 if (err < 0)
366 return err;
367 jack->jack->private_data = jack;
368 jack->jack->private_free = hda_free_jack_priv;
369 snd_jack_report(jack->jack, state ? jack->type : 0);
370 }
Takashi Iwai31ef2252011-12-01 17:41:36 +0100371#endif
Takashi Iwai01a61e12011-10-28 00:03:22 +0200372 return 0;
373}
David Henningsson80c8bfb2012-06-04 09:33:51 +0200374
375int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
376 const char *name, int idx)
377{
378 return __snd_hda_jack_add_kctl(codec, nid, name, idx, false);
379}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100380EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200381
Takashi Iwaif46c3292012-07-17 16:48:27 +0200382/* get the unique index number for the given kctl name */
383static int get_unique_index(struct hda_codec *codec, const char *name, int idx)
384{
385 struct hda_jack_tbl *jack;
386 int i, len = strlen(name);
387 again:
388 jack = codec->jacktbl.list;
389 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
390 /* jack->kctl.id contains "XXX Jack" name string with index */
391 if (jack->kctl &&
392 !strncmp(name, jack->kctl->id.name, len) &&
393 !strcmp(" Jack", jack->kctl->id.name + len) &&
394 jack->kctl->id.index == idx) {
395 idx++;
396 goto again;
397 }
398 }
399 return idx;
400}
401
Takashi Iwai201e06f2011-11-16 15:33:26 +0100402static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
David Henningssonb26b5112013-04-11 11:30:30 +0200403 const struct auto_pin_cfg *cfg,
404 const char *base_name)
Takashi Iwai01a61e12011-10-28 00:03:22 +0200405{
Takashi Iwai3a938972011-10-28 01:16:55 +0200406 unsigned int def_conf, conn;
Takashi Iwai975cc022013-06-28 11:56:49 +0200407 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai201e06f2011-11-16 15:33:26 +0100408 int idx, err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200409 bool phantom_jack;
Takashi Iwai3a938972011-10-28 01:16:55 +0200410
Takashi Iwai01a61e12011-10-28 00:03:22 +0200411 if (!nid)
412 return 0;
Takashi Iwai3a938972011-10-28 01:16:55 +0200413 def_conf = snd_hda_codec_get_pincfg(codec, nid);
414 conn = get_defcfg_connect(def_conf);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200415 if (conn == AC_JACK_PORT_NONE)
Takashi Iwai3a938972011-10-28 01:16:55 +0200416 return 0;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200417 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
418 !is_jack_detectable(codec, nid);
Takashi Iwai3a938972011-10-28 01:16:55 +0200419
David Henningssonb26b5112013-04-11 11:30:30 +0200420 if (base_name) {
421 strlcpy(name, base_name, sizeof(name));
422 idx = 0;
423 } else
424 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200425 if (phantom_jack)
426 /* Example final name: "Internal Mic Phantom Jack" */
427 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
Takashi Iwaif46c3292012-07-17 16:48:27 +0200428 idx = get_unique_index(codec, name, idx);
David Henningsson80c8bfb2012-06-04 09:33:51 +0200429 err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack);
Takashi Iwai3a938972011-10-28 01:16:55 +0200430 if (err < 0)
431 return err;
David Henningsson80c8bfb2012-06-04 09:33:51 +0200432
433 if (!phantom_jack)
434 return snd_hda_jack_detect_enable(codec, nid, 0);
435 return 0;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200436}
437
438/**
439 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
Takashi Iwai01a61e12011-10-28 00:03:22 +0200440 */
441int snd_hda_jack_add_kctls(struct hda_codec *codec,
442 const struct auto_pin_cfg *cfg)
443{
444 const hda_nid_t *p;
Takashi Iwaif46c3292012-07-17 16:48:27 +0200445 int i, err;
Takashi Iwai01a61e12011-10-28 00:03:22 +0200446
David Henningssonb26b5112013-04-11 11:30:30 +0200447 for (i = 0; i < cfg->num_inputs; i++) {
448 /* If we have headphone mics; make sure they get the right name
449 before grabbed by output pins */
450 if (cfg->inputs[i].is_headphone_mic) {
451 if (auto_cfg_hp_outs(cfg) == 1)
452 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
453 cfg, "Headphone Mic");
454 else
455 err = add_jack_kctl(codec, cfg->inputs[i].pin,
456 cfg, "Headphone Mic");
457 } else
458 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
459 NULL);
460 if (err < 0)
461 return err;
462 }
463
Takashi Iwai01a61e12011-10-28 00:03:22 +0200464 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
David Henningssonb26b5112013-04-11 11:30:30 +0200465 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200466 if (err < 0)
467 return err;
468 }
469 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
470 if (*p == *cfg->line_out_pins) /* might be duplicated */
471 break;
David Henningssonb26b5112013-04-11 11:30:30 +0200472 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200473 if (err < 0)
474 return err;
475 }
476 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
477 if (*p == *cfg->line_out_pins) /* might be duplicated */
478 break;
David Henningssonb26b5112013-04-11 11:30:30 +0200479 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200480 if (err < 0)
481 return err;
482 }
483 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
David Henningssonb26b5112013-04-11 11:30:30 +0200484 err = add_jack_kctl(codec, *p, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200485 if (err < 0)
486 return err;
487 }
David Henningssonb26b5112013-04-11 11:30:30 +0200488 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200489 if (err < 0)
490 return err;
David Henningssonb26b5112013-04-11 11:30:30 +0200491 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200492 if (err < 0)
493 return err;
494 return 0;
495}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100496EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
David Henningsson954df2a2012-09-25 11:30:59 +0200497
Dylan Reid0619ba82012-11-19 19:48:07 +0100498static void call_jack_callback(struct hda_codec *codec,
499 struct hda_jack_tbl *jack)
500{
501 if (jack->callback)
502 jack->callback(codec, jack);
503 if (jack->gated_jack) {
504 struct hda_jack_tbl *gated =
505 snd_hda_jack_tbl_get(codec, jack->gated_jack);
506 if (gated && gated->callback)
507 gated->callback(codec, gated);
508 }
509}
510
David Henningsson954df2a2012-09-25 11:30:59 +0200511void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
512{
513 struct hda_jack_tbl *event;
514 int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f;
515
516 event = snd_hda_jack_tbl_get_from_tag(codec, tag);
517 if (!event)
518 return;
519 event->jack_dirty = 1;
520
Dylan Reid0619ba82012-11-19 19:48:07 +0100521 call_jack_callback(codec, event);
David Henningsson954df2a2012-09-25 11:30:59 +0200522 snd_hda_jack_report_sync(codec);
523}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100524EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
David Henningsson954df2a2012-09-25 11:30:59 +0200525
David Henningsson26a6cb62012-10-09 15:04:21 +0200526void snd_hda_jack_poll_all(struct hda_codec *codec)
527{
528 struct hda_jack_tbl *jack = codec->jacktbl.list;
529 int i, changes = 0;
530
531 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
532 unsigned int old_sense;
533 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
534 continue;
535 old_sense = get_jack_plug_state(jack->pin_sense);
536 jack_detect_update(codec, jack);
537 if (old_sense == get_jack_plug_state(jack->pin_sense))
538 continue;
539 changes = 1;
Dylan Reid0619ba82012-11-19 19:48:07 +0100540 call_jack_callback(codec, jack);
David Henningsson26a6cb62012-10-09 15:04:21 +0200541 }
542 if (changes)
543 snd_hda_jack_report_sync(codec);
544}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100545EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);
David Henningsson26a6cb62012-10-09 15:04:21 +0200546