Refactor and enhance device tree platform registrations
Subsystems (7)
v2 cover Refactor and enhance device tree platform registrations
Technically only the last two patches are v2 patches, the first three are cleanups written when they were refactored, and this is a first posting for them.
The most interesting part of this series is the addition of of_platform_prepare() which is useful to allow DT and non-DT board support to share the same initialization code for on-chip platform devices.
---
Grant Likely (5):
dt: Refactor of_platform_bus_probe()
dt: protect against NULL matches passed to of_match_node()
dt: eliminate OF_NO_DEEP_PROBE and test for NULL match table
dt: add of_platform_populate() for creating device from the device tree
dt: add of_platform_prepare() to match nodes with static platform_devices
arch/arm/mach-versatile/core.c | 1 arch/powerpc/platforms/52xx/mpc52xx_common.c | 10 - drivers/of/address.c | 14 + drivers/of/base.c | 3 drivers/of/platform.c | 347 ++++++++++++++++++++++---- include/linux/of_address.h | 1 include/linux/of_platform.h | 8 - 7 files changed, 326 insertions(+), 58 deletions(-)
Signature · 1 line
Signature
PATCH v2 1/5 dt: Refactor of_platform_bus_probe()
The current implementation uses three copies of of basically identical code. This patch consolidates them to make the code simpler.
--- drivers/of/platform.c | 55 ++++++++++++++++--------------------------------- 1 files changed, 18 insertions(+), 37 deletions(-)
drivers/of/platform.c +18 -37
@@ -210,13 +210,16 @@ struct platform_device *of_platform_device_create(struct device_node *np,EXPORT_SYMBOL (of_platform_device_create);/** -* of_platform_bus_create - Create an OF device for a bus node and all its -* children. Optionally recursively instantiate matching busses. +* of_platform_bus_create() - Create a device for a node and its children. * @bus: device node of the bus to instantiate * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to -* disallow recursive creation of child busses +* disallow recursive creation of child buses +* @parent: parent for new device, or NULL for top level. +* +* 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 (const struct device_node *bus, +static int of_platform_bus_create (struct device_node *bus,const struct of_device_id *matches,struct device *parent) {
@@ -224,18 +227,13 @@ static int of_platform_bus_create(const struct device_node *bus,struct platform_device *dev;int rc =0 ; + dev =of_platform_device_create (bus,NULL , parent); +if (!dev || !of_match_node (matches, bus)) +return 0 ; +for_each_child_of_node (bus, child) {pr_debug (" create child: %s\n" , child->full_name); - dev =of_platform_device_create (child,NULL , parent); -if (dev ==NULL ) -continue ; - -if (!of_match_node (matches, child)) -continue ; -if (rc ==0 ) { -pr_debug (" and sub busses\n" ); - rc =of_platform_bus_create (child, matches, &dev->dev); - } + rc =of_platform_bus_create (child, matches, &dev->dev);if (rc) {of_node_put (child);break ;
@@ -245,7 +243,7 @@ static int of_platform_bus_create(const struct device_node *bus, }/** -* of_platform_bus_probe - Probe the device-tree for platform busses +* 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, NULL to use the default * @parent: parent to hook devices from, NULL for toplevel
@@ -258,7 +256,6 @@ int of_platform_bus_probe(struct device_node *root, struct device *parent) {struct device_node *child; -struct platform_device *dev;int rc =0 ;if (WARN_ON (!matches || matches ==OF_NO_DEEP_PROBE ))
@@ -277,31 +274,15 @@ int of_platform_bus_probe(struct device_node *root,* children */ if (of_match_node (matches, root)) { -pr_debug (" root match, create all sub devices\n" ); - dev =of_platform_device_create (root,NULL , parent); -if (dev ==NULL ) - goto bail; - -pr_debug (" create all sub busses\n" ); - rc =of_platform_bus_create (root, matches, &dev->dev); - goto bail; - } -for_each_child_of_node (root, child) { + rc =of_platform_bus_create (root, matches, parent); + } else for_each_child_of_node(root, child) {if (!of_match_node (matches, child))continue ; - -pr_debug (" match: %s\n" , child->full_name); - dev =of_platform_device_create (child,NULL , parent); -if (dev ==NULL ) -continue ; - - rc =of_platform_bus_create (child, matches, &dev->dev); -if (rc) { -of_node_put (child); + rc =of_platform_bus_create (child, matches, parent); +if (rc)break ; - } } - bail: +of_node_put (root);return rc; }
PATCH v2 2/5 dt: protect against NULL matches passed to of_match_node()
In reply to Grant LikelyThere are a few use cases where it is convenient to pass NULL to of_match_node() and have it fail gracefully. The patch adds a null check to the beginning so taht it does so.
--- drivers/of/base.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
drivers/of/base.c +3 -0
@@ -496,6 +496,9 @@ EXPORT_SYMBOL(of_find_node_with_property);const struct of_device_id *of_match_node (const struct of_device_id *matches,const struct device_node *node) { +if (!matches) +return NULL ; +while (matches->name[0 ] || matches->type[0 ] || matches->compatible[0 ]) {int match =1 ;if (matches->name[0 ])
PATCH v2 3/5 dt: eliminate OF_NO_DEEP_PROBE and test for NULL match table
In reply to Grant LikelyThere are no users of OF_NO_DEEP_PROBE, and of_match_node() now gracefully handles being passed a NULL pointer, so the checks at the top of of_platform_bus_probe can be dropped.
While at it, consolidate the root node pointer check to be easier to read and tidy up related comments.
--- drivers/of/platform.c | 17 +++++------------ include/linux/of_platform.h | 3 --- 2 files changed, 5 insertions(+), 15 deletions(-)
drivers/of/platform.c +5 -12
@@ -212,7 +212,7 @@ EXPORT_SYMBOL(of_platform_device_create);/** * of_platform_bus_create() - Create a device for a node and its children. * @bus: device node of the bus to instantiate -* @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to +* @matches: match table for bus nodes * disallow recursive creation of child buses * @parent: parent for new device, or NULL for top level. *
@@ -245,7 +245,7 @@ static int of_platform_bus_create(struct device_node *bus,/** * 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, NULL to use the default +* @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
@@ -258,21 +258,14 @@ int of_platform_bus_probe(struct device_node *root,struct device_node *child;int rc =0 ; -if (WARN_ON (!matches || matches ==OF_NO_DEEP_PROBE )) -return -EINVAL ; -if (root ==NULL ) - root =of_find_node_by_path ("/" ); -else -of_node_get (root); -if (root ==NULL ) + root = root ?of_node_get (root) :of_find_node_by_path ("/" ); +if (!root)return -EINVAL ;pr_debug ("of_platform_bus_probe()\n" );pr_debug (" starting at: %s\n" , root->full_name); -/* Do a self check of bus type, if there's a match, create -* children -*/ +/* Do a self check of bus type, if there's a match, create children */ if (of_match_node (matches, root)) { rc =of_platform_bus_create (root, matches, parent); } else for_each_child_of_node(root, child) {
include/linux/of_platform.h +0 -3
@@ -52,9 +52,6 @@ extern struct platform_device *of_platform_device_create(struct device_node *np,const char *bus_id, struct device *parent); -/* pseudo "matches" value to not do deep probe */ -#define OF_NO_DEEP_PROBE ((struct of_device_id *)-1) -extern int of_platform_bus_probe (struct device_node *root,const struct of_device_id *matches,struct device *parent);
PATCH v2 4/5 dt: add of_platform_populate() for creating device from the device tree
In reply to Grant Likelyof_platform_populate() is similar to of_platform_bus_probe() except that it strictly enforces that all device nodes must have a compatible property, and it can be used to register devices (not buses) which are children of the root node.
This patch also modifies MPC5200 support to use the new function.
--- arch/powerpc/platforms/52xx/mpc52xx_common.c | 10 ++--- drivers/of/platform.c | 54 ++++++++++++++++++++++++-- include/linux/of_platform.h | 3 + 3 files changed, 57 insertions(+), 10 deletions(-)
arch/powerpc/platforms/52xx/mpc52xx_common.c +4 -6
@@ -97,13 +97,11 @@ struct mpc52xx_gpio_wkup __iomem *wkup_gpio;* of the localplus bus to the of_platform * bus. */ -void __init -mpc52xx_declare_of_platform_devices (void ) +void __init mpc52xx_declare_of_platform_devices (void ) { -/* Find every child of the SOC node and add it to of_platform */ -if (of_platform_bus_probe (NULL , mpc52xx_bus_ids,NULL )) - printk(KERN_ERR __FILE__ ": " -"Error while probing of_platform bus\n" ); +/* Find all the 'platform' devices and register them. */ +if (of_platform_populate (NULL , mpc52xx_bus_ids,NULL )) +pr_err (__FILE__": Error while populating devices from DT\n" ); }/*
drivers/of/platform.c +50 -4
84 lines not on this page · raw message
include/linux/of_platform.h +3 -0
@@ -55,6 +55,9 @@ extern struct platform_device *of_platform_device_create(struct device_node *np,extern int of_platform_bus_probe (struct device_node *root,const struct of_device_id *matches,struct device *parent); +extern int of_platform_populate (struct device_node *root, +const struct of_device_id *matches, +struct device *parent);#endif /* !CONFIG_SPARC */ #endif /* CONFIG_OF_DEVICE */
PATCH v2 5/5 dt: add of_platform_prepare() to match nodes with static platform_devices
In reply to Grant LikelyThis patch implements an alternate method for using device tree data for populating machine device registration. Traditionally, board support has directly generated and registered devices based on nodes in the device tree. The board support code starts at the root of the tree and begins allocating devices for each device node it finds. Similarly, bus drivers (i2c, spi, etc.) use their child nodes to register child devices. This model can be seen in almost all the powerpc board ports (arch/powerpc/platforms/*).
However, for many of the ARM SoCs, there already exists complete board support for many SoCs that have their own code for registering the basic set of platform devices with non-trivial dependencies on clock structure and machine specific platform code. While starting at the base of the tree and working up is certainly possible, it requires modifying a lot of machine support code to get it working.
This patch adds the function of_platform_prepare() to declare which nodes will be used to create platform_devices without actually creating them yet. Then it uses a bus notifier to inspect platform_device registrations. If any match a node declared with of_platform_prepare(), it will set the of_node pointer before the device gets bound to a driver, making the device tree data available at probe time.
After all the static devices are registered, the platform code can still call either of_platform_populate or of_platform_bus_probe() on the same nodes that were declared with of_platform_prepare(). Any nodes that *were not* matched to a static platform_device will get created in the normal way.
Note: Board ports using this facility are still required to provide a fully populated device tree blob. It is not a shortcut to providing an accurate device tree model of the machine to the point that it would be reasonably possible to switch to a direct registration model for all devices without change the device tree. ie. The SoC still needs to be correctly identified and there should be nodes for all the discrete devices.
v2: Complete revamp to make of_platform_populate() and of_platform_bus_probe() respect the prepared nodes.
--- arch/arm/mach-versatile/core.c | 1 drivers/of/address.c | 14 ++ drivers/of/platform.c | 229 ++++++++++++++++++++++++++++++++++++++++ include/linux/of_address.h | 1 include/linux/of_platform.h | 2 5 files changed, 247 insertions(+), 0 deletions(-)
arch/arm/mach-versatile/core.c +1 -0
@@ -21,6 +21,7 @@#include <linux/init.h> #include <linux/device.h> #include <linux/dma-mapping.h> +#include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/sysdev.h> #include <linux/interrupt.h>
drivers/of/address.c +14 -0
@@ -556,6 +556,20 @@ static int __of_address_to_resource(struct device_node *dev, }/** +* of_address_count - Return the number of entries in the reg property +*/ +int of_address_count (struct device_node *np) +{ +struct resource temp_res; +int num_reg =0 ; + +while (of_address_to_resource (np, num_reg, &temp_res) ==0 ) + num_reg++; +return num_reg; +} +EXPORT_SYMBOL_GPL (of_address_count); + +/** * of_address_to_resource - Translate device tree address and return as resource * * Note that if your address is a PIO address, the conversion will fail if
drivers/of/platform.c +229 -0
253 lines not on this page · raw message
include/linux/of_address.h +1 -0
@@ -3,6 +3,7 @@#include <linux/ioport.h> #include <linux/of.h> +extern int of_address_count (struct device_node *np);extern u64 of_translate_address (struct device_node *np,const __be32 *addr);extern int of_address_to_resource (struct device_node *dev,int index,struct resource *r);
include/linux/of_platform.h +2 -0
@@ -52,6 +52,8 @@ extern struct platform_device *of_platform_device_create(struct device_node *np,const char *bus_id, struct device *parent); +extern void of_platform_prepare (struct device_node *root, +const struct of_device_id *matches);extern int of_platform_bus_probe (struct device_node *root,const struct of_device_id *matches,struct device *parent);
Re: [PATCH v2 4/5] dt: add of_platform_populate() for creating device from the device tree
In reply to Grant LikelyGrant,
This is a bit old, but my testing has uncovered a few issues.
Quoted 108 lines · Grant Likely
of_platform_populate()is similar toof_platform_bus_probe()except that it strictly enforces that all device nodes must have a compatible property, and it can be used to register devices (not buses) which are children of the root node.This patch also modifies MPC5200 support to use the new function.
Signed-off-by Grant Likely<grant.likely-s3s/[email protected]>--- arch/powerpc/platforms/52xx/mpc52xx_common.c | 10 ++--- drivers/of/platform.c | 54 ++++++++++++++++++++++++-- include/linux/of_platform.h | 3 + 3 files changed, 57 insertions(+), 10 deletions(-)arch/powerpc/platforms/52xx/mpc52xx_common.c +4
-6@@ -97,13 +97,11 @@ struct mpc52xx_gpio_wkup __iomem *wkup_gpio;* of the localplus bus to the of_platform * bus. */ -void __init -mpc52xx_declare_of_platform_devices (void ) +void __init mpc52xx_declare_of_platform_devices (void ) { -/* Find every child of the SOC node and add it to of_platform */ -if (of_platform_bus_probe (NULL , mpc52xx_bus_ids,NULL )) - printk(KERN_ERR __FILE__ ": " -"Error while probing of_platform bus\n" ); +/* Find all the 'platform' devices and register them. */ +if (of_platform_populate (NULL , mpc52xx_bus_ids,NULL )) +pr_err (__FILE__": Error while populating devices from DT\n" ); }/* drivers/of/platform.c +32
-4@@ -221,19 +221,26 @@ EXPORT_SYMBOL(of_platform_device_create);*/ static int of_platform_bus_create (struct device_node *bus,const struct of_device_id *matches, -struct device *parent) +struct device *parent,bool strict) {struct device_node *child;struct platform_device *dev;int rc =0 ; +/* Make sure it has a compatible property */ +if (strict&& (!of_get_property (bus,"compatible" ,NULL ))) { +pr_debug ("%s() - skipping %s, no compatible prop\n" , + __func__, bus->full_name); +return 0 ; + } + dev =of_platform_device_create (bus,NULL , parent);if (!dev || !of_match_node (matches, bus))return 0 ;for_each_child_of_node (bus, child) {pr_debug (" create child: %s\n" , child->full_name); - rc =of_platform_bus_create (child, matches,&dev->dev); + rc =of_platform_bus_create (child, matches,&dev->dev, strict);if (rc) {of_node_put (child);break ;@@ -267,11 +274,11 @@ int of_platform_bus_probe(struct device_node *root,/* Do a self check of bus type, if there's a match, create children */ if (of_match_node (matches, root)) { - rc =of_platform_bus_create (root, matches, parent); + rc =of_platform_bus_create (root, matches, parent, false); } else for_each_child_of_node(root, child) {if (!of_match_node (matches, child))continue ; - rc =of_platform_bus_create (child, matches, parent); + rc =of_platform_bus_create (child, matches, parent, false);if (rc)break ; }@@ -280,4 +287,43 @@ int of_platform_bus_probe(struct device_node *root,return rc; }EXPORT_SYMBOL (of_platform_bus_probe); + +/** +* 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 +* @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(). +* +* Returns 0 on success,< 0 on failure. +*/ +int of_platform_populate(struct device_node *root, + const struct of_device_id *matches,
drivers/of/platform.c int of_platform_bus_probe(struct device_node *root, · line 320
Quoted 10 lines
drivers/of/platform.c +10
-0+ struct device *parent) +{ +struct device_node *child; +int rc =0 ; + + root = root ?of_node_get (root) :of_find_node_by_path ("/" ); +if (!root) +return -EINVAL ; + +for_each_child_of_node (root, child) {
This needs a check of of_match_node. Otherwise, a device is created for all top level nodes, not just the matching node.
if (!of_match_node(matches, child)) continue;
drivers/of/platform.c int of_platform_bus_probe(struct device_node *root, · line 328
Quoted 8 lines
drivers/of/platform.c +8
-0+ rc =of_platform_bus_create (child, matches, parent, true); +if (rc) +break ; + } + +of_node_put (root); +return rc; +}
Missing EXPORT_SYMBOL.
Since this is only in next, do you want to fix it or I can submit a patch.
Rob