]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/isp1301_omap.c
Merge with /home/tmlind/src/kernel/linux-2.6
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / isp1301_omap.c
1 /*
2  * isp1301_omap - ISP 1301 USB transceiver, talking to OMAP OTG controller
3  *
4  * Copyright (C) 2004 Texas Instruments
5  * Copyright (C) 2004 David Brownell
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #undef  DEBUG
22 #undef  VERBOSE
23
24 #include <linux/config.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/platform_device.h>
31 #include <linux/usb_ch9.h>
32 #include <linux/usb_gadget.h>
33 #include <linux/usb.h>
34 #include <linux/usb_otg.h>
35 #include <linux/i2c.h>
36 #include <linux/workqueue.h>
37
38 #include <asm/irq.h>
39 #include <asm/mach-types.h>
40
41 #include <asm/arch/gpio.h>
42 #include <asm/arch/usb.h>
43 #include <asm/arch/mux.h>
44
45
46 #ifndef DEBUG
47 #undef  VERBOSE
48 #endif
49
50
51 #define DRIVER_VERSION  "24 August 2004"
52 #define DRIVER_NAME     (isp1301_driver.name)
53
54 MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
55 MODULE_LICENSE("GPL");
56
57 struct isp1301 {
58         struct otg_transceiver  otg;
59         struct i2c_client       client;
60         void                    (*i2c_release)(struct device *dev);
61
62         int                     irq;
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, struct pt_regs *regs)
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                                 SA_INTERRUPT, 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(void *data)
1123 {
1124         struct isp1301  *isp = data;
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, struct pt_regs *regs)
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         isp->timer.data = 0;
1249         set_bit(WORK_STOP, &isp->todo);
1250         del_timer_sync(&isp->timer);
1251         flush_scheduled_work();
1252
1253         put_device(&i2c->dev);
1254         the_transceiver = 0;
1255
1256         return i2c_detach_client(i2c);
1257 }
1258
1259 /*-------------------------------------------------------------------------*/
1260
1261 /* NOTE:  three modes are possible here, only one of which
1262  * will be standards-conformant on any given system:
1263  *
1264  *  - OTG mode (dual-role), required if there's a Mini-AB connector
1265  *  - HOST mode, for when there's one or more A (host) connectors
1266  *  - DEVICE mode, for when there's a B/Mini-B (device) connector
1267  *
1268  * As a rule, you won't have an isp1301 chip unless it's there to
1269  * support the OTG mode.  Other modes help testing USB controllers 
1270  * in isolation from (full) OTG support, or maybe so later board
1271  * revisions can help to support those feature.
1272  */
1273
1274 #ifdef  CONFIG_USB_OTG
1275
1276 static int isp1301_otg_enable(struct isp1301 *isp)
1277 {
1278         power_up(isp);
1279         otg_init(isp);
1280
1281         /* NOTE:  since we don't change this, this provides
1282          * a few more interrupts than are strictly needed.
1283          */
1284         isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1285                 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1286         isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1287                 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1288
1289         dev_info(&isp->client.dev, "ready for dual-role USB ...\n");
1290
1291         return 0;
1292 }
1293
1294 #endif
1295
1296 /* add or disable the host device+driver */
1297 static int
1298 isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1299 {
1300         struct isp1301  *isp = container_of(otg, struct isp1301, otg);
1301
1302         if (!otg || isp != the_transceiver)
1303                 return -ENODEV;
1304
1305         if (!host) {
1306                 OTG_IRQ_EN_REG = 0;
1307                 power_down(isp);
1308                 isp->otg.host = 0;
1309                 return 0;
1310         }
1311
1312 #ifdef  CONFIG_USB_OTG
1313         isp->otg.host = host;
1314         dev_dbg(&isp->client.dev, "registered host\n");
1315         host_suspend(isp);
1316         if (isp->otg.gadget)
1317                 return isp1301_otg_enable(isp);
1318         return 0;
1319
1320 #elif   !defined(CONFIG_USB_GADGET_OMAP)
1321         // FIXME update its refcount
1322         isp->otg.host = host;
1323
1324         power_up(isp);
1325
1326         if (machine_is_omap_h2() || machine_is_omap_h3())
1327                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1328
1329         dev_info(&isp->client.dev, "A-Host sessions ok\n");
1330         isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1331                 INTR_ID_GND);
1332         isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1333                 INTR_ID_GND);
1334
1335         /* If this has a Mini-AB connector, this mode is highly
1336          * nonstandard ... but can be handy for testing, especially with
1337          * the Mini-A end of an OTG cable.  (Or something nonstandard
1338          * like MiniB-to-StandardB, maybe built with a gender mender.)
1339          */
1340         isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
1341
1342         dump_regs(isp, __FUNCTION__);
1343
1344         return 0;
1345
1346 #else
1347         dev_dbg(&isp->client.dev, "host sessions not allowed\n");
1348         return -EINVAL;
1349 #endif
1350
1351 }
1352
1353 static int
1354 isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
1355 {
1356         struct isp1301  *isp = container_of(otg, struct isp1301, otg);
1357
1358         if (!otg || isp != the_transceiver)
1359                 return -ENODEV;
1360
1361         if (!gadget) {
1362                 OTG_IRQ_EN_REG = 0;
1363                 if (!isp->otg.default_a)
1364                         enable_vbus_draw(isp, 0);
1365                 usb_gadget_vbus_disconnect(isp->otg.gadget);
1366                 isp->otg.gadget = 0;
1367                 power_down(isp);
1368                 return 0;
1369         }
1370
1371 #ifdef  CONFIG_USB_OTG
1372         isp->otg.gadget = gadget;
1373         dev_dbg(&isp->client.dev, "registered gadget\n");
1374         /* gadget driver may be suspended until vbus_connect () */
1375         if (isp->otg.host)
1376                 return isp1301_otg_enable(isp);
1377         return 0;
1378
1379 #elif   !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
1380         isp->otg.gadget = gadget;
1381         // FIXME update its refcount
1382
1383         OTG_CTRL_REG = (OTG_CTRL_REG & OTG_CTRL_MASK
1384                                 & ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS))
1385                         | OTG_ID;
1386         power_up(isp);
1387         isp->otg.state = OTG_STATE_B_IDLE;
1388
1389         if (machine_is_omap_h2() || machine_is_omap_h3())
1390                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1391
1392         isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1393                 INTR_SESS_VLD | INTR_VBUS_VLD);
1394         isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1395                 INTR_VBUS_VLD | INTR_SESS_VLD);
1396         dev_info(&isp->client.dev, "B-Peripheral sessions ok\n");
1397         dump_regs(isp, __FUNCTION__);
1398
1399         /* If this has a Mini-AB connector, this mode is highly
1400          * nonstandard ... but can be handy for testing, so long
1401          * as you don't plug a Mini-A cable into the jack.
1402          */
1403         if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
1404                 b_peripheral(isp);
1405
1406         return 0;
1407
1408 #else
1409         dev_dbg(&isp->client.dev, "peripheral sessions not allowed\n");
1410         return -EINVAL;
1411 #endif
1412 }
1413
1414
1415 /*-------------------------------------------------------------------------*/
1416
1417 static int
1418 isp1301_set_power(struct otg_transceiver *dev, unsigned mA)
1419 {
1420         if (!the_transceiver)
1421                 return -ENODEV;
1422         if (dev->state == OTG_STATE_B_PERIPHERAL)
1423                 enable_vbus_draw(the_transceiver, mA);
1424         return 0;
1425 }
1426
1427 static int
1428 isp1301_start_srp(struct otg_transceiver *dev)
1429 {
1430         struct isp1301  *isp = container_of(dev, struct isp1301, otg);
1431         u32             otg_ctrl;
1432
1433         if (!dev || isp != the_transceiver
1434                         || isp->otg.state != OTG_STATE_B_IDLE)
1435                 return -ENODEV;
1436
1437         otg_ctrl = OTG_CTRL_REG;
1438         if (!(otg_ctrl & OTG_BSESSEND))
1439                 return -EINVAL;
1440
1441         otg_ctrl |= OTG_B_BUSREQ;
1442         otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
1443         OTG_CTRL_REG = otg_ctrl;
1444         isp->otg.state = OTG_STATE_B_SRP_INIT;
1445
1446         pr_debug("otg: SRP, %s ... %06x\n", state_name(isp), OTG_CTRL_REG);
1447 #ifdef  CONFIG_USB_OTG
1448         check_state(isp, __FUNCTION__);
1449 #endif
1450         return 0;
1451 }
1452
1453 static int
1454 isp1301_start_hnp(struct otg_transceiver *dev)
1455 {
1456 #ifdef  CONFIG_USB_OTG
1457         struct isp1301  *isp = container_of(dev, struct isp1301, otg);
1458
1459         if (!dev || isp != the_transceiver)
1460                 return -ENODEV;
1461         if (isp->otg.default_a && (isp->otg.host == NULL
1462                         || !isp->otg.host->b_hnp_enable))
1463                 return -ENOTCONN;
1464         if (!isp->otg.default_a && (isp->otg.gadget == NULL
1465                         || !isp->otg.gadget->b_hnp_enable))
1466                 return -ENOTCONN;
1467
1468         /* We want hardware to manage most HNP protocol timings.
1469          * So do this part as early as possible...
1470          */
1471         switch (isp->otg.state) {
1472         case OTG_STATE_B_PERIPHERAL:
1473                 isp->otg.state = OTG_STATE_B_WAIT_ACON;
1474                 isp1301_defer_work(isp, WORK_UPDATE_ISP);
1475                 break;
1476         case OTG_STATE_B_HOST:
1477                 isp->otg.state = OTG_STATE_B_PERIPHERAL;
1478                 /* caller will suspend next */
1479                 break;
1480         case OTG_STATE_A_HOST:
1481 #if 0
1482                 /* autoconnect mode avoids irq latency bugs */
1483                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1484                                 MC1_BDIS_ACON_EN);
1485 #endif
1486                 /* caller must suspend then clear A_BUSREQ */
1487                 usb_gadget_vbus_connect(isp->otg.gadget);
1488                 OTG_CTRL_REG |= OTG_A_SETB_HNPEN;
1489
1490                 break;
1491         case OTG_STATE_A_PERIPHERAL:
1492                 /* initiated by B-Host suspend */
1493                 break;
1494         default:
1495                 return -EILSEQ;
1496         }
1497         pr_debug("otg: HNP %s, %06x ...\n",
1498                 state_name(isp), OTG_CTRL_REG);
1499         check_state(isp, __FUNCTION__);
1500         return 0;
1501 #else
1502         /* srp-only */
1503         return -EINVAL;
1504 #endif
1505 }
1506
1507 /*-------------------------------------------------------------------------*/
1508
1509 /* no error returns, they'd just make bus scanning stop */
1510 static int isp1301_probe(struct i2c_adapter *bus, int address, int kind)
1511 {
1512         int                     status;
1513         struct isp1301          *isp;
1514         struct i2c_client       *i2c;
1515
1516         if (the_transceiver)
1517                 return 0;
1518
1519         isp = kzalloc(sizeof *isp, GFP_KERNEL);
1520         if (!isp)
1521                 return 0;
1522
1523         INIT_WORK(&isp->work, isp1301_work, isp);
1524         init_timer(&isp->timer);
1525         isp->timer.function = isp1301_timer;
1526         isp->timer.data = (unsigned long) isp;
1527
1528         isp->irq = -1;
1529         isp->client.addr = address;
1530         i2c_set_clientdata(&isp->client, isp);
1531         isp->client.adapter = bus;
1532         isp->client.driver = &isp1301_driver;
1533         strlcpy(isp->client.name, DRIVER_NAME, I2C_NAME_SIZE);
1534         i2c = &isp->client;
1535
1536         /* if this is a true probe, verify the chip ... */
1537         if (kind < 0) {
1538                 status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1539                 if (status != I2C_VENDOR_ID_PHILIPS) {
1540                         dev_dbg(&bus->dev, "addr %d not philips id: %d\n",
1541                                 address, status);
1542                         goto fail1;
1543                 }
1544                 status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1545                 if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1546                         dev_dbg(&bus->dev, "%d not isp1301, %d\n",
1547                                 address, status);
1548                         goto fail1;
1549                 }
1550         }
1551
1552         status = i2c_attach_client(i2c);
1553         if (status < 0) {
1554                 dev_dbg(&bus->dev, "can't attach %s to device %d, err %d\n",
1555                                 DRIVER_NAME, address, status);
1556 fail1:
1557                 kfree(isp);
1558                 return 0;
1559         }
1560         isp->i2c_release = i2c->dev.release;
1561         i2c->dev.release = isp1301_release;
1562
1563         /* initial development used chiprev 2.00 */
1564         status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
1565         dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
1566                 status >> 8, status & 0xff);
1567
1568         /* make like power-on reset */
1569         isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
1570
1571         isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
1572         isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
1573
1574         isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1575                                 OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
1576         isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1577                                 ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
1578
1579         isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
1580         isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1581         isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1582
1583 #ifdef  CONFIG_USB_OTG
1584         status = otg_bind(isp);
1585         if (status < 0) {
1586                 dev_dbg(&i2c->dev, "can't bind OTG\n");
1587                 goto fail2;
1588         }
1589 #endif
1590
1591         if (machine_is_omap_h2() || machine_is_omap_h3()) {
1592                 /* full speed signaling by default */
1593                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1594                         MC1_SPEED_REG);
1595                 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
1596                         MC2_SPD_SUSP_CTRL);
1597         }
1598
1599         if (machine_is_omap_h2()) {
1600                 /* IRQ wired at M14 */
1601                 omap_cfg_reg(M14_1510_GPIO2);
1602                 isp->irq = OMAP_GPIO_IRQ(2);
1603                 omap_request_gpio(2);
1604                 omap_set_gpio_direction(2, 1);
1605                 set_irq_type(isp->irq, IRQT_FALLING);
1606         }
1607
1608         if (machine_is_omap_h3()) {
1609                 /* IRQ wired at N21 */
1610                 omap_cfg_reg(N21_1710_GPIO14);
1611                 isp->irq = OMAP_GPIO_IRQ(14);
1612                 omap_request_gpio(14);
1613                 omap_set_gpio_direction(14, 1);
1614                 set_irq_type(isp->irq, IRQT_FALLING);
1615         }
1616
1617         status = request_irq(isp->irq, isp1301_irq,
1618                         SA_SAMPLE_RANDOM, DRIVER_NAME, isp);
1619         if (status < 0) {
1620                 dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
1621                                 isp->irq, status);
1622 #ifdef  CONFIG_USB_OTG
1623 fail2:
1624 #endif
1625                 i2c_detach_client(i2c);
1626                 goto fail1;
1627         }
1628
1629         isp->otg.dev = &isp->client.dev;
1630         isp->otg.label = DRIVER_NAME;
1631
1632         isp->otg.set_host = isp1301_set_host,
1633         isp->otg.set_peripheral = isp1301_set_peripheral,
1634         isp->otg.set_power = isp1301_set_power,
1635         isp->otg.start_srp = isp1301_start_srp,
1636         isp->otg.start_hnp = isp1301_start_hnp,
1637
1638         enable_vbus_draw(isp, 0);
1639         power_down(isp);
1640         the_transceiver = isp;
1641
1642 #ifdef  CONFIG_USB_OTG
1643         update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
1644         update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
1645 #endif
1646
1647         dump_regs(isp, __FUNCTION__);
1648
1649 #ifdef  VERBOSE
1650         mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1651         dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
1652 #endif
1653
1654         status = otg_set_transceiver(&isp->otg);
1655         if (status < 0)
1656                 dev_err(&i2c->dev, "can't register transceiver, %d\n",
1657                         status);
1658
1659         return 0;
1660 }
1661
1662 static int isp1301_scan_bus(struct i2c_adapter *bus)
1663 {
1664         if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA
1665                         | I2C_FUNC_SMBUS_READ_WORD_DATA))
1666                 return -EINVAL;
1667         return i2c_probe(bus, &addr_data, isp1301_probe);
1668 }
1669
1670 static struct i2c_driver isp1301_driver = {
1671         .driver = {
1672                 .name   = "isp1301_omap",
1673         },
1674         .id             = 1301,         /* FIXME "official", i2c-ids.h */
1675         .class          = I2C_CLASS_HWMON,
1676         .attach_adapter = isp1301_scan_bus,
1677         .detach_client  = isp1301_detach_client,
1678 };
1679
1680 /*-------------------------------------------------------------------------*/
1681
1682 static int __init isp_init(void)
1683 {
1684         return i2c_add_driver(&isp1301_driver);
1685 }
1686 module_init(isp_init);
1687
1688 static void __exit isp_exit(void)
1689 {
1690         if (the_transceiver)
1691                 otg_set_transceiver(0);
1692         i2c_del_driver(&isp1301_driver);
1693 }
1694 module_exit(isp_exit);
1695