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