soc-jack.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. snd_soc_dapm_new_widgets(&jack->codec->card->dapm);
  165. /* Update to reflect the last reported status; canned jack
  166. * implementations are likely to set their state before the
  167. * card has an opportunity to associate pins.
  168. */
  169. snd_soc_jack_report(jack, 0, 0);
  170. return 0;
  171. }
  172. EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
  173. /**
  174. * snd_soc_jack_notifier_register - Register a notifier for jack status
  175. *
  176. * @jack: ASoC jack
  177. * @nb: Notifier block to register
  178. *
  179. * Register for notification of the current status of the jack. Note
  180. * that it is not possible to report additional jack events in the
  181. * callback from the notifier, this is intended to support
  182. * applications such as enabling electrical detection only when a
  183. * mechanical detection event has occurred.
  184. */
  185. void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
  186. struct notifier_block *nb)
  187. {
  188. blocking_notifier_chain_register(&jack->notifier, nb);
  189. }
  190. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
  191. /**
  192. * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
  193. *
  194. * @jack: ASoC jack
  195. * @nb: Notifier block to unregister
  196. *
  197. * Stop notifying for status changes.
  198. */
  199. void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
  200. struct notifier_block *nb)
  201. {
  202. blocking_notifier_chain_unregister(&jack->notifier, nb);
  203. }
  204. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
  205. #ifdef CONFIG_GPIOLIB
  206. /* gpio detect */
  207. static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
  208. {
  209. struct snd_soc_jack *jack = gpio->jack;
  210. int enable;
  211. int report;
  212. enable = gpio_get_value_cansleep(gpio->gpio);
  213. if (gpio->invert)
  214. enable = !enable;
  215. if (enable)
  216. report = gpio->report;
  217. else
  218. report = 0;
  219. if (gpio->jack_status_check)
  220. report = gpio->jack_status_check();
  221. snd_soc_jack_report(jack, report, gpio->report);
  222. }
  223. /* irq handler for gpio pin */
  224. static irqreturn_t gpio_handler(int irq, void *data)
  225. {
  226. struct snd_soc_jack_gpio *gpio = data;
  227. struct device *dev = gpio->jack->codec->card->dev;
  228. trace_snd_soc_jack_irq(gpio->name);
  229. if (device_may_wakeup(dev))
  230. pm_wakeup_event(dev, gpio->debounce_time + 50);
  231. schedule_delayed_work(&gpio->work,
  232. msecs_to_jiffies(gpio->debounce_time));
  233. return IRQ_HANDLED;
  234. }
  235. /* gpio work */
  236. static void gpio_work(struct work_struct *work)
  237. {
  238. struct snd_soc_jack_gpio *gpio;
  239. gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
  240. snd_soc_jack_gpio_detect(gpio);
  241. }
  242. /**
  243. * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
  244. *
  245. * @jack: ASoC jack
  246. * @count: number of pins
  247. * @gpios: array of gpio pins
  248. *
  249. * This function will request gpio, set data direction and request irq
  250. * for each gpio in the array.
  251. */
  252. int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
  253. struct snd_soc_jack_gpio *gpios)
  254. {
  255. int i, ret;
  256. for (i = 0; i < count; i++) {
  257. if (!gpio_is_valid(gpios[i].gpio)) {
  258. printk(KERN_ERR "Invalid gpio %d\n",
  259. gpios[i].gpio);
  260. ret = -EINVAL;
  261. goto undo;
  262. }
  263. if (!gpios[i].name) {
  264. printk(KERN_ERR "No name for gpio %d\n",
  265. gpios[i].gpio);
  266. ret = -EINVAL;
  267. goto undo;
  268. }
  269. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  270. if (ret)
  271. goto undo;
  272. ret = gpio_direction_input(gpios[i].gpio);
  273. if (ret)
  274. goto err;
  275. INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
  276. gpios[i].jack = jack;
  277. ret = request_any_context_irq(gpio_to_irq(gpios[i].gpio),
  278. gpio_handler,
  279. IRQF_TRIGGER_RISING |
  280. IRQF_TRIGGER_FALLING,
  281. gpios[i].name,
  282. &gpios[i]);
  283. if (ret < 0)
  284. goto err;
  285. if (gpios[i].wake) {
  286. ret = irq_set_irq_wake(gpio_to_irq(gpios[i].gpio), 1);
  287. if (ret != 0)
  288. printk(KERN_ERR
  289. "Failed to mark GPIO %d as wake source: %d\n",
  290. gpios[i].gpio, ret);
  291. }
  292. #ifdef CONFIG_GPIO_SYSFS
  293. /* Expose GPIO value over sysfs for diagnostic purposes */
  294. gpio_export(gpios[i].gpio, false);
  295. #endif
  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. #ifdef CONFIG_GPIO_SYSFS
  322. gpio_unexport(gpios[i].gpio);
  323. #endif
  324. free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]);
  325. cancel_delayed_work_sync(&gpios[i].work);
  326. gpio_free(gpios[i].gpio);
  327. gpios[i].jack = NULL;
  328. }
  329. }
  330. EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
  331. #endif /* CONFIG_GPIOLIB */