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