of/device: Replace of_device with platform_device in includes and core code
[linux-2.6.git] / drivers / of / platform.c
1 /*
2  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3  *                       <benh@kernel.crashing.org>
4  *    and                Arnd Bergmann, IBM Corp.
5  *    Merged from powerpc/kernel/of_platform.c and
6  *    sparc{,64}/kernel/of_device.c by Stephen Rothwell
7  *
8  *  This program is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU General Public License
10  *  as published by the Free Software Foundation; either version
11  *  2 of the License, or (at your option) any later version.
12  *
13  */
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/slab.h>
19 #include <linux/of_address.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
24
25 static int platform_driver_probe_shim(struct platform_device *pdev)
26 {
27         struct platform_driver *pdrv;
28         struct of_platform_driver *ofpdrv;
29         const struct of_device_id *match;
30
31         pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
32         ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
33
34         /* There is an unlikely chance that an of_platform driver might match
35          * on a non-OF platform device.  If so, then of_match_device() will
36          * come up empty.  Return -EINVAL in this case so other drivers get
37          * the chance to bind. */
38         match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
39         return match ? ofpdrv->probe(pdev, match) : -EINVAL;
40 }
41
42 static void platform_driver_shutdown_shim(struct platform_device *pdev)
43 {
44         struct platform_driver *pdrv;
45         struct of_platform_driver *ofpdrv;
46
47         pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
48         ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
49         ofpdrv->shutdown(pdev);
50 }
51
52 /**
53  * of_register_platform_driver
54  */
55 int of_register_platform_driver(struct of_platform_driver *drv)
56 {
57         /* setup of_platform_driver to platform_driver adaptors */
58         drv->platform_driver.driver = drv->driver;
59         if (drv->probe)
60                 drv->platform_driver.probe = platform_driver_probe_shim;
61         drv->platform_driver.remove = drv->remove;
62         if (drv->shutdown)
63                 drv->platform_driver.shutdown = platform_driver_shutdown_shim;
64         drv->platform_driver.suspend = drv->suspend;
65         drv->platform_driver.resume = drv->resume;
66
67         return platform_driver_register(&drv->platform_driver);
68 }
69 EXPORT_SYMBOL(of_register_platform_driver);
70
71 void of_unregister_platform_driver(struct of_platform_driver *drv)
72 {
73         platform_driver_unregister(&drv->platform_driver);
74 }
75 EXPORT_SYMBOL(of_unregister_platform_driver);
76
77 #if defined(CONFIG_PPC_DCR)
78 #include <asm/dcr.h>
79 #endif
80
81 extern struct device_attribute of_platform_device_attrs[];
82
83 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
84 {
85         const struct of_device_id *matches = drv->of_match_table;
86
87         if (!matches)
88                 return 0;
89
90         return of_match_device(matches, dev) != NULL;
91 }
92
93 static int of_platform_device_probe(struct device *dev)
94 {
95         int error = -ENODEV;
96         struct of_platform_driver *drv;
97         struct platform_device *of_dev;
98         const struct of_device_id *match;
99
100         drv = to_of_platform_driver(dev->driver);
101         of_dev = to_platform_device(dev);
102
103         if (!drv->probe)
104                 return error;
105
106         of_dev_get(of_dev);
107
108         match = of_match_device(drv->driver.of_match_table, dev);
109         if (match)
110                 error = drv->probe(of_dev, match);
111         if (error)
112                 of_dev_put(of_dev);
113
114         return error;
115 }
116
117 static int of_platform_device_remove(struct device *dev)
118 {
119         struct platform_device *of_dev = to_platform_device(dev);
120         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
121
122         if (dev->driver && drv->remove)
123                 drv->remove(of_dev);
124         return 0;
125 }
126
127 static void of_platform_device_shutdown(struct device *dev)
128 {
129         struct platform_device *of_dev = to_platform_device(dev);
130         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
131
132         if (dev->driver && drv->shutdown)
133                 drv->shutdown(of_dev);
134 }
135
136 #ifdef CONFIG_PM_SLEEP
137
138 static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
139 {
140         struct platform_device *of_dev = to_platform_device(dev);
141         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
142         int ret = 0;
143
144         if (dev->driver && drv->suspend)
145                 ret = drv->suspend(of_dev, mesg);
146         return ret;
147 }
148
149 static int of_platform_legacy_resume(struct device *dev)
150 {
151         struct platform_device *of_dev = to_platform_device(dev);
152         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
153         int ret = 0;
154
155         if (dev->driver && drv->resume)
156                 ret = drv->resume(of_dev);
157         return ret;
158 }
159
160 static int of_platform_pm_prepare(struct device *dev)
161 {
162         struct device_driver *drv = dev->driver;
163         int ret = 0;
164
165         if (drv && drv->pm && drv->pm->prepare)
166                 ret = drv->pm->prepare(dev);
167
168         return ret;
169 }
170
171 static void of_platform_pm_complete(struct device *dev)
172 {
173         struct device_driver *drv = dev->driver;
174
175         if (drv && drv->pm && drv->pm->complete)
176                 drv->pm->complete(dev);
177 }
178
179 #ifdef CONFIG_SUSPEND
180
181 static int of_platform_pm_suspend(struct device *dev)
182 {
183         struct device_driver *drv = dev->driver;
184         int ret = 0;
185
186         if (!drv)
187                 return 0;
188
189         if (drv->pm) {
190                 if (drv->pm->suspend)
191                         ret = drv->pm->suspend(dev);
192         } else {
193                 ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
194         }
195
196         return ret;
197 }
198
199 static int of_platform_pm_suspend_noirq(struct device *dev)
200 {
201         struct device_driver *drv = dev->driver;
202         int ret = 0;
203
204         if (!drv)
205                 return 0;
206
207         if (drv->pm) {
208                 if (drv->pm->suspend_noirq)
209                         ret = drv->pm->suspend_noirq(dev);
210         }
211
212         return ret;
213 }
214
215 static int of_platform_pm_resume(struct device *dev)
216 {
217         struct device_driver *drv = dev->driver;
218         int ret = 0;
219
220         if (!drv)
221                 return 0;
222
223         if (drv->pm) {
224                 if (drv->pm->resume)
225                         ret = drv->pm->resume(dev);
226         } else {
227                 ret = of_platform_legacy_resume(dev);
228         }
229
230         return ret;
231 }
232
233 static int of_platform_pm_resume_noirq(struct device *dev)
234 {
235         struct device_driver *drv = dev->driver;
236         int ret = 0;
237
238         if (!drv)
239                 return 0;
240
241         if (drv->pm) {
242                 if (drv->pm->resume_noirq)
243                         ret = drv->pm->resume_noirq(dev);
244         }
245
246         return ret;
247 }
248
249 #else /* !CONFIG_SUSPEND */
250
251 #define of_platform_pm_suspend          NULL
252 #define of_platform_pm_resume           NULL
253 #define of_platform_pm_suspend_noirq    NULL
254 #define of_platform_pm_resume_noirq     NULL
255
256 #endif /* !CONFIG_SUSPEND */
257
258 #ifdef CONFIG_HIBERNATION
259
260 static int of_platform_pm_freeze(struct device *dev)
261 {
262         struct device_driver *drv = dev->driver;
263         int ret = 0;
264
265         if (!drv)
266                 return 0;
267
268         if (drv->pm) {
269                 if (drv->pm->freeze)
270                         ret = drv->pm->freeze(dev);
271         } else {
272                 ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
273         }
274
275         return ret;
276 }
277
278 static int of_platform_pm_freeze_noirq(struct device *dev)
279 {
280         struct device_driver *drv = dev->driver;
281         int ret = 0;
282
283         if (!drv)
284                 return 0;
285
286         if (drv->pm) {
287                 if (drv->pm->freeze_noirq)
288                         ret = drv->pm->freeze_noirq(dev);
289         }
290
291         return ret;
292 }
293
294 static int of_platform_pm_thaw(struct device *dev)
295 {
296         struct device_driver *drv = dev->driver;
297         int ret = 0;
298
299         if (!drv)
300                 return 0;
301
302         if (drv->pm) {
303                 if (drv->pm->thaw)
304                         ret = drv->pm->thaw(dev);
305         } else {
306                 ret = of_platform_legacy_resume(dev);
307         }
308
309         return ret;
310 }
311
312 static int of_platform_pm_thaw_noirq(struct device *dev)
313 {
314         struct device_driver *drv = dev->driver;
315         int ret = 0;
316
317         if (!drv)
318                 return 0;
319
320         if (drv->pm) {
321                 if (drv->pm->thaw_noirq)
322                         ret = drv->pm->thaw_noirq(dev);
323         }
324
325         return ret;
326 }
327
328 static int of_platform_pm_poweroff(struct device *dev)
329 {
330         struct device_driver *drv = dev->driver;
331         int ret = 0;
332
333         if (!drv)
334                 return 0;
335
336         if (drv->pm) {
337                 if (drv->pm->poweroff)
338                         ret = drv->pm->poweroff(dev);
339         } else {
340                 ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
341         }
342
343         return ret;
344 }
345
346 static int of_platform_pm_poweroff_noirq(struct device *dev)
347 {
348         struct device_driver *drv = dev->driver;
349         int ret = 0;
350
351         if (!drv)
352                 return 0;
353
354         if (drv->pm) {
355                 if (drv->pm->poweroff_noirq)
356                         ret = drv->pm->poweroff_noirq(dev);
357         }
358
359         return ret;
360 }
361
362 static int of_platform_pm_restore(struct device *dev)
363 {
364         struct device_driver *drv = dev->driver;
365         int ret = 0;
366
367         if (!drv)
368                 return 0;
369
370         if (drv->pm) {
371                 if (drv->pm->restore)
372                         ret = drv->pm->restore(dev);
373         } else {
374                 ret = of_platform_legacy_resume(dev);
375         }
376
377         return ret;
378 }
379
380 static int of_platform_pm_restore_noirq(struct device *dev)
381 {
382         struct device_driver *drv = dev->driver;
383         int ret = 0;
384
385         if (!drv)
386                 return 0;
387
388         if (drv->pm) {
389                 if (drv->pm->restore_noirq)
390                         ret = drv->pm->restore_noirq(dev);
391         }
392
393         return ret;
394 }
395
396 #else /* !CONFIG_HIBERNATION */
397
398 #define of_platform_pm_freeze           NULL
399 #define of_platform_pm_thaw             NULL
400 #define of_platform_pm_poweroff         NULL
401 #define of_platform_pm_restore          NULL
402 #define of_platform_pm_freeze_noirq     NULL
403 #define of_platform_pm_thaw_noirq               NULL
404 #define of_platform_pm_poweroff_noirq   NULL
405 #define of_platform_pm_restore_noirq    NULL
406
407 #endif /* !CONFIG_HIBERNATION */
408
409 static struct dev_pm_ops of_platform_dev_pm_ops = {
410         .prepare = of_platform_pm_prepare,
411         .complete = of_platform_pm_complete,
412         .suspend = of_platform_pm_suspend,
413         .resume = of_platform_pm_resume,
414         .freeze = of_platform_pm_freeze,
415         .thaw = of_platform_pm_thaw,
416         .poweroff = of_platform_pm_poweroff,
417         .restore = of_platform_pm_restore,
418         .suspend_noirq = of_platform_pm_suspend_noirq,
419         .resume_noirq = of_platform_pm_resume_noirq,
420         .freeze_noirq = of_platform_pm_freeze_noirq,
421         .thaw_noirq = of_platform_pm_thaw_noirq,
422         .poweroff_noirq = of_platform_pm_poweroff_noirq,
423         .restore_noirq = of_platform_pm_restore_noirq,
424 };
425
426 #define OF_PLATFORM_PM_OPS_PTR  (&of_platform_dev_pm_ops)
427
428 #else /* !CONFIG_PM_SLEEP */
429
430 #define OF_PLATFORM_PM_OPS_PTR  NULL
431
432 #endif /* !CONFIG_PM_SLEEP */
433
434 int of_bus_type_init(struct bus_type *bus, const char *name)
435 {
436         bus->name = name;
437         bus->match = of_platform_bus_match;
438         bus->probe = of_platform_device_probe;
439         bus->remove = of_platform_device_remove;
440         bus->shutdown = of_platform_device_shutdown;
441         bus->dev_attrs = of_platform_device_attrs;
442         bus->pm = OF_PLATFORM_PM_OPS_PTR;
443         return bus_register(bus);
444 }
445
446 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
447 {
448         /*
449          * Temporary: of_platform_bus used to be distinct from the platform
450          * bus.  It isn't anymore, and so drivers on the platform bus need
451          * to be registered in a special way.
452          *
453          * After all of_platform_bus_type drivers are converted to
454          * platform_drivers, this exception can be removed.
455          */
456         if (bus == &platform_bus_type)
457                 return of_register_platform_driver(drv);
458
459         /* register with core */
460         drv->driver.bus = bus;
461         return driver_register(&drv->driver);
462 }
463 EXPORT_SYMBOL(of_register_driver);
464
465 void of_unregister_driver(struct of_platform_driver *drv)
466 {
467         if (drv->driver.bus == &platform_bus_type)
468                 of_unregister_platform_driver(drv);
469         else
470                 driver_unregister(&drv->driver);
471 }
472 EXPORT_SYMBOL(of_unregister_driver);
473
474 #if !defined(CONFIG_SPARC)
475 /*
476  * The following routines scan a subtree and registers a device for
477  * each applicable node.
478  *
479  * Note: sparc doesn't use these routines because it has a different
480  * mechanism for creating devices from device tree nodes.
481  */
482
483 /**
484  * of_device_make_bus_id - Use the device node data to assign a unique name
485  * @dev: pointer to device structure that is linked to a device tree node
486  *
487  * This routine will first try using either the dcr-reg or the reg property
488  * value to derive a unique name.  As a last resort it will use the node
489  * name followed by a unique number.
490  */
491 static void of_device_make_bus_id(struct device *dev)
492 {
493         static atomic_t bus_no_reg_magic;
494         struct device_node *node = dev->of_node;
495         const u32 *reg;
496         u64 addr;
497         int magic;
498
499 #ifdef CONFIG_PPC_DCR
500         /*
501          * If it's a DCR based device, use 'd' for native DCRs
502          * and 'D' for MMIO DCRs.
503          */
504         reg = of_get_property(node, "dcr-reg", NULL);
505         if (reg) {
506 #ifdef CONFIG_PPC_DCR_NATIVE
507                 dev_set_name(dev, "d%x.%s", *reg, node->name);
508 #else /* CONFIG_PPC_DCR_NATIVE */
509                 u64 addr = of_translate_dcr_address(node, *reg, NULL);
510                 if (addr != OF_BAD_ADDR) {
511                         dev_set_name(dev, "D%llx.%s",
512                                      (unsigned long long)addr, node->name);
513                         return;
514                 }
515 #endif /* !CONFIG_PPC_DCR_NATIVE */
516         }
517 #endif /* CONFIG_PPC_DCR */
518
519         /*
520          * For MMIO, get the physical address
521          */
522         reg = of_get_property(node, "reg", NULL);
523         if (reg) {
524                 addr = of_translate_address(node, reg);
525                 if (addr != OF_BAD_ADDR) {
526                         dev_set_name(dev, "%llx.%s",
527                                      (unsigned long long)addr, node->name);
528                         return;
529                 }
530         }
531
532         /*
533          * No BusID, use the node name and add a globally incremented
534          * counter (and pray...)
535          */
536         magic = atomic_add_return(1, &bus_no_reg_magic);
537         dev_set_name(dev, "%s.%d", node->name, magic - 1);
538 }
539
540 /**
541  * of_device_alloc - Allocate and initialize an of_device
542  * @np: device node to assign to device
543  * @bus_id: Name to assign to the device.  May be null to use default name.
544  * @parent: Parent device.
545  */
546 struct platform_device *of_device_alloc(struct device_node *np,
547                                   const char *bus_id,
548                                   struct device *parent)
549 {
550         struct platform_device *dev;
551         int rc, i, num_reg = 0, num_irq = 0;
552         struct resource *res, temp_res;
553
554         /* First count how many resources are needed */
555         while (of_address_to_resource(np, num_reg, &temp_res) == 0)
556                 num_reg++;
557         while (of_irq_to_resource(np, num_irq, &temp_res) != NO_IRQ)
558                 num_irq++;
559
560         /* Allocate memory for both the struct device and the resource table */
561         dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
562                       GFP_KERNEL);
563         if (!dev)
564                 return NULL;
565         res = (struct resource *) &dev[1];
566
567         /* Populate the resource table */
568         if (num_irq || num_reg) {
569                 dev->num_resources = num_reg + num_irq;
570                 dev->resource = res;
571                 for (i = 0; i < num_reg; i++, res++) {
572                         rc = of_address_to_resource(np, i, res);
573                         WARN_ON(rc);
574                 }
575                 for (i = 0; i < num_irq; i++, res++) {
576                         rc = of_irq_to_resource(np, i, res);
577                         WARN_ON(rc == NO_IRQ);
578                 }
579         }
580
581         dev->dev.of_node = of_node_get(np);
582 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
583         dev->dev.dma_mask = &dev->archdata.dma_mask;
584 #endif
585         dev->dev.parent = parent;
586         dev->dev.release = of_release_dev;
587
588         if (bus_id)
589                 dev_set_name(&dev->dev, "%s", bus_id);
590         else
591                 of_device_make_bus_id(&dev->dev);
592
593         return dev;
594 }
595 EXPORT_SYMBOL(of_device_alloc);
596
597 /**
598  * of_platform_device_create - Alloc, initialize and register an of_device
599  * @np: pointer to node to create device for
600  * @bus_id: name to assign device
601  * @parent: Linux device model parent device.
602  */
603 struct platform_device *of_platform_device_create(struct device_node *np,
604                                             const char *bus_id,
605                                             struct device *parent)
606 {
607         struct platform_device *dev;
608
609         dev = of_device_alloc(np, bus_id, parent);
610         if (!dev)
611                 return NULL;
612
613 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
614         dev->archdata.dma_mask = 0xffffffffUL;
615 #endif
616         dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
617         dev->dev.bus = &platform_bus_type;
618
619         /* We do not fill the DMA ops for platform devices by default.
620          * This is currently the responsibility of the platform code
621          * to do such, possibly using a device notifier
622          */
623
624         if (of_device_register(dev) != 0) {
625                 of_device_free(dev);
626                 return NULL;
627         }
628
629         return dev;
630 }
631 EXPORT_SYMBOL(of_platform_device_create);
632
633 /**
634  * of_platform_bus_create - Create an OF device for a bus node and all its
635  * children. Optionally recursively instantiate matching busses.
636  * @bus: device node of the bus to instantiate
637  * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
638  * disallow recursive creation of child busses
639  */
640 static int of_platform_bus_create(const struct device_node *bus,
641                                   const struct of_device_id *matches,
642                                   struct device *parent)
643 {
644         struct device_node *child;
645         struct platform_device *dev;
646         int rc = 0;
647
648         for_each_child_of_node(bus, child) {
649                 pr_debug("   create child: %s\n", child->full_name);
650                 dev = of_platform_device_create(child, NULL, parent);
651                 if (dev == NULL)
652                         rc = -ENOMEM;
653                 else if (!of_match_node(matches, child))
654                         continue;
655                 if (rc == 0) {
656                         pr_debug("   and sub busses\n");
657                         rc = of_platform_bus_create(child, matches, &dev->dev);
658                 }
659                 if (rc) {
660                         of_node_put(child);
661                         break;
662                 }
663         }
664         return rc;
665 }
666
667 /**
668  * of_platform_bus_probe - Probe the device-tree for platform busses
669  * @root: parent of the first level to probe or NULL for the root of the tree
670  * @matches: match table, NULL to use the default
671  * @parent: parent to hook devices from, NULL for toplevel
672  *
673  * Note that children of the provided root are not instantiated as devices
674  * unless the specified root itself matches the bus list and is not NULL.
675  */
676 int of_platform_bus_probe(struct device_node *root,
677                           const struct of_device_id *matches,
678                           struct device *parent)
679 {
680         struct device_node *child;
681         struct platform_device *dev;
682         int rc = 0;
683
684         if (matches == NULL)
685                 matches = of_default_bus_ids;
686         if (matches == OF_NO_DEEP_PROBE)
687                 return -EINVAL;
688         if (root == NULL)
689                 root = of_find_node_by_path("/");
690         else
691                 of_node_get(root);
692         if (root == NULL)
693                 return -EINVAL;
694
695         pr_debug("of_platform_bus_probe()\n");
696         pr_debug(" starting at: %s\n", root->full_name);
697
698         /* Do a self check of bus type, if there's a match, create
699          * children
700          */
701         if (of_match_node(matches, root)) {
702                 pr_debug(" root match, create all sub devices\n");
703                 dev = of_platform_device_create(root, NULL, parent);
704                 if (dev == NULL) {
705                         rc = -ENOMEM;
706                         goto bail;
707                 }
708                 pr_debug(" create all sub busses\n");
709                 rc = of_platform_bus_create(root, matches, &dev->dev);
710                 goto bail;
711         }
712         for_each_child_of_node(root, child) {
713                 if (!of_match_node(matches, child))
714                         continue;
715
716                 pr_debug("  match: %s\n", child->full_name);
717                 dev = of_platform_device_create(child, NULL, parent);
718                 if (dev == NULL)
719                         rc = -ENOMEM;
720                 else
721                         rc = of_platform_bus_create(child, matches, &dev->dev);
722                 if (rc) {
723                         of_node_put(child);
724                         break;
725                 }
726         }
727  bail:
728         of_node_put(root);
729         return rc;
730 }
731 EXPORT_SYMBOL(of_platform_bus_probe);
732 #endif /* !CONFIG_SPARC */