gpio_vbus.c 9.2 KB

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