Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 1 | /* |
| 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 Iwai | bf815bf | 2011-11-16 14:28:33 +0100 | [diff] [blame] | 14 | #include <linux/export.h> |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 15 | #include <sound/core.h> |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 16 | #include <sound/control.h> |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 17 | #include <sound/jack.h> |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 18 | #include "hda_codec.h" |
| 19 | #include "hda_local.h" |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 20 | #include "hda_auto_parser.h" |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 21 | #include "hda_jack.h" |
| 22 | |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 23 | bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid) |
| 24 | { |
Takashi Iwai | 71b1e9e | 2012-02-13 11:55:02 +0100 | [diff] [blame] | 25 | if (codec->no_jack_detect) |
| 26 | return false; |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 27 | if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT)) |
| 28 | return false; |
David Henningsson | 5fe8e1e | 2012-09-07 07:25:44 +0200 | [diff] [blame] | 29 | if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) & |
| 30 | AC_DEFCFG_MISC_NO_PRESENCE) |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 31 | return false; |
Takashi Iwai | 76a19c6 | 2013-01-07 12:11:33 +0100 | [diff] [blame] | 32 | if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) && |
| 33 | !codec->jackpoll_interval) |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 34 | return false; |
| 35 | return true; |
| 36 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 37 | EXPORT_SYMBOL_GPL(is_jack_detectable); |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 38 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 39 | /* execute pin sense measurement */ |
| 40 | static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid) |
| 41 | { |
| 42 | u32 pincap; |
Takashi Iwai | 9cc159c | 2012-12-21 15:31:41 +0100 | [diff] [blame] | 43 | u32 val; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 44 | |
| 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 Iwai | 9cc159c | 2012-12-21 15:31:41 +0100 | [diff] [blame] | 51 | val = snd_hda_codec_read(codec, nid, 0, |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 52 | AC_VERB_GET_PIN_SENSE, 0); |
Takashi Iwai | 9cc159c | 2012-12-21 15:31:41 +0100 | [diff] [blame] | 53 | if (codec->inv_jack_detect) |
| 54 | val ^= AC_PINSENSE_PRESENCE; |
| 55 | return val; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * snd_hda_jack_tbl_get - query the jack-table entry for the given NID |
| 60 | */ |
| 61 | struct hda_jack_tbl * |
| 62 | snd_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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 75 | |
| 76 | /** |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 77 | * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag |
| 78 | */ |
| 79 | struct hda_jack_tbl * |
| 80 | snd_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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 92 | EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 93 | |
| 94 | /** |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 95 | * snd_hda_jack_tbl_new - create a jack-table entry for the given NID |
| 96 | */ |
Takashi Iwai | 81965f1 | 2014-09-11 14:22:03 +0200 | [diff] [blame] | 97 | static struct hda_jack_tbl * |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 98 | snd_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 Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 103 | jack = snd_array_new(&codec->jacktbl); |
| 104 | if (!jack) |
| 105 | return NULL; |
| 106 | jack->nid = nid; |
| 107 | jack->jack_dirty = 1; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 108 | jack->tag = codec->jacktbl.used; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 109 | return jack; |
| 110 | } |
| 111 | |
| 112 | void snd_hda_jack_tbl_clear(struct hda_codec *codec) |
| 113 | { |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 114 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 115 | int i; |
| 116 | |
| 117 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 118 | struct hda_jack_callback *cb, *next; |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 119 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 120 | /* free jack instances manually when clearing/reconfiguring */ |
| 121 | if (!codec->bus->shutdown && jack->jack) |
| 122 | snd_device_free(codec->bus->card, jack->jack); |
| 123 | #endif |
| 124 | for (cb = jack->callback; cb; cb = next) { |
| 125 | next = cb->next; |
| 126 | kfree(cb); |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 129 | snd_array_free(&codec->jacktbl); |
| 130 | } |
| 131 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 132 | #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE) |
| 133 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 134 | /* update the cached value and notification flag if needed */ |
| 135 | static void jack_detect_update(struct hda_codec *codec, |
| 136 | struct hda_jack_tbl *jack) |
| 137 | { |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 138 | if (!jack->jack_dirty) |
| 139 | return; |
| 140 | |
| 141 | if (jack->phantom_jack) |
| 142 | jack->pin_sense = AC_PINSENSE_PRESENCE; |
| 143 | else |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 144 | jack->pin_sense = read_pin_sense(codec, jack->nid); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 145 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 146 | /* A gating jack indicates the jack is invalid if gating is unplugged */ |
| 147 | if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack)) |
| 148 | jack->pin_sense &= ~AC_PINSENSE_PRESENCE; |
| 149 | |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 150 | jack->jack_dirty = 0; |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 151 | |
| 152 | /* If a jack is gated by this one update it. */ |
| 153 | if (jack->gated_jack) { |
| 154 | struct hda_jack_tbl *gated = |
| 155 | snd_hda_jack_tbl_get(codec, jack->gated_jack); |
| 156 | if (gated) { |
| 157 | gated->jack_dirty = 1; |
| 158 | jack_detect_update(codec, gated); |
| 159 | } |
| 160 | } |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
| 164 | * snd_hda_set_dirty_all - Mark all the cached as dirty |
| 165 | * |
| 166 | * This function sets the dirty flag to all entries of jack table. |
| 167 | * It's called from the resume path in hda_codec.c. |
| 168 | */ |
| 169 | void snd_hda_jack_set_dirty_all(struct hda_codec *codec) |
| 170 | { |
| 171 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 172 | int i; |
| 173 | |
| 174 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 175 | if (jack->nid) |
| 176 | jack->jack_dirty = 1; |
| 177 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 178 | EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * snd_hda_pin_sense - execute pin sense measurement |
| 182 | * @codec: the CODEC to sense |
| 183 | * @nid: the pin NID to sense |
| 184 | * |
| 185 | * Execute necessary pin sense measurement and return its Presence Detect, |
| 186 | * Impedance, ELD Valid etc. status bits. |
| 187 | */ |
| 188 | u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid) |
| 189 | { |
| 190 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
| 191 | if (jack) { |
| 192 | jack_detect_update(codec, jack); |
| 193 | return jack->pin_sense; |
| 194 | } |
| 195 | return read_pin_sense(codec, nid); |
| 196 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 197 | EXPORT_SYMBOL_GPL(snd_hda_pin_sense); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 198 | |
| 199 | /** |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 200 | * snd_hda_jack_detect_state - query pin Presence Detect status |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 201 | * @codec: the CODEC to sense |
| 202 | * @nid: the pin NID to sense |
| 203 | * |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 204 | * Query and return the pin's Presence Detect status, as either |
| 205 | * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM. |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 206 | */ |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 207 | int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 208 | { |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 209 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
| 210 | if (jack && jack->phantom_jack) |
| 211 | return HDA_JACK_PHANTOM; |
| 212 | else if (snd_hda_pin_sense(codec, nid) & AC_PINSENSE_PRESENCE) |
| 213 | return HDA_JACK_PRESENT; |
| 214 | else |
| 215 | return HDA_JACK_NOT_PRESENT; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 216 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 217 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * snd_hda_jack_detect_enable - enable the jack-detection |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 221 | * |
| 222 | * In the case of error, the return value will be a pointer embedded with |
| 223 | * errno. Check and handle the return value appropriately with standard |
| 224 | * macros such as @IS_ERR() and @PTR_ERR(). |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 225 | */ |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 226 | struct hda_jack_callback * |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 227 | snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 228 | hda_jack_callback_fn func) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 229 | { |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 230 | struct hda_jack_tbl *jack; |
| 231 | struct hda_jack_callback *callback = NULL; |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 232 | int err; |
| 233 | |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 234 | jack = snd_hda_jack_tbl_new(codec, nid); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 235 | if (!jack) |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 236 | return ERR_PTR(-ENOMEM); |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 237 | if (func) { |
| 238 | callback = kzalloc(sizeof(*callback), GFP_KERNEL); |
| 239 | if (!callback) |
| 240 | return ERR_PTR(-ENOMEM); |
| 241 | callback->func = func; |
| 242 | callback->tbl = jack; |
| 243 | callback->next = jack->callback; |
| 244 | jack->callback = callback; |
| 245 | } |
| 246 | |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 247 | if (jack->jack_detect) |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 248 | return callback; /* already registered */ |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 249 | jack->jack_detect = 1; |
David Henningsson | 34a9318 | 2012-10-17 14:45:05 +0200 | [diff] [blame] | 250 | if (codec->jackpoll_interval > 0) |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 251 | return callback; /* No unsol if we're polling instead */ |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 252 | err = snd_hda_codec_write_cache(codec, nid, 0, |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 253 | AC_VERB_SET_UNSOLICITED_ENABLE, |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 254 | AC_USRSP_EN | jack->tag); |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 255 | if (err < 0) |
| 256 | return ERR_PTR(err); |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 257 | return callback; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 258 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 259 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 260 | |
Takashi Iwai | 62f949b | 2014-09-11 14:06:53 +0200 | [diff] [blame] | 261 | int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid) |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 262 | { |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 263 | return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback(codec, nid, NULL)); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 264 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 265 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 266 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 267 | /** |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 268 | * snd_hda_jack_set_gating_jack - Set gating jack. |
| 269 | * |
| 270 | * Indicates the gated jack is only valid when the gating jack is plugged. |
| 271 | */ |
| 272 | int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid, |
| 273 | hda_nid_t gating_nid) |
| 274 | { |
Chih-Chung Chang | bde7bc6 | 2013-08-05 16:38:42 +0800 | [diff] [blame] | 275 | struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid); |
| 276 | struct hda_jack_tbl *gating = snd_hda_jack_tbl_new(codec, gating_nid); |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 277 | |
| 278 | if (!gated || !gating) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | gated->gating_jack = gating_nid; |
| 282 | gating->gated_jack = gated_nid; |
| 283 | |
| 284 | return 0; |
| 285 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 286 | EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack); |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 287 | |
| 288 | /** |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 289 | * snd_hda_jack_report_sync - sync the states of all jacks and report if changed |
| 290 | */ |
| 291 | void snd_hda_jack_report_sync(struct hda_codec *codec) |
| 292 | { |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 293 | struct hda_jack_tbl *jack; |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 294 | int i, state; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 295 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 296 | /* update all jacks at first */ |
| 297 | jack = codec->jacktbl.list; |
| 298 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 299 | if (jack->nid) |
| 300 | jack_detect_update(codec, jack); |
| 301 | |
| 302 | /* report the updated jacks; it's done after updating all jacks |
| 303 | * to make sure that all gating jacks properly have been set |
| 304 | */ |
| 305 | jack = codec->jacktbl.list; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 306 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 307 | if (jack->nid) { |
David Henningsson | 0f56895 | 2013-11-07 13:38:24 +0100 | [diff] [blame] | 308 | if (!jack->kctl || jack->block_report) |
Takashi Iwai | cfc7c9d | 2011-11-11 17:53:03 +0100 | [diff] [blame] | 309 | continue; |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 310 | state = get_jack_plug_state(jack->pin_sense); |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 311 | snd_kctl_jack_report(codec->bus->card, jack->kctl, state); |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 312 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 313 | if (jack->jack) |
| 314 | snd_jack_report(jack->jack, |
| 315 | state ? jack->type : 0); |
| 316 | #endif |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 317 | } |
| 318 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 319 | EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 320 | |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 321 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 322 | /* guess the jack type from the pin-config */ |
| 323 | static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid) |
| 324 | { |
| 325 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 326 | switch (get_defcfg_device(def_conf)) { |
| 327 | case AC_JACK_LINE_OUT: |
| 328 | case AC_JACK_SPEAKER: |
| 329 | return SND_JACK_LINEOUT; |
| 330 | case AC_JACK_HP_OUT: |
| 331 | return SND_JACK_HEADPHONE; |
| 332 | case AC_JACK_SPDIF_OUT: |
| 333 | case AC_JACK_DIG_OTHER_OUT: |
| 334 | return SND_JACK_AVOUT; |
| 335 | case AC_JACK_MIC_IN: |
| 336 | return SND_JACK_MICROPHONE; |
| 337 | default: |
| 338 | return SND_JACK_LINEIN; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static void hda_free_jack_priv(struct snd_jack *jack) |
| 343 | { |
| 344 | struct hda_jack_tbl *jacks = jack->private_data; |
| 345 | jacks->nid = 0; |
| 346 | jacks->jack = NULL; |
| 347 | } |
| 348 | #endif |
| 349 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 350 | /** |
| 351 | * snd_hda_jack_add_kctl - Add a kctl for the given pin |
| 352 | * |
| 353 | * This assigns a jack-detection kctl to the given pin. The kcontrol |
| 354 | * will have the given name and index. |
| 355 | */ |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 356 | static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
| 357 | const char *name, int idx, bool phantom_jack) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 358 | { |
| 359 | struct hda_jack_tbl *jack; |
| 360 | struct snd_kcontrol *kctl; |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 361 | int err, state; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 362 | |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 363 | jack = snd_hda_jack_tbl_new(codec, nid); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 364 | if (!jack) |
| 365 | return 0; |
| 366 | if (jack->kctl) |
| 367 | return 0; /* already created */ |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 368 | kctl = snd_kctl_jack_new(name, idx, codec); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 369 | if (!kctl) |
| 370 | return -ENOMEM; |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 371 | err = snd_hda_ctl_add(codec, nid, kctl); |
| 372 | if (err < 0) |
| 373 | return err; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 374 | jack->kctl = kctl; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 375 | jack->phantom_jack = !!phantom_jack; |
| 376 | |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 377 | state = snd_hda_jack_detect(codec, nid); |
| 378 | snd_kctl_jack_report(codec->bus->card, kctl, state); |
| 379 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 380 | if (!phantom_jack) { |
| 381 | jack->type = get_input_jack_type(codec, nid); |
| 382 | err = snd_jack_new(codec->bus->card, name, jack->type, |
| 383 | &jack->jack); |
| 384 | if (err < 0) |
| 385 | return err; |
| 386 | jack->jack->private_data = jack; |
| 387 | jack->jack->private_free = hda_free_jack_priv; |
| 388 | snd_jack_report(jack->jack, state ? jack->type : 0); |
| 389 | } |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 390 | #endif |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 391 | return 0; |
| 392 | } |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 393 | |
| 394 | int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
| 395 | const char *name, int idx) |
| 396 | { |
| 397 | return __snd_hda_jack_add_kctl(codec, nid, name, idx, false); |
| 398 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 399 | EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 400 | |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 401 | /* get the unique index number for the given kctl name */ |
| 402 | static int get_unique_index(struct hda_codec *codec, const char *name, int idx) |
| 403 | { |
| 404 | struct hda_jack_tbl *jack; |
| 405 | int i, len = strlen(name); |
| 406 | again: |
| 407 | jack = codec->jacktbl.list; |
| 408 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 409 | /* jack->kctl.id contains "XXX Jack" name string with index */ |
| 410 | if (jack->kctl && |
| 411 | !strncmp(name, jack->kctl->id.name, len) && |
| 412 | !strcmp(" Jack", jack->kctl->id.name + len) && |
| 413 | jack->kctl->id.index == idx) { |
| 414 | idx++; |
| 415 | goto again; |
| 416 | } |
| 417 | } |
| 418 | return idx; |
| 419 | } |
| 420 | |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 421 | 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] | 422 | const struct auto_pin_cfg *cfg, |
| 423 | const char *base_name) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 424 | { |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 425 | unsigned int def_conf, conn; |
Takashi Iwai | 975cc02 | 2013-06-28 11:56:49 +0200 | [diff] [blame] | 426 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 427 | int idx, err; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 428 | bool phantom_jack; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 429 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 430 | if (!nid) |
| 431 | return 0; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 432 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 433 | conn = get_defcfg_connect(def_conf); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 434 | if (conn == AC_JACK_PORT_NONE) |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 435 | return 0; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 436 | phantom_jack = (conn != AC_JACK_PORT_COMPLEX) || |
| 437 | !is_jack_detectable(codec, nid); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 438 | |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 439 | if (base_name) { |
| 440 | strlcpy(name, base_name, sizeof(name)); |
| 441 | idx = 0; |
| 442 | } else |
| 443 | snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 444 | if (phantom_jack) |
| 445 | /* Example final name: "Internal Mic Phantom Jack" */ |
| 446 | strncat(name, " Phantom", sizeof(name) - strlen(name) - 1); |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 447 | idx = get_unique_index(codec, name, idx); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 448 | err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 449 | if (err < 0) |
| 450 | return err; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 451 | |
| 452 | if (!phantom_jack) |
Takashi Iwai | 62f949b | 2014-09-11 14:06:53 +0200 | [diff] [blame] | 453 | return snd_hda_jack_detect_enable(codec, nid); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 454 | return 0; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
| 458 | * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 459 | */ |
| 460 | int snd_hda_jack_add_kctls(struct hda_codec *codec, |
| 461 | const struct auto_pin_cfg *cfg) |
| 462 | { |
| 463 | const hda_nid_t *p; |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 464 | int i, err; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 465 | |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 466 | for (i = 0; i < cfg->num_inputs; i++) { |
| 467 | /* If we have headphone mics; make sure they get the right name |
| 468 | before grabbed by output pins */ |
| 469 | if (cfg->inputs[i].is_headphone_mic) { |
| 470 | if (auto_cfg_hp_outs(cfg) == 1) |
| 471 | err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0], |
| 472 | cfg, "Headphone Mic"); |
| 473 | else |
| 474 | err = add_jack_kctl(codec, cfg->inputs[i].pin, |
| 475 | cfg, "Headphone Mic"); |
| 476 | } else |
| 477 | err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, |
| 478 | NULL); |
| 479 | if (err < 0) |
| 480 | return err; |
| 481 | } |
| 482 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 483 | 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] | 484 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 485 | if (err < 0) |
| 486 | return err; |
| 487 | } |
| 488 | for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { |
| 489 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 490 | break; |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 491 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 492 | if (err < 0) |
| 493 | return err; |
| 494 | } |
| 495 | for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { |
| 496 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 497 | break; |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 498 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 499 | if (err < 0) |
| 500 | return err; |
| 501 | } |
| 502 | 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] | 503 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 504 | if (err < 0) |
| 505 | return err; |
| 506 | } |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 507 | err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 508 | if (err < 0) |
| 509 | return err; |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 510 | err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 511 | if (err < 0) |
| 512 | return err; |
| 513 | return 0; |
| 514 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 515 | EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 516 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 517 | static void call_jack_callback(struct hda_codec *codec, |
| 518 | struct hda_jack_tbl *jack) |
| 519 | { |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 520 | struct hda_jack_callback *cb; |
| 521 | |
| 522 | for (cb = jack->callback; cb; cb = cb->next) |
| 523 | cb->func(codec, cb); |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 524 | if (jack->gated_jack) { |
| 525 | struct hda_jack_tbl *gated = |
| 526 | snd_hda_jack_tbl_get(codec, jack->gated_jack); |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame^] | 527 | if (gated) { |
| 528 | for (cb = gated->callback; cb; cb = cb->next) |
| 529 | cb->func(codec, cb); |
| 530 | } |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 534 | void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res) |
| 535 | { |
| 536 | struct hda_jack_tbl *event; |
| 537 | int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f; |
| 538 | |
| 539 | event = snd_hda_jack_tbl_get_from_tag(codec, tag); |
| 540 | if (!event) |
| 541 | return; |
| 542 | event->jack_dirty = 1; |
| 543 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 544 | call_jack_callback(codec, event); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 545 | snd_hda_jack_report_sync(codec); |
| 546 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 547 | EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 548 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 549 | void snd_hda_jack_poll_all(struct hda_codec *codec) |
| 550 | { |
| 551 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 552 | int i, changes = 0; |
| 553 | |
| 554 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 555 | unsigned int old_sense; |
| 556 | if (!jack->nid || !jack->jack_dirty || jack->phantom_jack) |
| 557 | continue; |
| 558 | old_sense = get_jack_plug_state(jack->pin_sense); |
| 559 | jack_detect_update(codec, jack); |
| 560 | if (old_sense == get_jack_plug_state(jack->pin_sense)) |
| 561 | continue; |
| 562 | changes = 1; |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 563 | call_jack_callback(codec, jack); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 564 | } |
| 565 | if (changes) |
| 566 | snd_hda_jack_report_sync(codec); |
| 567 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 568 | EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 569 | |