hda_jack.c 15 KB

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