2 * twl6030_usb - TWL6030 USB transceiver, talking to OMAP OTG driver.
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.
10 * Author: Hema HK <hemahk@ti.com>
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.
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.
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/platform_device.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>
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
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
65 /* to be moved to LDO */
66 #define TWL6030_MISC2 0xE5
67 #define TWL6030_CFG_LDO_PD2 0xF5
68 #define TWL6030_BACKUP_REG 0xFA
70 #define STS_HW_CONDITIONS 0x21
72 /* In module TWL6030_MODULE_PM_MASTER */
73 #define STS_HW_CONDITIONS 0x21
74 #define STS_USB_ID BIT(2)
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
81 /* in module TWL6030_MODULE_MAIN_CHARGE */
83 #define CHARGERUSB_CTRL1 0x8
85 #define CONTROLLER_STAT1 0x03
86 #define VBUS_DET BIT(2)
89 struct otg_transceiver otg
;
92 /* for vbus reporting with irqs disabled */
95 struct regulator
*usb3v3
;
104 #define xceiv_to_twl(x) container_of((x), struct twl6030_usb, otg)
106 /*-------------------------------------------------------------------------*/
108 static inline int twl6030_writeb(struct twl6030_usb
*twl
, u8 module
,
113 ret
= twl_i2c_write_u8(module
, data
, address
);
116 "Write[0x%x] Error %d\n", address
, ret
);
120 static inline u8
twl6030_readb(struct twl6030_usb
*twl
, u8 module
, u8 address
)
124 ret
= twl_i2c_read_u8(module
, &data
, address
);
129 "readb[0x%x,0x%x] Error %d\n",
130 module
, address
, ret
);
134 /*-------------------------------------------------------------------------*/
135 static int twl6030_set_phy_clk(struct otg_transceiver
*x
, int on
)
137 struct twl6030_usb
*twl
;
139 struct twl4030_usb_data
*pdata
;
141 twl
= xceiv_to_twl(x
);
143 pdata
= dev
->platform_data
;
145 pdata
->phy_set_clock(twl
->dev
, on
);
150 static int twl6030_phy_init(struct otg_transceiver
*x
)
152 struct twl6030_usb
*twl
;
154 struct twl4030_usb_data
*pdata
;
156 twl
= xceiv_to_twl(x
);
158 pdata
= dev
->platform_data
;
160 if (twl
->linkstat
== USB_EVENT_ID
)
161 pdata
->phy_power(twl
->dev
, 1, 1);
163 pdata
->phy_power(twl
->dev
, 0, 1);
168 static void twl6030_phy_shutdown(struct otg_transceiver
*x
)
170 struct twl6030_usb
*twl
;
172 struct twl4030_usb_data
*pdata
;
174 twl
= xceiv_to_twl(x
);
176 pdata
= dev
->platform_data
;
177 pdata
->phy_power(twl
->dev
, 0, 0);
180 static int twl6030_phy_suspend(struct otg_transceiver
*x
, int suspend
)
182 struct twl6030_usb
*twl
= xceiv_to_twl(x
);
183 struct device
*dev
= twl
->dev
;
184 struct twl4030_usb_data
*pdata
= dev
->platform_data
;
186 pdata
->phy_suspend(dev
, suspend
);
191 static int twl6030_usb_ldo_init(struct twl6030_usb
*twl
)
194 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
195 twl6030_writeb(twl
, TWL6030_MODULE_ID0
, 0x1, TWL6030_BACKUP_REG
);
197 /* Program CFG_LDO_PD2 register and set VUSB bit */
198 twl6030_writeb(twl
, TWL6030_MODULE_ID0
, 0x1, TWL6030_CFG_LDO_PD2
);
200 /* Program MISC2 register and set bit VUSB_IN_VBAT */
201 twl6030_writeb(twl
, TWL6030_MODULE_ID0
, 0x10, TWL6030_MISC2
);
203 twl
->usb3v3
= regulator_get(twl
->dev
, "vusb");
204 if (IS_ERR(twl
->usb3v3
))
207 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
208 twl6030_writeb(twl
, TWL_MODULE_USB
, 0x4, USB_VBUS_CTRL_SET
);
211 * Program the USB_ID_CTRL_SET register to enable GND drive
212 * and the ID comparators
214 twl6030_writeb(twl
, TWL_MODULE_USB
, 0x14, USB_ID_CTRL_SET
);
219 static ssize_t
twl6030_usb_vbus_show(struct device
*dev
,
220 struct device_attribute
*attr
, char *buf
)
222 struct twl6030_usb
*twl
= dev_get_drvdata(dev
);
226 spin_lock_irqsave(&twl
->lock
, flags
);
228 switch (twl
->linkstat
) {
230 ret
= snprintf(buf
, PAGE_SIZE
, "vbus\n");
233 ret
= snprintf(buf
, PAGE_SIZE
, "id\n");
236 ret
= snprintf(buf
, PAGE_SIZE
, "none\n");
239 ret
= snprintf(buf
, PAGE_SIZE
, "UNKNOWN\n");
241 spin_unlock_irqrestore(&twl
->lock
, flags
);
245 static DEVICE_ATTR(vbus
, 0444, twl6030_usb_vbus_show
, NULL
);
247 static irqreturn_t
twl6030_usb_irq(int irq
, void *_twl
)
249 struct twl6030_usb
*twl
= _twl
;
251 u8 vbus_state
, hw_state
;
253 hw_state
= twl6030_readb(twl
, TWL6030_MODULE_ID0
, STS_HW_CONDITIONS
);
255 vbus_state
= twl6030_readb(twl
, TWL_MODULE_MAIN_CHARGE
,
257 if (!(hw_state
& STS_USB_ID
)) {
258 if (vbus_state
& VBUS_DET
) {
259 regulator_enable(twl
->usb3v3
);
261 status
= USB_EVENT_VBUS
;
262 twl
->otg
.default_a
= false;
263 twl
->otg
.state
= OTG_STATE_B_IDLE
;
264 twl
->linkstat
= status
;
265 twl
->otg
.last_event
= status
;
266 atomic_notifier_call_chain(&twl
->otg
.notifier
,
267 status
, twl
->otg
.gadget
);
269 status
= USB_EVENT_NONE
;
270 twl
->linkstat
= status
;
271 twl
->otg
.last_event
= status
;
272 atomic_notifier_call_chain(&twl
->otg
.notifier
,
273 status
, twl
->otg
.gadget
);
275 regulator_disable(twl
->usb3v3
);
280 sysfs_notify(&twl
->dev
->kobj
, NULL
, "vbus");
285 static irqreturn_t
twl6030_usbotg_irq(int irq
, void *_twl
)
287 struct twl6030_usb
*twl
= _twl
;
288 int status
= USB_EVENT_NONE
;
291 hw_state
= twl6030_readb(twl
, TWL6030_MODULE_ID0
, STS_HW_CONDITIONS
);
293 if (hw_state
& STS_USB_ID
) {
295 regulator_enable(twl
->usb3v3
);
297 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_CLR
, 0x1);
298 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_SET
,
300 status
= USB_EVENT_ID
;
301 twl
->otg
.default_a
= true;
302 twl
->otg
.state
= OTG_STATE_A_IDLE
;
303 twl
->linkstat
= status
;
304 twl
->otg
.last_event
= status
;
305 atomic_notifier_call_chain(&twl
->otg
.notifier
, status
,
308 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_CLR
,
310 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_SET
,
313 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_LATCH_CLR
, status
);
318 static int twl6030_set_peripheral(struct otg_transceiver
*x
,
319 struct usb_gadget
*gadget
)
321 struct twl6030_usb
*twl
;
326 twl
= xceiv_to_twl(x
);
327 twl
->otg
.gadget
= gadget
;
329 twl
->otg
.state
= OTG_STATE_UNDEFINED
;
334 static int twl6030_enable_irq(struct otg_transceiver
*x
)
336 struct twl6030_usb
*twl
= xceiv_to_twl(x
);
338 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_SET
, 0x1);
339 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C
);
340 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C
);
342 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK
,
344 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK
,
346 twl6030_usb_irq(twl
->irq2
, twl
);
347 twl6030_usbotg_irq(twl
->irq1
, twl
);
352 static int twl6030_set_vbus(struct otg_transceiver
*x
, bool enabled
)
354 struct twl6030_usb
*twl
= xceiv_to_twl(x
);
357 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
358 * register. This enables boost mode.
361 twl6030_writeb(twl
, TWL_MODULE_MAIN_CHARGE
, 0x40,
364 twl6030_writeb(twl
, TWL_MODULE_MAIN_CHARGE
, 0x00,
369 static int twl6030_set_host(struct otg_transceiver
*x
, struct usb_bus
*host
)
371 struct twl6030_usb
*twl
;
376 twl
= xceiv_to_twl(x
);
377 twl
->otg
.host
= host
;
379 twl
->otg
.state
= OTG_STATE_UNDEFINED
;
383 static int __devinit
twl6030_usb_probe(struct platform_device
*pdev
)
385 struct twl6030_usb
*twl
;
387 struct twl4030_usb_data
*pdata
;
388 struct device
*dev
= &pdev
->dev
;
389 pdata
= dev
->platform_data
;
391 twl
= kzalloc(sizeof *twl
, GFP_KERNEL
);
395 twl
->dev
= &pdev
->dev
;
396 twl
->irq1
= platform_get_irq(pdev
, 0);
397 twl
->irq2
= platform_get_irq(pdev
, 1);
398 twl
->otg
.dev
= twl
->dev
;
399 twl
->otg
.label
= "twl6030";
400 twl
->otg
.set_host
= twl6030_set_host
;
401 twl
->otg
.set_peripheral
= twl6030_set_peripheral
;
402 twl
->otg
.set_vbus
= twl6030_set_vbus
;
403 twl
->otg
.init
= twl6030_phy_init
;
404 twl
->otg
.shutdown
= twl6030_phy_shutdown
;
405 twl
->otg
.set_suspend
= twl6030_phy_suspend
;
407 /* init spinlock for workqueue */
408 spin_lock_init(&twl
->lock
);
410 err
= twl6030_usb_ldo_init(twl
);
412 dev_err(&pdev
->dev
, "ldo init failed\n");
416 otg_set_transceiver(&twl
->otg
);
418 platform_set_drvdata(pdev
, twl
);
419 if (device_create_file(&pdev
->dev
, &dev_attr_vbus
))
420 dev_warn(&pdev
->dev
, "could not create sysfs file\n");
422 ATOMIC_INIT_NOTIFIER_HEAD(&twl
->otg
.notifier
);
424 twl
->irq_enabled
= true;
425 status
= request_threaded_irq(twl
->irq1
, NULL
, twl6030_usbotg_irq
,
426 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
429 dev_err(&pdev
->dev
, "can't get IRQ %d, err %d\n",
431 device_remove_file(twl
->dev
, &dev_attr_vbus
);
436 status
= request_threaded_irq(twl
->irq2
, NULL
, twl6030_usb_irq
,
437 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
440 dev_err(&pdev
->dev
, "can't get IRQ %d, err %d\n",
442 free_irq(twl
->irq1
, twl
);
443 device_remove_file(twl
->dev
, &dev_attr_vbus
);
449 pdata
->phy_init(dev
);
450 twl6030_phy_suspend(&twl
->otg
, 0);
451 twl6030_enable_irq(&twl
->otg
);
452 dev_info(&pdev
->dev
, "Initialized TWL6030 USB module\n");
457 static int __exit
twl6030_usb_remove(struct platform_device
*pdev
)
459 struct twl6030_usb
*twl
= platform_get_drvdata(pdev
);
461 struct twl4030_usb_data
*pdata
;
462 struct device
*dev
= &pdev
->dev
;
463 pdata
= dev
->platform_data
;
465 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK
,
467 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK
,
469 free_irq(twl
->irq1
, twl
);
470 free_irq(twl
->irq2
, twl
);
471 regulator_put(twl
->usb3v3
);
472 pdata
->phy_exit(twl
->dev
);
473 device_remove_file(twl
->dev
, &dev_attr_vbus
);
479 static struct platform_driver twl6030_usb_driver
= {
480 .probe
= twl6030_usb_probe
,
481 .remove
= __exit_p(twl6030_usb_remove
),
483 .name
= "twl6030_usb",
484 .owner
= THIS_MODULE
,
488 static int __init
twl6030_usb_init(void)
490 return platform_driver_register(&twl6030_usb_driver
);
492 subsys_initcall(twl6030_usb_init
);
494 static void __exit
twl6030_usb_exit(void)
496 platform_driver_unregister(&twl6030_usb_driver
);
498 module_exit(twl6030_usb_exit
);
500 MODULE_ALIAS("platform:twl6030_usb");
501 MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
502 MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
503 MODULE_LICENSE("GPL");