2 * gpio-vbus.c - simple GPIO VBUS sensing driver for B peripheral devices
4 * Copyright (c) 2008 Philipp Zabel <philipp.zabel@gmail.com>
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.
11 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/gpio.h>
14 #include <linux/interrupt.h>
15 #include <linux/usb.h>
16 #include <linux/workqueue.h>
18 #include <linux/regulator/consumer.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/gpio_vbus.h>
22 #include <linux/usb/otg.h>
26 * A simple GPIO VBUS sensing driver for B peripheral only devices
27 * with internal transceivers. It can control a D+ pullup GPIO and
28 * a regulator to limit the current drawn from VBUS.
30 * Needs to be loaded before the UDC driver that will use it.
32 struct gpio_vbus_data {
33 struct otg_transceiver otg;
35 struct regulator *vbus_draw;
36 int vbus_draw_enabled;
38 struct work_struct work;
43 * This driver relies on "both edges" triggering. VBUS has 100 msec to
44 * stabilize, so the peripheral controller driver may need to cope with
45 * some bouncing due to current surges (e.g. charging local capacitance)
46 * and contact chatter.
48 * REVISIT in desperate straits, toggling between rising and falling
49 * edges might be workable.
51 #define VBUS_IRQ_FLAGS \
52 ( IRQF_SAMPLE_RANDOM | IRQF_SHARED \
53 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING )
56 /* interface to regulator framework */
57 static void set_vbus_draw(struct gpio_vbus_data *gpio_vbus, unsigned mA)
59 struct regulator *vbus_draw = gpio_vbus->vbus_draw;
65 enabled = gpio_vbus->vbus_draw_enabled;
67 regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
69 regulator_enable(vbus_draw);
70 gpio_vbus->vbus_draw_enabled = 1;
74 regulator_disable(vbus_draw);
75 gpio_vbus->vbus_draw_enabled = 0;
81 static int is_vbus_powered(struct gpio_vbus_mach_info *pdata)
85 vbus = gpio_get_value(pdata->gpio_vbus);
86 if (pdata->gpio_vbus_inverted)
92 static void gpio_vbus_work(struct work_struct *work)
94 struct gpio_vbus_data *gpio_vbus =
95 container_of(work, struct gpio_vbus_data, work);
96 struct gpio_vbus_mach_info *pdata = gpio_vbus->dev->platform_data;
99 if (!gpio_vbus->otg.gadget)
102 /* Peripheral controllers which manage the pullup themselves won't have
103 * gpio_pullup configured here. If it's configured here, we'll do what
104 * isp1301_omap::b_peripheral() does and enable the pullup here... although
105 * that may complicate usb_gadget_{,dis}connect() support.
107 gpio = pdata->gpio_pullup;
108 if (is_vbus_powered(pdata)) {
109 gpio_vbus->otg.state = OTG_STATE_B_PERIPHERAL;
110 usb_gadget_vbus_connect(gpio_vbus->otg.gadget);
112 /* drawing a "unit load" is *always* OK, except for OTG */
113 set_vbus_draw(gpio_vbus, 100);
115 /* optionally enable D+ pullup */
116 if (gpio_is_valid(gpio))
117 gpio_set_value(gpio, !pdata->gpio_pullup_inverted);
119 /* optionally disable D+ pullup */
120 if (gpio_is_valid(gpio))
121 gpio_set_value(gpio, pdata->gpio_pullup_inverted);
123 set_vbus_draw(gpio_vbus, 0);
125 usb_gadget_vbus_disconnect(gpio_vbus->otg.gadget);
126 gpio_vbus->otg.state = OTG_STATE_B_IDLE;
130 /* VBUS change IRQ handler */
131 static irqreturn_t gpio_vbus_irq(int irq, void *data)
133 struct platform_device *pdev = data;
134 struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
135 struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
137 dev_dbg(&pdev->dev, "VBUS %s (gadget: %s)\n",
138 is_vbus_powered(pdata) ? "supplied" : "inactive",
139 gpio_vbus->otg.gadget ? gpio_vbus->otg.gadget->name : "none");
141 if (gpio_vbus->otg.gadget)
142 schedule_work(&gpio_vbus->work);
147 /* OTG transceiver interface */
149 /* bind/unbind the peripheral controller */
150 static int gpio_vbus_set_peripheral(struct otg_transceiver *otg,
151 struct usb_gadget *gadget)
153 struct gpio_vbus_data *gpio_vbus;
154 struct gpio_vbus_mach_info *pdata;
155 struct platform_device *pdev;
158 gpio_vbus = container_of(otg, struct gpio_vbus_data, otg);
159 pdev = to_platform_device(gpio_vbus->dev);
160 pdata = gpio_vbus->dev->platform_data;
161 irq = gpio_to_irq(pdata->gpio_vbus);
162 gpio = pdata->gpio_pullup;
165 dev_dbg(&pdev->dev, "unregistering gadget '%s'\n",
168 /* optionally disable D+ pullup */
169 if (gpio_is_valid(gpio))
170 gpio_set_value(gpio, pdata->gpio_pullup_inverted);
172 set_vbus_draw(gpio_vbus, 0);
174 usb_gadget_vbus_disconnect(otg->gadget);
175 otg->state = OTG_STATE_UNDEFINED;
181 otg->gadget = gadget;
182 dev_dbg(&pdev->dev, "registered gadget '%s'\n", gadget->name);
184 /* initialize connection state */
185 gpio_vbus_irq(irq, pdev);
189 /* effective for B devices, ignored for A-peripheral */
190 static int gpio_vbus_set_power(struct otg_transceiver *otg, unsigned mA)
192 struct gpio_vbus_data *gpio_vbus;
194 gpio_vbus = container_of(otg, struct gpio_vbus_data, otg);
196 if (otg->state == OTG_STATE_B_PERIPHERAL)
197 set_vbus_draw(gpio_vbus, mA);
201 /* for non-OTG B devices: set/clear transceiver suspend mode */
202 static int gpio_vbus_set_suspend(struct otg_transceiver *otg, int suspend)
204 struct gpio_vbus_data *gpio_vbus;
206 gpio_vbus = container_of(otg, struct gpio_vbus_data, otg);
208 /* draw max 0 mA from vbus in suspend mode; or the previously
209 * recorded amount of current if not suspended
211 * NOTE: high powered configs (mA > 100) may draw up to 2.5 mA
212 * if they're wake-enabled ... we don't handle that yet.
214 return gpio_vbus_set_power(otg, suspend ? 0 : gpio_vbus->mA);
217 /* platform driver interface */
219 static int __init gpio_vbus_probe(struct platform_device *pdev)
221 struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
222 struct gpio_vbus_data *gpio_vbus;
223 struct resource *res;
226 if (!pdata || !gpio_is_valid(pdata->gpio_vbus))
228 gpio = pdata->gpio_vbus;
230 gpio_vbus = kzalloc(sizeof(struct gpio_vbus_data), GFP_KERNEL);
234 platform_set_drvdata(pdev, gpio_vbus);
235 gpio_vbus->dev = &pdev->dev;
236 gpio_vbus->otg.label = "gpio-vbus";
237 gpio_vbus->otg.state = OTG_STATE_UNDEFINED;
238 gpio_vbus->otg.set_peripheral = gpio_vbus_set_peripheral;
239 gpio_vbus->otg.set_power = gpio_vbus_set_power;
240 gpio_vbus->otg.set_suspend = gpio_vbus_set_suspend;
242 err = gpio_request(gpio, "vbus_detect");
244 dev_err(&pdev->dev, "can't request vbus gpio %d, err: %d\n",
248 gpio_direction_input(gpio);
250 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
253 res->flags &= IRQF_TRIGGER_MASK;
254 res->flags |= IRQF_SAMPLE_RANDOM | IRQF_SHARED;
256 irq = gpio_to_irq(gpio);
258 /* if data line pullup is in use, initialize it to "not pulling up" */
259 gpio = pdata->gpio_pullup;
260 if (gpio_is_valid(gpio)) {
261 err = gpio_request(gpio, "udc_pullup");
264 "can't request pullup gpio %d, err: %d\n",
266 gpio_free(pdata->gpio_vbus);
269 gpio_direction_output(gpio, pdata->gpio_pullup_inverted);
272 err = request_irq(irq, gpio_vbus_irq, VBUS_IRQ_FLAGS,
273 "vbus_detect", pdev);
275 dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
279 INIT_WORK(&gpio_vbus->work, gpio_vbus_work);
281 /* only active when a gadget is registered */
282 err = otg_set_transceiver(&gpio_vbus->otg);
284 dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
289 gpio_vbus->vbus_draw = regulator_get(&pdev->dev, "vbus_draw");
290 if (IS_ERR(gpio_vbus->vbus_draw)) {
291 dev_dbg(&pdev->dev, "can't get vbus_draw regulator, err: %ld\n",
292 PTR_ERR(gpio_vbus->vbus_draw));
293 gpio_vbus->vbus_draw = NULL;
298 free_irq(irq, &pdev->dev);
300 if (gpio_is_valid(pdata->gpio_pullup))
301 gpio_free(pdata->gpio_pullup);
302 gpio_free(pdata->gpio_vbus);
304 platform_set_drvdata(pdev, NULL);
309 static int __exit gpio_vbus_remove(struct platform_device *pdev)
311 struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
312 struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
313 int gpio = pdata->gpio_vbus;
315 regulator_put(gpio_vbus->vbus_draw);
317 otg_set_transceiver(NULL);
319 free_irq(gpio_to_irq(gpio), &pdev->dev);
320 if (gpio_is_valid(pdata->gpio_pullup))
321 gpio_free(pdata->gpio_pullup);
323 platform_set_drvdata(pdev, NULL);
329 /* NOTE: the gpio-vbus device may *NOT* be hotplugged */
331 MODULE_ALIAS("platform:gpio-vbus");
333 static struct platform_driver gpio_vbus_driver = {
336 .owner = THIS_MODULE,
338 .remove = __exit_p(gpio_vbus_remove),
341 static int __init gpio_vbus_init(void)
343 return platform_driver_probe(&gpio_vbus_driver, gpio_vbus_probe);
345 module_init(gpio_vbus_init);
347 static void __exit gpio_vbus_exit(void)
349 platform_driver_unregister(&gpio_vbus_driver);
351 module_exit(gpio_vbus_exit);
353 MODULE_DESCRIPTION("simple GPIO controlled OTG transceiver driver");
354 MODULE_AUTHOR("Philipp Zabel");
355 MODULE_LICENSE("GPL");