gpio_vbus.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 otg_transceiver otg;
  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->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->otg.state = OTG_STATE_B_PERIPHERAL;
  95. usb_gadget_vbus_connect(gpio_vbus->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->otg.gadget);
  107. gpio_vbus->otg.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. dev_dbg(&pdev->dev, "VBUS %s (gadget: %s)\n",
  117. is_vbus_powered(pdata) ? "supplied" : "inactive",
  118. gpio_vbus->otg.gadget ? gpio_vbus->otg.gadget->name : "none");
  119. if (gpio_vbus->otg.gadget)
  120. schedule_work(&gpio_vbus->work);
  121. return IRQ_HANDLED;
  122. }
  123. /* OTG transceiver interface */
  124. /* bind/unbind the peripheral controller */
  125. static int gpio_vbus_set_peripheral(struct otg_transceiver *otg,
  126. struct usb_gadget *gadget)
  127. {
  128. struct gpio_vbus_data *gpio_vbus;
  129. struct gpio_vbus_mach_info *pdata;
  130. struct platform_device *pdev;
  131. int gpio, irq;
  132. gpio_vbus = container_of(otg, struct gpio_vbus_data, otg);
  133. pdev = to_platform_device(gpio_vbus->dev);
  134. pdata = gpio_vbus->dev->platform_data;
  135. irq = gpio_to_irq(pdata->gpio_vbus);
  136. gpio = pdata->gpio_pullup;
  137. if (!gadget) {
  138. dev_dbg(&pdev->dev, "unregistering gadget '%s'\n",
  139. otg->gadget->name);
  140. /* optionally disable D+ pullup */
  141. if (gpio_is_valid(gpio))
  142. gpio_set_value(gpio, pdata->gpio_pullup_inverted);
  143. set_vbus_draw(gpio_vbus, 0);
  144. usb_gadget_vbus_disconnect(otg->gadget);
  145. otg->state = OTG_STATE_UNDEFINED;
  146. otg->gadget = NULL;
  147. return 0;
  148. }
  149. otg->gadget = gadget;
  150. dev_dbg(&pdev->dev, "registered gadget '%s'\n", gadget->name);
  151. /* initialize connection state */
  152. gpio_vbus_irq(irq, pdev);
  153. return 0;
  154. }
  155. /* effective for B devices, ignored for A-peripheral */
  156. static int gpio_vbus_set_power(struct otg_transceiver *otg, unsigned mA)
  157. {
  158. struct gpio_vbus_data *gpio_vbus;
  159. gpio_vbus = container_of(otg, struct gpio_vbus_data, otg);
  160. if (otg->state == OTG_STATE_B_PERIPHERAL)
  161. set_vbus_draw(gpio_vbus, mA);
  162. return 0;
  163. }
  164. /* for non-OTG B devices: set/clear transceiver suspend mode */
  165. static int gpio_vbus_set_suspend(struct otg_transceiver *otg, int suspend)
  166. {
  167. struct gpio_vbus_data *gpio_vbus;
  168. gpio_vbus = container_of(otg, struct gpio_vbus_data, otg);
  169. /* draw max 0 mA from vbus in suspend mode; or the previously
  170. * recorded amount of current if not suspended
  171. *
  172. * NOTE: high powered configs (mA > 100) may draw up to 2.5 mA
  173. * if they're wake-enabled ... we don't handle that yet.
  174. */
  175. return gpio_vbus_set_power(otg, suspend ? 0 : gpio_vbus->mA);
  176. }
  177. /* platform driver interface */
  178. static int __init gpio_vbus_probe(struct platform_device *pdev)
  179. {
  180. struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
  181. struct gpio_vbus_data *gpio_vbus;
  182. struct resource *res;
  183. int err, gpio, irq;
  184. if (!pdata || !gpio_is_valid(pdata->gpio_vbus))
  185. return -EINVAL;
  186. gpio = pdata->gpio_vbus;
  187. gpio_vbus = kzalloc(sizeof(struct gpio_vbus_data), GFP_KERNEL);
  188. if (!gpio_vbus)
  189. return -ENOMEM;
  190. platform_set_drvdata(pdev, gpio_vbus);
  191. gpio_vbus->dev = &pdev->dev;
  192. gpio_vbus->otg.label = "gpio-vbus";
  193. gpio_vbus->otg.state = OTG_STATE_UNDEFINED;
  194. gpio_vbus->otg.set_peripheral = gpio_vbus_set_peripheral;
  195. gpio_vbus->otg.set_power = gpio_vbus_set_power;
  196. gpio_vbus->otg.set_suspend = gpio_vbus_set_suspend;
  197. err = gpio_request(gpio, "vbus_detect");
  198. if (err) {
  199. dev_err(&pdev->dev, "can't request vbus gpio %d, err: %d\n",
  200. gpio, err);
  201. goto err_gpio;
  202. }
  203. gpio_direction_input(gpio);
  204. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  205. if (res) {
  206. irq = res->start;
  207. res->flags &= IRQF_TRIGGER_MASK;
  208. res->flags |= IRQF_SAMPLE_RANDOM | IRQF_SHARED;
  209. } else
  210. irq = gpio_to_irq(gpio);
  211. /* if data line pullup is in use, initialize it to "not pulling up" */
  212. gpio = pdata->gpio_pullup;
  213. if (gpio_is_valid(gpio)) {
  214. err = gpio_request(gpio, "udc_pullup");
  215. if (err) {
  216. dev_err(&pdev->dev,
  217. "can't request pullup gpio %d, err: %d\n",
  218. gpio, err);
  219. gpio_free(pdata->gpio_vbus);
  220. goto err_gpio;
  221. }
  222. gpio_direction_output(gpio, pdata->gpio_pullup_inverted);
  223. }
  224. err = request_irq(irq, gpio_vbus_irq, VBUS_IRQ_FLAGS,
  225. "vbus_detect", pdev);
  226. if (err) {
  227. dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
  228. irq, err);
  229. goto err_irq;
  230. }
  231. INIT_WORK(&gpio_vbus->work, gpio_vbus_work);
  232. gpio_vbus->vbus_draw = regulator_get(&pdev->dev, "vbus_draw");
  233. if (IS_ERR(gpio_vbus->vbus_draw)) {
  234. dev_dbg(&pdev->dev, "can't get vbus_draw regulator, err: %ld\n",
  235. PTR_ERR(gpio_vbus->vbus_draw));
  236. gpio_vbus->vbus_draw = NULL;
  237. }
  238. /* only active when a gadget is registered */
  239. err = otg_set_transceiver(&gpio_vbus->otg);
  240. if (err) {
  241. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  242. err);
  243. goto err_otg;
  244. }
  245. return 0;
  246. err_otg:
  247. free_irq(irq, &pdev->dev);
  248. err_irq:
  249. if (gpio_is_valid(pdata->gpio_pullup))
  250. gpio_free(pdata->gpio_pullup);
  251. gpio_free(pdata->gpio_vbus);
  252. err_gpio:
  253. platform_set_drvdata(pdev, NULL);
  254. kfree(gpio_vbus);
  255. return err;
  256. }
  257. static int __exit gpio_vbus_remove(struct platform_device *pdev)
  258. {
  259. struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
  260. struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
  261. int gpio = pdata->gpio_vbus;
  262. regulator_put(gpio_vbus->vbus_draw);
  263. otg_set_transceiver(NULL);
  264. free_irq(gpio_to_irq(gpio), &pdev->dev);
  265. if (gpio_is_valid(pdata->gpio_pullup))
  266. gpio_free(pdata->gpio_pullup);
  267. gpio_free(gpio);
  268. platform_set_drvdata(pdev, NULL);
  269. kfree(gpio_vbus);
  270. return 0;
  271. }
  272. /* NOTE: the gpio-vbus device may *NOT* be hotplugged */
  273. MODULE_ALIAS("platform:gpio-vbus");
  274. static struct platform_driver gpio_vbus_driver = {
  275. .driver = {
  276. .name = "gpio-vbus",
  277. .owner = THIS_MODULE,
  278. },
  279. .remove = __exit_p(gpio_vbus_remove),
  280. };
  281. static int __init gpio_vbus_init(void)
  282. {
  283. return platform_driver_probe(&gpio_vbus_driver, gpio_vbus_probe);
  284. }
  285. module_init(gpio_vbus_init);
  286. static void __exit gpio_vbus_exit(void)
  287. {
  288. platform_driver_unregister(&gpio_vbus_driver);
  289. }
  290. module_exit(gpio_vbus_exit);
  291. MODULE_DESCRIPTION("simple GPIO controlled OTG transceiver driver");
  292. MODULE_AUTHOR("Philipp Zabel");
  293. MODULE_LICENSE("GPL");