2 * drivers/cbus/tahvo-usb.c
6 * Copyright (C) 2005 Nokia Corporation
8 * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
9 * Tony Lindgren <tony@atomide.com>, and
10 * Timo Teräs <timo.teras@nokia.com>
12 * This file is subject to the terms and conditions of the GNU General
13 * Public License. See the file "COPYING" in the main directory of this
14 * archive for more details.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/platform_device.h>
33 #include <linux/usb_ch9.h>
34 #include <linux/usb_gadget.h>
35 #include <linux/usb.h>
36 #include <linux/usb_otg.h>
37 #include <linux/i2c.h>
38 #include <linux/workqueue.h>
39 #include <linux/kobject.h>
40 #include <linux/clk.h>
43 #include <asm/semaphore.h>
44 #include <asm/arch/usb.h>
49 #define DRIVER_NAME "tahvo-usb"
51 #define USBR_SLAVE_CONTROL (1 << 8)
52 #define USBR_VPPVIO_SW (1 << 7)
53 #define USBR_SPEED (1 << 6)
54 #define USBR_REGOUT (1 << 5)
55 #define USBR_MASTER_SW2 (1 << 4)
56 #define USBR_MASTER_SW1 (1 << 3)
57 #define USBR_SLAVE_SW (1 << 2)
58 #define USBR_NSUSPEND (1 << 1)
59 #define USBR_SEMODE (1 << 0)
61 /* bits in OTG_CTRL_REG */
63 /* Bits that are controlled by OMAP OTG and are read-only */
64 #define OTG_CTRL_OMAP_MASK (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|\
65 OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
66 /* Bits that are controlled by transceiver */
67 #define OTG_CTRL_XCVR_MASK (OTG_ASESSVLD|OTG_BSESSEND|\
68 OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
69 /* Bits that are controlled by system */
70 #define OTG_CTRL_SYS_MASK (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|\
71 OTG_B_HNPEN|OTG_BUSDROP)
73 #if defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OTG)
74 #error tahvo-otg.c does not work with OCHI yet!
77 #define TAHVO_MODE_HOST 0
78 #define TAHVO_MODE_PERIPHERAL 1
81 #define TAHVO_MODE(tu) (tu)->tahvo_mode
82 #elif defined(CONFIG_USB_GADGET_OMAP)
83 #define TAHVO_MODE(tu) TAHVO_MODE_PERIPHERAL
85 #define TAHVO_MODE(tu) TAHVO_MODE_HOST
88 extern int ohci_omap_host_enable(struct usb_bus *host, int enable);
91 struct platform_device *pt_dev;
92 struct otg_transceiver otg;
94 struct work_struct irq_work;
95 struct semaphore serialize;
100 static struct platform_device tahvo_usb_device;
103 * ---------------------------------------------------------------------------
104 * OTG related functions
106 * These shoud be separated into omap-otg.c driver module, as they are used
107 * by various transceivers. These functions are needed in the UDC-only case
108 * as well. These functions are copied from GPL isp1301_omap.c
109 * ---------------------------------------------------------------------------
111 static struct platform_device *tahvo_otg_dev;
113 static irqreturn_t omap_otg_irq(int irq, void *arg, struct pt_regs *regs)
115 struct platform_device *otg_dev = (struct platform_device *) arg;
116 struct tahvo_usb *tu = (struct tahvo_usb *) otg_dev->dev.driver_data;
119 otg_irq = OTG_IRQ_SRC_REG;
120 if (otg_irq & OPRT_CHG) {
121 OTG_IRQ_SRC_REG = OPRT_CHG;
122 } else if (otg_irq & B_SRP_TMROUT) {
123 OTG_IRQ_SRC_REG = B_SRP_TMROUT;
124 } else if (otg_irq & B_HNP_FAIL) {
125 OTG_IRQ_SRC_REG = B_HNP_FAIL;
126 } else if (otg_irq & A_SRP_DETECT) {
127 OTG_IRQ_SRC_REG = A_SRP_DETECT;
128 } else if (otg_irq & A_REQ_TMROUT) {
129 OTG_IRQ_SRC_REG = A_REQ_TMROUT;
130 } else if (otg_irq & A_VBUS_ERR) {
131 OTG_IRQ_SRC_REG = A_VBUS_ERR;
132 } else if (otg_irq & DRIVER_SWITCH) {
134 if ((!(OTG_CTRL_REG & OTG_DRIVER_SEL)) &&
135 tu->otg.host && tu->otg.state == OTG_STATE_A_HOST) {
137 usb_bus_start_enum(tu->otg.host,
138 tu->otg.host->otg_port);
141 OTG_IRQ_SRC_REG = DRIVER_SWITCH;
149 static int omap_otg_init(void)
152 #ifdef CONFIG_USB_OTG
153 if (!tahvo_otg_dev) {
154 printk("tahvo-usb: no tahvo_otg_dev\n");
159 /* some of these values are board-specific... */
160 OTG_SYSCON_2_REG |= OTG_EN
162 | SRP_GPDATA /* 9msec Bdev D+ pulse */
163 | SRP_GPDVBUS /* discharge after VBUS pulse */
164 // | (3 << 24) /* 2msec VBUS pulse */
166 | (0 << 20) /* 200ms nominal A_WAIT_VRISE timer */
167 | SRP_DPW /* detect 167+ns SRP pulses */
168 | SRP_DATA | SRP_VBUS; /* accept both kinds of SRP pulse */
170 OTG_IRQ_EN_REG = DRIVER_SWITCH | OPRT_CHG
171 | B_SRP_TMROUT | B_HNP_FAIL
172 | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT;
173 OTG_SYSCON_2_REG |= OTG_EN;
178 static int omap_otg_probe(struct device *dev)
182 tahvo_otg_dev = to_platform_device(dev);
183 ret = omap_otg_init();
185 printk(KERN_ERR "tahvo-usb: omap_otg_init failed\n");
189 return request_irq(tahvo_otg_dev->resource[1].start,
190 omap_otg_irq, SA_INTERRUPT, DRIVER_NAME,
194 static int omap_otg_remove(struct device *dev)
196 tahvo_otg_dev = NULL;
197 free_irq(tahvo_otg_dev->resource[1].start, &tahvo_usb_device);
201 struct device_driver omap_otg_driver = {
203 .bus = &platform_bus_type,
204 .probe = omap_otg_probe,
205 .remove = omap_otg_remove,
209 * ---------------------------------------------------------------------------
210 * Tahvo related functions
211 * These are Nokia proprietary code, except for the OTG register settings,
212 * which are copied from isp1301.c
213 * ---------------------------------------------------------------------------
215 static ssize_t vbus_state_show(struct device *device,
216 struct device_attribute *attr, char *buf)
218 struct tahvo_usb *tu = (struct tahvo_usb*) device->driver_data;
219 return sprintf(buf, "%d\n", tu->vbus_state);
221 static DEVICE_ATTR(vbus_state, 0444, vbus_state_show, NULL);
225 static void check_vbus_state(struct tahvo_usb *tu)
229 reg = tahvo_read_reg(TAHVO_REG_IDSR);
232 switch (tu->otg.state) {
233 case OTG_STATE_B_IDLE:
234 /* Enable the gadget driver */
236 usb_gadget_vbus_connect(tu->otg.gadget);
237 /* Set B-session valid and not B-sessio ended to indicate
239 OTG_CTRL_REG = (OTG_CTRL_REG & ~OTG_BSESSEND) | OTG_BSESSVLD;
241 tu->otg.state = OTG_STATE_B_PERIPHERAL;
243 case OTG_STATE_A_IDLE:
244 /* Session is now valid assuming the USB hub is driving Vbus */
245 tu->otg.state = OTG_STATE_A_HOST;
247 ohci_omap_host_enable(tu->otg.host, 1);
252 printk("USB cable connected\n");
255 switch (tu->otg.state) {
256 case OTG_STATE_B_PERIPHERAL:
257 OTG_CTRL_REG = (OTG_CTRL_REG & ~OTG_BSESSVLD) | OTG_BSESSEND;
259 usb_gadget_vbus_disconnect(tu->otg.gadget);
260 tu->otg.state = OTG_STATE_B_IDLE;
262 case OTG_STATE_A_HOST:
263 tu->otg.state = OTG_STATE_A_IDLE;
265 ohci_omap_host_enable(tu->otg.host, 0);
271 printk("USB cable disconnected\n");
274 prev_state = tu->vbus_state;
275 tu->vbus_state = reg & 0x01;
276 if (prev_state != tu->vbus_state)
277 kobject_uevent(&tu->pt_dev->dev.kobj, KOBJ_CHANGE);
280 static void tahvo_usb_become_host(struct tahvo_usb *tu)
282 /* Clear system and transceiver controlled bits
283 * also mark the A-session is always valid */
285 OTG_CTRL_REG = (OTG_CTRL_REG & ~(OTG_CTRL_XCVR_MASK|OTG_CTRL_SYS_MASK))
288 /* Power up the transceiver in USB host mode */
289 tahvo_write_reg(TAHVO_REG_USBR, USBR_REGOUT | USBR_NSUSPEND |
290 USBR_MASTER_SW2 | USBR_MASTER_SW1);
291 tu->otg.state = OTG_STATE_A_IDLE;
293 check_vbus_state(tu);
296 static void tahvo_usb_stop_host(struct tahvo_usb *tu)
299 ohci_omap_host_enable(tu->otg.host, 0);
300 tu->otg.state = OTG_STATE_A_IDLE;
303 static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
305 /* Clear system and transceiver controlled bits
306 * and enable ID to mark peripheral mode and
307 * BSESSEND to mark no Vbus */
309 OTG_CTRL_REG = (OTG_CTRL_REG & ~(OTG_CTRL_XCVR_MASK|OTG_CTRL_SYS_MASK))
310 | OTG_ID | OTG_BSESSEND;
312 /* Power up transceiver and set it in USB perhiperal mode */
313 tahvo_write_reg(TAHVO_REG_USBR, USBR_SLAVE_CONTROL | USBR_REGOUT | USBR_NSUSPEND | USBR_SLAVE_SW);
314 tu->otg.state = OTG_STATE_B_IDLE;
316 check_vbus_state(tu);
319 static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
321 OTG_CTRL_REG = (OTG_CTRL_REG & ~OTG_BSESSVLD) | OTG_BSESSEND;
323 usb_gadget_vbus_disconnect(tu->otg.gadget);
324 tu->otg.state = OTG_STATE_B_IDLE;
328 static void tahvo_usb_power_off(struct tahvo_usb *tu)
332 /* Disable gadget controller if any */
334 usb_gadget_vbus_disconnect(tu->otg.gadget);
337 ohci_omap_host_enable(tu->otg.host, 0);
339 /* Disable OTG and interrupts */
340 if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
343 OTG_CTRL_REG = (OTG_CTRL_REG & ~(OTG_CTRL_XCVR_MASK|OTG_CTRL_SYS_MASK)) | id;
346 OTG_SYSCON_2_REG &= ~OTG_EN;
349 /* Power off transceiver */
350 tahvo_write_reg(TAHVO_REG_USBR, 0);
351 tu->otg.state = OTG_STATE_UNDEFINED;
355 static int tahvo_usb_set_power(struct otg_transceiver *dev, unsigned mA)
357 if (dev->state == OTG_STATE_B_PERIPHERAL) {
358 /* REVISIT: Can Tahvo charge battery from VBUS? */
363 static int tahvo_usb_set_suspend(struct otg_transceiver *dev, int suspend)
367 w = tahvo_read_reg(TAHVO_REG_USBR);
372 tahvo_write_reg(TAHVO_REG_USBR, w);
377 static int tahvo_usb_start_srp(struct otg_transceiver *dev)
379 struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
382 if (!dev || tu->otg.state != OTG_STATE_B_IDLE)
385 otg_ctrl = OTG_CTRL_REG;
386 if (!(otg_ctrl & OTG_BSESSEND))
389 otg_ctrl |= OTG_B_BUSREQ;
390 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_SYS_MASK;
391 OTG_CTRL_REG = otg_ctrl;
392 tu->otg.state = OTG_STATE_B_SRP_INIT;
394 pr_debug("otg: SRP, %s ... %06x\n", state_name(tu), OTG_CTRL_REG);
399 static int tahvo_usb_start_hnp(struct otg_transceiver *dev)
401 #ifdef CONFIG_USB_OTG
402 /* REVISIT: Add this for OTG */
407 static int tahvo_usb_set_host(struct otg_transceiver *otg, struct usb_bus *host)
409 struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
414 #if defined(CONFIG_USB_OTG) || !defined(CONFIG_USB_GADGET_OMAP)
416 down(&tu->serialize);
419 if (TAHVO_MODE(tu) == TAHVO_MODE_HOST)
420 tahvo_usb_power_off(tu);
426 if (TAHVO_MODE(tu) == TAHVO_MODE_HOST) {
428 tahvo_usb_become_host(tu);
430 ohci_omap_host_enable(host, 0);
436 /* No host mode configured, so do not allow host controlled to be set */
443 static int tahvo_usb_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
445 struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
450 #if defined(CONFIG_USB_OTG) || defined(CONFIG_USB_GADGET_OMAP)
452 down(&tu->serialize);
455 if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
456 tahvo_usb_power_off(tu);
462 tu->otg.gadget = gadget;
463 if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
464 tahvo_usb_become_peripheral(tu);
468 /* No gadget mode configured, so do not allow host controlled to be set */
475 static void tahvo_usb_irq_work(void *data)
477 struct tahvo_usb *tu = (struct tahvo_usb *)data;
479 down(&tu->serialize);
480 check_vbus_state(tu);
484 static void tahvo_usb_vbus_interrupt(unsigned long arg)
486 struct tahvo_usb *tu = (struct tahvo_usb *) arg;
488 tahvo_ack_irq(TAHVO_INT_VBUSON);
489 /* Seems we need this to acknowledge the interrupt */
490 tahvo_read_reg(TAHVO_REG_IDSR);
491 schedule_work(&tu->irq_work);
494 #ifdef CONFIG_USB_OTG
495 static ssize_t otg_mode_show(struct device *device,
496 struct device_attribute *attr, char *buf)
498 struct tahvo_usb *tu = (struct tahvo_usb*) device->driver_data;
499 switch (tu->tahvo_mode) {
500 case TAHVO_MODE_HOST:
501 return sprintf(buf, "host\n");
502 case TAHVO_MODE_PERIPHERAL:
503 return sprintf(buf, "peripheral\n");
505 return sprintf(buf, "unknown\n");
508 static ssize_t otg_mode_store(struct device *device,
509 struct device_attribute *attr,
510 const char *buf, size_t count)
512 struct tahvo_usb *tu = (struct tahvo_usb*) device->driver_data;
516 down(&tu->serialize);
517 if (strncmp(buf, "host", 4) == 0) {
518 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
519 tahvo_usb_stop_peripheral(tu);
520 tu->tahvo_mode = TAHVO_MODE_HOST;
522 printk(KERN_INFO "Selected HOST mode: host controller present.\n");
523 tahvo_usb_become_host(tu);
525 printk(KERN_INFO "Selected HOST mode: no host controller, powering off.\n");
526 tahvo_usb_power_off(tu);
528 } else if (strncmp(buf, "peripheral", 10) == 0) {
529 if (tu->tahvo_mode == TAHVO_MODE_HOST)
530 tahvo_usb_stop_host(tu);
531 tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
532 if (tu->otg.gadget) {
533 printk(KERN_INFO "Selected PERIPHERAL mode: gadget driver present.\n");
534 tahvo_usb_become_peripheral(tu);
536 printk(KERN_INFO "Selected PERIPHERAL mode: no gadget driver, powering off.\n");
537 tahvo_usb_power_off(tu);
546 static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store);
549 static int tahvo_usb_probe(struct device *dev)
551 struct tahvo_usb *tu;
554 /* Create driver data */
555 tu = kmalloc(sizeof(*tu), GFP_KERNEL);
558 memset(tu, 0, sizeof(*tu));
559 tu->pt_dev = container_of(dev, struct platform_device, dev);
560 #ifdef CONFIG_USB_OTG
562 #ifdef CONFIG_CBUS_TAHVO_USB_HOST_BY_DEFAULT
563 tu->tahvo_mode = TAHVO_MODE_HOST;
565 tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
569 INIT_WORK(&tu->irq_work, tahvo_usb_irq_work, tu);
570 init_MUTEX(&tu->serialize);
572 /* Set initial state, so that we generate kevents only on
574 tu->vbus_state = tahvo_read_reg(TAHVO_REG_IDSR) & 0x01;
576 /* We cannot enable interrupt until omap_udc is initialized */
577 ret = tahvo_request_irq(TAHVO_INT_VBUSON, tahvo_usb_vbus_interrupt,
578 (unsigned long) tu, "vbus_interrupt");
581 printk(KERN_ERR "Could not register Tahvo interrupt for VBUS\n");
586 device_create_file(dev, &dev_attr_vbus_state);
587 #ifdef CONFIG_USB_OTG
588 device_create_file(dev, &dev_attr_otg_mode);
591 /* Create OTG interface */
592 tahvo_usb_power_off(tu);
593 tu->otg.state = OTG_STATE_UNDEFINED;
594 tu->otg.label = DRIVER_NAME;
595 tu->otg.set_host = tahvo_usb_set_host;
596 tu->otg.set_peripheral = tahvo_usb_set_peripheral;
597 tu->otg.set_power = tahvo_usb_set_power;
598 tu->otg.set_suspend = tahvo_usb_set_suspend;
599 tu->otg.start_srp = tahvo_usb_start_srp;
600 tu->otg.start_hnp = tahvo_usb_start_hnp;
602 ret = otg_set_transceiver(&tu->otg);
604 printk(KERN_ERR "Cannot register USB transceiver\n");
606 tahvo_free_irq(TAHVO_INT_VBUSON);
610 dev->driver_data = tu;
612 /* Act upon current vbus state once at startup. A vbus state irq may or
613 * may not be generated in addition to this. */
614 schedule_work(&tu->irq_work);
618 static int tahvo_usb_remove(struct device *dev)
620 tahvo_free_irq(TAHVO_INT_VBUSON);
621 flush_scheduled_work();
622 otg_set_transceiver(0);
623 device_remove_file(dev, &dev_attr_vbus_state);
624 #ifdef CONFIG_USB_OTG
625 device_remove_file(dev, &dev_attr_otg_mode);
630 static struct device_driver tahvo_usb_driver = {
632 .bus = &platform_bus_type,
633 .probe = tahvo_usb_probe,
634 .remove = tahvo_usb_remove,
637 static struct platform_device tahvo_usb_device = {
642 static int __init tahvo_usb_init(void)
646 printk(KERN_INFO "Tahvo USB transceiver driver initializing\n");
647 ret = driver_register(&tahvo_usb_driver);
650 ret = platform_device_register(&tahvo_usb_device);
652 driver_unregister(&tahvo_usb_driver);
655 ret = driver_register(&omap_otg_driver);
657 platform_device_unregister(&tahvo_usb_device);
658 driver_unregister(&tahvo_usb_driver);
664 subsys_initcall(tahvo_usb_init);
666 static void __exit tahvo_usb_exit(void)
668 driver_unregister(&omap_otg_driver);
669 platform_device_unregister(&tahvo_usb_device);
670 driver_unregister(&tahvo_usb_driver);
672 module_exit(tahvo_usb_exit);
674 MODULE_DESCRIPTION("Tahvo USB OTG Transceiver Driver");
675 MODULE_LICENSE("GPL");
676 MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");