extcon-usb-gpio.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**
  3. * drivers/extcon/extcon-usb-gpio.c - USB GPIO extcon driver
  4. *
  5. * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com
  6. * Author: Roger Quadros <rogerq@ti.com>
  7. */
  8. #include <linux/extcon-provider.h>
  9. #include <linux/gpio.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/pinctrl/consumer.h>
  21. #define USB_GPIO_DEBOUNCE_MS 20 /* ms */
  22. struct usb_extcon_info {
  23. struct device *dev;
  24. struct extcon_dev *edev;
  25. struct gpio_desc *id_gpiod;
  26. struct gpio_desc *vbus_gpiod;
  27. int id_irq;
  28. int vbus_irq;
  29. unsigned long debounce_jiffies;
  30. struct delayed_work wq_detcable;
  31. };
  32. static const unsigned int usb_extcon_cable[] = {
  33. EXTCON_USB,
  34. EXTCON_USB_HOST,
  35. EXTCON_NONE,
  36. };
  37. /*
  38. * "USB" = VBUS and "USB-HOST" = !ID, so we have:
  39. * Both "USB" and "USB-HOST" can't be set as active at the
  40. * same time so if "USB-HOST" is active (i.e. ID is 0) we keep "USB" inactive
  41. * even if VBUS is on.
  42. *
  43. * State | ID | VBUS
  44. * ----------------------------------------
  45. * [1] USB | H | H
  46. * [2] none | H | L
  47. * [3] USB-HOST | L | H
  48. * [4] USB-HOST | L | L
  49. *
  50. * In case we have only one of these signals:
  51. * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1.
  52. * - ID only - we want to distinguish between [1] and [4], so VBUS = ID.
  53. */
  54. static void usb_extcon_detect_cable(struct work_struct *work)
  55. {
  56. int id, vbus;
  57. struct usb_extcon_info *info = container_of(to_delayed_work(work),
  58. struct usb_extcon_info,
  59. wq_detcable);
  60. /* check ID and VBUS and update cable state */
  61. id = info->id_gpiod ?
  62. gpiod_get_value_cansleep(info->id_gpiod) : 1;
  63. vbus = info->vbus_gpiod ?
  64. gpiod_get_value_cansleep(info->vbus_gpiod) : id;
  65. /* at first we clean states which are no longer active */
  66. if (id)
  67. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
  68. if (!vbus)
  69. extcon_set_state_sync(info->edev, EXTCON_USB, false);
  70. if (!id) {
  71. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
  72. } else {
  73. if (vbus)
  74. extcon_set_state_sync(info->edev, EXTCON_USB, true);
  75. }
  76. }
  77. static irqreturn_t usb_irq_handler(int irq, void *dev_id)
  78. {
  79. struct usb_extcon_info *info = dev_id;
  80. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  81. info->debounce_jiffies);
  82. return IRQ_HANDLED;
  83. }
  84. static int usb_extcon_probe(struct platform_device *pdev)
  85. {
  86. struct device *dev = &pdev->dev;
  87. struct device_node *np = dev->of_node;
  88. struct usb_extcon_info *info;
  89. int ret;
  90. if (!np)
  91. return -EINVAL;
  92. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  93. if (!info)
  94. return -ENOMEM;
  95. info->dev = dev;
  96. info->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id", GPIOD_IN);
  97. info->vbus_gpiod = devm_gpiod_get_optional(&pdev->dev, "vbus",
  98. GPIOD_IN);
  99. if (!info->id_gpiod && !info->vbus_gpiod) {
  100. dev_err(dev, "failed to get gpios\n");
  101. return -ENODEV;
  102. }
  103. if (IS_ERR(info->id_gpiod))
  104. return PTR_ERR(info->id_gpiod);
  105. if (IS_ERR(info->vbus_gpiod))
  106. return PTR_ERR(info->vbus_gpiod);
  107. info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
  108. if (IS_ERR(info->edev)) {
  109. dev_err(dev, "failed to allocate extcon device\n");
  110. return -ENOMEM;
  111. }
  112. ret = devm_extcon_dev_register(dev, info->edev);
  113. if (ret < 0) {
  114. dev_err(dev, "failed to register extcon device\n");
  115. return ret;
  116. }
  117. if (info->id_gpiod)
  118. ret = gpiod_set_debounce(info->id_gpiod,
  119. USB_GPIO_DEBOUNCE_MS * 1000);
  120. if (!ret && info->vbus_gpiod)
  121. ret = gpiod_set_debounce(info->vbus_gpiod,
  122. USB_GPIO_DEBOUNCE_MS * 1000);
  123. if (ret < 0)
  124. info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
  125. INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
  126. if (info->id_gpiod) {
  127. info->id_irq = gpiod_to_irq(info->id_gpiod);
  128. if (info->id_irq < 0) {
  129. dev_err(dev, "failed to get ID IRQ\n");
  130. return info->id_irq;
  131. }
  132. ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
  133. usb_irq_handler,
  134. IRQF_TRIGGER_RISING |
  135. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  136. pdev->name, info);
  137. if (ret < 0) {
  138. dev_err(dev, "failed to request handler for ID IRQ\n");
  139. return ret;
  140. }
  141. }
  142. if (info->vbus_gpiod) {
  143. info->vbus_irq = gpiod_to_irq(info->vbus_gpiod);
  144. if (info->vbus_irq < 0) {
  145. dev_err(dev, "failed to get VBUS IRQ\n");
  146. return info->vbus_irq;
  147. }
  148. ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
  149. usb_irq_handler,
  150. IRQF_TRIGGER_RISING |
  151. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  152. pdev->name, info);
  153. if (ret < 0) {
  154. dev_err(dev, "failed to request handler for VBUS IRQ\n");
  155. return ret;
  156. }
  157. }
  158. platform_set_drvdata(pdev, info);
  159. device_set_wakeup_capable(&pdev->dev, true);
  160. /* Perform initial detection */
  161. usb_extcon_detect_cable(&info->wq_detcable.work);
  162. return 0;
  163. }
  164. static int usb_extcon_remove(struct platform_device *pdev)
  165. {
  166. struct usb_extcon_info *info = platform_get_drvdata(pdev);
  167. cancel_delayed_work_sync(&info->wq_detcable);
  168. device_init_wakeup(&pdev->dev, false);
  169. return 0;
  170. }
  171. #ifdef CONFIG_PM_SLEEP
  172. static int usb_extcon_suspend(struct device *dev)
  173. {
  174. struct usb_extcon_info *info = dev_get_drvdata(dev);
  175. int ret = 0;
  176. if (device_may_wakeup(dev)) {
  177. if (info->id_gpiod) {
  178. ret = enable_irq_wake(info->id_irq);
  179. if (ret)
  180. return ret;
  181. }
  182. if (info->vbus_gpiod) {
  183. ret = enable_irq_wake(info->vbus_irq);
  184. if (ret) {
  185. if (info->id_gpiod)
  186. disable_irq_wake(info->id_irq);
  187. return ret;
  188. }
  189. }
  190. }
  191. /*
  192. * We don't want to process any IRQs after this point
  193. * as GPIOs used behind I2C subsystem might not be
  194. * accessible until resume completes. So disable IRQ.
  195. */
  196. if (info->id_gpiod)
  197. disable_irq(info->id_irq);
  198. if (info->vbus_gpiod)
  199. disable_irq(info->vbus_irq);
  200. if (!device_may_wakeup(dev))
  201. pinctrl_pm_select_sleep_state(dev);
  202. return ret;
  203. }
  204. static int usb_extcon_resume(struct device *dev)
  205. {
  206. struct usb_extcon_info *info = dev_get_drvdata(dev);
  207. int ret = 0;
  208. if (!device_may_wakeup(dev))
  209. pinctrl_pm_select_default_state(dev);
  210. if (device_may_wakeup(dev)) {
  211. if (info->id_gpiod) {
  212. ret = disable_irq_wake(info->id_irq);
  213. if (ret)
  214. return ret;
  215. }
  216. if (info->vbus_gpiod) {
  217. ret = disable_irq_wake(info->vbus_irq);
  218. if (ret) {
  219. if (info->id_gpiod)
  220. enable_irq_wake(info->id_irq);
  221. return ret;
  222. }
  223. }
  224. }
  225. if (info->id_gpiod)
  226. enable_irq(info->id_irq);
  227. if (info->vbus_gpiod)
  228. enable_irq(info->vbus_irq);
  229. queue_delayed_work(system_power_efficient_wq,
  230. &info->wq_detcable, 0);
  231. return ret;
  232. }
  233. #endif
  234. static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
  235. usb_extcon_suspend, usb_extcon_resume);
  236. static const struct of_device_id usb_extcon_dt_match[] = {
  237. { .compatible = "linux,extcon-usb-gpio", },
  238. { /* sentinel */ }
  239. };
  240. MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
  241. static const struct platform_device_id usb_extcon_platform_ids[] = {
  242. { .name = "extcon-usb-gpio", },
  243. { /* sentinel */ }
  244. };
  245. MODULE_DEVICE_TABLE(platform, usb_extcon_platform_ids);
  246. static struct platform_driver usb_extcon_driver = {
  247. .probe = usb_extcon_probe,
  248. .remove = usb_extcon_remove,
  249. .driver = {
  250. .name = "extcon-usb-gpio",
  251. .pm = &usb_extcon_pm_ops,
  252. .of_match_table = usb_extcon_dt_match,
  253. },
  254. .id_table = usb_extcon_platform_ids,
  255. };
  256. module_platform_driver(usb_extcon_driver);
  257. MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
  258. MODULE_DESCRIPTION("USB GPIO extcon driver");
  259. MODULE_LICENSE("GPL v2");