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