blob: a16b74f32aa9dfd0be4c4112932662d99752f6e8 [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0+
Stephen Rothwell3f23de12007-05-03 02:38:57 +10002/*
3 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
4 * <benh@kernel.crashing.org>
5 * and Arnd Bergmann, IBM Corp.
6 * Merged from powerpc/kernel/of_platform.c and
7 * sparc{,64}/kernel/of_device.c by Stephen Rothwell
Stephen Rothwell3f23de12007-05-03 02:38:57 +10008 */
Rob Herring606ad422016-06-15 08:32:18 -05009
10#define pr_fmt(fmt) "OF: " fmt
11
Stephen Rothwell3f23de12007-05-03 02:38:57 +100012#include <linux/errno.h>
Stephen Rothwell5c457082007-10-17 21:17:42 -070013#include <linux/module.h>
Grant Likely5de15402011-06-21 10:59:34 -060014#include <linux/amba/bus.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100015#include <linux/device.h>
Grant Likely5fd200f2010-06-08 07:48:13 -060016#include <linux/dma-mapping.h>
Grant Likely50ef5282010-06-08 07:48:20 -060017#include <linux/slab.h>
Grant Likely9e3288d2010-06-08 07:48:26 -060018#include <linux/of_address.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100019#include <linux/of_device.h>
Grant Likely9e3288d2010-06-08 07:48:26 -060020#include <linux/of_irq.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100021#include <linux/of_platform.h>
Grant Likelyeca39302010-06-08 07:48:21 -060022#include <linux/platform_device.h>
23
Grant Likelycbb49c22011-06-21 10:59:34 -060024const struct of_device_id of_default_bus_match_table[] = {
25 { .compatible = "simple-bus", },
Linus Walleij22869a92015-03-03 09:52:20 +010026 { .compatible = "simple-mfd", },
Paul Burtonecd76ed2016-09-19 22:21:24 +010027 { .compatible = "isa", },
Grant Likelycbb49c22011-06-21 10:59:34 -060028#ifdef CONFIG_ARM_AMBA
29 { .compatible = "arm,amba-bus", },
30#endif /* CONFIG_ARM_AMBA */
31 {} /* Empty terminated list */
32};
33
Viresh Kumar4550fe62018-04-17 11:57:34 +053034static const struct of_device_id of_skipped_node_table[] = {
35 { .compatible = "operating-points-v2", },
36 {} /* Empty terminated list */
37};
38
Jonas Bonnc6085582010-07-23 19:19:35 +020039/**
40 * of_find_device_by_node - Find the platform_device associated with a node
41 * @np: Pointer to device tree node
42 *
Johan Hovold4fb373d2016-11-01 11:53:22 +010043 * Takes a reference to the embedded struct device which needs to be dropped
44 * after use.
45 *
Rob Herring8c8239c2021-03-25 10:47:12 -060046 * Return: platform_device pointer, or NULL if not found
Jonas Bonnc6085582010-07-23 19:19:35 +020047 */
48struct platform_device *of_find_device_by_node(struct device_node *np)
49{
50 struct device *dev;
51
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +010052 dev = bus_find_device_by_of_node(&platform_bus_type, np);
Jonas Bonnc6085582010-07-23 19:19:35 +020053 return dev ? to_platform_device(dev) : NULL;
54}
55EXPORT_SYMBOL(of_find_device_by_node);
56
Grant Likely964dba22012-02-24 14:58:34 -070057#ifdef CONFIG_OF_ADDRESS
Grant Likely5fd200f2010-06-08 07:48:13 -060058/*
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/**
Grant Likely94c09312010-06-08 07:48:14 -060067 * 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 *
Grant Likely07e461c2014-05-21 15:40:31 +090070 * This routine will first try using the translated bus address to
71 * derive a unique name. If it cannot, then it will prepend names from
72 * parent nodes until a unique name can be derived.
Grant Likely94c09312010-06-08 07:48:14 -060073 */
Frank Rowande553f532016-10-17 12:17:43 -070074static void of_device_make_bus_id(struct device *dev)
Grant Likely94c09312010-06-08 07:48:14 -060075{
Grant Likely94c09312010-06-08 07:48:14 -060076 struct device_node *node = dev->of_node;
Kim Phillips24fb5302012-10-08 19:42:11 -050077 const __be32 *reg;
Grant Likely94c09312010-06-08 07:48:14 -060078 u64 addr;
Rob Herring68d16192021-11-09 10:46:49 -060079 u32 mask;
Grant Likely94c09312010-06-08 07:48:14 -060080
Grant Likely07e461c2014-05-21 15:40:31 +090081 /* Construct the name, using parent nodes if necessary to ensure uniqueness */
82 while (node->parent) {
83 /*
84 * If the address can be translated, then that is as much
85 * uniqueness as we need. Make it the first component and return
86 */
87 reg = of_get_property(node, "reg", NULL);
88 if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
Rob Herring68d16192021-11-09 10:46:49 -060089 if (!of_property_read_u32(node, "mask", &mask))
90 dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn",
91 addr, ffs(mask) - 1, node, dev_name(dev));
92
93 else
94 dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
95 addr, node, dev_name(dev));
Grant Likely94c09312010-06-08 07:48:14 -060096 return;
97 }
Grant Likely94c09312010-06-08 07:48:14 -060098
Grant Likely07e461c2014-05-21 15:40:31 +090099 /* format arguments only used if dev_name() resolves to NULL */
100 dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
Rob Herring95e6b1f2017-06-01 18:00:00 -0500101 kbasename(node->full_name), dev_name(dev));
Grant Likely07e461c2014-05-21 15:40:31 +0900102 node = node->parent;
Grant Likely94c09312010-06-08 07:48:14 -0600103 }
Grant Likely94c09312010-06-08 07:48:14 -0600104}
105
106/**
107 * of_device_alloc - Allocate and initialize an of_device
108 * @np: device node to assign to device
109 * @bus_id: Name to assign to the device. May be null to use default name.
110 * @parent: Parent device.
111 */
Grant Likely94a0cb12010-07-22 13:59:23 -0600112struct platform_device *of_device_alloc(struct device_node *np,
Grant Likely94c09312010-06-08 07:48:14 -0600113 const char *bus_id,
114 struct device *parent)
115{
Grant Likely94a0cb12010-07-22 13:59:23 -0600116 struct platform_device *dev;
Andres Salomon52f65372010-10-10 21:35:05 -0600117 int rc, i, num_reg = 0, num_irq;
Grant Likelyac80a512010-06-08 07:48:15 -0600118 struct resource *res, temp_res;
Grant Likely94c09312010-06-08 07:48:14 -0600119
Andy Shevchenko6d7e3bf82017-08-25 18:33:13 +0300120 dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
Grant Likely7096d042010-10-20 11:45:13 -0600121 if (!dev)
122 return NULL;
123
124 /* count the io and irq resources */
Rob Herringd9c68662014-05-07 15:23:56 -0500125 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
126 num_reg++;
Andres Salomon52f65372010-10-10 21:35:05 -0600127 num_irq = of_irq_count(np);
Grant Likelyac80a512010-06-08 07:48:15 -0600128
Grant Likelyac80a512010-06-08 07:48:15 -0600129 /* Populate the resource table */
130 if (num_irq || num_reg) {
Kees Cook6396bb22018-06-12 14:03:40 -0700131 res = kcalloc(num_irq + num_reg, sizeof(*res), GFP_KERNEL);
Grant Likely7096d042010-10-20 11:45:13 -0600132 if (!res) {
133 platform_device_put(dev);
134 return NULL;
135 }
136
Grant Likelyac80a512010-06-08 07:48:15 -0600137 dev->num_resources = num_reg + num_irq;
138 dev->resource = res;
139 for (i = 0; i < num_reg; i++, res++) {
140 rc = of_address_to_resource(np, i, res);
141 WARN_ON(rc);
142 }
Rob Herring9ec36ca2014-04-23 17:57:41 -0500143 if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
Rob Herringa613b262018-08-27 20:00:19 -0500144 pr_debug("not all legacy IRQ resources mapped for %pOFn\n",
145 np);
Grant Likelyac80a512010-06-08 07:48:15 -0600146 }
Grant Likely94c09312010-06-08 07:48:14 -0600147
148 dev->dev.of_node = of_node_get(np);
Robin Murphyf94277a2016-09-14 16:01:24 +0100149 dev->dev.fwnode = &np->fwnode;
Grant Likely43c07672014-11-04 10:26:26 +0000150 dev->dev.parent = parent ? : &platform_bus;
Grant Likely94c09312010-06-08 07:48:14 -0600151
152 if (bus_id)
153 dev_set_name(&dev->dev, "%s", bus_id);
154 else
155 of_device_make_bus_id(&dev->dev);
156
157 return dev;
158}
159EXPORT_SYMBOL(of_device_alloc);
160
Santosh Shilimkar591c1ee2014-04-24 11:30:04 -0400161/**
Grant Likely15c35972011-06-21 10:59:35 -0600162 * of_platform_device_create_pdata - Alloc, initialize and register an of_device
Grant Likely5fd200f2010-06-08 07:48:13 -0600163 * @np: pointer to node to create device for
164 * @bus_id: name to assign device
Grant Likely15c35972011-06-21 10:59:35 -0600165 * @platform_data: pointer to populate platform_data pointer with
Grant Likely5fd200f2010-06-08 07:48:13 -0600166 * @parent: Linux device model parent device.
Grant Likelycd1e6502011-01-03 15:51:11 -0700167 *
Rob Herring8c8239c2021-03-25 10:47:12 -0600168 * Return: Pointer to created platform device, or NULL if a device was not
Grant Likelycd1e6502011-01-03 15:51:11 -0700169 * registered. Unavailable devices will not get registered.
Grant Likely5fd200f2010-06-08 07:48:13 -0600170 */
Mark Brown245d9642013-07-01 20:26:52 +0100171static struct platform_device *of_platform_device_create_pdata(
Grant Likely15c35972011-06-21 10:59:35 -0600172 struct device_node *np,
173 const char *bus_id,
174 void *platform_data,
175 struct device *parent)
Grant Likely5fd200f2010-06-08 07:48:13 -0600176{
Grant Likely94a0cb12010-07-22 13:59:23 -0600177 struct platform_device *dev;
Grant Likely5fd200f2010-06-08 07:48:13 -0600178
Pawel Mollc6e126d2014-05-15 16:55:24 +0100179 if (!of_device_is_available(np) ||
180 of_node_test_and_set_flag(np, OF_POPULATED))
Grant Likelycd1e6502011-01-03 15:51:11 -0700181 return NULL;
182
Grant Likely5fd200f2010-06-08 07:48:13 -0600183 dev = of_device_alloc(np, bus_id, parent);
184 if (!dev)
Pawel Mollc6e126d2014-05-15 16:55:24 +0100185 goto err_clear_flag;
Grant Likely5fd200f2010-06-08 07:48:13 -0600186
Robin Murphya5516212018-07-27 15:14:15 +0100187 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
188 if (!dev->dev.dma_mask)
189 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
Grant Likelyeca39302010-06-08 07:48:21 -0600190 dev->dev.bus = &platform_bus_type;
Grant Likely15c35972011-06-21 10:59:35 -0600191 dev->dev.platform_data = platform_data;
Marc Zyngierc706c232015-07-28 14:46:15 +0100192 of_msi_configure(&dev->dev, dev->dev.of_node);
Grant Likely5fd200f2010-06-08 07:48:13 -0600193
Grant Likely7096d042010-10-20 11:45:13 -0600194 if (of_device_add(dev) != 0) {
195 platform_device_put(dev);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100196 goto err_clear_flag;
Grant Likely5fd200f2010-06-08 07:48:13 -0600197 }
198
199 return dev;
Pawel Mollc6e126d2014-05-15 16:55:24 +0100200
201err_clear_flag:
202 of_node_clear_flag(np, OF_POPULATED);
203 return NULL;
Grant Likely5fd200f2010-06-08 07:48:13 -0600204}
Grant Likely15c35972011-06-21 10:59:35 -0600205
206/**
207 * of_platform_device_create - Alloc, initialize and register an of_device
208 * @np: pointer to node to create device for
209 * @bus_id: name to assign device
210 * @parent: Linux device model parent device.
211 *
Rob Herring8c8239c2021-03-25 10:47:12 -0600212 * Return: Pointer to created platform device, or NULL if a device was not
Grant Likely15c35972011-06-21 10:59:35 -0600213 * registered. Unavailable devices will not get registered.
214 */
215struct platform_device *of_platform_device_create(struct device_node *np,
216 const char *bus_id,
217 struct device *parent)
218{
219 return of_platform_device_create_pdata(np, bus_id, NULL, parent);
220}
Grant Likely5fd200f2010-06-08 07:48:13 -0600221EXPORT_SYMBOL(of_platform_device_create);
222
Grant Likely5de15402011-06-21 10:59:34 -0600223#ifdef CONFIG_ARM_AMBA
224static struct amba_device *of_amba_device_create(struct device_node *node,
225 const char *bus_id,
226 void *platform_data,
227 struct device *parent)
228{
229 struct amba_device *dev;
230 const void *prop;
Wang Kefeng854f695c2021-08-23 10:41:43 +0100231 int ret;
Grant Likely5de15402011-06-21 10:59:34 -0600232
Rob Herring0d638a02017-06-01 15:50:55 -0500233 pr_debug("Creating amba device %pOF\n", node);
Grant Likely5de15402011-06-21 10:59:34 -0600234
Pawel Mollc6e126d2014-05-15 16:55:24 +0100235 if (!of_device_is_available(node) ||
236 of_node_test_and_set_flag(node, OF_POPULATED))
Grant Likely5de15402011-06-21 10:59:34 -0600237 return NULL;
238
Russell Kingc0f72f82011-12-18 11:45:17 +0000239 dev = amba_device_alloc(NULL, 0, 0);
Rob Herring606ad422016-06-15 08:32:18 -0500240 if (!dev)
Pawel Mollc6e126d2014-05-15 16:55:24 +0100241 goto err_clear_flag;
Grant Likely5de15402011-06-21 10:59:34 -0600242
Linus Walleij8c89ef72018-08-31 16:13:07 +0200243 /* AMBA devices only support a single DMA mask */
244 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
245 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
246
Grant Likely5de15402011-06-21 10:59:34 -0600247 /* setup generic device info */
Grant Likely5de15402011-06-21 10:59:34 -0600248 dev->dev.of_node = of_node_get(node);
Robin Murphyf94277a2016-09-14 16:01:24 +0100249 dev->dev.fwnode = &node->fwnode;
Grant Likely43c07672014-11-04 10:26:26 +0000250 dev->dev.parent = parent ? : &platform_bus;
Grant Likely5de15402011-06-21 10:59:34 -0600251 dev->dev.platform_data = platform_data;
252 if (bus_id)
253 dev_set_name(&dev->dev, "%s", bus_id);
254 else
255 of_device_make_bus_id(&dev->dev);
256
Grant Likely5de15402011-06-21 10:59:34 -0600257 /* Allow the HW Peripheral ID to be overridden */
258 prop = of_get_property(node, "arm,primecell-periphid", NULL);
259 if (prop)
260 dev->periphid = of_read_ulong(prop, 1);
261
Grant Likely5de15402011-06-21 10:59:34 -0600262 ret = of_address_to_resource(node, 0, &dev->res);
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200263 if (ret) {
Rob Herring0d638a02017-06-01 15:50:55 -0500264 pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
265 ret, node);
Grant Likely5de15402011-06-21 10:59:34 -0600266 goto err_free;
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200267 }
Grant Likely5de15402011-06-21 10:59:34 -0600268
Russell Kingc0f72f82011-12-18 11:45:17 +0000269 ret = amba_device_add(dev, &iomem_resource);
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200270 if (ret) {
Rob Herring0d638a02017-06-01 15:50:55 -0500271 pr_err("amba_device_add() failed (%d) for %pOF\n",
272 ret, node);
Grant Likely5de15402011-06-21 10:59:34 -0600273 goto err_free;
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200274 }
Grant Likely5de15402011-06-21 10:59:34 -0600275
276 return dev;
277
278err_free:
Russell Kingc0f72f82011-12-18 11:45:17 +0000279 amba_device_put(dev);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100280err_clear_flag:
281 of_node_clear_flag(node, OF_POPULATED);
Grant Likely5de15402011-06-21 10:59:34 -0600282 return NULL;
283}
284#else /* CONFIG_ARM_AMBA */
285static struct amba_device *of_amba_device_create(struct device_node *node,
286 const char *bus_id,
287 void *platform_data,
288 struct device *parent)
289{
290 return NULL;
291}
292#endif /* CONFIG_ARM_AMBA */
293
Lee Jonesf3896a72021-03-18 10:40:29 +0000294/*
Qi Zhengf9a529b2020-05-24 23:26:14 +0800295 * of_dev_lookup() - Given a device node, lookup the preferred Linux name
Grant Likely15c35972011-06-21 10:59:35 -0600296 */
297static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
298 struct device_node *np)
299{
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700300 const struct of_dev_auxdata *auxdata;
Grant Likely15c35972011-06-21 10:59:35 -0600301 struct resource res;
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700302 int compatible = 0;
Olof Johansson303f59d2011-11-02 22:07:29 -0700303
304 if (!lookup)
305 return NULL;
306
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700307 auxdata = lookup;
308 for (; auxdata->compatible; auxdata++) {
309 if (!of_device_is_compatible(np, auxdata->compatible))
Olof Johansson303f59d2011-11-02 22:07:29 -0700310 continue;
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700311 compatible++;
Lee Jones84774e62012-07-05 15:15:36 +0100312 if (!of_address_to_resource(np, 0, &res))
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700313 if (res.start != auxdata->phys_addr)
Lee Jones84774e62012-07-05 15:15:36 +0100314 continue;
Rob Herring0d638a02017-06-01 15:50:55 -0500315 pr_debug("%pOF: devname=%s\n", np, auxdata->name);
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700316 return auxdata;
317 }
318
319 if (!compatible)
320 return NULL;
321
322 /* Try compatible match if no phys_addr and name are specified */
323 auxdata = lookup;
324 for (; auxdata->compatible; auxdata++) {
325 if (!of_device_is_compatible(np, auxdata->compatible))
326 continue;
327 if (!auxdata->phys_addr && !auxdata->name) {
Rob Herring0d638a02017-06-01 15:50:55 -0500328 pr_debug("%pOF: compatible match\n", np);
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700329 return auxdata;
330 }
Grant Likely15c35972011-06-21 10:59:35 -0600331 }
Olof Johansson303f59d2011-11-02 22:07:29 -0700332
Grant Likely15c35972011-06-21 10:59:35 -0600333 return NULL;
334}
335
336/**
Grant Likely38e9e212011-03-18 10:21:28 -0600337 * of_platform_bus_create() - Create a device for a node and its children.
Grant Likely5fd200f2010-06-08 07:48:13 -0600338 * @bus: device node of the bus to instantiate
Grant Likely1eed4c02011-03-18 10:21:29 -0600339 * @matches: match table for bus nodes
Olof Johansson303f59d2011-11-02 22:07:29 -0700340 * @lookup: auxdata table for matching id and platform_data with device nodes
Grant Likely38e9e212011-03-18 10:21:28 -0600341 * @parent: parent for new device, or NULL for top level.
Olof Johansson303f59d2011-11-02 22:07:29 -0700342 * @strict: require compatible property
Grant Likely38e9e212011-03-18 10:21:28 -0600343 *
344 * Creates a platform_device for the provided device_node, and optionally
345 * recursively create devices for all the child nodes.
Grant Likely5fd200f2010-06-08 07:48:13 -0600346 */
Grant Likely38e9e212011-03-18 10:21:28 -0600347static int of_platform_bus_create(struct device_node *bus,
Grant Likely5fd200f2010-06-08 07:48:13 -0600348 const struct of_device_id *matches,
Grant Likely15c35972011-06-21 10:59:35 -0600349 const struct of_dev_auxdata *lookup,
Grant Likely29d4f8a2011-06-21 10:59:34 -0600350 struct device *parent, bool strict)
Grant Likely5fd200f2010-06-08 07:48:13 -0600351{
Grant Likely15c35972011-06-21 10:59:35 -0600352 const struct of_dev_auxdata *auxdata;
Grant Likely5fd200f2010-06-08 07:48:13 -0600353 struct device_node *child;
Grant Likely94a0cb12010-07-22 13:59:23 -0600354 struct platform_device *dev;
Grant Likely15c35972011-06-21 10:59:35 -0600355 const char *bus_id = NULL;
356 void *platform_data = NULL;
Grant Likely5fd200f2010-06-08 07:48:13 -0600357 int rc = 0;
358
Grant Likely29d4f8a2011-06-21 10:59:34 -0600359 /* Make sure it has a compatible property */
360 if (strict && (!of_get_property(bus, "compatible", NULL))) {
Rob Herring0d638a02017-06-01 15:50:55 -0500361 pr_debug("%s() - skipping %pOF, no compatible prop\n",
362 __func__, bus);
Grant Likely29d4f8a2011-06-21 10:59:34 -0600363 return 0;
364 }
365
Viresh Kumar4550fe62018-04-17 11:57:34 +0530366 /* Skip nodes for which we don't want to create devices */
367 if (unlikely(of_match_node(of_skipped_node_table, bus))) {
368 pr_debug("%s() - skipping %pOF node\n", __func__, bus);
369 return 0;
370 }
371
Kefeng Wang44a71852016-06-01 14:52:54 +0800372 if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500373 pr_debug("%s() - skipping %pOF, already populated\n",
374 __func__, bus);
Kefeng Wang44a71852016-06-01 14:52:54 +0800375 return 0;
376 }
377
Grant Likely15c35972011-06-21 10:59:35 -0600378 auxdata = of_dev_lookup(lookup, bus);
379 if (auxdata) {
380 bus_id = auxdata->name;
381 platform_data = auxdata->platform_data;
382 }
383
Grant Likely5de15402011-06-21 10:59:34 -0600384 if (of_device_is_compatible(bus, "arm,primecell")) {
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200385 /*
386 * Don't return an error here to keep compatibility with older
387 * device tree files.
388 */
Grant Likely15c35972011-06-21 10:59:35 -0600389 of_amba_device_create(bus, bus_id, platform_data, parent);
Grant Likely5de15402011-06-21 10:59:34 -0600390 return 0;
391 }
392
Grant Likely15c35972011-06-21 10:59:35 -0600393 dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
Grant Likely38e9e212011-03-18 10:21:28 -0600394 if (!dev || !of_match_node(matches, bus))
395 return 0;
396
Grant Likely5fd200f2010-06-08 07:48:13 -0600397 for_each_child_of_node(bus, child) {
Rob Herring0d638a02017-06-01 15:50:55 -0500398 pr_debug(" create child: %pOF\n", child);
Grant Likely15c35972011-06-21 10:59:35 -0600399 rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
Grant Likely5fd200f2010-06-08 07:48:13 -0600400 if (rc) {
401 of_node_put(child);
402 break;
403 }
404 }
Grant Likely75f353b2014-06-24 16:13:47 +0100405 of_node_set_flag(bus, OF_POPULATED_BUS);
Grant Likely5fd200f2010-06-08 07:48:13 -0600406 return rc;
407}
408
409/**
Grant Likely38e9e212011-03-18 10:21:28 -0600410 * of_platform_bus_probe() - Probe the device-tree for platform buses
Grant Likely5fd200f2010-06-08 07:48:13 -0600411 * @root: parent of the first level to probe or NULL for the root of the tree
Grant Likely1eed4c02011-03-18 10:21:29 -0600412 * @matches: match table for bus nodes
Grant Likely5fd200f2010-06-08 07:48:13 -0600413 * @parent: parent to hook devices from, NULL for toplevel
414 *
415 * Note that children of the provided root are not instantiated as devices
416 * unless the specified root itself matches the bus list and is not NULL.
417 */
418int of_platform_bus_probe(struct device_node *root,
419 const struct of_device_id *matches,
420 struct device *parent)
421{
422 struct device_node *child;
Grant Likely5fd200f2010-06-08 07:48:13 -0600423 int rc = 0;
424
Grant Likely1eed4c02011-03-18 10:21:29 -0600425 root = root ? of_node_get(root) : of_find_node_by_path("/");
426 if (!root)
Grant Likely60d59912010-06-08 07:48:25 -0600427 return -EINVAL;
Grant Likely5fd200f2010-06-08 07:48:13 -0600428
Kefeng Wang44a71852016-06-01 14:52:54 +0800429 pr_debug("%s()\n", __func__);
Rob Herring0d638a02017-06-01 15:50:55 -0500430 pr_debug(" starting at: %pOF\n", root);
Grant Likely5fd200f2010-06-08 07:48:13 -0600431
Grant Likely1eed4c02011-03-18 10:21:29 -0600432 /* Do a self check of bus type, if there's a match, create children */
Grant Likely5fd200f2010-06-08 07:48:13 -0600433 if (of_match_node(matches, root)) {
Grant Likely15c35972011-06-21 10:59:35 -0600434 rc = of_platform_bus_create(root, matches, NULL, parent, false);
Grant Likely38e9e212011-03-18 10:21:28 -0600435 } else for_each_child_of_node(root, child) {
Grant Likely5fd200f2010-06-08 07:48:13 -0600436 if (!of_match_node(matches, child))
437 continue;
Grant Likely15c35972011-06-21 10:59:35 -0600438 rc = of_platform_bus_create(child, matches, NULL, parent, false);
Julia Lawall7fad948a2015-10-22 11:02:49 +0200439 if (rc) {
440 of_node_put(child);
Grant Likely5fd200f2010-06-08 07:48:13 -0600441 break;
Julia Lawall7fad948a2015-10-22 11:02:49 +0200442 }
Grant Likely5fd200f2010-06-08 07:48:13 -0600443 }
Grant Likely38e9e212011-03-18 10:21:28 -0600444
Grant Likely5fd200f2010-06-08 07:48:13 -0600445 of_node_put(root);
446 return rc;
447}
448EXPORT_SYMBOL(of_platform_bus_probe);
Grant Likely29d4f8a2011-06-21 10:59:34 -0600449
450/**
451 * of_platform_populate() - Populate platform_devices from device tree data
452 * @root: parent of the first level to probe or NULL for the root of the tree
453 * @matches: match table, NULL to use the default
Javi Merino92039ed2012-11-23 16:25:08 +0000454 * @lookup: auxdata table for matching id and platform_data with device nodes
Grant Likely29d4f8a2011-06-21 10:59:34 -0600455 * @parent: parent to hook devices from, NULL for toplevel
456 *
457 * Similar to of_platform_bus_probe(), this function walks the device tree
458 * and creates devices from nodes. It differs in that it follows the modern
459 * convention of requiring all device nodes to have a 'compatible' property,
460 * and it is suitable for creating devices which are children of the root
461 * node (of_platform_bus_probe will only create children of the root which
462 * are selected by the @matches argument).
463 *
464 * New board support should be using this function instead of
465 * of_platform_bus_probe().
466 *
Rob Herring8c8239c2021-03-25 10:47:12 -0600467 * Return: 0 on success, < 0 on failure.
Grant Likely29d4f8a2011-06-21 10:59:34 -0600468 */
469int of_platform_populate(struct device_node *root,
470 const struct of_device_id *matches,
Grant Likely15c35972011-06-21 10:59:35 -0600471 const struct of_dev_auxdata *lookup,
Grant Likely29d4f8a2011-06-21 10:59:34 -0600472 struct device *parent)
473{
474 struct device_node *child;
475 int rc = 0;
476
477 root = root ? of_node_get(root) : of_find_node_by_path("/");
478 if (!root)
479 return -EINVAL;
480
Kefeng Wang44a71852016-06-01 14:52:54 +0800481 pr_debug("%s()\n", __func__);
Rob Herring0d638a02017-06-01 15:50:55 -0500482 pr_debug(" starting at: %pOF\n", root);
Kefeng Wang44a71852016-06-01 14:52:54 +0800483
Saravana Kannan5e666932019-09-04 14:11:24 -0700484 device_links_supplier_sync_state_pause();
Grant Likely29d4f8a2011-06-21 10:59:34 -0600485 for_each_child_of_node(root, child) {
Grant Likely15c35972011-06-21 10:59:35 -0600486 rc = of_platform_bus_create(child, matches, lookup, parent, true);
Julia Lawall7fad948a2015-10-22 11:02:49 +0200487 if (rc) {
488 of_node_put(child);
Grant Likely29d4f8a2011-06-21 10:59:34 -0600489 break;
Julia Lawall7fad948a2015-10-22 11:02:49 +0200490 }
Grant Likely29d4f8a2011-06-21 10:59:34 -0600491 }
Saravana Kannan5e666932019-09-04 14:11:24 -0700492 device_links_supplier_sync_state_resume();
493
Grant Likely2d0747c2014-11-19 22:35:39 +0000494 of_node_set_flag(root, OF_POPULATED_BUS);
Grant Likely29d4f8a2011-06-21 10:59:34 -0600495
496 of_node_put(root);
497 return rc;
498}
Stephen Warrene001f1c2012-06-07 17:57:36 -0600499EXPORT_SYMBOL_GPL(of_platform_populate);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100500
Hauke Mehrtens43443ad2015-08-02 19:44:43 +0200501int of_platform_default_populate(struct device_node *root,
502 const struct of_dev_auxdata *lookup,
503 struct device *parent)
504{
505 return of_platform_populate(root, of_default_bus_match_table, lookup,
506 parent);
507}
508EXPORT_SYMBOL_GPL(of_platform_default_populate);
509
Kevin Haofc520f82016-08-12 13:49:34 +0800510#ifndef CONFIG_PPC
Bjorn Anderssona50ff192017-10-10 22:08:53 -0700511static const struct of_device_id reserved_mem_matches[] = {
Bjorn Anderssond1de6d62017-10-16 13:17:08 -0500512 { .compatible = "qcom,rmtfs-mem" },
Mahesh Sivasubramanian312416d2018-04-10 11:57:23 -0600513 { .compatible = "qcom,cmd-db" },
Bjorn Anderssonb5af64f2021-09-30 11:21:10 -0700514 { .compatible = "qcom,smem" },
Bjorn Anderssona50ff192017-10-10 22:08:53 -0700515 { .compatible = "ramoops" },
Nicolas Saenz Julienne5a3fa752021-01-29 17:14:29 +0000516 { .compatible = "nvmem-rmem" },
David Brazdilf396ede2022-01-26 23:12:37 +0000517 { .compatible = "google,open-dice" },
Bjorn Anderssona50ff192017-10-10 22:08:53 -0700518 {}
519};
520
Kefeng Wang44a71852016-06-01 14:52:54 +0800521static int __init of_platform_default_populate_init(void)
522{
Kees Cook529182e2016-07-29 18:11:32 -0700523 struct device_node *node;
524
Saravana Kannanee9b2802019-12-09 11:31:19 -0800525 device_links_supplier_sync_state_pause();
526
Kees Cook529182e2016-07-29 18:11:32 -0700527 if (!of_have_populated_dt())
528 return -ENODEV;
529
530 /*
Bjorn Anderssona50ff192017-10-10 22:08:53 -0700531 * Handle certain compatibles explicitly, since we don't want to create
532 * platform_devices for every node in /reserved-memory with a
533 * "compatible",
Kees Cook529182e2016-07-29 18:11:32 -0700534 */
Bjorn Anderssona50ff192017-10-10 22:08:53 -0700535 for_each_matching_node(node, reserved_mem_matches)
536 of_platform_device_create(node, NULL, NULL);
Kees Cook529182e2016-07-29 18:11:32 -0700537
Sudeep Holla3aa05822017-09-28 11:45:59 +0100538 node = of_find_node_by_path("/firmware");
Sudeep Hollae2105ca2018-01-18 10:43:39 +0000539 if (node) {
Sudeep Holla3aa05822017-09-28 11:45:59 +0100540 of_platform_populate(node, NULL, NULL, NULL);
Sudeep Hollae2105ca2018-01-18 10:43:39 +0000541 of_node_put(node);
542 }
Sudeep Holla3aa05822017-09-28 11:45:59 +0100543
Hector Martin2f92ea22021-12-12 15:24:05 +0900544 node = of_get_compatible_child(of_chosen, "simple-framebuffer");
545 of_platform_device_create(node, NULL, NULL);
546 of_node_put(node);
547
Kees Cook529182e2016-07-29 18:11:32 -0700548 /* Populate everything else. */
549 of_platform_default_populate(NULL, NULL, NULL);
Kefeng Wang44a71852016-06-01 14:52:54 +0800550
551 return 0;
552}
553arch_initcall_sync(of_platform_default_populate_init);
Saravana Kannan5e666932019-09-04 14:11:24 -0700554
555static int __init of_platform_sync_state_init(void)
556{
Saravana Kannanee9b2802019-12-09 11:31:19 -0800557 device_links_supplier_sync_state_resume();
Saravana Kannan5e666932019-09-04 14:11:24 -0700558 return 0;
559}
560late_initcall_sync(of_platform_sync_state_init);
Kevin Haofc520f82016-08-12 13:49:34 +0800561#endif
Kefeng Wang44a71852016-06-01 14:52:54 +0800562
Jan Glauberc2372c22017-05-22 13:09:20 +0200563int of_platform_device_destroy(struct device *dev, void *data)
Pawel Mollc6e126d2014-05-15 16:55:24 +0100564{
Pawel Mollc6e126d2014-05-15 16:55:24 +0100565 /* Do not touch devices not populated from the device tree */
Grant Likely75f353b2014-06-24 16:13:47 +0100566 if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
Pawel Mollc6e126d2014-05-15 16:55:24 +0100567 return 0;
Pawel Mollc6e126d2014-05-15 16:55:24 +0100568
Grant Likely75f353b2014-06-24 16:13:47 +0100569 /* Recurse for any nodes that were treated as busses */
570 if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
571 device_for_each_child(dev, NULL, of_platform_device_destroy);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100572
Srinivas Kandagatla522811e2018-06-04 15:14:08 +0100573 of_node_clear_flag(dev->of_node, OF_POPULATED);
574 of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
575
Pawel Mollc6e126d2014-05-15 16:55:24 +0100576 if (dev->bus == &platform_bus_type)
577 platform_device_unregister(to_platform_device(dev));
578#ifdef CONFIG_ARM_AMBA
579 else if (dev->bus == &amba_bustype)
580 amba_device_unregister(to_amba_device(dev));
581#endif
Pawel Mollc6e126d2014-05-15 16:55:24 +0100582
Pawel Mollc6e126d2014-05-15 16:55:24 +0100583 return 0;
584}
Jan Glauberc2372c22017-05-22 13:09:20 +0200585EXPORT_SYMBOL_GPL(of_platform_device_destroy);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100586
587/**
588 * of_platform_depopulate() - Remove devices populated from device tree
Grant Likely75f353b2014-06-24 16:13:47 +0100589 * @parent: device which children will be removed
Pawel Mollc6e126d2014-05-15 16:55:24 +0100590 *
591 * Complementary to of_platform_populate(), this function removes children
592 * of the given device (and, recurrently, their children) that have been
593 * created from their respective device tree nodes (and only those,
594 * leaving others - eg. manually created - unharmed).
Pawel Mollc6e126d2014-05-15 16:55:24 +0100595 */
Grant Likely75f353b2014-06-24 16:13:47 +0100596void of_platform_depopulate(struct device *parent)
Pawel Mollc6e126d2014-05-15 16:55:24 +0100597{
Grant Likely2d0747c2014-11-19 22:35:39 +0000598 if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
Thierry Reding70a29202020-08-06 17:36:50 +0200599 device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
Grant Likely2d0747c2014-11-19 22:35:39 +0000600 of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
601 }
Pawel Mollc6e126d2014-05-15 16:55:24 +0100602}
603EXPORT_SYMBOL_GPL(of_platform_depopulate);
604
Benjamin Gaignard38b0b212017-02-24 17:14:33 +0100605static void devm_of_platform_populate_release(struct device *dev, void *res)
606{
607 of_platform_depopulate(*(struct device **)res);
608}
609
610/**
611 * devm_of_platform_populate() - Populate platform_devices from device tree data
612 * @dev: device that requested to populate from device tree data
613 *
614 * Similar to of_platform_populate(), but will automatically call
615 * of_platform_depopulate() when the device is unbound from the bus.
616 *
Rob Herring8c8239c2021-03-25 10:47:12 -0600617 * Return: 0 on success, < 0 on failure.
Benjamin Gaignard38b0b212017-02-24 17:14:33 +0100618 */
619int devm_of_platform_populate(struct device *dev)
620{
621 struct device **ptr;
622 int ret;
623
624 if (!dev)
625 return -EINVAL;
626
627 ptr = devres_alloc(devm_of_platform_populate_release,
628 sizeof(*ptr), GFP_KERNEL);
629 if (!ptr)
630 return -ENOMEM;
631
632 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
633 if (ret) {
634 devres_free(ptr);
635 } else {
636 *ptr = dev;
637 devres_add(dev, ptr);
638 }
639
640 return ret;
641}
642EXPORT_SYMBOL_GPL(devm_of_platform_populate);
643
644static int devm_of_platform_match(struct device *dev, void *res, void *data)
645{
646 struct device **ptr = res;
647
648 if (!ptr) {
649 WARN_ON(!ptr);
650 return 0;
651 }
652
653 return *ptr == data;
654}
655
656/**
657 * devm_of_platform_depopulate() - Remove devices populated from device tree
658 * @dev: device that requested to depopulate from device tree data
659 *
660 * Complementary to devm_of_platform_populate(), this function removes children
661 * of the given device (and, recurrently, their children) that have been
662 * created from their respective device tree nodes (and only those,
663 * leaving others - eg. manually created - unharmed).
664 */
665void devm_of_platform_depopulate(struct device *dev)
666{
667 int ret;
668
669 ret = devres_release(dev, devm_of_platform_populate_release,
670 devm_of_platform_match, dev);
671
672 WARN_ON(ret);
673}
674EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
675
Pantelis Antoniou801d7282014-10-28 22:36:01 +0200676#ifdef CONFIG_OF_DYNAMIC
677static int of_platform_notify(struct notifier_block *nb,
678 unsigned long action, void *arg)
679{
680 struct of_reconfig_data *rd = arg;
681 struct platform_device *pdev_parent, *pdev;
682 bool children_left;
683
684 switch (of_reconfig_get_state_change(action, rd)) {
685 case OF_RECONFIG_CHANGE_ADD:
686 /* verify that the parent is a bus */
687 if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
688 return NOTIFY_OK; /* not for us */
689
Pantelis Antoniou15204ab2014-12-16 19:45:26 +0200690 /* already populated? (driver using of_populate manually) */
691 if (of_node_check_flag(rd->dn, OF_POPULATED))
692 return NOTIFY_OK;
693
Pantelis Antoniou801d7282014-10-28 22:36:01 +0200694 /* pdev_parent may be NULL when no bus platform device */
695 pdev_parent = of_find_device_by_node(rd->dn->parent);
696 pdev = of_platform_device_create(rd->dn, NULL,
697 pdev_parent ? &pdev_parent->dev : NULL);
Rob Herring83c4a4e2021-02-11 17:27:44 -0600698 platform_device_put(pdev_parent);
Pantelis Antoniou801d7282014-10-28 22:36:01 +0200699
700 if (pdev == NULL) {
Rob Herring0d638a02017-06-01 15:50:55 -0500701 pr_err("%s: failed to create for '%pOF'\n",
702 __func__, rd->dn);
Pantelis Antoniou801d7282014-10-28 22:36:01 +0200703 /* of_platform_device_create tosses the error code */
704 return notifier_from_errno(-EINVAL);
705 }
706 break;
707
708 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou15204ab2014-12-16 19:45:26 +0200709
710 /* already depopulated? */
711 if (!of_node_check_flag(rd->dn, OF_POPULATED))
712 return NOTIFY_OK;
713
Pantelis Antoniou801d7282014-10-28 22:36:01 +0200714 /* find our device by node */
715 pdev = of_find_device_by_node(rd->dn);
716 if (pdev == NULL)
717 return NOTIFY_OK; /* no? not meant for us */
718
719 /* unregister takes one ref away */
720 of_platform_device_destroy(&pdev->dev, &children_left);
721
722 /* and put the reference of the find */
Rob Herring83c4a4e2021-02-11 17:27:44 -0600723 platform_device_put(pdev);
Pantelis Antoniou801d7282014-10-28 22:36:01 +0200724 break;
725 }
726
727 return NOTIFY_OK;
728}
729
730static struct notifier_block platform_of_notifier = {
731 .notifier_call = of_platform_notify,
732};
733
734void of_platform_register_reconfig_notifier(void)
735{
736 WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
737}
738#endif /* CONFIG_OF_DYNAMIC */
739
Grant Likely964dba22012-02-24 14:58:34 -0700740#endif /* CONFIG_OF_ADDRESS */