gpio_vbus.c 8.7 KB

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