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