soc-jack.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #include <linux/switch.h>
  22. #define SEC_JACK_NO_DEVICE 0
  23. #define SEC_HEADSET_4POLE 1
  24. #define SEC_HEADSET_3POLE 2
  25. #define WCD9XXX_JACK_MASK (SND_JACK_HEADSET | SND_JACK_OC_HPHL | \
  26. SND_JACK_OC_HPHR | SND_JACK_LINEOUT | \
  27. SND_JACK_UNSUPPORTED)
  28. /* Android jack detection */
  29. struct switch_dev android_switch = {
  30. .name = "h2w",
  31. };
  32. /**
  33. * snd_soc_jack_new - Create a new jack
  34. * @card: ASoC card
  35. * @id: an identifying string for this jack
  36. * @type: a bitmask of enum snd_jack_type values that can be detected by
  37. * this jack
  38. * @jack: structure to use for the jack
  39. *
  40. * Creates a new jack object.
  41. *
  42. * Returns zero if successful, or a negative error code on failure.
  43. * On success jack will be initialised.
  44. */
  45. int snd_soc_jack_new(struct snd_soc_codec *codec, const char *id, int type,
  46. struct snd_soc_jack *jack)
  47. {
  48. mutex_init(&jack->mutex);
  49. jack->codec = codec;
  50. INIT_LIST_HEAD(&jack->pins);
  51. INIT_LIST_HEAD(&jack->jack_zones);
  52. BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
  53. if(!strcmp(id, "Headset Jack")) {
  54. switch_dev_register(&android_switch);
  55. }
  56. return snd_jack_new(codec->card->snd_card, id, type, &jack->jack);
  57. }
  58. EXPORT_SYMBOL_GPL(snd_soc_jack_new);
  59. /**
  60. * snd_soc_jack_report - Report the current status for a jack
  61. *
  62. * @jack: the jack
  63. * @status: a bitmask of enum snd_jack_type values that are currently detected.
  64. * @mask: a bitmask of enum snd_jack_type values that being reported.
  65. *
  66. * If configured using snd_soc_jack_add_pins() then the associated
  67. * DAPM pins will be enabled or disabled as appropriate and DAPM
  68. * synchronised.
  69. *
  70. * Note: This function uses mutexes and should be called from a
  71. * context which can sleep (such as a workqueue).
  72. */
  73. void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
  74. {
  75. struct snd_soc_codec *codec;
  76. struct snd_soc_dapm_context *dapm;
  77. struct snd_soc_jack_pin *pin;
  78. int enable;
  79. int oldstatus;
  80. if (!jack)
  81. return;
  82. trace_snd_soc_jack_report(jack, mask, status);
  83. codec = jack->codec;
  84. dapm = &codec->dapm;
  85. mutex_lock(&jack->mutex);
  86. oldstatus = jack->status;
  87. jack->status &= ~mask;
  88. jack->status |= status & mask;
  89. /* The DAPM sync is expensive enough to be worth skipping.
  90. * However, empty mask means pin synchronization is desired. */
  91. if (mask && (jack->status == oldstatus))
  92. goto out;
  93. trace_snd_soc_jack_notify(jack, status);
  94. list_for_each_entry(pin, &jack->pins, list) {
  95. enable = pin->mask & jack->status;
  96. if (pin->invert)
  97. enable = !enable;
  98. if (enable)
  99. snd_soc_dapm_enable_pin(dapm, pin->pin);
  100. else
  101. snd_soc_dapm_disable_pin(dapm, pin->pin);
  102. }
  103. /* Report before the DAPM sync to help users updating micbias status */
  104. blocking_notifier_call_chain(&jack->notifier, status, jack);
  105. snd_soc_dapm_sync(dapm);
  106. snd_jack_report(jack->jack, jack->status);
  107. out:
  108. mutex_unlock(&jack->mutex);
  109. }
  110. EXPORT_SYMBOL_GPL(snd_soc_jack_report);
  111. /**
  112. * snd_soc_jack_report_no_dapm - Report the current status for a jack
  113. * without DAPM sync
  114. * @jack: the jack
  115. * @status: a bitmask of enum snd_jack_type values that are currently detected.
  116. * @mask: a bitmask of enum snd_jack_type values that being reported.
  117. */
  118. void snd_soc_jack_report_no_dapm(struct snd_soc_jack *jack, int status,
  119. int mask)
  120. {
  121. jack->status &= ~mask;
  122. jack->status |= status & mask;
  123. if (mask & WCD9XXX_JACK_MASK) {
  124. if (status == SEC_JACK_NO_DEVICE)
  125. switch_set_state(&android_switch, SEC_JACK_NO_DEVICE);
  126. else if (status == SND_JACK_HEADPHONE)
  127. switch_set_state(&android_switch, SEC_HEADSET_3POLE);
  128. else if (status == SND_JACK_HEADSET)
  129. switch_set_state(&android_switch, SEC_HEADSET_4POLE);
  130. }
  131. snd_jack_report(jack->jack, jack->status);
  132. }
  133. EXPORT_SYMBOL_GPL(snd_soc_jack_report_no_dapm);
  134. /**
  135. * snd_soc_jack_add_zones - Associate voltage zones with jack
  136. *
  137. * @jack: ASoC jack
  138. * @count: Number of zones
  139. * @zone: Array of zones
  140. *
  141. * After this function has been called the zones specified in the
  142. * array will be associated with the jack.
  143. */
  144. int snd_soc_jack_add_zones(struct snd_soc_jack *jack, int count,
  145. struct snd_soc_jack_zone *zones)
  146. {
  147. int i;
  148. for (i = 0; i < count; i++) {
  149. INIT_LIST_HEAD(&zones[i].list);
  150. list_add(&(zones[i].list), &jack->jack_zones);
  151. }
  152. return 0;
  153. }
  154. EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones);
  155. /**
  156. * snd_soc_jack_get_type - Based on the mic bias value, this function returns
  157. * the type of jack from the zones delcared in the jack type
  158. *
  159. * @micbias_voltage: mic bias voltage at adc channel when jack is plugged in
  160. *
  161. * Based on the mic bias value passed, this function helps identify
  162. * the type of jack from the already delcared jack zones
  163. */
  164. int snd_soc_jack_get_type(struct snd_soc_jack *jack, int micbias_voltage)
  165. {
  166. struct snd_soc_jack_zone *zone;
  167. list_for_each_entry(zone, &jack->jack_zones, list) {
  168. if (micbias_voltage >= zone->min_mv &&
  169. micbias_voltage < zone->max_mv)
  170. return zone->jack_type;
  171. }
  172. return 0;
  173. }
  174. EXPORT_SYMBOL_GPL(snd_soc_jack_get_type);
  175. /**
  176. * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
  177. *
  178. * @jack: ASoC jack
  179. * @count: Number of pins
  180. * @pins: Array of pins
  181. *
  182. * After this function has been called the DAPM pins specified in the
  183. * pins array will have their status updated to reflect the current
  184. * state of the jack whenever the jack status is updated.
  185. */
  186. int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
  187. struct snd_soc_jack_pin *pins)
  188. {
  189. int i;
  190. for (i = 0; i < count; i++) {
  191. if (!pins[i].pin) {
  192. printk(KERN_ERR "No name for pin %d\n", i);
  193. return -EINVAL;
  194. }
  195. if (!pins[i].mask) {
  196. printk(KERN_ERR "No mask for pin %d (%s)\n", i,
  197. pins[i].pin);
  198. return -EINVAL;
  199. }
  200. INIT_LIST_HEAD(&pins[i].list);
  201. list_add(&(pins[i].list), &jack->pins);
  202. }
  203. snd_soc_dapm_new_widgets(&jack->codec->card->dapm);
  204. /* Update to reflect the last reported status; canned jack
  205. * implementations are likely to set their state before the
  206. * card has an opportunity to associate pins.
  207. */
  208. snd_soc_jack_report(jack, 0, 0);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
  212. /**
  213. * snd_soc_jack_notifier_register - Register a notifier for jack status
  214. *
  215. * @jack: ASoC jack
  216. * @nb: Notifier block to register
  217. *
  218. * Register for notification of the current status of the jack. Note
  219. * that it is not possible to report additional jack events in the
  220. * callback from the notifier, this is intended to support
  221. * applications such as enabling electrical detection only when a
  222. * mechanical detection event has occurred.
  223. */
  224. void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
  225. struct notifier_block *nb)
  226. {
  227. blocking_notifier_chain_register(&jack->notifier, nb);
  228. }
  229. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
  230. /**
  231. * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
  232. *
  233. * @jack: ASoC jack
  234. * @nb: Notifier block to unregister
  235. *
  236. * Stop notifying for status changes.
  237. */
  238. void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
  239. struct notifier_block *nb)
  240. {
  241. blocking_notifier_chain_unregister(&jack->notifier, nb);
  242. }
  243. EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
  244. #ifdef CONFIG_GPIOLIB
  245. /* gpio detect */
  246. static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
  247. {
  248. struct snd_soc_jack *jack = gpio->jack;
  249. int enable;
  250. int report;
  251. enable = gpio_get_value_cansleep(gpio->gpio);
  252. if (gpio->invert)
  253. enable = !enable;
  254. if (enable)
  255. report = gpio->report;
  256. else
  257. report = 0;
  258. if (gpio->jack_status_check)
  259. report = gpio->jack_status_check();
  260. snd_soc_jack_report(jack, report, gpio->report);
  261. }
  262. /* irq handler for gpio pin */
  263. static irqreturn_t gpio_handler(int irq, void *data)
  264. {
  265. struct snd_soc_jack_gpio *gpio = data;
  266. struct device *dev = gpio->jack->codec->card->dev;
  267. trace_snd_soc_jack_irq(gpio->name);
  268. if (device_may_wakeup(dev))
  269. pm_wakeup_event(dev, gpio->debounce_time + 50);
  270. schedule_delayed_work(&gpio->work,
  271. msecs_to_jiffies(gpio->debounce_time));
  272. return IRQ_HANDLED;
  273. }
  274. /* gpio work */
  275. static void gpio_work(struct work_struct *work)
  276. {
  277. struct snd_soc_jack_gpio *gpio;
  278. gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
  279. snd_soc_jack_gpio_detect(gpio);
  280. }
  281. /**
  282. * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
  283. *
  284. * @jack: ASoC jack
  285. * @count: number of pins
  286. * @gpios: array of gpio pins
  287. *
  288. * This function will request gpio, set data direction and request irq
  289. * for each gpio in the array.
  290. */
  291. int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
  292. struct snd_soc_jack_gpio *gpios)
  293. {
  294. int i, ret;
  295. for (i = 0; i < count; i++) {
  296. if (!gpio_is_valid(gpios[i].gpio)) {
  297. printk(KERN_ERR "Invalid gpio %d\n",
  298. gpios[i].gpio);
  299. ret = -EINVAL;
  300. goto undo;
  301. }
  302. if (!gpios[i].name) {
  303. printk(KERN_ERR "No name for gpio %d\n",
  304. gpios[i].gpio);
  305. ret = -EINVAL;
  306. goto undo;
  307. }
  308. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  309. if (ret)
  310. goto undo;
  311. ret = gpio_direction_input(gpios[i].gpio);
  312. if (ret)
  313. goto err;
  314. INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
  315. gpios[i].jack = jack;
  316. ret = request_any_context_irq(gpio_to_irq(gpios[i].gpio),
  317. gpio_handler,
  318. IRQF_TRIGGER_RISING |
  319. IRQF_TRIGGER_FALLING,
  320. gpios[i].name,
  321. &gpios[i]);
  322. if (ret < 0)
  323. goto err;
  324. if (gpios[i].wake) {
  325. ret = irq_set_irq_wake(gpio_to_irq(gpios[i].gpio), 1);
  326. if (ret != 0)
  327. printk(KERN_ERR
  328. "Failed to mark GPIO %d as wake source: %d\n",
  329. gpios[i].gpio, ret);
  330. }
  331. /* Expose GPIO value over sysfs for diagnostic purposes */
  332. gpio_export(gpios[i].gpio, false);
  333. /* Update initial jack status */
  334. snd_soc_jack_gpio_detect(&gpios[i]);
  335. }
  336. return 0;
  337. err:
  338. gpio_free(gpios[i].gpio);
  339. undo:
  340. snd_soc_jack_free_gpios(jack, i, gpios);
  341. return ret;
  342. }
  343. EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
  344. /**
  345. * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
  346. *
  347. * @jack: ASoC jack
  348. * @count: number of pins
  349. * @gpios: array of gpio pins
  350. *
  351. * Release gpio and irq resources for gpio pins associated with an ASoC jack.
  352. */
  353. void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
  354. struct snd_soc_jack_gpio *gpios)
  355. {
  356. int i;
  357. for (i = 0; i < count; i++) {
  358. gpio_unexport(gpios[i].gpio);
  359. free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]);
  360. cancel_delayed_work_sync(&gpios[i].work);
  361. gpio_free(gpios[i].gpio);
  362. gpios[i].jack = NULL;
  363. }
  364. }
  365. EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
  366. #endif /* CONFIG_GPIOLIB */