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