soc-jack.c 9.5 KB

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