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