usb-conn-gpio.c 9.1 KB

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