soc-jack.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/gpio/consumer.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/delay.h>
  20. #include <linux/export.h>
  21. #include <trace/events/asoc.h>
  22. /**
  23. * snd_soc_jack_new - Create a new jack
  24. * @codec: ASoC codec
  25. * @id: an identifying string for this jack
  26. * @type: a bitmask of enum snd_jack_type values that can be detected by
  27. * this jack
  28. * @jack: structure to use for the jack
  29. *
  30. * Creates a new jack object.
  31. *
  32. * Returns zero if successful, or a negative error code on failure.
  33. * On success jack will be initialised.
  34. */
  35. int snd_soc_jack_new(struct snd_soc_codec *codec, const char *id, int type,
  36. struct snd_soc_jack *jack)
  37. {
  38. mutex_init(&jack->mutex);
  39. jack->codec = codec;
  40. INIT_LIST_HEAD(&jack->pins);
  41. INIT_LIST_HEAD(&jack->jack_zones);
  42. BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
  43. return snd_jack_new(codec->card->snd_card, id, type, &jack->jack);
  44. }
  45. EXPORT_SYMBOL_GPL(snd_soc_jack_new);
  46. /**
  47. * snd_soc_jack_report - Report the current status for a jack
  48. *
  49. * @jack: the jack
  50. * @status: a bitmask of enum snd_jack_type values that are currently detected.
  51. * @mask: a bitmask of enum snd_jack_type values that being reported.
  52. *
  53. * If configured using snd_soc_jack_add_pins() then the associated
  54. * DAPM pins will be enabled or disabled as appropriate and DAPM
  55. * synchronised.
  56. *
  57. * Note: This function uses mutexes and should be called from a
  58. * context which can sleep (such as a workqueue).
  59. */
  60. void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
  61. {
  62. struct snd_soc_codec *codec;
  63. struct snd_soc_dapm_context *dapm;
  64. struct snd_soc_jack_pin *pin;
  65. unsigned int sync = 0;
  66. int enable;
  67. trace_snd_soc_jack_report(jack, mask, status);
  68. if (!jack)
  69. return;
  70. codec = jack->codec;
  71. dapm = &codec->dapm;
  72. mutex_lock(&jack->mutex);
  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. /* we need to sync for this case only */
  85. sync = 1;
  86. }
  87. /* Report before the DAPM sync to help users updating micbias status */
  88. blocking_notifier_call_chain(&jack->notifier, jack->status, jack);
  89. if (sync)
  90. snd_soc_dapm_sync(dapm);
  91. snd_jack_report(jack->jack, jack->status);
  92. mutex_unlock(&jack->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 declared in the jack type
  119. *
  120. * @jack: ASoC jack
  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 declared 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. dev_err(jack->codec->dev, "ASoC: No name for pin %d\n",
  155. i);
  156. return -EINVAL;
  157. }
  158. if (!pins[i].mask) {
  159. dev_err(jack->codec->dev, "ASoC: No mask for pin %d"
  160. " (%s)\n", i, pins[i].pin);
  161. return -EINVAL;
  162. }
  163. INIT_LIST_HEAD(&pins[i].list);
  164. list_add(&(pins[i].list), &jack->pins);
  165. }
  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 = gpiod_get_value_cansleep(gpio->desc);
  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(gpio->data);
  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. queue_delayed_work(system_power_efficient_wq, &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 (!gpios[i].name) {
  259. dev_err(jack->codec->dev,
  260. "ASoC: No name for gpio at index %d\n", i);
  261. ret = -EINVAL;
  262. goto undo;
  263. }
  264. if (gpios[i].gpiod_dev) {
  265. /* GPIO descriptor */
  266. gpios[i].desc = gpiod_get_index(gpios[i].gpiod_dev,
  267. gpios[i].name,
  268. gpios[i].idx);
  269. if (IS_ERR(gpios[i].desc)) {
  270. ret = PTR_ERR(gpios[i].desc);
  271. dev_err(gpios[i].gpiod_dev,
  272. "ASoC: Cannot get gpio at index %d: %d",
  273. i, ret);
  274. goto undo;
  275. }
  276. } else {
  277. /* legacy GPIO number */
  278. if (!gpio_is_valid(gpios[i].gpio)) {
  279. dev_err(jack->codec->dev,
  280. "ASoC: Invalid gpio %d\n",
  281. gpios[i].gpio);
  282. ret = -EINVAL;
  283. goto undo;
  284. }
  285. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  286. if (ret)
  287. goto undo;
  288. gpios[i].desc = gpio_to_desc(gpios[i].gpio);
  289. }
  290. ret = gpiod_direction_input(gpios[i].desc);
  291. if (ret)
  292. goto err;
  293. INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
  294. gpios[i].jack = jack;
  295. ret = request_any_context_irq(gpiod_to_irq(gpios[i].desc),
  296. gpio_handler,
  297. IRQF_TRIGGER_RISING |
  298. IRQF_TRIGGER_FALLING,
  299. gpios[i].name,
  300. &gpios[i]);
  301. if (ret < 0)
  302. goto err;
  303. if (gpios[i].wake) {
  304. ret = irq_set_irq_wake(gpiod_to_irq(gpios[i].desc), 1);
  305. if (ret != 0)
  306. dev_err(jack->codec->dev,
  307. "ASoC: Failed to mark GPIO at index %d as wake source: %d\n",
  308. i, ret);
  309. }
  310. /* Expose GPIO value over sysfs for diagnostic purposes */
  311. gpiod_export(gpios[i].desc, false);
  312. /* Update initial jack status */
  313. schedule_delayed_work(&gpios[i].work,
  314. msecs_to_jiffies(gpios[i].debounce_time));
  315. }
  316. return 0;
  317. err:
  318. gpio_free(gpios[i].gpio);
  319. undo:
  320. snd_soc_jack_free_gpios(jack, i, gpios);
  321. return ret;
  322. }
  323. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
  324. /**
  325. * snd_soc_jack_add_gpiods - Associate GPIO descriptor pins with an ASoC jack
  326. *
  327. * @gpiod_dev: GPIO consumer device
  328. * @jack: ASoC jack
  329. * @count: number of pins
  330. * @gpios: array of gpio pins
  331. *
  332. * This function will request gpio, set data direction and request irq
  333. * for each gpio in the array.
  334. */
  335. int snd_soc_jack_add_gpiods(struct device *gpiod_dev,
  336. struct snd_soc_jack *jack,
  337. int count, struct snd_soc_jack_gpio *gpios)
  338. {
  339. int i;
  340. for (i = 0; i < count; i++)
  341. gpios[i].gpiod_dev = gpiod_dev;
  342. return snd_soc_jack_add_gpios(jack, count, gpios);
  343. }
  344. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods);
  345. /**
  346. * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
  347. *
  348. * @jack: ASoC jack
  349. * @count: number of pins
  350. * @gpios: array of gpio pins
  351. *
  352. * Release gpio and irq resources for gpio pins associated with an ASoC jack.
  353. */
  354. void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
  355. struct snd_soc_jack_gpio *gpios)
  356. {
  357. int i;
  358. for (i = 0; i < count; i++) {
  359. gpiod_unexport(gpios[i].desc);
  360. free_irq(gpiod_to_irq(gpios[i].desc), &gpios[i]);
  361. cancel_delayed_work_sync(&gpios[i].work);
  362. gpiod_put(gpios[i].desc);
  363. gpios[i].jack = NULL;
  364. }
  365. }
  366. EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
  367. #endif /* CONFIG_GPIOLIB */