soc-jack.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 <linux/gpio.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/delay.h>
  19. #include <linux/export.h>
  20. #include <trace/events/asoc.h>
  21. /**
  22. * snd_soc_jack_new - Create a new jack
  23. * @card: ASoC card
  24. * @id: an identifying string for this jack
  25. * @type: a bitmask of enum snd_jack_type values that can be detected by
  26. * this jack
  27. * @jack: structure to use for the jack
  28. *
  29. * Creates a new jack object.
  30. *
  31. * Returns zero if successful, or a negative error code on failure.
  32. * On success jack will be initialised.
  33. */
  34. int snd_soc_jack_new(struct snd_soc_codec *codec, const char *id, int type,
  35. struct snd_soc_jack *jack)
  36. {
  37. mutex_init(&jack->mutex);
  38. jack->codec = codec;
  39. INIT_LIST_HEAD(&jack->pins);
  40. INIT_LIST_HEAD(&jack->jack_zones);
  41. BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
  42. return snd_jack_new(codec->card->snd_card, id, type, &jack->jack);
  43. }
  44. EXPORT_SYMBOL_GPL(snd_soc_jack_new);
  45. /**
  46. * snd_soc_jack_report - Report the current status for a jack
  47. *
  48. * @jack: the jack
  49. * @status: a bitmask of enum snd_jack_type values that are currently detected.
  50. * @mask: a bitmask of enum snd_jack_type values that being reported.
  51. *
  52. * If configured using snd_soc_jack_add_pins() then the associated
  53. * DAPM pins will be enabled or disabled as appropriate and DAPM
  54. * synchronised.
  55. *
  56. * Note: This function uses mutexes and should be called from a
  57. * context which can sleep (such as a workqueue).
  58. */
  59. void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
  60. {
  61. struct snd_soc_codec *codec;
  62. struct snd_soc_dapm_context *dapm;
  63. struct snd_soc_jack_pin *pin;
  64. int enable;
  65. int oldstatus;
  66. trace_snd_soc_jack_report(jack, mask, status);
  67. if (!jack)
  68. return;
  69. codec = jack->codec;
  70. dapm = &codec->dapm;
  71. mutex_lock(&jack->mutex);
  72. oldstatus = jack->status;
  73. jack->status &= ~mask;
  74. jack->status |= status & mask;
  75. /* The DAPM sync is expensive enough to be worth skipping.
  76. * However, empty mask means pin synchronization is desired. */
  77. if (mask && (jack->status == oldstatus))
  78. goto out;
  79. trace_snd_soc_jack_notify(jack, status);
  80. list_for_each_entry(pin, &jack->pins, list) {
  81. enable = pin->mask & jack->status;
  82. if (pin->invert)
  83. enable = !enable;
  84. if (enable)
  85. snd_soc_dapm_enable_pin(dapm, pin->pin);
  86. else
  87. snd_soc_dapm_disable_pin(dapm, pin->pin);
  88. }
  89. /* Report before the DAPM sync to help users updating micbias status */
  90. blocking_notifier_call_chain(&jack->notifier, status, jack);
  91. snd_soc_dapm_sync(dapm);
  92. snd_jack_report(jack->jack, jack->status);
  93. out:
  94. mutex_unlock(&jack->mutex);
  95. }
  96. EXPORT_SYMBOL_GPL(snd_soc_jack_report);
  97. /**
  98. * snd_soc_jack_add_zones - Associate voltage zones with jack
  99. *
  100. * @jack: ASoC jack
  101. * @count: Number of zones
  102. * @zone: Array of zones
  103. *
  104. * After this function has been called the zones specified in the
  105. * array will be associated with the jack.
  106. */
  107. int snd_soc_jack_add_zones(struct snd_soc_jack *jack, int count,
  108. struct snd_soc_jack_zone *zones)
  109. {
  110. int i;
  111. for (i = 0; i < count; i++) {
  112. INIT_LIST_HEAD(&zones[i].list);
  113. list_add(&(zones[i].list), &jack->jack_zones);
  114. }
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones);
  118. /**
  119. * snd_soc_jack_get_type - Based on the mic bias value, this function returns
  120. * the type of jack from the zones delcared in the jack type
  121. *
  122. * @micbias_voltage: mic bias voltage at adc channel when jack is plugged in
  123. *
  124. * Based on the mic bias value passed, this function helps identify
  125. * the type of jack from the already delcared jack zones
  126. */
  127. int snd_soc_jack_get_type(struct snd_soc_jack *jack, int micbias_voltage)
  128. {
  129. struct snd_soc_jack_zone *zone;
  130. list_for_each_entry(zone, &jack->jack_zones, list) {
  131. if (micbias_voltage >= zone->min_mv &&
  132. micbias_voltage < zone->max_mv)
  133. return zone->jack_type;
  134. }
  135. return 0;
  136. }
  137. EXPORT_SYMBOL_GPL(snd_soc_jack_get_type);
  138. /**
  139. * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
  140. *
  141. * @jack: ASoC jack
  142. * @count: Number of pins
  143. * @pins: Array of pins
  144. *
  145. * After this function has been called the DAPM pins specified in the
  146. * pins array will have their status updated to reflect the current
  147. * state of the jack whenever the jack status is updated.
  148. */
  149. int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
  150. struct snd_soc_jack_pin *pins)
  151. {
  152. int i;
  153. for (i = 0; i < count; i++) {
  154. if (!pins[i].pin) {
  155. printk(KERN_ERR "No name for pin %d\n", i);
  156. return -EINVAL;
  157. }
  158. if (!pins[i].mask) {
  159. printk(KERN_ERR "No mask for pin %d (%s)\n", i,
  160. pins[i].pin);
  161. return -EINVAL;
  162. }
  163. INIT_LIST_HEAD(&pins[i].list);
  164. list_add(&(pins[i].list), &jack->pins);
  165. }
  166. snd_soc_dapm_new_widgets(&jack->codec->card->dapm);
  167. /* Update to reflect the last reported status; canned jack
  168. * implementations are likely to set their state before the
  169. * card has an opportunity to associate pins.
  170. */
  171. snd_soc_jack_report(jack, 0, 0);
  172. return 0;
  173. }
  174. EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
  175. /**
  176. * snd_soc_jack_notifier_register - Register a notifier for jack status
  177. *
  178. * @jack: ASoC jack
  179. * @nb: Notifier block to register
  180. *
  181. * Register for notification of the current status of the jack. Note
  182. * that it is not possible to report additional jack events in the
  183. * callback from the notifier, this is intended to support
  184. * applications such as enabling electrical detection only when a
  185. * mechanical detection event has occurred.
  186. */
  187. void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
  188. struct notifier_block *nb)
  189. {
  190. blocking_notifier_chain_register(&jack->notifier, nb);
  191. }
  192. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
  193. /**
  194. * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
  195. *
  196. * @jack: ASoC jack
  197. * @nb: Notifier block to unregister
  198. *
  199. * Stop notifying for status changes.
  200. */
  201. void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
  202. struct notifier_block *nb)
  203. {
  204. blocking_notifier_chain_unregister(&jack->notifier, nb);
  205. }
  206. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
  207. #ifdef CONFIG_GPIOLIB
  208. /* gpio detect */
  209. static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
  210. {
  211. struct snd_soc_jack *jack = gpio->jack;
  212. int enable;
  213. int report;
  214. enable = gpio_get_value_cansleep(gpio->gpio);
  215. if (gpio->invert)
  216. enable = !enable;
  217. if (enable)
  218. report = gpio->report;
  219. else
  220. report = 0;
  221. if (gpio->jack_status_check)
  222. report = gpio->jack_status_check();
  223. snd_soc_jack_report(jack, report, gpio->report);
  224. }
  225. /* irq handler for gpio pin */
  226. static irqreturn_t gpio_handler(int irq, void *data)
  227. {
  228. struct snd_soc_jack_gpio *gpio = data;
  229. struct device *dev = gpio->jack->codec->card->dev;
  230. trace_snd_soc_jack_irq(gpio->name);
  231. if (device_may_wakeup(dev))
  232. pm_wakeup_event(dev, gpio->debounce_time + 50);
  233. schedule_delayed_work(&gpio->work,
  234. msecs_to_jiffies(gpio->debounce_time));
  235. return IRQ_HANDLED;
  236. }
  237. /* gpio work */
  238. static void gpio_work(struct work_struct *work)
  239. {
  240. struct snd_soc_jack_gpio *gpio;
  241. gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
  242. snd_soc_jack_gpio_detect(gpio);
  243. }
  244. /**
  245. * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
  246. *
  247. * @jack: ASoC jack
  248. * @count: number of pins
  249. * @gpios: array of gpio pins
  250. *
  251. * This function will request gpio, set data direction and request irq
  252. * for each gpio in the array.
  253. */
  254. int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
  255. struct snd_soc_jack_gpio *gpios)
  256. {
  257. int i, ret;
  258. for (i = 0; i < count; i++) {
  259. if (!gpio_is_valid(gpios[i].gpio)) {
  260. printk(KERN_ERR "Invalid gpio %d\n",
  261. gpios[i].gpio);
  262. ret = -EINVAL;
  263. goto undo;
  264. }
  265. if (!gpios[i].name) {
  266. printk(KERN_ERR "No name for gpio %d\n",
  267. gpios[i].gpio);
  268. ret = -EINVAL;
  269. goto undo;
  270. }
  271. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  272. if (ret)
  273. goto undo;
  274. ret = gpio_direction_input(gpios[i].gpio);
  275. if (ret)
  276. goto err;
  277. INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
  278. gpios[i].jack = jack;
  279. ret = request_any_context_irq(gpio_to_irq(gpios[i].gpio),
  280. gpio_handler,
  281. IRQF_TRIGGER_RISING |
  282. IRQF_TRIGGER_FALLING,
  283. gpios[i].name,
  284. &gpios[i]);
  285. if (ret < 0)
  286. goto err;
  287. if (gpios[i].wake) {
  288. ret = irq_set_irq_wake(gpio_to_irq(gpios[i].gpio), 1);
  289. if (ret != 0)
  290. printk(KERN_ERR
  291. "Failed to mark GPIO %d as wake source: %d\n",
  292. gpios[i].gpio, ret);
  293. }
  294. /* Expose GPIO value over sysfs for diagnostic purposes */
  295. gpio_export(gpios[i].gpio, false);
  296. /* Update initial jack status */
  297. snd_soc_jack_gpio_detect(&gpios[i]);
  298. }
  299. return 0;
  300. err:
  301. gpio_free(gpios[i].gpio);
  302. undo:
  303. snd_soc_jack_free_gpios(jack, i, gpios);
  304. return ret;
  305. }
  306. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
  307. /**
  308. * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
  309. *
  310. * @jack: ASoC jack
  311. * @count: number of pins
  312. * @gpios: array of gpio pins
  313. *
  314. * Release gpio and irq resources for gpio pins associated with an ASoC jack.
  315. */
  316. void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
  317. struct snd_soc_jack_gpio *gpios)
  318. {
  319. int i;
  320. for (i = 0; i < count; i++) {
  321. gpio_unexport(gpios[i].gpio);
  322. free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]);
  323. cancel_delayed_work_sync(&gpios[i].work);
  324. gpio_free(gpios[i].gpio);
  325. gpios[i].jack = NULL;
  326. }
  327. }
  328. EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
  329. #endif /* CONFIG_GPIOLIB */