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