blob: 3ece43a2e4c14b2b4a07fee9dcfaab4be03cfa3c [file] [log] [blame]
Philipp Zabel6084f1b2008-11-24 12:00:01 -08001/*
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
11#include <linux/kernel.h>
12#include <linux/platform_device.h>
13#include <linux/gpio.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040014#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Philipp Zabel6084f1b2008-11-24 12:00:01 -080016#include <linux/interrupt.h>
17#include <linux/usb.h>
Robert Jarzmikc2344f12009-01-24 23:54:31 -080018#include <linux/workqueue.h>
Philipp Zabel6084f1b2008-11-24 12:00:01 -080019
20#include <linux/regulator/consumer.h>
21
22#include <linux/usb/gadget.h>
23#include <linux/usb/gpio_vbus.h>
24#include <linux/usb/otg.h>
25
26
27/*
28 * A simple GPIO VBUS sensing driver for B peripheral only devices
29 * with internal transceivers. It can control a D+ pullup GPIO and
30 * a regulator to limit the current drawn from VBUS.
31 *
32 * Needs to be loaded before the UDC driver that will use it.
33 */
34struct gpio_vbus_data {
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +020035 struct usb_phy phy;
Philipp Zabel6084f1b2008-11-24 12:00:01 -080036 struct device *dev;
37 struct regulator *vbus_draw;
38 int vbus_draw_enabled;
39 unsigned mA;
Robert Jarzmikc2344f12009-01-24 23:54:31 -080040 struct work_struct work;
Philipp Zabel6084f1b2008-11-24 12:00:01 -080041};
42
43
44/*
45 * This driver relies on "both edges" triggering. VBUS has 100 msec to
46 * stabilize, so the peripheral controller driver may need to cope with
47 * some bouncing due to current surges (e.g. charging local capacitance)
48 * and contact chatter.
49 *
50 * REVISIT in desperate straits, toggling between rising and falling
51 * edges might be workable.
52 */
53#define VBUS_IRQ_FLAGS \
54 ( IRQF_SAMPLE_RANDOM | IRQF_SHARED \
55 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING )
56
57
58/* interface to regulator framework */
59static void set_vbus_draw(struct gpio_vbus_data *gpio_vbus, unsigned mA)
60{
61 struct regulator *vbus_draw = gpio_vbus->vbus_draw;
62 int enabled;
63
64 if (!vbus_draw)
65 return;
66
67 enabled = gpio_vbus->vbus_draw_enabled;
68 if (mA) {
69 regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
70 if (!enabled) {
71 regulator_enable(vbus_draw);
72 gpio_vbus->vbus_draw_enabled = 1;
73 }
74 } else {
75 if (enabled) {
76 regulator_disable(vbus_draw);
77 gpio_vbus->vbus_draw_enabled = 0;
78 }
79 }
80 gpio_vbus->mA = mA;
81}
82
Robert Jarzmikc2344f12009-01-24 23:54:31 -080083static int is_vbus_powered(struct gpio_vbus_mach_info *pdata)
Philipp Zabel6084f1b2008-11-24 12:00:01 -080084{
Robert Jarzmikc2344f12009-01-24 23:54:31 -080085 int vbus;
Philipp Zabel6084f1b2008-11-24 12:00:01 -080086
87 vbus = gpio_get_value(pdata->gpio_vbus);
88 if (pdata->gpio_vbus_inverted)
89 vbus = !vbus;
90
Robert Jarzmikc2344f12009-01-24 23:54:31 -080091 return vbus;
92}
93
94static void gpio_vbus_work(struct work_struct *work)
95{
96 struct gpio_vbus_data *gpio_vbus =
97 container_of(work, struct gpio_vbus_data, work);
98 struct gpio_vbus_mach_info *pdata = gpio_vbus->dev->platform_data;
99 int gpio;
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800100
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200101 if (!gpio_vbus->phy.otg->gadget)
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800102 return;
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800103
104 /* Peripheral controllers which manage the pullup themselves won't have
105 * gpio_pullup configured here. If it's configured here, we'll do what
106 * isp1301_omap::b_peripheral() does and enable the pullup here... although
107 * that may complicate usb_gadget_{,dis}connect() support.
108 */
109 gpio = pdata->gpio_pullup;
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800110 if (is_vbus_powered(pdata)) {
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200111 gpio_vbus->phy.state = OTG_STATE_B_PERIPHERAL;
112 usb_gadget_vbus_connect(gpio_vbus->phy.otg->gadget);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800113
114 /* drawing a "unit load" is *always* OK, except for OTG */
115 set_vbus_draw(gpio_vbus, 100);
116
117 /* optionally enable D+ pullup */
118 if (gpio_is_valid(gpio))
119 gpio_set_value(gpio, !pdata->gpio_pullup_inverted);
120 } else {
121 /* optionally disable D+ pullup */
122 if (gpio_is_valid(gpio))
123 gpio_set_value(gpio, pdata->gpio_pullup_inverted);
124
125 set_vbus_draw(gpio_vbus, 0);
126
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200127 usb_gadget_vbus_disconnect(gpio_vbus->phy.otg->gadget);
128 gpio_vbus->phy.state = OTG_STATE_B_IDLE;
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800129 }
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800130}
131
132/* VBUS change IRQ handler */
133static irqreturn_t gpio_vbus_irq(int irq, void *data)
134{
135 struct platform_device *pdev = data;
136 struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
137 struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200138 struct usb_otg *otg = gpio_vbus->phy.otg;
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800139
140 dev_dbg(&pdev->dev, "VBUS %s (gadget: %s)\n",
141 is_vbus_powered(pdata) ? "supplied" : "inactive",
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200142 otg->gadget ? otg->gadget->name : "none");
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800143
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200144 if (otg->gadget)
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800145 schedule_work(&gpio_vbus->work);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800146
147 return IRQ_HANDLED;
148}
149
150/* OTG transceiver interface */
151
152/* bind/unbind the peripheral controller */
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200153static int gpio_vbus_set_peripheral(struct usb_otg *otg,
154 struct usb_gadget *gadget)
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800155{
156 struct gpio_vbus_data *gpio_vbus;
157 struct gpio_vbus_mach_info *pdata;
158 struct platform_device *pdev;
159 int gpio, irq;
160
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200161 gpio_vbus = container_of(otg->phy, struct gpio_vbus_data, phy);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800162 pdev = to_platform_device(gpio_vbus->dev);
163 pdata = gpio_vbus->dev->platform_data;
164 irq = gpio_to_irq(pdata->gpio_vbus);
165 gpio = pdata->gpio_pullup;
166
167 if (!gadget) {
168 dev_dbg(&pdev->dev, "unregistering gadget '%s'\n",
169 otg->gadget->name);
170
171 /* optionally disable D+ pullup */
172 if (gpio_is_valid(gpio))
173 gpio_set_value(gpio, pdata->gpio_pullup_inverted);
174
175 set_vbus_draw(gpio_vbus, 0);
176
177 usb_gadget_vbus_disconnect(otg->gadget);
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200178 otg->phy->state = OTG_STATE_UNDEFINED;
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800179
180 otg->gadget = NULL;
181 return 0;
182 }
183
184 otg->gadget = gadget;
185 dev_dbg(&pdev->dev, "registered gadget '%s'\n", gadget->name);
186
187 /* initialize connection state */
188 gpio_vbus_irq(irq, pdev);
189 return 0;
190}
191
192/* effective for B devices, ignored for A-peripheral */
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200193static int gpio_vbus_set_power(struct usb_phy *phy, unsigned mA)
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800194{
195 struct gpio_vbus_data *gpio_vbus;
196
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200197 gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800198
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200199 if (phy->state == OTG_STATE_B_PERIPHERAL)
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800200 set_vbus_draw(gpio_vbus, mA);
201 return 0;
202}
203
204/* for non-OTG B devices: set/clear transceiver suspend mode */
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200205static int gpio_vbus_set_suspend(struct usb_phy *phy, int suspend)
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800206{
207 struct gpio_vbus_data *gpio_vbus;
208
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200209 gpio_vbus = container_of(phy, struct gpio_vbus_data, phy);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800210
211 /* draw max 0 mA from vbus in suspend mode; or the previously
212 * recorded amount of current if not suspended
213 *
214 * NOTE: high powered configs (mA > 100) may draw up to 2.5 mA
215 * if they're wake-enabled ... we don't handle that yet.
216 */
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200217 return gpio_vbus_set_power(phy, suspend ? 0 : gpio_vbus->mA);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800218}
219
220/* platform driver interface */
221
222static int __init gpio_vbus_probe(struct platform_device *pdev)
223{
224 struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
225 struct gpio_vbus_data *gpio_vbus;
226 struct resource *res;
227 int err, gpio, irq;
228
229 if (!pdata || !gpio_is_valid(pdata->gpio_vbus))
230 return -EINVAL;
231 gpio = pdata->gpio_vbus;
232
233 gpio_vbus = kzalloc(sizeof(struct gpio_vbus_data), GFP_KERNEL);
234 if (!gpio_vbus)
235 return -ENOMEM;
236
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200237 gpio_vbus->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
238 if (!gpio_vbus->phy.otg) {
239 kfree(gpio_vbus);
240 return -ENOMEM;
241 }
242
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800243 platform_set_drvdata(pdev, gpio_vbus);
244 gpio_vbus->dev = &pdev->dev;
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200245 gpio_vbus->phy.label = "gpio-vbus";
246 gpio_vbus->phy.set_power = gpio_vbus_set_power;
247 gpio_vbus->phy.set_suspend = gpio_vbus_set_suspend;
248 gpio_vbus->phy.state = OTG_STATE_UNDEFINED;
249
250 gpio_vbus->phy.otg->phy = &gpio_vbus->phy;
251 gpio_vbus->phy.otg->set_peripheral = gpio_vbus_set_peripheral;
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800252
253 err = gpio_request(gpio, "vbus_detect");
254 if (err) {
255 dev_err(&pdev->dev, "can't request vbus gpio %d, err: %d\n",
256 gpio, err);
257 goto err_gpio;
258 }
259 gpio_direction_input(gpio);
260
261 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
262 if (res) {
263 irq = res->start;
264 res->flags &= IRQF_TRIGGER_MASK;
265 res->flags |= IRQF_SAMPLE_RANDOM | IRQF_SHARED;
266 } else
267 irq = gpio_to_irq(gpio);
268
269 /* if data line pullup is in use, initialize it to "not pulling up" */
270 gpio = pdata->gpio_pullup;
271 if (gpio_is_valid(gpio)) {
272 err = gpio_request(gpio, "udc_pullup");
273 if (err) {
274 dev_err(&pdev->dev,
275 "can't request pullup gpio %d, err: %d\n",
276 gpio, err);
277 gpio_free(pdata->gpio_vbus);
278 goto err_gpio;
279 }
280 gpio_direction_output(gpio, pdata->gpio_pullup_inverted);
281 }
282
283 err = request_irq(irq, gpio_vbus_irq, VBUS_IRQ_FLAGS,
284 "vbus_detect", pdev);
285 if (err) {
286 dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
287 irq, err);
288 goto err_irq;
289 }
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800290 INIT_WORK(&gpio_vbus->work, gpio_vbus_work);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800291
Dmitry Eremin-Solenikov2887ba92011-05-03 12:46:46 +0400292 gpio_vbus->vbus_draw = regulator_get(&pdev->dev, "vbus_draw");
293 if (IS_ERR(gpio_vbus->vbus_draw)) {
294 dev_dbg(&pdev->dev, "can't get vbus_draw regulator, err: %ld\n",
295 PTR_ERR(gpio_vbus->vbus_draw));
296 gpio_vbus->vbus_draw = NULL;
297 }
298
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800299 /* only active when a gadget is registered */
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200300 err = usb_set_transceiver(&gpio_vbus->phy);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800301 if (err) {
302 dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
303 err);
304 goto err_otg;
305 }
306
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800307 return 0;
308err_otg:
309 free_irq(irq, &pdev->dev);
310err_irq:
311 if (gpio_is_valid(pdata->gpio_pullup))
312 gpio_free(pdata->gpio_pullup);
313 gpio_free(pdata->gpio_vbus);
314err_gpio:
315 platform_set_drvdata(pdev, NULL);
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200316 kfree(gpio_vbus->phy.otg);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800317 kfree(gpio_vbus);
318 return err;
319}
320
321static int __exit gpio_vbus_remove(struct platform_device *pdev)
322{
323 struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
324 struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
325 int gpio = pdata->gpio_vbus;
326
327 regulator_put(gpio_vbus->vbus_draw);
328
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200329 usb_set_transceiver(NULL);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800330
331 free_irq(gpio_to_irq(gpio), &pdev->dev);
332 if (gpio_is_valid(pdata->gpio_pullup))
333 gpio_free(pdata->gpio_pullup);
334 gpio_free(gpio);
335 platform_set_drvdata(pdev, NULL);
Heikki Krogerus16bc1bb2012-02-13 13:24:07 +0200336 kfree(gpio_vbus->phy.otg);
Philipp Zabel6084f1b2008-11-24 12:00:01 -0800337 kfree(gpio_vbus);
338
339 return 0;
340}
341
342/* NOTE: the gpio-vbus device may *NOT* be hotplugged */
343
344MODULE_ALIAS("platform:gpio-vbus");
345
346static struct platform_driver gpio_vbus_driver = {
347 .driver = {
348 .name = "gpio-vbus",
349 .owner = THIS_MODULE,
350 },
351 .remove = __exit_p(gpio_vbus_remove),
352};
353
354static int __init gpio_vbus_init(void)
355{
356 return platform_driver_probe(&gpio_vbus_driver, gpio_vbus_probe);
357}
358module_init(gpio_vbus_init);
359
360static void __exit gpio_vbus_exit(void)
361{
362 platform_driver_unregister(&gpio_vbus_driver);
363}
364module_exit(gpio_vbus_exit);
365
366MODULE_DESCRIPTION("simple GPIO controlled OTG transceiver driver");
367MODULE_AUTHOR("Philipp Zabel");
368MODULE_LICENSE("GPL");