usb-conn-gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB GPIO Based Connection Detection Driver
  4. *
  5. * Copyright (C) 2019 MediaTek Inc.
  6. *
  7. * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
  8. *
  9. * Some code borrowed from drivers/extcon/extcon-usb-gpio.c
  10. */
  11. #include <linux/device.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/pinctrl/consumer.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/usb/role.h>
  21. #define USB_GPIO_DEB_MS 20 /* ms */
  22. #define USB_GPIO_DEB_US ((USB_GPIO_DEB_MS) * 1000) /* us */
  23. #define USB_CONN_IRQF \
  24. (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT)
  25. struct usb_conn_info {
  26. struct device *dev;
  27. struct usb_role_switch *role_sw;
  28. enum usb_role last_role;
  29. struct regulator *vbus;
  30. struct delayed_work dw_det;
  31. unsigned long debounce_jiffies;
  32. struct gpio_desc *id_gpiod;
  33. struct gpio_desc *vbus_gpiod;
  34. int id_irq;
  35. int vbus_irq;
  36. };
  37. /**
  38. * "DEVICE" = VBUS and "HOST" = !ID, so we have:
  39. * Both "DEVICE" and "HOST" can't be set as active at the same time
  40. * so if "HOST" is active (i.e. ID is 0) we keep "DEVICE" inactive
  41. * even if VBUS is on.
  42. *
  43. * Role | ID | VBUS
  44. * ------------------------------------
  45. * [1] DEVICE | H | H
  46. * [2] NONE | H | L
  47. * [3] HOST | L | H
  48. * [4] 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_conn_detect_cable(struct work_struct *work)
  55. {
  56. struct usb_conn_info *info;
  57. enum usb_role role;
  58. int id, vbus, ret;
  59. info = container_of(to_delayed_work(work),
  60. struct usb_conn_info, dw_det);
  61. /* check ID and VBUS */
  62. id = info->id_gpiod ?
  63. gpiod_get_value_cansleep(info->id_gpiod) : 1;
  64. vbus = info->vbus_gpiod ?
  65. gpiod_get_value_cansleep(info->vbus_gpiod) : id;
  66. if (!id)
  67. role = USB_ROLE_HOST;
  68. else if (vbus)
  69. role = USB_ROLE_DEVICE;
  70. else
  71. role = USB_ROLE_NONE;
  72. dev_dbg(info->dev, "role %d/%d, gpios: id %d, vbus %d\n",
  73. info->last_role, role, id, vbus);
  74. if (info->last_role == role) {
  75. dev_warn(info->dev, "repeated role: %d\n", role);
  76. return;
  77. }
  78. if (info->last_role == USB_ROLE_HOST)
  79. regulator_disable(info->vbus);
  80. ret = usb_role_switch_set_role(info->role_sw, role);
  81. if (ret)
  82. dev_err(info->dev, "failed to set role: %d\n", ret);
  83. if (role == USB_ROLE_HOST) {
  84. ret = regulator_enable(info->vbus);
  85. if (ret)
  86. dev_err(info->dev, "enable vbus regulator failed\n");
  87. }
  88. info->last_role = role;
  89. dev_dbg(info->dev, "vbus regulator is %s\n",
  90. regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
  91. }
  92. static void usb_conn_queue_dwork(struct usb_conn_info *info,
  93. unsigned long delay)
  94. {
  95. queue_delayed_work(system_power_efficient_wq, &info->dw_det, delay);
  96. }
  97. static irqreturn_t usb_conn_isr(int irq, void *dev_id)
  98. {
  99. struct usb_conn_info *info = dev_id;
  100. usb_conn_queue_dwork(info, info->debounce_jiffies);
  101. return IRQ_HANDLED;
  102. }
  103. static int usb_conn_probe(struct platform_device *pdev)
  104. {
  105. struct device *dev = &pdev->dev;
  106. struct usb_conn_info *info;
  107. int ret = 0;
  108. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  109. if (!info)
  110. return -ENOMEM;
  111. info->dev = dev;
  112. info->id_gpiod = devm_gpiod_get_optional(dev, "id", GPIOD_IN);
  113. if (IS_ERR(info->id_gpiod))
  114. return PTR_ERR(info->id_gpiod);
  115. info->vbus_gpiod = devm_gpiod_get_optional(dev, "vbus", GPIOD_IN);
  116. if (IS_ERR(info->vbus_gpiod))
  117. return PTR_ERR(info->vbus_gpiod);
  118. if (!info->id_gpiod && !info->vbus_gpiod) {
  119. dev_err(dev, "failed to get gpios\n");
  120. return -ENODEV;
  121. }
  122. if (info->id_gpiod)
  123. ret = gpiod_set_debounce(info->id_gpiod, USB_GPIO_DEB_US);
  124. if (!ret && info->vbus_gpiod)
  125. ret = gpiod_set_debounce(info->vbus_gpiod, USB_GPIO_DEB_US);
  126. if (ret < 0)
  127. info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEB_MS);
  128. INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable);
  129. info->vbus = devm_regulator_get(dev, "vbus");
  130. if (IS_ERR(info->vbus)) {
  131. if (PTR_ERR(info->vbus) != -EPROBE_DEFER)
  132. dev_err(dev, "failed to get vbus\n");
  133. return PTR_ERR(info->vbus);
  134. }
  135. info->role_sw = usb_role_switch_get(dev);
  136. if (IS_ERR(info->role_sw)) {
  137. if (PTR_ERR(info->role_sw) != -EPROBE_DEFER)
  138. dev_err(dev, "failed to get role switch\n");
  139. return PTR_ERR(info->role_sw);
  140. }
  141. if (info->id_gpiod) {
  142. info->id_irq = gpiod_to_irq(info->id_gpiod);
  143. if (info->id_irq < 0) {
  144. dev_err(dev, "failed to get ID IRQ\n");
  145. ret = info->id_irq;
  146. goto put_role_sw;
  147. }
  148. ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
  149. usb_conn_isr, USB_CONN_IRQF,
  150. pdev->name, info);
  151. if (ret < 0) {
  152. dev_err(dev, "failed to request ID IRQ\n");
  153. goto put_role_sw;
  154. }
  155. }
  156. if (info->vbus_gpiod) {
  157. info->vbus_irq = gpiod_to_irq(info->vbus_gpiod);
  158. if (info->vbus_irq < 0) {
  159. dev_err(dev, "failed to get VBUS IRQ\n");
  160. ret = info->vbus_irq;
  161. goto put_role_sw;
  162. }
  163. ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
  164. usb_conn_isr, USB_CONN_IRQF,
  165. pdev->name, info);
  166. if (ret < 0) {
  167. dev_err(dev, "failed to request VBUS IRQ\n");
  168. goto put_role_sw;
  169. }
  170. }
  171. platform_set_drvdata(pdev, info);
  172. /* Perform initial detection */
  173. usb_conn_queue_dwork(info, 0);
  174. return 0;
  175. put_role_sw:
  176. usb_role_switch_put(info->role_sw);
  177. return ret;
  178. }
  179. static int usb_conn_remove(struct platform_device *pdev)
  180. {
  181. struct usb_conn_info *info = platform_get_drvdata(pdev);
  182. cancel_delayed_work_sync(&info->dw_det);
  183. if (info->last_role == USB_ROLE_HOST)
  184. regulator_disable(info->vbus);
  185. usb_role_switch_put(info->role_sw);
  186. return 0;
  187. }
  188. static int __maybe_unused usb_conn_suspend(struct device *dev)
  189. {
  190. struct usb_conn_info *info = dev_get_drvdata(dev);
  191. if (info->id_gpiod)
  192. disable_irq(info->id_irq);
  193. if (info->vbus_gpiod)
  194. disable_irq(info->vbus_irq);
  195. pinctrl_pm_select_sleep_state(dev);
  196. return 0;
  197. }
  198. static int __maybe_unused usb_conn_resume(struct device *dev)
  199. {
  200. struct usb_conn_info *info = dev_get_drvdata(dev);
  201. pinctrl_pm_select_default_state(dev);
  202. if (info->id_gpiod)
  203. enable_irq(info->id_irq);
  204. if (info->vbus_gpiod)
  205. enable_irq(info->vbus_irq);
  206. usb_conn_queue_dwork(info, 0);
  207. return 0;
  208. }
  209. static SIMPLE_DEV_PM_OPS(usb_conn_pm_ops,
  210. usb_conn_suspend, usb_conn_resume);
  211. static const struct of_device_id usb_conn_dt_match[] = {
  212. { .compatible = "gpio-usb-b-connector", },
  213. { }
  214. };
  215. MODULE_DEVICE_TABLE(of, usb_conn_dt_match);
  216. static struct platform_driver usb_conn_driver = {
  217. .probe = usb_conn_probe,
  218. .remove = usb_conn_remove,
  219. .driver = {
  220. .name = "usb-conn-gpio",
  221. .pm = &usb_conn_pm_ops,
  222. .of_match_table = usb_conn_dt_match,
  223. },
  224. };
  225. module_platform_driver(usb_conn_driver);
  226. MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
  227. MODULE_DESCRIPTION("USB GPIO based connection detection driver");
  228. MODULE_LICENSE("GPL v2");