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