dt: add of_platform_populate() for creating device from the device tree
[firefly-linux-kernel-4.4.55.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 const struct of_device_id of_default_bus_match_table[] = {
26         { .compatible = "simple-bus", },
27 #ifdef CONFIG_ARM_AMBA
28         { .compatible = "arm,amba-bus", },
29 #endif /* CONFIG_ARM_AMBA */
30         {} /* Empty terminated list */
31 };
32
33 static int of_dev_node_match(struct device *dev, void *data)
34 {
35         return dev->of_node == data;
36 }
37
38 /**
39  * of_find_device_by_node - Find the platform_device associated with a node
40  * @np: Pointer to device tree node
41  *
42  * Returns platform_device pointer, or NULL if not found
43  */
44 struct platform_device *of_find_device_by_node(struct device_node *np)
45 {
46         struct device *dev;
47
48         dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
49         return dev ? to_platform_device(dev) : NULL;
50 }
51 EXPORT_SYMBOL(of_find_device_by_node);
52
53 #if defined(CONFIG_PPC_DCR)
54 #include <asm/dcr.h>
55 #endif
56
57 #if !defined(CONFIG_SPARC)
58 /*
59  * The following routines scan a subtree and registers a device for
60  * each applicable node.
61  *
62  * Note: sparc doesn't use these routines because it has a different
63  * mechanism for creating devices from device tree nodes.
64  */
65
66 /**
67  * of_device_make_bus_id - Use the device node data to assign a unique name
68  * @dev: pointer to device structure that is linked to a device tree node
69  *
70  * This routine will first try using either the dcr-reg or the reg property
71  * value to derive a unique name.  As a last resort it will use the node
72  * name followed by a unique number.
73  */
74 void of_device_make_bus_id(struct device *dev)
75 {
76         static atomic_t bus_no_reg_magic;
77         struct device_node *node = dev->of_node;
78         const u32 *reg;
79         u64 addr;
80         int magic;
81
82 #ifdef CONFIG_PPC_DCR
83         /*
84          * If it's a DCR based device, use 'd' for native DCRs
85          * and 'D' for MMIO DCRs.
86          */
87         reg = of_get_property(node, "dcr-reg", NULL);
88         if (reg) {
89 #ifdef CONFIG_PPC_DCR_NATIVE
90                 dev_set_name(dev, "d%x.%s", *reg, node->name);
91 #else /* CONFIG_PPC_DCR_NATIVE */
92                 u64 addr = of_translate_dcr_address(node, *reg, NULL);
93                 if (addr != OF_BAD_ADDR) {
94                         dev_set_name(dev, "D%llx.%s",
95                                      (unsigned long long)addr, node->name);
96                         return;
97                 }
98 #endif /* !CONFIG_PPC_DCR_NATIVE */
99         }
100 #endif /* CONFIG_PPC_DCR */
101
102         /*
103          * For MMIO, get the physical address
104          */
105         reg = of_get_property(node, "reg", NULL);
106         if (reg) {
107                 addr = of_translate_address(node, reg);
108                 if (addr != OF_BAD_ADDR) {
109                         dev_set_name(dev, "%llx.%s",
110                                      (unsigned long long)addr, node->name);
111                         return;
112                 }
113         }
114
115         /*
116          * No BusID, use the node name and add a globally incremented
117          * counter (and pray...)
118          */
119         magic = atomic_add_return(1, &bus_no_reg_magic);
120         dev_set_name(dev, "%s.%d", node->name, magic - 1);
121 }
122
123 /**
124  * of_device_alloc - Allocate and initialize an of_device
125  * @np: device node to assign to device
126  * @bus_id: Name to assign to the device.  May be null to use default name.
127  * @parent: Parent device.
128  */
129 struct platform_device *of_device_alloc(struct device_node *np,
130                                   const char *bus_id,
131                                   struct device *parent)
132 {
133         struct platform_device *dev;
134         int rc, i, num_reg = 0, num_irq;
135         struct resource *res, temp_res;
136
137         dev = platform_device_alloc("", -1);
138         if (!dev)
139                 return NULL;
140
141         /* count the io and irq resources */
142         while (of_address_to_resource(np, num_reg, &temp_res) == 0)
143                 num_reg++;
144         num_irq = of_irq_count(np);
145
146         /* Populate the resource table */
147         if (num_irq || num_reg) {
148                 res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
149                 if (!res) {
150                         platform_device_put(dev);
151                         return NULL;
152                 }
153
154                 dev->num_resources = num_reg + num_irq;
155                 dev->resource = res;
156                 for (i = 0; i < num_reg; i++, res++) {
157                         rc = of_address_to_resource(np, i, res);
158                         WARN_ON(rc);
159                 }
160                 WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
161         }
162
163         dev->dev.of_node = of_node_get(np);
164 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
165         dev->dev.dma_mask = &dev->archdata.dma_mask;
166 #endif
167         dev->dev.parent = parent;
168
169         if (bus_id)
170                 dev_set_name(&dev->dev, "%s", bus_id);
171         else
172                 of_device_make_bus_id(&dev->dev);
173
174         return dev;
175 }
176 EXPORT_SYMBOL(of_device_alloc);
177
178 /**
179  * of_platform_device_create - Alloc, initialize and register an of_device
180  * @np: pointer to node to create device for
181  * @bus_id: name to assign device
182  * @parent: Linux device model parent device.
183  *
184  * Returns pointer to created platform device, or NULL if a device was not
185  * registered.  Unavailable devices will not get registered.
186  */
187 struct platform_device *of_platform_device_create(struct device_node *np,
188                                             const char *bus_id,
189                                             struct device *parent)
190 {
191         struct platform_device *dev;
192
193         if (!of_device_is_available(np))
194                 return NULL;
195
196         dev = of_device_alloc(np, bus_id, parent);
197         if (!dev)
198                 return NULL;
199
200 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
201         dev->archdata.dma_mask = 0xffffffffUL;
202 #endif
203         dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
204         dev->dev.bus = &platform_bus_type;
205
206         /* We do not fill the DMA ops for platform devices by default.
207          * This is currently the responsibility of the platform code
208          * to do such, possibly using a device notifier
209          */
210
211         if (of_device_add(dev) != 0) {
212                 platform_device_put(dev);
213                 return NULL;
214         }
215
216         return dev;
217 }
218 EXPORT_SYMBOL(of_platform_device_create);
219
220 /**
221  * of_platform_bus_create() - Create a device for a node and its children.
222  * @bus: device node of the bus to instantiate
223  * @matches: match table for bus nodes
224  * disallow recursive creation of child buses
225  * @parent: parent for new device, or NULL for top level.
226  *
227  * Creates a platform_device for the provided device_node, and optionally
228  * recursively create devices for all the child nodes.
229  */
230 static int of_platform_bus_create(struct device_node *bus,
231                                   const struct of_device_id *matches,
232                                   struct device *parent, bool strict)
233 {
234         struct device_node *child;
235         struct platform_device *dev;
236         int rc = 0;
237
238         /* Make sure it has a compatible property */
239         if (strict && (!of_get_property(bus, "compatible", NULL))) {
240                 pr_debug("%s() - skipping %s, no compatible prop\n",
241                          __func__, bus->full_name);
242                 return 0;
243         }
244
245         dev = of_platform_device_create(bus, NULL, parent);
246         if (!dev || !of_match_node(matches, bus))
247                 return 0;
248
249         for_each_child_of_node(bus, child) {
250                 pr_debug("   create child: %s\n", child->full_name);
251                 rc = of_platform_bus_create(child, matches, &dev->dev, strict);
252                 if (rc) {
253                         of_node_put(child);
254                         break;
255                 }
256         }
257         return rc;
258 }
259
260 /**
261  * of_platform_bus_probe() - Probe the device-tree for platform buses
262  * @root: parent of the first level to probe or NULL for the root of the tree
263  * @matches: match table for bus nodes
264  * @parent: parent to hook devices from, NULL for toplevel
265  *
266  * Note that children of the provided root are not instantiated as devices
267  * unless the specified root itself matches the bus list and is not NULL.
268  */
269 int of_platform_bus_probe(struct device_node *root,
270                           const struct of_device_id *matches,
271                           struct device *parent)
272 {
273         struct device_node *child;
274         int rc = 0;
275
276         root = root ? of_node_get(root) : of_find_node_by_path("/");
277         if (!root)
278                 return -EINVAL;
279
280         pr_debug("of_platform_bus_probe()\n");
281         pr_debug(" starting at: %s\n", root->full_name);
282
283         /* Do a self check of bus type, if there's a match, create children */
284         if (of_match_node(matches, root)) {
285                 rc = of_platform_bus_create(root, matches, parent, false);
286         } else for_each_child_of_node(root, child) {
287                 if (!of_match_node(matches, child))
288                         continue;
289                 rc = of_platform_bus_create(child, matches, parent, false);
290                 if (rc)
291                         break;
292         }
293
294         of_node_put(root);
295         return rc;
296 }
297 EXPORT_SYMBOL(of_platform_bus_probe);
298
299 /**
300  * of_platform_populate() - Populate platform_devices from device tree data
301  * @root: parent of the first level to probe or NULL for the root of the tree
302  * @matches: match table, NULL to use the default
303  * @parent: parent to hook devices from, NULL for toplevel
304  *
305  * Similar to of_platform_bus_probe(), this function walks the device tree
306  * and creates devices from nodes.  It differs in that it follows the modern
307  * convention of requiring all device nodes to have a 'compatible' property,
308  * and it is suitable for creating devices which are children of the root
309  * node (of_platform_bus_probe will only create children of the root which
310  * are selected by the @matches argument).
311  *
312  * New board support should be using this function instead of
313  * of_platform_bus_probe().
314  *
315  * Returns 0 on success, < 0 on failure.
316  */
317 int of_platform_populate(struct device_node *root,
318                         const struct of_device_id *matches,
319                         struct device *parent)
320 {
321         struct device_node *child;
322         int rc = 0;
323
324         root = root ? of_node_get(root) : of_find_node_by_path("/");
325         if (!root)
326                 return -EINVAL;
327
328         for_each_child_of_node(root, child) {
329                 rc = of_platform_bus_create(child, matches, parent, true);
330                 if (rc)
331                         break;
332         }
333
334         of_node_put(root);
335         return rc;
336 }
337 #endif /* !CONFIG_SPARC */