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