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