phy-gpio-vbus-usb.c 10 KB

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