gpio_vbus.c 9.5 KB

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