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 | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 197 | state = get_jack_plug_state(jack->pin_sense); |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 198 | snd_kctl_jack_report(codec->bus->card, jack->kctl, state); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync); |
| 202 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 203 | /** |
| 204 | * snd_hda_jack_add_kctl - Add a kctl for the given pin |
| 205 | * |
| 206 | * This assigns a jack-detection kctl to the given pin. The kcontrol |
| 207 | * will have the given name and index. |
| 208 | */ |
| 209 | int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
| 210 | const char *name, int idx) |
| 211 | { |
| 212 | struct hda_jack_tbl *jack; |
| 213 | struct snd_kcontrol *kctl; |
| 214 | |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 215 | jack = snd_hda_jack_tbl_new(codec, nid); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 216 | if (!jack) |
| 217 | return 0; |
| 218 | if (jack->kctl) |
| 219 | return 0; /* already created */ |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 220 | kctl = snd_kctl_jack_new(name, idx, codec); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 221 | if (!kctl) |
| 222 | return -ENOMEM; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 223 | if (snd_hda_ctl_add(codec, nid, kctl) < 0) |
| 224 | return -ENOMEM; |
| 225 | jack->kctl = kctl; |
| 226 | return 0; |
| 227 | } |
David Henningsson | d1cb620 | 2011-11-11 17:13:15 +0100 | [diff] [blame^] | 228 | EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 229 | |
| 230 | static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, int idx, |
| 231 | const struct auto_pin_cfg *cfg) |
| 232 | { |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 233 | unsigned int def_conf, conn; |
| 234 | int err; |
| 235 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 236 | if (!nid) |
| 237 | return 0; |
| 238 | if (!is_jack_detectable(codec, nid)) |
| 239 | return 0; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 240 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 241 | conn = get_defcfg_connect(def_conf); |
| 242 | if (conn != AC_JACK_PORT_COMPLEX) |
| 243 | return 0; |
| 244 | |
| 245 | err = snd_hda_jack_add_kctl(codec, nid, |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 246 | snd_hda_get_pin_label(codec, nid, cfg), |
| 247 | idx); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 248 | if (err < 0) |
| 249 | return err; |
| 250 | return snd_hda_jack_detect_enable(codec, nid, 0); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
| 254 | * 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] | 255 | */ |
| 256 | int snd_hda_jack_add_kctls(struct hda_codec *codec, |
| 257 | const struct auto_pin_cfg *cfg) |
| 258 | { |
| 259 | const hda_nid_t *p; |
| 260 | int i, err; |
| 261 | |
| 262 | for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) { |
| 263 | err = add_jack_kctl(codec, *p, i, cfg); |
| 264 | if (err < 0) |
| 265 | return err; |
| 266 | } |
| 267 | for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { |
| 268 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 269 | break; |
| 270 | err = add_jack_kctl(codec, *p, i, cfg); |
| 271 | if (err < 0) |
| 272 | return err; |
| 273 | } |
| 274 | for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { |
| 275 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 276 | break; |
| 277 | err = add_jack_kctl(codec, *p, i, cfg); |
| 278 | if (err < 0) |
| 279 | return err; |
| 280 | } |
| 281 | for (i = 0; i < cfg->num_inputs; i++) { |
| 282 | err = add_jack_kctl(codec, cfg->inputs[i].pin, 0, cfg); |
| 283 | if (err < 0) |
| 284 | return err; |
| 285 | } |
| 286 | for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) { |
| 287 | err = add_jack_kctl(codec, *p, i, cfg); |
| 288 | if (err < 0) |
| 289 | return err; |
| 290 | } |
| 291 | err = add_jack_kctl(codec, cfg->dig_in_pin, 0, cfg); |
| 292 | if (err < 0) |
| 293 | return err; |
| 294 | err = add_jack_kctl(codec, cfg->mono_out_pin, 0, cfg); |
| 295 | if (err < 0) |
| 296 | return err; |
| 297 | return 0; |
| 298 | } |
| 299 | EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls); |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 300 | |
| 301 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 302 | /* |
| 303 | * Input-jack notification support |
| 304 | */ |
| 305 | static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid, |
| 306 | int type) |
| 307 | { |
| 308 | switch (type) { |
| 309 | case SND_JACK_HEADPHONE: |
| 310 | return "Headphone"; |
| 311 | case SND_JACK_MICROPHONE: |
| 312 | return "Mic"; |
| 313 | case SND_JACK_LINEOUT: |
| 314 | return "Line-out"; |
| 315 | case SND_JACK_LINEIN: |
| 316 | return "Line-in"; |
| 317 | case SND_JACK_HEADSET: |
| 318 | return "Headset"; |
| 319 | case SND_JACK_VIDEOOUT: |
| 320 | return "HDMI/DP"; |
| 321 | default: |
| 322 | return "Misc"; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static void hda_free_jack_priv(struct snd_jack *jack) |
| 327 | { |
| 328 | struct hda_jack_tbl *jacks = jack->private_data; |
| 329 | jacks->nid = 0; |
| 330 | jacks->jack = NULL; |
| 331 | } |
| 332 | |
| 333 | int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type, |
| 334 | const char *name) |
| 335 | { |
| 336 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid); |
| 337 | int err; |
| 338 | |
| 339 | if (!jack) |
| 340 | return -ENOMEM; |
| 341 | if (!name) |
| 342 | name = get_jack_default_name(codec, nid, type); |
| 343 | err = snd_jack_new(codec->bus->card, name, type, &jack->jack); |
| 344 | if (err < 0) |
| 345 | return err; |
| 346 | jack->jack->private_data = jack; |
| 347 | jack->jack->private_free = hda_free_jack_priv; |
| 348 | return 0; |
| 349 | } |
| 350 | EXPORT_SYMBOL_HDA(snd_hda_input_jack_add); |
| 351 | |
| 352 | void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid) |
| 353 | { |
| 354 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
| 355 | unsigned int pin_ctl; |
| 356 | unsigned int present; |
| 357 | int type; |
| 358 | |
| 359 | if (!jack) |
| 360 | return; |
| 361 | |
| 362 | present = snd_hda_jack_detect(codec, nid); |
| 363 | type = jack->type; |
| 364 | if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) { |
| 365 | pin_ctl = snd_hda_codec_read(codec, nid, 0, |
| 366 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); |
| 367 | type = (pin_ctl & AC_PINCTL_HP_EN) ? |
| 368 | SND_JACK_HEADPHONE : SND_JACK_LINEOUT; |
| 369 | } |
| 370 | snd_jack_report(jack->jack, present ? type : 0); |
| 371 | } |
| 372 | EXPORT_SYMBOL_HDA(snd_hda_input_jack_report); |
| 373 | |
| 374 | /* free jack instances manually when clearing/reconfiguring */ |
| 375 | static void snd_hda_input_jack_free(struct hda_codec *codec) |
| 376 | { |
| 377 | if (!codec->bus->shutdown && codec->jacktbl.list) { |
| 378 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 379 | int i; |
| 380 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 381 | if (jack->jack) |
| 382 | snd_device_free(codec->bus->card, jack->jack); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | #endif /* CONFIG_SND_HDA_INPUT_JACK */ |