soc-jack.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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/interrupt.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/delay.h>
  19. #include <trace/events/asoc.h>
  20. /**
  21. * snd_soc_jack_new - Create a new jack
  22. * @card: ASoC card
  23. * @id: an identifying string for this jack
  24. * @type: a bitmask of enum snd_jack_type values that can be detected by
  25. * this jack
  26. * @jack: structure to use for the jack
  27. *
  28. * Creates a new jack object.
  29. *
  30. * Returns zero if successful, or a negative error code on failure.
  31. * On success jack will be initialised.
  32. */
  33. int snd_soc_jack_new(struct snd_soc_codec *codec, const char *id, int type,
  34. struct snd_soc_jack *jack)
  35. {
  36. jack->codec = codec;
  37. INIT_LIST_HEAD(&jack->pins);
  38. BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
  39. return snd_jack_new(codec->card->snd_card, id, type, &jack->jack);
  40. }
  41. EXPORT_SYMBOL_GPL(snd_soc_jack_new);
  42. /**
  43. * snd_soc_jack_report - Report the current status for a jack
  44. *
  45. * @jack: the jack
  46. * @status: a bitmask of enum snd_jack_type values that are currently detected.
  47. * @mask: a bitmask of enum snd_jack_type values that being reported.
  48. *
  49. * If configured using snd_soc_jack_add_pins() then the associated
  50. * DAPM pins will be enabled or disabled as appropriate and DAPM
  51. * synchronised.
  52. *
  53. * Note: This function uses mutexes and should be called from a
  54. * context which can sleep (such as a workqueue).
  55. */
  56. void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
  57. {
  58. struct snd_soc_codec *codec;
  59. struct snd_soc_dapm_context *dapm;
  60. struct snd_soc_jack_pin *pin;
  61. int enable;
  62. int oldstatus;
  63. trace_snd_soc_jack_report(jack, mask, status);
  64. if (!jack)
  65. return;
  66. codec = jack->codec;
  67. dapm = &codec->dapm;
  68. mutex_lock(&codec->mutex);
  69. oldstatus = jack->status;
  70. jack->status &= ~mask;
  71. jack->status |= status & mask;
  72. /* The DAPM sync is expensive enough to be worth skipping.
  73. * However, empty mask means pin synchronization is desired. */
  74. if (mask && (jack->status == oldstatus))
  75. goto out;
  76. trace_snd_soc_jack_notify(jack, status);
  77. list_for_each_entry(pin, &jack->pins, list) {
  78. enable = pin->mask & jack->status;
  79. if (pin->invert)
  80. enable = !enable;
  81. if (enable)
  82. snd_soc_dapm_enable_pin(dapm, pin->pin);
  83. else
  84. snd_soc_dapm_disable_pin(dapm, pin->pin);
  85. }
  86. /* Report before the DAPM sync to help users updating micbias status */
  87. blocking_notifier_call_chain(&jack->notifier, status, NULL);
  88. snd_soc_dapm_sync(dapm);
  89. snd_jack_report(jack->jack, status);
  90. out:
  91. mutex_unlock(&codec->mutex);
  92. }
  93. EXPORT_SYMBOL_GPL(snd_soc_jack_report);
  94. /**
  95. * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
  96. *
  97. * @jack: ASoC jack
  98. * @count: Number of pins
  99. * @pins: Array of pins
  100. *
  101. * After this function has been called the DAPM pins specified in the
  102. * pins array will have their status updated to reflect the current
  103. * state of the jack whenever the jack status is updated.
  104. */
  105. int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
  106. struct snd_soc_jack_pin *pins)
  107. {
  108. int i;
  109. for (i = 0; i < count; i++) {
  110. if (!pins[i].pin) {
  111. printk(KERN_ERR "No name for pin %d\n", i);
  112. return -EINVAL;
  113. }
  114. if (!pins[i].mask) {
  115. printk(KERN_ERR "No mask for pin %d (%s)\n", i,
  116. pins[i].pin);
  117. return -EINVAL;
  118. }
  119. INIT_LIST_HEAD(&pins[i].list);
  120. list_add(&(pins[i].list), &jack->pins);
  121. }
  122. /* Update to reflect the last reported status; canned jack
  123. * implementations are likely to set their state before the
  124. * card has an opportunity to associate pins.
  125. */
  126. snd_soc_jack_report(jack, 0, 0);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
  130. /**
  131. * snd_soc_jack_notifier_register - Register a notifier for jack status
  132. *
  133. * @jack: ASoC jack
  134. * @nb: Notifier block to register
  135. *
  136. * Register for notification of the current status of the jack. Note
  137. * that it is not possible to report additional jack events in the
  138. * callback from the notifier, this is intended to support
  139. * applications such as enabling electrical detection only when a
  140. * mechanical detection event has occurred.
  141. */
  142. void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
  143. struct notifier_block *nb)
  144. {
  145. blocking_notifier_chain_register(&jack->notifier, nb);
  146. }
  147. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
  148. /**
  149. * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
  150. *
  151. * @jack: ASoC jack
  152. * @nb: Notifier block to unregister
  153. *
  154. * Stop notifying for status changes.
  155. */
  156. void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
  157. struct notifier_block *nb)
  158. {
  159. blocking_notifier_chain_unregister(&jack->notifier, nb);
  160. }
  161. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
  162. #ifdef CONFIG_GPIOLIB
  163. /* gpio detect */
  164. static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
  165. {
  166. struct snd_soc_jack *jack = gpio->jack;
  167. int enable;
  168. int report;
  169. enable = gpio_get_value(gpio->gpio);
  170. if (gpio->invert)
  171. enable = !enable;
  172. if (enable)
  173. report = gpio->report;
  174. else
  175. report = 0;
  176. if (gpio->jack_status_check)
  177. report = gpio->jack_status_check();
  178. snd_soc_jack_report(jack, report, gpio->report);
  179. }
  180. /* irq handler for gpio pin */
  181. static irqreturn_t gpio_handler(int irq, void *data)
  182. {
  183. struct snd_soc_jack_gpio *gpio = data;
  184. struct device *dev = gpio->jack->codec->card->dev;
  185. trace_snd_soc_jack_irq(gpio->name);
  186. if (device_may_wakeup(dev))
  187. pm_wakeup_event(dev, gpio->debounce_time + 50);
  188. schedule_delayed_work(&gpio->work,
  189. msecs_to_jiffies(gpio->debounce_time));
  190. return IRQ_HANDLED;
  191. }
  192. /* gpio work */
  193. static void gpio_work(struct work_struct *work)
  194. {
  195. struct snd_soc_jack_gpio *gpio;
  196. gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
  197. snd_soc_jack_gpio_detect(gpio);
  198. }
  199. /**
  200. * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
  201. *
  202. * @jack: ASoC jack
  203. * @count: number of pins
  204. * @gpios: array of gpio pins
  205. *
  206. * This function will request gpio, set data direction and request irq
  207. * for each gpio in the array.
  208. */
  209. int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
  210. struct snd_soc_jack_gpio *gpios)
  211. {
  212. int i, ret;
  213. for (i = 0; i < count; i++) {
  214. if (!gpio_is_valid(gpios[i].gpio)) {
  215. printk(KERN_ERR "Invalid gpio %d\n",
  216. gpios[i].gpio);
  217. ret = -EINVAL;
  218. goto undo;
  219. }
  220. if (!gpios[i].name) {
  221. printk(KERN_ERR "No name for gpio %d\n",
  222. gpios[i].gpio);
  223. ret = -EINVAL;
  224. goto undo;
  225. }
  226. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  227. if (ret)
  228. goto undo;
  229. ret = gpio_direction_input(gpios[i].gpio);
  230. if (ret)
  231. goto err;
  232. INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
  233. gpios[i].jack = jack;
  234. ret = request_any_context_irq(gpio_to_irq(gpios[i].gpio),
  235. gpio_handler,
  236. IRQF_TRIGGER_RISING |
  237. IRQF_TRIGGER_FALLING,
  238. jack->codec->dev->driver->name,
  239. &gpios[i]);
  240. if (ret)
  241. goto err;
  242. #ifdef CONFIG_GPIO_SYSFS
  243. /* Expose GPIO value over sysfs for diagnostic purposes */
  244. gpio_export(gpios[i].gpio, false);
  245. #endif
  246. /* Update initial jack status */
  247. snd_soc_jack_gpio_detect(&gpios[i]);
  248. }
  249. return 0;
  250. err:
  251. gpio_free(gpios[i].gpio);
  252. undo:
  253. snd_soc_jack_free_gpios(jack, i, gpios);
  254. return ret;
  255. }
  256. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
  257. /**
  258. * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
  259. *
  260. * @jack: ASoC jack
  261. * @count: number of pins
  262. * @gpios: array of gpio pins
  263. *
  264. * Release gpio and irq resources for gpio pins associated with an ASoC jack.
  265. */
  266. void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
  267. struct snd_soc_jack_gpio *gpios)
  268. {
  269. int i;
  270. for (i = 0; i < count; i++) {
  271. #ifdef CONFIG_GPIO_SYSFS
  272. gpio_unexport(gpios[i].gpio);
  273. #endif
  274. free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]);
  275. cancel_delayed_work_sync(&gpios[i].work);
  276. gpio_free(gpios[i].gpio);
  277. gpios[i].jack = NULL;
  278. }
  279. }
  280. EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
  281. #endif /* CONFIG_GPIOLIB */