]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/cbus/tahvo-usb.c
ARM: OMAP: Misc compile fixes
[linux-2.6-omap-h63xx.git] / drivers / cbus / tahvo-usb.c
1 /**
2  * drivers/cbus/tahvo-usb.c
3  *
4  * Tahvo USB transeiver
5  *
6  * Copyright (C) 2005-2006 Nokia Corporation
7  *
8  * Parts copied from drivers/i2c/chips/isp1301_omap.c
9  * Copyright (C) 2004 Texas Instruments
10  * Copyright (C) 2004 David Brownell
11  *
12  * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
13  *            Tony Lindgren <tony@atomide.com>, and
14  *            Timo Teräs <timo.teras@nokia.com>
15  *
16  * This file is subject to the terms and conditions of the GNU General
17  * Public License. See the file "COPYING" in the main directory of this
18  * archive for more details.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/io.h>
35 #include <linux/interrupt.h>
36 #include <linux/platform_device.h>
37 #include <linux/usb/ch9.h>
38 #include <linux/usb/gadget.h>
39 #include <linux/usb.h>
40 #include <linux/usb/otg.h>
41 #include <linux/i2c.h>
42 #include <linux/workqueue.h>
43 #include <linux/kobject.h>
44 #include <linux/clk.h>
45 #include <linux/mutex.h>
46
47 #include <asm/irq.h>
48 #include <asm/arch/usb.h>
49
50 #include "cbus.h"
51 #include "tahvo.h"
52
53 #define DRIVER_NAME     "tahvo-usb"
54
55 #define USBR_SLAVE_CONTROL      (1 << 8)
56 #define USBR_VPPVIO_SW          (1 << 7)
57 #define USBR_SPEED              (1 << 6)
58 #define USBR_REGOUT             (1 << 5)
59 #define USBR_MASTER_SW2         (1 << 4)
60 #define USBR_MASTER_SW1         (1 << 3)
61 #define USBR_SLAVE_SW           (1 << 2)
62 #define USBR_NSUSPEND           (1 << 1)
63 #define USBR_SEMODE             (1 << 0)
64
65 /* bits in OTG_CTRL_REG */
66
67 /* Bits that are controlled by OMAP OTG and are read-only */
68 #define OTG_CTRL_OMAP_MASK      (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|\
69                                 OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
70 /* Bits that are controlled by transceiver */
71 #define OTG_CTRL_XCVR_MASK      (OTG_ASESSVLD|OTG_BSESSEND|\
72                                 OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
73 /* Bits that are controlled by system */
74 #define OTG_CTRL_SYS_MASK       (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|\
75                                 OTG_B_HNPEN|OTG_BUSDROP)
76
77 #if defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OTG)
78 #error tahvo-otg.c does not work with OCHI yet!
79 #endif
80
81 #define TAHVO_MODE_HOST         0
82 #define TAHVO_MODE_PERIPHERAL   1
83
84 #ifdef CONFIG_USB_OTG
85 #define TAHVO_MODE(tu)          (tu)->tahvo_mode
86 #elif defined(CONFIG_USB_GADGET_OMAP)
87 #define TAHVO_MODE(tu)          TAHVO_MODE_PERIPHERAL
88 #else
89 #define TAHVO_MODE(tu)          TAHVO_MODE_HOST
90 #endif
91
92 struct tahvo_usb {
93         struct platform_device *pt_dev;
94         struct otg_transceiver otg;
95         int vbus_state;
96         struct work_struct irq_work;
97         struct mutex serialize;
98 #ifdef CONFIG_USB_OTG
99         int tahvo_mode;
100 #endif
101 };
102 static struct platform_device tahvo_usb_device;
103
104 /*
105  * ---------------------------------------------------------------------------
106  * OTG related functions
107  *
108  * These shoud be separated into omap-otg.c driver module, as they are used
109  * by various transceivers. These functions are needed in the UDC-only case
110  * as well. These functions are copied from GPL isp1301_omap.c
111  * ---------------------------------------------------------------------------
112  */
113 static struct platform_device *tahvo_otg_dev;
114
115 static irqreturn_t omap_otg_irq(int irq, void *arg)
116 {
117         struct platform_device *otg_dev = (struct platform_device *) arg;
118         struct tahvo_usb *tu = (struct tahvo_usb *) otg_dev->dev.driver_data;
119         u16 otg_irq;
120
121         otg_irq = OTG_IRQ_SRC_REG;
122         if (otg_irq & OPRT_CHG) {
123                 OTG_IRQ_SRC_REG = OPRT_CHG;
124         } else if (otg_irq & B_SRP_TMROUT) {
125                 OTG_IRQ_SRC_REG = B_SRP_TMROUT;
126         } else if (otg_irq & B_HNP_FAIL) {
127                 OTG_IRQ_SRC_REG = B_HNP_FAIL;
128         } else if (otg_irq & A_SRP_DETECT) {
129                 OTG_IRQ_SRC_REG = A_SRP_DETECT;
130         } else if (otg_irq & A_REQ_TMROUT) {
131                 OTG_IRQ_SRC_REG = A_REQ_TMROUT;
132         } else if (otg_irq & A_VBUS_ERR) {
133                 OTG_IRQ_SRC_REG = A_VBUS_ERR;
134         } else if (otg_irq & DRIVER_SWITCH) {
135                 if ((!(OTG_CTRL_REG & OTG_DRIVER_SEL)) &&
136                    tu->otg.host && tu->otg.state == OTG_STATE_A_HOST) {
137                         /* role is host */
138                         usb_bus_start_enum(tu->otg.host,
139                                            tu->otg.host->otg_port);
140                 }
141                 OTG_IRQ_SRC_REG = DRIVER_SWITCH;
142         } else
143                 return IRQ_NONE;
144
145         return IRQ_HANDLED;
146
147 }
148
149 static int omap_otg_init(void)
150 {
151
152 #ifdef CONFIG_USB_OTG
153         if (!tahvo_otg_dev) {
154                 printk("tahvo-usb: no tahvo_otg_dev\n");
155                 return -ENODEV;
156         }
157 #endif
158         OTG_SYSCON_1_REG &= ~OTG_IDLE_EN;
159         udelay(100);
160
161         /* some of these values are board-specific... */
162         OTG_SYSCON_2_REG |= OTG_EN
163                 /* for B-device: */
164                 | SRP_GPDATA            /* 9msec Bdev D+ pulse */
165                 | SRP_GPDVBUS           /* discharge after VBUS pulse */
166                 // | (3 << 24)          /* 2msec VBUS pulse */
167                 /* for A-device: */
168                 | (0 << 20)             /* 200ms nominal A_WAIT_VRISE timer */
169                 | SRP_DPW               /* detect 167+ns SRP pulses */
170                 | SRP_DATA | SRP_VBUS;  /* accept both kinds of SRP pulse */
171
172         OTG_IRQ_EN_REG = DRIVER_SWITCH | OPRT_CHG
173                         | B_SRP_TMROUT | B_HNP_FAIL
174                         | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT;
175         OTG_SYSCON_2_REG |= OTG_EN;
176
177         return 0;
178 }
179
180 static int omap_otg_probe(struct device *dev)
181 {
182         int ret;
183
184         tahvo_otg_dev = to_platform_device(dev);
185         ret = omap_otg_init();
186         if (ret != 0) {
187                 printk(KERN_ERR "tahvo-usb: omap_otg_init failed\n");
188                 return ret;
189         }
190
191         return request_irq(tahvo_otg_dev->resource[1].start,
192                            omap_otg_irq, IRQF_DISABLED, DRIVER_NAME,
193                            &tahvo_usb_device);
194 }
195
196 static int omap_otg_remove(struct device *dev)
197 {
198         free_irq(tahvo_otg_dev->resource[1].start, &tahvo_usb_device);
199         tahvo_otg_dev = NULL;
200
201         return 0;
202 }
203
204 struct device_driver omap_otg_driver = {
205         .name           = "omap_otg",
206         .bus            = &platform_bus_type,
207         .probe          = omap_otg_probe,
208         .remove         = omap_otg_remove,
209 };
210
211 /*
212  * ---------------------------------------------------------------------------
213  * Tahvo related functions
214  * These are Nokia proprietary code, except for the OTG register settings,
215  * which are copied from isp1301.c
216  * ---------------------------------------------------------------------------
217  */
218 static ssize_t vbus_state_show(struct device *device,
219                                struct device_attribute *attr, char *buf)
220 {
221         struct tahvo_usb *tu = (struct tahvo_usb*) device->driver_data;
222         return sprintf(buf, "%d\n", tu->vbus_state);
223 }
224 static DEVICE_ATTR(vbus_state, 0444, vbus_state_show, NULL);
225
226 int vbus_active = 0;
227
228 #if 0
229
230 static int host_suspend(struct tahvo_usb *tu)
231 {
232         struct device   *dev;
233
234         if (!tu->otg.host)
235                 return -ENODEV;
236
237         /* Currently ASSUMES only the OTG port matters;
238          * other ports could be active...
239          */
240         dev = tu->otg.host->controller;
241         return dev->driver->suspend(dev, PMSG_SUSPEND);
242 }
243
244 static int host_resume(struct tahvo_usb *tu)
245 {
246         struct device   *dev;
247
248         if (!tu->otg.host)
249                 return -ENODEV;
250
251         dev = tu->otg.host->controller;
252         return dev->driver->resume(dev);
253 }
254
255 #else
256
257 static int host_suspend(struct tahvo_usb *tu)
258 {
259         return 0;
260 }
261
262 static int host_resume(struct tahvo_usb *tu)
263 {
264         return 0;
265 }
266
267 #endif
268
269 static void check_vbus_state(struct tahvo_usb *tu)
270 {
271         int reg, prev_state;
272
273         reg = tahvo_read_reg(TAHVO_REG_IDSR);
274         if (reg & 0x01) {
275                 vbus_active = 1;
276                 switch (tu->otg.state) {
277                 case OTG_STATE_B_IDLE:
278                         /* Enable the gadget driver */
279                         if (tu->otg.gadget)
280                                 usb_gadget_vbus_connect(tu->otg.gadget);
281                         /* Set B-session valid and not B-sessio ended to indicate
282                          * Vbus to be ok. */
283                         OTG_CTRL_REG = (OTG_CTRL_REG & ~OTG_BSESSEND) | OTG_BSESSVLD;
284
285                         tu->otg.state = OTG_STATE_B_PERIPHERAL;
286                         break;
287                 case OTG_STATE_A_IDLE:
288                         /* Session is now valid assuming the USB hub is driving Vbus */
289                         tu->otg.state = OTG_STATE_A_HOST;
290                         host_resume(tu);
291                         break;
292                 default:
293                         break;
294                 }
295                 printk("USB cable connected\n");
296         } else {
297                 switch (tu->otg.state) {
298                 case OTG_STATE_B_PERIPHERAL:
299                         if (tu->otg.gadget)
300                                 usb_gadget_vbus_disconnect(tu->otg.gadget);
301                         tu->otg.state = OTG_STATE_B_IDLE;
302                         break;
303                 case OTG_STATE_A_HOST:
304                         tu->otg.state = OTG_STATE_A_IDLE;
305                         break;
306                 default:
307                         break;
308                 }
309                 printk("USB cable disconnected\n");
310                 vbus_active = 0;
311         }
312
313         prev_state = tu->vbus_state;
314         tu->vbus_state = reg & 0x01;
315         if (prev_state != tu->vbus_state)
316                 sysfs_notify(&tu->pt_dev->dev.kobj, NULL, "vbus_state");
317 }
318
319 static void tahvo_usb_become_host(struct tahvo_usb *tu)
320 {
321         u32 l;
322
323         /* Clear system and transceiver controlled bits
324          * also mark the A-session is always valid */
325         omap_otg_init();
326
327         l = OTG_CTRL_REG;
328         l &= ~(OTG_CTRL_XCVR_MASK|OTG_CTRL_SYS_MASK);
329         l |= OTG_ASESSVLD;
330         OTG_CTRL_REG = l;
331
332         /* Power up the transceiver in USB host mode */
333         tahvo_write_reg(TAHVO_REG_USBR, USBR_REGOUT | USBR_NSUSPEND |
334                         USBR_MASTER_SW2 | USBR_MASTER_SW1);
335         tu->otg.state = OTG_STATE_A_IDLE;
336
337         check_vbus_state(tu);
338 }
339
340 static void tahvo_usb_stop_host(struct tahvo_usb *tu)
341 {
342         host_suspend(tu);
343         tu->otg.state = OTG_STATE_A_IDLE;
344 }
345
346 static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
347 {
348         /* Clear system and transceiver controlled bits
349          * and enable ID to mark peripheral mode and
350          * BSESSEND to mark no Vbus */
351         omap_otg_init();
352         OTG_CTRL_REG = (OTG_CTRL_REG & ~(OTG_CTRL_XCVR_MASK|OTG_CTRL_SYS_MASK|OTG_BSESSVLD))
353                 | OTG_ID | OTG_BSESSEND;
354
355         /* Power up transceiver and set it in USB perhiperal mode */
356         tahvo_write_reg(TAHVO_REG_USBR, USBR_SLAVE_CONTROL | USBR_REGOUT | USBR_NSUSPEND | USBR_SLAVE_SW);
357         tu->otg.state = OTG_STATE_B_IDLE;
358
359         check_vbus_state(tu);
360 }
361
362 static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
363 {
364         OTG_CTRL_REG = (OTG_CTRL_REG & ~OTG_BSESSVLD) | OTG_BSESSEND;
365         if (tu->otg.gadget)
366                 usb_gadget_vbus_disconnect(tu->otg.gadget);
367         tu->otg.state = OTG_STATE_B_IDLE;
368
369 }
370
371 static void tahvo_usb_power_off(struct tahvo_usb *tu)
372 {
373         u32 l;
374         int id;
375
376         /* Disable gadget controller if any */
377         if (tu->otg.gadget)
378                 usb_gadget_vbus_disconnect(tu->otg.gadget);
379
380         host_suspend(tu);
381
382         /* Disable OTG and interrupts */
383         if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
384                 id = OTG_ID;
385         else
386                 id = 0;
387         l = OTG_CTRL_REG;
388         l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD);
389         l |= id | OTG_BSESSEND;
390         OTG_CTRL_REG = l;
391         OTG_IRQ_EN_REG = 0;
392
393         OTG_SYSCON_2_REG &= ~OTG_EN;
394
395         OTG_SYSCON_1_REG |= OTG_IDLE_EN;
396
397         /* Power off transceiver */
398         tahvo_write_reg(TAHVO_REG_USBR, 0);
399         tu->otg.state = OTG_STATE_UNDEFINED;
400 }
401
402
403 static int tahvo_usb_set_power(struct otg_transceiver *dev, unsigned mA)
404 {
405         struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
406
407         dev_dbg(&tu->pt_dev->dev, "set_power %d mA\n", mA);
408
409         if (dev->state == OTG_STATE_B_PERIPHERAL) {
410                 /* REVISIT: Can Tahvo charge battery from VBUS? */
411         }
412         return 0;
413 }
414
415 static int tahvo_usb_set_suspend(struct otg_transceiver *dev, int suspend)
416 {
417         struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
418         u16 w;
419
420         dev_dbg(&tu->pt_dev->dev, "set_suspend\n");
421
422         w = tahvo_read_reg(TAHVO_REG_USBR);
423         if (suspend)
424                 w &= ~USBR_NSUSPEND;
425         else
426                 w |= USBR_NSUSPEND;
427         tahvo_write_reg(TAHVO_REG_USBR, w);
428
429         return 0;
430 }
431
432 static int tahvo_usb_start_srp(struct otg_transceiver *dev)
433 {
434         struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
435         u32 otg_ctrl;
436
437         dev_dbg(&tu->pt_dev->dev, "start_srp\n");
438
439         if (!dev || tu->otg.state != OTG_STATE_B_IDLE)
440                 return -ENODEV;
441
442         otg_ctrl = OTG_CTRL_REG;
443         if (!(otg_ctrl & OTG_BSESSEND))
444                 return -EINVAL;
445
446         otg_ctrl |= OTG_B_BUSREQ;
447         otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_SYS_MASK;
448         OTG_CTRL_REG = otg_ctrl;
449         tu->otg.state = OTG_STATE_B_SRP_INIT;
450
451         return 0;
452 }
453
454 static int tahvo_usb_start_hnp(struct otg_transceiver *otg)
455 {
456         struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
457
458         dev_dbg(&tu->pt_dev->dev, "start_hnp\n");
459 #ifdef CONFIG_USB_OTG
460         /* REVISIT: Add this for OTG */
461 #endif
462         return -EINVAL;
463 }
464
465 static int tahvo_usb_set_host(struct otg_transceiver *otg, struct usb_bus *host)
466 {
467         struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
468
469         dev_dbg(&tu->pt_dev->dev, "set_host %p\n", host);
470
471         if (otg == NULL)
472                 return -ENODEV;
473
474 #if defined(CONFIG_USB_OTG) || !defined(CONFIG_USB_GADGET_OMAP)
475
476         mutex_lock(&tu->serialize);
477
478         if (host == NULL) {
479                 if (TAHVO_MODE(tu) == TAHVO_MODE_HOST)
480                         tahvo_usb_power_off(tu);
481                 tu->otg.host = NULL;
482                 mutex_unlock(&tu->serialize);
483                 return 0;
484         }
485
486         OTG_SYSCON_1_REG &= ~(OTG_IDLE_EN | HST_IDLE_EN | DEV_IDLE_EN);
487
488         if (TAHVO_MODE(tu) == TAHVO_MODE_HOST) {
489                 tu->otg.host = NULL;
490                 tahvo_usb_become_host(tu);
491         } else
492                 host_suspend(tu);
493
494         tu->otg.host = host;
495
496         mutex_unlock(&tu->serialize);
497 #else
498         /* No host mode configured, so do not allow host controlled to be set */
499         return -EINVAL;
500 #endif
501
502         return 0;
503 }
504
505 static int tahvo_usb_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
506 {
507         struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
508
509         dev_dbg(&tu->pt_dev->dev, "set_peripheral %p\n", gadget);
510
511         if (!otg)
512                 return -ENODEV;
513
514 #if defined(CONFIG_USB_OTG) || defined(CONFIG_USB_GADGET_OMAP)
515
516         mutex_lock(&tu->serialize);
517
518         if (!gadget) {
519                 if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
520                         tahvo_usb_power_off(tu);
521                 tu->otg.gadget = NULL;
522                 mutex_unlock(&tu->serialize);
523                 return 0;
524         }
525
526         tu->otg.gadget = gadget;
527         if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
528                 tahvo_usb_become_peripheral(tu);
529
530         mutex_unlock(&tu->serialize);
531 #else
532         /* No gadget mode configured, so do not allow host controlled to be set */
533         return -EINVAL;
534 #endif
535
536         return 0;
537 }
538
539 static void tahvo_usb_irq_work(struct work_struct *work)
540 {
541         struct tahvo_usb *tu = container_of(work, struct tahvo_usb, irq_work);
542
543         mutex_lock(&tu->serialize);
544         check_vbus_state(tu);
545         mutex_unlock(&tu->serialize);
546 }
547
548 static void tahvo_usb_vbus_interrupt(unsigned long arg)
549 {
550         struct tahvo_usb *tu = (struct tahvo_usb *) arg;
551
552         tahvo_ack_irq(TAHVO_INT_VBUSON);
553         /* Seems we need this to acknowledge the interrupt */
554         tahvo_read_reg(TAHVO_REG_IDSR);
555         schedule_work(&tu->irq_work);
556 }
557
558 #ifdef CONFIG_USB_OTG
559 static ssize_t otg_mode_show(struct device *device,
560                              struct device_attribute *attr, char *buf)
561 {
562         struct tahvo_usb *tu = (struct tahvo_usb*) device->driver_data;
563         switch (tu->tahvo_mode) {
564         case TAHVO_MODE_HOST:
565                 return sprintf(buf, "host\n");
566         case TAHVO_MODE_PERIPHERAL:
567                 return sprintf(buf, "peripheral\n");
568         }
569         return sprintf(buf, "unknown\n");
570 }
571
572 static ssize_t otg_mode_store(struct device *device,
573                               struct device_attribute *attr,
574                               const char *buf, size_t count)
575 {
576         struct tahvo_usb *tu = (struct tahvo_usb*) device->driver_data;
577         int r;
578
579         r = strlen(buf);
580         mutex_lock(&tu->serialize);
581         if (strncmp(buf, "host", 4) == 0) {
582                 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
583                         tahvo_usb_stop_peripheral(tu);
584                 tu->tahvo_mode = TAHVO_MODE_HOST;
585                 if (tu->otg.host) {
586                         printk(KERN_INFO "Selected HOST mode: host controller present.\n");
587                         tahvo_usb_become_host(tu);
588                 } else {
589                         printk(KERN_INFO "Selected HOST mode: no host controller, powering off.\n");
590                         tahvo_usb_power_off(tu);
591                 }
592         } else if (strncmp(buf, "peripheral", 10) == 0) {
593                 if (tu->tahvo_mode == TAHVO_MODE_HOST)
594                         tahvo_usb_stop_host(tu);
595                 tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
596                 if (tu->otg.gadget) {
597                         printk(KERN_INFO "Selected PERIPHERAL mode: gadget driver present.\n");
598                         tahvo_usb_become_peripheral(tu);
599                 } else {
600                         printk(KERN_INFO "Selected PERIPHERAL mode: no gadget driver, powering off.\n");
601                         tahvo_usb_power_off(tu);
602                 }
603         } else
604                 r = -EINVAL;
605
606         mutex_unlock(&tu->serialize);
607         return r;
608 }
609
610 static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store);
611 #endif
612
613 static int tahvo_usb_probe(struct device *dev)
614 {
615         struct tahvo_usb *tu;
616         int ret;
617
618         dev_dbg(dev, "probe\n");
619
620         /* Create driver data */
621         tu = kmalloc(sizeof(*tu), GFP_KERNEL);
622         if (!tu)
623                 return -ENOMEM;
624         memset(tu, 0, sizeof(*tu));
625         tu->pt_dev = container_of(dev, struct platform_device, dev);
626 #ifdef CONFIG_USB_OTG
627         /* Default mode */
628 #ifdef CONFIG_CBUS_TAHVO_USB_HOST_BY_DEFAULT
629         tu->tahvo_mode = TAHVO_MODE_HOST;
630 #else
631         tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
632 #endif
633 #endif
634
635         INIT_WORK(&tu->irq_work, tahvo_usb_irq_work);
636         mutex_init(&tu->serialize);
637
638         /* Set initial state, so that we generate kevents only on
639          * state changes */
640         tu->vbus_state = tahvo_read_reg(TAHVO_REG_IDSR) & 0x01;
641
642         /* We cannot enable interrupt until omap_udc is initialized */
643         ret = tahvo_request_irq(TAHVO_INT_VBUSON, tahvo_usb_vbus_interrupt,
644                                 (unsigned long) tu, "vbus_interrupt");
645         if (ret != 0) {
646                 kfree(tu);
647                 printk(KERN_ERR "Could not register Tahvo interrupt for VBUS\n");
648                 return ret;
649         }
650
651         /* Attributes */
652         ret = device_create_file(dev, &dev_attr_vbus_state);
653 #ifdef CONFIG_USB_OTG
654         ret |= device_create_file(dev, &dev_attr_otg_mode);
655 #endif
656         if (ret)
657                 printk(KERN_ERR "attribute creation failed: %d\n", ret);
658
659         /* Create OTG interface */
660         tahvo_usb_power_off(tu);
661         tu->otg.state = OTG_STATE_UNDEFINED;
662         tu->otg.label = DRIVER_NAME;
663         tu->otg.set_host = tahvo_usb_set_host;
664         tu->otg.set_peripheral = tahvo_usb_set_peripheral;
665         tu->otg.set_power = tahvo_usb_set_power;
666         tu->otg.set_suspend = tahvo_usb_set_suspend;
667         tu->otg.start_srp = tahvo_usb_start_srp;
668         tu->otg.start_hnp = tahvo_usb_start_hnp;
669
670         ret = otg_set_transceiver(&tu->otg);
671         if (ret < 0) {
672                 printk(KERN_ERR "Cannot register USB transceiver\n");
673                 kfree(tu);
674                 tahvo_free_irq(TAHVO_INT_VBUSON);
675                 return ret;
676         }
677
678         dev->driver_data = tu;
679
680         /* Act upon current vbus state once at startup. A vbus state irq may or
681          * may not be generated in addition to this. */
682         schedule_work(&tu->irq_work);
683         return 0;
684 }
685
686 static int tahvo_usb_remove(struct device *dev)
687 {
688         dev_dbg(dev, "remove\n");
689
690         tahvo_free_irq(TAHVO_INT_VBUSON);
691         flush_scheduled_work();
692         otg_set_transceiver(0);
693         device_remove_file(dev, &dev_attr_vbus_state);
694 #ifdef CONFIG_USB_OTG
695         device_remove_file(dev, &dev_attr_otg_mode);
696 #endif
697         return 0;
698 }
699
700 static struct device_driver tahvo_usb_driver = {
701         .name           = "tahvo-usb",
702         .bus            = &platform_bus_type,
703         .probe          = tahvo_usb_probe,
704         .remove         = tahvo_usb_remove,
705 };
706
707 static struct platform_device tahvo_usb_device = {
708         .name           = "tahvo-usb",
709         .id             = -1,
710 };
711
712 static int __init tahvo_usb_init(void)
713 {
714         int ret = 0;
715
716         printk(KERN_INFO "Tahvo USB transceiver driver initializing\n");
717         ret = driver_register(&tahvo_usb_driver);
718         if (ret)
719                 return ret;
720         ret = platform_device_register(&tahvo_usb_device);
721         if (ret < 0) {
722                 driver_unregister(&tahvo_usb_driver);
723                 return ret;
724         }
725         ret = driver_register(&omap_otg_driver);
726         if (ret) {
727                 platform_device_unregister(&tahvo_usb_device);
728                 driver_unregister(&tahvo_usb_driver);
729                 return ret;
730         }
731         return 0;
732 }
733
734 subsys_initcall(tahvo_usb_init);
735
736 static void __exit tahvo_usb_exit(void)
737 {
738         driver_unregister(&omap_otg_driver);
739         platform_device_unregister(&tahvo_usb_device);
740         driver_unregister(&tahvo_usb_driver);
741 }
742 module_exit(tahvo_usb_exit);
743
744 MODULE_DESCRIPTION("Tahvo USB OTG Transceiver Driver");
745 MODULE_LICENSE("GPL");
746 MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");