gpio_vbus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. if (!vbus_draw)
  56. return;
  57. enabled = gpio_vbus->vbus_draw_enabled;
  58. if (mA) {
  59. regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
  60. if (!enabled) {
  61. regulator_enable(vbus_draw);
  62. gpio_vbus->vbus_draw_enabled = 1;
  63. }
  64. } else {
  65. if (enabled) {
  66. regulator_disable(vbus_draw);
  67. gpio_vbus->vbus_draw_enabled = 0;
  68. }
  69. }
  70. gpio_vbus->mA = mA;
  71. }
  72. static int is_vbus_powered(struct gpio_vbus_mach_info *pdata)
  73. {
  74. int vbus;
  75. vbus = gpio_get_value(pdata->gpio_vbus);
  76. if (pdata->gpio_vbus_inverted)
  77. vbus = !vbus;
  78. return vbus;
  79. }
  80. static void gpio_vbus_work(struct work_struct *work)
  81. {
  82. struct gpio_vbus_data *gpio_vbus =
  83. container_of(work, struct gpio_vbus_data, work.work);
  84. struct gpio_vbus_mach_info *pdata = gpio_vbus->dev->platform_data;
  85. int gpio, status, vbus;
  86. if (!gpio_vbus->phy.otg->gadget)
  87. return;
  88. vbus = is_vbus_powered(pdata);
  89. if ((vbus ^ gpio_vbus->vbus) == 0)
  90. return;
  91. gpio_vbus->vbus = vbus;
  92. /* Peripheral controllers which manage the pullup themselves won't have
  93. * gpio_pullup configured here. If it's configured here, we'll do what
  94. * isp1301_omap::b_peripheral() does and enable the pullup here... although
  95. * that may complicate usb_gadget_{,dis}connect() support.
  96. */
  97. gpio = pdata->gpio_pullup;
  98. if (vbus) {
  99. status = USB_EVENT_VBUS;
  100. gpio_vbus->phy.state = OTG_STATE_B_PERIPHERAL;
  101. gpio_vbus->phy.last_event = status;
  102. usb_gadget_vbus_connect(gpio_vbus->phy.otg->gadget);
  103. /* drawing a "unit load" is *always* OK, except for OTG */
  104. set_vbus_draw(gpio_vbus, 100);
  105. /* optionally enable D+ pullup */
  106. if (gpio_is_valid(gpio))
  107. gpio_set_value(gpio, !pdata->gpio_pullup_inverted);
  108. atomic_notifier_call_chain(&gpio_vbus->phy.notifier,
  109. status, gpio_vbus->phy.otg->gadget);
  110. } else {
  111. /* optionally disable D+ pullup */
  112. if (gpio_is_valid(gpio))
  113. gpio_set_value(gpio, pdata->gpio_pullup_inverted);
  114. set_vbus_draw(gpio_vbus, 0);
  115. usb_gadget_vbus_disconnect(gpio_vbus->phy.otg->gadget);
  116. status = USB_EVENT_NONE;
  117. gpio_vbus->phy.state = OTG_STATE_B_IDLE;
  118. gpio_vbus->phy.last_event = status;
  119. atomic_notifier_call_chain(&gpio_vbus->phy.notifier,
  120. status, gpio_vbus->phy.otg->gadget);
  121. }
  122. }
  123. /* VBUS change IRQ handler */
  124. static irqreturn_t gpio_vbus_irq(int irq, void *data)
  125. {
  126. struct platform_device *pdev = data;
  127. struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
  128. struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
  129. struct usb_otg *otg = gpio_vbus->phy.otg;
  130. dev_dbg(&pdev->dev, "VBUS %s (gadget: %s)\n",
  131. is_vbus_powered(pdata) ? "supplied" : "inactive",
  132. otg->gadget ? otg->gadget->name : "none");
  133. if (otg->gadget)
  134. schedule_delayed_work(&gpio_vbus->work, msecs_to_jiffies(100));
  135. return IRQ_HANDLED;
  136. }
  137. /* OTG transceiver interface */
  138. /* bind/unbind the peripheral controller */
  139. static int gpio_vbus_set_peripheral(struct usb_otg *otg,
  140. struct usb_gadget *gadget)
  141. {
  142. struct gpio_vbus_data *gpio_vbus;
  143. struct gpio_vbus_mach_info *pdata;
  144. struct platform_device *pdev;
  145. int gpio;
  146. gpio_vbus = container_of(otg->phy, struct gpio_vbus_data, phy);
  147. pdev = to_platform_device(gpio_vbus->dev);
  148. pdata = gpio_vbus->dev->platform_data;
  149. gpio = pdata->gpio_pullup;
  150. if (!gadget) {
  151. dev_dbg(&pdev->dev, "unregistering gadget '%s'\n",
  152. otg->gadget->name);
  153. /* optionally disable D+ pullup */
  154. if (gpio_is_valid(gpio))
  155. gpio_set_value(gpio, pdata->gpio_pullup_inverted);
  156. set_vbus_draw(gpio_vbus, 0);
  157. usb_gadget_vbus_disconnect(otg->gadget);
  158. otg->phy->state = OTG_STATE_UNDEFINED;
  159. otg->gadget = NULL;
  160. return 0;
  161. }
  162. otg->gadget = gadget;
  163. dev_dbg(&pdev->dev, "registered gadget '%s'\n", gadget->name);
  164. /* initialize connection state */
  165. gpio_vbus->vbus = 0; /* start with disconnected */
  166. gpio_vbus_irq(gpio_vbus->irq, pdev);
  167. return 0;
  168. }
  169. /* effective for B devices, ignored for A-peripheral */
  170. static int gpio_vbus_set_power(struct usb_phy *phy, unsigned mA)
  171. {
  172. struct gpio_vbus_data *gpio_vbus;
  173. gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
  174. if (phy->state == OTG_STATE_B_PERIPHERAL)
  175. set_vbus_draw(gpio_vbus, mA);
  176. return 0;
  177. }
  178. /* for non-OTG B devices: set/clear transceiver suspend mode */
  179. static int gpio_vbus_set_suspend(struct usb_phy *phy, int suspend)
  180. {
  181. struct gpio_vbus_data *gpio_vbus;
  182. gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
  183. /* draw max 0 mA from vbus in suspend mode; or the previously
  184. * recorded amount of current if not suspended
  185. *
  186. * NOTE: high powered configs (mA > 100) may draw up to 2.5 mA
  187. * if they're wake-enabled ... we don't handle that yet.
  188. */
  189. return gpio_vbus_set_power(phy, suspend ? 0 : gpio_vbus->mA);
  190. }
  191. /* platform driver interface */
  192. static int __init gpio_vbus_probe(struct platform_device *pdev)
  193. {
  194. struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
  195. struct gpio_vbus_data *gpio_vbus;
  196. struct resource *res;
  197. int err, gpio, irq;
  198. if (!pdata || !gpio_is_valid(pdata->gpio_vbus))
  199. return -EINVAL;
  200. gpio = pdata->gpio_vbus;
  201. gpio_vbus = kzalloc(sizeof(struct gpio_vbus_data), GFP_KERNEL);
  202. if (!gpio_vbus)
  203. return -ENOMEM;
  204. gpio_vbus->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
  205. if (!gpio_vbus->phy.otg) {
  206. kfree(gpio_vbus);
  207. return -ENOMEM;
  208. }
  209. platform_set_drvdata(pdev, gpio_vbus);
  210. gpio_vbus->dev = &pdev->dev;
  211. gpio_vbus->phy.label = "gpio-vbus";
  212. gpio_vbus->phy.set_power = gpio_vbus_set_power;
  213. gpio_vbus->phy.set_suspend = gpio_vbus_set_suspend;
  214. gpio_vbus->phy.state = OTG_STATE_UNDEFINED;
  215. gpio_vbus->phy.otg->phy = &gpio_vbus->phy;
  216. gpio_vbus->phy.otg->set_peripheral = gpio_vbus_set_peripheral;
  217. err = gpio_request(gpio, "vbus_detect");
  218. if (err) {
  219. dev_err(&pdev->dev, "can't request vbus gpio %d, err: %d\n",
  220. gpio, err);
  221. goto err_gpio;
  222. }
  223. gpio_direction_input(gpio);
  224. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  225. if (res) {
  226. irq = res->start;
  227. res->flags &= IRQF_TRIGGER_MASK;
  228. res->flags |= IRQF_SHARED;
  229. } else
  230. irq = gpio_to_irq(gpio);
  231. gpio_vbus->irq = irq;
  232. /* if data line pullup is in use, initialize it to "not pulling up" */
  233. gpio = pdata->gpio_pullup;
  234. if (gpio_is_valid(gpio)) {
  235. err = gpio_request(gpio, "udc_pullup");
  236. if (err) {
  237. dev_err(&pdev->dev,
  238. "can't request pullup gpio %d, err: %d\n",
  239. gpio, err);
  240. gpio_free(pdata->gpio_vbus);
  241. goto err_gpio;
  242. }
  243. gpio_direction_output(gpio, pdata->gpio_pullup_inverted);
  244. }
  245. err = request_irq(irq, gpio_vbus_irq, VBUS_IRQ_FLAGS,
  246. "vbus_detect", pdev);
  247. if (err) {
  248. dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
  249. irq, err);
  250. goto err_irq;
  251. }
  252. ATOMIC_INIT_NOTIFIER_HEAD(&gpio_vbus->phy.notifier);
  253. INIT_DELAYED_WORK(&gpio_vbus->work, gpio_vbus_work);
  254. gpio_vbus->vbus_draw = regulator_get(&pdev->dev, "vbus_draw");
  255. if (IS_ERR(gpio_vbus->vbus_draw)) {
  256. dev_dbg(&pdev->dev, "can't get vbus_draw regulator, err: %ld\n",
  257. PTR_ERR(gpio_vbus->vbus_draw));
  258. gpio_vbus->vbus_draw = NULL;
  259. }
  260. /* only active when a gadget is registered */
  261. err = usb_set_transceiver(&gpio_vbus->phy);
  262. if (err) {
  263. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  264. err);
  265. goto err_otg;
  266. }
  267. return 0;
  268. err_otg:
  269. regulator_put(gpio_vbus->vbus_draw);
  270. free_irq(irq, pdev);
  271. err_irq:
  272. if (gpio_is_valid(pdata->gpio_pullup))
  273. gpio_free(pdata->gpio_pullup);
  274. gpio_free(pdata->gpio_vbus);
  275. err_gpio:
  276. platform_set_drvdata(pdev, NULL);
  277. kfree(gpio_vbus->phy.otg);
  278. kfree(gpio_vbus);
  279. return err;
  280. }
  281. static int __exit gpio_vbus_remove(struct platform_device *pdev)
  282. {
  283. struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
  284. struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
  285. int gpio = pdata->gpio_vbus;
  286. regulator_put(gpio_vbus->vbus_draw);
  287. usb_set_transceiver(NULL);
  288. free_irq(gpio_vbus->irq, pdev);
  289. if (gpio_is_valid(pdata->gpio_pullup))
  290. gpio_free(pdata->gpio_pullup);
  291. gpio_free(gpio);
  292. platform_set_drvdata(pdev, NULL);
  293. kfree(gpio_vbus->phy.otg);
  294. kfree(gpio_vbus);
  295. return 0;
  296. }
  297. /* NOTE: the gpio-vbus device may *NOT* be hotplugged */
  298. MODULE_ALIAS("platform:gpio-vbus");
  299. static struct platform_driver gpio_vbus_driver = {
  300. .driver = {
  301. .name = "gpio-vbus",
  302. .owner = THIS_MODULE,
  303. },
  304. .remove = __exit_p(gpio_vbus_remove),
  305. };
  306. static int __init gpio_vbus_init(void)
  307. {
  308. return platform_driver_probe(&gpio_vbus_driver, gpio_vbus_probe);
  309. }
  310. module_init(gpio_vbus_init);
  311. static void __exit gpio_vbus_exit(void)
  312. {
  313. platform_driver_unregister(&gpio_vbus_driver);
  314. }
  315. module_exit(gpio_vbus_exit);
  316. MODULE_DESCRIPTION("simple GPIO controlled OTG transceiver driver");
  317. MODULE_AUTHOR("Philipp Zabel");
  318. MODULE_LICENSE("GPL");