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