gpio_vbus.c 10 KB

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