phy-gpio-vbus-usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 = gpio_vbus->dev->platform_data;
  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.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. } else {
  116. /* optionally disable D+ pullup */
  117. if (gpio_is_valid(gpio))
  118. gpio_set_value(gpio, pdata->gpio_pullup_inverted);
  119. set_vbus_draw(gpio_vbus, 0);
  120. usb_gadget_vbus_disconnect(gpio_vbus->phy.otg->gadget);
  121. status = USB_EVENT_NONE;
  122. gpio_vbus->phy.state = OTG_STATE_B_IDLE;
  123. gpio_vbus->phy.last_event = status;
  124. atomic_notifier_call_chain(&gpio_vbus->phy.notifier,
  125. status, gpio_vbus->phy.otg->gadget);
  126. }
  127. }
  128. /* VBUS change IRQ handler */
  129. static irqreturn_t gpio_vbus_irq(int irq, void *data)
  130. {
  131. struct platform_device *pdev = data;
  132. struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
  133. struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
  134. struct usb_otg *otg = gpio_vbus->phy.otg;
  135. dev_dbg(&pdev->dev, "VBUS %s (gadget: %s)\n",
  136. is_vbus_powered(pdata) ? "supplied" : "inactive",
  137. otg->gadget ? otg->gadget->name : "none");
  138. if (otg->gadget)
  139. schedule_delayed_work(&gpio_vbus->work, msecs_to_jiffies(100));
  140. return IRQ_HANDLED;
  141. }
  142. /* OTG transceiver interface */
  143. /* bind/unbind the peripheral controller */
  144. static int gpio_vbus_set_peripheral(struct usb_otg *otg,
  145. struct usb_gadget *gadget)
  146. {
  147. struct gpio_vbus_data *gpio_vbus;
  148. struct gpio_vbus_mach_info *pdata;
  149. struct platform_device *pdev;
  150. int gpio;
  151. gpio_vbus = container_of(otg->phy, struct gpio_vbus_data, phy);
  152. pdev = to_platform_device(gpio_vbus->dev);
  153. pdata = gpio_vbus->dev->platform_data;
  154. gpio = pdata->gpio_pullup;
  155. if (!gadget) {
  156. dev_dbg(&pdev->dev, "unregistering gadget '%s'\n",
  157. otg->gadget->name);
  158. /* optionally disable D+ pullup */
  159. if (gpio_is_valid(gpio))
  160. gpio_set_value(gpio, pdata->gpio_pullup_inverted);
  161. set_vbus_draw(gpio_vbus, 0);
  162. usb_gadget_vbus_disconnect(otg->gadget);
  163. otg->phy->state = OTG_STATE_UNDEFINED;
  164. otg->gadget = NULL;
  165. return 0;
  166. }
  167. otg->gadget = gadget;
  168. dev_dbg(&pdev->dev, "registered gadget '%s'\n", gadget->name);
  169. /* initialize connection state */
  170. gpio_vbus->vbus = 0; /* start with disconnected */
  171. gpio_vbus_irq(gpio_vbus->irq, pdev);
  172. return 0;
  173. }
  174. /* effective for B devices, ignored for A-peripheral */
  175. static int gpio_vbus_set_power(struct usb_phy *phy, unsigned mA)
  176. {
  177. struct gpio_vbus_data *gpio_vbus;
  178. gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
  179. if (phy->state == OTG_STATE_B_PERIPHERAL)
  180. set_vbus_draw(gpio_vbus, mA);
  181. return 0;
  182. }
  183. /* for non-OTG B devices: set/clear transceiver suspend mode */
  184. static int gpio_vbus_set_suspend(struct usb_phy *phy, int suspend)
  185. {
  186. struct gpio_vbus_data *gpio_vbus;
  187. gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
  188. /* draw max 0 mA from vbus in suspend mode; or the previously
  189. * recorded amount of current if not suspended
  190. *
  191. * NOTE: high powered configs (mA > 100) may draw up to 2.5 mA
  192. * if they're wake-enabled ... we don't handle that yet.
  193. */
  194. return gpio_vbus_set_power(phy, suspend ? 0 : gpio_vbus->mA);
  195. }
  196. /* platform driver interface */
  197. static int __init gpio_vbus_probe(struct platform_device *pdev)
  198. {
  199. struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
  200. struct gpio_vbus_data *gpio_vbus;
  201. struct resource *res;
  202. int err, gpio, irq;
  203. unsigned long irqflags;
  204. if (!pdata || !gpio_is_valid(pdata->gpio_vbus))
  205. return -EINVAL;
  206. gpio = pdata->gpio_vbus;
  207. gpio_vbus = kzalloc(sizeof(struct gpio_vbus_data), GFP_KERNEL);
  208. if (!gpio_vbus)
  209. return -ENOMEM;
  210. gpio_vbus->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
  211. if (!gpio_vbus->phy.otg) {
  212. kfree(gpio_vbus);
  213. return -ENOMEM;
  214. }
  215. platform_set_drvdata(pdev, gpio_vbus);
  216. gpio_vbus->dev = &pdev->dev;
  217. gpio_vbus->phy.label = "gpio-vbus";
  218. gpio_vbus->phy.set_power = gpio_vbus_set_power;
  219. gpio_vbus->phy.set_suspend = gpio_vbus_set_suspend;
  220. gpio_vbus->phy.state = OTG_STATE_UNDEFINED;
  221. gpio_vbus->phy.otg->phy = &gpio_vbus->phy;
  222. gpio_vbus->phy.otg->set_peripheral = gpio_vbus_set_peripheral;
  223. err = gpio_request(gpio, "vbus_detect");
  224. if (err) {
  225. dev_err(&pdev->dev, "can't request vbus gpio %d, err: %d\n",
  226. gpio, err);
  227. goto err_gpio;
  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 = gpio_request(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. gpio_free(pdata->gpio_vbus);
  248. goto err_gpio;
  249. }
  250. gpio_direction_output(gpio, pdata->gpio_pullup_inverted);
  251. }
  252. err = request_irq(irq, gpio_vbus_irq, irqflags, "vbus_detect", pdev);
  253. if (err) {
  254. dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
  255. irq, err);
  256. goto err_irq;
  257. }
  258. ATOMIC_INIT_NOTIFIER_HEAD(&gpio_vbus->phy.notifier);
  259. INIT_DELAYED_WORK(&gpio_vbus->work, gpio_vbus_work);
  260. gpio_vbus->vbus_draw = regulator_get(&pdev->dev, "vbus_draw");
  261. if (IS_ERR(gpio_vbus->vbus_draw)) {
  262. dev_dbg(&pdev->dev, "can't get vbus_draw regulator, err: %ld\n",
  263. PTR_ERR(gpio_vbus->vbus_draw));
  264. gpio_vbus->vbus_draw = NULL;
  265. }
  266. /* only active when a gadget is registered */
  267. err = usb_add_phy(&gpio_vbus->phy, USB_PHY_TYPE_USB2);
  268. if (err) {
  269. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  270. err);
  271. goto err_otg;
  272. }
  273. device_init_wakeup(&pdev->dev, pdata->wakeup);
  274. return 0;
  275. err_otg:
  276. regulator_put(gpio_vbus->vbus_draw);
  277. free_irq(irq, pdev);
  278. err_irq:
  279. if (gpio_is_valid(pdata->gpio_pullup))
  280. gpio_free(pdata->gpio_pullup);
  281. gpio_free(pdata->gpio_vbus);
  282. err_gpio:
  283. platform_set_drvdata(pdev, NULL);
  284. kfree(gpio_vbus->phy.otg);
  285. kfree(gpio_vbus);
  286. return err;
  287. }
  288. static int __exit gpio_vbus_remove(struct platform_device *pdev)
  289. {
  290. struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
  291. struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
  292. int gpio = pdata->gpio_vbus;
  293. device_init_wakeup(&pdev->dev, 0);
  294. cancel_delayed_work_sync(&gpio_vbus->work);
  295. regulator_put(gpio_vbus->vbus_draw);
  296. usb_remove_phy(&gpio_vbus->phy);
  297. free_irq(gpio_vbus->irq, pdev);
  298. if (gpio_is_valid(pdata->gpio_pullup))
  299. gpio_free(pdata->gpio_pullup);
  300. gpio_free(gpio);
  301. platform_set_drvdata(pdev, NULL);
  302. kfree(gpio_vbus->phy.otg);
  303. kfree(gpio_vbus);
  304. return 0;
  305. }
  306. #ifdef CONFIG_PM
  307. static int gpio_vbus_pm_suspend(struct device *dev)
  308. {
  309. struct gpio_vbus_data *gpio_vbus = dev_get_drvdata(dev);
  310. if (device_may_wakeup(dev))
  311. enable_irq_wake(gpio_vbus->irq);
  312. return 0;
  313. }
  314. static int gpio_vbus_pm_resume(struct device *dev)
  315. {
  316. struct gpio_vbus_data *gpio_vbus = dev_get_drvdata(dev);
  317. if (device_may_wakeup(dev))
  318. disable_irq_wake(gpio_vbus->irq);
  319. return 0;
  320. }
  321. static const struct dev_pm_ops gpio_vbus_dev_pm_ops = {
  322. .suspend = gpio_vbus_pm_suspend,
  323. .resume = gpio_vbus_pm_resume,
  324. };
  325. #endif
  326. /* NOTE: the gpio-vbus device may *NOT* be hotplugged */
  327. MODULE_ALIAS("platform:gpio-vbus");
  328. static struct platform_driver gpio_vbus_driver = {
  329. .driver = {
  330. .name = "gpio-vbus",
  331. .owner = THIS_MODULE,
  332. #ifdef CONFIG_PM
  333. .pm = &gpio_vbus_dev_pm_ops,
  334. #endif
  335. },
  336. .remove = __exit_p(gpio_vbus_remove),
  337. };
  338. module_platform_driver_probe(gpio_vbus_driver, gpio_vbus_probe);
  339. MODULE_DESCRIPTION("simple GPIO controlled OTG transceiver driver");
  340. MODULE_AUTHOR("Philipp Zabel");
  341. MODULE_LICENSE("GPL");