soc-jack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // soc-jack.c -- ALSA SoC jack handling
  4. //
  5. // Copyright 2008 Wolfson Microelectronics PLC.
  6. //
  7. // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. #include <sound/jack.h>
  9. #include <sound/soc.h>
  10. #include <linux/gpio.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/delay.h>
  15. #include <linux/export.h>
  16. #include <linux/suspend.h>
  17. #include <trace/events/asoc.h>
  18. struct jack_gpio_tbl {
  19. int count;
  20. struct snd_soc_jack *jack;
  21. struct snd_soc_jack_gpio *gpios;
  22. };
  23. /**
  24. * snd_soc_component_set_jack - configure component jack.
  25. * @component: COMPONENTs
  26. * @jack: structure to use for the jack
  27. * @data: can be used if codec driver need extra data for configuring jack
  28. *
  29. * Configures and enables jack detection function.
  30. */
  31. int snd_soc_component_set_jack(struct snd_soc_component *component,
  32. struct snd_soc_jack *jack, void *data)
  33. {
  34. if (component->driver->set_jack)
  35. return component->driver->set_jack(component, jack, data);
  36. return -ENOTSUPP;
  37. }
  38. EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
  39. /**
  40. * snd_soc_card_jack_new - Create a new jack
  41. * @card: ASoC card
  42. * @id: an identifying string for this jack
  43. * @type: a bitmask of enum snd_jack_type values that can be detected by
  44. * this jack
  45. * @jack: structure to use for the jack
  46. * @pins: Array of jack pins to be added to the jack or NULL
  47. * @num_pins: Number of elements in the @pins array
  48. *
  49. * Creates a new jack object.
  50. *
  51. * Returns zero if successful, or a negative error code on failure.
  52. * On success jack will be initialised.
  53. */
  54. int snd_soc_card_jack_new(struct snd_soc_card *card, const char *id, int type,
  55. struct snd_soc_jack *jack, struct snd_soc_jack_pin *pins,
  56. unsigned int num_pins)
  57. {
  58. int ret;
  59. mutex_init(&jack->mutex);
  60. jack->card = card;
  61. INIT_LIST_HEAD(&jack->pins);
  62. INIT_LIST_HEAD(&jack->jack_zones);
  63. BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
  64. ret = snd_jack_new(card->snd_card, id, type, &jack->jack, false, false);
  65. if (ret)
  66. return ret;
  67. if (num_pins)
  68. return snd_soc_jack_add_pins(jack, num_pins, pins);
  69. return 0;
  70. }
  71. EXPORT_SYMBOL_GPL(snd_soc_card_jack_new);
  72. /**
  73. * snd_soc_jack_report - Report the current status for a jack
  74. *
  75. * @jack: the jack
  76. * @status: a bitmask of enum snd_jack_type values that are currently detected.
  77. * @mask: a bitmask of enum snd_jack_type values that being reported.
  78. *
  79. * If configured using snd_soc_jack_add_pins() then the associated
  80. * DAPM pins will be enabled or disabled as appropriate and DAPM
  81. * synchronised.
  82. *
  83. * Note: This function uses mutexes and should be called from a
  84. * context which can sleep (such as a workqueue).
  85. */
  86. void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
  87. {
  88. struct snd_soc_dapm_context *dapm;
  89. struct snd_soc_jack_pin *pin;
  90. unsigned int sync = 0;
  91. int enable;
  92. if (!jack)
  93. return;
  94. trace_snd_soc_jack_report(jack, mask, status);
  95. dapm = &jack->card->dapm;
  96. mutex_lock(&jack->mutex);
  97. jack->status &= ~mask;
  98. jack->status |= status & mask;
  99. trace_snd_soc_jack_notify(jack, status);
  100. list_for_each_entry(pin, &jack->pins, list) {
  101. enable = pin->mask & jack->status;
  102. if (pin->invert)
  103. enable = !enable;
  104. if (enable)
  105. snd_soc_dapm_enable_pin(dapm, pin->pin);
  106. else
  107. snd_soc_dapm_disable_pin(dapm, pin->pin);
  108. /* we need to sync for this case only */
  109. sync = 1;
  110. }
  111. /* Report before the DAPM sync to help users updating micbias status */
  112. blocking_notifier_call_chain(&jack->notifier, jack->status, jack);
  113. if (sync)
  114. snd_soc_dapm_sync(dapm);
  115. snd_jack_report(jack->jack, jack->status);
  116. mutex_unlock(&jack->mutex);
  117. }
  118. EXPORT_SYMBOL_GPL(snd_soc_jack_report);
  119. /**
  120. * snd_soc_jack_add_zones - Associate voltage zones with jack
  121. *
  122. * @jack: ASoC jack
  123. * @count: Number of zones
  124. * @zones: Array of zones
  125. *
  126. * After this function has been called the zones specified in the
  127. * array will be associated with the jack.
  128. */
  129. int snd_soc_jack_add_zones(struct snd_soc_jack *jack, int count,
  130. struct snd_soc_jack_zone *zones)
  131. {
  132. int i;
  133. for (i = 0; i < count; i++) {
  134. INIT_LIST_HEAD(&zones[i].list);
  135. list_add(&(zones[i].list), &jack->jack_zones);
  136. }
  137. return 0;
  138. }
  139. EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones);
  140. /**
  141. * snd_soc_jack_get_type - Based on the mic bias value, this function returns
  142. * the type of jack from the zones declared in the jack type
  143. *
  144. * @jack: ASoC jack
  145. * @micbias_voltage: mic bias voltage at adc channel when jack is plugged in
  146. *
  147. * Based on the mic bias value passed, this function helps identify
  148. * the type of jack from the already declared jack zones
  149. */
  150. int snd_soc_jack_get_type(struct snd_soc_jack *jack, int micbias_voltage)
  151. {
  152. struct snd_soc_jack_zone *zone;
  153. list_for_each_entry(zone, &jack->jack_zones, list) {
  154. if (micbias_voltage >= zone->min_mv &&
  155. micbias_voltage < zone->max_mv)
  156. return zone->jack_type;
  157. }
  158. return 0;
  159. }
  160. EXPORT_SYMBOL_GPL(snd_soc_jack_get_type);
  161. /**
  162. * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
  163. *
  164. * @jack: ASoC jack
  165. * @count: Number of pins
  166. * @pins: Array of pins
  167. *
  168. * After this function has been called the DAPM pins specified in the
  169. * pins array will have their status updated to reflect the current
  170. * state of the jack whenever the jack status is updated.
  171. */
  172. int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
  173. struct snd_soc_jack_pin *pins)
  174. {
  175. int i;
  176. for (i = 0; i < count; i++) {
  177. if (!pins[i].pin) {
  178. dev_err(jack->card->dev, "ASoC: No name for pin %d\n",
  179. i);
  180. return -EINVAL;
  181. }
  182. if (!pins[i].mask) {
  183. dev_err(jack->card->dev, "ASoC: No mask for pin %d"
  184. " (%s)\n", i, pins[i].pin);
  185. return -EINVAL;
  186. }
  187. INIT_LIST_HEAD(&pins[i].list);
  188. list_add(&(pins[i].list), &jack->pins);
  189. snd_jack_add_new_kctl(jack->jack, pins[i].pin, pins[i].mask);
  190. }
  191. /* Update to reflect the last reported status; canned jack
  192. * implementations are likely to set their state before the
  193. * card has an opportunity to associate pins.
  194. */
  195. snd_soc_jack_report(jack, 0, 0);
  196. return 0;
  197. }
  198. EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
  199. /**
  200. * snd_soc_jack_notifier_register - Register a notifier for jack status
  201. *
  202. * @jack: ASoC jack
  203. * @nb: Notifier block to register
  204. *
  205. * Register for notification of the current status of the jack. Note
  206. * that it is not possible to report additional jack events in the
  207. * callback from the notifier, this is intended to support
  208. * applications such as enabling electrical detection only when a
  209. * mechanical detection event has occurred.
  210. */
  211. void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
  212. struct notifier_block *nb)
  213. {
  214. blocking_notifier_chain_register(&jack->notifier, nb);
  215. }
  216. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
  217. /**
  218. * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
  219. *
  220. * @jack: ASoC jack
  221. * @nb: Notifier block to unregister
  222. *
  223. * Stop notifying for status changes.
  224. */
  225. void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
  226. struct notifier_block *nb)
  227. {
  228. blocking_notifier_chain_unregister(&jack->notifier, nb);
  229. }
  230. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
  231. #ifdef CONFIG_GPIOLIB
  232. /* gpio detect */
  233. static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
  234. {
  235. struct snd_soc_jack *jack = gpio->jack;
  236. int enable;
  237. int report;
  238. enable = gpiod_get_value_cansleep(gpio->desc);
  239. if (gpio->invert)
  240. enable = !enable;
  241. if (enable)
  242. report = gpio->report;
  243. else
  244. report = 0;
  245. if (gpio->jack_status_check)
  246. report = gpio->jack_status_check(gpio->data);
  247. snd_soc_jack_report(jack, report, gpio->report);
  248. }
  249. /* irq handler for gpio pin */
  250. static irqreturn_t gpio_handler(int irq, void *data)
  251. {
  252. struct snd_soc_jack_gpio *gpio = data;
  253. struct device *dev = gpio->jack->card->dev;
  254. trace_snd_soc_jack_irq(gpio->name);
  255. if (device_may_wakeup(dev))
  256. pm_wakeup_event(dev, gpio->debounce_time + 50);
  257. queue_delayed_work(system_power_efficient_wq, &gpio->work,
  258. msecs_to_jiffies(gpio->debounce_time));
  259. return IRQ_HANDLED;
  260. }
  261. /* gpio work */
  262. static void gpio_work(struct work_struct *work)
  263. {
  264. struct snd_soc_jack_gpio *gpio;
  265. gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
  266. snd_soc_jack_gpio_detect(gpio);
  267. }
  268. static int snd_soc_jack_pm_notifier(struct notifier_block *nb,
  269. unsigned long action, void *data)
  270. {
  271. struct snd_soc_jack_gpio *gpio =
  272. container_of(nb, struct snd_soc_jack_gpio, pm_notifier);
  273. switch (action) {
  274. case PM_POST_SUSPEND:
  275. case PM_POST_HIBERNATION:
  276. case PM_POST_RESTORE:
  277. /*
  278. * Use workqueue so we do not have to care about running
  279. * concurrently with work triggered by the interrupt handler.
  280. */
  281. queue_delayed_work(system_power_efficient_wq, &gpio->work, 0);
  282. break;
  283. }
  284. return NOTIFY_DONE;
  285. }
  286. static void jack_free_gpios(struct snd_soc_jack *jack, int count,
  287. struct snd_soc_jack_gpio *gpios)
  288. {
  289. int i;
  290. for (i = 0; i < count; i++) {
  291. gpiod_unexport(gpios[i].desc);
  292. unregister_pm_notifier(&gpios[i].pm_notifier);
  293. free_irq(gpiod_to_irq(gpios[i].desc), &gpios[i]);
  294. cancel_delayed_work_sync(&gpios[i].work);
  295. gpiod_put(gpios[i].desc);
  296. gpios[i].jack = NULL;
  297. }
  298. }
  299. static void jack_devres_free_gpios(struct device *dev, void *res)
  300. {
  301. struct jack_gpio_tbl *tbl = res;
  302. jack_free_gpios(tbl->jack, tbl->count, tbl->gpios);
  303. }
  304. /**
  305. * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
  306. *
  307. * @jack: ASoC jack
  308. * @count: number of pins
  309. * @gpios: array of gpio pins
  310. *
  311. * This function will request gpio, set data direction and request irq
  312. * for each gpio in the array.
  313. */
  314. int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
  315. struct snd_soc_jack_gpio *gpios)
  316. {
  317. int i, ret;
  318. struct jack_gpio_tbl *tbl;
  319. tbl = devres_alloc(jack_devres_free_gpios, sizeof(*tbl), GFP_KERNEL);
  320. if (!tbl)
  321. return -ENOMEM;
  322. tbl->jack = jack;
  323. tbl->count = count;
  324. tbl->gpios = gpios;
  325. for (i = 0; i < count; i++) {
  326. if (!gpios[i].name) {
  327. dev_err(jack->card->dev,
  328. "ASoC: No name for gpio at index %d\n", i);
  329. ret = -EINVAL;
  330. goto undo;
  331. }
  332. if (gpios[i].desc) {
  333. /* Already have a GPIO descriptor. */
  334. goto got_gpio;
  335. } else if (gpios[i].gpiod_dev) {
  336. /* Get a GPIO descriptor */
  337. gpios[i].desc = gpiod_get_index(gpios[i].gpiod_dev,
  338. gpios[i].name,
  339. gpios[i].idx, GPIOD_IN);
  340. if (IS_ERR(gpios[i].desc)) {
  341. ret = PTR_ERR(gpios[i].desc);
  342. dev_err(gpios[i].gpiod_dev,
  343. "ASoC: Cannot get gpio at index %d: %d",
  344. i, ret);
  345. goto undo;
  346. }
  347. } else {
  348. /* legacy GPIO number */
  349. if (!gpio_is_valid(gpios[i].gpio)) {
  350. dev_err(jack->card->dev,
  351. "ASoC: Invalid gpio %d\n",
  352. gpios[i].gpio);
  353. ret = -EINVAL;
  354. goto undo;
  355. }
  356. ret = gpio_request_one(gpios[i].gpio, GPIOF_IN,
  357. gpios[i].name);
  358. if (ret)
  359. goto undo;
  360. gpios[i].desc = gpio_to_desc(gpios[i].gpio);
  361. }
  362. got_gpio:
  363. INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
  364. gpios[i].jack = jack;
  365. ret = request_any_context_irq(gpiod_to_irq(gpios[i].desc),
  366. gpio_handler,
  367. IRQF_TRIGGER_RISING |
  368. IRQF_TRIGGER_FALLING,
  369. gpios[i].name,
  370. &gpios[i]);
  371. if (ret < 0)
  372. goto err;
  373. if (gpios[i].wake) {
  374. ret = irq_set_irq_wake(gpiod_to_irq(gpios[i].desc), 1);
  375. if (ret != 0)
  376. dev_err(jack->card->dev,
  377. "ASoC: Failed to mark GPIO at index %d as wake source: %d\n",
  378. i, ret);
  379. }
  380. /*
  381. * Register PM notifier so we do not miss state transitions
  382. * happening while system is asleep.
  383. */
  384. gpios[i].pm_notifier.notifier_call = snd_soc_jack_pm_notifier;
  385. register_pm_notifier(&gpios[i].pm_notifier);
  386. /* Expose GPIO value over sysfs for diagnostic purposes */
  387. gpiod_export(gpios[i].desc, false);
  388. /* Update initial jack status */
  389. schedule_delayed_work(&gpios[i].work,
  390. msecs_to_jiffies(gpios[i].debounce_time));
  391. }
  392. devres_add(jack->card->dev, tbl);
  393. return 0;
  394. err:
  395. gpio_free(gpios[i].gpio);
  396. undo:
  397. jack_free_gpios(jack, i, gpios);
  398. devres_free(tbl);
  399. return ret;
  400. }
  401. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
  402. /**
  403. * snd_soc_jack_add_gpiods - Associate GPIO descriptor pins with an ASoC jack
  404. *
  405. * @gpiod_dev: GPIO consumer device
  406. * @jack: ASoC jack
  407. * @count: number of pins
  408. * @gpios: array of gpio pins
  409. *
  410. * This function will request gpio, set data direction and request irq
  411. * for each gpio in the array.
  412. */
  413. int snd_soc_jack_add_gpiods(struct device *gpiod_dev,
  414. struct snd_soc_jack *jack,
  415. int count, struct snd_soc_jack_gpio *gpios)
  416. {
  417. int i;
  418. for (i = 0; i < count; i++)
  419. gpios[i].gpiod_dev = gpiod_dev;
  420. return snd_soc_jack_add_gpios(jack, count, gpios);
  421. }
  422. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods);
  423. /**
  424. * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
  425. *
  426. * @jack: ASoC jack
  427. * @count: number of pins
  428. * @gpios: array of gpio pins
  429. *
  430. * Release gpio and irq resources for gpio pins associated with an ASoC jack.
  431. */
  432. void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
  433. struct snd_soc_jack_gpio *gpios)
  434. {
  435. jack_free_gpios(jack, count, gpios);
  436. devres_destroy(jack->card->dev, jack_devres_free_gpios, NULL, NULL);
  437. }
  438. EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
  439. #endif /* CONFIG_GPIOLIB */