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> |
| 14 | #include <sound/core.h> |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 15 | #include <sound/control.h> |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 16 | #include <sound/jack.h> |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 17 | #include "hda_codec.h" |
| 18 | #include "hda_local.h" |
| 19 | #include "hda_jack.h" |
| 20 | |
| 21 | /* execute pin sense measurement */ |
| 22 | static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid) |
| 23 | { |
| 24 | u32 pincap; |
| 25 | |
| 26 | if (!codec->no_trigger_sense) { |
| 27 | pincap = snd_hda_query_pin_caps(codec, nid); |
| 28 | if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */ |
| 29 | snd_hda_codec_read(codec, nid, 0, |
| 30 | AC_VERB_SET_PIN_SENSE, 0); |
| 31 | } |
| 32 | return snd_hda_codec_read(codec, nid, 0, |
| 33 | AC_VERB_GET_PIN_SENSE, 0); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * snd_hda_jack_tbl_get - query the jack-table entry for the given NID |
| 38 | */ |
| 39 | struct hda_jack_tbl * |
| 40 | snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid) |
| 41 | { |
| 42 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 43 | int i; |
| 44 | |
| 45 | if (!nid || !jack) |
| 46 | return NULL; |
| 47 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 48 | if (jack->nid == nid) |
| 49 | return jack; |
| 50 | return NULL; |
| 51 | } |
| 52 | EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get); |
| 53 | |
| 54 | /** |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 55 | * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag |
| 56 | */ |
| 57 | struct hda_jack_tbl * |
| 58 | snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag) |
| 59 | { |
| 60 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 61 | int i; |
| 62 | |
| 63 | if (!tag || !jack) |
| 64 | return NULL; |
| 65 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 66 | if (jack->tag == tag) |
| 67 | return jack; |
| 68 | return NULL; |
| 69 | } |
| 70 | EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag); |
| 71 | |
| 72 | /** |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 73 | * snd_hda_jack_tbl_new - create a jack-table entry for the given NID |
| 74 | */ |
| 75 | struct hda_jack_tbl * |
| 76 | snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid) |
| 77 | { |
| 78 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
| 79 | if (jack) |
| 80 | return jack; |
| 81 | snd_array_init(&codec->jacktbl, sizeof(*jack), 16); |
| 82 | jack = snd_array_new(&codec->jacktbl); |
| 83 | if (!jack) |
| 84 | return NULL; |
| 85 | jack->nid = nid; |
| 86 | jack->jack_dirty = 1; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 87 | jack->tag = codec->jacktbl.used; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 88 | return jack; |
| 89 | } |
David Henningsson | d1cb620 | 2011-11-11 17:13:15 +0100 | [diff] [blame] | 90 | EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 91 | |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 92 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 93 | static void snd_hda_input_jack_free(struct hda_codec *codec); |
| 94 | #else |
| 95 | #define snd_hda_input_jack_free(codec) |
| 96 | #endif |
| 97 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 98 | void snd_hda_jack_tbl_clear(struct hda_codec *codec) |
| 99 | { |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 100 | snd_hda_input_jack_free(codec); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 101 | snd_array_free(&codec->jacktbl); |
| 102 | } |
| 103 | |
| 104 | /* update the cached value and notification flag if needed */ |
| 105 | static void jack_detect_update(struct hda_codec *codec, |
| 106 | struct hda_jack_tbl *jack) |
| 107 | { |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 108 | if (jack->jack_dirty || !jack->jack_detect) { |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 109 | jack->pin_sense = read_pin_sense(codec, jack->nid); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 110 | jack->jack_dirty = 0; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * snd_hda_set_dirty_all - Mark all the cached as dirty |
| 116 | * |
| 117 | * This function sets the dirty flag to all entries of jack table. |
| 118 | * It's called from the resume path in hda_codec.c. |
| 119 | */ |
| 120 | void snd_hda_jack_set_dirty_all(struct hda_codec *codec) |
| 121 | { |
| 122 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 123 | int i; |
| 124 | |
| 125 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 126 | if (jack->nid) |
| 127 | jack->jack_dirty = 1; |
| 128 | } |
| 129 | EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all); |
| 130 | |
| 131 | /** |
| 132 | * snd_hda_pin_sense - execute pin sense measurement |
| 133 | * @codec: the CODEC to sense |
| 134 | * @nid: the pin NID to sense |
| 135 | * |
| 136 | * Execute necessary pin sense measurement and return its Presence Detect, |
| 137 | * Impedance, ELD Valid etc. status bits. |
| 138 | */ |
| 139 | u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid) |
| 140 | { |
| 141 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
| 142 | if (jack) { |
| 143 | jack_detect_update(codec, jack); |
| 144 | return jack->pin_sense; |
| 145 | } |
| 146 | return read_pin_sense(codec, nid); |
| 147 | } |
| 148 | EXPORT_SYMBOL_HDA(snd_hda_pin_sense); |
| 149 | |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 150 | #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE) |
| 151 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 152 | /** |
| 153 | * snd_hda_jack_detect - query pin Presence Detect status |
| 154 | * @codec: the CODEC to sense |
| 155 | * @nid: the pin NID to sense |
| 156 | * |
| 157 | * Query and return the pin's Presence Detect status. |
| 158 | */ |
| 159 | int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid) |
| 160 | { |
| 161 | u32 sense = snd_hda_pin_sense(codec, nid); |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 162 | return get_jack_plug_state(sense); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 163 | } |
| 164 | EXPORT_SYMBOL_HDA(snd_hda_jack_detect); |
| 165 | |
| 166 | /** |
| 167 | * snd_hda_jack_detect_enable - enable the jack-detection |
| 168 | */ |
| 169 | int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 170 | unsigned char action) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 171 | { |
| 172 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid); |
| 173 | if (!jack) |
| 174 | return -ENOMEM; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 175 | if (jack->jack_detect) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 176 | return 0; /* already registered */ |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 177 | jack->jack_detect = 1; |
| 178 | if (action) |
| 179 | jack->action = action; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 180 | return snd_hda_codec_write_cache(codec, nid, 0, |
| 181 | AC_VERB_SET_UNSOLICITED_ENABLE, |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 182 | AC_USRSP_EN | jack->tag); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 183 | } |
| 184 | EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 185 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 186 | /** |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 187 | * snd_hda_jack_report_sync - sync the states of all jacks and report if changed |
| 188 | */ |
| 189 | void snd_hda_jack_report_sync(struct hda_codec *codec) |
| 190 | { |
| 191 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 192 | int i, state; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 193 | |
| 194 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 195 | if (jack->nid) { |
| 196 | jack_detect_update(codec, jack); |
Takashi Iwai | cfc7c9d | 2011-11-11 17:53:03 +0100 | [diff] [blame^] | 197 | if (!jack->kctl) |
| 198 | continue; |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 199 | state = get_jack_plug_state(jack->pin_sense); |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 200 | snd_kctl_jack_report(codec->bus->card, jack->kctl, state); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync); |
| 204 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 205 | /** |
| 206 | * snd_hda_jack_add_kctl - Add a kctl for the given pin |
| 207 | * |
| 208 | * This assigns a jack-detection kctl to the given pin. The kcontrol |
| 209 | * will have the given name and index. |
| 210 | */ |
| 211 | int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
| 212 | const char *name, int idx) |
| 213 | { |
| 214 | struct hda_jack_tbl *jack; |
| 215 | struct snd_kcontrol *kctl; |
| 216 | |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 217 | jack = snd_hda_jack_tbl_new(codec, nid); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 218 | if (!jack) |
| 219 | return 0; |
| 220 | if (jack->kctl) |
| 221 | return 0; /* already created */ |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 222 | kctl = snd_kctl_jack_new(name, idx, codec); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 223 | if (!kctl) |
| 224 | return -ENOMEM; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 225 | if (snd_hda_ctl_add(codec, nid, kctl) < 0) |
| 226 | return -ENOMEM; |
| 227 | jack->kctl = kctl; |
| 228 | return 0; |
| 229 | } |
David Henningsson | d1cb620 | 2011-11-11 17:13:15 +0100 | [diff] [blame] | 230 | EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 231 | |
| 232 | static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, int idx, |
| 233 | const struct auto_pin_cfg *cfg) |
| 234 | { |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 235 | unsigned int def_conf, conn; |
| 236 | int err; |
| 237 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 238 | if (!nid) |
| 239 | return 0; |
| 240 | if (!is_jack_detectable(codec, nid)) |
| 241 | return 0; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 242 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 243 | conn = get_defcfg_connect(def_conf); |
| 244 | if (conn != AC_JACK_PORT_COMPLEX) |
| 245 | return 0; |
| 246 | |
| 247 | err = snd_hda_jack_add_kctl(codec, nid, |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 248 | snd_hda_get_pin_label(codec, nid, cfg), |
| 249 | idx); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 250 | if (err < 0) |
| 251 | return err; |
| 252 | return snd_hda_jack_detect_enable(codec, nid, 0); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * 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] | 257 | */ |
| 258 | int snd_hda_jack_add_kctls(struct hda_codec *codec, |
| 259 | const struct auto_pin_cfg *cfg) |
| 260 | { |
| 261 | const hda_nid_t *p; |
| 262 | int i, err; |
| 263 | |
| 264 | for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) { |
| 265 | err = add_jack_kctl(codec, *p, i, cfg); |
| 266 | if (err < 0) |
| 267 | return err; |
| 268 | } |
| 269 | for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { |
| 270 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 271 | break; |
| 272 | err = add_jack_kctl(codec, *p, i, cfg); |
| 273 | if (err < 0) |
| 274 | return err; |
| 275 | } |
| 276 | for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { |
| 277 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 278 | break; |
| 279 | err = add_jack_kctl(codec, *p, i, cfg); |
| 280 | if (err < 0) |
| 281 | return err; |
| 282 | } |
| 283 | for (i = 0; i < cfg->num_inputs; i++) { |
| 284 | err = add_jack_kctl(codec, cfg->inputs[i].pin, 0, cfg); |
| 285 | if (err < 0) |
| 286 | return err; |
| 287 | } |
| 288 | for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) { |
| 289 | err = add_jack_kctl(codec, *p, i, cfg); |
| 290 | if (err < 0) |
| 291 | return err; |
| 292 | } |
| 293 | err = add_jack_kctl(codec, cfg->dig_in_pin, 0, cfg); |
| 294 | if (err < 0) |
| 295 | return err; |
| 296 | err = add_jack_kctl(codec, cfg->mono_out_pin, 0, cfg); |
| 297 | if (err < 0) |
| 298 | return err; |
| 299 | return 0; |
| 300 | } |
| 301 | EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls); |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 302 | |
| 303 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 304 | /* |
| 305 | * Input-jack notification support |
| 306 | */ |
| 307 | static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid, |
| 308 | int type) |
| 309 | { |
| 310 | switch (type) { |
| 311 | case SND_JACK_HEADPHONE: |
| 312 | return "Headphone"; |
| 313 | case SND_JACK_MICROPHONE: |
| 314 | return "Mic"; |
| 315 | case SND_JACK_LINEOUT: |
| 316 | return "Line-out"; |
| 317 | case SND_JACK_LINEIN: |
| 318 | return "Line-in"; |
| 319 | case SND_JACK_HEADSET: |
| 320 | return "Headset"; |
| 321 | case SND_JACK_VIDEOOUT: |
| 322 | return "HDMI/DP"; |
| 323 | default: |
| 324 | return "Misc"; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static void hda_free_jack_priv(struct snd_jack *jack) |
| 329 | { |
| 330 | struct hda_jack_tbl *jacks = jack->private_data; |
| 331 | jacks->nid = 0; |
| 332 | jacks->jack = NULL; |
| 333 | } |
| 334 | |
| 335 | int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type, |
| 336 | const char *name) |
| 337 | { |
| 338 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid); |
| 339 | int err; |
| 340 | |
| 341 | if (!jack) |
| 342 | return -ENOMEM; |
| 343 | if (!name) |
| 344 | name = get_jack_default_name(codec, nid, type); |
| 345 | err = snd_jack_new(codec->bus->card, name, type, &jack->jack); |
| 346 | if (err < 0) |
| 347 | return err; |
| 348 | jack->jack->private_data = jack; |
| 349 | jack->jack->private_free = hda_free_jack_priv; |
| 350 | return 0; |
| 351 | } |
| 352 | EXPORT_SYMBOL_HDA(snd_hda_input_jack_add); |
| 353 | |
| 354 | void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid) |
| 355 | { |
| 356 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
| 357 | unsigned int pin_ctl; |
| 358 | unsigned int present; |
| 359 | int type; |
| 360 | |
Takashi Iwai | cfc7c9d | 2011-11-11 17:53:03 +0100 | [diff] [blame^] | 361 | if (!jack || !jack->jack) |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 362 | return; |
| 363 | |
| 364 | present = snd_hda_jack_detect(codec, nid); |
| 365 | type = jack->type; |
| 366 | if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) { |
| 367 | pin_ctl = snd_hda_codec_read(codec, nid, 0, |
| 368 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 369 | type = (pin_ctl & AC_PINCTL_HP_EN) ? |
| 370 | SND_JACK_HEADPHONE : SND_JACK_LINEOUT; |
| 371 | } |
| 372 | snd_jack_report(jack->jack, present ? type : 0); |
| 373 | } |
| 374 | EXPORT_SYMBOL_HDA(snd_hda_input_jack_report); |
| 375 | |
| 376 | /* free jack instances manually when clearing/reconfiguring */ |
| 377 | static void snd_hda_input_jack_free(struct hda_codec *codec) |
| 378 | { |
| 379 | if (!codec->bus->shutdown && codec->jacktbl.list) { |
| 380 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 381 | int i; |
| 382 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 383 | if (jack->jack) |
| 384 | snd_device_free(codec->bus->card, jack->jack); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | #endif /* CONFIG_SND_HDA_INPUT_JACK */ |