// SPDX-License-Identifier: GPL-2.0+ /* * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp. * <[email protected]> * and Arnd Bergmann, IBM Corp. * Merged from powerpc/kernel/of_platform.c and * sparc{,64}/kernel/of_device.c by Stephen Rothwell */ #define pr_fmt(fmt) … #include <linux/errno.h> #include <linux/module.h> #include <linux/amba/bus.h> #include <linux/device.h> #include <linux/dma-mapping.h> #include <linux/slab.h> #include <linux/of_address.h> #include <linux/of_device.h> #include <linux/of_irq.h> #include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/sysfb.h> #include "of_private.h" const struct of_device_id of_default_bus_match_table[] = …; /** * of_find_device_by_node - Find the platform_device associated with a node * @np: Pointer to device tree node * * Takes a reference to the embedded struct device which needs to be dropped * after use. * * Return: platform_device pointer, or NULL if not found */ struct platform_device *of_find_device_by_node(struct device_node *np) { … } EXPORT_SYMBOL(…); int of_device_add(struct platform_device *ofdev) { … } int of_device_register(struct platform_device *pdev) { … } EXPORT_SYMBOL(…); void of_device_unregister(struct platform_device *ofdev) { … } EXPORT_SYMBOL(…); #ifdef CONFIG_OF_ADDRESS static const struct of_device_id of_skipped_node_table[] = …; /* * The following routines scan a subtree and registers a device for * each applicable node. * * Note: sparc doesn't use these routines because it has a different * mechanism for creating devices from device tree nodes. */ /** * of_device_alloc - Allocate and initialize an of_device * @np: device node to assign to device * @bus_id: Name to assign to the device. May be null to use default name. * @parent: Parent device. */ struct platform_device *of_device_alloc(struct device_node *np, const char *bus_id, struct device *parent) { … } EXPORT_SYMBOL(…); /** * of_platform_device_create_pdata - Alloc, initialize and register an of_device * @np: pointer to node to create device for * @bus_id: name to assign device * @platform_data: pointer to populate platform_data pointer with * @parent: Linux device model parent device. * * Return: Pointer to created platform device, or NULL if a device was not * registered. Unavailable devices will not get registered. */ static struct platform_device *of_platform_device_create_pdata( struct device_node *np, const char *bus_id, void *platform_data, struct device *parent) { … } /** * of_platform_device_create - Alloc, initialize and register an of_device * @np: pointer to node to create device for * @bus_id: name to assign device * @parent: Linux device model parent device. * * Return: Pointer to created platform device, or NULL if a device was not * registered. Unavailable devices will not get registered. */ struct platform_device *of_platform_device_create(struct device_node *np, const char *bus_id, struct device *parent) { … } EXPORT_SYMBOL(…); #ifdef CONFIG_ARM_AMBA static struct amba_device *of_amba_device_create(struct device_node *node, const char *bus_id, void *platform_data, struct device *parent) { struct amba_device *dev; int ret; pr_debug("Creating amba device %pOF\n", node); if (!of_device_is_available(node) || of_node_test_and_set_flag(node, OF_POPULATED)) return NULL; dev = amba_device_alloc(NULL, 0, 0); if (!dev) goto err_clear_flag; /* AMBA devices only support a single DMA mask */ dev->dev.coherent_dma_mask = DMA_BIT_MASK(32); dev->dev.dma_mask = &dev->dev.coherent_dma_mask; /* setup generic device info */ device_set_node(&dev->dev, of_fwnode_handle(node)); dev->dev.parent = parent ? : &platform_bus; dev->dev.platform_data = platform_data; if (bus_id) dev_set_name(&dev->dev, "%s", bus_id); else of_device_make_bus_id(&dev->dev); /* Allow the HW Peripheral ID to be overridden */ of_property_read_u32(node, "arm,primecell-periphid", &dev->periphid); ret = of_address_to_resource(node, 0, &dev->res); if (ret) { pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n", ret, node); goto err_free; } ret = amba_device_add(dev, &iomem_resource); if (ret) { pr_err("amba_device_add() failed (%d) for %pOF\n", ret, node); goto err_free; } return dev; err_free: amba_device_put(dev); err_clear_flag: of_node_clear_flag(node, OF_POPULATED); return NULL; } #else /* CONFIG_ARM_AMBA */ static struct amba_device *of_amba_device_create(struct device_node *node, const char *bus_id, void *platform_data, struct device *parent) { … } #endif /* CONFIG_ARM_AMBA */ /* * of_dev_lookup() - Given a device node, lookup the preferred Linux name */ static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup, struct device_node *np) { … } /** * of_platform_bus_create() - Create a device for a node and its children. * @bus: device node of the bus to instantiate * @matches: match table for bus nodes * @lookup: auxdata table for matching id and platform_data with device nodes * @parent: parent for new device, or NULL for top level. * @strict: require compatible property * * Creates a platform_device for the provided device_node, and optionally * recursively create devices for all the child nodes. */ static int of_platform_bus_create(struct device_node *bus, const struct of_device_id *matches, const struct of_dev_auxdata *lookup, struct device *parent, bool strict) { … } /** * of_platform_bus_probe() - Probe the device-tree for platform buses * @root: parent of the first level to probe or NULL for the root of the tree * @matches: match table for bus nodes * @parent: parent to hook devices from, NULL for toplevel * * Note that children of the provided root are not instantiated as devices * unless the specified root itself matches the bus list and is not NULL. */ int of_platform_bus_probe(struct device_node *root, const struct of_device_id *matches, struct device *parent) { … } EXPORT_SYMBOL(…); /** * of_platform_populate() - Populate platform_devices from device tree data * @root: parent of the first level to probe or NULL for the root of the tree * @matches: match table, NULL to use the default * @lookup: auxdata table for matching id and platform_data with device nodes * @parent: parent to hook devices from, NULL for toplevel * * Similar to of_platform_bus_probe(), this function walks the device tree * and creates devices from nodes. It differs in that it follows the modern * convention of requiring all device nodes to have a 'compatible' property, * and it is suitable for creating devices which are children of the root * node (of_platform_bus_probe will only create children of the root which * are selected by the @matches argument). * * New board support should be using this function instead of * of_platform_bus_probe(). * * Return: 0 on success, < 0 on failure. */ int of_platform_populate(struct device_node *root, const struct of_device_id *matches, const struct of_dev_auxdata *lookup, struct device *parent) { … } EXPORT_SYMBOL_GPL(…); int of_platform_default_populate(struct device_node *root, const struct of_dev_auxdata *lookup, struct device *parent) { … } EXPORT_SYMBOL_GPL(…); static const struct of_device_id reserved_mem_matches[] = …; static int __init of_platform_default_populate_init(void) { … } arch_initcall_sync(of_platform_default_populate_init); static int __init of_platform_sync_state_init(void) { … } late_initcall_sync(of_platform_sync_state_init); int of_platform_device_destroy(struct device *dev, void *data) { … } EXPORT_SYMBOL_GPL(…); /** * of_platform_depopulate() - Remove devices populated from device tree * @parent: device which children will be removed * * Complementary to of_platform_populate(), this function removes children * of the given device (and, recursively, their children) that have been * created from their respective device tree nodes (and only those, * leaving others - eg. manually created - unharmed). */ void of_platform_depopulate(struct device *parent) { … } EXPORT_SYMBOL_GPL(…); static void devm_of_platform_populate_release(struct device *dev, void *res) { … } /** * devm_of_platform_populate() - Populate platform_devices from device tree data * @dev: device that requested to populate from device tree data * * Similar to of_platform_populate(), but will automatically call * of_platform_depopulate() when the device is unbound from the bus. * * Return: 0 on success, < 0 on failure. */ int devm_of_platform_populate(struct device *dev) { … } EXPORT_SYMBOL_GPL(…); static int devm_of_platform_match(struct device *dev, void *res, void *data) { … } /** * devm_of_platform_depopulate() - Remove devices populated from device tree * @dev: device that requested to depopulate from device tree data * * Complementary to devm_of_platform_populate(), this function removes children * of the given device (and, recursively, their children) that have been * created from their respective device tree nodes (and only those, * leaving others - eg. manually created - unharmed). */ void devm_of_platform_depopulate(struct device *dev) { … } EXPORT_SYMBOL_GPL(…); #ifdef CONFIG_OF_DYNAMIC static int of_platform_notify(struct notifier_block *nb, unsigned long action, void *arg) { … } static struct notifier_block platform_of_notifier = …; void of_platform_register_reconfig_notifier(void) { … } #endif /* CONFIG_OF_DYNAMIC */ #endif /* CONFIG_OF_ADDRESS */