soc-jack.c 9.6 KB

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