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