soc-jack.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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;
  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. codec = jack->card->codec;
  66. mutex_lock(&codec->mutex);
  67. oldstatus = jack->status;
  68. jack->status &= ~mask;
  69. jack->status |= status & mask;
  70. /* The DAPM sync is expensive enough to be worth skipping.
  71. * However, empty mask means pin synchronization is desired. */
  72. if (mask && (jack->status == oldstatus))
  73. goto out;
  74. list_for_each_entry(pin, &jack->pins, list) {
  75. enable = pin->mask & jack->status;
  76. if (pin->invert)
  77. enable = !enable;
  78. if (enable)
  79. snd_soc_dapm_enable_pin(codec, pin->pin);
  80. else
  81. snd_soc_dapm_disable_pin(codec, pin->pin);
  82. }
  83. snd_soc_dapm_sync(codec);
  84. snd_jack_report(jack->jack, status);
  85. out:
  86. mutex_unlock(&codec->mutex);
  87. }
  88. EXPORT_SYMBOL_GPL(snd_soc_jack_report);
  89. /**
  90. * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
  91. *
  92. * @jack: ASoC jack
  93. * @count: Number of pins
  94. * @pins: Array of pins
  95. *
  96. * After this function has been called the DAPM pins specified in the
  97. * pins array will have their status updated to reflect the current
  98. * state of the jack whenever the jack status is updated.
  99. */
  100. int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
  101. struct snd_soc_jack_pin *pins)
  102. {
  103. int i;
  104. for (i = 0; i < count; i++) {
  105. if (!pins[i].pin) {
  106. printk(KERN_ERR "No name for pin %d\n", i);
  107. return -EINVAL;
  108. }
  109. if (!pins[i].mask) {
  110. printk(KERN_ERR "No mask for pin %d (%s)\n", i,
  111. pins[i].pin);
  112. return -EINVAL;
  113. }
  114. INIT_LIST_HEAD(&pins[i].list);
  115. list_add(&(pins[i].list), &jack->pins);
  116. }
  117. /* Update to reflect the last reported status; canned jack
  118. * implementations are likely to set their state before the
  119. * card has an opportunity to associate pins.
  120. */
  121. snd_soc_jack_report(jack, 0, 0);
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
  125. #ifdef CONFIG_GPIOLIB
  126. /* gpio detect */
  127. static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
  128. {
  129. struct snd_soc_jack *jack = gpio->jack;
  130. int enable;
  131. int report;
  132. if (gpio->debounce_time > 0)
  133. mdelay(gpio->debounce_time);
  134. enable = gpio_get_value(gpio->gpio);
  135. if (gpio->invert)
  136. enable = !enable;
  137. if (enable)
  138. report = gpio->report;
  139. else
  140. report = 0;
  141. if (gpio->jack_status_check)
  142. report = gpio->jack_status_check();
  143. snd_soc_jack_report(jack, report, gpio->report);
  144. }
  145. /* irq handler for gpio pin */
  146. static irqreturn_t gpio_handler(int irq, void *data)
  147. {
  148. struct snd_soc_jack_gpio *gpio = data;
  149. schedule_work(&gpio->work);
  150. return IRQ_HANDLED;
  151. }
  152. /* gpio work */
  153. static void gpio_work(struct work_struct *work)
  154. {
  155. struct snd_soc_jack_gpio *gpio;
  156. gpio = container_of(work, struct snd_soc_jack_gpio, work);
  157. snd_soc_jack_gpio_detect(gpio);
  158. }
  159. /**
  160. * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
  161. *
  162. * @jack: ASoC jack
  163. * @count: number of pins
  164. * @gpios: array of gpio pins
  165. *
  166. * This function will request gpio, set data direction and request irq
  167. * for each gpio in the array.
  168. */
  169. int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
  170. struct snd_soc_jack_gpio *gpios)
  171. {
  172. int i, ret;
  173. for (i = 0; i < count; i++) {
  174. if (!gpio_is_valid(gpios[i].gpio)) {
  175. printk(KERN_ERR "Invalid gpio %d\n",
  176. gpios[i].gpio);
  177. ret = -EINVAL;
  178. goto undo;
  179. }
  180. if (!gpios[i].name) {
  181. printk(KERN_ERR "No name for gpio %d\n",
  182. gpios[i].gpio);
  183. ret = -EINVAL;
  184. goto undo;
  185. }
  186. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  187. if (ret)
  188. goto undo;
  189. ret = gpio_direction_input(gpios[i].gpio);
  190. if (ret)
  191. goto err;
  192. INIT_WORK(&gpios[i].work, gpio_work);
  193. gpios[i].jack = jack;
  194. ret = request_irq(gpio_to_irq(gpios[i].gpio),
  195. gpio_handler,
  196. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  197. jack->card->dev->driver->name,
  198. &gpios[i]);
  199. if (ret)
  200. goto err;
  201. #ifdef CONFIG_GPIO_SYSFS
  202. /* Expose GPIO value over sysfs for diagnostic purposes */
  203. gpio_export(gpios[i].gpio, false);
  204. #endif
  205. /* Update initial jack status */
  206. snd_soc_jack_gpio_detect(&gpios[i]);
  207. }
  208. return 0;
  209. err:
  210. gpio_free(gpios[i].gpio);
  211. undo:
  212. snd_soc_jack_free_gpios(jack, i, gpios);
  213. return ret;
  214. }
  215. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
  216. /**
  217. * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
  218. *
  219. * @jack: ASoC jack
  220. * @count: number of pins
  221. * @gpios: array of gpio pins
  222. *
  223. * Release gpio and irq resources for gpio pins associated with an ASoC jack.
  224. */
  225. void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
  226. struct snd_soc_jack_gpio *gpios)
  227. {
  228. int i;
  229. for (i = 0; i < count; i++) {
  230. #ifdef CONFIG_GPIO_SYSFS
  231. gpio_unexport(gpios[i].gpio);
  232. #endif
  233. free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]);
  234. gpio_free(gpios[i].gpio);
  235. gpios[i].jack = NULL;
  236. }
  237. }
  238. EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
  239. #endif /* CONFIG_GPIOLIB */