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