soc-jack.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <sound/soc-dapm.h>
  16. #include <linux/gpio.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/delay.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_jack_pin *pin;
  60. int enable;
  61. int oldstatus;
  62. if (!jack)
  63. return;
  64. codec = jack->codec;
  65. mutex_lock(&codec->mutex);
  66. oldstatus = jack->status;
  67. jack->status &= ~mask;
  68. jack->status |= status & mask;
  69. /* The DAPM sync is expensive enough to be worth skipping.
  70. * However, empty mask means pin synchronization is desired. */
  71. if (mask && (jack->status == oldstatus))
  72. goto out;
  73. list_for_each_entry(pin, &jack->pins, list) {
  74. enable = pin->mask & jack->status;
  75. if (pin->invert)
  76. enable = !enable;
  77. if (enable)
  78. snd_soc_dapm_enable_pin(codec, pin->pin);
  79. else
  80. snd_soc_dapm_disable_pin(codec, pin->pin);
  81. }
  82. /* Report before the DAPM sync to help users updating micbias status */
  83. blocking_notifier_call_chain(&jack->notifier, status, NULL);
  84. snd_soc_dapm_sync(codec);
  85. snd_jack_report(jack->jack, status);
  86. out:
  87. mutex_unlock(&codec->mutex);
  88. }
  89. EXPORT_SYMBOL_GPL(snd_soc_jack_report);
  90. /**
  91. * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
  92. *
  93. * @jack: ASoC jack
  94. * @count: Number of pins
  95. * @pins: Array of pins
  96. *
  97. * After this function has been called the DAPM pins specified in the
  98. * pins array will have their status updated to reflect the current
  99. * state of the jack whenever the jack status is updated.
  100. */
  101. int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
  102. struct snd_soc_jack_pin *pins)
  103. {
  104. int i;
  105. for (i = 0; i < count; i++) {
  106. if (!pins[i].pin) {
  107. printk(KERN_ERR "No name for pin %d\n", i);
  108. return -EINVAL;
  109. }
  110. if (!pins[i].mask) {
  111. printk(KERN_ERR "No mask for pin %d (%s)\n", i,
  112. pins[i].pin);
  113. return -EINVAL;
  114. }
  115. INIT_LIST_HEAD(&pins[i].list);
  116. list_add(&(pins[i].list), &jack->pins);
  117. }
  118. /* Update to reflect the last reported status; canned jack
  119. * implementations are likely to set their state before the
  120. * card has an opportunity to associate pins.
  121. */
  122. snd_soc_jack_report(jack, 0, 0);
  123. return 0;
  124. }
  125. EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
  126. /**
  127. * snd_soc_jack_notifier_register - Register a notifier for jack status
  128. *
  129. * @jack: ASoC jack
  130. * @nb: Notifier block to register
  131. *
  132. * Register for notification of the current status of the jack. Note
  133. * that it is not possible to report additional jack events in the
  134. * callback from the notifier, this is intended to support
  135. * applications such as enabling electrical detection only when a
  136. * mechanical detection event has occurred.
  137. */
  138. void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
  139. struct notifier_block *nb)
  140. {
  141. blocking_notifier_chain_register(&jack->notifier, nb);
  142. }
  143. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
  144. /**
  145. * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
  146. *
  147. * @jack: ASoC jack
  148. * @nb: Notifier block to unregister
  149. *
  150. * Stop notifying for status changes.
  151. */
  152. void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
  153. struct notifier_block *nb)
  154. {
  155. blocking_notifier_chain_unregister(&jack->notifier, nb);
  156. }
  157. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
  158. #ifdef CONFIG_GPIOLIB
  159. /* gpio detect */
  160. static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
  161. {
  162. struct snd_soc_jack *jack = gpio->jack;
  163. int enable;
  164. int report;
  165. enable = gpio_get_value(gpio->gpio);
  166. if (gpio->invert)
  167. enable = !enable;
  168. if (enable)
  169. report = gpio->report;
  170. else
  171. report = 0;
  172. if (gpio->jack_status_check)
  173. report = gpio->jack_status_check();
  174. snd_soc_jack_report(jack, report, gpio->report);
  175. }
  176. /* irq handler for gpio pin */
  177. static irqreturn_t gpio_handler(int irq, void *data)
  178. {
  179. struct snd_soc_jack_gpio *gpio = data;
  180. schedule_delayed_work(&gpio->work,
  181. msecs_to_jiffies(gpio->debounce_time));
  182. return IRQ_HANDLED;
  183. }
  184. /* gpio work */
  185. static void gpio_work(struct work_struct *work)
  186. {
  187. struct snd_soc_jack_gpio *gpio;
  188. gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
  189. snd_soc_jack_gpio_detect(gpio);
  190. }
  191. /**
  192. * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
  193. *
  194. * @jack: ASoC jack
  195. * @count: number of pins
  196. * @gpios: array of gpio pins
  197. *
  198. * This function will request gpio, set data direction and request irq
  199. * for each gpio in the array.
  200. */
  201. int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
  202. struct snd_soc_jack_gpio *gpios)
  203. {
  204. int i, ret;
  205. for (i = 0; i < count; i++) {
  206. if (!gpio_is_valid(gpios[i].gpio)) {
  207. printk(KERN_ERR "Invalid gpio %d\n",
  208. gpios[i].gpio);
  209. ret = -EINVAL;
  210. goto undo;
  211. }
  212. if (!gpios[i].name) {
  213. printk(KERN_ERR "No name for gpio %d\n",
  214. gpios[i].gpio);
  215. ret = -EINVAL;
  216. goto undo;
  217. }
  218. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  219. if (ret)
  220. goto undo;
  221. ret = gpio_direction_input(gpios[i].gpio);
  222. if (ret)
  223. goto err;
  224. INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
  225. gpios[i].jack = jack;
  226. ret = request_irq(gpio_to_irq(gpios[i].gpio),
  227. gpio_handler,
  228. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  229. jack->codec->dev->driver->name,
  230. &gpios[i]);
  231. if (ret)
  232. goto err;
  233. #ifdef CONFIG_GPIO_SYSFS
  234. /* Expose GPIO value over sysfs for diagnostic purposes */
  235. gpio_export(gpios[i].gpio, false);
  236. #endif
  237. /* Update initial jack status */
  238. snd_soc_jack_gpio_detect(&gpios[i]);
  239. }
  240. return 0;
  241. err:
  242. gpio_free(gpios[i].gpio);
  243. undo:
  244. snd_soc_jack_free_gpios(jack, i, gpios);
  245. return ret;
  246. }
  247. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
  248. /**
  249. * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
  250. *
  251. * @jack: ASoC jack
  252. * @count: number of pins
  253. * @gpios: array of gpio pins
  254. *
  255. * Release gpio and irq resources for gpio pins associated with an ASoC jack.
  256. */
  257. void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
  258. struct snd_soc_jack_gpio *gpios)
  259. {
  260. int i;
  261. for (i = 0; i < count; i++) {
  262. #ifdef CONFIG_GPIO_SYSFS
  263. gpio_unexport(gpios[i].gpio);
  264. #endif
  265. free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]);
  266. cancel_delayed_work_sync(&gpios[i].work);
  267. gpio_free(gpios[i].gpio);
  268. gpios[i].jack = NULL;
  269. }
  270. }
  271. EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
  272. #endif /* CONFIG_GPIOLIB */