soc-jack.c 11 KB

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