extcon-usb-gpio.c 7.1 KB

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