usb-conn-gpio.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <[email protected]>
  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/power_supply.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/usb/role.h>
  22. #define USB_GPIO_DEB_MS 20 /* ms */
  23. #define USB_GPIO_DEB_US ((USB_GPIO_DEB_MS) * 1000) /* us */
  24. #define USB_CONN_IRQF \
  25. (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT)
  26. struct usb_conn_info {
  27. struct device *dev;
  28. struct usb_role_switch *role_sw;
  29. enum usb_role last_role;
  30. struct regulator *vbus;
  31. struct delayed_work dw_det;
  32. unsigned long debounce_jiffies;
  33. struct gpio_desc *id_gpiod;
  34. struct gpio_desc *vbus_gpiod;
  35. int id_irq;
  36. int vbus_irq;
  37. struct power_supply_desc desc;
  38. struct power_supply *charger;
  39. bool initial_detection;
  40. };
  41. /*
  42. * "DEVICE" = VBUS and "HOST" = !ID, so we have:
  43. * Both "DEVICE" and "HOST" can't be set as active at the same time
  44. * so if "HOST" is active (i.e. ID is 0) we keep "DEVICE" inactive
  45. * even if VBUS is on.
  46. *
  47. * Role | ID | VBUS
  48. * ------------------------------------
  49. * [1] DEVICE | H | H
  50. * [2] NONE | H | L
  51. * [3] HOST | L | H
  52. * [4] HOST | L | L
  53. *
  54. * In case we have only one of these signals:
  55. * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1
  56. * - ID only - we want to distinguish between [1] and [4], so VBUS = ID
  57. */
  58. static void usb_conn_detect_cable(struct work_struct *work)
  59. {
  60. struct usb_conn_info *info;
  61. enum usb_role role;
  62. int id, vbus, ret;
  63. info = container_of(to_delayed_work(work),
  64. struct usb_conn_info, dw_det);
  65. /* check ID and VBUS */
  66. id = info->id_gpiod ?
  67. gpiod_get_value_cansleep(info->id_gpiod) : 1;
  68. vbus = info->vbus_gpiod ?
  69. gpiod_get_value_cansleep(info->vbus_gpiod) : id;
  70. if (!id)
  71. role = USB_ROLE_HOST;
  72. else if (vbus)
  73. role = USB_ROLE_DEVICE;
  74. else
  75. role = USB_ROLE_NONE;
  76. dev_dbg(info->dev, "role %s -> %s, gpios: id %d, vbus %d\n",
  77. usb_role_string(info->last_role), usb_role_string(role), id, vbus);
  78. if (!info->initial_detection && info->last_role == role) {
  79. dev_warn(info->dev, "repeated role: %s\n", usb_role_string(role));
  80. return;
  81. }
  82. info->initial_detection = false;
  83. if (info->last_role == USB_ROLE_HOST && info->vbus)
  84. regulator_disable(info->vbus);
  85. ret = usb_role_switch_set_role(info->role_sw, role);
  86. if (ret)
  87. dev_err(info->dev, "failed to set role: %d\n", ret);
  88. if (role == USB_ROLE_HOST && info->vbus) {
  89. ret = regulator_enable(info->vbus);
  90. if (ret)
  91. dev_err(info->dev, "enable vbus regulator failed\n");
  92. }
  93. info->last_role = role;
  94. if (info->vbus)
  95. dev_dbg(info->dev, "vbus regulator is %s\n",
  96. regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
  97. power_supply_changed(info->charger);
  98. }
  99. static void usb_conn_queue_dwork(struct usb_conn_info *info,
  100. unsigned long delay)
  101. {
  102. queue_delayed_work(system_power_efficient_wq, &info->dw_det, delay);
  103. }
  104. static irqreturn_t usb_conn_isr(int irq, void *dev_id)
  105. {
  106. struct usb_conn_info *info = dev_id;
  107. usb_conn_queue_dwork(info, info->debounce_jiffies);
  108. return IRQ_HANDLED;
  109. }
  110. static enum power_supply_property usb_charger_properties[] = {
  111. POWER_SUPPLY_PROP_ONLINE,
  112. };
  113. static int usb_charger_get_property(struct power_supply *psy,
  114. enum power_supply_property psp,
  115. union power_supply_propval *val)
  116. {
  117. struct usb_conn_info *info = power_supply_get_drvdata(psy);
  118. switch (psp) {
  119. case POWER_SUPPLY_PROP_ONLINE:
  120. val->intval = info->last_role == USB_ROLE_DEVICE;
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. return 0;
  126. }
  127. static int usb_conn_psy_register(struct usb_conn_info *info)
  128. {
  129. struct device *dev = info->dev;
  130. struct power_supply_desc *desc = &info->desc;
  131. struct power_supply_config cfg = {
  132. .of_node = dev->of_node,
  133. };
  134. desc->name = "usb-charger";
  135. desc->properties = usb_charger_properties;
  136. desc->num_properties = ARRAY_SIZE(usb_charger_properties);
  137. desc->get_property = usb_charger_get_property;
  138. desc->type = POWER_SUPPLY_TYPE_USB;
  139. cfg.drv_data = info;
  140. info->charger = devm_power_supply_register(dev, desc, &cfg);
  141. if (IS_ERR(info->charger))
  142. dev_err(dev, "Unable to register charger\n");
  143. return PTR_ERR_OR_ZERO(info->charger);
  144. }
  145. static int usb_conn_probe(struct platform_device *pdev)
  146. {
  147. struct device *dev = &pdev->dev;
  148. struct usb_conn_info *info;
  149. int ret = 0;
  150. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  151. if (!info)
  152. return -ENOMEM;
  153. info->dev = dev;
  154. info->id_gpiod = devm_gpiod_get_optional(dev, "id", GPIOD_IN);
  155. if (IS_ERR(info->id_gpiod))
  156. return PTR_ERR(info->id_gpiod);
  157. info->vbus_gpiod = devm_gpiod_get_optional(dev, "vbus", GPIOD_IN);
  158. if (IS_ERR(info->vbus_gpiod))
  159. return PTR_ERR(info->vbus_gpiod);
  160. if (!info->id_gpiod && !info->vbus_gpiod) {
  161. dev_err(dev, "failed to get gpios\n");
  162. return -ENODEV;
  163. }
  164. if (info->id_gpiod)
  165. ret = gpiod_set_debounce(info->id_gpiod, USB_GPIO_DEB_US);
  166. if (!ret && info->vbus_gpiod)
  167. ret = gpiod_set_debounce(info->vbus_gpiod, USB_GPIO_DEB_US);
  168. if (ret < 0)
  169. info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEB_MS);
  170. INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable);
  171. info->vbus = devm_regulator_get_optional(dev, "vbus");
  172. if (PTR_ERR(info->vbus) == -ENODEV)
  173. info->vbus = NULL;
  174. if (IS_ERR(info->vbus))
  175. return dev_err_probe(dev, PTR_ERR(info->vbus), "failed to get vbus\n");
  176. info->role_sw = usb_role_switch_get(dev);
  177. if (IS_ERR(info->role_sw))
  178. return dev_err_probe(dev, PTR_ERR(info->role_sw),
  179. "failed to get role switch\n");
  180. ret = usb_conn_psy_register(info);
  181. if (ret)
  182. goto put_role_sw;
  183. if (info->id_gpiod) {
  184. info->id_irq = gpiod_to_irq(info->id_gpiod);
  185. if (info->id_irq < 0) {
  186. dev_err(dev, "failed to get ID IRQ\n");
  187. ret = info->id_irq;
  188. goto put_role_sw;
  189. }
  190. ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
  191. usb_conn_isr, USB_CONN_IRQF,
  192. pdev->name, info);
  193. if (ret < 0) {
  194. dev_err(dev, "failed to request ID IRQ\n");
  195. goto put_role_sw;
  196. }
  197. }
  198. if (info->vbus_gpiod) {
  199. info->vbus_irq = gpiod_to_irq(info->vbus_gpiod);
  200. if (info->vbus_irq < 0) {
  201. dev_err(dev, "failed to get VBUS IRQ\n");
  202. ret = info->vbus_irq;
  203. goto put_role_sw;
  204. }
  205. ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
  206. usb_conn_isr, USB_CONN_IRQF,
  207. pdev->name, info);
  208. if (ret < 0) {
  209. dev_err(dev, "failed to request VBUS IRQ\n");
  210. goto put_role_sw;
  211. }
  212. }
  213. platform_set_drvdata(pdev, info);
  214. device_set_wakeup_capable(&pdev->dev, true);
  215. /* Perform initial detection */
  216. info->initial_detection = true;
  217. usb_conn_queue_dwork(info, 0);
  218. return 0;
  219. put_role_sw:
  220. usb_role_switch_put(info->role_sw);
  221. return ret;
  222. }
  223. static int usb_conn_remove(struct platform_device *pdev)
  224. {
  225. struct usb_conn_info *info = platform_get_drvdata(pdev);
  226. cancel_delayed_work_sync(&info->dw_det);
  227. if (info->last_role == USB_ROLE_HOST && info->vbus)
  228. regulator_disable(info->vbus);
  229. usb_role_switch_put(info->role_sw);
  230. return 0;
  231. }
  232. static int __maybe_unused usb_conn_suspend(struct device *dev)
  233. {
  234. struct usb_conn_info *info = dev_get_drvdata(dev);
  235. if (device_may_wakeup(dev)) {
  236. if (info->id_gpiod)
  237. enable_irq_wake(info->id_irq);
  238. if (info->vbus_gpiod)
  239. enable_irq_wake(info->vbus_irq);
  240. return 0;
  241. }
  242. if (info->id_gpiod)
  243. disable_irq(info->id_irq);
  244. if (info->vbus_gpiod)
  245. disable_irq(info->vbus_irq);
  246. pinctrl_pm_select_sleep_state(dev);
  247. return 0;
  248. }
  249. static int __maybe_unused usb_conn_resume(struct device *dev)
  250. {
  251. struct usb_conn_info *info = dev_get_drvdata(dev);
  252. if (device_may_wakeup(dev)) {
  253. if (info->id_gpiod)
  254. disable_irq_wake(info->id_irq);
  255. if (info->vbus_gpiod)
  256. disable_irq_wake(info->vbus_irq);
  257. return 0;
  258. }
  259. pinctrl_pm_select_default_state(dev);
  260. if (info->id_gpiod)
  261. enable_irq(info->id_irq);
  262. if (info->vbus_gpiod)
  263. enable_irq(info->vbus_irq);
  264. usb_conn_queue_dwork(info, 0);
  265. return 0;
  266. }
  267. static SIMPLE_DEV_PM_OPS(usb_conn_pm_ops,
  268. usb_conn_suspend, usb_conn_resume);
  269. static const struct of_device_id usb_conn_dt_match[] = {
  270. { .compatible = "gpio-usb-b-connector", },
  271. { }
  272. };
  273. MODULE_DEVICE_TABLE(of, usb_conn_dt_match);
  274. static struct platform_driver usb_conn_driver = {
  275. .probe = usb_conn_probe,
  276. .remove = usb_conn_remove,
  277. .driver = {
  278. .name = "usb-conn-gpio",
  279. .pm = &usb_conn_pm_ops,
  280. .of_match_table = usb_conn_dt_match,
  281. },
  282. };
  283. module_platform_driver(usb_conn_driver);
  284. MODULE_AUTHOR("Chunfeng Yun <[email protected]>");
  285. MODULE_DESCRIPTION("USB GPIO based connection detection driver");
  286. MODULE_LICENSE("GPL v2");