soc-jack.c 11 KB

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