]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-usb.c
I2C: TWL4030: Move twl4030's headers under include/linux/i2c/
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / twl4030-usb.c
1 /*
2  * twl4030_usb - TWL4030 USB transceiver, talking to OMAP OTG controller
3  *
4  * Copyright (C) 2004-2007 Texas Instruments
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 as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * Current status:
21  *      - HS USB ULPI mode works.
22  *      - 3-pin mode support may be added in future.
23  */
24
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/time.h>
29 #include <linux/interrupt.h>
30 #include <linux/usb.h>
31
32 #include <linux/i2c/twl4030.h>
33
34 /* Register defines */
35
36 #define VENDOR_ID_LO                    0x00
37 #define VENDOR_ID_HI                    0x01
38 #define PRODUCT_ID_LO                   0x02
39 #define PRODUCT_ID_HI                   0x03
40
41 #define FUNC_CTRL                       0x04
42 #define FUNC_CTRL_SET                   0x05
43 #define FUNC_CTRL_CLR                   0x06
44 #define FUNC_CTRL_SUSPENDM              (1 << 6)
45 #define FUNC_CTRL_RESET                 (1 << 5)
46 #define FUNC_CTRL_OPMODE_MASK           (3 << 3) /* bits 3 and 4 */
47 #define FUNC_CTRL_OPMODE_NORMAL         (0 << 3)
48 #define FUNC_CTRL_OPMODE_NONDRIVING     (1 << 3)
49 #define FUNC_CTRL_OPMODE_DISABLE_BIT_NRZI       (2 << 3)
50 #define FUNC_CTRL_TERMSELECT            (1 << 2)
51 #define FUNC_CTRL_XCVRSELECT_MASK       (3 << 0) /* bits 0 and 1 */
52 #define FUNC_CTRL_XCVRSELECT_HS         (0 << 0)
53 #define FUNC_CTRL_XCVRSELECT_FS         (1 << 0)
54 #define FUNC_CTRL_XCVRSELECT_LS         (2 << 0)
55 #define FUNC_CTRL_XCVRSELECT_FS4LS      (3 << 0)
56
57 #define IFC_CTRL                        0x07
58 #define IFC_CTRL_SET                    0x08
59 #define IFC_CTRL_CLR                    0x09
60 #define IFC_CTRL_INTERFACE_PROTECT_DISABLE      (1 << 7)
61 #define IFC_CTRL_AUTORESUME             (1 << 4)
62 #define IFC_CTRL_CLOCKSUSPENDM          (1 << 3)
63 #define IFC_CTRL_CARKITMODE             (1 << 2)
64 #define IFC_CTRL_FSLSSERIALMODE_3PIN    (1 << 1)
65
66 #define OTG_CTRL                        0x0A
67 #define OTG_CTRL_SET                    0x0B
68 #define OTG_CTRL_CLR                    0x0C
69 #define OTG_CTRL_DRVVBUS                (1 << 5)
70 #define OTG_CTRL_CHRGVBUS               (1 << 4)
71 #define OTG_CTRL_DISCHRGVBUS            (1 << 3)
72 #define OTG_CTRL_DMPULLDOWN             (1 << 2)
73 #define OTG_CTRL_DPPULLDOWN             (1 << 1)
74 #define OTG_CTRL_IDPULLUP               (1 << 0)
75
76 #define USB_INT_EN_RISE                 0x0D
77 #define USB_INT_EN_RISE_SET             0x0E
78 #define USB_INT_EN_RISE_CLR             0x0F
79 #define USB_INT_EN_FALL                 0x10
80 #define USB_INT_EN_FALL_SET             0x11
81 #define USB_INT_EN_FALL_CLR             0x12
82 #define USB_INT_STS                     0x13
83 #define USB_INT_LATCH                   0x14
84 #define USB_INT_IDGND                   (1 << 4)
85 #define USB_INT_SESSEND                 (1 << 3)
86 #define USB_INT_SESSVALID               (1 << 2)
87 #define USB_INT_VBUSVALID               (1 << 1)
88 #define USB_INT_HOSTDISCONNECT          (1 << 0)
89
90 #define CARKIT_CTRL                     0x19
91 #define CARKIT_CTRL_SET                 0x1A
92 #define CARKIT_CTRL_CLR                 0x1B
93 #define CARKIT_CTRL_MICEN               (1 << 6)
94 #define CARKIT_CTRL_SPKRIGHTEN          (1 << 5)
95 #define CARKIT_CTRL_SPKLEFTEN           (1 << 4)
96 #define CARKIT_CTRL_RXDEN               (1 << 3)
97 #define CARKIT_CTRL_TXDEN               (1 << 2)
98 #define CARKIT_CTRL_IDGNDDRV            (1 << 1)
99 #define CARKIT_CTRL_CARKITPWR           (1 << 0)
100 #define CARKIT_PLS_CTRL                 0x22
101 #define CARKIT_PLS_CTRL_SET             0x23
102 #define CARKIT_PLS_CTRL_CLR             0x24
103 #define CARKIT_PLS_CTRL_SPKRRIGHT_BIASEN        (1 << 3)
104 #define CARKIT_PLS_CTRL_SPKRLEFT_BIASEN (1 << 2)
105 #define CARKIT_PLS_CTRL_RXPLSEN         (1 << 1)
106 #define CARKIT_PLS_CTRL_TXPLSEN         (1 << 0)
107
108 #define MCPC_CTRL                       0x30
109 #define MCPC_CTRL_SET                   0x31
110 #define MCPC_CTRL_CLR                   0x32
111 #define MCPC_CTRL_RTSOL                 (1 << 7)
112 #define MCPC_CTRL_EXTSWR                (1 << 6)
113 #define MCPC_CTRL_EXTSWC                (1 << 5)
114 #define MCPC_CTRL_VOICESW               (1 << 4)
115 #define MCPC_CTRL_OUT64K                (1 << 3)
116 #define MCPC_CTRL_RTSCTSSW              (1 << 2)
117 #define MCPC_CTRL_HS_UART               (1 << 0)
118
119 #define MCPC_IO_CTRL                    0x33
120 #define MCPC_IO_CTRL_SET                0x34
121 #define MCPC_IO_CTRL_CLR                0x35
122 #define MCPC_IO_CTRL_MICBIASEN          (1 << 5)
123 #define MCPC_IO_CTRL_CTS_NPU            (1 << 4)
124 #define MCPC_IO_CTRL_RXD_PU             (1 << 3)
125 #define MCPC_IO_CTRL_TXDTYP             (1 << 2)
126 #define MCPC_IO_CTRL_CTSTYP             (1 << 1)
127 #define MCPC_IO_CTRL_RTSTYP             (1 << 0)
128
129 #define MCPC_CTRL2                      0x36
130 #define MCPC_CTRL2_SET                  0x37
131 #define MCPC_CTRL2_CLR                  0x38
132 #define MCPC_CTRL2_MCPC_CK_EN           (1 << 0)
133
134 #define OTHER_FUNC_CTRL                 0x80
135 #define OTHER_FUNC_CTRL_SET             0x81
136 #define OTHER_FUNC_CTRL_CLR             0x82
137 #define OTHER_FUNC_CTRL_BDIS_ACON_EN    (1 << 4)
138 #define OTHER_FUNC_CTRL_FIVEWIRE_MODE   (1 << 2)
139
140 #define OTHER_IFC_CTRL                  0x83
141 #define OTHER_IFC_CTRL_SET              0x84
142 #define OTHER_IFC_CTRL_CLR              0x85
143 #define OTHER_IFC_CTRL_OE_INT_EN        (1 << 6)
144 #define OTHER_IFC_CTRL_CEA2011_MODE     (1 << 5)
145 #define OTHER_IFC_CTRL_FSLSSERIALMODE_4PIN      (1 << 4)
146 #define OTHER_IFC_CTRL_HIZ_ULPI_60MHZ_OUT       (1 << 3)
147 #define OTHER_IFC_CTRL_HIZ_ULPI         (1 << 2)
148 #define OTHER_IFC_CTRL_ALT_INT_REROUTE  (1 << 0)
149
150 #define OTHER_INT_EN_RISE               0x86
151 #define OTHER_INT_EN_RISE_SET           0x87
152 #define OTHER_INT_EN_RISE_CLR           0x88
153 #define OTHER_INT_EN_FALL               0x89
154 #define OTHER_INT_EN_FALL_SET           0x8A
155 #define OTHER_INT_EN_FALL_CLR           0x8B
156 #define OTHER_INT_STS                   0x8C
157 #define OTHER_INT_LATCH                 0x8D
158 #define OTHER_INT_VB_SESS_VLD           (1 << 7)
159 #define OTHER_INT_DM_HI                 (1 << 6) /* not valid for "latch" reg */
160 #define OTHER_INT_DP_HI                 (1 << 5) /* not valid for "latch" reg */
161 #define OTHER_INT_BDIS_ACON             (1 << 3) /* not valid for "fall" regs */
162 #define OTHER_INT_MANU                  (1 << 1)
163 #define OTHER_INT_ABNORMAL_STRESS       (1 << 0)
164
165 #define ID_STATUS                       0x96
166 #define ID_RES_FLOAT                    (1 << 4)
167 #define ID_RES_440K                     (1 << 3)
168 #define ID_RES_200K                     (1 << 2)
169 #define ID_RES_102K                     (1 << 1)
170 #define ID_RES_GND                      (1 << 0)
171
172 #define POWER_CTRL                      0xAC
173 #define POWER_CTRL_SET                  0xAD
174 #define POWER_CTRL_CLR                  0xAE
175 #define POWER_CTRL_OTG_ENAB             (1 << 5)
176
177 #define OTHER_IFC_CTRL2                 0xAF
178 #define OTHER_IFC_CTRL2_SET             0xB0
179 #define OTHER_IFC_CTRL2_CLR             0xB1
180 #define OTHER_IFC_CTRL2_ULPI_STP_LOW    (1 << 4)
181 #define OTHER_IFC_CTRL2_ULPI_TXEN_POL   (1 << 3)
182 #define OTHER_IFC_CTRL2_ULPI_4PIN_2430  (1 << 2)
183 #define OTHER_IFC_CTRL2_USB_INT_OUTSEL_MASK     (3 << 0) /* bits 0 and 1 */
184 #define OTHER_IFC_CTRL2_USB_INT_OUTSEL_INT1N    (0 << 0)
185 #define OTHER_IFC_CTRL2_USB_INT_OUTSEL_INT2N    (1 << 0)
186
187 #define REG_CTRL_EN                     0xB2
188 #define REG_CTRL_EN_SET                 0xB3
189 #define REG_CTRL_EN_CLR                 0xB4
190 #define REG_CTRL_ERROR                  0xB5
191 #define ULPI_I2C_CONFLICT_INTEN         (1 << 0)
192
193 #define OTHER_FUNC_CTRL2                0xB8
194 #define OTHER_FUNC_CTRL2_SET            0xB9
195 #define OTHER_FUNC_CTRL2_CLR            0xBA
196 #define OTHER_FUNC_CTRL2_VBAT_TIMER_EN  (1 << 0)
197
198 /* following registers do not have separate _clr and _set registers */
199 #define VBUS_DEBOUNCE                   0xC0
200 #define ID_DEBOUNCE                     0xC1
201 #define VBAT_TIMER                      0xD3
202 #define PHY_PWR_CTRL                    0xFD
203 #define PHY_PWR_PHYPWD                  (1 << 0)
204 #define PHY_CLK_CTRL                    0xFE
205 #define PHY_CLK_CTRL_CLOCKGATING_EN     (1 << 2)
206 #define PHY_CLK_CTRL_CLK32K_EN          (1 << 1)
207 #define REQ_PHY_DPLL_CLK                (1 << 0)
208 #define PHY_CLK_CTRL_STS                0xFF
209 #define PHY_DPLL_CLK                    (1 << 0)
210
211 /* In module TWL4030_MODULE_PM_MASTER */
212 #define PROTECT_KEY                     0x0E
213
214 /* In module TWL4030_MODULE_PM_RECEIVER */
215 #define VUSB_DEDICATED1                 0x7D
216 #define VUSB_DEDICATED2                 0x7E
217 #define VUSB1V5_DEV_GRP                 0x71
218 #define VUSB1V5_TYPE                    0x72
219 #define VUSB1V5_REMAP                   0x73
220 #define VUSB1V8_DEV_GRP                 0x74
221 #define VUSB1V8_TYPE                    0x75
222 #define VUSB1V8_REMAP                   0x76
223 #define VUSB3V1_DEV_GRP                 0x77
224 #define VUSB3V1_TYPE                    0x78
225 #define VUSB3V1_REMAP                   0x79
226
227 #define ID_STATUS                       0x96
228 #define ID_RES_FLOAT                    (1 << 4) /* mini-B */
229 #define ID_RES_440K                     (1 << 3) /* type 2 charger */
230 #define ID_RES_200K                     (1 << 2) /* 5-wire carkit or
231                                                     type 1 charger */
232 #define ID_RES_102K                     (1 << 1) /* phone */
233 #define ID_RES_GND                      (1 << 0) /* mini-A */
234
235 /* In module TWL4030_MODULE_INTBR */
236 #define PMBR1                           0x0D
237 #define GPIO_USB_4PIN_ULPI_2430C        (3 << 0)
238
239 /* In module TWL4030_MODULE_INT */
240 #define REG_PWR_ISR1                    0x00
241 #define REG_PWR_IMR1                    0x01
242 #define USB_PRES                        (1 << 2)
243 #define REG_PWR_EDR1                    0x05
244 #define USB_PRES_FALLING                (1 << 4)
245 #define USB_PRES_RISING                 (1 << 5)
246 #define REG_PWR_SIH_CTRL                0x07
247 #define COR                             (1 << 2)
248
249 /*-------------------------------------------------------------------------*/
250
251 static int twl4030_i2c_write_u8_verify(u8 module, u8 data, u8 address)
252 {
253         u8 check;
254
255         if ((twl4030_i2c_write_u8(module, data, address) >= 0) &&
256             (twl4030_i2c_read_u8(module, &check, address) >= 0) &&
257                                                 (check == data))
258                 return 0;
259         /* Failed once: Try again */
260         if ((twl4030_i2c_write_u8(module, data, address) >= 0) &&
261             (twl4030_i2c_read_u8(module, &check, address) >= 0) &&
262                                                 (check == data))
263                 return 0;
264         /* Failed again: Return error */
265         return -EBUSY;
266 }
267
268 #define twl4030_usb_write_verify(address, data) \
269         twl4030_i2c_write_u8_verify(TWL4030_MODULE_USB, (data), (address))
270
271 static inline int twl4030_usb_write(u8 address, u8 data)
272 {
273         int ret = 0;
274         ret = twl4030_i2c_write_u8(TWL4030_MODULE_USB, data, address);
275         if (ret >= 0) {
276 #if 0   /* debug */
277                 u8 data1;
278                 if (twl4030_i2c_read_u8(TWL4030_MODULE_USB, &data1,
279                                         address) < 0)
280                         printk(KERN_ERR "re-read failed\n");
281                 else
282                         printk(KERN_INFO
283                                "Write %s wrote %x read %x from reg %x\n",
284                                (data1 == data) ? "succeed" : "mismatch",
285                                data, data1, address);
286 #endif
287         } else {
288                 printk(KERN_WARNING
289                         "TWL4030:USB:Write[0x%x] Error %d\n", address, ret);
290         }
291         return ret;
292 }
293
294 static inline int twl4030_usb_read(u8 address)
295 {
296         u8 data;
297         int ret = 0;
298         ret = twl4030_i2c_read_u8(TWL4030_MODULE_USB, &data, address);
299         if (ret >= 0) {
300                 ret = data;
301         } else {
302                 printk(KERN_WARNING
303                         "TWL4030:USB:Read[0x%x] Error %d\n", address, ret);
304         }
305         return ret;
306 }
307
308 /*-------------------------------------------------------------------------*/
309
310 struct twl4030_usb {
311         int                     irq;
312         u8                      usb_mode;       /* pin configuration */
313 #define T2_USB_MODE_ULPI                1
314 /* #define T2_USB_MODE_CEA2011_3PIN     2 */
315         u8                      asleep;
316 };
317
318 static struct twl4030_usb *the_transceiver;
319
320 /*-------------------------------------------------------------------------*/
321
322 static inline int
323 twl4030_usb_set_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
324 {
325         return twl4030_usb_write(reg + 1, bits);
326 }
327
328 static inline int
329 twl4030_usb_clear_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
330 {
331         return twl4030_usb_write(reg + 2, bits);
332 }
333
334 /*-------------------------------------------------------------------------*/
335
336 static void twl4030_usb_set_mode(struct twl4030_usb *twl, int mode)
337 {
338         twl->usb_mode = mode;
339
340         switch (mode) {
341         case T2_USB_MODE_ULPI:
342                 twl4030_usb_clear_bits(twl, IFC_CTRL, IFC_CTRL_CARKITMODE);
343                 twl4030_usb_set_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
344                 twl4030_usb_clear_bits(twl, FUNC_CTRL,
345                                         FUNC_CTRL_XCVRSELECT_MASK |
346                                         FUNC_CTRL_OPMODE_MASK);
347                 break;
348 /*
349         case T2_USB_MODE_CEA2011_3PIN:
350                 twl4030_cea2011_3_pin_FS_setup(twl);
351                 break;
352 */
353         default:
354                 /* FIXME: power on defaults */
355                 break;
356         };
357 }
358
359 #ifdef CONFIG_TWL4030_USB_HS_ULPI
360 static void hs_usb_init(struct twl4030_usb *twl)
361 {
362         twl->usb_mode = T2_USB_MODE_ULPI;
363         return;
364 }
365
366 #endif
367
368 static void twl4030_i2c_access(int on)
369 {
370         unsigned long timeout;
371         int val = twl4030_usb_read(PHY_CLK_CTRL);
372
373         if (val >= 0) {
374                 if (on) {
375                         /* enable DPLL to access PHY registers over I2C */
376                         val |= REQ_PHY_DPLL_CLK;
377                         if (twl4030_usb_write_verify(PHY_CLK_CTRL,
378                                                                 (u8)val) < 0) {
379                                 printk(KERN_ERR "twl4030_usb: i2c write failed,"
380                                                 " line %d\n", __LINE__);
381                                 return;
382                         }
383
384                         timeout = jiffies + HZ;
385                         while (!(twl4030_usb_read(PHY_CLK_CTRL_STS) &
386                                                         PHY_DPLL_CLK)
387                                 && time_before(jiffies, timeout))
388                                         udelay(10);
389                         if (!(twl4030_usb_read(PHY_CLK_CTRL_STS) &
390                                                         PHY_DPLL_CLK))
391                                 printk(KERN_ERR "Timeout setting T2 HSUSB "
392                                                 "PHY DPLL clock\n");
393                 } else {
394                         /* let ULPI control the DPLL clock */
395                         val &= ~REQ_PHY_DPLL_CLK;
396                         if (twl4030_usb_write_verify(PHY_CLK_CTRL,
397                                                                 (u8)val) < 0) {
398                                 printk(KERN_ERR "twl4030_usb: i2c write failed,"
399                                                 " line %d\n", __LINE__);
400                         }
401                 }
402         }
403         return;
404 }
405
406 static void usb_irq_enable(int rising, int falling)
407 {
408         u8 val;
409
410         /* edge setup */
411         if (twl4030_i2c_read_u8(TWL4030_MODULE_INT, &val, REG_PWR_EDR1) < 0) {
412                 printk(KERN_ERR "twl4030_usb: i2c read failed,"
413                                 " line %d\n", __LINE__);
414                 return;
415         }
416         val &= ~(USB_PRES_RISING | USB_PRES_FALLING);
417         if (rising)
418                 val = val | USB_PRES_RISING;
419         if (falling)
420                 val = val | USB_PRES_FALLING;
421         if (twl4030_i2c_write_u8_verify(TWL4030_MODULE_INT, val,
422                                                         REG_PWR_EDR1) < 0) {
423                 printk(KERN_ERR "twl4030_usb: i2c write failed,"
424                                 " line %d\n", __LINE__);
425                 return;
426         }
427
428         /* un-mask interrupt */
429         if (twl4030_i2c_read_u8(TWL4030_MODULE_INT, &val, REG_PWR_IMR1) < 0) {
430                 printk(KERN_ERR "twl4030_usb: i2c read failed,"
431                                 " line %d\n", __LINE__);
432                 return;
433         }
434         val &= ~USB_PRES;
435         if (twl4030_i2c_write_u8_verify(TWL4030_MODULE_INT, val,
436                                                         REG_PWR_IMR1) < 0)
437                 printk(KERN_ERR "twl4030_usb: i2c write failed,"
438                                 " line %d\n", __LINE__);
439
440         return;
441 }
442
443 static void usb_irq_disable(void)
444 {
445         u8 val;
446
447         /* undo edge setup */
448         if (twl4030_i2c_read_u8(TWL4030_MODULE_INT, &val, REG_PWR_EDR1) < 0) {
449                 printk(KERN_ERR "twl4030_usb: i2c read failed,"
450                                 " line %d\n", __LINE__);
451                 return;
452         }
453         val &= ~(USB_PRES_RISING | USB_PRES_FALLING);
454         if (twl4030_i2c_write_u8_verify(TWL4030_MODULE_INT, val,
455                                                         REG_PWR_EDR1) < 0) {
456                 printk(KERN_ERR "twl4030_usb: i2c write failed,"
457                                 " line %d\n", __LINE__);
458                 return;
459         }
460
461         /* mask interrupt */
462         if (twl4030_i2c_read_u8(TWL4030_MODULE_INT, &val, REG_PWR_IMR1) < 0) {
463                 printk(KERN_ERR "twl4030_usb: i2c read failed,"
464                                 " line %d\n", __LINE__);
465                 return;
466         }
467         val |= USB_PRES;
468         if (twl4030_i2c_write_u8_verify(TWL4030_MODULE_INT, val,
469                                                         REG_PWR_IMR1) < 0)
470                 printk(KERN_ERR "twl4030_usb: i2c write failed,"
471                                 " line %d\n", __LINE__);
472
473         return;
474 }
475
476 void twl4030_phy_suspend(int controller_off);
477 void twl4030_phy_resume(void);
478
479 static irqreturn_t twl4030_usb_irq(int irq, void *_twl)
480 {
481         int ret = IRQ_NONE;
482         u8 val;
483
484         if (twl4030_i2c_read_u8(TWL4030_MODULE_INT, &val, REG_PWR_ISR1) < 0) {
485                 printk(KERN_ERR "twl4030_usb: i2c read failed,"
486                                 " line %d\n", __LINE__);
487                 goto done;
488         }
489
490         /* this interrupt line may be shared */
491         if (!(val & USB_PRES))
492                 goto done;
493
494         /* clear the interrupt */
495         twl4030_i2c_write_u8(TWL4030_MODULE_INT, USB_PRES, REG_PWR_ISR1);
496
497         /* action based on cable attach or detach */
498         if (twl4030_i2c_read_u8(TWL4030_MODULE_INT, &val, REG_PWR_EDR1) < 0) {
499                 printk(KERN_ERR "twl4030_usb: i2c read failed,"
500                                 " line %d\n", __LINE__);
501                 goto done;
502         }
503
504         if (val & USB_PRES_RISING)
505                 twl4030_phy_resume();
506         else
507                 twl4030_phy_suspend(0);
508
509         ret = IRQ_HANDLED;
510
511 done:
512         return ret;
513 }
514
515 static void twl4030_phy_power(struct twl4030_usb *twl, int on)
516 {
517         u8 pwr;
518
519         pwr = twl4030_usb_read(PHY_PWR_CTRL);
520         if (on) {
521                 pwr &= ~PHY_PWR_PHYPWD;
522                 if (twl4030_usb_write_verify(PHY_PWR_CTRL, pwr) < 0) {
523                         printk(KERN_ERR "twl4030_usb: i2c write failed,"
524                                         " line %d\n", __LINE__);
525                         return;
526                 }
527                 twl4030_usb_write(PHY_CLK_CTRL,
528                                   twl4030_usb_read(PHY_CLK_CTRL) |
529                                         (PHY_CLK_CTRL_CLOCKGATING_EN |
530                                                 PHY_CLK_CTRL_CLK32K_EN));
531         } else  {
532                 pwr |= PHY_PWR_PHYPWD;
533                 if (twl4030_usb_write_verify(PHY_PWR_CTRL, pwr) < 0) {
534                         printk(KERN_ERR "twl4030_usb: i2c write failed,"
535                                         " line %d\n", __LINE__);
536                 }
537         }
538         return;
539 }
540
541 static void twl4030_usb_ldo_init(struct twl4030_usb *twl)
542 {
543         /* Enable writing to power configuration registers */
544         twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0xC0, PROTECT_KEY);
545         twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0x0C, PROTECT_KEY);
546
547         /* put VUSB3V1 LDO in active state */
548         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB_DEDICATED2);
549
550         /* input to VUSB3V1 LDO is from VBAT, not VBUS */
551         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x14, VUSB_DEDICATED1);
552
553         /* turn on 3.1V regulator */
554         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB3V1_DEV_GRP);
555         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB3V1_TYPE);
556
557         /* turn on 1.5V regulator */
558         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB1V5_DEV_GRP);
559         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V5_TYPE);
560
561         /* turn on 1.8V regulator */
562         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB1V8_DEV_GRP);
563         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V8_TYPE);
564
565         /* disable access to power configuration registers */
566         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, PROTECT_KEY);
567 }
568
569 void twl4030_phy_suspend(int controller_off)
570 {
571         struct twl4030_usb *twl = the_transceiver;
572
573         if (controller_off)
574                 usb_irq_disable();
575
576         if (twl->asleep)
577                 return;
578
579         if (!controller_off)
580                 /* enable rising edge interrupt to detect cable attach */
581                 usb_irq_enable(1, 0);
582
583         twl4030_phy_power(twl, 0);
584         twl->asleep = 1;
585         return;
586 }
587 EXPORT_SYMBOL(twl4030_phy_suspend);
588
589
590 void twl4030_phy_resume(void)
591 {
592         struct twl4030_usb *twl = the_transceiver;
593
594         if (!twl->asleep)
595                 return;
596
597         /* enable falling edge interrupt to detect cable detach */
598         usb_irq_enable(0, 1);
599
600         twl4030_phy_power(twl, 1);
601         twl4030_i2c_access(1);
602         twl4030_usb_set_mode(twl, twl->usb_mode);
603         if (twl->usb_mode == T2_USB_MODE_ULPI)
604                 twl4030_i2c_access(0);
605         twl->asleep = 0;
606         return;
607 }
608 EXPORT_SYMBOL(twl4030_phy_resume);
609
610
611 static int __init twl4030_usb_init(void)
612 {
613         struct twl4030_usb      *twl;
614         int status;
615
616         if (the_transceiver)
617                 return 0;
618
619         twl = kcalloc(1, sizeof *twl, GFP_KERNEL);
620         if (!twl)
621                 return 0;
622
623         the_transceiver = twl;
624
625         twl->irq = TWL4030_MODIRQ_PWR;
626
627         usb_irq_disable();
628         status = request_irq(twl->irq, twl4030_usb_irq,
629                 IRQF_DISABLED | IRQF_SHARED, "twl4030_usb", twl);
630         if (status < 0) {
631                 printk(KERN_DEBUG "can't get IRQ %d, err %d\n",
632                         twl->irq, status);
633                 kfree(twl);
634                 return -ENODEV;
635         }
636
637 #if defined(CONFIG_TWL4030_USB_HS_ULPI)
638         hs_usb_init(twl);
639 #endif
640         twl4030_usb_ldo_init(twl);
641         twl4030_phy_power(twl, 1);
642         twl4030_i2c_access(1);
643         twl4030_usb_set_mode(twl, twl->usb_mode);
644         if (twl->usb_mode == T2_USB_MODE_ULPI)
645                 twl4030_i2c_access(0);
646
647         twl->asleep = 0;
648
649         if (twl->usb_mode == T2_USB_MODE_ULPI)
650                 twl4030_phy_suspend(1);
651
652         printk(KERN_INFO "Initialized TWL4030 USB module");
653
654         return 0;
655 }
656
657
658 static void __exit twl4030_usb_exit(void)
659 {
660         struct twl4030_usb *twl = the_transceiver;
661         int val;
662
663         usb_irq_disable();
664         free_irq(twl->irq, twl);
665
666         /* set transceiver mode to power on defaults */
667         twl4030_usb_set_mode(twl, -1);
668
669         /* autogate 60MHz ULPI clock,
670          * clear dpll clock request for i2c access,
671          * disable 32KHz
672          */
673         val = twl4030_usb_read(PHY_CLK_CTRL);
674         if (val >= 0) {
675                 val |= PHY_CLK_CTRL_CLOCKGATING_EN;
676                 val &= ~(PHY_CLK_CTRL_CLK32K_EN | REQ_PHY_DPLL_CLK);
677                 twl4030_usb_write(PHY_CLK_CTRL, (u8)val);
678         }
679
680         /* disable complete OTG block */
681         twl4030_usb_clear_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
682
683         twl4030_phy_power(twl, 0);
684
685         kfree(twl);
686 }
687
688 subsys_initcall(twl4030_usb_init);
689 module_exit(twl4030_usb_exit);
690
691 MODULE_AUTHOR("Texas Instruments, Inc.");
692 MODULE_DESCRIPTION("TWL4030 USB transceiver driver");
693 MODULE_LICENSE("GPL");