soc-jack.c 7.8 KB

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