soc-jack.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. return snd_jack_new(card->codec->card, id, type, &jack->jack);
  39. }
  40. EXPORT_SYMBOL_GPL(snd_soc_jack_new);
  41. /**
  42. * snd_soc_jack_report - Report the current status for a jack
  43. *
  44. * @jack: the jack
  45. * @status: a bitmask of enum snd_jack_type values that are currently detected.
  46. * @mask: a bitmask of enum snd_jack_type values that being reported.
  47. *
  48. * If configured using snd_soc_jack_add_pins() then the associated
  49. * DAPM pins will be enabled or disabled as appropriate and DAPM
  50. * synchronised.
  51. *
  52. * Note: This function uses mutexes and should be called from a
  53. * context which can sleep (such as a workqueue).
  54. */
  55. void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
  56. {
  57. struct snd_soc_codec *codec = jack->card->codec;
  58. struct snd_soc_jack_pin *pin;
  59. int enable;
  60. int oldstatus;
  61. if (!jack) {
  62. WARN_ON_ONCE(!jack);
  63. return;
  64. }
  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. snd_soc_dapm_sync(codec);
  83. snd_jack_report(jack->jack, status);
  84. out:
  85. mutex_unlock(&codec->mutex);
  86. }
  87. EXPORT_SYMBOL_GPL(snd_soc_jack_report);
  88. /**
  89. * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
  90. *
  91. * @jack: ASoC jack
  92. * @count: Number of pins
  93. * @pins: Array of pins
  94. *
  95. * After this function has been called the DAPM pins specified in the
  96. * pins array will have their status updated to reflect the current
  97. * state of the jack whenever the jack status is updated.
  98. */
  99. int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
  100. struct snd_soc_jack_pin *pins)
  101. {
  102. int i;
  103. for (i = 0; i < count; i++) {
  104. if (!pins[i].pin) {
  105. printk(KERN_ERR "No name for pin %d\n", i);
  106. return -EINVAL;
  107. }
  108. if (!pins[i].mask) {
  109. printk(KERN_ERR "No mask for pin %d (%s)\n", i,
  110. pins[i].pin);
  111. return -EINVAL;
  112. }
  113. INIT_LIST_HEAD(&pins[i].list);
  114. list_add(&(pins[i].list), &jack->pins);
  115. }
  116. /* Update to reflect the last reported status; canned jack
  117. * implementations are likely to set their state before the
  118. * card has an opportunity to associate pins.
  119. */
  120. snd_soc_jack_report(jack, 0, 0);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
  124. #ifdef CONFIG_GPIOLIB
  125. /* gpio detect */
  126. static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
  127. {
  128. struct snd_soc_jack *jack = gpio->jack;
  129. int enable;
  130. int report;
  131. if (gpio->debounce_time > 0)
  132. mdelay(gpio->debounce_time);
  133. enable = gpio_get_value(gpio->gpio);
  134. if (gpio->invert)
  135. enable = !enable;
  136. if (enable)
  137. report = gpio->report;
  138. else
  139. report = 0;
  140. snd_soc_jack_report(jack, report, gpio->report);
  141. }
  142. /* irq handler for gpio pin */
  143. static irqreturn_t gpio_handler(int irq, void *data)
  144. {
  145. struct snd_soc_jack_gpio *gpio = data;
  146. schedule_work(&gpio->work);
  147. return IRQ_HANDLED;
  148. }
  149. /* gpio work */
  150. static void gpio_work(struct work_struct *work)
  151. {
  152. struct snd_soc_jack_gpio *gpio;
  153. gpio = container_of(work, struct snd_soc_jack_gpio, work);
  154. snd_soc_jack_gpio_detect(gpio);
  155. }
  156. /**
  157. * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
  158. *
  159. * @jack: ASoC jack
  160. * @count: number of pins
  161. * @gpios: array of gpio pins
  162. *
  163. * This function will request gpio, set data direction and request irq
  164. * for each gpio in the array.
  165. */
  166. int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
  167. struct snd_soc_jack_gpio *gpios)
  168. {
  169. int i, ret;
  170. for (i = 0; i < count; i++) {
  171. if (!gpio_is_valid(gpios[i].gpio)) {
  172. printk(KERN_ERR "Invalid gpio %d\n",
  173. gpios[i].gpio);
  174. ret = -EINVAL;
  175. goto undo;
  176. }
  177. if (!gpios[i].name) {
  178. printk(KERN_ERR "No name for gpio %d\n",
  179. gpios[i].gpio);
  180. ret = -EINVAL;
  181. goto undo;
  182. }
  183. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  184. if (ret)
  185. goto undo;
  186. ret = gpio_direction_input(gpios[i].gpio);
  187. if (ret)
  188. goto err;
  189. INIT_WORK(&gpios[i].work, gpio_work);
  190. gpios[i].jack = jack;
  191. ret = request_irq(gpio_to_irq(gpios[i].gpio),
  192. gpio_handler,
  193. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  194. jack->card->dev->driver->name,
  195. &gpios[i]);
  196. if (ret)
  197. goto err;
  198. #ifdef CONFIG_GPIO_SYSFS
  199. /* Expose GPIO value over sysfs for diagnostic purposes */
  200. gpio_export(gpios[i].gpio, false);
  201. #endif
  202. /* Update initial jack status */
  203. snd_soc_jack_gpio_detect(&gpios[i]);
  204. }
  205. return 0;
  206. err:
  207. gpio_free(gpios[i].gpio);
  208. undo:
  209. snd_soc_jack_free_gpios(jack, i, gpios);
  210. return ret;
  211. }
  212. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
  213. /**
  214. * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
  215. *
  216. * @jack: ASoC jack
  217. * @count: number of pins
  218. * @gpios: array of gpio pins
  219. *
  220. * Release gpio and irq resources for gpio pins associated with an ASoC jack.
  221. */
  222. void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
  223. struct snd_soc_jack_gpio *gpios)
  224. {
  225. int i;
  226. for (i = 0; i < count; i++) {
  227. #ifdef CONFIG_GPIO_SYSFS
  228. gpio_unexport(gpios[i].gpio);
  229. #endif
  230. free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]);
  231. gpio_free(gpios[i].gpio);
  232. gpios[i].jack = NULL;
  233. }
  234. }
  235. EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
  236. #endif /* CONFIG_GPIOLIB */