Thomas Gleixner | 62810db | 2019-05-20 19:08:11 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Jack-detection handling for HD-audio |
| 4 | * |
| 5 | * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de> |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/slab.h> |
Takashi Iwai | bf815bf | 2011-11-16 14:28:33 +0100 | [diff] [blame] | 10 | #include <linux/export.h> |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 11 | #include <sound/core.h> |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 12 | #include <sound/control.h> |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 13 | #include <sound/jack.h> |
Pierre-Louis Bossart | be57bff | 2018-08-22 15:24:57 -0500 | [diff] [blame] | 14 | #include <sound/hda_codec.h> |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 15 | #include "hda_local.h" |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 16 | #include "hda_auto_parser.h" |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 17 | #include "hda_jack.h" |
| 18 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 19 | /** |
| 20 | * is_jack_detectable - Check whether the given pin is jack-detectable |
| 21 | * @codec: the HDA codec |
| 22 | * @nid: pin NID |
| 23 | * |
| 24 | * Check whether the given pin is capable to report the jack detection. |
| 25 | * The jack detection might not work by various reasons, e.g. the jack |
| 26 | * detection is prohibited in the codec level, the pin config has |
| 27 | * AC_DEFCFG_MISC_NO_PRESENCE bit, no unsol support, etc. |
| 28 | */ |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 29 | bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid) |
| 30 | { |
Takashi Iwai | 71b1e9e4 | 2012-02-13 11:55:02 +0100 | [diff] [blame] | 31 | if (codec->no_jack_detect) |
| 32 | return false; |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 33 | if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT)) |
| 34 | return false; |
David Henningsson | 5fe8e1e | 2012-09-07 07:25:44 +0200 | [diff] [blame] | 35 | if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) & |
| 36 | AC_DEFCFG_MISC_NO_PRESENCE) |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 37 | return false; |
Takashi Iwai | 76a19c6 | 2013-01-07 12:11:33 +0100 | [diff] [blame] | 38 | if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) && |
| 39 | !codec->jackpoll_interval) |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 40 | return false; |
| 41 | return true; |
| 42 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 43 | EXPORT_SYMBOL_GPL(is_jack_detectable); |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 44 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 45 | /* execute pin sense measurement */ |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 46 | static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 47 | { |
| 48 | u32 pincap; |
Takashi Iwai | 9cc159c | 2012-12-21 15:31:41 +0100 | [diff] [blame] | 49 | u32 val; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 50 | |
| 51 | if (!codec->no_trigger_sense) { |
| 52 | pincap = snd_hda_query_pin_caps(codec, nid); |
| 53 | if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */ |
| 54 | snd_hda_codec_read(codec, nid, 0, |
| 55 | AC_VERB_SET_PIN_SENSE, 0); |
| 56 | } |
Takashi Iwai | 9cc159c | 2012-12-21 15:31:41 +0100 | [diff] [blame] | 57 | val = snd_hda_codec_read(codec, nid, 0, |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 58 | AC_VERB_GET_PIN_SENSE, dev_id); |
Takashi Iwai | 9cc159c | 2012-12-21 15:31:41 +0100 | [diff] [blame] | 59 | if (codec->inv_jack_detect) |
| 60 | val ^= AC_PINSENSE_PRESENCE; |
| 61 | return val; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 65 | * snd_hda_jack_tbl_get_mst - query the jack-table entry for the given NID |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 66 | * @codec: the HDA codec |
| 67 | * @nid: pin NID to refer to |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 68 | * @dev_id: pin device entry id |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 69 | */ |
| 70 | struct hda_jack_tbl * |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 71 | snd_hda_jack_tbl_get_mst(struct hda_codec *codec, hda_nid_t nid, int dev_id) |
| 72 | { |
| 73 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 74 | int i; |
| 75 | |
| 76 | if (!nid || !jack) |
| 77 | return NULL; |
| 78 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 79 | if (jack->nid == nid && jack->dev_id == dev_id) |
| 80 | return jack; |
| 81 | return NULL; |
| 82 | } |
| 83 | EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_mst); |
| 84 | |
| 85 | /** |
| 86 | * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag |
| 87 | * @codec: the HDA codec |
| 88 | * @tag: tag value to refer to |
| 89 | * @dev_id: pin device entry id |
| 90 | */ |
| 91 | struct hda_jack_tbl * |
| 92 | snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, |
| 93 | unsigned char tag, int dev_id) |
| 94 | { |
| 95 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 96 | int i; |
| 97 | |
| 98 | if (!tag || !jack) |
| 99 | return NULL; |
| 100 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 101 | if (jack->tag == tag && jack->dev_id == dev_id) |
| 102 | return jack; |
| 103 | return NULL; |
| 104 | } |
| 105 | EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag); |
| 106 | |
| 107 | static struct hda_jack_tbl * |
| 108 | any_jack_tbl_get_from_nid(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 109 | { |
| 110 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 111 | int i; |
| 112 | |
| 113 | if (!nid || !jack) |
| 114 | return NULL; |
| 115 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 116 | if (jack->nid == nid) |
| 117 | return jack; |
| 118 | return NULL; |
| 119 | } |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 120 | |
| 121 | /** |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 122 | * snd_hda_jack_tbl_new - create a jack-table entry for the given NID |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 123 | * @codec: the HDA codec |
| 124 | * @nid: pin NID to assign |
Pierre-Louis Bossart | 4f5c265 | 2020-01-13 15:14:04 -0600 | [diff] [blame] | 125 | * @dev_id: pin device entry id |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 126 | */ |
Takashi Iwai | 81965f1 | 2014-09-11 14:22:03 +0200 | [diff] [blame] | 127 | static struct hda_jack_tbl * |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 128 | snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid, int dev_id) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 129 | { |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 130 | struct hda_jack_tbl *jack = |
| 131 | snd_hda_jack_tbl_get_mst(codec, nid, dev_id); |
| 132 | struct hda_jack_tbl *existing_nid_jack = |
| 133 | any_jack_tbl_get_from_nid(codec, nid); |
| 134 | |
| 135 | WARN_ON(dev_id != 0 && !codec->dp_mst); |
| 136 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 137 | if (jack) |
| 138 | return jack; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 139 | jack = snd_array_new(&codec->jacktbl); |
| 140 | if (!jack) |
| 141 | return NULL; |
| 142 | jack->nid = nid; |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 143 | jack->dev_id = dev_id; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 144 | jack->jack_dirty = 1; |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 145 | if (existing_nid_jack) { |
| 146 | jack->tag = existing_nid_jack->tag; |
| 147 | |
| 148 | /* |
| 149 | * Copy jack_detect from existing_nid_jack to avoid |
| 150 | * snd_hda_jack_detect_enable_callback_mst() making multiple |
| 151 | * SET_UNSOLICITED_ENABLE calls on the same pin. |
| 152 | */ |
| 153 | jack->jack_detect = existing_nid_jack->jack_detect; |
| 154 | } else { |
| 155 | jack->tag = codec->jacktbl.used; |
| 156 | } |
| 157 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 158 | return jack; |
| 159 | } |
| 160 | |
| 161 | void snd_hda_jack_tbl_clear(struct hda_codec *codec) |
| 162 | { |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 163 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 164 | int i; |
| 165 | |
| 166 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 167 | struct hda_jack_callback *cb, *next; |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 168 | |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 169 | /* free jack instances manually when clearing/reconfiguring */ |
| 170 | if (!codec->bus->shutdown && jack->jack) |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 171 | snd_device_free(codec->card, jack->jack); |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 172 | |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 173 | for (cb = jack->callback; cb; cb = next) { |
| 174 | next = cb->next; |
| 175 | kfree(cb); |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 176 | } |
| 177 | } |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 178 | snd_array_free(&codec->jacktbl); |
| 179 | } |
| 180 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 181 | #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE) |
| 182 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 183 | /* update the cached value and notification flag if needed */ |
| 184 | static void jack_detect_update(struct hda_codec *codec, |
| 185 | struct hda_jack_tbl *jack) |
| 186 | { |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 187 | if (!jack->jack_dirty) |
| 188 | return; |
| 189 | |
| 190 | if (jack->phantom_jack) |
| 191 | jack->pin_sense = AC_PINSENSE_PRESENCE; |
| 192 | else |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 193 | jack->pin_sense = read_pin_sense(codec, jack->nid, |
| 194 | jack->dev_id); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 195 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 196 | /* A gating jack indicates the jack is invalid if gating is unplugged */ |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 197 | if (jack->gating_jack && |
| 198 | !snd_hda_jack_detect_mst(codec, jack->gating_jack, jack->dev_id)) |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 199 | jack->pin_sense &= ~AC_PINSENSE_PRESENCE; |
| 200 | |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 201 | jack->jack_dirty = 0; |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 202 | |
| 203 | /* If a jack is gated by this one update it. */ |
| 204 | if (jack->gated_jack) { |
| 205 | struct hda_jack_tbl *gated = |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 206 | snd_hda_jack_tbl_get_mst(codec, jack->gated_jack, |
| 207 | jack->dev_id); |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 208 | if (gated) { |
| 209 | gated->jack_dirty = 1; |
| 210 | jack_detect_update(codec, gated); |
| 211 | } |
| 212 | } |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * snd_hda_set_dirty_all - Mark all the cached as dirty |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 217 | * @codec: the HDA codec |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 218 | * |
| 219 | * This function sets the dirty flag to all entries of jack table. |
| 220 | * It's called from the resume path in hda_codec.c. |
| 221 | */ |
| 222 | void snd_hda_jack_set_dirty_all(struct hda_codec *codec) |
| 223 | { |
| 224 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 225 | int i; |
| 226 | |
| 227 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 228 | if (jack->nid) |
| 229 | jack->jack_dirty = 1; |
| 230 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 231 | EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 232 | |
| 233 | /** |
Nikhil Mahale | 80b917a | 2019-11-19 14:17:07 +0530 | [diff] [blame] | 234 | * snd_hda_jack_pin_sense - execute pin sense measurement |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 235 | * @codec: the CODEC to sense |
| 236 | * @nid: the pin NID to sense |
Pierre-Louis Bossart | 4f5c265 | 2020-01-13 15:14:04 -0600 | [diff] [blame] | 237 | * @dev_id: pin device entry id |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 238 | * |
| 239 | * Execute necessary pin sense measurement and return its Presence Detect, |
| 240 | * Impedance, ELD Valid etc. status bits. |
| 241 | */ |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 242 | u32 snd_hda_jack_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 243 | { |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 244 | struct hda_jack_tbl *jack = |
| 245 | snd_hda_jack_tbl_get_mst(codec, nid, dev_id); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 246 | if (jack) { |
| 247 | jack_detect_update(codec, jack); |
| 248 | return jack->pin_sense; |
| 249 | } |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 250 | return read_pin_sense(codec, nid, dev_id); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 251 | } |
Nikhil Mahale | 80b917a | 2019-11-19 14:17:07 +0530 | [diff] [blame] | 252 | EXPORT_SYMBOL_GPL(snd_hda_jack_pin_sense); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 253 | |
| 254 | /** |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 255 | * snd_hda_jack_detect_state_mst - query pin Presence Detect status |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 256 | * @codec: the CODEC to sense |
| 257 | * @nid: the pin NID to sense |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 258 | * @dev_id: pin device entry id |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 259 | * |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 260 | * Query and return the pin's Presence Detect status, as either |
| 261 | * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM. |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 262 | */ |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 263 | int snd_hda_jack_detect_state_mst(struct hda_codec *codec, |
| 264 | hda_nid_t nid, int dev_id) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 265 | { |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 266 | struct hda_jack_tbl *jack = |
| 267 | snd_hda_jack_tbl_get_mst(codec, nid, dev_id); |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 268 | if (jack && jack->phantom_jack) |
| 269 | return HDA_JACK_PHANTOM; |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 270 | else if (snd_hda_jack_pin_sense(codec, nid, dev_id) & |
| 271 | AC_PINSENSE_PRESENCE) |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 272 | return HDA_JACK_PRESENT; |
| 273 | else |
| 274 | return HDA_JACK_NOT_PRESENT; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 275 | } |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 276 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state_mst); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 277 | |
Hui Wang | 033e404 | 2020-10-22 11:02:21 +0800 | [diff] [blame] | 278 | static struct hda_jack_callback * |
| 279 | find_callback_from_list(struct hda_jack_tbl *jack, |
| 280 | hda_jack_callback_fn func) |
Hui Wang | f4794c6 | 2020-09-30 13:51:46 +0800 | [diff] [blame] | 281 | { |
| 282 | struct hda_jack_callback *cb; |
| 283 | |
Hui Wang | 033e404 | 2020-10-22 11:02:21 +0800 | [diff] [blame] | 284 | if (!func) |
| 285 | return NULL; |
| 286 | |
Hui Wang | f4794c6 | 2020-09-30 13:51:46 +0800 | [diff] [blame] | 287 | for (cb = jack->callback; cb; cb = cb->next) { |
| 288 | if (cb->func == func) |
Hui Wang | 033e404 | 2020-10-22 11:02:21 +0800 | [diff] [blame] | 289 | return cb; |
Hui Wang | f4794c6 | 2020-09-30 13:51:46 +0800 | [diff] [blame] | 290 | } |
Hui Wang | 033e404 | 2020-10-22 11:02:21 +0800 | [diff] [blame] | 291 | |
| 292 | return NULL; |
Hui Wang | f4794c6 | 2020-09-30 13:51:46 +0800 | [diff] [blame] | 293 | } |
| 294 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 295 | /** |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 296 | * snd_hda_jack_detect_enable_mst - enable the jack-detection |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 297 | * @codec: the HDA codec |
| 298 | * @nid: pin NID to enable |
| 299 | * @func: callback function to register |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 300 | * @dev_id: pin device entry id |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 301 | * |
| 302 | * In the case of error, the return value will be a pointer embedded with |
| 303 | * errno. Check and handle the return value appropriately with standard |
| 304 | * macros such as @IS_ERR() and @PTR_ERR(). |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 305 | */ |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 306 | struct hda_jack_callback * |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 307 | snd_hda_jack_detect_enable_callback_mst(struct hda_codec *codec, hda_nid_t nid, |
| 308 | int dev_id, hda_jack_callback_fn func) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 309 | { |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 310 | struct hda_jack_tbl *jack; |
| 311 | struct hda_jack_callback *callback = NULL; |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 312 | int err; |
| 313 | |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 314 | jack = snd_hda_jack_tbl_new(codec, nid, dev_id); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 315 | if (!jack) |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 316 | return ERR_PTR(-ENOMEM); |
Hui Wang | 033e404 | 2020-10-22 11:02:21 +0800 | [diff] [blame] | 317 | |
| 318 | callback = find_callback_from_list(jack, func); |
| 319 | |
| 320 | if (func && !callback) { |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 321 | callback = kzalloc(sizeof(*callback), GFP_KERNEL); |
| 322 | if (!callback) |
| 323 | return ERR_PTR(-ENOMEM); |
| 324 | callback->func = func; |
Takashi Iwai | 2ebab40 | 2016-02-09 10:23:52 +0100 | [diff] [blame] | 325 | callback->nid = jack->nid; |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 326 | callback->dev_id = jack->dev_id; |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 327 | callback->next = jack->callback; |
| 328 | jack->callback = callback; |
| 329 | } |
| 330 | |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 331 | if (jack->jack_detect) |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 332 | return callback; /* already registered */ |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 333 | jack->jack_detect = 1; |
David Henningsson | 34a9318 | 2012-10-17 14:45:05 +0200 | [diff] [blame] | 334 | if (codec->jackpoll_interval > 0) |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 335 | return callback; /* No unsol if we're polling instead */ |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 336 | err = snd_hda_codec_write_cache(codec, nid, 0, |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 337 | AC_VERB_SET_UNSOLICITED_ENABLE, |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 338 | AC_USRSP_EN | jack->tag); |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 339 | if (err < 0) |
| 340 | return ERR_PTR(err); |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 341 | return callback; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 342 | } |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback_mst); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 344 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 345 | /** |
| 346 | * snd_hda_jack_detect_enable - Enable the jack detection on the given pin |
| 347 | * @codec: the HDA codec |
| 348 | * @nid: pin NID to enable jack detection |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 349 | * @dev_id: pin device entry id |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 350 | * |
| 351 | * Enable the jack detection with the default callback. Returns zero if |
| 352 | * successful or a negative error code. |
| 353 | */ |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 354 | int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid, |
| 355 | int dev_id) |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 356 | { |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 357 | return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback_mst(codec, |
| 358 | nid, |
| 359 | dev_id, |
| 360 | NULL)); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 361 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 362 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 363 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 364 | /** |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 365 | * snd_hda_jack_set_gating_jack - Set gating jack. |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 366 | * @codec: the HDA codec |
| 367 | * @gated_nid: gated pin NID |
| 368 | * @gating_nid: gating pin NID |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 369 | * |
| 370 | * Indicates the gated jack is only valid when the gating jack is plugged. |
| 371 | */ |
| 372 | int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid, |
| 373 | hda_nid_t gating_nid) |
| 374 | { |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 375 | struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid, 0); |
| 376 | struct hda_jack_tbl *gating = |
| 377 | snd_hda_jack_tbl_new(codec, gating_nid, 0); |
| 378 | |
| 379 | WARN_ON(codec->dp_mst); |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 380 | |
| 381 | if (!gated || !gating) |
| 382 | return -EINVAL; |
| 383 | |
| 384 | gated->gating_jack = gating_nid; |
| 385 | gating->gated_jack = gated_nid; |
| 386 | |
| 387 | return 0; |
| 388 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 389 | EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack); |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 390 | |
| 391 | /** |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 392 | * snd_hda_jack_report_sync - sync the states of all jacks and report if changed |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 393 | * @codec: the HDA codec |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 394 | */ |
| 395 | void snd_hda_jack_report_sync(struct hda_codec *codec) |
| 396 | { |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 397 | struct hda_jack_tbl *jack; |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 398 | int i, state; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 399 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 400 | /* update all jacks at first */ |
| 401 | jack = codec->jacktbl.list; |
| 402 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 403 | if (jack->nid) |
| 404 | jack_detect_update(codec, jack); |
| 405 | |
| 406 | /* report the updated jacks; it's done after updating all jacks |
| 407 | * to make sure that all gating jacks properly have been set |
| 408 | */ |
| 409 | jack = codec->jacktbl.list; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 410 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 411 | if (jack->nid) { |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 412 | if (!jack->jack || jack->block_report) |
Takashi Iwai | cfc7c9d | 2011-11-11 17:53:03 +0100 | [diff] [blame] | 413 | continue; |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 414 | state = jack->button_state; |
| 415 | if (get_jack_plug_state(jack->pin_sense)) |
| 416 | state |= jack->type; |
| 417 | snd_jack_report(jack->jack, state); |
| 418 | if (jack->button_state) { |
| 419 | snd_jack_report(jack->jack, |
| 420 | state & ~jack->button_state); |
| 421 | jack->button_state = 0; /* button released */ |
| 422 | } |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 423 | } |
| 424 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 425 | EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 426 | |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 427 | /* guess the jack type from the pin-config */ |
| 428 | static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid) |
| 429 | { |
| 430 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 431 | switch (get_defcfg_device(def_conf)) { |
| 432 | case AC_JACK_LINE_OUT: |
| 433 | case AC_JACK_SPEAKER: |
| 434 | return SND_JACK_LINEOUT; |
| 435 | case AC_JACK_HP_OUT: |
| 436 | return SND_JACK_HEADPHONE; |
| 437 | case AC_JACK_SPDIF_OUT: |
| 438 | case AC_JACK_DIG_OTHER_OUT: |
| 439 | return SND_JACK_AVOUT; |
| 440 | case AC_JACK_MIC_IN: |
| 441 | return SND_JACK_MICROPHONE; |
| 442 | default: |
| 443 | return SND_JACK_LINEIN; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | static void hda_free_jack_priv(struct snd_jack *jack) |
| 448 | { |
| 449 | struct hda_jack_tbl *jacks = jack->private_data; |
| 450 | jacks->nid = 0; |
| 451 | jacks->jack = NULL; |
| 452 | } |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 453 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 454 | /** |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 455 | * snd_hda_jack_add_kctl_mst - Add a kctl for the given pin |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 456 | * @codec: the HDA codec |
| 457 | * @nid: pin NID to assign |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 458 | * @dev_id : pin device entry id |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 459 | * @name: string name for the jack |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 460 | * @phantom_jack: flag to deal as a phantom jack |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 461 | * @type: jack type bits to be reported, 0 for guessing from pincfg |
| 462 | * @keymap: optional jack / key mapping |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 463 | * |
| 464 | * This assigns a jack-detection kctl to the given pin. The kcontrol |
| 465 | * will have the given name and index. |
| 466 | */ |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 467 | int snd_hda_jack_add_kctl_mst(struct hda_codec *codec, hda_nid_t nid, |
| 468 | int dev_id, const char *name, bool phantom_jack, |
| 469 | int type, const struct hda_jack_keymap *keymap) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 470 | { |
| 471 | struct hda_jack_tbl *jack; |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 472 | const struct hda_jack_keymap *map; |
| 473 | int err, state, buttons; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 474 | |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 475 | jack = snd_hda_jack_tbl_new(codec, nid, dev_id); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 476 | if (!jack) |
| 477 | return 0; |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 478 | if (jack->jack) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 479 | return 0; /* already created */ |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 480 | |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 481 | if (!type) |
| 482 | type = get_input_jack_type(codec, nid); |
| 483 | |
| 484 | buttons = 0; |
| 485 | if (keymap) { |
| 486 | for (map = keymap; map->type; map++) |
| 487 | buttons |= map->type; |
| 488 | } |
| 489 | |
| 490 | err = snd_jack_new(codec->card, name, type | buttons, |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 491 | &jack->jack, true, phantom_jack); |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 492 | if (err < 0) |
| 493 | return err; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 494 | |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 495 | jack->phantom_jack = !!phantom_jack; |
| 496 | jack->type = type; |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 497 | jack->button_state = 0; |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 498 | jack->jack->private_data = jack; |
| 499 | jack->jack->private_free = hda_free_jack_priv; |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 500 | if (keymap) { |
| 501 | for (map = keymap; map->type; map++) |
| 502 | snd_jack_set_key(jack->jack, map->type, map->key); |
| 503 | } |
| 504 | |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 505 | state = snd_hda_jack_detect_mst(codec, nid, dev_id); |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 506 | snd_jack_report(jack->jack, state ? jack->type : 0); |
| 507 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 508 | return 0; |
| 509 | } |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 510 | EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl_mst); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 511 | |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 512 | static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 513 | const struct auto_pin_cfg *cfg, |
| 514 | const char *base_name) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 515 | { |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 516 | unsigned int def_conf, conn; |
Takashi Iwai | 975cc02 | 2013-06-28 11:56:49 +0200 | [diff] [blame] | 517 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 518 | int err; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 519 | bool phantom_jack; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 520 | |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 521 | WARN_ON(codec->dp_mst); |
| 522 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 523 | if (!nid) |
| 524 | return 0; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 525 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 526 | conn = get_defcfg_connect(def_conf); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 527 | if (conn == AC_JACK_PORT_NONE) |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 528 | return 0; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 529 | phantom_jack = (conn != AC_JACK_PORT_COMPLEX) || |
| 530 | !is_jack_detectable(codec, nid); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 531 | |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 532 | if (base_name) |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 533 | strlcpy(name, base_name, sizeof(name)); |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 534 | else |
| 535 | snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 536 | if (phantom_jack) |
| 537 | /* Example final name: "Internal Mic Phantom Jack" */ |
| 538 | strncat(name, " Phantom", sizeof(name) - strlen(name) - 1); |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 539 | err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack, 0, NULL); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 540 | if (err < 0) |
| 541 | return err; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 542 | |
| 543 | if (!phantom_jack) |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 544 | return snd_hda_jack_detect_enable(codec, nid, 0); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 545 | return 0; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /** |
| 549 | * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg |
Takashi Iwai | a11e9b1 | 2014-10-29 15:06:01 +0100 | [diff] [blame] | 550 | * @codec: the HDA codec |
| 551 | * @cfg: pin config table to parse |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 552 | */ |
| 553 | int snd_hda_jack_add_kctls(struct hda_codec *codec, |
| 554 | const struct auto_pin_cfg *cfg) |
| 555 | { |
| 556 | const hda_nid_t *p; |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 557 | int i, err; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 558 | |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 559 | for (i = 0; i < cfg->num_inputs; i++) { |
| 560 | /* If we have headphone mics; make sure they get the right name |
| 561 | before grabbed by output pins */ |
| 562 | if (cfg->inputs[i].is_headphone_mic) { |
| 563 | if (auto_cfg_hp_outs(cfg) == 1) |
| 564 | err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0], |
| 565 | cfg, "Headphone Mic"); |
| 566 | else |
| 567 | err = add_jack_kctl(codec, cfg->inputs[i].pin, |
| 568 | cfg, "Headphone Mic"); |
| 569 | } else |
| 570 | err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, |
| 571 | NULL); |
| 572 | if (err < 0) |
| 573 | return err; |
| 574 | } |
| 575 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 576 | for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) { |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 577 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 578 | if (err < 0) |
| 579 | return err; |
| 580 | } |
| 581 | for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { |
| 582 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 583 | break; |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 584 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 585 | if (err < 0) |
| 586 | return err; |
| 587 | } |
| 588 | for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { |
| 589 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 590 | break; |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 591 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 592 | if (err < 0) |
| 593 | return err; |
| 594 | } |
| 595 | for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) { |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 596 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 597 | if (err < 0) |
| 598 | return err; |
| 599 | } |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 600 | err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 601 | if (err < 0) |
| 602 | return err; |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 603 | err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 604 | if (err < 0) |
| 605 | return err; |
| 606 | return 0; |
| 607 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 608 | EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 609 | |
Takashi Iwai | e6ce180 | 2018-11-28 14:17:58 +0100 | [diff] [blame] | 610 | static void call_jack_callback(struct hda_codec *codec, unsigned int res, |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 611 | struct hda_jack_tbl *jack) |
| 612 | { |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 613 | struct hda_jack_callback *cb; |
| 614 | |
Takashi Iwai | e6ce180 | 2018-11-28 14:17:58 +0100 | [diff] [blame] | 615 | for (cb = jack->callback; cb; cb = cb->next) { |
| 616 | cb->jack = jack; |
| 617 | cb->unsol_res = res; |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 618 | cb->func(codec, cb); |
Takashi Iwai | e6ce180 | 2018-11-28 14:17:58 +0100 | [diff] [blame] | 619 | } |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 620 | if (jack->gated_jack) { |
| 621 | struct hda_jack_tbl *gated = |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 622 | snd_hda_jack_tbl_get_mst(codec, jack->gated_jack, |
| 623 | jack->dev_id); |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 624 | if (gated) { |
Takashi Iwai | e6ce180 | 2018-11-28 14:17:58 +0100 | [diff] [blame] | 625 | for (cb = gated->callback; cb; cb = cb->next) { |
| 626 | cb->jack = gated; |
| 627 | cb->unsol_res = res; |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 628 | cb->func(codec, cb); |
Takashi Iwai | e6ce180 | 2018-11-28 14:17:58 +0100 | [diff] [blame] | 629 | } |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 630 | } |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 634 | /** |
| 635 | * snd_hda_jack_unsol_event - Handle an unsolicited event |
| 636 | * @codec: the HDA codec |
| 637 | * @res: the unsolicited event data |
| 638 | */ |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 639 | void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res) |
| 640 | { |
| 641 | struct hda_jack_tbl *event; |
Guennadi Liakhovetski | c7e68a6 | 2019-06-11 19:09:07 +0200 | [diff] [blame] | 642 | int tag = (res & AC_UNSOL_RES_TAG) >> AC_UNSOL_RES_TAG_SHIFT; |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 643 | |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 644 | if (codec->dp_mst) { |
| 645 | int dev_entry = |
| 646 | (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT; |
| 647 | |
| 648 | event = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry); |
| 649 | } else { |
| 650 | event = snd_hda_jack_tbl_get_from_tag(codec, tag, 0); |
| 651 | } |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 652 | if (!event) |
| 653 | return; |
| 654 | event->jack_dirty = 1; |
| 655 | |
Takashi Iwai | e6ce180 | 2018-11-28 14:17:58 +0100 | [diff] [blame] | 656 | call_jack_callback(codec, res, event); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 657 | snd_hda_jack_report_sync(codec); |
| 658 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 659 | EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 660 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 661 | /** |
| 662 | * snd_hda_jack_poll_all - Poll all jacks |
| 663 | * @codec: the HDA codec |
| 664 | * |
| 665 | * Poll all detectable jacks with dirty flag, update the status, call |
| 666 | * callbacks and call snd_hda_jack_report_sync() if any changes are found. |
| 667 | */ |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 668 | void snd_hda_jack_poll_all(struct hda_codec *codec) |
| 669 | { |
| 670 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 671 | int i, changes = 0; |
| 672 | |
| 673 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 674 | unsigned int old_sense; |
| 675 | if (!jack->nid || !jack->jack_dirty || jack->phantom_jack) |
| 676 | continue; |
| 677 | old_sense = get_jack_plug_state(jack->pin_sense); |
| 678 | jack_detect_update(codec, jack); |
| 679 | if (old_sense == get_jack_plug_state(jack->pin_sense)) |
| 680 | continue; |
| 681 | changes = 1; |
Takashi Iwai | e6ce180 | 2018-11-28 14:17:58 +0100 | [diff] [blame] | 682 | call_jack_callback(codec, 0, jack); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 683 | } |
| 684 | if (changes) |
| 685 | snd_hda_jack_report_sync(codec); |
| 686 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 687 | EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 688 | |