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>
34 #include <linux/delay.h>
36 /* usb register definitions */
37 #define USB_VENDOR_ID_LSB 0x00
38 #define USB_VENDOR_ID_MSB 0x01
39 #define USB_PRODUCT_ID_LSB 0x02
40 #define USB_PRODUCT_ID_MSB 0x03
41 #define USB_VBUS_CTRL_SET 0x04
42 #define USB_VBUS_CTRL_CLR 0x05
43 #define USB_ID_CTRL_SET 0x06
44 #define USB_ID_CTRL_CLR 0x07
45 #define USB_VBUS_INT_SRC 0x08
46 #define USB_VBUS_INT_LATCH_SET 0x09
47 #define USB_VBUS_INT_LATCH_CLR 0x0A
48 #define USB_VBUS_INT_EN_LO_SET 0x0B
49 #define USB_VBUS_INT_EN_LO_CLR 0x0C
50 #define USB_VBUS_INT_EN_HI_SET 0x0D
51 #define USB_VBUS_INT_EN_HI_CLR 0x0E
52 #define USB_ID_INT_SRC 0x0F
53 #define USB_ID_INT_LATCH_SET 0x10
54 #define USB_ID_INT_LATCH_CLR 0x11
56 #define USB_ID_INT_EN_LO_SET 0x12
57 #define USB_ID_INT_EN_LO_CLR 0x13
58 #define USB_ID_INT_EN_HI_SET 0x14
59 #define USB_ID_INT_EN_HI_CLR 0x15
60 #define USB_OTG_ADP_CTRL 0x16
61 #define USB_OTG_ADP_HIGH 0x17
62 #define USB_OTG_ADP_LOW 0x18
63 #define USB_OTG_ADP_RISE 0x19
64 #define USB_OTG_REVISION 0x1A
66 /* to be moved to LDO */
67 #define TWL6030_MISC2 0xE5
68 #define TWL6030_CFG_LDO_PD2 0xF5
69 #define TWL6030_BACKUP_REG 0xFA
71 #define STS_HW_CONDITIONS 0x21
73 /* In module TWL6030_MODULE_PM_MASTER */
74 #define STS_HW_CONDITIONS 0x21
75 #define STS_USB_ID BIT(2)
77 /* In module TWL6030_MODULE_PM_RECEIVER */
78 #define VUSB_CFG_TRANS 0x71
79 #define VUSB_CFG_STATE 0x72
80 #define VUSB_CFG_VOLTAGE 0x73
82 /* in module TWL6030_MODULE_MAIN_CHARGE */
84 #define CHARGERUSB_CTRL1 0x8
86 #define CONTROLLER_STAT1 0x03
87 #define VBUS_DET BIT(2)
90 struct otg_transceiver otg
;
93 /* for vbus reporting with irqs disabled */
96 struct regulator
*usb3v3
;
105 #define xceiv_to_twl(x) container_of((x), struct twl6030_usb, otg)
107 /*-------------------------------------------------------------------------*/
109 static inline int twl6030_writeb(struct twl6030_usb
*twl
, u8 module
,
114 ret
= twl_i2c_write_u8(module
, data
, address
);
117 "Write[0x%x] Error %d\n", address
, ret
);
121 static inline u8
twl6030_readb(struct twl6030_usb
*twl
, u8 module
, u8 address
)
125 ret
= twl_i2c_read_u8(module
, &data
, address
);
130 "readb[0x%x,0x%x] Error %d\n",
131 module
, address
, ret
);
135 /*-------------------------------------------------------------------------*/
136 static int twl6030_set_phy_clk(struct otg_transceiver
*x
, int on
)
138 struct twl6030_usb
*twl
;
140 struct twl4030_usb_data
*pdata
;
142 twl
= xceiv_to_twl(x
);
144 pdata
= dev
->platform_data
;
146 pdata
->phy_set_clock(twl
->dev
, on
);
151 static int twl6030_phy_init(struct otg_transceiver
*x
)
153 struct twl6030_usb
*twl
;
155 struct twl4030_usb_data
*pdata
;
157 twl
= xceiv_to_twl(x
);
159 pdata
= dev
->platform_data
;
161 if (twl
->linkstat
== USB_EVENT_ID
)
162 pdata
->phy_power(twl
->dev
, 1, 1);
164 pdata
->phy_power(twl
->dev
, 0, 1);
169 static void twl6030_phy_shutdown(struct otg_transceiver
*x
)
171 struct twl6030_usb
*twl
;
173 struct twl4030_usb_data
*pdata
;
175 twl
= xceiv_to_twl(x
);
177 pdata
= dev
->platform_data
;
178 pdata
->phy_power(twl
->dev
, 0, 0);
181 static int twl6030_phy_suspend(struct otg_transceiver
*x
, int suspend
)
183 struct twl6030_usb
*twl
= xceiv_to_twl(x
);
184 struct device
*dev
= twl
->dev
;
185 struct twl4030_usb_data
*pdata
= dev
->platform_data
;
187 pdata
->phy_suspend(dev
, suspend
);
192 static int twl6030_start_srp(struct otg_transceiver
*x
)
194 struct twl6030_usb
*twl
= xceiv_to_twl(x
);
196 twl6030_writeb(twl
, TWL_MODULE_USB
, 0x24, USB_VBUS_CTRL_SET
);
197 twl6030_writeb(twl
, TWL_MODULE_USB
, 0x84, USB_VBUS_CTRL_SET
);
200 twl6030_writeb(twl
, TWL_MODULE_USB
, 0xa0, USB_VBUS_CTRL_CLR
);
205 static int twl6030_usb_ldo_init(struct twl6030_usb
*twl
)
208 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
209 twl6030_writeb(twl
, TWL6030_MODULE_ID0
, 0x1, TWL6030_BACKUP_REG
);
211 /* Program CFG_LDO_PD2 register and set VUSB bit */
212 twl6030_writeb(twl
, TWL6030_MODULE_ID0
, 0x1, TWL6030_CFG_LDO_PD2
);
214 /* Program MISC2 register and set bit VUSB_IN_VBAT */
215 twl6030_writeb(twl
, TWL6030_MODULE_ID0
, 0x10, TWL6030_MISC2
);
217 twl
->usb3v3
= regulator_get(twl
->dev
, "vusb");
218 if (IS_ERR(twl
->usb3v3
))
221 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
222 twl6030_writeb(twl
, TWL_MODULE_USB
, 0x4, USB_VBUS_CTRL_SET
);
225 * Program the USB_ID_CTRL_SET register to enable GND drive
226 * and the ID comparators
228 twl6030_writeb(twl
, TWL_MODULE_USB
, 0x14, USB_ID_CTRL_SET
);
233 static ssize_t
twl6030_usb_vbus_show(struct device
*dev
,
234 struct device_attribute
*attr
, char *buf
)
236 struct twl6030_usb
*twl
= dev_get_drvdata(dev
);
240 spin_lock_irqsave(&twl
->lock
, flags
);
242 switch (twl
->linkstat
) {
244 ret
= snprintf(buf
, PAGE_SIZE
, "vbus\n");
247 ret
= snprintf(buf
, PAGE_SIZE
, "id\n");
250 ret
= snprintf(buf
, PAGE_SIZE
, "none\n");
253 ret
= snprintf(buf
, PAGE_SIZE
, "UNKNOWN\n");
255 spin_unlock_irqrestore(&twl
->lock
, flags
);
259 static DEVICE_ATTR(vbus
, 0444, twl6030_usb_vbus_show
, NULL
);
261 static irqreturn_t
twl6030_usb_irq(int irq
, void *_twl
)
263 struct twl6030_usb
*twl
= _twl
;
265 u8 vbus_state
, hw_state
;
267 hw_state
= twl6030_readb(twl
, TWL6030_MODULE_ID0
, STS_HW_CONDITIONS
);
269 vbus_state
= twl6030_readb(twl
, TWL_MODULE_MAIN_CHARGE
,
271 if (!(hw_state
& STS_USB_ID
)) {
272 if (vbus_state
& VBUS_DET
) {
273 regulator_enable(twl
->usb3v3
);
275 status
= USB_EVENT_VBUS
;
276 twl
->otg
.default_a
= false;
277 twl
->otg
.state
= OTG_STATE_B_IDLE
;
278 twl
->linkstat
= status
;
279 twl
->otg
.last_event
= status
;
280 atomic_notifier_call_chain(&twl
->otg
.notifier
,
281 status
, twl
->otg
.gadget
);
283 status
= USB_EVENT_NONE
;
284 twl
->linkstat
= status
;
285 twl
->otg
.last_event
= status
;
286 atomic_notifier_call_chain(&twl
->otg
.notifier
,
287 status
, twl
->otg
.gadget
);
289 regulator_disable(twl
->usb3v3
);
294 sysfs_notify(&twl
->dev
->kobj
, NULL
, "vbus");
299 static irqreturn_t
twl6030_usbotg_irq(int irq
, void *_twl
)
301 struct twl6030_usb
*twl
= _twl
;
302 int status
= USB_EVENT_NONE
;
305 hw_state
= twl6030_readb(twl
, TWL6030_MODULE_ID0
, STS_HW_CONDITIONS
);
307 if (hw_state
& STS_USB_ID
) {
309 regulator_enable(twl
->usb3v3
);
311 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_CLR
, 0x1);
312 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_SET
,
314 status
= USB_EVENT_ID
;
315 twl
->otg
.default_a
= true;
316 twl
->otg
.state
= OTG_STATE_A_IDLE
;
317 twl
->linkstat
= status
;
318 twl
->otg
.last_event
= status
;
319 atomic_notifier_call_chain(&twl
->otg
.notifier
, status
,
322 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_CLR
,
324 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_SET
,
327 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_LATCH_CLR
, status
);
332 static int twl6030_set_peripheral(struct otg_transceiver
*x
,
333 struct usb_gadget
*gadget
)
335 struct twl6030_usb
*twl
;
340 twl
= xceiv_to_twl(x
);
341 twl
->otg
.gadget
= gadget
;
343 twl
->otg
.state
= OTG_STATE_UNDEFINED
;
348 static int twl6030_enable_irq(struct otg_transceiver
*x
)
350 struct twl6030_usb
*twl
= xceiv_to_twl(x
);
352 twl6030_writeb(twl
, TWL_MODULE_USB
, USB_ID_INT_EN_HI_SET
, 0x1);
353 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C
);
354 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C
);
356 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK
,
358 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK
,
360 twl6030_usb_irq(twl
->irq2
, twl
);
361 twl6030_usbotg_irq(twl
->irq1
, twl
);
366 static int twl6030_set_vbus(struct otg_transceiver
*x
, bool enabled
)
368 struct twl6030_usb
*twl
= xceiv_to_twl(x
);
371 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
372 * register. This enables boost mode.
375 twl6030_writeb(twl
, TWL_MODULE_MAIN_CHARGE
, 0x40,
378 twl6030_writeb(twl
, TWL_MODULE_MAIN_CHARGE
, 0x00,
383 static int twl6030_set_host(struct otg_transceiver
*x
, struct usb_bus
*host
)
385 struct twl6030_usb
*twl
;
390 twl
= xceiv_to_twl(x
);
391 twl
->otg
.host
= host
;
393 twl
->otg
.state
= OTG_STATE_UNDEFINED
;
397 static int __devinit
twl6030_usb_probe(struct platform_device
*pdev
)
399 struct twl6030_usb
*twl
;
401 struct twl4030_usb_data
*pdata
;
402 struct device
*dev
= &pdev
->dev
;
403 pdata
= dev
->platform_data
;
405 twl
= kzalloc(sizeof *twl
, GFP_KERNEL
);
409 twl
->dev
= &pdev
->dev
;
410 twl
->irq1
= platform_get_irq(pdev
, 0);
411 twl
->irq2
= platform_get_irq(pdev
, 1);
412 twl
->otg
.dev
= twl
->dev
;
413 twl
->otg
.label
= "twl6030";
414 twl
->otg
.set_host
= twl6030_set_host
;
415 twl
->otg
.set_peripheral
= twl6030_set_peripheral
;
416 twl
->otg
.set_vbus
= twl6030_set_vbus
;
417 twl
->otg
.init
= twl6030_phy_init
;
418 twl
->otg
.shutdown
= twl6030_phy_shutdown
;
419 twl
->otg
.set_suspend
= twl6030_phy_suspend
;
420 twl
->otg
.start_srp
= twl6030_start_srp
;
422 /* init spinlock for workqueue */
423 spin_lock_init(&twl
->lock
);
425 err
= twl6030_usb_ldo_init(twl
);
427 dev_err(&pdev
->dev
, "ldo init failed\n");
431 otg_set_transceiver(&twl
->otg
);
433 platform_set_drvdata(pdev
, twl
);
434 if (device_create_file(&pdev
->dev
, &dev_attr_vbus
))
435 dev_warn(&pdev
->dev
, "could not create sysfs file\n");
437 ATOMIC_INIT_NOTIFIER_HEAD(&twl
->otg
.notifier
);
439 twl
->irq_enabled
= true;
440 status
= request_threaded_irq(twl
->irq1
, NULL
, twl6030_usbotg_irq
,
441 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
444 dev_err(&pdev
->dev
, "can't get IRQ %d, err %d\n",
446 device_remove_file(twl
->dev
, &dev_attr_vbus
);
451 status
= request_threaded_irq(twl
->irq2
, NULL
, twl6030_usb_irq
,
452 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
455 dev_err(&pdev
->dev
, "can't get IRQ %d, err %d\n",
457 free_irq(twl
->irq1
, twl
);
458 device_remove_file(twl
->dev
, &dev_attr_vbus
);
464 pdata
->phy_init(dev
);
465 twl6030_phy_suspend(&twl
->otg
, 0);
466 twl6030_enable_irq(&twl
->otg
);
467 dev_info(&pdev
->dev
, "Initialized TWL6030 USB module\n");
472 static int __exit
twl6030_usb_remove(struct platform_device
*pdev
)
474 struct twl6030_usb
*twl
= platform_get_drvdata(pdev
);
476 struct twl4030_usb_data
*pdata
;
477 struct device
*dev
= &pdev
->dev
;
478 pdata
= dev
->platform_data
;
480 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK
,
482 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK
,
484 free_irq(twl
->irq1
, twl
);
485 free_irq(twl
->irq2
, twl
);
486 regulator_put(twl
->usb3v3
);
487 pdata
->phy_exit(twl
->dev
);
488 device_remove_file(twl
->dev
, &dev_attr_vbus
);
494 static struct platform_driver twl6030_usb_driver
= {
495 .probe
= twl6030_usb_probe
,
496 .remove
= __exit_p(twl6030_usb_remove
),
498 .name
= "twl6030_usb",
499 .owner
= THIS_MODULE
,
503 static int __init
twl6030_usb_init(void)
505 return platform_driver_register(&twl6030_usb_driver
);
507 subsys_initcall(twl6030_usb_init
);
509 static void __exit
twl6030_usb_exit(void)
511 platform_driver_unregister(&twl6030_usb_driver
);
513 module_exit(twl6030_usb_exit
);
515 MODULE_ALIAS("platform:twl6030_usb");
516 MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
517 MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
518 MODULE_LICENSE("GPL");