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