69072c70096c1f12f038fe19d800c43237cf7602
[pandora-kernel.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  * Copyright (C) 2008 Nokia Corporation
6  * Contact: Felipe Balbi <felipe.balbi@nokia.com>
7  *
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.
12  *
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.
17  *
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.
21  *
22  * Current status:
23  *      - HS USB ULPI mode works.
24  *      - 3-pin mode support may be added in future.
25  */
26
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/platform_device.h>
31 #include <linux/spinlock.h>
32 #include <linux/workqueue.h>
33 #include <linux/io.h>
34 #include <linux/delay.h>
35 #include <linux/usb/otg.h>
36 #include <linux/usb/musb.h>
37 #include <linux/i2c/twl4030.h>
38 #include <mach/usb.h>
39
40 /* Register defines */
41
42 #define VENDOR_ID_LO                    0x00
43 #define VENDOR_ID_HI                    0x01
44 #define PRODUCT_ID_LO                   0x02
45 #define PRODUCT_ID_HI                   0x03
46
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)
62
63 #define IFC_CTRL                        0x07
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)
71
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)
81
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)
95
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)
113
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)
124
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)
134
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)
139
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)
145
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)
155
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)
170
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)
177
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)
182
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)
192
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)
198
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)
203
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)
216
217 /* In module TWL4030_MODULE_PM_MASTER */
218 #define PROTECT_KEY                     0x0E
219
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
232
233 /* In module TWL4030_MODULE_INTBR */
234 #define PMBR1                           0x0D
235 #define GPIO_USB_4PIN_ULPI_2430C        (3 << 0)
236
237
238 /* bits in OTG_CTRL */
239 #define OTG_XCEIV_OUTPUTS \
240         (OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
241 #define OTG_XCEIV_INPUTS \
242         (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
243 #define OTG_CTRL_BITS \
244         (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
245         /* and OTG_PULLUP is sometimes written */
246
247 #define OTG_CTRL_MASK   (OTG_DRIVER_SEL| \
248         OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
249         OTG_CTRL_BITS)
250
251
252 enum linkstat {
253         USB_LINK_UNKNOWN = 0,
254         USB_LINK_NONE,
255         USB_LINK_VBUS,
256         USB_LINK_ID,
257 };
258
259 struct twl4030_usb {
260         struct otg_transceiver  otg;
261         struct device           *dev;
262
263         /* for vbus reporting with irqs disabled */
264         spinlock_t              lock;
265
266         /* pin configuration */
267         enum twl4030_usb_mode   usb_mode;
268
269         int                     irq;
270         u8                      linkstat;
271         u8                      asleep;
272         bool                    irq_enabled;
273
274         struct work_struct      change_mode_work;
275 };
276
277 /* internal define on top of container_of */
278 #define xceiv_to_twl(x)         container_of((x), struct twl4030_usb, otg);
279
280 /*-------------------------------------------------------------------------*/
281
282 static int twl4030_i2c_write_u8_verify(struct twl4030_usb *twl,
283                 u8 module, u8 data, u8 address)
284 {
285         u8 check;
286
287         if ((twl4030_i2c_write_u8(module, data, address) >= 0) &&
288             (twl4030_i2c_read_u8(module, &check, address) >= 0) &&
289                                                 (check == data))
290                 return 0;
291         /* Failed once: Try again */
292         if ((twl4030_i2c_write_u8(module, data, address) >= 0) &&
293             (twl4030_i2c_read_u8(module, &check, address) >= 0) &&
294                                                 (check == data))
295                 return 0;
296         /* Failed again: Return error */
297
298         return -EBUSY;
299 }
300
301 #define twl4030_usb_write_verify(twl, address, data)    \
302         twl4030_i2c_write_u8_verify(twl, TWL4030_MODULE_USB, (data), (address))
303
304 static inline int twl4030_usb_write(struct twl4030_usb *twl,
305                 u8 address, u8 data)
306 {
307         int ret = 0;
308
309         ret = twl4030_i2c_write_u8(TWL4030_MODULE_USB, data, address);
310         if (ret >= 0) {
311 #if 0   /* debug */
312                 u8 data1;
313                 if (twl4030_i2c_read_u8(TWL4030_MODULE_USB, &data1,
314                                         address) < 0)
315                         dev_err(twl->dev, "re-read failed\n");
316                 else
317                         dev_dbg(twl->dev,
318                                "Write %s wrote %x read %x from reg %x\n",
319                                (data1 == data) ? "succeed" : "mismatch",
320                                data, data1, address);
321 #endif
322         } else {
323                 dev_warn(twl->dev,
324                         "TWL4030:USB:Write[0x%x] Error %d\n", address, ret);
325         }
326
327         return ret;
328 }
329
330 static inline int twl4030_readb(struct twl4030_usb *twl, u8 module, u8 address)
331 {
332         u8 data;
333         int ret = 0;
334
335         ret = twl4030_i2c_read_u8(module, &data, address);
336         if (ret >= 0)
337                 ret = data;
338         else
339                 dev_warn(twl->dev,
340                         "TWL4030:readb[0x%x,0x%x] Error %d\n",
341                                         module, address, ret);
342
343         return ret;
344 }
345
346 static inline int twl4030_usb_read(struct twl4030_usb *twl, u8 address)
347 {
348         return twl4030_readb(twl, TWL4030_MODULE_USB, address);
349 }
350
351 /*-------------------------------------------------------------------------*/
352
353 static inline int
354 twl4030_usb_set_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
355 {
356         return twl4030_usb_write(twl, reg + 1, bits);
357 }
358
359 static inline int
360 twl4030_usb_clear_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
361 {
362         return twl4030_usb_write(twl, reg + 2, bits);
363 }
364
365 /*-------------------------------------------------------------------------*/
366
367 static enum linkstat twl4030_usb_linkstat(struct twl4030_usb *twl)
368 {
369         int     status;
370         int     linkstat = USB_LINK_UNKNOWN;
371
372         /* STS_HW_CONDITIONS */
373         status = twl4030_readb(twl, TWL4030_MODULE_PM_MASTER, 0x0f);
374         if (status < 0)
375                 dev_err(twl->dev, "USB link status err %d\n", status);
376         else if (status & BIT(2))
377                 linkstat = USB_LINK_ID;
378         else if (status & BIT(7))
379                 linkstat = USB_LINK_VBUS;
380         else
381                 linkstat = USB_LINK_NONE;
382
383         dev_dbg(twl->dev, "HW_CONDITIONS 0x%02x/%d; link %d\n",
384                         status, status, linkstat);
385
386         spin_lock_irq(&twl->lock);
387         twl->linkstat = linkstat;
388         if (linkstat == USB_LINK_ID) {
389                 twl->otg.default_a = true;
390                 twl->otg.state = OTG_STATE_A_IDLE;
391         } else {
392                 twl->otg.default_a = false;
393                 twl->otg.state = OTG_STATE_B_IDLE;
394         }
395         spin_unlock_irq(&twl->lock);
396
397         return linkstat;
398 }
399
400 static void twl4030_usb_set_mode(struct twl4030_usb *twl, int mode)
401 {
402         twl->usb_mode = mode;
403
404         switch (mode) {
405         case T2_USB_MODE_ULPI:
406                 twl4030_usb_clear_bits(twl, IFC_CTRL, IFC_CTRL_CARKITMODE);
407                 twl4030_usb_set_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
408                 twl4030_usb_clear_bits(twl, FUNC_CTRL,
409                                         FUNC_CTRL_XCVRSELECT_MASK |
410                                         FUNC_CTRL_OPMODE_MASK);
411                 break;
412 /*
413         case T2_USB_MODE_CEA2011_3PIN:
414                 twl4030_cea2011_3_pin_FS_setup(twl);
415                 break;
416 */
417         default:
418                 /* FIXME: power on defaults */
419                 break;
420         };
421 }
422
423 static void twl4030_i2c_access(struct twl4030_usb *twl, int on)
424 {
425         unsigned long timeout;
426         int val = twl4030_usb_read(twl, PHY_CLK_CTRL);
427
428         if (val >= 0) {
429                 if (on) {
430                         /* enable DPLL to access PHY registers over I2C */
431                         val |= REQ_PHY_DPLL_CLK;
432                         WARN_ON(twl4030_usb_write_verify(twl, PHY_CLK_CTRL,
433                                                 (u8)val) < 0);
434
435                         timeout = jiffies + HZ;
436                         while (!(twl4030_usb_read(twl, PHY_CLK_CTRL_STS) &
437                                                         PHY_DPLL_CLK)
438                                 && time_before(jiffies, timeout))
439                                         udelay(10);
440                         if (!(twl4030_usb_read(twl, PHY_CLK_CTRL_STS) &
441                                                         PHY_DPLL_CLK))
442                                 dev_err(twl->dev, "Timeout setting T2 HSUSB "
443                                                 "PHY DPLL clock\n");
444                 } else {
445                         /* let ULPI control the DPLL clock */
446                         val &= ~REQ_PHY_DPLL_CLK;
447                         WARN_ON(twl4030_usb_write_verify(twl, PHY_CLK_CTRL,
448                                                 (u8)val) < 0);
449                 }
450         }
451 }
452
453 static void twl4030_phy_power(struct twl4030_usb *twl, int on)
454 {
455         u8 pwr;
456
457         pwr = twl4030_usb_read(twl, PHY_PWR_CTRL);
458         if (on) {
459                 pwr &= ~PHY_PWR_PHYPWD;
460                 WARN_ON(twl4030_usb_write_verify(twl, PHY_PWR_CTRL, pwr) < 0);
461                 twl4030_usb_write(twl, PHY_CLK_CTRL,
462                                   twl4030_usb_read(twl, PHY_CLK_CTRL) |
463                                         (PHY_CLK_CTRL_CLOCKGATING_EN |
464                                                 PHY_CLK_CTRL_CLK32K_EN));
465         } else  {
466                 pwr |= PHY_PWR_PHYPWD;
467                 WARN_ON(twl4030_usb_write_verify(twl, PHY_PWR_CTRL, pwr) < 0);
468         }
469 }
470
471 static void twl4030_phy_suspend(struct twl4030_usb *twl, int controller_off)
472 {
473         if (twl->asleep)
474                 return;
475
476         twl4030_phy_power(twl, 0);
477         twl->asleep = 1;
478 }
479
480 static void twl4030_phy_resume(struct twl4030_usb *twl)
481 {
482         if (!twl->asleep)
483                 return;
484
485         twl4030_phy_power(twl, 1);
486         twl4030_i2c_access(twl, 1);
487         twl4030_usb_set_mode(twl, twl->usb_mode);
488         if (twl->usb_mode == T2_USB_MODE_ULPI)
489                 twl4030_i2c_access(twl, 0);
490         twl->asleep = 0;
491 }
492
493 static void twl4030_usb_ldo_init(struct twl4030_usb *twl)
494 {
495         /* Enable writing to power configuration registers */
496         twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0xC0, PROTECT_KEY);
497         twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0x0C, PROTECT_KEY);
498
499         /* put VUSB3V1 LDO in active state */
500         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB_DEDICATED2);
501
502         /* turn on 3.1V regulator */
503         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB3V1_DEV_GRP);
504         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB3V1_TYPE);
505
506         /* turn on 1.5V regulator */
507         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB1V5_DEV_GRP);
508         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V5_TYPE);
509
510         /* turn on 1.8V regulator */
511         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB1V8_DEV_GRP);
512         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V8_TYPE);
513
514         /* disable access to power configuration registers */
515         twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0, PROTECT_KEY);
516 }
517
518 static ssize_t twl4030_usb_vbus_show(struct device *dev,
519                 struct device_attribute *attr, char *buf)
520 {
521         struct twl4030_usb *twl = dev_get_drvdata(dev);
522         unsigned long flags;
523         int ret = -EINVAL;
524
525         spin_lock_irqsave(&twl->lock, flags);
526         ret = sprintf(buf, "%s\n",
527                         (twl->linkstat == USB_LINK_VBUS) ? "on" : "off");
528         spin_unlock_irqrestore(&twl->lock, flags);
529
530         return ret;
531 }
532 static DEVICE_ATTR(vbus, 0444, twl4030_usb_vbus_show, NULL);
533
534 static void twl4030_change_mode_work(struct work_struct *work)
535 {
536         struct twl4030_usb *twl = container_of(work,
537                 struct twl4030_usb, change_mode_work);
538         u8 old_dedicated1, dedicated1;
539
540         if (twl->linkstat != USB_LINK_UNKNOWN) {
541                 twl4030_i2c_read_u8(TWL4030_MODULE_PM_RECEIVER,
542                         &old_dedicated1, VUSB_DEDICATED1);
543
544                 if (twl->linkstat == USB_LINK_ID)
545                         /* input to VUSB3V1 LDO is VBAT */
546                         dedicated1 = 0x14;
547                 else
548                         /* input to VUSB3V1 LDO is VBUS */
549                         dedicated1 = 0x18;
550
551                 if (dedicated1 != old_dedicated1) {
552                         dev_dbg(twl->dev, "VUSB_DEDICATED1 change %x -> %x\n",
553                                 old_dedicated1, dedicated1);
554                         /* turn off VUSB3V1 input for a bit before change */
555                         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
556                                 0x10, VUSB_DEDICATED1);
557                         mdelay(50);
558                         twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
559                                 dedicated1, VUSB_DEDICATED1);
560                         mdelay(50);
561
562                         if (twl->linkstat == USB_LINK_ID)
563                                 musb_kick_host();
564                 }
565
566                 /* FIXME add a set_power() method so that B-devices can
567                  * configure the charger appropriately.  It's not always
568                  * correct to consume VBUS power, and how much current to
569                  * consume is a function of the USB configuration chosen
570                  * by the host.
571                  *
572                  * REVISIT usb_gadget_vbus_connect(...) as needed, ditto
573                  * its disconnect() sibling, when changing to/from the
574                  * USB_LINK_VBUS state.  musb_hdrc won't care until it
575                  * starts to handle softconnect right.
576                  */
577                 twl4030charger_usb_en(twl->linkstat == USB_LINK_VBUS);
578         }
579 }
580
581 static irqreturn_t twl4030_usb_irq(int irq, void *_twl)
582 {
583         struct twl4030_usb *twl = _twl;
584         int status;
585
586 #ifdef CONFIG_LOCKDEP
587         /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
588          * we don't want and can't tolerate.  Although it might be
589          * friendlier not to borrow this thread context...
590          */
591         local_irq_enable();
592 #endif
593
594         status = twl4030_usb_linkstat(twl);
595         if (status != USB_LINK_UNKNOWN) {
596
597                 if (status == USB_LINK_NONE)
598                         twl4030_phy_suspend(twl, 0);
599                 else
600                         twl4030_phy_resume(twl);
601
602                 schedule_work(&twl->change_mode_work);
603         }
604         sysfs_notify(&twl->dev->kobj, NULL, "vbus");
605
606         return IRQ_HANDLED;
607 }
608
609 static int twl4030_set_suspend(struct otg_transceiver *x, int suspend)
610 {
611         struct twl4030_usb *twl = xceiv_to_twl(x);
612
613         if (suspend)
614                 twl4030_phy_suspend(twl, 1);
615         else
616                 twl4030_phy_resume(twl);
617
618         return 0;
619 }
620
621 static int twl4030_set_peripheral(struct otg_transceiver *x,
622                 struct usb_gadget *gadget)
623 {
624         struct twl4030_usb *twl;
625
626         if (!x)
627                 return -ENODEV;
628
629         twl = xceiv_to_twl(x);
630         twl->otg.gadget = gadget;
631
632         return 0;
633 }
634
635 static int twl4030_set_host(struct otg_transceiver *x, struct usb_bus *host)
636 {
637         struct twl4030_usb *twl;
638
639         if (!x)
640                 return -ENODEV;
641
642         twl = xceiv_to_twl(x);
643         twl->otg.host = host;
644
645         return 0;
646 }
647
648 static int __init twl4030_usb_probe(struct platform_device *pdev)
649 {
650         struct twl4030_usb_data *pdata = pdev->dev.platform_data;
651         struct twl4030_usb      *twl;
652         int                     status;
653
654         twl = kzalloc(sizeof *twl, GFP_KERNEL);
655         if (!twl)
656                 return -ENOMEM;
657
658         if (!pdata) {
659                 dev_info(&pdev->dev, "platform_data not available\n");
660                 return -EINVAL;
661         }
662
663         twl->dev                = &pdev->dev;
664         twl->irq                = platform_get_irq(pdev, 0);
665         twl->otg.dev            = twl->dev;
666         twl->otg.label          = "twl4030";
667         twl->otg.set_host       = twl4030_set_host;
668         twl->otg.set_peripheral = twl4030_set_peripheral;
669         twl->otg.set_suspend    = twl4030_set_suspend;
670         twl->usb_mode           = pdata->usb_mode;
671         twl->asleep             = 1;
672
673         /* init spinlock for workqueue */
674         spin_lock_init(&twl->lock);
675
676         twl4030_usb_ldo_init(twl);
677         otg_set_transceiver(&twl->otg);
678
679         platform_set_drvdata(pdev, twl);
680         if (device_create_file(&pdev->dev, &dev_attr_vbus))
681                 dev_warn(&pdev->dev, "could not create sysfs file\n");
682
683         INIT_WORK(&twl->change_mode_work, twl4030_change_mode_work);
684
685         /* Our job is to use irqs and status from the power module
686          * to keep the transceiver disabled when nothing's connected.
687          *
688          * FIXME we actually shouldn't start enabling it until the
689          * USB controller drivers have said they're ready, by calling
690          * set_host() and/or set_peripheral() ... OTG_capable boards
691          * need both handles, otherwise just one suffices.
692          */
693         twl->irq_enabled = true;
694         status = request_irq(twl->irq, twl4030_usb_irq,
695                         IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
696                         "twl4030_usb", twl);
697         if (status < 0) {
698                 dev_dbg(&pdev->dev, "can't get IRQ %d, err %d\n",
699                         twl->irq, status);
700                 kfree(twl);
701                 return status;
702         }
703
704         /* The IRQ handler just handles changes from the previous states
705          * of the ID and VBUS pins ... in probe() we must initialize that
706          * previous state.  The easy way:  fake an IRQ.
707          *
708          * REVISIT:  a real IRQ might have happened already, if PREEMPT is
709          * enabled.  Else the IRQ may not yet be configured or enabled,
710          * because of scheduling delays.
711          */
712         twl4030_usb_irq(twl->irq, twl);
713
714         dev_info(&pdev->dev, "Initialized TWL4030 USB module\n");
715         return 0;
716 }
717
718 static int __exit twl4030_usb_remove(struct platform_device *pdev)
719 {
720         struct twl4030_usb *twl = platform_get_drvdata(pdev);
721         int val;
722
723         free_irq(twl->irq, twl);
724         device_remove_file(twl->dev, &dev_attr_vbus);
725
726         /* set transceiver mode to power on defaults */
727         twl4030_usb_set_mode(twl, -1);
728
729         /* autogate 60MHz ULPI clock,
730          * clear dpll clock request for i2c access,
731          * disable 32KHz
732          */
733         val = twl4030_usb_read(twl, PHY_CLK_CTRL);
734         if (val >= 0) {
735                 val |= PHY_CLK_CTRL_CLOCKGATING_EN;
736                 val &= ~(PHY_CLK_CTRL_CLK32K_EN | REQ_PHY_DPLL_CLK);
737                 twl4030_usb_write(twl, PHY_CLK_CTRL, (u8)val);
738         }
739
740         /* disable complete OTG block */
741         twl4030_usb_clear_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
742
743         twl4030_phy_power(twl, 0);
744
745         kfree(twl);
746
747         return 0;
748 }
749
750 static struct platform_driver twl4030_usb_driver = {
751         .probe          = twl4030_usb_probe,
752         .remove         = __exit_p(twl4030_remove),
753         .driver         = {
754                 .name   = "twl4030_usb",
755                 .owner  = THIS_MODULE,
756         },
757 };
758
759 static int __init twl4030_usb_init(void)
760 {
761         return platform_driver_register(&twl4030_usb_driver);
762 }
763 subsys_initcall(twl4030_usb_init);
764
765 static void __exit twl4030_usb_exit(void)
766 {
767         platform_driver_unregister(&twl4030_usb_driver);
768 }
769 module_exit(twl4030_usb_exit);
770
771 MODULE_ALIAS("platform:twl4030_usb");
772 MODULE_AUTHOR("Texas Instruments, Inc, Nokia Corporation");
773 MODULE_DESCRIPTION("TWL4030 USB transceiver driver");
774 MODULE_LICENSE("GPL");