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