phy-gpio-vbus-usb.c 10 KB

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