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