soc-jack.c 9.5 KB

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