irda: prevent integer underflow in IRLMP_ENUMDEVICES
[pandora-kernel.git] / drivers / vlynq / vlynq.c
1 /*
2  * Copyright (C) 2006, 2007 Eugene Konev <ejka@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Parts of the VLYNQ specification can be found here:
19  * http://www.ti.com/litv/pdf/sprue36a
20  */
21
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/device.h>
27 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/platform_device.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/io.h>
33 #include <linux/slab.h>
34 #include <linux/irq.h>
35
36 #include <linux/vlynq.h>
37
38 #define VLYNQ_CTRL_PM_ENABLE            0x80000000
39 #define VLYNQ_CTRL_CLOCK_INT            0x00008000
40 #define VLYNQ_CTRL_CLOCK_DIV(x)         (((x) & 7) << 16)
41 #define VLYNQ_CTRL_INT_LOCAL            0x00004000
42 #define VLYNQ_CTRL_INT_ENABLE           0x00002000
43 #define VLYNQ_CTRL_INT_VECTOR(x)        (((x) & 0x1f) << 8)
44 #define VLYNQ_CTRL_INT2CFG              0x00000080
45 #define VLYNQ_CTRL_RESET                0x00000001
46
47 #define VLYNQ_CTRL_CLOCK_MASK          (0x7 << 16)
48
49 #define VLYNQ_INT_OFFSET                0x00000014
50 #define VLYNQ_REMOTE_OFFSET             0x00000080
51
52 #define VLYNQ_STATUS_LINK               0x00000001
53 #define VLYNQ_STATUS_LERROR             0x00000080
54 #define VLYNQ_STATUS_RERROR             0x00000100
55
56 #define VINT_ENABLE                     0x00000100
57 #define VINT_TYPE_EDGE                  0x00000080
58 #define VINT_LEVEL_LOW                  0x00000040
59 #define VINT_VECTOR(x)                  ((x) & 0x1f)
60 #define VINT_OFFSET(irq)                (8 * ((irq) % 4))
61
62 #define VLYNQ_AUTONEGO_V2               0x00010000
63
64 struct vlynq_regs {
65         u32 revision;
66         u32 control;
67         u32 status;
68         u32 int_prio;
69         u32 int_status;
70         u32 int_pending;
71         u32 int_ptr;
72         u32 tx_offset;
73         struct vlynq_mapping rx_mapping[4];
74         u32 chip;
75         u32 autonego;
76         u32 unused[6];
77         u32 int_device[8];
78 };
79
80 #ifdef CONFIG_VLYNQ_DEBUG
81 static void vlynq_dump_regs(struct vlynq_device *dev)
82 {
83         int i;
84
85         printk(KERN_DEBUG "VLYNQ local=%p remote=%p\n",
86                         dev->local, dev->remote);
87         for (i = 0; i < 32; i++) {
88                 printk(KERN_DEBUG "VLYNQ: local %d: %08x\n",
89                         i + 1, ((u32 *)dev->local)[i]);
90                 printk(KERN_DEBUG "VLYNQ: remote %d: %08x\n",
91                         i + 1, ((u32 *)dev->remote)[i]);
92         }
93 }
94
95 static void vlynq_dump_mem(u32 *base, int count)
96 {
97         int i;
98
99         for (i = 0; i < (count + 3) / 4; i++) {
100                 if (i % 4 == 0)
101                         printk(KERN_DEBUG "\nMEM[0x%04x]:", i * 4);
102                 printk(KERN_DEBUG " 0x%08x", *(base + i));
103         }
104         printk(KERN_DEBUG "\n");
105 }
106 #endif
107
108 /* Check the VLYNQ link status with a given device */
109 static int vlynq_linked(struct vlynq_device *dev)
110 {
111         int i;
112
113         for (i = 0; i < 100; i++)
114                 if (readl(&dev->local->status) & VLYNQ_STATUS_LINK)
115                         return 1;
116                 else
117                         cpu_relax();
118
119         return 0;
120 }
121
122 static void vlynq_reset(struct vlynq_device *dev)
123 {
124         writel(readl(&dev->local->control) | VLYNQ_CTRL_RESET,
125                         &dev->local->control);
126
127         /* Wait for the devices to finish resetting */
128         msleep(5);
129
130         /* Remove reset bit */
131         writel(readl(&dev->local->control) & ~VLYNQ_CTRL_RESET,
132                         &dev->local->control);
133
134         /* Give some time for the devices to settle */
135         msleep(5);
136 }
137
138 static void vlynq_irq_unmask(unsigned int irq)
139 {
140         u32 val;
141         struct vlynq_device *dev = get_irq_chip_data(irq);
142         int virq;
143
144         BUG_ON(!dev);
145         virq = irq - dev->irq_start;
146         val = readl(&dev->remote->int_device[virq >> 2]);
147         val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq);
148         writel(val, &dev->remote->int_device[virq >> 2]);
149 }
150
151 static void vlynq_irq_mask(unsigned int irq)
152 {
153         u32 val;
154         struct vlynq_device *dev = get_irq_chip_data(irq);
155         int virq;
156
157         BUG_ON(!dev);
158         virq = irq - dev->irq_start;
159         val = readl(&dev->remote->int_device[virq >> 2]);
160         val &= ~(VINT_ENABLE << VINT_OFFSET(virq));
161         writel(val, &dev->remote->int_device[virq >> 2]);
162 }
163
164 static int vlynq_irq_type(unsigned int irq, unsigned int flow_type)
165 {
166         u32 val;
167         struct vlynq_device *dev = get_irq_chip_data(irq);
168         int virq;
169
170         BUG_ON(!dev);
171         virq = irq - dev->irq_start;
172         val = readl(&dev->remote->int_device[virq >> 2]);
173         switch (flow_type & IRQ_TYPE_SENSE_MASK) {
174         case IRQ_TYPE_EDGE_RISING:
175         case IRQ_TYPE_EDGE_FALLING:
176         case IRQ_TYPE_EDGE_BOTH:
177                 val |= VINT_TYPE_EDGE << VINT_OFFSET(virq);
178                 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
179                 break;
180         case IRQ_TYPE_LEVEL_HIGH:
181                 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
182                 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
183                 break;
184         case IRQ_TYPE_LEVEL_LOW:
185                 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
186                 val |= VINT_LEVEL_LOW << VINT_OFFSET(virq);
187                 break;
188         default:
189                 return -EINVAL;
190         }
191         writel(val, &dev->remote->int_device[virq >> 2]);
192         return 0;
193 }
194
195 static void vlynq_local_ack(unsigned int irq)
196 {
197         struct vlynq_device *dev = get_irq_chip_data(irq);
198
199         u32 status = readl(&dev->local->status);
200
201         pr_debug("%s: local status: 0x%08x\n",
202                        dev_name(&dev->dev), status);
203         writel(status, &dev->local->status);
204 }
205
206 static void vlynq_remote_ack(unsigned int irq)
207 {
208         struct vlynq_device *dev = get_irq_chip_data(irq);
209
210         u32 status = readl(&dev->remote->status);
211
212         pr_debug("%s: remote status: 0x%08x\n",
213                        dev_name(&dev->dev), status);
214         writel(status, &dev->remote->status);
215 }
216
217 static irqreturn_t vlynq_irq(int irq, void *dev_id)
218 {
219         struct vlynq_device *dev = dev_id;
220         u32 status;
221         int virq = 0;
222
223         status = readl(&dev->local->int_status);
224         writel(status, &dev->local->int_status);
225
226         if (unlikely(!status))
227                 spurious_interrupt();
228
229         while (status) {
230                 if (status & 1)
231                         do_IRQ(dev->irq_start + virq);
232                 status >>= 1;
233                 virq++;
234         }
235
236         return IRQ_HANDLED;
237 }
238
239 static struct irq_chip vlynq_irq_chip = {
240         .name = "vlynq",
241         .unmask = vlynq_irq_unmask,
242         .mask = vlynq_irq_mask,
243         .set_type = vlynq_irq_type,
244 };
245
246 static struct irq_chip vlynq_local_chip = {
247         .name = "vlynq local error",
248         .unmask = vlynq_irq_unmask,
249         .mask = vlynq_irq_mask,
250         .ack = vlynq_local_ack,
251 };
252
253 static struct irq_chip vlynq_remote_chip = {
254         .name = "vlynq local error",
255         .unmask = vlynq_irq_unmask,
256         .mask = vlynq_irq_mask,
257         .ack = vlynq_remote_ack,
258 };
259
260 static int vlynq_setup_irq(struct vlynq_device *dev)
261 {
262         u32 val;
263         int i, virq;
264
265         if (dev->local_irq == dev->remote_irq) {
266                 printk(KERN_ERR
267                        "%s: local vlynq irq should be different from remote\n",
268                        dev_name(&dev->dev));
269                 return -EINVAL;
270         }
271
272         /* Clear local and remote error bits */
273         writel(readl(&dev->local->status), &dev->local->status);
274         writel(readl(&dev->remote->status), &dev->remote->status);
275
276         /* Now setup interrupts */
277         val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
278         val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
279                 VLYNQ_CTRL_INT2CFG;
280         val |= readl(&dev->local->control);
281         writel(VLYNQ_INT_OFFSET, &dev->local->int_ptr);
282         writel(val, &dev->local->control);
283
284         val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq);
285         val |= VLYNQ_CTRL_INT_ENABLE;
286         val |= readl(&dev->remote->control);
287         writel(VLYNQ_INT_OFFSET, &dev->remote->int_ptr);
288         writel(val, &dev->remote->int_ptr);
289         writel(val, &dev->remote->control);
290
291         for (i = dev->irq_start; i <= dev->irq_end; i++) {
292                 virq = i - dev->irq_start;
293                 if (virq == dev->local_irq) {
294                         set_irq_chip_and_handler(i, &vlynq_local_chip,
295                                                  handle_level_irq);
296                         set_irq_chip_data(i, dev);
297                 } else if (virq == dev->remote_irq) {
298                         set_irq_chip_and_handler(i, &vlynq_remote_chip,
299                                                  handle_level_irq);
300                         set_irq_chip_data(i, dev);
301                 } else {
302                         set_irq_chip_and_handler(i, &vlynq_irq_chip,
303                                                  handle_simple_irq);
304                         set_irq_chip_data(i, dev);
305                         writel(0, &dev->remote->int_device[virq >> 2]);
306                 }
307         }
308
309         if (request_irq(dev->irq, vlynq_irq, IRQF_SHARED, "vlynq", dev)) {
310                 printk(KERN_ERR "%s: request_irq failed\n",
311                                         dev_name(&dev->dev));
312                 return -EAGAIN;
313         }
314
315         return 0;
316 }
317
318 static void vlynq_device_release(struct device *dev)
319 {
320         struct vlynq_device *vdev = to_vlynq_device(dev);
321         kfree(vdev);
322 }
323
324 static int vlynq_device_match(struct device *dev,
325                               struct device_driver *drv)
326 {
327         struct vlynq_device *vdev = to_vlynq_device(dev);
328         struct vlynq_driver *vdrv = to_vlynq_driver(drv);
329         struct vlynq_device_id *ids = vdrv->id_table;
330
331         while (ids->id) {
332                 if (ids->id == vdev->dev_id) {
333                         vdev->divisor = ids->divisor;
334                         vlynq_set_drvdata(vdev, ids);
335                         printk(KERN_INFO "Driver found for VLYNQ "
336                                 "device: %08x\n", vdev->dev_id);
337                         return 1;
338                 }
339                 printk(KERN_DEBUG "Not using the %08x VLYNQ device's driver"
340                         " for VLYNQ device: %08x\n", ids->id, vdev->dev_id);
341                 ids++;
342         }
343         return 0;
344 }
345
346 static int vlynq_device_probe(struct device *dev)
347 {
348         struct vlynq_device *vdev = to_vlynq_device(dev);
349         struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
350         struct vlynq_device_id *id = vlynq_get_drvdata(vdev);
351         int result = -ENODEV;
352
353         if (drv->probe)
354                 result = drv->probe(vdev, id);
355         if (result)
356                 put_device(dev);
357         return result;
358 }
359
360 static int vlynq_device_remove(struct device *dev)
361 {
362         struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
363
364         if (drv->remove)
365                 drv->remove(to_vlynq_device(dev));
366
367         return 0;
368 }
369
370 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
371 {
372         driver->driver.name = driver->name;
373         driver->driver.bus = &vlynq_bus_type;
374         return driver_register(&driver->driver);
375 }
376 EXPORT_SYMBOL(__vlynq_register_driver);
377
378 void vlynq_unregister_driver(struct vlynq_driver *driver)
379 {
380         driver_unregister(&driver->driver);
381 }
382 EXPORT_SYMBOL(vlynq_unregister_driver);
383
384 /*
385  * A VLYNQ remote device can clock the VLYNQ bus master
386  * using a dedicated clock line. In that case, both the
387  * remove device and the bus master should have the same
388  * serial clock dividers configured. Iterate through the
389  * 8 possible dividers until we actually link with the
390  * device.
391  */
392 static int __vlynq_try_remote(struct vlynq_device *dev)
393 {
394         int i;
395
396         vlynq_reset(dev);
397         for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ?
398                         i <= vlynq_rdiv8 : i >= vlynq_rdiv2;
399                 dev->dev_id ? i++ : i--) {
400
401                 if (!vlynq_linked(dev))
402                         break;
403
404                 writel((readl(&dev->remote->control) &
405                                 ~VLYNQ_CTRL_CLOCK_MASK) |
406                                 VLYNQ_CTRL_CLOCK_INT |
407                                 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
408                                 &dev->remote->control);
409                 writel((readl(&dev->local->control)
410                                 & ~(VLYNQ_CTRL_CLOCK_INT |
411                                 VLYNQ_CTRL_CLOCK_MASK)) |
412                                 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
413                                 &dev->local->control);
414
415                 if (vlynq_linked(dev)) {
416                         printk(KERN_DEBUG
417                                 "%s: using remote clock divisor %d\n",
418                                 dev_name(&dev->dev), i - vlynq_rdiv1 + 1);
419                         dev->divisor = i;
420                         return 0;
421                 } else {
422                         vlynq_reset(dev);
423                 }
424         }
425
426         return -ENODEV;
427 }
428
429 /*
430  * A VLYNQ remote device can be clocked by the VLYNQ bus
431  * master using a dedicated clock line. In that case, only
432  * the bus master configures the serial clock divider.
433  * Iterate through the 8 possible dividers until we
434  * actually get a link with the device.
435  */
436 static int __vlynq_try_local(struct vlynq_device *dev)
437 {
438         int i;
439
440         vlynq_reset(dev);
441
442         for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ?
443                         i <= vlynq_ldiv8 : i >= vlynq_ldiv2;
444                 dev->dev_id ? i++ : i--) {
445
446                 writel((readl(&dev->local->control) &
447                                 ~VLYNQ_CTRL_CLOCK_MASK) |
448                                 VLYNQ_CTRL_CLOCK_INT |
449                                 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1),
450                                 &dev->local->control);
451
452                 if (vlynq_linked(dev)) {
453                         printk(KERN_DEBUG
454                                 "%s: using local clock divisor %d\n",
455                                 dev_name(&dev->dev), i - vlynq_ldiv1 + 1);
456                         dev->divisor = i;
457                         return 0;
458                 } else {
459                         vlynq_reset(dev);
460                 }
461         }
462
463         return -ENODEV;
464 }
465
466 /*
467  * When using external clocking method, serial clock
468  * is supplied by an external oscillator, therefore we
469  * should mask the local clock bit in the clock control
470  * register for both the bus master and the remote device.
471  */
472 static int __vlynq_try_external(struct vlynq_device *dev)
473 {
474         vlynq_reset(dev);
475         if (!vlynq_linked(dev))
476                 return -ENODEV;
477
478         writel((readl(&dev->remote->control) &
479                         ~VLYNQ_CTRL_CLOCK_INT),
480                         &dev->remote->control);
481
482         writel((readl(&dev->local->control) &
483                         ~VLYNQ_CTRL_CLOCK_INT),
484                         &dev->local->control);
485
486         if (vlynq_linked(dev)) {
487                 printk(KERN_DEBUG "%s: using external clock\n",
488                         dev_name(&dev->dev));
489                         dev->divisor = vlynq_div_external;
490                 return 0;
491         }
492
493         return -ENODEV;
494 }
495
496 static int __vlynq_enable_device(struct vlynq_device *dev)
497 {
498         int result;
499         struct plat_vlynq_ops *ops = dev->dev.platform_data;
500
501         result = ops->on(dev);
502         if (result)
503                 return result;
504
505         switch (dev->divisor) {
506         case vlynq_div_external:
507         case vlynq_div_auto:
508                 /* When the device is brought from reset it should have clock
509                  * generation negotiated by hardware.
510                  * Check which device is generating clocks and perform setup
511                  * accordingly */
512                 if (vlynq_linked(dev) && readl(&dev->remote->control) &
513                    VLYNQ_CTRL_CLOCK_INT) {
514                         if (!__vlynq_try_remote(dev) ||
515                                 !__vlynq_try_local(dev)  ||
516                                 !__vlynq_try_external(dev))
517                                 return 0;
518                 } else {
519                         if (!__vlynq_try_external(dev) ||
520                                 !__vlynq_try_local(dev)    ||
521                                 !__vlynq_try_remote(dev))
522                                 return 0;
523                 }
524                 break;
525         case vlynq_ldiv1:
526         case vlynq_ldiv2:
527         case vlynq_ldiv3:
528         case vlynq_ldiv4:
529         case vlynq_ldiv5:
530         case vlynq_ldiv6:
531         case vlynq_ldiv7:
532         case vlynq_ldiv8:
533                 writel(VLYNQ_CTRL_CLOCK_INT |
534                         VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
535                         vlynq_ldiv1), &dev->local->control);
536                 writel(0, &dev->remote->control);
537                 if (vlynq_linked(dev)) {
538                         printk(KERN_DEBUG
539                                 "%s: using local clock divisor %d\n",
540                                 dev_name(&dev->dev),
541                                 dev->divisor - vlynq_ldiv1 + 1);
542                         return 0;
543                 }
544                 break;
545         case vlynq_rdiv1:
546         case vlynq_rdiv2:
547         case vlynq_rdiv3:
548         case vlynq_rdiv4:
549         case vlynq_rdiv5:
550         case vlynq_rdiv6:
551         case vlynq_rdiv7:
552         case vlynq_rdiv8:
553                 writel(0, &dev->local->control);
554                 writel(VLYNQ_CTRL_CLOCK_INT |
555                         VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
556                         vlynq_rdiv1), &dev->remote->control);
557                 if (vlynq_linked(dev)) {
558                         printk(KERN_DEBUG
559                                 "%s: using remote clock divisor %d\n",
560                                 dev_name(&dev->dev),
561                                 dev->divisor - vlynq_rdiv1 + 1);
562                         return 0;
563                 }
564                 break;
565         }
566
567         ops->off(dev);
568         return -ENODEV;
569 }
570
571 int vlynq_enable_device(struct vlynq_device *dev)
572 {
573         struct plat_vlynq_ops *ops = dev->dev.platform_data;
574         int result = -ENODEV;
575
576         result = __vlynq_enable_device(dev);
577         if (result)
578                 return result;
579
580         result = vlynq_setup_irq(dev);
581         if (result)
582                 ops->off(dev);
583
584         dev->enabled = !result;
585         return result;
586 }
587 EXPORT_SYMBOL(vlynq_enable_device);
588
589
590 void vlynq_disable_device(struct vlynq_device *dev)
591 {
592         struct plat_vlynq_ops *ops = dev->dev.platform_data;
593
594         dev->enabled = 0;
595         free_irq(dev->irq, dev);
596         ops->off(dev);
597 }
598 EXPORT_SYMBOL(vlynq_disable_device);
599
600 int vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
601                             struct vlynq_mapping *mapping)
602 {
603         int i;
604
605         if (!dev->enabled)
606                 return -ENXIO;
607
608         writel(tx_offset, &dev->local->tx_offset);
609         for (i = 0; i < 4; i++) {
610                 writel(mapping[i].offset, &dev->local->rx_mapping[i].offset);
611                 writel(mapping[i].size, &dev->local->rx_mapping[i].size);
612         }
613         return 0;
614 }
615 EXPORT_SYMBOL(vlynq_set_local_mapping);
616
617 int vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
618                              struct vlynq_mapping *mapping)
619 {
620         int i;
621
622         if (!dev->enabled)
623                 return -ENXIO;
624
625         writel(tx_offset, &dev->remote->tx_offset);
626         for (i = 0; i < 4; i++) {
627                 writel(mapping[i].offset, &dev->remote->rx_mapping[i].offset);
628                 writel(mapping[i].size, &dev->remote->rx_mapping[i].size);
629         }
630         return 0;
631 }
632 EXPORT_SYMBOL(vlynq_set_remote_mapping);
633
634 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
635 {
636         int irq = dev->irq_start + virq;
637         if (dev->enabled)
638                 return -EBUSY;
639
640         if ((irq < dev->irq_start) || (irq > dev->irq_end))
641                 return -EINVAL;
642
643         if (virq == dev->remote_irq)
644                 return -EINVAL;
645
646         dev->local_irq = virq;
647
648         return 0;
649 }
650 EXPORT_SYMBOL(vlynq_set_local_irq);
651
652 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
653 {
654         int irq = dev->irq_start + virq;
655         if (dev->enabled)
656                 return -EBUSY;
657
658         if ((irq < dev->irq_start) || (irq > dev->irq_end))
659                 return -EINVAL;
660
661         if (virq == dev->local_irq)
662                 return -EINVAL;
663
664         dev->remote_irq = virq;
665
666         return 0;
667 }
668 EXPORT_SYMBOL(vlynq_set_remote_irq);
669
670 static int vlynq_probe(struct platform_device *pdev)
671 {
672         struct vlynq_device *dev;
673         struct resource *regs_res, *mem_res, *irq_res;
674         int len, result;
675
676         regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
677         if (!regs_res)
678                 return -ENODEV;
679
680         mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
681         if (!mem_res)
682                 return -ENODEV;
683
684         irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
685         if (!irq_res)
686                 return -ENODEV;
687
688         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
689         if (!dev) {
690                 printk(KERN_ERR
691                        "vlynq: failed to allocate device structure\n");
692                 return -ENOMEM;
693         }
694
695         dev->id = pdev->id;
696         dev->dev.bus = &vlynq_bus_type;
697         dev->dev.parent = &pdev->dev;
698         dev_set_name(&dev->dev, "vlynq%d", dev->id);
699         dev->dev.platform_data = pdev->dev.platform_data;
700         dev->dev.release = vlynq_device_release;
701
702         dev->regs_start = regs_res->start;
703         dev->regs_end = regs_res->end;
704         dev->mem_start = mem_res->start;
705         dev->mem_end = mem_res->end;
706
707         len = resource_size(regs_res);
708         if (!request_mem_region(regs_res->start, len, dev_name(&dev->dev))) {
709                 printk(KERN_ERR "%s: Can't request vlynq registers\n",
710                        dev_name(&dev->dev));
711                 result = -ENXIO;
712                 goto fail_request;
713         }
714
715         dev->local = ioremap(regs_res->start, len);
716         if (!dev->local) {
717                 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
718                        dev_name(&dev->dev));
719                 result = -ENXIO;
720                 goto fail_remap;
721         }
722
723         dev->remote = (struct vlynq_regs *)((void *)dev->local +
724                                             VLYNQ_REMOTE_OFFSET);
725
726         dev->irq = platform_get_irq_byname(pdev, "irq");
727         dev->irq_start = irq_res->start;
728         dev->irq_end = irq_res->end;
729         dev->local_irq = dev->irq_end - dev->irq_start;
730         dev->remote_irq = dev->local_irq - 1;
731
732         if (device_register(&dev->dev))
733                 goto fail_register;
734         platform_set_drvdata(pdev, dev);
735
736         printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
737                dev_name(&dev->dev), (void *)dev->regs_start, dev->irq,
738                (void *)dev->mem_start);
739
740         dev->dev_id = 0;
741         dev->divisor = vlynq_div_auto;
742         result = __vlynq_enable_device(dev);
743         if (result == 0) {
744                 dev->dev_id = readl(&dev->remote->chip);
745                 ((struct plat_vlynq_ops *)(dev->dev.platform_data))->off(dev);
746         }
747         if (dev->dev_id)
748                 printk(KERN_INFO "Found a VLYNQ device: %08x\n", dev->dev_id);
749
750         return 0;
751
752 fail_register:
753         iounmap(dev->local);
754 fail_remap:
755 fail_request:
756         release_mem_region(regs_res->start, len);
757         kfree(dev);
758         return result;
759 }
760
761 static int vlynq_remove(struct platform_device *pdev)
762 {
763         struct vlynq_device *dev = platform_get_drvdata(pdev);
764
765         device_unregister(&dev->dev);
766         iounmap(dev->local);
767         release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
768
769         kfree(dev);
770
771         return 0;
772 }
773
774 static struct platform_driver vlynq_platform_driver = {
775         .driver.name = "vlynq",
776         .probe = vlynq_probe,
777         .remove = __devexit_p(vlynq_remove),
778 };
779
780 struct bus_type vlynq_bus_type = {
781         .name = "vlynq",
782         .match = vlynq_device_match,
783         .probe = vlynq_device_probe,
784         .remove = vlynq_device_remove,
785 };
786 EXPORT_SYMBOL(vlynq_bus_type);
787
788 static int __devinit vlynq_init(void)
789 {
790         int res = 0;
791
792         res = bus_register(&vlynq_bus_type);
793         if (res)
794                 goto fail_bus;
795
796         res = platform_driver_register(&vlynq_platform_driver);
797         if (res)
798                 goto fail_platform;
799
800         return 0;
801
802 fail_platform:
803         bus_unregister(&vlynq_bus_type);
804 fail_bus:
805         return res;
806 }
807
808 static void __devexit vlynq_exit(void)
809 {
810         platform_driver_unregister(&vlynq_platform_driver);
811         bus_unregister(&vlynq_bus_type);
812 }
813
814 module_init(vlynq_init);
815 module_exit(vlynq_exit);