hda_jack.c 15 KB

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