soc-jack.c 9.6 KB

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