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