^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * <benh@kernel.crashing.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * and Arnd Bergmann, IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Merged from powerpc/kernel/of_platform.c and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * sparc{,64}/kernel/of_device.c by Stephen Rothwell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) "OF: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/amba/bus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/of_iommu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/of_irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) const struct of_device_id of_default_bus_match_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) { .compatible = "simple-bus", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) { .compatible = "simple-mfd", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) { .compatible = "isa", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #ifdef CONFIG_ARM_AMBA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) { .compatible = "arm,amba-bus", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #endif /* CONFIG_ARM_AMBA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {} /* Empty terminated list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static const struct of_device_id of_skipped_node_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) { .compatible = "operating-points-v2", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {} /* Empty terminated list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * of_find_device_by_node - Find the platform_device associated with a node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @np: Pointer to device tree node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Takes a reference to the embedded struct device which needs to be dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * after use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Returns platform_device pointer, or NULL if not found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct platform_device *of_find_device_by_node(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) dev = bus_find_device_by_of_node(&platform_bus_type, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return dev ? to_platform_device(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) EXPORT_SYMBOL(of_find_device_by_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #ifdef CONFIG_OF_ADDRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * The following routines scan a subtree and registers a device for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * each applicable node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * Note: sparc doesn't use these routines because it has a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * mechanism for creating devices from device tree nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * of_device_make_bus_id - Use the device node data to assign a unique name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * @dev: pointer to device structure that is linked to a device tree node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * This routine will first try using the translated bus address to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * derive a unique name. If it cannot, then it will prepend names from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * parent nodes until a unique name can be derived.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static void of_device_make_bus_id(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct device_node *node = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) const __be32 *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) u64 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* Construct the name, using parent nodes if necessary to ensure uniqueness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) while (node->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * If the address can be translated, then that is as much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * uniqueness as we need. Make it the first component and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) reg = of_get_property(node, "reg", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) addr, node, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* format arguments only used if dev_name() resolves to NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) kbasename(node->full_name), dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) node = node->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * of_device_alloc - Allocate and initialize an of_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * @np: device node to assign to device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * @bus_id: Name to assign to the device. May be null to use default name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * @parent: Parent device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct platform_device *of_device_alloc(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) const char *bus_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct platform_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int rc, i, num_reg = 0, num_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct resource *res, temp_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* count the io and irq resources */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) while (of_address_to_resource(np, num_reg, &temp_res) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) num_reg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) num_irq = of_irq_count(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* Populate the resource table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (num_irq || num_reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) res = kcalloc(num_irq + num_reg, sizeof(*res), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) platform_device_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) dev->num_resources = num_reg + num_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) dev->resource = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) for (i = 0; i < num_reg; i++, res++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) rc = of_address_to_resource(np, i, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) WARN_ON(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) pr_debug("not all legacy IRQ resources mapped for %pOFn\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) dev->dev.of_node = of_node_get(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) dev->dev.fwnode = &np->fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) dev->dev.parent = parent ? : &platform_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (bus_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) dev_set_name(&dev->dev, "%s", bus_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) of_device_make_bus_id(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) EXPORT_SYMBOL(of_device_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * of_platform_device_create_pdata - Alloc, initialize and register an of_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @np: pointer to node to create device for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * @bus_id: name to assign device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * @platform_data: pointer to populate platform_data pointer with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @parent: Linux device model parent device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Returns pointer to created platform device, or NULL if a device was not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * registered. Unavailable devices will not get registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static struct platform_device *of_platform_device_create_pdata(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) const char *bus_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) void *platform_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct platform_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!of_device_is_available(np) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) of_node_test_and_set_flag(np, OF_POPULATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) dev = of_device_alloc(np, bus_id, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) goto err_clear_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!dev->dev.dma_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) dev->dev.bus = &platform_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) dev->dev.platform_data = platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) of_msi_configure(&dev->dev, dev->dev.of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (of_device_add(dev) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) platform_device_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) goto err_clear_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) err_clear_flag:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) of_node_clear_flag(np, OF_POPULATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * of_platform_device_create - Alloc, initialize and register an of_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * @np: pointer to node to create device for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * @bus_id: name to assign device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * @parent: Linux device model parent device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * Returns pointer to created platform device, or NULL if a device was not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * registered. Unavailable devices will not get registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct platform_device *of_platform_device_create(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) const char *bus_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return of_platform_device_create_pdata(np, bus_id, NULL, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) EXPORT_SYMBOL(of_platform_device_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) #ifdef CONFIG_ARM_AMBA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static struct amba_device *of_amba_device_create(struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) const char *bus_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) void *platform_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct amba_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) const void *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) pr_debug("Creating amba device %pOF\n", node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!of_device_is_available(node) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) of_node_test_and_set_flag(node, OF_POPULATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) dev = amba_device_alloc(NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) goto err_clear_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* AMBA devices only support a single DMA mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* setup generic device info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dev->dev.of_node = of_node_get(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) dev->dev.fwnode = &node->fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) dev->dev.parent = parent ? : &platform_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dev->dev.platform_data = platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (bus_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) dev_set_name(&dev->dev, "%s", bus_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) of_device_make_bus_id(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* Allow the HW Peripheral ID to be overridden */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) prop = of_get_property(node, "arm,primecell-periphid", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) dev->periphid = of_read_ulong(prop, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* Decode the IRQs and address ranges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) for (i = 0; i < AMBA_NR_IRQS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) dev->irq[i] = irq_of_parse_and_map(node, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ret = of_address_to_resource(node, 0, &dev->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) ret, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ret = amba_device_add(dev, &iomem_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) pr_err("amba_device_add() failed (%d) for %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ret, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) amba_device_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) err_clear_flag:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) of_node_clear_flag(node, OF_POPULATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) #else /* CONFIG_ARM_AMBA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static struct amba_device *of_amba_device_create(struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) const char *bus_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) void *platform_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) #endif /* CONFIG_ARM_AMBA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * of_dev_lookup() - Given a device node, lookup the preferred Linux name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) const struct of_dev_auxdata *auxdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct resource res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int compatible = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) auxdata = lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) for (; auxdata->compatible; auxdata++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!of_device_is_compatible(np, auxdata->compatible))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) compatible++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (!of_address_to_resource(np, 0, &res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (res.start != auxdata->phys_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) pr_debug("%pOF: devname=%s\n", np, auxdata->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return auxdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (!compatible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /* Try compatible match if no phys_addr and name are specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) auxdata = lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) for (; auxdata->compatible; auxdata++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (!of_device_is_compatible(np, auxdata->compatible))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (!auxdata->phys_addr && !auxdata->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) pr_debug("%pOF: compatible match\n", np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return auxdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * of_platform_bus_create() - Create a device for a node and its children.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * @bus: device node of the bus to instantiate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * @matches: match table for bus nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * @lookup: auxdata table for matching id and platform_data with device nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * @parent: parent for new device, or NULL for top level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * @strict: require compatible property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * Creates a platform_device for the provided device_node, and optionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * recursively create devices for all the child nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static int of_platform_bus_create(struct device_node *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) const struct of_device_id *matches,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) const struct of_dev_auxdata *lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct device *parent, bool strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) const struct of_dev_auxdata *auxdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct platform_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) const char *bus_id = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) void *platform_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* Make sure it has a compatible property */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (strict && (!of_get_property(bus, "compatible", NULL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) pr_debug("%s() - skipping %pOF, no compatible prop\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) __func__, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* Skip nodes for which we don't want to create devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (unlikely(of_match_node(of_skipped_node_table, bus))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) pr_debug("%s() - skipping %pOF node\n", __func__, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) pr_debug("%s() - skipping %pOF, already populated\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) __func__, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) auxdata = of_dev_lookup(lookup, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (auxdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) bus_id = auxdata->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) platform_data = auxdata->platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (of_device_is_compatible(bus, "arm,primecell")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * Don't return an error here to keep compatibility with older
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * device tree files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) of_amba_device_create(bus, bus_id, platform_data, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (!dev || !of_match_node(matches, bus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) for_each_child_of_node(bus, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) pr_debug(" create child: %pOF\n", child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) of_node_set_flag(bus, OF_POPULATED_BUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * of_platform_bus_probe() - Probe the device-tree for platform buses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * @root: parent of the first level to probe or NULL for the root of the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * @matches: match table for bus nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * @parent: parent to hook devices from, NULL for toplevel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * Note that children of the provided root are not instantiated as devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * unless the specified root itself matches the bus list and is not NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) int of_platform_bus_probe(struct device_node *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) const struct of_device_id *matches,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) root = root ? of_node_get(root) : of_find_node_by_path("/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (!root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) pr_debug("%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) pr_debug(" starting at: %pOF\n", root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /* Do a self check of bus type, if there's a match, create children */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (of_match_node(matches, root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) rc = of_platform_bus_create(root, matches, NULL, parent, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) } else for_each_child_of_node(root, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (!of_match_node(matches, child))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) rc = of_platform_bus_create(child, matches, NULL, parent, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) of_node_put(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) EXPORT_SYMBOL(of_platform_bus_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * of_platform_populate() - Populate platform_devices from device tree data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * @root: parent of the first level to probe or NULL for the root of the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * @matches: match table, NULL to use the default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * @lookup: auxdata table for matching id and platform_data with device nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * @parent: parent to hook devices from, NULL for toplevel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * Similar to of_platform_bus_probe(), this function walks the device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * and creates devices from nodes. It differs in that it follows the modern
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * convention of requiring all device nodes to have a 'compatible' property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * and it is suitable for creating devices which are children of the root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * node (of_platform_bus_probe will only create children of the root which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * are selected by the @matches argument).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * New board support should be using this function instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * of_platform_bus_probe().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * Returns 0 on success, < 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int of_platform_populate(struct device_node *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) const struct of_device_id *matches,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) const struct of_dev_auxdata *lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) root = root ? of_node_get(root) : of_find_node_by_path("/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (!root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) pr_debug("%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) pr_debug(" starting at: %pOF\n", root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) device_links_supplier_sync_state_pause();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) for_each_child_of_node(root, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) rc = of_platform_bus_create(child, matches, lookup, parent, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) device_links_supplier_sync_state_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) of_node_set_flag(root, OF_POPULATED_BUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) of_node_put(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) EXPORT_SYMBOL_GPL(of_platform_populate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) int of_platform_default_populate(struct device_node *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) const struct of_dev_auxdata *lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) return of_platform_populate(root, of_default_bus_match_table, lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) EXPORT_SYMBOL_GPL(of_platform_default_populate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) #ifndef CONFIG_PPC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static const struct of_device_id reserved_mem_matches[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) { .compatible = "qcom,rmtfs-mem" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) { .compatible = "qcom,cmd-db" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) { .compatible = "ramoops" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static int __init of_platform_default_populate_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) device_links_supplier_sync_state_pause();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (!of_have_populated_dt())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) * Handle certain compatibles explicitly, since we don't want to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * platform_devices for every node in /reserved-memory with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * "compatible",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) for_each_matching_node(node, reserved_mem_matches)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) of_platform_device_create(node, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) node = of_find_node_by_path("/firmware");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) of_platform_populate(node, NULL, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /* Populate everything else. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) of_platform_default_populate(NULL, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) arch_initcall_sync(of_platform_default_populate_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static int __init of_platform_sync_state_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) device_links_supplier_sync_state_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) late_initcall_sync(of_platform_sync_state_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) int of_platform_device_destroy(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) /* Do not touch devices not populated from the device tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* Recurse for any nodes that were treated as busses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) device_for_each_child(dev, NULL, of_platform_device_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) of_node_clear_flag(dev->of_node, OF_POPULATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (dev->bus == &platform_bus_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) platform_device_unregister(to_platform_device(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) #ifdef CONFIG_ARM_AMBA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) else if (dev->bus == &amba_bustype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) amba_device_unregister(to_amba_device(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) EXPORT_SYMBOL_GPL(of_platform_device_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * of_platform_depopulate() - Remove devices populated from device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * @parent: device which children will be removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * Complementary to of_platform_populate(), this function removes children
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * of the given device (and, recurrently, their children) that have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * created from their respective device tree nodes (and only those,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * leaving others - eg. manually created - unharmed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) void of_platform_depopulate(struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) EXPORT_SYMBOL_GPL(of_platform_depopulate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static void devm_of_platform_populate_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) of_platform_depopulate(*(struct device **)res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) * devm_of_platform_populate() - Populate platform_devices from device tree data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * @dev: device that requested to populate from device tree data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * Similar to of_platform_populate(), but will automatically call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * of_platform_depopulate() when the device is unbound from the bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * Returns 0 on success, < 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) int devm_of_platform_populate(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct device **ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) ptr = devres_alloc(devm_of_platform_populate_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) sizeof(*ptr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) *ptr = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) EXPORT_SYMBOL_GPL(devm_of_platform_populate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) static int devm_of_platform_match(struct device *dev, void *res, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) struct device **ptr = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (!ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) WARN_ON(!ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) return *ptr == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * devm_of_platform_depopulate() - Remove devices populated from device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * @dev: device that requested to depopulate from device tree data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * Complementary to devm_of_platform_populate(), this function removes children
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * of the given device (and, recurrently, their children) that have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * created from their respective device tree nodes (and only those,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * leaving others - eg. manually created - unharmed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) void devm_of_platform_depopulate(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) ret = devres_release(dev, devm_of_platform_populate_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) devm_of_platform_match, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) WARN_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) #ifdef CONFIG_OF_DYNAMIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) static int of_platform_notify(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) unsigned long action, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) struct of_reconfig_data *rd = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) struct platform_device *pdev_parent, *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) bool children_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) switch (of_reconfig_get_state_change(action, rd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) case OF_RECONFIG_CHANGE_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /* verify that the parent is a bus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return NOTIFY_OK; /* not for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) /* already populated? (driver using of_populate manually) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (of_node_check_flag(rd->dn, OF_POPULATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /* pdev_parent may be NULL when no bus platform device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) pdev_parent = of_find_device_by_node(rd->dn->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) pdev = of_platform_device_create(rd->dn, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) pdev_parent ? &pdev_parent->dev : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) of_dev_put(pdev_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (pdev == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) pr_err("%s: failed to create for '%pOF'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) __func__, rd->dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) /* of_platform_device_create tosses the error code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return notifier_from_errno(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) case OF_RECONFIG_CHANGE_REMOVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) /* already depopulated? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) if (!of_node_check_flag(rd->dn, OF_POPULATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) /* find our device by node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) pdev = of_find_device_by_node(rd->dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (pdev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return NOTIFY_OK; /* no? not meant for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) /* unregister takes one ref away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) of_platform_device_destroy(&pdev->dev, &children_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) /* and put the reference of the find */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) of_dev_put(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) static struct notifier_block platform_of_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) .notifier_call = of_platform_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) void of_platform_register_reconfig_notifier(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) #endif /* CONFIG_OF_DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) #endif /* CONFIG_OF_ADDRESS */