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