gpio_vbus.c 9.9 KB

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