Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

device property: Make it possible to use secondary firmware nodes

Add a secondary pointer to struct fwnode_handle so as to make it
possible for a device to have two firmware nodes associated with
it at the same time, for example, an ACPI node and a node with
a set of properties provided by platform initialization code.

In the future that will allow device property lookup to fall back
from the primary firmware node to the secondary one if the given
property is not present there to make it easier to provide defaults
for device properties used by device drivers.

Introduce two helper routines, set_primary_fwnode() and
set_secondary_fwnode() allowing callers to add a primary/secondary
firmware node to the given device in such a way that

(1) If there's only one firmware node for that device, it will be
pointed to by the device's firmware node pointer.
(2) If both the primary and secondary firmware nodes are present,
the primary one will be pointed to by the device's firmware
node pointer, while the secondary one will be pointed to by the
primary node's secondary pointer.
(3) If one of these nodes is removed (by calling one of the new
nelpers with NULL as the second argument), the other one will
be preserved.

Make ACPI use set_primary_fwnode() for attaching its firmware nodes
to devices.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Tested-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

+57 -2
+51
drivers/base/core.c
··· 12 12 13 13 #include <linux/device.h> 14 14 #include <linux/err.h> 15 + #include <linux/fwnode.h> 15 16 #include <linux/init.h> 16 17 #include <linux/module.h> 17 18 #include <linux/slab.h> ··· 2134 2133 define_dev_printk_level(_dev_info, KERN_INFO); 2135 2134 2136 2135 #endif 2136 + 2137 + static inline bool fwnode_is_primary(struct fwnode_handle *fwnode) 2138 + { 2139 + return fwnode && !IS_ERR(fwnode->secondary); 2140 + } 2141 + 2142 + /** 2143 + * set_primary_fwnode - Change the primary firmware node of a given device. 2144 + * @dev: Device to handle. 2145 + * @fwnode: New primary firmware node of the device. 2146 + * 2147 + * Set the device's firmware node pointer to @fwnode, but if a secondary 2148 + * firmware node of the device is present, preserve it. 2149 + */ 2150 + void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) 2151 + { 2152 + if (fwnode) { 2153 + struct fwnode_handle *fn = dev->fwnode; 2154 + 2155 + if (fwnode_is_primary(fn)) 2156 + fn = fn->secondary; 2157 + 2158 + fwnode->secondary = fn; 2159 + dev->fwnode = fwnode; 2160 + } else { 2161 + dev->fwnode = fwnode_is_primary(dev->fwnode) ? 2162 + dev->fwnode->secondary : NULL; 2163 + } 2164 + } 2165 + EXPORT_SYMBOL_GPL(set_primary_fwnode); 2166 + 2167 + /** 2168 + * set_secondary_fwnode - Change the secondary firmware node of a given device. 2169 + * @dev: Device to handle. 2170 + * @fwnode: New secondary firmware node of the device. 2171 + * 2172 + * If a primary firmware node of the device is present, set its secondary 2173 + * pointer to @fwnode. Otherwise, set the device's firmware node pointer to 2174 + * @fwnode. 2175 + */ 2176 + void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) 2177 + { 2178 + if (fwnode) 2179 + fwnode->secondary = ERR_PTR(-ENODEV); 2180 + 2181 + if (fwnode_is_primary(dev->fwnode)) 2182 + dev->fwnode->secondary = fwnode; 2183 + else 2184 + dev->fwnode = fwnode; 2185 + }
+2 -2
include/linux/acpi.h
··· 54 54 } 55 55 56 56 #define ACPI_COMPANION(dev) acpi_node((dev)->fwnode) 57 - #define ACPI_COMPANION_SET(dev, adev) (dev)->fwnode = (adev) ? \ 58 - acpi_fwnode_handle(adev) : NULL 57 + #define ACPI_COMPANION_SET(dev, adev) set_primary_fwnode(dev, (adev) ? \ 58 + acpi_fwnode_handle(adev) : NULL) 59 59 #define ACPI_HANDLE(dev) acpi_device_handle(ACPI_COMPANION(dev)) 60 60 61 61 static inline bool has_acpi_companion(struct device *dev)
+3
include/linux/device.h
··· 940 940 extern int lock_device_hotplug_sysfs(void); 941 941 extern int device_offline(struct device *dev); 942 942 extern int device_online(struct device *dev); 943 + extern void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); 944 + extern void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode); 945 + 943 946 /* 944 947 * Root device objects for grouping under /sys/devices 945 948 */
+1
include/linux/fwnode.h
··· 20 20 21 21 struct fwnode_handle { 22 22 enum fwnode_type type; 23 + struct fwnode_handle *secondary; 23 24 }; 24 25 25 26 #endif