soc-jack.c 9.5 KB

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