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