soc-jack.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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_card *card, const char *id, int type,
  34. struct snd_soc_jack *jack)
  35. {
  36. jack->card = card;
  37. INIT_LIST_HEAD(&jack->pins);
  38. BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
  39. return snd_jack_new(card->codec->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->card->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. if (gpio->debounce_time > 0)
  166. mdelay(gpio->debounce_time);
  167. enable = gpio_get_value(gpio->gpio);
  168. if (gpio->invert)
  169. enable = !enable;
  170. if (enable)
  171. report = gpio->report;
  172. else
  173. report = 0;
  174. if (gpio->jack_status_check)
  175. report = gpio->jack_status_check();
  176. snd_soc_jack_report(jack, report, gpio->report);
  177. }
  178. /* irq handler for gpio pin */
  179. static irqreturn_t gpio_handler(int irq, void *data)
  180. {
  181. struct snd_soc_jack_gpio *gpio = data;
  182. schedule_work(&gpio->work);
  183. return IRQ_HANDLED;
  184. }
  185. /* gpio work */
  186. static void gpio_work(struct work_struct *work)
  187. {
  188. struct snd_soc_jack_gpio *gpio;
  189. gpio = container_of(work, struct snd_soc_jack_gpio, work);
  190. snd_soc_jack_gpio_detect(gpio);
  191. }
  192. /**
  193. * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
  194. *
  195. * @jack: ASoC jack
  196. * @count: number of pins
  197. * @gpios: array of gpio pins
  198. *
  199. * This function will request gpio, set data direction and request irq
  200. * for each gpio in the array.
  201. */
  202. int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
  203. struct snd_soc_jack_gpio *gpios)
  204. {
  205. int i, ret;
  206. for (i = 0; i < count; i++) {
  207. if (!gpio_is_valid(gpios[i].gpio)) {
  208. printk(KERN_ERR "Invalid gpio %d\n",
  209. gpios[i].gpio);
  210. ret = -EINVAL;
  211. goto undo;
  212. }
  213. if (!gpios[i].name) {
  214. printk(KERN_ERR "No name for gpio %d\n",
  215. gpios[i].gpio);
  216. ret = -EINVAL;
  217. goto undo;
  218. }
  219. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  220. if (ret)
  221. goto undo;
  222. ret = gpio_direction_input(gpios[i].gpio);
  223. if (ret)
  224. goto err;
  225. INIT_WORK(&gpios[i].work, gpio_work);
  226. gpios[i].jack = jack;
  227. ret = request_irq(gpio_to_irq(gpios[i].gpio),
  228. gpio_handler,
  229. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  230. jack->card->dev->driver->name,
  231. &gpios[i]);
  232. if (ret)
  233. goto err;
  234. #ifdef CONFIG_GPIO_SYSFS
  235. /* Expose GPIO value over sysfs for diagnostic purposes */
  236. gpio_export(gpios[i].gpio, false);
  237. #endif
  238. /* Update initial jack status */
  239. snd_soc_jack_gpio_detect(&gpios[i]);
  240. }
  241. return 0;
  242. err:
  243. gpio_free(gpios[i].gpio);
  244. undo:
  245. snd_soc_jack_free_gpios(jack, i, gpios);
  246. return ret;
  247. }
  248. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
  249. /**
  250. * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
  251. *
  252. * @jack: ASoC jack
  253. * @count: number of pins
  254. * @gpios: array of gpio pins
  255. *
  256. * Release gpio and irq resources for gpio pins associated with an ASoC jack.
  257. */
  258. void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
  259. struct snd_soc_jack_gpio *gpios)
  260. {
  261. int i;
  262. for (i = 0; i < count; i++) {
  263. #ifdef CONFIG_GPIO_SYSFS
  264. gpio_unexport(gpios[i].gpio);
  265. #endif
  266. free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]);
  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 */