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 <linux/export.h> 15 #include <sound/core.h> 16 #include <sound/control.h> 17 #include <sound/jack.h> 18 #include <sound/hda_codec.h> 19 #include "hda_local.h" 20 #include "hda_auto_parser.h" 21 #include "hda_jack.h" 22 23 /** 24 * is_jack_detectable - Check whether the given pin is jack-detectable 25 * @codec: the HDA codec 26 * @nid: pin NID 27 * 28 * Check whether the given pin is capable to report the jack detection. 29 * The jack detection might not work by various reasons, e.g. the jack 30 * detection is prohibited in the codec level, the pin config has 31 * AC_DEFCFG_MISC_NO_PRESENCE bit, no unsol support, etc. 32 */ 33 bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid) 34 { 35 if (codec->no_jack_detect) 36 return false; 37 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT)) 38 return false; 39 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) & 40 AC_DEFCFG_MISC_NO_PRESENCE) 41 return false; 42 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) && 43 !codec->jackpoll_interval) 44 return false; 45 return true; 46 } 47 EXPORT_SYMBOL_GPL(is_jack_detectable); 48 49 /* execute pin sense measurement */ 50 static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid) 51 { 52 u32 pincap; 53 u32 val; 54 55 if (!codec->no_trigger_sense) { 56 pincap = snd_hda_query_pin_caps(codec, nid); 57 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */ 58 snd_hda_codec_read(codec, nid, 0, 59 AC_VERB_SET_PIN_SENSE, 0); 60 } 61 val = snd_hda_codec_read(codec, nid, 0, 62 AC_VERB_GET_PIN_SENSE, 0); 63 if (codec->inv_jack_detect) 64 val ^= AC_PINSENSE_PRESENCE; 65 return val; 66 } 67 68 /** 69 * snd_hda_jack_tbl_get - query the jack-table entry for the given NID 70 * @codec: the HDA codec 71 * @nid: pin NID to refer to 72 */ 73 struct hda_jack_tbl * 74 snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid) 75 { 76 struct hda_jack_tbl *jack = codec->jacktbl.list; 77 int i; 78 79 if (!nid || !jack) 80 return NULL; 81 for (i = 0; i < codec->jacktbl.used; i++, jack++) 82 if (jack->nid == nid) 83 return jack; 84 return NULL; 85 } 86 EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get); 87 88 /** 89 * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag 90 * @codec: the HDA codec 91 * @tag: tag value to refer to 92 */ 93 struct hda_jack_tbl * 94 snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag) 95 { 96 struct hda_jack_tbl *jack = codec->jacktbl.list; 97 int i; 98 99 if (!tag || !jack) 100 return NULL; 101 for (i = 0; i < codec->jacktbl.used; i++, jack++) 102 if (jack->tag == tag) 103 return jack; 104 return NULL; 105 } 106 EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag); 107 108 /** 109 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID 110 * @codec: the HDA codec 111 * @nid: pin NID to assign 112 */ 113 static struct hda_jack_tbl * 114 snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid) 115 { 116 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); 117 if (jack) 118 return jack; 119 jack = snd_array_new(&codec->jacktbl); 120 if (!jack) 121 return NULL; 122 jack->nid = nid; 123 jack->jack_dirty = 1; 124 jack->tag = codec->jacktbl.used; 125 return jack; 126 } 127 128 void snd_hda_jack_tbl_clear(struct hda_codec *codec) 129 { 130 struct hda_jack_tbl *jack = codec->jacktbl.list; 131 int i; 132 133 for (i = 0; i < codec->jacktbl.used; i++, jack++) { 134 struct hda_jack_callback *cb, *next; 135 136 /* free jack instances manually when clearing/reconfiguring */ 137 if (!codec->bus->shutdown && jack->jack) 138 snd_device_free(codec->card, jack->jack); 139 140 for (cb = jack->callback; cb; cb = next) { 141 next = cb->next; 142 kfree(cb); 143 } 144 } 145 snd_array_free(&codec->jacktbl); 146 } 147 148 #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE) 149 150 /* update the cached value and notification flag if needed */ 151 static void jack_detect_update(struct hda_codec *codec, 152 struct hda_jack_tbl *jack) 153 { 154 if (!jack->jack_dirty) 155 return; 156 157 if (jack->phantom_jack) 158 jack->pin_sense = AC_PINSENSE_PRESENCE; 159 else 160 jack->pin_sense = read_pin_sense(codec, jack->nid); 161 162 /* A gating jack indicates the jack is invalid if gating is unplugged */ 163 if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack)) 164 jack->pin_sense &= ~AC_PINSENSE_PRESENCE; 165 166 jack->jack_dirty = 0; 167 168 /* If a jack is gated by this one update it. */ 169 if (jack->gated_jack) { 170 struct hda_jack_tbl *gated = 171 snd_hda_jack_tbl_get(codec, jack->gated_jack); 172 if (gated) { 173 gated->jack_dirty = 1; 174 jack_detect_update(codec, gated); 175 } 176 } 177 } 178 179 /** 180 * snd_hda_set_dirty_all - Mark all the cached as dirty 181 * @codec: the HDA codec 182 * 183 * This function sets the dirty flag to all entries of jack table. 184 * It's called from the resume path in hda_codec.c. 185 */ 186 void snd_hda_jack_set_dirty_all(struct hda_codec *codec) 187 { 188 struct hda_jack_tbl *jack = codec->jacktbl.list; 189 int i; 190 191 for (i = 0; i < codec->jacktbl.used; i++, jack++) 192 if (jack->nid) 193 jack->jack_dirty = 1; 194 } 195 EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all); 196 197 /** 198 * snd_hda_pin_sense - execute pin sense measurement 199 * @codec: the CODEC to sense 200 * @nid: the pin NID to sense 201 * 202 * Execute necessary pin sense measurement and return its Presence Detect, 203 * Impedance, ELD Valid etc. status bits. 204 */ 205 u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid) 206 { 207 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); 208 if (jack) { 209 jack_detect_update(codec, jack); 210 return jack->pin_sense; 211 } 212 return read_pin_sense(codec, nid); 213 } 214 EXPORT_SYMBOL_GPL(snd_hda_pin_sense); 215 216 /** 217 * snd_hda_jack_detect_state - query pin Presence Detect status 218 * @codec: the CODEC to sense 219 * @nid: the pin NID to sense 220 * 221 * Query and return the pin's Presence Detect status, as either 222 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM. 223 */ 224 int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid) 225 { 226 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); 227 if (jack && jack->phantom_jack) 228 return HDA_JACK_PHANTOM; 229 else if (snd_hda_pin_sense(codec, nid) & AC_PINSENSE_PRESENCE) 230 return HDA_JACK_PRESENT; 231 else 232 return HDA_JACK_NOT_PRESENT; 233 } 234 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state); 235 236 /** 237 * snd_hda_jack_detect_enable - enable the jack-detection 238 * @codec: the HDA codec 239 * @nid: pin NID to enable 240 * @func: callback function to register 241 * 242 * In the case of error, the return value will be a pointer embedded with 243 * errno. Check and handle the return value appropriately with standard 244 * macros such as @IS_ERR() and @PTR_ERR(). 245 */ 246 struct hda_jack_callback * 247 snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid, 248 hda_jack_callback_fn func) 249 { 250 struct hda_jack_tbl *jack; 251 struct hda_jack_callback *callback = NULL; 252 int err; 253 254 jack = snd_hda_jack_tbl_new(codec, nid); 255 if (!jack) 256 return ERR_PTR(-ENOMEM); 257 if (func) { 258 callback = kzalloc(sizeof(*callback), GFP_KERNEL); 259 if (!callback) 260 return ERR_PTR(-ENOMEM); 261 callback->func = func; 262 callback->nid = jack->nid; 263 callback->next = jack->callback; 264 jack->callback = callback; 265 } 266 267 if (jack->jack_detect) 268 return callback; /* already registered */ 269 jack->jack_detect = 1; 270 if (codec->jackpoll_interval > 0) 271 return callback; /* No unsol if we're polling instead */ 272 err = snd_hda_codec_write_cache(codec, nid, 0, 273 AC_VERB_SET_UNSOLICITED_ENABLE, 274 AC_USRSP_EN | jack->tag); 275 if (err < 0) 276 return ERR_PTR(err); 277 return callback; 278 } 279 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback); 280 281 /** 282 * snd_hda_jack_detect_enable - Enable the jack detection on the given pin 283 * @codec: the HDA codec 284 * @nid: pin NID to enable jack detection 285 * 286 * Enable the jack detection with the default callback. Returns zero if 287 * successful or a negative error code. 288 */ 289 int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid) 290 { 291 return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback(codec, nid, NULL)); 292 } 293 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable); 294 295 /** 296 * snd_hda_jack_set_gating_jack - Set gating jack. 297 * @codec: the HDA codec 298 * @gated_nid: gated pin NID 299 * @gating_nid: gating pin NID 300 * 301 * Indicates the gated jack is only valid when the gating jack is plugged. 302 */ 303 int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid, 304 hda_nid_t gating_nid) 305 { 306 struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid); 307 struct hda_jack_tbl *gating = snd_hda_jack_tbl_new(codec, gating_nid); 308 309 if (!gated || !gating) 310 return -EINVAL; 311 312 gated->gating_jack = gating_nid; 313 gating->gated_jack = gated_nid; 314 315 return 0; 316 } 317 EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack); 318 319 /** 320 * snd_hda_jack_report_sync - sync the states of all jacks and report if changed 321 * @codec: the HDA codec 322 */ 323 void snd_hda_jack_report_sync(struct hda_codec *codec) 324 { 325 struct hda_jack_tbl *jack; 326 int i, state; 327 328 /* update all jacks at first */ 329 jack = codec->jacktbl.list; 330 for (i = 0; i < codec->jacktbl.used; i++, jack++) 331 if (jack->nid) 332 jack_detect_update(codec, jack); 333 334 /* report the updated jacks; it's done after updating all jacks 335 * to make sure that all gating jacks properly have been set 336 */ 337 jack = codec->jacktbl.list; 338 for (i = 0; i < codec->jacktbl.used; i++, jack++) 339 if (jack->nid) { 340 if (!jack->jack || jack->block_report) 341 continue; 342 state = jack->button_state; 343 if (get_jack_plug_state(jack->pin_sense)) 344 state |= jack->type; 345 snd_jack_report(jack->jack, state); 346 if (jack->button_state) { 347 snd_jack_report(jack->jack, 348 state & ~jack->button_state); 349 jack->button_state = 0; /* button released */ 350 } 351 } 352 } 353 EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync); 354 355 /* guess the jack type from the pin-config */ 356 static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid) 357 { 358 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); 359 switch (get_defcfg_device(def_conf)) { 360 case AC_JACK_LINE_OUT: 361 case AC_JACK_SPEAKER: 362 return SND_JACK_LINEOUT; 363 case AC_JACK_HP_OUT: 364 return SND_JACK_HEADPHONE; 365 case AC_JACK_SPDIF_OUT: 366 case AC_JACK_DIG_OTHER_OUT: 367 return SND_JACK_AVOUT; 368 case AC_JACK_MIC_IN: 369 return SND_JACK_MICROPHONE; 370 default: 371 return SND_JACK_LINEIN; 372 } 373 } 374 375 static void hda_free_jack_priv(struct snd_jack *jack) 376 { 377 struct hda_jack_tbl *jacks = jack->private_data; 378 jacks->nid = 0; 379 jacks->jack = NULL; 380 } 381 382 /** 383 * snd_hda_jack_add_kctl - Add a kctl for the given pin 384 * @codec: the HDA codec 385 * @nid: pin NID to assign 386 * @name: string name for the jack 387 * @phantom_jack: flag to deal as a phantom jack 388 * @type: jack type bits to be reported, 0 for guessing from pincfg 389 * @keymap: optional jack / key mapping 390 * 391 * This assigns a jack-detection kctl to the given pin. The kcontrol 392 * will have the given name and index. 393 */ 394 int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, 395 const char *name, bool phantom_jack, 396 int type, const struct hda_jack_keymap *keymap) 397 { 398 struct hda_jack_tbl *jack; 399 const struct hda_jack_keymap *map; 400 int err, state, buttons; 401 402 jack = snd_hda_jack_tbl_new(codec, nid); 403 if (!jack) 404 return 0; 405 if (jack->jack) 406 return 0; /* already created */ 407 408 if (!type) 409 type = get_input_jack_type(codec, nid); 410 411 buttons = 0; 412 if (keymap) { 413 for (map = keymap; map->type; map++) 414 buttons |= map->type; 415 } 416 417 err = snd_jack_new(codec->card, name, type | buttons, 418 &jack->jack, true, phantom_jack); 419 if (err < 0) 420 return err; 421 422 jack->phantom_jack = !!phantom_jack; 423 jack->type = type; 424 jack->button_state = 0; 425 jack->jack->private_data = jack; 426 jack->jack->private_free = hda_free_jack_priv; 427 if (keymap) { 428 for (map = keymap; map->type; map++) 429 snd_jack_set_key(jack->jack, map->type, map->key); 430 } 431 432 state = snd_hda_jack_detect(codec, nid); 433 snd_jack_report(jack->jack, state ? jack->type : 0); 434 435 return 0; 436 } 437 EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl); 438 439 static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, 440 const struct auto_pin_cfg *cfg, 441 const char *base_name) 442 { 443 unsigned int def_conf, conn; 444 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 445 int err; 446 bool phantom_jack; 447 448 if (!nid) 449 return 0; 450 def_conf = snd_hda_codec_get_pincfg(codec, nid); 451 conn = get_defcfg_connect(def_conf); 452 if (conn == AC_JACK_PORT_NONE) 453 return 0; 454 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) || 455 !is_jack_detectable(codec, nid); 456 457 if (base_name) 458 strlcpy(name, base_name, sizeof(name)); 459 else 460 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL); 461 if (phantom_jack) 462 /* Example final name: "Internal Mic Phantom Jack" */ 463 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1); 464 err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack, 0, NULL); 465 if (err < 0) 466 return err; 467 468 if (!phantom_jack) 469 return snd_hda_jack_detect_enable(codec, nid); 470 return 0; 471 } 472 473 /** 474 * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg 475 * @codec: the HDA codec 476 * @cfg: pin config table to parse 477 */ 478 int snd_hda_jack_add_kctls(struct hda_codec *codec, 479 const struct auto_pin_cfg *cfg) 480 { 481 const hda_nid_t *p; 482 int i, err; 483 484 for (i = 0; i < cfg->num_inputs; i++) { 485 /* If we have headphone mics; make sure they get the right name 486 before grabbed by output pins */ 487 if (cfg->inputs[i].is_headphone_mic) { 488 if (auto_cfg_hp_outs(cfg) == 1) 489 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0], 490 cfg, "Headphone Mic"); 491 else 492 err = add_jack_kctl(codec, cfg->inputs[i].pin, 493 cfg, "Headphone Mic"); 494 } else 495 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, 496 NULL); 497 if (err < 0) 498 return err; 499 } 500 501 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) { 502 err = add_jack_kctl(codec, *p, cfg, NULL); 503 if (err < 0) 504 return err; 505 } 506 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { 507 if (*p == *cfg->line_out_pins) /* might be duplicated */ 508 break; 509 err = add_jack_kctl(codec, *p, cfg, NULL); 510 if (err < 0) 511 return err; 512 } 513 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { 514 if (*p == *cfg->line_out_pins) /* might be duplicated */ 515 break; 516 err = add_jack_kctl(codec, *p, cfg, NULL); 517 if (err < 0) 518 return err; 519 } 520 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) { 521 err = add_jack_kctl(codec, *p, cfg, NULL); 522 if (err < 0) 523 return err; 524 } 525 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL); 526 if (err < 0) 527 return err; 528 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL); 529 if (err < 0) 530 return err; 531 return 0; 532 } 533 EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls); 534 535 static void call_jack_callback(struct hda_codec *codec, unsigned int res, 536 struct hda_jack_tbl *jack) 537 { 538 struct hda_jack_callback *cb; 539 540 for (cb = jack->callback; cb; cb = cb->next) { 541 cb->jack = jack; 542 cb->unsol_res = res; 543 cb->func(codec, cb); 544 } 545 if (jack->gated_jack) { 546 struct hda_jack_tbl *gated = 547 snd_hda_jack_tbl_get(codec, jack->gated_jack); 548 if (gated) { 549 for (cb = gated->callback; cb; cb = cb->next) { 550 cb->jack = gated; 551 cb->unsol_res = res; 552 cb->func(codec, cb); 553 } 554 } 555 } 556 } 557 558 /** 559 * snd_hda_jack_unsol_event - Handle an unsolicited event 560 * @codec: the HDA codec 561 * @res: the unsolicited event data 562 */ 563 void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res) 564 { 565 struct hda_jack_tbl *event; 566 int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f; 567 568 event = snd_hda_jack_tbl_get_from_tag(codec, tag); 569 if (!event) 570 return; 571 event->jack_dirty = 1; 572 573 call_jack_callback(codec, res, event); 574 snd_hda_jack_report_sync(codec); 575 } 576 EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event); 577 578 /** 579 * snd_hda_jack_poll_all - Poll all jacks 580 * @codec: the HDA codec 581 * 582 * Poll all detectable jacks with dirty flag, update the status, call 583 * callbacks and call snd_hda_jack_report_sync() if any changes are found. 584 */ 585 void snd_hda_jack_poll_all(struct hda_codec *codec) 586 { 587 struct hda_jack_tbl *jack = codec->jacktbl.list; 588 int i, changes = 0; 589 590 for (i = 0; i < codec->jacktbl.used; i++, jack++) { 591 unsigned int old_sense; 592 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack) 593 continue; 594 old_sense = get_jack_plug_state(jack->pin_sense); 595 jack_detect_update(codec, jack); 596 if (old_sense == get_jack_plug_state(jack->pin_sense)) 597 continue; 598 changes = 1; 599 call_jack_callback(codec, 0, jack); 600 } 601 if (changes) 602 snd_hda_jack_report_sync(codec); 603 } 604 EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all); 605 606