soc-jack.c 11 KB

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