2 * twl4030_usb - TWL4030 USB transceiver, talking to OMAP OTG controller
4 * Copyright (C) 2004-2007 Texas Instruments
5 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * - HS USB ULPI mode works.
24 * - 3-pin mode support may be added in future.
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/time.h>
30 #include <linux/interrupt.h>
31 #include <linux/platform_device.h>
33 #include <linux/usb.h>
34 #include <linux/usb/ch9.h>
35 #include <linux/usb/gadget.h>
36 #include <linux/usb/otg.h>
37 #include <linux/i2c/twl4030.h>
40 /* Register defines */
42 #define VENDOR_ID_LO 0x00
43 #define VENDOR_ID_HI 0x01
44 #define PRODUCT_ID_LO 0x02
45 #define PRODUCT_ID_HI 0x03
47 #define FUNC_CTRL 0x04
48 #define FUNC_CTRL_SET 0x05
49 #define FUNC_CTRL_CLR 0x06
50 #define FUNC_CTRL_SUSPENDM (1 << 6)
51 #define FUNC_CTRL_RESET (1 << 5)
52 #define FUNC_CTRL_OPMODE_MASK (3 << 3) /* bits 3 and 4 */
53 #define FUNC_CTRL_OPMODE_NORMAL (0 << 3)
54 #define FUNC_CTRL_OPMODE_NONDRIVING (1 << 3)
55 #define FUNC_CTRL_OPMODE_DISABLE_BIT_NRZI (2 << 3)
56 #define FUNC_CTRL_TERMSELECT (1 << 2)
57 #define FUNC_CTRL_XCVRSELECT_MASK (3 << 0) /* bits 0 and 1 */
58 #define FUNC_CTRL_XCVRSELECT_HS (0 << 0)
59 #define FUNC_CTRL_XCVRSELECT_FS (1 << 0)
60 #define FUNC_CTRL_XCVRSELECT_LS (2 << 0)
61 #define FUNC_CTRL_XCVRSELECT_FS4LS (3 << 0)
64 #define IFC_CTRL_SET 0x08
65 #define IFC_CTRL_CLR 0x09
66 #define IFC_CTRL_INTERFACE_PROTECT_DISABLE (1 << 7)
67 #define IFC_CTRL_AUTORESUME (1 << 4)
68 #define IFC_CTRL_CLOCKSUSPENDM (1 << 3)
69 #define IFC_CTRL_CARKITMODE (1 << 2)
70 #define IFC_CTRL_FSLSSERIALMODE_3PIN (1 << 1)
72 #define TWL4030_OTG_CTRL 0x0A
73 #define TWL4030_OTG_CTRL_SET 0x0B
74 #define TWL4030_OTG_CTRL_CLR 0x0C
75 #define TWL4030_OTG_CTRL_DRVVBUS (1 << 5)
76 #define TWL4030_OTG_CTRL_CHRGVBUS (1 << 4)
77 #define TWL4030_OTG_CTRL_DISCHRGVBUS (1 << 3)
78 #define TWL4030_OTG_CTRL_DMPULLDOWN (1 << 2)
79 #define TWL4030_OTG_CTRL_DPPULLDOWN (1 << 1)
80 #define TWL4030_OTG_CTRL_IDPULLUP (1 << 0)
82 #define USB_INT_EN_RISE 0x0D
83 #define USB_INT_EN_RISE_SET 0x0E
84 #define USB_INT_EN_RISE_CLR 0x0F
85 #define USB_INT_EN_FALL 0x10
86 #define USB_INT_EN_FALL_SET 0x11
87 #define USB_INT_EN_FALL_CLR 0x12
88 #define USB_INT_STS 0x13
89 #define USB_INT_LATCH 0x14
90 #define USB_INT_IDGND (1 << 4)
91 #define USB_INT_SESSEND (1 << 3)
92 #define USB_INT_SESSVALID (1 << 2)
93 #define USB_INT_VBUSVALID (1 << 1)
94 #define USB_INT_HOSTDISCONNECT (1 << 0)
96 #define CARKIT_CTRL 0x19
97 #define CARKIT_CTRL_SET 0x1A
98 #define CARKIT_CTRL_CLR 0x1B
99 #define CARKIT_CTRL_MICEN (1 << 6)
100 #define CARKIT_CTRL_SPKRIGHTEN (1 << 5)
101 #define CARKIT_CTRL_SPKLEFTEN (1 << 4)
102 #define CARKIT_CTRL_RXDEN (1 << 3)
103 #define CARKIT_CTRL_TXDEN (1 << 2)
104 #define CARKIT_CTRL_IDGNDDRV (1 << 1)
105 #define CARKIT_CTRL_CARKITPWR (1 << 0)
106 #define CARKIT_PLS_CTRL 0x22
107 #define CARKIT_PLS_CTRL_SET 0x23
108 #define CARKIT_PLS_CTRL_CLR 0x24
109 #define CARKIT_PLS_CTRL_SPKRRIGHT_BIASEN (1 << 3)
110 #define CARKIT_PLS_CTRL_SPKRLEFT_BIASEN (1 << 2)
111 #define CARKIT_PLS_CTRL_RXPLSEN (1 << 1)
112 #define CARKIT_PLS_CTRL_TXPLSEN (1 << 0)
114 #define MCPC_CTRL 0x30
115 #define MCPC_CTRL_SET 0x31
116 #define MCPC_CTRL_CLR 0x32
117 #define MCPC_CTRL_RTSOL (1 << 7)
118 #define MCPC_CTRL_EXTSWR (1 << 6)
119 #define MCPC_CTRL_EXTSWC (1 << 5)
120 #define MCPC_CTRL_VOICESW (1 << 4)
121 #define MCPC_CTRL_OUT64K (1 << 3)
122 #define MCPC_CTRL_RTSCTSSW (1 << 2)
123 #define MCPC_CTRL_HS_UART (1 << 0)
125 #define MCPC_IO_CTRL 0x33
126 #define MCPC_IO_CTRL_SET 0x34
127 #define MCPC_IO_CTRL_CLR 0x35
128 #define MCPC_IO_CTRL_MICBIASEN (1 << 5)
129 #define MCPC_IO_CTRL_CTS_NPU (1 << 4)
130 #define MCPC_IO_CTRL_RXD_PU (1 << 3)
131 #define MCPC_IO_CTRL_TXDTYP (1 << 2)
132 #define MCPC_IO_CTRL_CTSTYP (1 << 1)
133 #define MCPC_IO_CTRL_RTSTYP (1 << 0)
135 #define MCPC_CTRL2 0x36
136 #define MCPC_CTRL2_SET 0x37
137 #define MCPC_CTRL2_CLR 0x38
138 #define MCPC_CTRL2_MCPC_CK_EN (1 << 0)
140 #define OTHER_FUNC_CTRL 0x80
141 #define OTHER_FUNC_CTRL_SET 0x81
142 #define OTHER_FUNC_CTRL_CLR 0x82
143 #define OTHER_FUNC_CTRL_BDIS_ACON_EN (1 << 4)
144 #define OTHER_FUNC_CTRL_FIVEWIRE_MODE (1 << 2)
146 #define OTHER_IFC_CTRL 0x83
147 #define OTHER_IFC_CTRL_SET 0x84
148 #define OTHER_IFC_CTRL_CLR 0x85
149 #define OTHER_IFC_CTRL_OE_INT_EN (1 << 6)
150 #define OTHER_IFC_CTRL_CEA2011_MODE (1 << 5)
151 #define OTHER_IFC_CTRL_FSLSSERIALMODE_4PIN (1 << 4)
152 #define OTHER_IFC_CTRL_HIZ_ULPI_60MHZ_OUT (1 << 3)
153 #define OTHER_IFC_CTRL_HIZ_ULPI (1 << 2)
154 #define OTHER_IFC_CTRL_ALT_INT_REROUTE (1 << 0)
156 #define OTHER_INT_EN_RISE 0x86
157 #define OTHER_INT_EN_RISE_SET 0x87
158 #define OTHER_INT_EN_RISE_CLR 0x88
159 #define OTHER_INT_EN_FALL 0x89
160 #define OTHER_INT_EN_FALL_SET 0x8A
161 #define OTHER_INT_EN_FALL_CLR 0x8B
162 #define OTHER_INT_STS 0x8C
163 #define OTHER_INT_LATCH 0x8D
164 #define OTHER_INT_VB_SESS_VLD (1 << 7)
165 #define OTHER_INT_DM_HI (1 << 6) /* not valid for "latch" reg */
166 #define OTHER_INT_DP_HI (1 << 5) /* not valid for "latch" reg */
167 #define OTHER_INT_BDIS_ACON (1 << 3) /* not valid for "fall" regs */
168 #define OTHER_INT_MANU (1 << 1)
169 #define OTHER_INT_ABNORMAL_STRESS (1 << 0)
171 #define ID_STATUS 0x96
172 #define ID_RES_FLOAT (1 << 4)
173 #define ID_RES_440K (1 << 3)
174 #define ID_RES_200K (1 << 2)
175 #define ID_RES_102K (1 << 1)
176 #define ID_RES_GND (1 << 0)
178 #define POWER_CTRL 0xAC
179 #define POWER_CTRL_SET 0xAD
180 #define POWER_CTRL_CLR 0xAE
181 #define POWER_CTRL_OTG_ENAB (1 << 5)
183 #define OTHER_IFC_CTRL2 0xAF
184 #define OTHER_IFC_CTRL2_SET 0xB0
185 #define OTHER_IFC_CTRL2_CLR 0xB1
186 #define OTHER_IFC_CTRL2_ULPI_STP_LOW (1 << 4)
187 #define OTHER_IFC_CTRL2_ULPI_TXEN_POL (1 << 3)
188 #define OTHER_IFC_CTRL2_ULPI_4PIN_2430 (1 << 2)
189 #define OTHER_IFC_CTRL2_USB_INT_OUTSEL_MASK (3 << 0) /* bits 0 and 1 */
190 #define OTHER_IFC_CTRL2_USB_INT_OUTSEL_INT1N (0 << 0)
191 #define OTHER_IFC_CTRL2_USB_INT_OUTSEL_INT2N (1 << 0)
193 #define REG_CTRL_EN 0xB2
194 #define REG_CTRL_EN_SET 0xB3
195 #define REG_CTRL_EN_CLR 0xB4
196 #define REG_CTRL_ERROR 0xB5
197 #define ULPI_I2C_CONFLICT_INTEN (1 << 0)
199 #define OTHER_FUNC_CTRL2 0xB8
200 #define OTHER_FUNC_CTRL2_SET 0xB9
201 #define OTHER_FUNC_CTRL2_CLR 0xBA
202 #define OTHER_FUNC_CTRL2_VBAT_TIMER_EN (1 << 0)
204 /* following registers do not have separate _clr and _set registers */
205 #define VBUS_DEBOUNCE 0xC0
206 #define ID_DEBOUNCE 0xC1
207 #define VBAT_TIMER 0xD3
208 #define PHY_PWR_CTRL 0xFD
209 #define PHY_PWR_PHYPWD (1 << 0)
210 #define PHY_CLK_CTRL 0xFE
211 #define PHY_CLK_CTRL_CLOCKGATING_EN (1 << 2)
212 #define PHY_CLK_CTRL_CLK32K_EN (1 << 1)
213 #define REQ_PHY_DPLL_CLK (1 << 0)
214 #define PHY_CLK_CTRL_STS 0xFF
215 #define PHY_DPLL_CLK (1 << 0)
217 /* In module TWL4030_MODULE_PM_MASTER */
218 #define PROTECT_KEY 0x0E
220 /* In module TWL4030_MODULE_PM_RECEIVER */
221 #define VUSB_DEDICATED1 0x7D
222 #define VUSB_DEDICATED2 0x7E
223 #define VUSB1V5_DEV_GRP 0x71
224 #define VUSB1V5_TYPE 0x72
225 #define VUSB1V5_REMAP 0x73
226 #define VUSB1V8_DEV_GRP 0x74
227 #define VUSB1V8_TYPE 0x75
228 #define VUSB1V8_REMAP 0x76
229 #define VUSB3V1_DEV_GRP 0x77
230 #define VUSB3V1_TYPE 0x78
231 #define VUSB3V1_REMAP 0x79
233 /* In module TWL4030_MODULE_INTBR */
235 #define GPIO_USB_4PIN_ULPI_2430C (3 << 0)
237 /* In module TWL4030_MODULE_INT */
238 #define REG_PWR_ISR1 0x00
239 #define REG_PWR_IMR1 0x01
240 #define USB_PRES (1 << 2)
241 #define REG_PWR_EDR1 0x05
242 #define USB_PRES_FALLING (1 << 4)
243 #define USB_PRES_RISING (1 << 5)
244 #define REG_PWR_SIH_CTRL 0x07
247 /* bits in OTG_CTRL */
248 #define OTG_XCEIV_OUTPUTS \
249 (OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
250 #define OTG_XCEIV_INPUTS \
251 (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
252 #define OTG_CTRL_BITS \
253 (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
254 /* and OTG_PULLUP is sometimes written */
256 #define OTG_CTRL_MASK (OTG_DRIVER_SEL| \
257 OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
262 struct otg_transceiver otg;
265 /* pin configuration */
266 enum twl4030_usb_mode usb_mode;
271 /* internal define on top of container_of */
272 #define xceiv_to_twl(x) container_of((x), struct twl4030_usb, otg);
274 /*-------------------------------------------------------------------------*/
276 static int twl4030_i2c_write_u8_verify(struct twl4030_usb *twl,
277 u8 module, u8 data, u8 address)
281 if ((twl4030_i2c_write_u8(module, data, address) >= 0) &&
282 (twl4030_i2c_read_u8(module, &check, address) >= 0) &&
285 /* Failed once: Try again */
286 if ((twl4030_i2c_write_u8(module, data, address) >= 0) &&
287 (twl4030_i2c_read_u8(module, &check, address) >= 0) &&
290 /* Failed again: Return error */
295 #define twl4030_usb_write_verify(twl, address, data) \
296 twl4030_i2c_write_u8_verify(twl, TWL4030_MODULE_USB, (data), (address))
298 static inline int twl4030_usb_write(struct twl4030_usb *twl,
303 ret = twl4030_i2c_write_u8(TWL4030_MODULE_USB, data, address);
307 if (twl4030_i2c_read_u8(TWL4030_MODULE_USB, &data1,
309 dev_err(twl->dev, "re-read failed\n");
312 "Write %s wrote %x read %x from reg %x\n",
313 (data1 == data) ? "succeed" : "mismatch",
314 data, data1, address);
318 "TWL4030:USB:Write[0x%x] Error %d\n", address, ret);
324 static inline int twl4030_usb_read(struct twl4030_usb *twl, u8 address)
329 ret = twl4030_i2c_read_u8(TWL4030_MODULE_USB, &data, address);
334 "TWL4030:USB:Read[0x%x] Error %d\n", address, ret);
339 /*-------------------------------------------------------------------------*/
342 twl4030_usb_set_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
344 return twl4030_usb_write(twl, reg + 1, bits);
348 twl4030_usb_clear_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
350 return twl4030_usb_write(twl, reg + 2, bits);
353 /*-------------------------------------------------------------------------*/
355 static void twl4030_usb_set_mode(struct twl4030_usb *twl, int mode)
357 twl->usb_mode = mode;
360 case T2_USB_MODE_ULPI:
361 twl4030_usb_clear_bits(twl, IFC_CTRL, IFC_CTRL_CARKITMODE);
362 twl4030_usb_set_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
363 twl4030_usb_clear_bits(twl, FUNC_CTRL,
364 FUNC_CTRL_XCVRSELECT_MASK |
365 FUNC_CTRL_OPMODE_MASK);
368 case T2_USB_MODE_CEA2011_3PIN:
369 twl4030_cea2011_3_pin_FS_setup(twl);
373 /* FIXME: power on defaults */
378 static void twl4030_i2c_access(struct twl4030_usb *twl, int on)
380 unsigned long timeout;
381 int val = twl4030_usb_read(twl, PHY_CLK_CTRL);
385 /* enable DPLL to access PHY registers over I2C */
386 val |= REQ_PHY_DPLL_CLK;
387 WARN_ON(twl4030_usb_write_verify(twl, PHY_CLK_CTRL,
390 timeout = jiffies + HZ;
391 while (!(twl4030_usb_read(twl, PHY_CLK_CTRL_STS) &
393 && time_before(jiffies, timeout))
395 if (!(twl4030_usb_read(twl, PHY_CLK_CTRL_STS) &
397 dev_err(twl->dev, "Timeout setting T2 HSUSB "
400 /* let ULPI control the DPLL clock */
401 val &= ~REQ_PHY_DPLL_CLK;
402 WARN_ON(twl4030_usb_write_verify(twl, PHY_CLK_CTRL,
408 static void usb_irq_enable(struct twl4030_usb *twl, int rising, int falling)
413 WARN_ON(twl4030_i2c_read_u8(TWL4030_MODULE_INT,
414 &val, REG_PWR_EDR1) < 0);
416 val &= ~(USB_PRES_RISING | USB_PRES_FALLING);
418 val = val | USB_PRES_RISING;
420 val = val | USB_PRES_FALLING;
421 WARN_ON(twl4030_i2c_write_u8_verify(twl, TWL4030_MODULE_INT,
422 val, REG_PWR_EDR1) < 0);
424 /* un-mask interrupt */
425 WARN_ON(twl4030_i2c_read_u8(TWL4030_MODULE_INT,
426 &val, REG_PWR_IMR1) < 0);
430 WARN_ON(twl4030_i2c_write_u8_verify(twl, TWL4030_MODULE_INT,
431 val, REG_PWR_IMR1) < 0);
434 static void usb_irq_disable(struct twl4030_usb *twl)
438 /* undo edge setup */
439 WARN_ON(twl4030_i2c_read_u8(TWL4030_MODULE_INT,
440 &val, REG_PWR_EDR1) < 0);
441 val &= ~(USB_PRES_RISING | USB_PRES_FALLING);
442 WARN_ON(twl4030_i2c_write_u8_verify(twl, TWL4030_MODULE_INT,
443 val, REG_PWR_EDR1) < 0);
446 WARN_ON(twl4030_i2c_read_u8(TWL4030_MODULE_INT,
447 &val, REG_PWR_IMR1) < 0);
450 WARN_ON(twl4030_i2c_write_u8_verify(twl, TWL4030_MODULE_INT,
451 val, REG_PWR_IMR1) < 0);
454 static void twl4030_phy_power(struct twl4030_usb *twl, int on)
458 pwr = twl4030_usb_read(twl, PHY_PWR_CTRL);
460 pwr &= ~PHY_PWR_PHYPWD;
461 WARN_ON(twl4030_usb_write_verify(twl, PHY_PWR_CTRL, pwr) < 0);
462 twl4030_usb_write(twl, PHY_CLK_CTRL,
463 twl4030_usb_read(twl, PHY_CLK_CTRL) |
464 (PHY_CLK_CTRL_CLOCKGATING_EN |
465 PHY_CLK_CTRL_CLK32K_EN));
467 pwr |= PHY_PWR_PHYPWD;
468 WARN_ON(twl4030_usb_write_verify(twl, PHY_PWR_CTRL, pwr) < 0);
472 static void twl4030_phy_suspend(struct twl4030_usb *twl, int controller_off)
475 usb_irq_disable(twl);
481 /* enable rising edge interrupt to detect cable attach */
482 usb_irq_enable(twl, 1, 0);
484 twl4030_phy_power(twl, 0);
488 static void twl4030_phy_resume(struct twl4030_usb *twl)
493 /* enable falling edge interrupt to detect cable detach */
494 usb_irq_enable(twl, 0, 1);
496 twl4030_phy_power(twl, 1);
497 twl4030_i2c_access(twl, 1);
498 twl4030_usb_set_mode(twl, twl->usb_mode);
499 if (twl->usb_mode == T2_USB_MODE_ULPI)
500 twl4030_i2c_access(twl, 0);
504 static void twl4030_usb_ldo_init(struct twl4030_usb *twl)
506 /* Enable writing to power configuration registers */
507 twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0xC0, PROTECT_KEY);
508 twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0x0C, PROTECT_KEY);
510 /* put VUSB3V1 LDO in active state */
511 twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB_DEDICATED2);
513 /* input to VUSB3V1 LDO is from VBAT, not VBUS */
514 twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x14, VUSB_DEDICATED1);
516 /* turn on 3.1V regulator */
517 twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB3V1_DEV_GRP);
518 twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB3V1_TYPE);
520 /* turn on 1.5V regulator */
521 twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB1V5_DEV_GRP);
522 twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V5_TYPE);
524 /* turn on 1.8V regulator */
525 twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB1V8_DEV_GRP);
526 twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V8_TYPE);
528 /* disable access to power configuration registers */
529 twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0, PROTECT_KEY);
532 static irqreturn_t twl4030_usb_irq(int irq, void *_twl)
534 struct twl4030_usb *twl = _twl;
537 /* action based on cable attach or detach */
538 WARN_ON(twl4030_i2c_read_u8(TWL4030_MODULE_INT,
539 &val, REG_PWR_EDR1) < 0);
541 if (val & USB_PRES_RISING) {
542 twl4030_phy_resume(twl);
543 twl4030charger_usb_en(1);
545 twl4030charger_usb_en(0);
546 twl4030_phy_suspend(twl, 0);
552 static int twl4030_set_suspend(struct otg_transceiver *x, int suspend)
554 struct twl4030_usb *twl = xceiv_to_twl(x);
557 twl4030_phy_suspend(twl, 1);
559 twl4030_phy_resume(twl);
564 static int twl4030_set_peripheral(struct otg_transceiver *x,
565 struct usb_gadget *gadget)
567 struct twl4030_usb *twl;
573 twl = xceiv_to_twl(x);
576 omap_writew(0, OTG_IRQ_EN);
577 twl4030_phy_suspend(twl, 1);
578 twl->otg.gadget = NULL;
583 twl->otg.gadget = gadget;
584 twl4030_phy_resume(twl);
586 l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
587 l &= ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS);
589 omap_writel(l, OTG_CTRL);
591 twl->otg.state = OTG_STATE_B_IDLE;
593 twl4030_usb_set_bits(twl, USB_INT_EN_RISE,
594 USB_INT_SESSVALID | USB_INT_VBUSVALID);
595 twl4030_usb_set_bits(twl, USB_INT_EN_FALL,
596 USB_INT_SESSVALID | USB_INT_VBUSVALID);
601 static int twl4030_set_host(struct otg_transceiver *x, struct usb_bus *host)
603 struct twl4030_usb *twl;
608 twl = xceiv_to_twl(x);
611 omap_writew(0, OTG_IRQ_EN);
612 twl4030_phy_suspend(twl, 1);
613 twl->otg.host = NULL;
618 twl->otg.host = host;
619 twl4030_phy_resume(twl);
621 twl4030_usb_set_bits(twl, TWL4030_OTG_CTRL,
622 TWL4030_OTG_CTRL_DMPULLDOWN
623 | TWL4030_OTG_CTRL_DPPULLDOWN);
624 twl4030_usb_set_bits(twl, USB_INT_EN_RISE, USB_INT_IDGND);
625 twl4030_usb_set_bits(twl, USB_INT_EN_FALL, USB_INT_IDGND);
626 twl4030_usb_set_bits(twl, FUNC_CTRL, FUNC_CTRL_SUSPENDM);
627 twl4030_usb_set_bits(twl, TWL4030_OTG_CTRL, TWL4030_OTG_CTRL_DRVVBUS);
632 static int __init twl4030_usb_probe(struct platform_device *pdev)
634 struct twl4030_usb_data *pdata = pdev->dev.platform_data;
635 struct twl4030_usb *twl;
638 twl = kzalloc(sizeof *twl, GFP_KERNEL);
643 dev_info(&pdev->dev, "platform_data not available\n");
647 twl->dev = &pdev->dev;
648 twl->irq = TWL4030_PWRIRQ_USB_PRES;
649 twl->otg.set_host = twl4030_set_host;
650 twl->otg.set_peripheral = twl4030_set_peripheral;
651 twl->otg.set_suspend = twl4030_set_suspend;
652 twl->usb_mode = pdata->usb_mode;
654 usb_irq_disable(twl);
655 status = request_irq(twl->irq, twl4030_usb_irq, 0, "twl4030_usb", twl);
657 dev_dbg(&pdev->dev, "can't get IRQ %d, err %d\n",
664 twl4030_usb_ldo_init(twl);
665 twl4030_phy_power(twl, 1);
666 twl4030_i2c_access(twl, 1);
667 twl4030_usb_set_mode(twl, twl->usb_mode);
671 if (twl->usb_mode == T2_USB_MODE_ULPI) {
672 twl4030_i2c_access(twl, 0);
673 twl4030_phy_suspend(twl, 0);
676 otg_set_transceiver(&twl->otg);
677 platform_set_drvdata(pdev, twl);
678 dev_info(&pdev->dev, "Initialized TWL4030 USB module\n");
683 static int __exit twl4030_usb_remove(struct platform_device *pdev)
685 struct twl4030_usb *twl = platform_get_drvdata(pdev);
688 usb_irq_disable(twl);
689 free_irq(twl->irq, twl);
691 /* set transceiver mode to power on defaults */
692 twl4030_usb_set_mode(twl, -1);
694 /* autogate 60MHz ULPI clock,
695 * clear dpll clock request for i2c access,
698 val = twl4030_usb_read(twl, PHY_CLK_CTRL);
700 val |= PHY_CLK_CTRL_CLOCKGATING_EN;
701 val &= ~(PHY_CLK_CTRL_CLK32K_EN | REQ_PHY_DPLL_CLK);
702 twl4030_usb_write(twl, PHY_CLK_CTRL, (u8)val);
705 /* disable complete OTG block */
706 twl4030_usb_clear_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
708 twl4030_phy_power(twl, 0);
715 static struct platform_driver twl4030_driver = {
716 .probe = twl4030_usb_probe,
717 .remove = __exit_p(twl4030_remove),
719 .name = "twl4030_usb",
720 .owner = THIS_MODULE,
724 static int __init twl4030_usb_init(void)
726 return platform_driver_register(&twl4030_driver);
728 module_init(twl4030_usb_init);
730 static void __exit twl4030_usb_exit(void)
732 platform_driver_unregister(&twl4030_driver);
734 module_exit(twl4030_usb_exit);
736 MODULE_ALIAS("platform:twl4030_usb");
737 MODULE_AUTHOR("Texas Instruments, Inc, Nokia Corporation");
738 MODULE_DESCRIPTION("TWL4030 USB transceiver driver");
739 MODULE_LICENSE("GPL");