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