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 | */ |
| 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 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 111 | EXPORT_SYMBOL_GPL(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 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 175 | EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 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 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 194 | EXPORT_SYMBOL_GPL(snd_hda_pin_sense); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 195 | |
| 196 | /** |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 197 | * snd_hda_jack_detect_state - query pin Presence Detect status |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 198 | * @codec: the CODEC to sense |
| 199 | * @nid: the pin NID to sense |
| 200 | * |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 201 | * Query and return the pin's Presence Detect status, as either |
| 202 | * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM. |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 203 | */ |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 204 | 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] | 205 | { |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 206 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
| 207 | if (jack && jack->phantom_jack) |
| 208 | return HDA_JACK_PHANTOM; |
| 209 | else if (snd_hda_pin_sense(codec, nid) & AC_PINSENSE_PRESENCE) |
| 210 | return HDA_JACK_PRESENT; |
| 211 | else |
| 212 | return HDA_JACK_NOT_PRESENT; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 213 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 214 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * snd_hda_jack_detect_enable - enable the jack-detection |
| 218 | */ |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 219 | int snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid, |
| 220 | unsigned char action, |
| 221 | hda_jack_callback cb) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 222 | { |
| 223 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid); |
| 224 | if (!jack) |
| 225 | return -ENOMEM; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 226 | if (jack->jack_detect) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 227 | return 0; /* already registered */ |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 228 | jack->jack_detect = 1; |
| 229 | if (action) |
| 230 | jack->action = action; |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 231 | if (cb) |
| 232 | jack->callback = cb; |
David Henningsson | 34a9318 | 2012-10-17 14:45:05 +0200 | [diff] [blame] | 233 | if (codec->jackpoll_interval > 0) |
| 234 | return 0; /* No unsol if we're polling instead */ |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 235 | return snd_hda_codec_write_cache(codec, nid, 0, |
| 236 | AC_VERB_SET_UNSOLICITED_ENABLE, |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 237 | AC_USRSP_EN | jack->tag); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 238 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 239 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 240 | |
| 241 | int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid, |
| 242 | unsigned char action) |
| 243 | { |
| 244 | return snd_hda_jack_detect_enable_callback(codec, nid, action, NULL); |
| 245 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 246 | EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 247 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 248 | /** |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 249 | * snd_hda_jack_set_gating_jack - Set gating jack. |
| 250 | * |
| 251 | * Indicates the gated jack is only valid when the gating jack is plugged. |
| 252 | */ |
| 253 | int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid, |
| 254 | hda_nid_t gating_nid) |
| 255 | { |
Chih-Chung Chang | bde7bc6 | 2013-08-05 16:38:42 +0800 | [diff] [blame] | 256 | struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid); |
| 257 | struct hda_jack_tbl *gating = snd_hda_jack_tbl_new(codec, gating_nid); |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 258 | |
| 259 | if (!gated || !gating) |
| 260 | return -EINVAL; |
| 261 | |
| 262 | gated->gating_jack = gating_nid; |
| 263 | gating->gated_jack = gated_nid; |
| 264 | |
| 265 | return 0; |
| 266 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 267 | EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack); |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 268 | |
| 269 | /** |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 270 | * snd_hda_jack_report_sync - sync the states of all jacks and report if changed |
| 271 | */ |
| 272 | void snd_hda_jack_report_sync(struct hda_codec *codec) |
| 273 | { |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 274 | struct hda_jack_tbl *jack; |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 275 | int i, state; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 276 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 277 | /* update all jacks at first */ |
| 278 | jack = codec->jacktbl.list; |
| 279 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 280 | if (jack->nid) |
| 281 | jack_detect_update(codec, jack); |
| 282 | |
| 283 | /* report the updated jacks; it's done after updating all jacks |
| 284 | * to make sure that all gating jacks properly have been set |
| 285 | */ |
| 286 | jack = codec->jacktbl.list; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 287 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 288 | if (jack->nid) { |
David Henningsson | 0f56895 | 2013-11-07 13:38:24 +0100 | [diff] [blame] | 289 | if (!jack->kctl || jack->block_report) |
Takashi Iwai | cfc7c9d | 2011-11-11 17:53:03 +0100 | [diff] [blame] | 290 | continue; |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 291 | state = get_jack_plug_state(jack->pin_sense); |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 292 | snd_kctl_jack_report(codec->bus->card, jack->kctl, state); |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 293 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 294 | if (jack->jack) |
| 295 | snd_jack_report(jack->jack, |
| 296 | state ? jack->type : 0); |
| 297 | #endif |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 298 | } |
| 299 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 300 | EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 301 | |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 302 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 303 | /* guess the jack type from the pin-config */ |
| 304 | static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid) |
| 305 | { |
| 306 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 307 | switch (get_defcfg_device(def_conf)) { |
| 308 | case AC_JACK_LINE_OUT: |
| 309 | case AC_JACK_SPEAKER: |
| 310 | return SND_JACK_LINEOUT; |
| 311 | case AC_JACK_HP_OUT: |
| 312 | return SND_JACK_HEADPHONE; |
| 313 | case AC_JACK_SPDIF_OUT: |
| 314 | case AC_JACK_DIG_OTHER_OUT: |
| 315 | return SND_JACK_AVOUT; |
| 316 | case AC_JACK_MIC_IN: |
| 317 | return SND_JACK_MICROPHONE; |
| 318 | default: |
| 319 | return SND_JACK_LINEIN; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static void hda_free_jack_priv(struct snd_jack *jack) |
| 324 | { |
| 325 | struct hda_jack_tbl *jacks = jack->private_data; |
| 326 | jacks->nid = 0; |
| 327 | jacks->jack = NULL; |
| 328 | } |
| 329 | #endif |
| 330 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 331 | /** |
| 332 | * snd_hda_jack_add_kctl - Add a kctl for the given pin |
| 333 | * |
| 334 | * This assigns a jack-detection kctl to the given pin. The kcontrol |
| 335 | * will have the given name and index. |
| 336 | */ |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 337 | static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
| 338 | const char *name, int idx, bool phantom_jack) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 339 | { |
| 340 | struct hda_jack_tbl *jack; |
| 341 | struct snd_kcontrol *kctl; |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 342 | int err, state; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 343 | |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 344 | jack = snd_hda_jack_tbl_new(codec, nid); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 345 | if (!jack) |
| 346 | return 0; |
| 347 | if (jack->kctl) |
| 348 | return 0; /* already created */ |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 349 | kctl = snd_kctl_jack_new(name, idx, codec); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 350 | if (!kctl) |
| 351 | return -ENOMEM; |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 352 | err = snd_hda_ctl_add(codec, nid, kctl); |
| 353 | if (err < 0) |
| 354 | return err; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 355 | jack->kctl = kctl; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 356 | jack->phantom_jack = !!phantom_jack; |
| 357 | |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 358 | state = snd_hda_jack_detect(codec, nid); |
| 359 | snd_kctl_jack_report(codec->bus->card, kctl, state); |
| 360 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 361 | if (!phantom_jack) { |
| 362 | jack->type = get_input_jack_type(codec, nid); |
| 363 | err = snd_jack_new(codec->bus->card, name, jack->type, |
| 364 | &jack->jack); |
| 365 | if (err < 0) |
| 366 | return err; |
| 367 | jack->jack->private_data = jack; |
| 368 | jack->jack->private_free = hda_free_jack_priv; |
| 369 | snd_jack_report(jack->jack, state ? jack->type : 0); |
| 370 | } |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 371 | #endif |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 372 | return 0; |
| 373 | } |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 374 | |
| 375 | int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
| 376 | const char *name, int idx) |
| 377 | { |
| 378 | return __snd_hda_jack_add_kctl(codec, nid, name, idx, false); |
| 379 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 380 | EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 381 | |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 382 | /* get the unique index number for the given kctl name */ |
| 383 | static int get_unique_index(struct hda_codec *codec, const char *name, int idx) |
| 384 | { |
| 385 | struct hda_jack_tbl *jack; |
| 386 | int i, len = strlen(name); |
| 387 | again: |
| 388 | jack = codec->jacktbl.list; |
| 389 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 390 | /* jack->kctl.id contains "XXX Jack" name string with index */ |
| 391 | if (jack->kctl && |
| 392 | !strncmp(name, jack->kctl->id.name, len) && |
| 393 | !strcmp(" Jack", jack->kctl->id.name + len) && |
| 394 | jack->kctl->id.index == idx) { |
| 395 | idx++; |
| 396 | goto again; |
| 397 | } |
| 398 | } |
| 399 | return idx; |
| 400 | } |
| 401 | |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 402 | 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] | 403 | const struct auto_pin_cfg *cfg, |
| 404 | const char *base_name) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 405 | { |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 406 | unsigned int def_conf, conn; |
Takashi Iwai | 975cc02 | 2013-06-28 11:56:49 +0200 | [diff] [blame] | 407 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 408 | int idx, err; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 409 | bool phantom_jack; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 410 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 411 | if (!nid) |
| 412 | return 0; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 413 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 414 | conn = get_defcfg_connect(def_conf); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 415 | if (conn == AC_JACK_PORT_NONE) |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 416 | return 0; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 417 | phantom_jack = (conn != AC_JACK_PORT_COMPLEX) || |
| 418 | !is_jack_detectable(codec, nid); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 419 | |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 420 | if (base_name) { |
| 421 | strlcpy(name, base_name, sizeof(name)); |
| 422 | idx = 0; |
| 423 | } else |
| 424 | snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 425 | if (phantom_jack) |
| 426 | /* Example final name: "Internal Mic Phantom Jack" */ |
| 427 | strncat(name, " Phantom", sizeof(name) - strlen(name) - 1); |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 428 | idx = get_unique_index(codec, name, idx); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 429 | err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 430 | if (err < 0) |
| 431 | return err; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 432 | |
| 433 | if (!phantom_jack) |
| 434 | return snd_hda_jack_detect_enable(codec, nid, 0); |
| 435 | return 0; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
| 439 | * 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] | 440 | */ |
| 441 | int snd_hda_jack_add_kctls(struct hda_codec *codec, |
| 442 | const struct auto_pin_cfg *cfg) |
| 443 | { |
| 444 | const hda_nid_t *p; |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 445 | int i, err; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 446 | |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 447 | for (i = 0; i < cfg->num_inputs; i++) { |
| 448 | /* If we have headphone mics; make sure they get the right name |
| 449 | before grabbed by output pins */ |
| 450 | if (cfg->inputs[i].is_headphone_mic) { |
| 451 | if (auto_cfg_hp_outs(cfg) == 1) |
| 452 | err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0], |
| 453 | cfg, "Headphone Mic"); |
| 454 | else |
| 455 | err = add_jack_kctl(codec, cfg->inputs[i].pin, |
| 456 | cfg, "Headphone Mic"); |
| 457 | } else |
| 458 | err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, |
| 459 | NULL); |
| 460 | if (err < 0) |
| 461 | return err; |
| 462 | } |
| 463 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 464 | 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] | 465 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 466 | if (err < 0) |
| 467 | return err; |
| 468 | } |
| 469 | for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { |
| 470 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 471 | break; |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 472 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 473 | if (err < 0) |
| 474 | return err; |
| 475 | } |
| 476 | for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { |
| 477 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 478 | break; |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 479 | err = add_jack_kctl(codec, *p, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 480 | if (err < 0) |
| 481 | return err; |
| 482 | } |
| 483 | 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] | 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 | } |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 488 | err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 489 | if (err < 0) |
| 490 | return err; |
David Henningsson | b26b511 | 2013-04-11 11:30:30 +0200 | [diff] [blame] | 491 | err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 492 | if (err < 0) |
| 493 | return err; |
| 494 | return 0; |
| 495 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 496 | EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 497 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 498 | static void call_jack_callback(struct hda_codec *codec, |
| 499 | struct hda_jack_tbl *jack) |
| 500 | { |
| 501 | if (jack->callback) |
| 502 | jack->callback(codec, jack); |
| 503 | if (jack->gated_jack) { |
| 504 | struct hda_jack_tbl *gated = |
| 505 | snd_hda_jack_tbl_get(codec, jack->gated_jack); |
| 506 | if (gated && gated->callback) |
| 507 | gated->callback(codec, gated); |
| 508 | } |
| 509 | } |
| 510 | |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 511 | void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res) |
| 512 | { |
| 513 | struct hda_jack_tbl *event; |
| 514 | int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f; |
| 515 | |
| 516 | event = snd_hda_jack_tbl_get_from_tag(codec, tag); |
| 517 | if (!event) |
| 518 | return; |
| 519 | event->jack_dirty = 1; |
| 520 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 521 | call_jack_callback(codec, event); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 522 | snd_hda_jack_report_sync(codec); |
| 523 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 524 | EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event); |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 525 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 526 | void snd_hda_jack_poll_all(struct hda_codec *codec) |
| 527 | { |
| 528 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 529 | int i, changes = 0; |
| 530 | |
| 531 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 532 | unsigned int old_sense; |
| 533 | if (!jack->nid || !jack->jack_dirty || jack->phantom_jack) |
| 534 | continue; |
| 535 | old_sense = get_jack_plug_state(jack->pin_sense); |
| 536 | jack_detect_update(codec, jack); |
| 537 | if (old_sense == get_jack_plug_state(jack->pin_sense)) |
| 538 | continue; |
| 539 | changes = 1; |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 540 | call_jack_callback(codec, jack); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 541 | } |
| 542 | if (changes) |
| 543 | snd_hda_jack_report_sync(codec); |
| 544 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 545 | EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all); |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 546 | |