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 
11 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/gpio.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/usb.h>
18 #include <linux/workqueue.h>
19 
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  */
34 struct gpio_vbus_data {
35 	struct otg_transceiver otg;
36 	struct device          *dev;
37 	struct regulator       *vbus_draw;
38 	int			vbus_draw_enabled;
39 	unsigned		mA;
40 	struct work_struct	work;
41 };
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 */
set_vbus_draw(struct gpio_vbus_data * gpio_vbus,unsigned mA)59 static 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 
is_vbus_powered(struct gpio_vbus_mach_info * pdata)83 static int is_vbus_powered(struct gpio_vbus_mach_info *pdata)
84 {
85 	int vbus;
86 
87 	vbus = gpio_get_value(pdata->gpio_vbus);
88 	if (pdata->gpio_vbus_inverted)
89 		vbus = !vbus;
90 
91 	return vbus;
92 }
93 
gpio_vbus_work(struct work_struct * work)94 static 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;
100 
101 	if (!gpio_vbus->otg.gadget)
102 		return;
103 
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;
110 	if (is_vbus_powered(pdata)) {
111 		gpio_vbus->otg.state = OTG_STATE_B_PERIPHERAL;
112 		usb_gadget_vbus_connect(gpio_vbus->otg.gadget);
113 
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 
127 		usb_gadget_vbus_disconnect(gpio_vbus->otg.gadget);
128 		gpio_vbus->otg.state = OTG_STATE_B_IDLE;
129 	}
130 }
131 
132 /* VBUS change IRQ handler */
gpio_vbus_irq(int irq,void * data)133 static 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);
138 
139 	dev_dbg(&pdev->dev, "VBUS %s (gadget: %s)\n",
140 		is_vbus_powered(pdata) ? "supplied" : "inactive",
141 		gpio_vbus->otg.gadget ? gpio_vbus->otg.gadget->name : "none");
142 
143 	if (gpio_vbus->otg.gadget)
144 		schedule_work(&gpio_vbus->work);
145 
146 	return IRQ_HANDLED;
147 }
148 
149 /* OTG transceiver interface */
150 
151 /* bind/unbind the peripheral controller */
gpio_vbus_set_peripheral(struct otg_transceiver * otg,struct usb_gadget * gadget)152 static int gpio_vbus_set_peripheral(struct otg_transceiver *otg,
153 				struct usb_gadget *gadget)
154 {
155 	struct gpio_vbus_data *gpio_vbus;
156 	struct gpio_vbus_mach_info *pdata;
157 	struct platform_device *pdev;
158 	int gpio, irq;
159 
160 	gpio_vbus = container_of(otg, struct gpio_vbus_data, otg);
161 	pdev = to_platform_device(gpio_vbus->dev);
162 	pdata = gpio_vbus->dev->platform_data;
163 	irq = gpio_to_irq(pdata->gpio_vbus);
164 	gpio = pdata->gpio_pullup;
165 
166 	if (!gadget) {
167 		dev_dbg(&pdev->dev, "unregistering gadget '%s'\n",
168 			otg->gadget->name);
169 
170 		/* optionally disable D+ pullup */
171 		if (gpio_is_valid(gpio))
172 			gpio_set_value(gpio, pdata->gpio_pullup_inverted);
173 
174 		set_vbus_draw(gpio_vbus, 0);
175 
176 		usb_gadget_vbus_disconnect(otg->gadget);
177 		otg->state = OTG_STATE_UNDEFINED;
178 
179 		otg->gadget = NULL;
180 		return 0;
181 	}
182 
183 	otg->gadget = gadget;
184 	dev_dbg(&pdev->dev, "registered gadget '%s'\n", gadget->name);
185 
186 	/* initialize connection state */
187 	gpio_vbus_irq(irq, pdev);
188 	return 0;
189 }
190 
191 /* effective for B devices, ignored for A-peripheral */
gpio_vbus_set_power(struct otg_transceiver * otg,unsigned mA)192 static int gpio_vbus_set_power(struct otg_transceiver *otg, unsigned mA)
193 {
194 	struct gpio_vbus_data *gpio_vbus;
195 
196 	gpio_vbus = container_of(otg, struct gpio_vbus_data, otg);
197 
198 	if (otg->state == OTG_STATE_B_PERIPHERAL)
199 		set_vbus_draw(gpio_vbus, mA);
200 	return 0;
201 }
202 
203 /* for non-OTG B devices: set/clear transceiver suspend mode */
gpio_vbus_set_suspend(struct otg_transceiver * otg,int suspend)204 static int gpio_vbus_set_suspend(struct otg_transceiver *otg, int suspend)
205 {
206 	struct gpio_vbus_data *gpio_vbus;
207 
208 	gpio_vbus = container_of(otg, struct gpio_vbus_data, otg);
209 
210 	/* draw max 0 mA from vbus in suspend mode; or the previously
211 	 * recorded amount of current if not suspended
212 	 *
213 	 * NOTE: high powered configs (mA > 100) may draw up to 2.5 mA
214 	 * if they're wake-enabled ... we don't handle that yet.
215 	 */
216 	return gpio_vbus_set_power(otg, suspend ? 0 : gpio_vbus->mA);
217 }
218 
219 /* platform driver interface */
220 
gpio_vbus_probe(struct platform_device * pdev)221 static int __init gpio_vbus_probe(struct platform_device *pdev)
222 {
223 	struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
224 	struct gpio_vbus_data *gpio_vbus;
225 	struct resource *res;
226 	int err, gpio, irq;
227 
228 	if (!pdata || !gpio_is_valid(pdata->gpio_vbus))
229 		return -EINVAL;
230 	gpio = pdata->gpio_vbus;
231 
232 	gpio_vbus = kzalloc(sizeof(struct gpio_vbus_data), GFP_KERNEL);
233 	if (!gpio_vbus)
234 		return -ENOMEM;
235 
236 	platform_set_drvdata(pdev, gpio_vbus);
237 	gpio_vbus->dev = &pdev->dev;
238 	gpio_vbus->otg.label = "gpio-vbus";
239 	gpio_vbus->otg.state = OTG_STATE_UNDEFINED;
240 	gpio_vbus->otg.set_peripheral = gpio_vbus_set_peripheral;
241 	gpio_vbus->otg.set_power = gpio_vbus_set_power;
242 	gpio_vbus->otg.set_suspend = gpio_vbus_set_suspend;
243 
244 	err = gpio_request(gpio, "vbus_detect");
245 	if (err) {
246 		dev_err(&pdev->dev, "can't request vbus gpio %d, err: %d\n",
247 			gpio, err);
248 		goto err_gpio;
249 	}
250 	gpio_direction_input(gpio);
251 
252 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
253 	if (res) {
254 		irq = res->start;
255 		res->flags &= IRQF_TRIGGER_MASK;
256 		res->flags |= IRQF_SAMPLE_RANDOM | IRQF_SHARED;
257 	} else
258 		irq = gpio_to_irq(gpio);
259 
260 	/* if data line pullup is in use, initialize it to "not pulling up" */
261 	gpio = pdata->gpio_pullup;
262 	if (gpio_is_valid(gpio)) {
263 		err = gpio_request(gpio, "udc_pullup");
264 		if (err) {
265 			dev_err(&pdev->dev,
266 				"can't request pullup gpio %d, err: %d\n",
267 				gpio, err);
268 			gpio_free(pdata->gpio_vbus);
269 			goto err_gpio;
270 		}
271 		gpio_direction_output(gpio, pdata->gpio_pullup_inverted);
272 	}
273 
274 	err = request_irq(irq, gpio_vbus_irq, VBUS_IRQ_FLAGS,
275 		"vbus_detect", pdev);
276 	if (err) {
277 		dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
278 			irq, err);
279 		goto err_irq;
280 	}
281 	INIT_WORK(&gpio_vbus->work, gpio_vbus_work);
282 
283 	gpio_vbus->vbus_draw = regulator_get(&pdev->dev, "vbus_draw");
284 	if (IS_ERR(gpio_vbus->vbus_draw)) {
285 		dev_dbg(&pdev->dev, "can't get vbus_draw regulator, err: %ld\n",
286 			PTR_ERR(gpio_vbus->vbus_draw));
287 		gpio_vbus->vbus_draw = NULL;
288 	}
289 
290 	/* only active when a gadget is registered */
291 	err = otg_set_transceiver(&gpio_vbus->otg);
292 	if (err) {
293 		dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
294 			err);
295 		goto err_otg;
296 	}
297 
298 	return 0;
299 err_otg:
300 	free_irq(irq, &pdev->dev);
301 err_irq:
302 	if (gpio_is_valid(pdata->gpio_pullup))
303 		gpio_free(pdata->gpio_pullup);
304 	gpio_free(pdata->gpio_vbus);
305 err_gpio:
306 	platform_set_drvdata(pdev, NULL);
307 	kfree(gpio_vbus);
308 	return err;
309 }
310 
gpio_vbus_remove(struct platform_device * pdev)311 static int __exit gpio_vbus_remove(struct platform_device *pdev)
312 {
313 	struct gpio_vbus_data *gpio_vbus = platform_get_drvdata(pdev);
314 	struct gpio_vbus_mach_info *pdata = pdev->dev.platform_data;
315 	int gpio = pdata->gpio_vbus;
316 
317 	regulator_put(gpio_vbus->vbus_draw);
318 
319 	otg_set_transceiver(NULL);
320 
321 	free_irq(gpio_to_irq(gpio), &pdev->dev);
322 	if (gpio_is_valid(pdata->gpio_pullup))
323 		gpio_free(pdata->gpio_pullup);
324 	gpio_free(gpio);
325 	platform_set_drvdata(pdev, NULL);
326 	kfree(gpio_vbus);
327 
328 	return 0;
329 }
330 
331 /* NOTE:  the gpio-vbus device may *NOT* be hotplugged */
332 
333 MODULE_ALIAS("platform:gpio-vbus");
334 
335 static struct platform_driver gpio_vbus_driver = {
336 	.driver = {
337 		.name  = "gpio-vbus",
338 		.owner = THIS_MODULE,
339 	},
340 	.remove  = __exit_p(gpio_vbus_remove),
341 };
342 
gpio_vbus_init(void)343 static int __init gpio_vbus_init(void)
344 {
345 	return platform_driver_probe(&gpio_vbus_driver, gpio_vbus_probe);
346 }
347 module_init(gpio_vbus_init);
348 
gpio_vbus_exit(void)349 static void __exit gpio_vbus_exit(void)
350 {
351 	platform_driver_unregister(&gpio_vbus_driver);
352 }
353 module_exit(gpio_vbus_exit);
354 
355 MODULE_DESCRIPTION("simple GPIO controlled OTG transceiver driver");
356 MODULE_AUTHOR("Philipp Zabel");
357 MODULE_LICENSE("GPL");
358