Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
0004  *           <benh@kernel.crashing.org>
0005  *    and        Arnd Bergmann, IBM Corp.
0006  *    Merged from powerpc/kernel/of_platform.c and
0007  *    sparc{,64}/kernel/of_device.c by Stephen Rothwell
0008  */
0009 
0010 #define pr_fmt(fmt) "OF: " fmt
0011 
0012 #include <linux/errno.h>
0013 #include <linux/module.h>
0014 #include <linux/amba/bus.h>
0015 #include <linux/device.h>
0016 #include <linux/dma-mapping.h>
0017 #include <linux/slab.h>
0018 #include <linux/of_address.h>
0019 #include <linux/of_device.h>
0020 #include <linux/of_irq.h>
0021 #include <linux/of_platform.h>
0022 #include <linux/platform_device.h>
0023 
0024 const struct of_device_id of_default_bus_match_table[] = {
0025     { .compatible = "simple-bus", },
0026     { .compatible = "simple-mfd", },
0027     { .compatible = "isa", },
0028 #ifdef CONFIG_ARM_AMBA
0029     { .compatible = "arm,amba-bus", },
0030 #endif /* CONFIG_ARM_AMBA */
0031     {} /* Empty terminated list */
0032 };
0033 
0034 static const struct of_device_id of_skipped_node_table[] = {
0035     { .compatible = "operating-points-v2", },
0036     {} /* Empty terminated list */
0037 };
0038 
0039 /**
0040  * of_find_device_by_node - Find the platform_device associated with a node
0041  * @np: Pointer to device tree node
0042  *
0043  * Takes a reference to the embedded struct device which needs to be dropped
0044  * after use.
0045  *
0046  * Return: platform_device pointer, or NULL if not found
0047  */
0048 struct platform_device *of_find_device_by_node(struct device_node *np)
0049 {
0050     struct device *dev;
0051 
0052     dev = bus_find_device_by_of_node(&platform_bus_type, np);
0053     return dev ? to_platform_device(dev) : NULL;
0054 }
0055 EXPORT_SYMBOL(of_find_device_by_node);
0056 
0057 #ifdef CONFIG_OF_ADDRESS
0058 /*
0059  * The following routines scan a subtree and registers a device for
0060  * each applicable node.
0061  *
0062  * Note: sparc doesn't use these routines because it has a different
0063  * mechanism for creating devices from device tree nodes.
0064  */
0065 
0066 /**
0067  * of_device_make_bus_id - Use the device node data to assign a unique name
0068  * @dev: pointer to device structure that is linked to a device tree node
0069  *
0070  * This routine will first try using the translated bus address to
0071  * derive a unique name. If it cannot, then it will prepend names from
0072  * parent nodes until a unique name can be derived.
0073  */
0074 static void of_device_make_bus_id(struct device *dev)
0075 {
0076     struct device_node *node = dev->of_node;
0077     const __be32 *reg;
0078     u64 addr;
0079     u32 mask;
0080 
0081     /* Construct the name, using parent nodes if necessary to ensure uniqueness */
0082     while (node->parent) {
0083         /*
0084          * If the address can be translated, then that is as much
0085          * uniqueness as we need. Make it the first component and return
0086          */
0087         reg = of_get_property(node, "reg", NULL);
0088         if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
0089             if (!of_property_read_u32(node, "mask", &mask))
0090                 dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn",
0091                          addr, ffs(mask) - 1, node, dev_name(dev));
0092 
0093             else
0094                 dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
0095                          addr, node, dev_name(dev));
0096             return;
0097         }
0098 
0099         /* format arguments only used if dev_name() resolves to NULL */
0100         dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
0101                  kbasename(node->full_name), dev_name(dev));
0102         node = node->parent;
0103     }
0104 }
0105 
0106 /**
0107  * of_device_alloc - Allocate and initialize an of_device
0108  * @np: device node to assign to device
0109  * @bus_id: Name to assign to the device.  May be null to use default name.
0110  * @parent: Parent device.
0111  */
0112 struct platform_device *of_device_alloc(struct device_node *np,
0113                   const char *bus_id,
0114                   struct device *parent)
0115 {
0116     struct platform_device *dev;
0117     int rc, i, num_reg = 0;
0118     struct resource *res, temp_res;
0119 
0120     dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
0121     if (!dev)
0122         return NULL;
0123 
0124     /* count the io resources */
0125     while (of_address_to_resource(np, num_reg, &temp_res) == 0)
0126         num_reg++;
0127 
0128     /* Populate the resource table */
0129     if (num_reg) {
0130         res = kcalloc(num_reg, sizeof(*res), GFP_KERNEL);
0131         if (!res) {
0132             platform_device_put(dev);
0133             return NULL;
0134         }
0135 
0136         dev->num_resources = num_reg;
0137         dev->resource = res;
0138         for (i = 0; i < num_reg; i++, res++) {
0139             rc = of_address_to_resource(np, i, res);
0140             WARN_ON(rc);
0141         }
0142     }
0143 
0144     dev->dev.of_node = of_node_get(np);
0145     dev->dev.fwnode = &np->fwnode;
0146     dev->dev.parent = parent ? : &platform_bus;
0147 
0148     if (bus_id)
0149         dev_set_name(&dev->dev, "%s", bus_id);
0150     else
0151         of_device_make_bus_id(&dev->dev);
0152 
0153     return dev;
0154 }
0155 EXPORT_SYMBOL(of_device_alloc);
0156 
0157 /**
0158  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
0159  * @np: pointer to node to create device for
0160  * @bus_id: name to assign device
0161  * @platform_data: pointer to populate platform_data pointer with
0162  * @parent: Linux device model parent device.
0163  *
0164  * Return: Pointer to created platform device, or NULL if a device was not
0165  * registered.  Unavailable devices will not get registered.
0166  */
0167 static struct platform_device *of_platform_device_create_pdata(
0168                     struct device_node *np,
0169                     const char *bus_id,
0170                     void *platform_data,
0171                     struct device *parent)
0172 {
0173     struct platform_device *dev;
0174 
0175     if (!of_device_is_available(np) ||
0176         of_node_test_and_set_flag(np, OF_POPULATED))
0177         return NULL;
0178 
0179     dev = of_device_alloc(np, bus_id, parent);
0180     if (!dev)
0181         goto err_clear_flag;
0182 
0183     dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
0184     if (!dev->dev.dma_mask)
0185         dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
0186     dev->dev.bus = &platform_bus_type;
0187     dev->dev.platform_data = platform_data;
0188     of_msi_configure(&dev->dev, dev->dev.of_node);
0189 
0190     if (of_device_add(dev) != 0) {
0191         platform_device_put(dev);
0192         goto err_clear_flag;
0193     }
0194 
0195     return dev;
0196 
0197 err_clear_flag:
0198     of_node_clear_flag(np, OF_POPULATED);
0199     return NULL;
0200 }
0201 
0202 /**
0203  * of_platform_device_create - Alloc, initialize and register an of_device
0204  * @np: pointer to node to create device for
0205  * @bus_id: name to assign device
0206  * @parent: Linux device model parent device.
0207  *
0208  * Return: Pointer to created platform device, or NULL if a device was not
0209  * registered.  Unavailable devices will not get registered.
0210  */
0211 struct platform_device *of_platform_device_create(struct device_node *np,
0212                         const char *bus_id,
0213                         struct device *parent)
0214 {
0215     return of_platform_device_create_pdata(np, bus_id, NULL, parent);
0216 }
0217 EXPORT_SYMBOL(of_platform_device_create);
0218 
0219 #ifdef CONFIG_ARM_AMBA
0220 static struct amba_device *of_amba_device_create(struct device_node *node,
0221                          const char *bus_id,
0222                          void *platform_data,
0223                          struct device *parent)
0224 {
0225     struct amba_device *dev;
0226     const void *prop;
0227     int ret;
0228 
0229     pr_debug("Creating amba device %pOF\n", node);
0230 
0231     if (!of_device_is_available(node) ||
0232         of_node_test_and_set_flag(node, OF_POPULATED))
0233         return NULL;
0234 
0235     dev = amba_device_alloc(NULL, 0, 0);
0236     if (!dev)
0237         goto err_clear_flag;
0238 
0239     /* AMBA devices only support a single DMA mask */
0240     dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
0241     dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
0242 
0243     /* setup generic device info */
0244     dev->dev.of_node = of_node_get(node);
0245     dev->dev.fwnode = &node->fwnode;
0246     dev->dev.parent = parent ? : &platform_bus;
0247     dev->dev.platform_data = platform_data;
0248     if (bus_id)
0249         dev_set_name(&dev->dev, "%s", bus_id);
0250     else
0251         of_device_make_bus_id(&dev->dev);
0252 
0253     /* Allow the HW Peripheral ID to be overridden */
0254     prop = of_get_property(node, "arm,primecell-periphid", NULL);
0255     if (prop)
0256         dev->periphid = of_read_ulong(prop, 1);
0257 
0258     ret = of_address_to_resource(node, 0, &dev->res);
0259     if (ret) {
0260         pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
0261                ret, node);
0262         goto err_free;
0263     }
0264 
0265     ret = amba_device_add(dev, &iomem_resource);
0266     if (ret) {
0267         pr_err("amba_device_add() failed (%d) for %pOF\n",
0268                ret, node);
0269         goto err_free;
0270     }
0271 
0272     return dev;
0273 
0274 err_free:
0275     amba_device_put(dev);
0276 err_clear_flag:
0277     of_node_clear_flag(node, OF_POPULATED);
0278     return NULL;
0279 }
0280 #else /* CONFIG_ARM_AMBA */
0281 static struct amba_device *of_amba_device_create(struct device_node *node,
0282                          const char *bus_id,
0283                          void *platform_data,
0284                          struct device *parent)
0285 {
0286     return NULL;
0287 }
0288 #endif /* CONFIG_ARM_AMBA */
0289 
0290 /*
0291  * of_dev_lookup() - Given a device node, lookup the preferred Linux name
0292  */
0293 static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
0294                  struct device_node *np)
0295 {
0296     const struct of_dev_auxdata *auxdata;
0297     struct resource res;
0298     int compatible = 0;
0299 
0300     if (!lookup)
0301         return NULL;
0302 
0303     auxdata = lookup;
0304     for (; auxdata->compatible; auxdata++) {
0305         if (!of_device_is_compatible(np, auxdata->compatible))
0306             continue;
0307         compatible++;
0308         if (!of_address_to_resource(np, 0, &res))
0309             if (res.start != auxdata->phys_addr)
0310                 continue;
0311         pr_debug("%pOF: devname=%s\n", np, auxdata->name);
0312         return auxdata;
0313     }
0314 
0315     if (!compatible)
0316         return NULL;
0317 
0318     /* Try compatible match if no phys_addr and name are specified */
0319     auxdata = lookup;
0320     for (; auxdata->compatible; auxdata++) {
0321         if (!of_device_is_compatible(np, auxdata->compatible))
0322             continue;
0323         if (!auxdata->phys_addr && !auxdata->name) {
0324             pr_debug("%pOF: compatible match\n", np);
0325             return auxdata;
0326         }
0327     }
0328 
0329     return NULL;
0330 }
0331 
0332 /**
0333  * of_platform_bus_create() - Create a device for a node and its children.
0334  * @bus: device node of the bus to instantiate
0335  * @matches: match table for bus nodes
0336  * @lookup: auxdata table for matching id and platform_data with device nodes
0337  * @parent: parent for new device, or NULL for top level.
0338  * @strict: require compatible property
0339  *
0340  * Creates a platform_device for the provided device_node, and optionally
0341  * recursively create devices for all the child nodes.
0342  */
0343 static int of_platform_bus_create(struct device_node *bus,
0344                   const struct of_device_id *matches,
0345                   const struct of_dev_auxdata *lookup,
0346                   struct device *parent, bool strict)
0347 {
0348     const struct of_dev_auxdata *auxdata;
0349     struct device_node *child;
0350     struct platform_device *dev;
0351     const char *bus_id = NULL;
0352     void *platform_data = NULL;
0353     int rc = 0;
0354 
0355     /* Make sure it has a compatible property */
0356     if (strict && (!of_get_property(bus, "compatible", NULL))) {
0357         pr_debug("%s() - skipping %pOF, no compatible prop\n",
0358              __func__, bus);
0359         return 0;
0360     }
0361 
0362     /* Skip nodes for which we don't want to create devices */
0363     if (unlikely(of_match_node(of_skipped_node_table, bus))) {
0364         pr_debug("%s() - skipping %pOF node\n", __func__, bus);
0365         return 0;
0366     }
0367 
0368     if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
0369         pr_debug("%s() - skipping %pOF, already populated\n",
0370             __func__, bus);
0371         return 0;
0372     }
0373 
0374     auxdata = of_dev_lookup(lookup, bus);
0375     if (auxdata) {
0376         bus_id = auxdata->name;
0377         platform_data = auxdata->platform_data;
0378     }
0379 
0380     if (of_device_is_compatible(bus, "arm,primecell")) {
0381         /*
0382          * Don't return an error here to keep compatibility with older
0383          * device tree files.
0384          */
0385         of_amba_device_create(bus, bus_id, platform_data, parent);
0386         return 0;
0387     }
0388 
0389     dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
0390     if (!dev || !of_match_node(matches, bus))
0391         return 0;
0392 
0393     for_each_child_of_node(bus, child) {
0394         pr_debug("   create child: %pOF\n", child);
0395         rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
0396         if (rc) {
0397             of_node_put(child);
0398             break;
0399         }
0400     }
0401     of_node_set_flag(bus, OF_POPULATED_BUS);
0402     return rc;
0403 }
0404 
0405 /**
0406  * of_platform_bus_probe() - Probe the device-tree for platform buses
0407  * @root: parent of the first level to probe or NULL for the root of the tree
0408  * @matches: match table for bus nodes
0409  * @parent: parent to hook devices from, NULL for toplevel
0410  *
0411  * Note that children of the provided root are not instantiated as devices
0412  * unless the specified root itself matches the bus list and is not NULL.
0413  */
0414 int of_platform_bus_probe(struct device_node *root,
0415               const struct of_device_id *matches,
0416               struct device *parent)
0417 {
0418     struct device_node *child;
0419     int rc = 0;
0420 
0421     root = root ? of_node_get(root) : of_find_node_by_path("/");
0422     if (!root)
0423         return -EINVAL;
0424 
0425     pr_debug("%s()\n", __func__);
0426     pr_debug(" starting at: %pOF\n", root);
0427 
0428     /* Do a self check of bus type, if there's a match, create children */
0429     if (of_match_node(matches, root)) {
0430         rc = of_platform_bus_create(root, matches, NULL, parent, false);
0431     } else for_each_child_of_node(root, child) {
0432         if (!of_match_node(matches, child))
0433             continue;
0434         rc = of_platform_bus_create(child, matches, NULL, parent, false);
0435         if (rc) {
0436             of_node_put(child);
0437             break;
0438         }
0439     }
0440 
0441     of_node_put(root);
0442     return rc;
0443 }
0444 EXPORT_SYMBOL(of_platform_bus_probe);
0445 
0446 /**
0447  * of_platform_populate() - Populate platform_devices from device tree data
0448  * @root: parent of the first level to probe or NULL for the root of the tree
0449  * @matches: match table, NULL to use the default
0450  * @lookup: auxdata table for matching id and platform_data with device nodes
0451  * @parent: parent to hook devices from, NULL for toplevel
0452  *
0453  * Similar to of_platform_bus_probe(), this function walks the device tree
0454  * and creates devices from nodes.  It differs in that it follows the modern
0455  * convention of requiring all device nodes to have a 'compatible' property,
0456  * and it is suitable for creating devices which are children of the root
0457  * node (of_platform_bus_probe will only create children of the root which
0458  * are selected by the @matches argument).
0459  *
0460  * New board support should be using this function instead of
0461  * of_platform_bus_probe().
0462  *
0463  * Return: 0 on success, < 0 on failure.
0464  */
0465 int of_platform_populate(struct device_node *root,
0466             const struct of_device_id *matches,
0467             const struct of_dev_auxdata *lookup,
0468             struct device *parent)
0469 {
0470     struct device_node *child;
0471     int rc = 0;
0472 
0473     root = root ? of_node_get(root) : of_find_node_by_path("/");
0474     if (!root)
0475         return -EINVAL;
0476 
0477     pr_debug("%s()\n", __func__);
0478     pr_debug(" starting at: %pOF\n", root);
0479 
0480     device_links_supplier_sync_state_pause();
0481     for_each_child_of_node(root, child) {
0482         rc = of_platform_bus_create(child, matches, lookup, parent, true);
0483         if (rc) {
0484             of_node_put(child);
0485             break;
0486         }
0487     }
0488     device_links_supplier_sync_state_resume();
0489 
0490     of_node_set_flag(root, OF_POPULATED_BUS);
0491 
0492     of_node_put(root);
0493     return rc;
0494 }
0495 EXPORT_SYMBOL_GPL(of_platform_populate);
0496 
0497 int of_platform_default_populate(struct device_node *root,
0498                  const struct of_dev_auxdata *lookup,
0499                  struct device *parent)
0500 {
0501     return of_platform_populate(root, of_default_bus_match_table, lookup,
0502                     parent);
0503 }
0504 EXPORT_SYMBOL_GPL(of_platform_default_populate);
0505 
0506 static const struct of_device_id reserved_mem_matches[] = {
0507     { .compatible = "phram" },
0508     { .compatible = "qcom,rmtfs-mem" },
0509     { .compatible = "qcom,cmd-db" },
0510     { .compatible = "qcom,smem" },
0511     { .compatible = "ramoops" },
0512     { .compatible = "nvmem-rmem" },
0513     { .compatible = "google,open-dice" },
0514     {}
0515 };
0516 
0517 static int __init of_platform_default_populate_init(void)
0518 {
0519     struct device_node *node;
0520 
0521     device_links_supplier_sync_state_pause();
0522 
0523     if (!of_have_populated_dt())
0524         return -ENODEV;
0525 
0526     if (IS_ENABLED(CONFIG_PPC)) {
0527         struct device_node *boot_display = NULL;
0528         struct platform_device *dev;
0529         int ret;
0530 
0531         /* Check if we have a MacOS display without a node spec */
0532         if (of_get_property(of_chosen, "linux,bootx-noscreen", NULL)) {
0533             /*
0534              * The old code tried to work out which node was the MacOS
0535              * display based on the address. I'm dropping that since the
0536              * lack of a node spec only happens with old BootX versions
0537              * (users can update) and with this code, they'll still get
0538              * a display (just not the palette hacks).
0539              */
0540             dev = platform_device_alloc("bootx-noscreen", 0);
0541             if (WARN_ON(!dev))
0542                 return -ENOMEM;
0543             ret = platform_device_add(dev);
0544             if (WARN_ON(ret)) {
0545                 platform_device_put(dev);
0546                 return ret;
0547             }
0548         }
0549 
0550         /*
0551          * For OF framebuffers, first create the device for the boot display,
0552          * then for the other framebuffers. Only fail for the boot display;
0553          * ignore errors for the rest.
0554          */
0555         for_each_node_by_type(node, "display") {
0556             if (!of_get_property(node, "linux,opened", NULL) ||
0557                 !of_get_property(node, "linux,boot-display", NULL))
0558                 continue;
0559             dev = of_platform_device_create(node, "of-display", NULL);
0560             if (WARN_ON(!dev))
0561                 return -ENOMEM;
0562             boot_display = node;
0563             break;
0564         }
0565         for_each_node_by_type(node, "display") {
0566             if (!of_get_property(node, "linux,opened", NULL) || node == boot_display)
0567                 continue;
0568             of_platform_device_create(node, "of-display", NULL);
0569         }
0570 
0571     } else {
0572         /*
0573          * Handle certain compatibles explicitly, since we don't want to create
0574          * platform_devices for every node in /reserved-memory with a
0575          * "compatible",
0576          */
0577         for_each_matching_node(node, reserved_mem_matches)
0578             of_platform_device_create(node, NULL, NULL);
0579 
0580         node = of_find_node_by_path("/firmware");
0581         if (node) {
0582             of_platform_populate(node, NULL, NULL, NULL);
0583             of_node_put(node);
0584         }
0585 
0586         node = of_get_compatible_child(of_chosen, "simple-framebuffer");
0587         of_platform_device_create(node, NULL, NULL);
0588         of_node_put(node);
0589 
0590         /* Populate everything else. */
0591         of_platform_default_populate(NULL, NULL, NULL);
0592     }
0593 
0594     return 0;
0595 }
0596 arch_initcall_sync(of_platform_default_populate_init);
0597 
0598 static int __init of_platform_sync_state_init(void)
0599 {
0600     device_links_supplier_sync_state_resume();
0601     return 0;
0602 }
0603 late_initcall_sync(of_platform_sync_state_init);
0604 
0605 int of_platform_device_destroy(struct device *dev, void *data)
0606 {
0607     /* Do not touch devices not populated from the device tree */
0608     if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
0609         return 0;
0610 
0611     /* Recurse for any nodes that were treated as busses */
0612     if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
0613         device_for_each_child(dev, NULL, of_platform_device_destroy);
0614 
0615     of_node_clear_flag(dev->of_node, OF_POPULATED);
0616     of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
0617 
0618     if (dev->bus == &platform_bus_type)
0619         platform_device_unregister(to_platform_device(dev));
0620 #ifdef CONFIG_ARM_AMBA
0621     else if (dev->bus == &amba_bustype)
0622         amba_device_unregister(to_amba_device(dev));
0623 #endif
0624 
0625     return 0;
0626 }
0627 EXPORT_SYMBOL_GPL(of_platform_device_destroy);
0628 
0629 /**
0630  * of_platform_depopulate() - Remove devices populated from device tree
0631  * @parent: device which children will be removed
0632  *
0633  * Complementary to of_platform_populate(), this function removes children
0634  * of the given device (and, recurrently, their children) that have been
0635  * created from their respective device tree nodes (and only those,
0636  * leaving others - eg. manually created - unharmed).
0637  */
0638 void of_platform_depopulate(struct device *parent)
0639 {
0640     if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
0641         device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
0642         of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
0643     }
0644 }
0645 EXPORT_SYMBOL_GPL(of_platform_depopulate);
0646 
0647 static void devm_of_platform_populate_release(struct device *dev, void *res)
0648 {
0649     of_platform_depopulate(*(struct device **)res);
0650 }
0651 
0652 /**
0653  * devm_of_platform_populate() - Populate platform_devices from device tree data
0654  * @dev: device that requested to populate from device tree data
0655  *
0656  * Similar to of_platform_populate(), but will automatically call
0657  * of_platform_depopulate() when the device is unbound from the bus.
0658  *
0659  * Return: 0 on success, < 0 on failure.
0660  */
0661 int devm_of_platform_populate(struct device *dev)
0662 {
0663     struct device **ptr;
0664     int ret;
0665 
0666     if (!dev)
0667         return -EINVAL;
0668 
0669     ptr = devres_alloc(devm_of_platform_populate_release,
0670                sizeof(*ptr), GFP_KERNEL);
0671     if (!ptr)
0672         return -ENOMEM;
0673 
0674     ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
0675     if (ret) {
0676         devres_free(ptr);
0677     } else {
0678         *ptr = dev;
0679         devres_add(dev, ptr);
0680     }
0681 
0682     return ret;
0683 }
0684 EXPORT_SYMBOL_GPL(devm_of_platform_populate);
0685 
0686 static int devm_of_platform_match(struct device *dev, void *res, void *data)
0687 {
0688     struct device **ptr = res;
0689 
0690     if (!ptr) {
0691         WARN_ON(!ptr);
0692         return 0;
0693     }
0694 
0695     return *ptr == data;
0696 }
0697 
0698 /**
0699  * devm_of_platform_depopulate() - Remove devices populated from device tree
0700  * @dev: device that requested to depopulate from device tree data
0701  *
0702  * Complementary to devm_of_platform_populate(), this function removes children
0703  * of the given device (and, recurrently, their children) that have been
0704  * created from their respective device tree nodes (and only those,
0705  * leaving others - eg. manually created - unharmed).
0706  */
0707 void devm_of_platform_depopulate(struct device *dev)
0708 {
0709     int ret;
0710 
0711     ret = devres_release(dev, devm_of_platform_populate_release,
0712                  devm_of_platform_match, dev);
0713 
0714     WARN_ON(ret);
0715 }
0716 EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
0717 
0718 #ifdef CONFIG_OF_DYNAMIC
0719 static int of_platform_notify(struct notifier_block *nb,
0720                 unsigned long action, void *arg)
0721 {
0722     struct of_reconfig_data *rd = arg;
0723     struct platform_device *pdev_parent, *pdev;
0724     bool children_left;
0725 
0726     switch (of_reconfig_get_state_change(action, rd)) {
0727     case OF_RECONFIG_CHANGE_ADD:
0728         /* verify that the parent is a bus */
0729         if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
0730             return NOTIFY_OK;   /* not for us */
0731 
0732         /* already populated? (driver using of_populate manually) */
0733         if (of_node_check_flag(rd->dn, OF_POPULATED))
0734             return NOTIFY_OK;
0735 
0736         /* pdev_parent may be NULL when no bus platform device */
0737         pdev_parent = of_find_device_by_node(rd->dn->parent);
0738         pdev = of_platform_device_create(rd->dn, NULL,
0739                 pdev_parent ? &pdev_parent->dev : NULL);
0740         platform_device_put(pdev_parent);
0741 
0742         if (pdev == NULL) {
0743             pr_err("%s: failed to create for '%pOF'\n",
0744                     __func__, rd->dn);
0745             /* of_platform_device_create tosses the error code */
0746             return notifier_from_errno(-EINVAL);
0747         }
0748         break;
0749 
0750     case OF_RECONFIG_CHANGE_REMOVE:
0751 
0752         /* already depopulated? */
0753         if (!of_node_check_flag(rd->dn, OF_POPULATED))
0754             return NOTIFY_OK;
0755 
0756         /* find our device by node */
0757         pdev = of_find_device_by_node(rd->dn);
0758         if (pdev == NULL)
0759             return NOTIFY_OK;   /* no? not meant for us */
0760 
0761         /* unregister takes one ref away */
0762         of_platform_device_destroy(&pdev->dev, &children_left);
0763 
0764         /* and put the reference of the find */
0765         platform_device_put(pdev);
0766         break;
0767     }
0768 
0769     return NOTIFY_OK;
0770 }
0771 
0772 static struct notifier_block platform_of_notifier = {
0773     .notifier_call = of_platform_notify,
0774 };
0775 
0776 void of_platform_register_reconfig_notifier(void)
0777 {
0778     WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
0779 }
0780 #endif /* CONFIG_OF_DYNAMIC */
0781 
0782 #endif /* CONFIG_OF_ADDRESS */