Merge ../linux-2.6
[linux-2.6] / drivers / i2c / chips / isp1301_omap.c
1 /*
2  * isp1301_omap - ISP 1301 USB transceiver, talking to OMAP OTG controller
3  *
4  * Copyright (C) 2004 Texas Instruments
5  * Copyright (C) 2004 David Brownell
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
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.
16  *
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.
20  */
21 #undef  DEBUG
22 #undef  VERBOSE
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/platform_device.h>
30 #include <linux/usb_ch9.h>
31 #include <linux/usb_gadget.h>
32 #include <linux/usb.h>
33 #include <linux/usb_otg.h>
34 #include <linux/i2c.h>
35 #include <linux/workqueue.h>
36
37 #include <asm/irq.h>
38 #include <asm/arch/usb.h>
39
40
41 #ifndef DEBUG
42 #undef  VERBOSE
43 #endif
44
45
46 #define DRIVER_VERSION  "24 August 2004"
47 #define DRIVER_NAME     (isp1301_driver.name)
48
49 MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
50 MODULE_LICENSE("GPL");
51
52 struct isp1301 {
53         struct otg_transceiver  otg;
54         struct i2c_client       client;
55         void                    (*i2c_release)(struct device *dev);
56
57         int                     irq;
58
59         u32                     last_otg_ctrl;
60         unsigned                working:1;
61
62         struct timer_list       timer;
63
64         /* use keventd context to change the state for us */
65         struct work_struct      work;
66         
67         unsigned long           todo;
68 #               define WORK_UPDATE_ISP  0       /* update ISP from OTG */
69 #               define WORK_UPDATE_OTG  1       /* update OTG from ISP */
70 #               define WORK_HOST_RESUME 4       /* resume host */
71 #               define WORK_TIMER       6       /* timer fired */
72 #               define WORK_STOP        7       /* don't resubmit */
73 };
74
75
76 /* bits in OTG_CTRL_REG */
77
78 #define OTG_XCEIV_OUTPUTS \
79         (OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
80 #define OTG_XCEIV_INPUTS \
81         (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
82 #define OTG_CTRL_BITS \
83         (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
84         /* and OTG_PULLUP is sometimes written */
85
86 #define OTG_CTRL_MASK   (OTG_DRIVER_SEL| \
87         OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
88         OTG_CTRL_BITS)
89
90
91 /*-------------------------------------------------------------------------*/
92
93 #ifdef  CONFIG_MACH_OMAP_H2
94
95 /* board-specific PM hooks */
96
97 #include <asm/arch/gpio.h>
98 #include <asm/arch/mux.h>
99 #include <asm/mach-types.h>
100
101
102 #if     defined(CONFIG_TPS65010) || defined(CONFIG_TPS65010_MODULE)
103
104 #include <asm/arch/tps65010.h>
105
106 #else
107
108 static inline int tps65010_set_vbus_draw(unsigned mA)
109 {
110         pr_debug("tps65010: draw %d mA (STUB)\n", mA);
111         return 0;
112 }
113
114 #endif
115
116 static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
117 {
118         int status = tps65010_set_vbus_draw(mA);
119         if (status < 0)
120                 pr_debug("  VBUS %d mA error %d\n", mA, status);
121 }
122
123 static void enable_vbus_source(struct isp1301 *isp)
124 {
125         /* this board won't supply more than 8mA vbus power.
126          * some boards can switch a 100ma "unit load" (or more).
127          */
128 }
129
130
131 /* products will deliver OTG messages with LEDs, GUI, etc */
132 static inline void notresponding(struct isp1301 *isp)
133 {
134         printk(KERN_NOTICE "OTG device not responding.\n");
135 }
136
137
138 #endif
139
140 /*-------------------------------------------------------------------------*/
141
142 /* only two addresses possible */
143 #define ISP_BASE                0x2c
144 static unsigned short normal_i2c[] = {
145         ISP_BASE, ISP_BASE + 1,
146         I2C_CLIENT_END };
147
148 I2C_CLIENT_INSMOD;
149
150 static struct i2c_driver isp1301_driver;
151
152 /* smbus apis are used for portability */
153
154 static inline u8
155 isp1301_get_u8(struct isp1301 *isp, u8 reg)
156 {
157         return i2c_smbus_read_byte_data(&isp->client, reg + 0);
158 }
159
160 static inline int
161 isp1301_get_u16(struct isp1301 *isp, u8 reg)
162 {
163         return i2c_smbus_read_word_data(&isp->client, reg);
164 }
165
166 static inline int
167 isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
168 {
169         return i2c_smbus_write_byte_data(&isp->client, reg + 0, bits);
170 }
171
172 static inline int
173 isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
174 {
175         return i2c_smbus_write_byte_data(&isp->client, reg + 1, bits);
176 }
177
178 /*-------------------------------------------------------------------------*/
179
180 /* identification */
181 #define ISP1301_VENDOR_ID               0x00    /* u16 read */
182 #define ISP1301_PRODUCT_ID              0x02    /* u16 read */
183 #define ISP1301_BCD_DEVICE              0x14    /* u16 read */
184
185 #define I2C_VENDOR_ID_PHILIPS           0x04cc
186 #define I2C_PRODUCT_ID_PHILIPS_1301     0x1301
187
188 /* operational registers */
189 #define ISP1301_MODE_CONTROL_1          0x04    /* u8 read, set, +1 clear */
190 #       define  MC1_SPEED_REG           (1 << 0)
191 #       define  MC1_SUSPEND_REG         (1 << 1)
192 #       define  MC1_DAT_SE0             (1 << 2)
193 #       define  MC1_TRANSPARENT         (1 << 3)
194 #       define  MC1_BDIS_ACON_EN        (1 << 4)
195 #       define  MC1_OE_INT_EN           (1 << 5)
196 #       define  MC1_UART_EN             (1 << 6)
197 #       define  MC1_MASK                0x7f
198 #define ISP1301_MODE_CONTROL_2          0x12    /* u8 read, set, +1 clear */
199 #       define  MC2_GLOBAL_PWR_DN       (1 << 0)
200 #       define  MC2_SPD_SUSP_CTRL       (1 << 1)
201 #       define  MC2_BI_DI               (1 << 2)
202 #       define  MC2_TRANSP_BDIR0        (1 << 3)
203 #       define  MC2_TRANSP_BDIR1        (1 << 4)
204 #       define  MC2_AUDIO_EN            (1 << 5)
205 #       define  MC2_PSW_EN              (1 << 6)
206 #       define  MC2_EN2V7               (1 << 7)
207 #define ISP1301_OTG_CONTROL_1           0x06    /* u8 read, set, +1 clear */
208 #       define  OTG1_DP_PULLUP          (1 << 0)
209 #       define  OTG1_DM_PULLUP          (1 << 1)
210 #       define  OTG1_DP_PULLDOWN        (1 << 2)
211 #       define  OTG1_DM_PULLDOWN        (1 << 3)
212 #       define  OTG1_ID_PULLDOWN        (1 << 4)
213 #       define  OTG1_VBUS_DRV           (1 << 5)
214 #       define  OTG1_VBUS_DISCHRG       (1 << 6)
215 #       define  OTG1_VBUS_CHRG          (1 << 7)
216 #define ISP1301_OTG_STATUS              0x10    /* u8 readonly */
217 #       define  OTG_B_SESS_END          (1 << 6)
218 #       define  OTG_B_SESS_VLD          (1 << 7)
219
220 #define ISP1301_INTERRUPT_SOURCE        0x08    /* u8 read */
221 #define ISP1301_INTERRUPT_LATCH         0x0A    /* u8 read, set, +1 clear */
222
223 #define ISP1301_INTERRUPT_FALLING       0x0C    /* u8 read, set, +1 clear */
224 #define ISP1301_INTERRUPT_RISING        0x0E    /* u8 read, set, +1 clear */
225
226 /* same bitfields in all interrupt registers */
227 #       define  INTR_VBUS_VLD           (1 << 0)
228 #       define  INTR_SESS_VLD           (1 << 1)
229 #       define  INTR_DP_HI              (1 << 2)
230 #       define  INTR_ID_GND             (1 << 3)
231 #       define  INTR_DM_HI              (1 << 4)
232 #       define  INTR_ID_FLOAT           (1 << 5)
233 #       define  INTR_BDIS_ACON          (1 << 6)
234 #       define  INTR_CR_INT             (1 << 7)
235
236 /*-------------------------------------------------------------------------*/
237
238 static const char *state_string(enum usb_otg_state state)
239 {
240         switch (state) {
241         case OTG_STATE_A_IDLE:          return "a_idle";
242         case OTG_STATE_A_WAIT_VRISE:    return "a_wait_vrise";
243         case OTG_STATE_A_WAIT_BCON:     return "a_wait_bcon";
244         case OTG_STATE_A_HOST:          return "a_host";
245         case OTG_STATE_A_SUSPEND:       return "a_suspend";
246         case OTG_STATE_A_PERIPHERAL:    return "a_peripheral";
247         case OTG_STATE_A_WAIT_VFALL:    return "a_wait_vfall";
248         case OTG_STATE_A_VBUS_ERR:      return "a_vbus_err";
249         case OTG_STATE_B_IDLE:          return "b_idle";
250         case OTG_STATE_B_SRP_INIT:      return "b_srp_init";
251         case OTG_STATE_B_PERIPHERAL:    return "b_peripheral";
252         case OTG_STATE_B_WAIT_ACON:     return "b_wait_acon";
253         case OTG_STATE_B_HOST:          return "b_host";
254         default:                        return "UNDEFINED";
255         }
256 }
257
258 static inline const char *state_name(struct isp1301 *isp)
259 {
260         return state_string(isp->otg.state);
261 }
262
263 #ifdef  VERBOSE
264 #define dev_vdbg                        dev_dbg
265 #else
266 #define dev_vdbg(dev, fmt, arg...)      do{}while(0)
267 #endif
268
269 /*-------------------------------------------------------------------------*/
270
271 /* NOTE:  some of this ISP1301 setup is specific to H2 boards;
272  * not everything is guarded by board-specific checks, or even using
273  * omap_usb_config data to deduce MC1_DAT_SE0 and MC2_BI_DI.
274  *
275  * ALSO:  this currently doesn't use ISP1301 low-power modes
276  * while OTG is running.
277  */
278
279 static void power_down(struct isp1301 *isp)
280 {
281         isp->otg.state = OTG_STATE_UNDEFINED;
282
283         // isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
284         isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND_REG);
285
286         isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_ID_PULLDOWN);
287         isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
288 }
289
290 static void power_up(struct isp1301 *isp)
291 {
292         // isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
293         isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND_REG);
294         
295         /* do this only when cpu is driving transceiver,
296          * so host won't see a low speed device...
297          */
298         isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
299 }
300
301 #define NO_HOST_SUSPEND
302
303 static int host_suspend(struct isp1301 *isp)
304 {
305 #ifdef  NO_HOST_SUSPEND
306         return 0;
307 #else
308         struct device   *dev;
309
310         if (!isp->otg.host)
311                 return -ENODEV;
312
313         /* Currently ASSUMES only the OTG port matters;
314          * other ports could be active...
315          */
316         dev = isp->otg.host->controller;
317         return dev->driver->suspend(dev, 3, 0);
318 #endif
319 }
320
321 static int host_resume(struct isp1301 *isp)
322 {
323 #ifdef  NO_HOST_SUSPEND
324         return 0;
325 #else
326         struct device   *dev;
327
328         if (!isp->otg.host)
329                 return -ENODEV;
330
331         dev = isp->otg.host->controller;
332         return dev->driver->resume(dev, 0);
333 #endif
334 }
335
336 static int gadget_suspend(struct isp1301 *isp)
337 {
338         isp->otg.gadget->b_hnp_enable = 0;
339         isp->otg.gadget->a_hnp_support = 0;
340         isp->otg.gadget->a_alt_hnp_support = 0;
341         return usb_gadget_vbus_disconnect(isp->otg.gadget);
342 }
343
344 /*-------------------------------------------------------------------------*/
345
346 #define TIMER_MINUTES   10
347 #define TIMER_JIFFIES   (TIMER_MINUTES * 60 * HZ)
348
349 /* Almost all our I2C messaging comes from a work queue's task context.
350  * NOTE: guaranteeing certain response times might mean we shouldn't
351  * share keventd's work queue; a realtime task might be safest.
352  */
353 void
354 isp1301_defer_work(struct isp1301 *isp, int work)
355 {
356         int status;
357
358         if (isp && !test_and_set_bit(work, &isp->todo)) {
359                 (void) get_device(&isp->client.dev);
360                 status = schedule_work(&isp->work);
361                 if (!status && !isp->working)
362                         dev_vdbg(&isp->client.dev,
363                                 "work item %d may be lost\n", work);
364         }
365 }
366
367 /* called from irq handlers */
368 static void a_idle(struct isp1301 *isp, const char *tag)
369 {
370         if (isp->otg.state == OTG_STATE_A_IDLE)
371                 return;
372
373         isp->otg.default_a = 1;
374         if (isp->otg.host) {
375                 isp->otg.host->is_b_host = 0;
376                 host_suspend(isp);
377         }
378         if (isp->otg.gadget) {
379                 isp->otg.gadget->is_a_peripheral = 1;
380                 gadget_suspend(isp);
381         }
382         isp->otg.state = OTG_STATE_A_IDLE;
383         isp->last_otg_ctrl = OTG_CTRL_REG = OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
384         pr_debug("  --> %s/%s\n", state_name(isp), tag);
385 }
386
387 /* called from irq handlers */
388 static void b_idle(struct isp1301 *isp, const char *tag)
389 {
390         if (isp->otg.state == OTG_STATE_B_IDLE)
391                 return;
392
393         isp->otg.default_a = 0;
394         if (isp->otg.host) {
395                 isp->otg.host->is_b_host = 1;
396                 host_suspend(isp);
397         }
398         if (isp->otg.gadget) {
399                 isp->otg.gadget->is_a_peripheral = 0;
400                 gadget_suspend(isp);
401         }
402         isp->otg.state = OTG_STATE_B_IDLE;
403         isp->last_otg_ctrl = OTG_CTRL_REG = OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
404         pr_debug("  --> %s/%s\n", state_name(isp), tag);
405 }
406
407 static void
408 dump_regs(struct isp1301 *isp, const char *label)
409 {
410 #ifdef  DEBUG
411         u8      ctrl = isp1301_get_u8(isp, ISP1301_OTG_CONTROL_1);
412         u8      status = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
413         u8      src = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
414
415         pr_debug("otg: %06x, %s %s, otg/%02x stat/%02x.%02x\n",
416                 OTG_CTRL_REG, label, state_name(isp),
417                 ctrl, status, src);
418         /* mode control and irq enables don't change much */
419 #endif
420 }
421
422 /*-------------------------------------------------------------------------*/
423
424 #ifdef  CONFIG_USB_OTG
425
426 /*
427  * The OMAP OTG controller handles most of the OTG state transitions.
428  *
429  * We translate isp1301 outputs (mostly voltage comparator status) into
430  * OTG inputs; OTG outputs (mostly pullup/pulldown controls) and HNP state
431  * flags into isp1301 inputs ... and infer state transitions.
432  */
433
434 #ifdef  VERBOSE
435
436 static void check_state(struct isp1301 *isp, const char *tag)
437 {
438         enum usb_otg_state      state = OTG_STATE_UNDEFINED;
439         u8                      fsm = OTG_TEST_REG & 0x0ff;
440         unsigned                extra = 0;
441
442         switch (fsm) {
443
444         /* default-b */
445         case 0x0:
446                 state = OTG_STATE_B_IDLE;
447                 break;
448         case 0x3:
449         case 0x7:
450                 extra = 1;
451         case 0x1:
452                 state = OTG_STATE_B_PERIPHERAL;
453                 break;
454         case 0x11:
455                 state = OTG_STATE_B_SRP_INIT;
456                 break;
457
458         /* extra dual-role default-b states */
459         case 0x12:
460         case 0x13:
461         case 0x16:
462                 extra = 1;
463         case 0x17:
464                 state = OTG_STATE_B_WAIT_ACON;
465                 break;
466         case 0x34:
467                 state = OTG_STATE_B_HOST;
468                 break;
469
470         /* default-a */
471         case 0x36:
472                 state = OTG_STATE_A_IDLE;
473                 break;
474         case 0x3c:
475                 state = OTG_STATE_A_WAIT_VFALL;
476                 break;
477         case 0x7d:
478                 state = OTG_STATE_A_VBUS_ERR;
479                 break;
480         case 0x9e:
481         case 0x9f:
482                 extra = 1;
483         case 0x89:
484                 state = OTG_STATE_A_PERIPHERAL;
485                 break;
486         case 0xb7:
487                 state = OTG_STATE_A_WAIT_VRISE;
488                 break;
489         case 0xb8:
490                 state = OTG_STATE_A_WAIT_BCON;
491                 break;
492         case 0xb9:
493                 state = OTG_STATE_A_HOST;
494                 break;
495         case 0xba:
496                 state = OTG_STATE_A_SUSPEND;
497                 break;
498         default:
499                 break;
500         }
501         if (isp->otg.state == state && !extra)
502                 return;
503         pr_debug("otg: %s FSM %s/%02x, %s, %06x\n", tag,
504                 state_string(state), fsm, state_name(isp), OTG_CTRL_REG);
505 }
506
507 #else
508
509 static inline void check_state(struct isp1301 *isp, const char *tag) { }
510
511 #endif
512
513 /* outputs from ISP1301_INTERRUPT_SOURCE */
514 static void update_otg1(struct isp1301 *isp, u8 int_src)
515 {
516         u32     otg_ctrl;
517
518         otg_ctrl = OTG_CTRL_REG
519                         & OTG_CTRL_MASK
520                         & ~OTG_XCEIV_INPUTS
521                         & ~(OTG_ID|OTG_ASESSVLD|OTG_VBUSVLD);
522         if (int_src & INTR_SESS_VLD)
523                 otg_ctrl |= OTG_ASESSVLD;
524         else if (isp->otg.state == OTG_STATE_A_WAIT_VFALL) {
525                 a_idle(isp, "vfall");
526                 otg_ctrl &= ~OTG_CTRL_BITS;
527         }
528         if (int_src & INTR_VBUS_VLD)
529                 otg_ctrl |= OTG_VBUSVLD;
530         if (int_src & INTR_ID_GND) {            /* default-A */
531                 if (isp->otg.state == OTG_STATE_B_IDLE
532                                 || isp->otg.state == OTG_STATE_UNDEFINED) {
533                         a_idle(isp, "init");
534                         return;
535                 }
536         } else {                                /* default-B */
537                 otg_ctrl |= OTG_ID;
538                 if (isp->otg.state == OTG_STATE_A_IDLE
539                                 || isp->otg.state == OTG_STATE_UNDEFINED) {
540                         b_idle(isp, "init");
541                         return;
542                 }
543         }
544         OTG_CTRL_REG = otg_ctrl;
545 }
546
547 /* outputs from ISP1301_OTG_STATUS */
548 static void update_otg2(struct isp1301 *isp, u8 otg_status)
549 {
550         u32     otg_ctrl;
551
552         otg_ctrl = OTG_CTRL_REG
553                         & OTG_CTRL_MASK
554                         & ~OTG_XCEIV_INPUTS
555                         & ~(OTG_BSESSVLD|OTG_BSESSEND);
556         if (otg_status & OTG_B_SESS_VLD)
557                 otg_ctrl |= OTG_BSESSVLD;
558         else if (otg_status & OTG_B_SESS_END)
559                 otg_ctrl |= OTG_BSESSEND;
560         OTG_CTRL_REG = otg_ctrl;
561 }
562
563 /* inputs going to ISP1301 */
564 static void otg_update_isp(struct isp1301 *isp)
565 {
566         u32     otg_ctrl, otg_change;
567         u8      set = OTG1_DM_PULLDOWN, clr = OTG1_DM_PULLUP;
568
569         otg_ctrl = OTG_CTRL_REG;
570         otg_change = otg_ctrl ^ isp->last_otg_ctrl;
571         isp->last_otg_ctrl = otg_ctrl;
572         otg_ctrl = otg_ctrl & OTG_XCEIV_INPUTS;
573
574         switch (isp->otg.state) {
575         case OTG_STATE_B_IDLE:
576         case OTG_STATE_B_PERIPHERAL:
577         case OTG_STATE_B_SRP_INIT:
578                 if (!(otg_ctrl & OTG_PULLUP)) {
579                         // if (otg_ctrl & OTG_B_HNPEN) {
580                         if (isp->otg.gadget->b_hnp_enable) {
581                                 isp->otg.state = OTG_STATE_B_WAIT_ACON;
582                                 pr_debug("  --> b_wait_acon\n");
583                         }
584                         goto pulldown;
585                 }
586 pullup:
587                 set |= OTG1_DP_PULLUP;
588                 clr |= OTG1_DP_PULLDOWN;
589                 break;
590         case OTG_STATE_A_SUSPEND:
591         case OTG_STATE_A_PERIPHERAL:
592                 if (otg_ctrl & OTG_PULLUP)
593                         goto pullup;
594                 /* FALLTHROUGH */
595         // case OTG_STATE_B_WAIT_ACON:
596         default:
597 pulldown:
598                 set |= OTG1_DP_PULLDOWN;
599                 clr |= OTG1_DP_PULLUP;
600                 break;
601         }
602
603 #       define toggle(OTG,ISP) do { \
604                 if (otg_ctrl & OTG) set |= ISP; \
605                 else clr |= ISP; \
606                 } while (0)
607
608         if (!(isp->otg.host))
609                 otg_ctrl &= ~OTG_DRV_VBUS;
610
611         switch (isp->otg.state) {
612         case OTG_STATE_A_SUSPEND:
613                 if (otg_ctrl & OTG_DRV_VBUS) {
614                         set |= OTG1_VBUS_DRV;
615                         break;
616                 }
617                 /* HNP failed for some reason (A_AIDL_BDIS timeout) */
618                 notresponding(isp);
619
620                 /* FALLTHROUGH */
621         case OTG_STATE_A_VBUS_ERR:
622                 isp->otg.state = OTG_STATE_A_WAIT_VFALL;
623                 pr_debug("  --> a_wait_vfall\n");
624                 /* FALLTHROUGH */
625         case OTG_STATE_A_WAIT_VFALL:
626                 /* FIXME usbcore thinks port power is still on ... */
627                 clr |= OTG1_VBUS_DRV;
628                 break;
629         case OTG_STATE_A_IDLE:
630                 if (otg_ctrl & OTG_DRV_VBUS) {
631                         isp->otg.state = OTG_STATE_A_WAIT_VRISE;
632                         pr_debug("  --> a_wait_vrise\n");
633                 }
634                 /* FALLTHROUGH */
635         default:
636                 toggle(OTG_DRV_VBUS, OTG1_VBUS_DRV);
637         }
638
639         toggle(OTG_PU_VBUS, OTG1_VBUS_CHRG);
640         toggle(OTG_PD_VBUS, OTG1_VBUS_DISCHRG);
641
642 #       undef toggle
643
644         isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, set);
645         isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, clr);
646
647         /* HNP switch to host or peripheral; and SRP */
648         if (otg_change & OTG_PULLUP) {
649                 switch (isp->otg.state) {
650                 case OTG_STATE_B_IDLE:
651                         if (clr & OTG1_DP_PULLUP)
652                                 break;
653                         isp->otg.state = OTG_STATE_B_PERIPHERAL;
654                         pr_debug("  --> b_peripheral\n");
655                         break;
656                 case OTG_STATE_A_SUSPEND:
657                         if (clr & OTG1_DP_PULLUP)
658                                 break;
659                         isp->otg.state = OTG_STATE_A_PERIPHERAL;
660                         pr_debug("  --> a_peripheral\n");
661                         break;
662                 default:
663                         break;
664                 }
665                 OTG_CTRL_REG |= OTG_PULLUP;
666         }
667
668         check_state(isp, __FUNCTION__);
669         dump_regs(isp, "otg->isp1301");
670 }
671
672 static irqreturn_t omap_otg_irq(int irq, void *_isp, struct pt_regs *regs)
673 {
674         u16             otg_irq = OTG_IRQ_SRC_REG;
675         u32             otg_ctrl;
676         int             ret = IRQ_NONE;
677         struct isp1301  *isp = _isp;
678
679         /* update ISP1301 transciever from OTG controller */
680         if (otg_irq & OPRT_CHG) {
681                 OTG_IRQ_SRC_REG = OPRT_CHG;
682                 isp1301_defer_work(isp, WORK_UPDATE_ISP);
683                 ret = IRQ_HANDLED;
684
685         /* SRP to become b_peripheral failed */
686         } else if (otg_irq & B_SRP_TMROUT) {
687                 pr_debug("otg: B_SRP_TIMEOUT, %06x\n", OTG_CTRL_REG);
688                 notresponding(isp);
689
690                 /* gadget drivers that care should monitor all kinds of
691                  * remote wakeup (SRP, normal) using their own timer
692                  * to give "check cable and A-device" messages.
693                  */
694                 if (isp->otg.state == OTG_STATE_B_SRP_INIT)
695                         b_idle(isp, "srp_timeout");
696
697                 OTG_IRQ_SRC_REG = B_SRP_TMROUT;
698                 ret = IRQ_HANDLED;
699
700         /* HNP to become b_host failed */
701         } else if (otg_irq & B_HNP_FAIL) {
702                 pr_debug("otg: %s B_HNP_FAIL, %06x\n",
703                                 state_name(isp), OTG_CTRL_REG);
704                 notresponding(isp);
705
706                 otg_ctrl = OTG_CTRL_REG;
707                 otg_ctrl |= OTG_BUSDROP;
708                 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
709                 OTG_CTRL_REG = otg_ctrl;
710
711                 /* subset of b_peripheral()... */
712                 isp->otg.state = OTG_STATE_B_PERIPHERAL;
713                 pr_debug("  --> b_peripheral\n");
714
715                 OTG_IRQ_SRC_REG = B_HNP_FAIL;
716                 ret = IRQ_HANDLED;
717
718         /* detect SRP from B-device ... */
719         } else if (otg_irq & A_SRP_DETECT) {
720                 pr_debug("otg: %s SRP_DETECT, %06x\n",
721                                 state_name(isp), OTG_CTRL_REG);
722
723                 isp1301_defer_work(isp, WORK_UPDATE_OTG);
724                 switch (isp->otg.state) {
725                 case OTG_STATE_A_IDLE:
726                         if (!isp->otg.host)
727                                 break;
728                         isp1301_defer_work(isp, WORK_HOST_RESUME);
729                         otg_ctrl = OTG_CTRL_REG;
730                         otg_ctrl |= OTG_A_BUSREQ;
731                         otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
732                                         & ~OTG_XCEIV_INPUTS
733                                         & OTG_CTRL_MASK;
734                         OTG_CTRL_REG = otg_ctrl;
735                         break;
736                 default:
737                         break;
738                 }
739
740                 OTG_IRQ_SRC_REG = A_SRP_DETECT;
741                 ret = IRQ_HANDLED;
742
743         /* timer expired:  T(a_wait_bcon) and maybe T(a_wait_vrise)
744          * we don't track them separately
745          */
746         } else if (otg_irq & A_REQ_TMROUT) {
747                 otg_ctrl = OTG_CTRL_REG;
748                 pr_info("otg: BCON_TMOUT from %s, %06x\n",
749                                 state_name(isp), otg_ctrl);
750                 notresponding(isp);
751
752                 otg_ctrl |= OTG_BUSDROP;
753                 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
754                 OTG_CTRL_REG = otg_ctrl;
755                 isp->otg.state = OTG_STATE_A_WAIT_VFALL;
756
757                 OTG_IRQ_SRC_REG = A_REQ_TMROUT;
758                 ret = IRQ_HANDLED;
759
760         /* A-supplied voltage fell too low; overcurrent */
761         } else if (otg_irq & A_VBUS_ERR) {
762                 otg_ctrl = OTG_CTRL_REG;
763                 printk(KERN_ERR "otg: %s, VBUS_ERR %04x ctrl %06x\n",
764                         state_name(isp), otg_irq, otg_ctrl);
765
766                 otg_ctrl |= OTG_BUSDROP;
767                 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
768                 OTG_CTRL_REG = otg_ctrl;
769                 isp->otg.state = OTG_STATE_A_VBUS_ERR;
770
771                 OTG_IRQ_SRC_REG = A_VBUS_ERR;
772                 ret = IRQ_HANDLED;
773
774         /* switch driver; the transciever code activates it,
775          * ungating the udc clock or resuming OHCI.
776          */
777         } else if (otg_irq & DRIVER_SWITCH) {
778                 int     kick = 0;
779
780                 otg_ctrl = OTG_CTRL_REG;
781                 printk(KERN_NOTICE "otg: %s, SWITCH to %s, ctrl %06x\n",
782                                 state_name(isp),
783                                 (otg_ctrl & OTG_DRIVER_SEL)
784                                         ? "gadget" : "host",
785                                 otg_ctrl);
786                 isp1301_defer_work(isp, WORK_UPDATE_ISP);
787
788                 /* role is peripheral */
789                 if (otg_ctrl & OTG_DRIVER_SEL) {
790                         switch (isp->otg.state) {
791                         case OTG_STATE_A_IDLE:
792                                 b_idle(isp, __FUNCTION__);
793                                 break;
794                         default:
795                                 break;
796                         }
797                         isp1301_defer_work(isp, WORK_UPDATE_ISP);
798
799                 /* role is host */
800                 } else {
801                         if (!(otg_ctrl & OTG_ID)) {
802                                 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
803                                 OTG_CTRL_REG = otg_ctrl | OTG_A_BUSREQ;
804                         }
805
806                         if (isp->otg.host) {
807                                 switch (isp->otg.state) {
808                                 case OTG_STATE_B_WAIT_ACON:
809                                         isp->otg.state = OTG_STATE_B_HOST;
810                                         pr_debug("  --> b_host\n");
811                                         kick = 1;
812                                         break;
813                                 case OTG_STATE_A_WAIT_BCON:
814                                         isp->otg.state = OTG_STATE_A_HOST;
815                                         pr_debug("  --> a_host\n");
816                                         break;
817                                 case OTG_STATE_A_PERIPHERAL:
818                                         isp->otg.state = OTG_STATE_A_WAIT_BCON;
819                                         pr_debug("  --> a_wait_bcon\n");
820                                         break;
821                                 default:
822                                         break;
823                                 }
824                                 isp1301_defer_work(isp, WORK_HOST_RESUME);
825                         }
826                 }
827
828                 OTG_IRQ_SRC_REG = DRIVER_SWITCH;
829                 ret = IRQ_HANDLED;
830
831                 if (kick)
832                         usb_bus_start_enum(isp->otg.host,
833                                                 isp->otg.host->otg_port);
834         }
835
836         check_state(isp, __FUNCTION__);
837         return ret;
838 }
839
840 static struct platform_device *otg_dev;
841
842 static int otg_init(struct isp1301 *isp)
843 {
844         if (!otg_dev)
845                 return -ENODEV;
846
847         dump_regs(isp, __FUNCTION__);
848         /* some of these values are board-specific... */
849         OTG_SYSCON_2_REG |= OTG_EN
850                 /* for B-device: */
851                 | SRP_GPDATA            /* 9msec Bdev D+ pulse */
852                 | SRP_GPDVBUS           /* discharge after VBUS pulse */
853                 // | (3 << 24)          /* 2msec VBUS pulse */
854                 /* for A-device: */
855                 | (0 << 20)             /* 200ms nominal A_WAIT_VRISE timer */
856                 | SRP_DPW               /* detect 167+ns SRP pulses */
857                 | SRP_DATA | SRP_VBUS   /* accept both kinds of SRP pulse */
858                 ;
859
860         update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
861         update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
862
863         check_state(isp, __FUNCTION__);
864         pr_debug("otg: %s, %s %06x\n",
865                         state_name(isp), __FUNCTION__, OTG_CTRL_REG);
866
867         OTG_IRQ_EN_REG = DRIVER_SWITCH | OPRT_CHG
868                         | B_SRP_TMROUT | B_HNP_FAIL
869                         | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT;
870         OTG_SYSCON_2_REG |= OTG_EN;
871
872         return 0;
873 }
874
875 static int otg_probe(struct platform_device *dev)
876 {
877         // struct omap_usb_config *config = dev->platform_data;
878
879         otg_dev = dev;
880         return 0;
881 }
882
883 static int otg_remove(struct platform_device *dev)
884 {
885         otg_dev = 0;
886         return 0;
887 }
888
889 struct platform_driver omap_otg_driver = {
890         .probe          = otg_probe,
891         .remove         = otg_remove,
892         .driver         = {
893                 .owner  = THIS_MODULE,
894                 .name   = "omap_otg",
895         },
896 };
897
898 static int otg_bind(struct isp1301 *isp)
899 {
900         int     status;
901
902         if (otg_dev)
903                 return -EBUSY;
904
905         status = platform_driver_register(&omap_otg_driver);
906         if (status < 0)
907                 return status;
908
909         if (otg_dev)
910                 status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
911                                 IRQF_DISABLED, DRIVER_NAME, isp);
912         else
913                 status = -ENODEV;
914
915         if (status < 0)
916                 platform_driver_unregister(&omap_otg_driver);
917         return status;
918 }
919
920 static void otg_unbind(struct isp1301 *isp)
921 {
922         if (!otg_dev)
923                 return;
924         free_irq(otg_dev->resource[1].start, isp);
925 }
926
927 #else
928
929 /* OTG controller isn't clocked */
930
931 #endif  /* CONFIG_USB_OTG */
932
933 /*-------------------------------------------------------------------------*/
934
935 static void b_peripheral(struct isp1301 *isp)
936 {
937         OTG_CTRL_REG = OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
938         usb_gadget_vbus_connect(isp->otg.gadget);
939
940 #ifdef  CONFIG_USB_OTG
941         enable_vbus_draw(isp, 8);
942         otg_update_isp(isp);
943 #else
944         enable_vbus_draw(isp, 100);
945         /* UDC driver just set OTG_BSESSVLD */
946         isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
947         isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
948         isp->otg.state = OTG_STATE_B_PERIPHERAL;
949         pr_debug("  --> b_peripheral\n");
950         dump_regs(isp, "2periph");
951 #endif
952 }
953
954 static void isp_update_otg(struct isp1301 *isp, u8 stat)
955 {
956         u8                      isp_stat, isp_bstat;
957         enum usb_otg_state      state = isp->otg.state;
958
959         if (stat & INTR_BDIS_ACON)
960                 pr_debug("OTG:  BDIS_ACON, %s\n", state_name(isp));
961
962         /* start certain state transitions right away */
963         isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
964         if (isp_stat & INTR_ID_GND) {
965                 if (isp->otg.default_a) {
966                         switch (state) {
967                         case OTG_STATE_B_IDLE:
968                                 a_idle(isp, "idle");
969                                 /* FALLTHROUGH */
970                         case OTG_STATE_A_IDLE:
971                                 enable_vbus_source(isp);
972                                 /* FALLTHROUGH */
973                         case OTG_STATE_A_WAIT_VRISE:
974                                 /* we skip over OTG_STATE_A_WAIT_BCON, since
975                                  * the HC will transition to A_HOST (or
976                                  * A_SUSPEND!) without our noticing except
977                                  * when HNP is used.
978                                  */
979                                 if (isp_stat & INTR_VBUS_VLD)
980                                         isp->otg.state = OTG_STATE_A_HOST;
981                                 break;
982                         case OTG_STATE_A_WAIT_VFALL:
983                                 if (!(isp_stat & INTR_SESS_VLD))
984                                         a_idle(isp, "vfell");
985                                 break;
986                         default:
987                                 if (!(isp_stat & INTR_VBUS_VLD))
988                                         isp->otg.state = OTG_STATE_A_VBUS_ERR;
989                                 break;
990                         }
991                         isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
992                 } else {
993                         switch (state) {
994                         case OTG_STATE_B_PERIPHERAL:
995                         case OTG_STATE_B_HOST:
996                         case OTG_STATE_B_WAIT_ACON:
997                                 usb_gadget_vbus_disconnect(isp->otg.gadget);
998                                 break;
999                         default:
1000                                 break;
1001                         }
1002                         if (state != OTG_STATE_A_IDLE)
1003                                 a_idle(isp, "id");
1004                         if (isp->otg.host && state == OTG_STATE_A_IDLE)
1005                                 isp1301_defer_work(isp, WORK_HOST_RESUME);
1006                         isp_bstat = 0;
1007                 }
1008         } else {
1009                 /* if user unplugged mini-A end of cable,
1010                  * don't bypass A_WAIT_VFALL.
1011                  */
1012                 if (isp->otg.default_a) {
1013                         switch (state) {
1014                         default:
1015                                 isp->otg.state = OTG_STATE_A_WAIT_VFALL;
1016                                 break;
1017                         case OTG_STATE_A_WAIT_VFALL:
1018                                 state = OTG_STATE_A_IDLE;
1019                                 /* khubd may take a while to notice and
1020                                  * handle this disconnect, so don't go
1021                                  * to B_IDLE quite yet.
1022                                  */
1023                                 break;
1024                         case OTG_STATE_A_IDLE:
1025                                 host_suspend(isp);
1026                                 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
1027                                                 MC1_BDIS_ACON_EN);
1028                                 isp->otg.state = OTG_STATE_B_IDLE;
1029                                 OTG_CTRL_REG &= OTG_CTRL_REG & OTG_CTRL_MASK
1030                                                 & ~OTG_CTRL_BITS;
1031                                 break;
1032                         case OTG_STATE_B_IDLE:
1033                                 break;
1034                         }
1035                 }
1036                 isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
1037
1038                 switch (isp->otg.state) {
1039                 case OTG_STATE_B_PERIPHERAL:
1040                 case OTG_STATE_B_WAIT_ACON:
1041                 case OTG_STATE_B_HOST:
1042                         if (likely(isp_bstat & OTG_B_SESS_VLD))
1043                                 break;
1044                         enable_vbus_draw(isp, 0);
1045 #ifndef CONFIG_USB_OTG
1046                         /* UDC driver will clear OTG_BSESSVLD */
1047                         isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1048                                                 OTG1_DP_PULLDOWN);
1049                         isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1050                                                 OTG1_DP_PULLUP);
1051                         dump_regs(isp, __FUNCTION__);
1052 #endif
1053                         /* FALLTHROUGH */
1054                 case OTG_STATE_B_SRP_INIT:
1055                         b_idle(isp, __FUNCTION__);
1056                         OTG_CTRL_REG &= OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
1057                         /* FALLTHROUGH */
1058                 case OTG_STATE_B_IDLE:
1059                         if (isp->otg.gadget && (isp_bstat & OTG_B_SESS_VLD)) {
1060 #ifdef  CONFIG_USB_OTG
1061                                 update_otg1(isp, isp_stat);
1062                                 update_otg2(isp, isp_bstat);
1063 #endif
1064                                 b_peripheral(isp);
1065                         } else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
1066                                 isp_bstat |= OTG_B_SESS_END;
1067                         break;
1068                 case OTG_STATE_A_WAIT_VFALL:
1069                         break;
1070                 default:
1071                         pr_debug("otg: unsupported b-device %s\n",
1072                                 state_name(isp));
1073                         break;
1074                 }
1075         }
1076
1077         if (state != isp->otg.state)
1078                 pr_debug("  isp, %s -> %s\n",
1079                                 state_string(state), state_name(isp));
1080
1081 #ifdef  CONFIG_USB_OTG
1082         /* update the OTG controller state to match the isp1301; may
1083          * trigger OPRT_CHG irqs for changes going to the isp1301.
1084          */
1085         update_otg1(isp, isp_stat);
1086         update_otg2(isp, isp_bstat);
1087         check_state(isp, __FUNCTION__);
1088 #endif
1089
1090         dump_regs(isp, "isp1301->otg");
1091 }
1092
1093 /*-------------------------------------------------------------------------*/
1094
1095 static u8 isp1301_clear_latch(struct isp1301 *isp)
1096 {
1097         u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
1098         isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
1099         return latch;
1100 }
1101
1102 static void
1103 isp1301_work(void *data)
1104 {
1105         struct isp1301  *isp = data;
1106         int             stop;
1107
1108         /* implicit lock:  we're the only task using this device */
1109         isp->working = 1;
1110         do {
1111                 stop = test_bit(WORK_STOP, &isp->todo);
1112
1113 #ifdef  CONFIG_USB_OTG
1114                 /* transfer state from otg engine to isp1301 */
1115                 if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
1116                         otg_update_isp(isp);
1117                         put_device(&isp->client.dev);
1118                 }
1119 #endif
1120                 /* transfer state from isp1301 to otg engine */
1121                 if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
1122                         u8              stat = isp1301_clear_latch(isp);
1123
1124                         isp_update_otg(isp, stat);
1125                         put_device(&isp->client.dev);
1126                 }
1127
1128                 if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
1129                         u32     otg_ctrl;
1130
1131                         /*
1132                          * skip A_WAIT_VRISE; hc transitions invisibly
1133                          * skip A_WAIT_BCON; same.
1134                          */
1135                         switch (isp->otg.state) {
1136                         case OTG_STATE_A_WAIT_BCON:
1137                         case OTG_STATE_A_WAIT_VRISE:
1138                                 isp->otg.state = OTG_STATE_A_HOST;
1139                                 pr_debug("  --> a_host\n");
1140                                 otg_ctrl = OTG_CTRL_REG;
1141                                 otg_ctrl |= OTG_A_BUSREQ;
1142                                 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
1143                                                 & OTG_CTRL_MASK;
1144                                 OTG_CTRL_REG = otg_ctrl;
1145                                 break;
1146                         case OTG_STATE_B_WAIT_ACON:
1147                                 isp->otg.state = OTG_STATE_B_HOST;
1148                                 pr_debug("  --> b_host (acon)\n");
1149                                 break;
1150                         case OTG_STATE_B_HOST:
1151                         case OTG_STATE_B_IDLE:
1152                         case OTG_STATE_A_IDLE:
1153                                 break;
1154                         default:
1155                                 pr_debug("  host resume in %s\n",
1156                                                 state_name(isp));
1157                         }
1158                         host_resume(isp);
1159                         // mdelay(10);
1160                         put_device(&isp->client.dev);
1161                 }
1162
1163                 if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
1164 #ifdef  VERBOSE
1165                         dump_regs(isp, "timer");
1166                         if (!stop)
1167                                 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1168 #endif
1169                         put_device(&isp->client.dev);
1170                 }
1171
1172                 if (isp->todo)
1173                         dev_vdbg(&isp->client.dev,
1174                                 "work done, todo = 0x%lx\n",
1175                                 isp->todo);
1176                 if (stop) {
1177                         dev_dbg(&isp->client.dev, "stop\n");
1178                         break;
1179                 }
1180         } while (isp->todo);
1181         isp->working = 0;
1182 }
1183
1184 static irqreturn_t isp1301_irq(int irq, void *isp, struct pt_regs *regs)
1185 {
1186         isp1301_defer_work(isp, WORK_UPDATE_OTG);
1187         return IRQ_HANDLED;
1188 }
1189
1190 static void isp1301_timer(unsigned long _isp)
1191 {
1192         isp1301_defer_work((void *)_isp, WORK_TIMER);
1193 }
1194
1195 /*-------------------------------------------------------------------------*/
1196
1197 static void isp1301_release(struct device *dev)
1198 {
1199         struct isp1301  *isp;
1200
1201         isp = container_of(dev, struct isp1301, client.dev);
1202
1203         /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
1204         if (isp->i2c_release)
1205                 isp->i2c_release(dev);
1206         kfree (isp);
1207 }
1208
1209 static struct isp1301 *the_transceiver;
1210
1211 static int isp1301_detach_client(struct i2c_client *i2c)
1212 {
1213         struct isp1301  *isp;
1214
1215         isp = container_of(i2c, struct isp1301, client);
1216
1217         isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1218         isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1219         free_irq(isp->irq, isp);
1220 #ifdef  CONFIG_USB_OTG
1221         otg_unbind(isp);
1222 #endif
1223         if (machine_is_omap_h2())
1224                 omap_free_gpio(2);
1225
1226         isp->timer.data = 0;
1227         set_bit(WORK_STOP, &isp->todo);
1228         del_timer_sync(&isp->timer);
1229         flush_scheduled_work();
1230
1231         put_device(&i2c->dev);
1232         the_transceiver = 0;
1233
1234         return i2c_detach_client(i2c);
1235 }
1236
1237 /*-------------------------------------------------------------------------*/
1238
1239 /* NOTE:  three modes are possible here, only one of which
1240  * will be standards-conformant on any given system:
1241  *
1242  *  - OTG mode (dual-role), required if there's a Mini-AB connector
1243  *  - HOST mode, for when there's one or more A (host) connectors
1244  *  - DEVICE mode, for when there's a B/Mini-B (device) connector
1245  *
1246  * As a rule, you won't have an isp1301 chip unless it's there to
1247  * support the OTG mode.  Other modes help testing USB controllers 
1248  * in isolation from (full) OTG support, or maybe so later board
1249  * revisions can help to support those feature.
1250  */
1251
1252 #ifdef  CONFIG_USB_OTG
1253
1254 static int isp1301_otg_enable(struct isp1301 *isp)
1255 {
1256         power_up(isp);
1257         otg_init(isp);
1258
1259         /* NOTE:  since we don't change this, this provides
1260          * a few more interrupts than are strictly needed.
1261          */
1262         isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1263                 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1264         isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1265                 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1266
1267         dev_info(&isp->client.dev, "ready for dual-role USB ...\n");
1268
1269         return 0;
1270 }
1271
1272 #endif
1273
1274 /* add or disable the host device+driver */
1275 static int
1276 isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1277 {
1278         struct isp1301  *isp = container_of(otg, struct isp1301, otg);
1279
1280         if (!otg || isp != the_transceiver)
1281                 return -ENODEV;
1282
1283         if (!host) {
1284                 OTG_IRQ_EN_REG = 0;
1285                 power_down(isp);
1286                 isp->otg.host = 0;
1287                 return 0;
1288         }
1289
1290 #ifdef  CONFIG_USB_OTG
1291         isp->otg.host = host;
1292         dev_dbg(&isp->client.dev, "registered host\n");
1293         host_suspend(isp);
1294         if (isp->otg.gadget)
1295                 return isp1301_otg_enable(isp);
1296         return 0;
1297
1298 #elif   !defined(CONFIG_USB_GADGET_OMAP)
1299         // FIXME update its refcount
1300         isp->otg.host = host;
1301
1302         power_up(isp);
1303
1304         if (machine_is_omap_h2())
1305                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1306
1307         dev_info(&isp->client.dev, "A-Host sessions ok\n");
1308         isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1309                 INTR_ID_GND);
1310         isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1311                 INTR_ID_GND);
1312
1313         /* If this has a Mini-AB connector, this mode is highly
1314          * nonstandard ... but can be handy for testing, especially with
1315          * the Mini-A end of an OTG cable.  (Or something nonstandard
1316          * like MiniB-to-StandardB, maybe built with a gender mender.)
1317          */
1318         isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
1319
1320         dump_regs(isp, __FUNCTION__);
1321
1322         return 0;
1323
1324 #else
1325         dev_dbg(&isp->client.dev, "host sessions not allowed\n");
1326         return -EINVAL;
1327 #endif
1328
1329 }
1330
1331 static int
1332 isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
1333 {
1334         struct isp1301  *isp = container_of(otg, struct isp1301, otg);
1335
1336         if (!otg || isp != the_transceiver)
1337                 return -ENODEV;
1338
1339         if (!gadget) {
1340                 OTG_IRQ_EN_REG = 0;
1341                 if (!isp->otg.default_a)
1342                         enable_vbus_draw(isp, 0);
1343                 usb_gadget_vbus_disconnect(isp->otg.gadget);
1344                 isp->otg.gadget = 0;
1345                 power_down(isp);
1346                 return 0;
1347         }
1348
1349 #ifdef  CONFIG_USB_OTG
1350         isp->otg.gadget = gadget;
1351         dev_dbg(&isp->client.dev, "registered gadget\n");
1352         /* gadget driver may be suspended until vbus_connect () */
1353         if (isp->otg.host)
1354                 return isp1301_otg_enable(isp);
1355         return 0;
1356
1357 #elif   !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
1358         isp->otg.gadget = gadget;
1359         // FIXME update its refcount
1360
1361         OTG_CTRL_REG = (OTG_CTRL_REG & OTG_CTRL_MASK
1362                                 & ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS))
1363                         | OTG_ID;
1364         power_up(isp);
1365         isp->otg.state = OTG_STATE_B_IDLE;
1366
1367         if (machine_is_omap_h2())
1368                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1369
1370         isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1371                 INTR_SESS_VLD);
1372         isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1373                 INTR_VBUS_VLD);
1374         dev_info(&isp->client.dev, "B-Peripheral sessions ok\n");
1375         dump_regs(isp, __FUNCTION__);
1376
1377         /* If this has a Mini-AB connector, this mode is highly
1378          * nonstandard ... but can be handy for testing, so long
1379          * as you don't plug a Mini-A cable into the jack.
1380          */
1381         if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
1382                 b_peripheral(isp);
1383
1384         return 0;
1385
1386 #else
1387         dev_dbg(&isp->client.dev, "peripheral sessions not allowed\n");
1388         return -EINVAL;
1389 #endif
1390 }
1391
1392
1393 /*-------------------------------------------------------------------------*/
1394
1395 static int
1396 isp1301_set_power(struct otg_transceiver *dev, unsigned mA)
1397 {
1398         if (!the_transceiver)
1399                 return -ENODEV;
1400         if (dev->state == OTG_STATE_B_PERIPHERAL)
1401                 enable_vbus_draw(the_transceiver, mA);
1402         return 0;
1403 }
1404
1405 static int
1406 isp1301_start_srp(struct otg_transceiver *dev)
1407 {
1408         struct isp1301  *isp = container_of(dev, struct isp1301, otg);
1409         u32             otg_ctrl;
1410
1411         if (!dev || isp != the_transceiver
1412                         || isp->otg.state != OTG_STATE_B_IDLE)
1413                 return -ENODEV;
1414
1415         otg_ctrl = OTG_CTRL_REG;
1416         if (!(otg_ctrl & OTG_BSESSEND))
1417                 return -EINVAL;
1418
1419         otg_ctrl |= OTG_B_BUSREQ;
1420         otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
1421         OTG_CTRL_REG = otg_ctrl;
1422         isp->otg.state = OTG_STATE_B_SRP_INIT;
1423
1424         pr_debug("otg: SRP, %s ... %06x\n", state_name(isp), OTG_CTRL_REG);
1425 #ifdef  CONFIG_USB_OTG
1426         check_state(isp, __FUNCTION__);
1427 #endif
1428         return 0;
1429 }
1430
1431 static int
1432 isp1301_start_hnp(struct otg_transceiver *dev)
1433 {
1434 #ifdef  CONFIG_USB_OTG
1435         struct isp1301  *isp = container_of(dev, struct isp1301, otg);
1436
1437         if (!dev || isp != the_transceiver)
1438                 return -ENODEV;
1439         if (isp->otg.default_a && (isp->otg.host == NULL
1440                         || !isp->otg.host->b_hnp_enable))
1441                 return -ENOTCONN;
1442         if (!isp->otg.default_a && (isp->otg.gadget == NULL
1443                         || !isp->otg.gadget->b_hnp_enable))
1444                 return -ENOTCONN;
1445
1446         /* We want hardware to manage most HNP protocol timings.
1447          * So do this part as early as possible...
1448          */
1449         switch (isp->otg.state) {
1450         case OTG_STATE_B_HOST:
1451                 isp->otg.state = OTG_STATE_B_PERIPHERAL;
1452                 /* caller will suspend next */
1453                 break;
1454         case OTG_STATE_A_HOST:
1455 #if 0
1456                 /* autoconnect mode avoids irq latency bugs */
1457                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1458                                 MC1_BDIS_ACON_EN);
1459 #endif
1460                 /* caller must suspend then clear A_BUSREQ */
1461                 usb_gadget_vbus_connect(isp->otg.gadget);
1462                 OTG_CTRL_REG |= OTG_A_SETB_HNPEN;
1463
1464                 break;
1465         case OTG_STATE_A_PERIPHERAL:
1466                 /* initiated by B-Host suspend */
1467                 break;
1468         default:
1469                 return -EILSEQ;
1470         }
1471         pr_debug("otg: HNP %s, %06x ...\n",
1472                 state_name(isp), OTG_CTRL_REG);
1473         check_state(isp, __FUNCTION__);
1474         return 0;
1475 #else
1476         /* srp-only */
1477         return -EINVAL;
1478 #endif
1479 }
1480
1481 /*-------------------------------------------------------------------------*/
1482
1483 /* no error returns, they'd just make bus scanning stop */
1484 static int isp1301_probe(struct i2c_adapter *bus, int address, int kind)
1485 {
1486         int                     status;
1487         struct isp1301          *isp;
1488         struct i2c_client       *i2c;
1489
1490         if (the_transceiver)
1491                 return 0;
1492
1493         isp = kzalloc(sizeof *isp, GFP_KERNEL);
1494         if (!isp)
1495                 return 0;
1496
1497         INIT_WORK(&isp->work, isp1301_work, isp);
1498         init_timer(&isp->timer);
1499         isp->timer.function = isp1301_timer;
1500         isp->timer.data = (unsigned long) isp;
1501
1502         isp->irq = -1;
1503         isp->client.addr = address;
1504         i2c_set_clientdata(&isp->client, isp);
1505         isp->client.adapter = bus;
1506         isp->client.driver = &isp1301_driver;
1507         strlcpy(isp->client.name, DRIVER_NAME, I2C_NAME_SIZE);
1508         i2c = &isp->client;
1509
1510         /* if this is a true probe, verify the chip ... */
1511         if (kind < 0) {
1512                 status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1513                 if (status != I2C_VENDOR_ID_PHILIPS) {
1514                         dev_dbg(&bus->dev, "addr %d not philips id: %d\n",
1515                                 address, status);
1516                         goto fail1;
1517                 }
1518                 status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1519                 if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1520                         dev_dbg(&bus->dev, "%d not isp1301, %d\n",
1521                                 address, status);
1522                         goto fail1;
1523                 }
1524         }
1525
1526         status = i2c_attach_client(i2c);
1527         if (status < 0) {
1528                 dev_dbg(&bus->dev, "can't attach %s to device %d, err %d\n",
1529                                 DRIVER_NAME, address, status);
1530 fail1:
1531                 kfree(isp);
1532                 return 0;
1533         }
1534         isp->i2c_release = i2c->dev.release;
1535         i2c->dev.release = isp1301_release;
1536
1537         /* initial development used chiprev 2.00 */
1538         status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
1539         dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
1540                 status >> 8, status & 0xff);
1541
1542         /* make like power-on reset */
1543         isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
1544
1545         isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
1546         isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
1547
1548         isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1549                                 OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
1550         isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1551                                 ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
1552
1553         isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
1554         isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1555         isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1556
1557 #ifdef  CONFIG_USB_OTG
1558         status = otg_bind(isp);
1559         if (status < 0) {
1560                 dev_dbg(&i2c->dev, "can't bind OTG\n");
1561                 goto fail2;
1562         }
1563 #endif
1564
1565         if (machine_is_omap_h2()) {
1566                 /* full speed signaling by default */
1567                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1568                         MC1_SPEED_REG);
1569                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
1570                         MC2_SPD_SUSP_CTRL);
1571
1572                 /* IRQ wired at M14 */
1573                 omap_cfg_reg(M14_1510_GPIO2);
1574                 isp->irq = OMAP_GPIO_IRQ(2);
1575                 omap_request_gpio(2);
1576                 omap_set_gpio_direction(2, 1);
1577                 omap_set_gpio_edge_ctrl(2, OMAP_GPIO_FALLING_EDGE);
1578         }
1579
1580         status = request_irq(isp->irq, isp1301_irq,
1581                         IRQF_SAMPLE_RANDOM, DRIVER_NAME, isp);
1582         if (status < 0) {
1583                 dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
1584                                 isp->irq, status);
1585 #ifdef  CONFIG_USB_OTG
1586 fail2:
1587 #endif
1588                 i2c_detach_client(i2c);
1589                 goto fail1;
1590         }
1591
1592         isp->otg.dev = &isp->client.dev;
1593         isp->otg.label = DRIVER_NAME;
1594
1595         isp->otg.set_host = isp1301_set_host,
1596         isp->otg.set_peripheral = isp1301_set_peripheral,
1597         isp->otg.set_power = isp1301_set_power,
1598         isp->otg.start_srp = isp1301_start_srp,
1599         isp->otg.start_hnp = isp1301_start_hnp,
1600
1601         enable_vbus_draw(isp, 0);
1602         power_down(isp);
1603         the_transceiver = isp;
1604
1605 #ifdef  CONFIG_USB_OTG
1606         update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
1607         update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
1608 #endif
1609
1610         dump_regs(isp, __FUNCTION__);
1611
1612 #ifdef  VERBOSE
1613         mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1614         dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
1615 #endif
1616
1617         status = otg_set_transceiver(&isp->otg);
1618         if (status < 0)
1619                 dev_err(&i2c->dev, "can't register transceiver, %d\n",
1620                         status);
1621
1622         return 0;
1623 }
1624
1625 static int isp1301_scan_bus(struct i2c_adapter *bus)
1626 {
1627         if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA
1628                         | I2C_FUNC_SMBUS_READ_WORD_DATA))
1629                 return -EINVAL;
1630         return i2c_probe(bus, &addr_data, isp1301_probe);
1631 }
1632
1633 static struct i2c_driver isp1301_driver = {
1634         .driver = {
1635                 .name   = "isp1301_omap",
1636         },
1637         .attach_adapter = isp1301_scan_bus,
1638         .detach_client  = isp1301_detach_client,
1639 };
1640
1641 /*-------------------------------------------------------------------------*/
1642
1643 static int __init isp_init(void)
1644 {
1645         return i2c_add_driver(&isp1301_driver);
1646 }
1647 module_init(isp_init);
1648
1649 static void __exit isp_exit(void)
1650 {
1651         if (the_transceiver)
1652                 otg_set_transceiver(0);
1653         i2c_del_driver(&isp1301_driver);
1654 }
1655 module_exit(isp_exit);
1656