phy-gpio-vbus-usb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * gpio-vbus.c - simple GPIO VBUS sensing driver for B peripheral devices
  4. *
  5. * Copyright (c) 2008 Philipp Zabel <[email protected]>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/usb.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/usb/gadget.h>
  17. #include <linux/usb/otg.h>
  18. /*
  19. * A simple GPIO VBUS sensing driver for B peripheral only devices
  20. * with internal transceivers. It can control a D+ pullup GPIO and
  21. * a regulator to limit the current drawn from VBUS.
  22. *
  23. * Needs to be loaded before the UDC driver that will use it.
  24. */
  25. struct gpio_vbus_data {
  26. struct gpio_desc *vbus_gpiod;
  27. struct gpio_desc *pullup_gpiod;
  28. struct usb_phy phy;
  29. struct device *dev;
  30. struct regulator *vbus_draw;
  31. int vbus_draw_enabled;
  32. unsigned mA;
  33. struct delayed_work work;
  34. int vbus;
  35. int irq;
  36. };
  37. /*
  38. * This driver relies on "both edges" triggering. VBUS has 100 msec to
  39. * stabilize, so the peripheral controller driver may need to cope with
  40. * some bouncing due to current surges (e.g. charging local capacitance)
  41. * and contact chatter.
  42. *
  43. * REVISIT in desperate straits, toggling between rising and falling
  44. * edges might be workable.
  45. */
  46. #define VBUS_IRQ_FLAGS \
  47. (IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
  48. /* interface to regulator framework */
  49. static void set_vbus_draw(struct gpio_vbus_data *gpio_vbus, unsigned mA)
  50. {
  51. struct regulator *vbus_draw = gpio_vbus->vbus_draw;
  52. int enabled;
  53. int ret;
  54. if (!vbus_draw)
  55. return;
  56. enabled = gpio_vbus->vbus_draw_enabled;
  57. if (mA) {
  58. regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
  59. if (!enabled) {
  60. ret = regulator_enable(vbus_draw);
  61. if (ret < 0)
  62. return;
  63. gpio_vbus->vbus_draw_enabled = 1;
  64. }
  65. } else {
  66. if (enabled) {
  67. ret = regulator_disable(vbus_draw);
  68. if (ret < 0)
  69. return;
  70. gpio_vbus->vbus_draw_enabled = 0;
  71. }
  72. }
  73. gpio_vbus->mA = mA;
  74. }
  75. static int is_vbus_powered(struct gpio_vbus_data *gpio_vbus)
  76. {
  77. return gpiod_get_value(gpio_vbus->vbus_gpiod);
  78. }
  79. static void gpio_vbus_work(struct work_struct *work)
  80. {
  81. struct gpio_vbus_data *gpio_vbus =
  82. container_of(work, struct gpio_vbus_data, work.work);
  83. int status, vbus;
  84. if (!gpio_vbus->phy.otg->gadget)
  85. return;
  86. vbus = is_vbus_powered(gpio_vbus);
  87. if ((vbus ^ gpio_vbus->vbus) == 0)
  88. return;
  89. gpio_vbus->vbus = vbus;
  90. /* Peripheral controllers which manage the pullup themselves won't have
  91. * a pullup GPIO configured here. If it's configured here, we'll do
  92. * what isp1301_omap::b_peripheral() does and enable the pullup here...
  93. * although that may complicate usb_gadget_{,dis}connect() support.
  94. */
  95. if (vbus) {
  96. status = USB_EVENT_VBUS;
  97. gpio_vbus->phy.otg->state = OTG_STATE_B_PERIPHERAL;
  98. gpio_vbus->phy.last_event = status;
  99. usb_gadget_vbus_connect(gpio_vbus->phy.otg->gadget);
  100. /* drawing a "unit load" is *always* OK, except for OTG */
  101. set_vbus_draw(gpio_vbus, 100);
  102. /* optionally enable D+ pullup */
  103. if (gpio_vbus->pullup_gpiod)
  104. gpiod_set_value(gpio_vbus->pullup_gpiod, 1);
  105. atomic_notifier_call_chain(&gpio_vbus->phy.notifier,
  106. status, gpio_vbus->phy.otg->gadget);
  107. usb_phy_set_event(&gpio_vbus->phy, USB_EVENT_ENUMERATED);
  108. } else {
  109. /* optionally disable D+ pullup */
  110. if (gpio_vbus->pullup_gpiod)
  111. gpiod_set_value(gpio_vbus->pullup_gpiod, 0);
  112. set_vbus_draw(gpio_vbus, 0);
  113. usb_gadget_vbus_disconnect(gpio_vbus->phy.otg->gadget);
  114. status = USB_EVENT_NONE;
  115. gpio_vbus->phy.otg->state = OTG_STATE_B_IDLE;
  116. gpio_vbus->phy.last_event = status;
  117. atomic_notifier_call_chain(&gpio_vbus->phy.notifier,
  118. status, gpio_vbus->phy.otg->gadget);
  119. usb_phy_set_event(&gpio_vbus->phy, USB_EVENT_NONE);
  120. }
  121. }
  122. /* VBUS change IRQ handler */
  123. static irqreturn_t gpio_vbus_irq(int irq, void *data)
  124. {
  125. struct platform_device *pdev = data;
  126. struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
  127. struct usb_otg *otg = gpio_vbus->phy.otg;
  128. dev_dbg(&pdev->dev, "VBUS %s (gadget: %s)\n",
  129. is_vbus_powered(gpio_vbus) ? "supplied" : "inactive",
  130. otg->gadget ? otg->gadget->name : "none");
  131. if (otg->gadget)
  132. schedule_delayed_work(&gpio_vbus->work, msecs_to_jiffies(100));
  133. return IRQ_HANDLED;
  134. }
  135. /* OTG transceiver interface */
  136. /* bind/unbind the peripheral controller */
  137. static int gpio_vbus_set_peripheral(struct usb_otg *otg,
  138. struct usb_gadget *gadget)
  139. {
  140. struct gpio_vbus_data *gpio_vbus;
  141. struct platform_device *pdev;
  142. gpio_vbus = container_of(otg->usb_phy, struct gpio_vbus_data, phy);
  143. pdev = to_platform_device(gpio_vbus->dev);
  144. if (!gadget) {
  145. dev_dbg(&pdev->dev, "unregistering gadget '%s'\n",
  146. otg->gadget->name);
  147. /* optionally disable D+ pullup */
  148. if (gpio_vbus->pullup_gpiod)
  149. gpiod_set_value(gpio_vbus->pullup_gpiod, 0);
  150. set_vbus_draw(gpio_vbus, 0);
  151. usb_gadget_vbus_disconnect(otg->gadget);
  152. otg->state = OTG_STATE_UNDEFINED;
  153. otg->gadget = NULL;
  154. return 0;
  155. }
  156. otg->gadget = gadget;
  157. dev_dbg(&pdev->dev, "registered gadget '%s'\n", gadget->name);
  158. /* initialize connection state */
  159. gpio_vbus->vbus = 0; /* start with disconnected */
  160. gpio_vbus_irq(gpio_vbus->irq, pdev);
  161. return 0;
  162. }
  163. /* effective for B devices, ignored for A-peripheral */
  164. static int gpio_vbus_set_power(struct usb_phy *phy, unsigned mA)
  165. {
  166. struct gpio_vbus_data *gpio_vbus;
  167. gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
  168. if (phy->otg->state == OTG_STATE_B_PERIPHERAL)
  169. set_vbus_draw(gpio_vbus, mA);
  170. return 0;
  171. }
  172. /* for non-OTG B devices: set/clear transceiver suspend mode */
  173. static int gpio_vbus_set_suspend(struct usb_phy *phy, int suspend)
  174. {
  175. struct gpio_vbus_data *gpio_vbus;
  176. gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
  177. /* draw max 0 mA from vbus in suspend mode; or the previously
  178. * recorded amount of current if not suspended
  179. *
  180. * NOTE: high powered configs (mA > 100) may draw up to 2.5 mA
  181. * if they're wake-enabled ... we don't handle that yet.
  182. */
  183. return gpio_vbus_set_power(phy, suspend ? 0 : gpio_vbus->mA);
  184. }
  185. /* platform driver interface */
  186. static int gpio_vbus_probe(struct platform_device *pdev)
  187. {
  188. struct gpio_vbus_data *gpio_vbus;
  189. struct resource *res;
  190. struct device *dev = &pdev->dev;
  191. int err, irq;
  192. unsigned long irqflags;
  193. gpio_vbus = devm_kzalloc(&pdev->dev, sizeof(struct gpio_vbus_data),
  194. GFP_KERNEL);
  195. if (!gpio_vbus)
  196. return -ENOMEM;
  197. gpio_vbus->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
  198. GFP_KERNEL);
  199. if (!gpio_vbus->phy.otg)
  200. return -ENOMEM;
  201. platform_set_drvdata(pdev, gpio_vbus);
  202. gpio_vbus->dev = &pdev->dev;
  203. gpio_vbus->phy.label = "gpio-vbus";
  204. gpio_vbus->phy.dev = gpio_vbus->dev;
  205. gpio_vbus->phy.set_power = gpio_vbus_set_power;
  206. gpio_vbus->phy.set_suspend = gpio_vbus_set_suspend;
  207. gpio_vbus->phy.otg->state = OTG_STATE_UNDEFINED;
  208. gpio_vbus->phy.otg->usb_phy = &gpio_vbus->phy;
  209. gpio_vbus->phy.otg->set_peripheral = gpio_vbus_set_peripheral;
  210. /* Look up the VBUS sensing GPIO */
  211. gpio_vbus->vbus_gpiod = devm_gpiod_get(dev, "vbus", GPIOD_IN);
  212. if (IS_ERR(gpio_vbus->vbus_gpiod)) {
  213. err = PTR_ERR(gpio_vbus->vbus_gpiod);
  214. dev_err(&pdev->dev, "can't request vbus gpio, err: %d\n", err);
  215. return err;
  216. }
  217. gpiod_set_consumer_name(gpio_vbus->vbus_gpiod, "vbus_detect");
  218. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  219. if (res) {
  220. irq = res->start;
  221. irqflags = (res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
  222. } else {
  223. irq = gpiod_to_irq(gpio_vbus->vbus_gpiod);
  224. irqflags = VBUS_IRQ_FLAGS;
  225. }
  226. gpio_vbus->irq = irq;
  227. /*
  228. * The VBUS sensing GPIO should have a pulldown, which will normally be
  229. * part of a resistor ladder turning a 4.0V-5.25V level on VBUS into a
  230. * value the GPIO detects as active. Some systems will use comparators.
  231. * Get the optional D+ or D- pullup GPIO. If the data line pullup is
  232. * in use, initialize it to "not pulling up"
  233. */
  234. gpio_vbus->pullup_gpiod = devm_gpiod_get_optional(dev, "pullup",
  235. GPIOD_OUT_LOW);
  236. if (IS_ERR(gpio_vbus->pullup_gpiod)) {
  237. err = PTR_ERR(gpio_vbus->pullup_gpiod);
  238. dev_err(&pdev->dev, "can't request pullup gpio, err: %d\n",
  239. err);
  240. return err;
  241. }
  242. if (gpio_vbus->pullup_gpiod)
  243. gpiod_set_consumer_name(gpio_vbus->pullup_gpiod, "udc_pullup");
  244. err = devm_request_irq(&pdev->dev, irq, gpio_vbus_irq, irqflags,
  245. "vbus_detect", pdev);
  246. if (err) {
  247. dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
  248. irq, err);
  249. return err;
  250. }
  251. INIT_DELAYED_WORK(&gpio_vbus->work, gpio_vbus_work);
  252. gpio_vbus->vbus_draw = devm_regulator_get(&pdev->dev, "vbus_draw");
  253. if (IS_ERR(gpio_vbus->vbus_draw)) {
  254. dev_dbg(&pdev->dev, "can't get vbus_draw regulator, err: %ld\n",
  255. PTR_ERR(gpio_vbus->vbus_draw));
  256. gpio_vbus->vbus_draw = NULL;
  257. }
  258. /* only active when a gadget is registered */
  259. err = usb_add_phy(&gpio_vbus->phy, USB_PHY_TYPE_USB2);
  260. if (err) {
  261. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  262. err);
  263. return err;
  264. }
  265. /* TODO: wakeup could be enabled here with device_init_wakeup(dev, 1) */
  266. return 0;
  267. }
  268. static int gpio_vbus_remove(struct platform_device *pdev)
  269. {
  270. struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
  271. device_init_wakeup(&pdev->dev, 0);
  272. cancel_delayed_work_sync(&gpio_vbus->work);
  273. usb_remove_phy(&gpio_vbus->phy);
  274. return 0;
  275. }
  276. #ifdef CONFIG_PM
  277. static int gpio_vbus_pm_suspend(struct device *dev)
  278. {
  279. struct gpio_vbus_data *gpio_vbus = dev_get_drvdata(dev);
  280. if (device_may_wakeup(dev))
  281. enable_irq_wake(gpio_vbus->irq);
  282. return 0;
  283. }
  284. static int gpio_vbus_pm_resume(struct device *dev)
  285. {
  286. struct gpio_vbus_data *gpio_vbus = dev_get_drvdata(dev);
  287. if (device_may_wakeup(dev))
  288. disable_irq_wake(gpio_vbus->irq);
  289. return 0;
  290. }
  291. static const struct dev_pm_ops gpio_vbus_dev_pm_ops = {
  292. .suspend = gpio_vbus_pm_suspend,
  293. .resume = gpio_vbus_pm_resume,
  294. };
  295. #endif
  296. MODULE_ALIAS("platform:gpio-vbus");
  297. static struct platform_driver gpio_vbus_driver = {
  298. .driver = {
  299. .name = "gpio-vbus",
  300. #ifdef CONFIG_PM
  301. .pm = &gpio_vbus_dev_pm_ops,
  302. #endif
  303. },
  304. .probe = gpio_vbus_probe,
  305. .remove = gpio_vbus_remove,
  306. };
  307. module_platform_driver(gpio_vbus_driver);
  308. MODULE_DESCRIPTION("simple GPIO controlled OTG transceiver driver");
  309. MODULE_AUTHOR("Philipp Zabel");
  310. MODULE_LICENSE("GPL");