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