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