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