usb: otg: enable regulator only on cable/device connect
[linux-2.6-block.git] / drivers / usb / otg / twl6030-usb.c
CommitLineData
c33fad0c
HH
1/*
2 * twl6030_usb - TWL6030 USB transceiver, talking to OMAP OTG driver.
3 *
4 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Author: Hema HK <hemahk@ti.com>
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/platform_device.h>
27#include <linux/io.h>
28#include <linux/usb/otg.h>
29#include <linux/i2c/twl.h>
30#include <linux/regulator/consumer.h>
31#include <linux/err.h>
32#include <linux/notifier.h>
33#include <linux/slab.h>
34
35/* usb register definitions */
36#define USB_VENDOR_ID_LSB 0x00
37#define USB_VENDOR_ID_MSB 0x01
38#define USB_PRODUCT_ID_LSB 0x02
39#define USB_PRODUCT_ID_MSB 0x03
40#define USB_VBUS_CTRL_SET 0x04
41#define USB_VBUS_CTRL_CLR 0x05
42#define USB_ID_CTRL_SET 0x06
43#define USB_ID_CTRL_CLR 0x07
44#define USB_VBUS_INT_SRC 0x08
45#define USB_VBUS_INT_LATCH_SET 0x09
46#define USB_VBUS_INT_LATCH_CLR 0x0A
47#define USB_VBUS_INT_EN_LO_SET 0x0B
48#define USB_VBUS_INT_EN_LO_CLR 0x0C
49#define USB_VBUS_INT_EN_HI_SET 0x0D
50#define USB_VBUS_INT_EN_HI_CLR 0x0E
51#define USB_ID_INT_SRC 0x0F
52#define USB_ID_INT_LATCH_SET 0x10
53#define USB_ID_INT_LATCH_CLR 0x11
54
55#define USB_ID_INT_EN_LO_SET 0x12
56#define USB_ID_INT_EN_LO_CLR 0x13
57#define USB_ID_INT_EN_HI_SET 0x14
58#define USB_ID_INT_EN_HI_CLR 0x15
59#define USB_OTG_ADP_CTRL 0x16
60#define USB_OTG_ADP_HIGH 0x17
61#define USB_OTG_ADP_LOW 0x18
62#define USB_OTG_ADP_RISE 0x19
63#define USB_OTG_REVISION 0x1A
64
65/* to be moved to LDO */
66#define TWL6030_MISC2 0xE5
67#define TWL6030_CFG_LDO_PD2 0xF5
68#define TWL6030_BACKUP_REG 0xFA
69
70#define STS_HW_CONDITIONS 0x21
71
72/* In module TWL6030_MODULE_PM_MASTER */
73#define STS_HW_CONDITIONS 0x21
74#define STS_USB_ID BIT(2)
75
76/* In module TWL6030_MODULE_PM_RECEIVER */
77#define VUSB_CFG_TRANS 0x71
78#define VUSB_CFG_STATE 0x72
79#define VUSB_CFG_VOLTAGE 0x73
80
81/* in module TWL6030_MODULE_MAIN_CHARGE */
82
83#define CHARGERUSB_CTRL1 0x8
84
85#define CONTROLLER_STAT1 0x03
86#define VBUS_DET BIT(2)
87
88struct twl6030_usb {
89 struct otg_transceiver otg;
90 struct device *dev;
91
92 /* for vbus reporting with irqs disabled */
93 spinlock_t lock;
94
95 struct regulator *usb3v3;
96
97 int irq1;
98 int irq2;
99 u8 linkstat;
100 u8 asleep;
101 bool irq_enabled;
102};
103
104#define xceiv_to_twl(x) container_of((x), struct twl6030_usb, otg);
105
106/*-------------------------------------------------------------------------*/
107
108static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
109 u8 data, u8 address)
110{
111 int ret = 0;
112
113 ret = twl_i2c_write_u8(module, data, address);
114 if (ret < 0)
115 dev_err(twl->dev,
116 "Write[0x%x] Error %d\n", address, ret);
117 return ret;
118}
119
120static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
121{
122 u8 data, ret = 0;
123
124 ret = twl_i2c_read_u8(module, &data, address);
125 if (ret >= 0)
126 ret = data;
127 else
128 dev_err(twl->dev,
129 "readb[0x%x,0x%x] Error %d\n",
130 module, address, ret);
131 return ret;
132}
133
134/*-------------------------------------------------------------------------*/
135static int twl6030_set_phy_clk(struct otg_transceiver *x, int on)
136{
137 struct twl6030_usb *twl;
138 struct device *dev;
139 struct twl4030_usb_data *pdata;
140
141 twl = xceiv_to_twl(x);
142 dev = twl->dev;
143 pdata = dev->platform_data;
144
145 pdata->phy_set_clock(twl->dev, on);
146
147 return 0;
148}
149
150static int twl6030_phy_init(struct otg_transceiver *x)
151{
152 u8 hw_state;
153 struct twl6030_usb *twl;
154 struct device *dev;
155 struct twl4030_usb_data *pdata;
156
157 twl = xceiv_to_twl(x);
158 dev = twl->dev;
159 pdata = dev->platform_data;
160
c33fad0c
HH
161 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
162
163 if (hw_state & STS_USB_ID)
164 pdata->phy_power(twl->dev, 1, 1);
165 else
166 pdata->phy_power(twl->dev, 0, 1);
167
168 return 0;
169}
170
171static void twl6030_phy_shutdown(struct otg_transceiver *x)
172{
173 struct twl6030_usb *twl;
174 struct device *dev;
175 struct twl4030_usb_data *pdata;
176
177 twl = xceiv_to_twl(x);
178 dev = twl->dev;
179 pdata = dev->platform_data;
180 pdata->phy_power(twl->dev, 0, 0);
c33fad0c
HH
181}
182
183static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
184{
185
186 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
187 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
188
189 /* Program CFG_LDO_PD2 register and set VUSB bit */
190 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
191
192 /* Program MISC2 register and set bit VUSB_IN_VBAT */
193 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
194
195 twl->usb3v3 = regulator_get(twl->dev, "vusb");
196 if (IS_ERR(twl->usb3v3))
197 return -ENODEV;
198
c33fad0c
HH
199 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
200 twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
201
202 /*
203 * Program the USB_ID_CTRL_SET register to enable GND drive
204 * and the ID comparators
205 */
206 twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
207
208 return 0;
209}
210
211static ssize_t twl6030_usb_vbus_show(struct device *dev,
212 struct device_attribute *attr, char *buf)
213{
214 struct twl6030_usb *twl = dev_get_drvdata(dev);
215 unsigned long flags;
216 int ret = -EINVAL;
217
218 spin_lock_irqsave(&twl->lock, flags);
219
220 switch (twl->linkstat) {
221 case USB_EVENT_VBUS:
222 ret = snprintf(buf, PAGE_SIZE, "vbus\n");
223 break;
224 case USB_EVENT_ID:
225 ret = snprintf(buf, PAGE_SIZE, "id\n");
226 break;
227 case USB_EVENT_NONE:
228 ret = snprintf(buf, PAGE_SIZE, "none\n");
229 break;
230 default:
231 ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
232 }
233 spin_unlock_irqrestore(&twl->lock, flags);
234
235 return ret;
236}
237static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
238
239static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
240{
241 struct twl6030_usb *twl = _twl;
242 int status;
243 u8 vbus_state, hw_state;
244
245 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
246
247 vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
248 CONTROLLER_STAT1);
249 if (!(hw_state & STS_USB_ID)) {
250 if (vbus_state & VBUS_DET) {
6dc2503b
HH
251 regulator_enable(twl->usb3v3);
252 twl->asleep = 1;
c33fad0c
HH
253 status = USB_EVENT_VBUS;
254 twl->otg.default_a = false;
255 twl->otg.state = OTG_STATE_B_IDLE;
6dc2503b
HH
256 twl->linkstat = status;
257 blocking_notifier_call_chain(&twl->otg.notifier,
258 status, twl->otg.gadget);
c33fad0c
HH
259 } else {
260 status = USB_EVENT_NONE;
c33fad0c
HH
261 twl->linkstat = status;
262 blocking_notifier_call_chain(&twl->otg.notifier,
263 status, twl->otg.gadget);
6dc2503b
HH
264 if (twl->asleep) {
265 regulator_disable(twl->usb3v3);
266 twl->asleep = 0;
267 }
c33fad0c
HH
268 }
269 }
270 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
271
272 return IRQ_HANDLED;
273}
274
275static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
276{
277 struct twl6030_usb *twl = _twl;
278 int status = USB_EVENT_NONE;
279 u8 hw_state;
280
281 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
282
283 if (hw_state & STS_USB_ID) {
284
6dc2503b
HH
285 regulator_enable(twl->usb3v3);
286 twl->asleep = 1;
c33fad0c
HH
287 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR, 0x1);
288 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
289 0x10);
290 status = USB_EVENT_ID;
291 twl->otg.default_a = true;
292 twl->otg.state = OTG_STATE_A_IDLE;
293 blocking_notifier_call_chain(&twl->otg.notifier, status,
294 twl->otg.gadget);
295 } else {
296 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR,
297 0x10);
298 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
299 0x1);
300 }
301 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_LATCH_CLR, status);
302 twl->linkstat = status;
303
304 return IRQ_HANDLED;
305}
306
307static int twl6030_set_peripheral(struct otg_transceiver *x,
308 struct usb_gadget *gadget)
309{
310 struct twl6030_usb *twl;
311
312 if (!x)
313 return -ENODEV;
314
315 twl = xceiv_to_twl(x);
316 twl->otg.gadget = gadget;
317 if (!gadget)
318 twl->otg.state = OTG_STATE_UNDEFINED;
319
320 return 0;
321}
322
323static int twl6030_enable_irq(struct otg_transceiver *x)
324{
325 struct twl6030_usb *twl = xceiv_to_twl(x);
326
327 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET, 0x1);
328 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
329 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
330
331 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
332 REG_INT_MSK_LINE_C);
333 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
334 REG_INT_MSK_STS_C);
335 twl6030_usb_irq(twl->irq2, twl);
336 twl6030_usbotg_irq(twl->irq1, twl);
337
338 return 0;
339}
340
341static int twl6030_set_vbus(struct otg_transceiver *x, bool enabled)
342{
343 struct twl6030_usb *twl = xceiv_to_twl(x);
344
345 /*
346 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
347 * register. This enables boost mode.
348 */
349 if (enabled)
350 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
351 CHARGERUSB_CTRL1);
352 else
353 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
354 CHARGERUSB_CTRL1);
355 return 0;
356}
357
358static int twl6030_set_host(struct otg_transceiver *x, struct usb_bus *host)
359{
360 struct twl6030_usb *twl;
361
362 if (!x)
363 return -ENODEV;
364
365 twl = xceiv_to_twl(x);
366 twl->otg.host = host;
367 if (!host)
368 twl->otg.state = OTG_STATE_UNDEFINED;
369 return 0;
370}
371
372static int __devinit twl6030_usb_probe(struct platform_device *pdev)
373{
374 struct twl6030_usb *twl;
375 int status, err;
376 struct twl4030_usb_data *pdata;
377 struct device *dev = &pdev->dev;
378 pdata = dev->platform_data;
379
380 twl = kzalloc(sizeof *twl, GFP_KERNEL);
381 if (!twl)
382 return -ENOMEM;
383
384 twl->dev = &pdev->dev;
385 twl->irq1 = platform_get_irq(pdev, 0);
386 twl->irq2 = platform_get_irq(pdev, 1);
387 twl->otg.dev = twl->dev;
388 twl->otg.label = "twl6030";
389 twl->otg.set_host = twl6030_set_host;
390 twl->otg.set_peripheral = twl6030_set_peripheral;
391 twl->otg.set_vbus = twl6030_set_vbus;
392 twl->otg.init = twl6030_phy_init;
393 twl->otg.shutdown = twl6030_phy_shutdown;
394
395 /* init spinlock for workqueue */
396 spin_lock_init(&twl->lock);
397
398 err = twl6030_usb_ldo_init(twl);
399 if (err) {
400 dev_err(&pdev->dev, "ldo init failed\n");
401 kfree(twl);
402 return err;
403 }
404 otg_set_transceiver(&twl->otg);
405
406 platform_set_drvdata(pdev, twl);
407 if (device_create_file(&pdev->dev, &dev_attr_vbus))
408 dev_warn(&pdev->dev, "could not create sysfs file\n");
409
410 BLOCKING_INIT_NOTIFIER_HEAD(&twl->otg.notifier);
411
412 twl->irq_enabled = true;
413 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
414 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
415 "twl6030_usb", twl);
416 if (status < 0) {
417 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
418 twl->irq1, status);
419 device_remove_file(twl->dev, &dev_attr_vbus);
420 kfree(twl);
421 return status;
422 }
423
424 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
425 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
426 "twl6030_usb", twl);
427 if (status < 0) {
428 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
429 twl->irq2, status);
430 free_irq(twl->irq1, twl);
431 device_remove_file(twl->dev, &dev_attr_vbus);
432 kfree(twl);
433 return status;
434 }
435
6dc2503b 436 twl->asleep = 0;
c33fad0c
HH
437 pdata->phy_init(dev);
438 twl6030_enable_irq(&twl->otg);
439 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
440
441 return 0;
442}
443
444static int __exit twl6030_usb_remove(struct platform_device *pdev)
445{
446 struct twl6030_usb *twl = platform_get_drvdata(pdev);
447
448 struct twl4030_usb_data *pdata;
449 struct device *dev = &pdev->dev;
450 pdata = dev->platform_data;
451
452 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
453 REG_INT_MSK_LINE_C);
454 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
455 REG_INT_MSK_STS_C);
456 free_irq(twl->irq1, twl);
457 free_irq(twl->irq2, twl);
458 regulator_put(twl->usb3v3);
459 pdata->phy_exit(twl->dev);
460 device_remove_file(twl->dev, &dev_attr_vbus);
461 kfree(twl);
462
463 return 0;
464}
465
466static struct platform_driver twl6030_usb_driver = {
467 .probe = twl6030_usb_probe,
468 .remove = __exit_p(twl6030_usb_remove),
469 .driver = {
470 .name = "twl6030_usb",
471 .owner = THIS_MODULE,
472 },
473};
474
475static int __init twl6030_usb_init(void)
476{
477 return platform_driver_register(&twl6030_usb_driver);
478}
479subsys_initcall(twl6030_usb_init);
480
481static void __exit twl6030_usb_exit(void)
482{
483 platform_driver_unregister(&twl6030_usb_driver);
484}
485module_exit(twl6030_usb_exit);
486
487MODULE_ALIAS("platform:twl6030_usb");
488MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
489MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
490MODULE_LICENSE("GPL");