Skip to content

Commit

Permalink
drivers: Introduce device lookup variants by of_node
Browse files Browse the repository at this point in the history
Introduce wrappers for {bus/driver/class}_find_device() to
locate devices by its of_node.

Cc: Maarten Lankhorst <[email protected]>
Cc: Maxime Ripard <[email protected]>
Cc: [email protected]
Cc: David Airlie <[email protected]>
Cc: Daniel Vetter <[email protected]>
Cc: [email protected]
Cc: Florian Fainelli <[email protected]>
Cc: Frank Rowand <[email protected]>
Cc: Heiko Stuebner <[email protected]>
Cc: Liam Girdwood <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: Mathieu Poirier <[email protected]>
Cc: Rob Herring <[email protected]>
Cc: Srinivas Kandagatla <[email protected]>
Cc: Takashi Iwai <[email protected]>
Cc: Alan Tull <[email protected]>
Cc: [email protected]
Cc: Peter Rosin <[email protected]>
Cc: Florian Fainelli <[email protected]>
Cc: Heiner Kallweit <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: Liam Girdwood <[email protected]>
Cc: "Rafael J. Wysocki" <[email protected]>
Cc: Thor Thayer <[email protected]>
Cc: Jiri Slaby <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: Peter Rosin <[email protected]>
Signed-off-by: Suzuki K Poulose <[email protected]>
Acked-by: Lee Jones <[email protected]>
Acked-by: Wolfram Sang <[email protected]> # I2C part
Acked-by: Moritz Fischer <[email protected]> # For FPGA part
Acked-by: Mark Brown <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
Suzuki K Poulose authored and gregkh committed Jul 30, 2019
1 parent 6cda08a commit cfba5de
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 110 deletions.
11 changes: 1 addition & 10 deletions drivers/amba/tegra-ahb.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,13 @@ static inline void gizmo_writel(struct tegra_ahb *ahb, u32 value, u32 offset)
}

#ifdef CONFIG_TEGRA_IOMMU_SMMU
static int tegra_ahb_match_by_smmu(struct device *dev, const void *data)
{
struct tegra_ahb *ahb = dev_get_drvdata(dev);
const struct device_node *dn = data;

return (ahb->dev->of_node == dn) ? 1 : 0;
}

int tegra_ahb_enable_smmu(struct device_node *dn)
{
struct device *dev;
u32 val;
struct tegra_ahb *ahb;

dev = driver_find_device(&tegra_ahb_driver.driver, NULL, dn,
tegra_ahb_match_by_smmu);
dev = driver_find_device_by_of_node(&tegra_ahb_driver.driver, dn);
if (!dev)
return -EPROBE_DEFER;
ahb = dev_get_drvdata(dev);
Expand Down
8 changes: 1 addition & 7 deletions drivers/fpga/fpga-bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ static struct class *fpga_bridge_class;
/* Lock for adding/removing bridges to linked lists*/
static spinlock_t bridge_list_lock;

static int fpga_bridge_of_node_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

/**
* fpga_bridge_enable - Enable transactions on the bridge
*
Expand Down Expand Up @@ -104,8 +99,7 @@ struct fpga_bridge *of_fpga_bridge_get(struct device_node *np,
{
struct device *dev;

dev = class_find_device(fpga_bridge_class, NULL, np,
fpga_bridge_of_node_match);
dev = class_find_device_by_of_node(fpga_bridge_class, np);
if (!dev)
return ERR_PTR(-ENODEV);

Expand Down
8 changes: 1 addition & 7 deletions drivers/fpga/fpga-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,6 @@ struct fpga_manager *fpga_mgr_get(struct device *dev)
}
EXPORT_SYMBOL_GPL(fpga_mgr_get);

static int fpga_mgr_of_node_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

/**
* of_fpga_mgr_get - Given a device node, get a reference to a fpga mgr.
*
Expand All @@ -498,8 +493,7 @@ struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
{
struct device *dev;

dev = class_find_device(fpga_mgr_class, NULL, node,
fpga_mgr_of_node_match);
dev = class_find_device_by_of_node(fpga_mgr_class, node);
if (!dev)
return ERR_PTR(-ENODEV);

Expand Down
7 changes: 1 addition & 6 deletions drivers/gpu/drm/drm_mipi_dsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ static struct bus_type mipi_dsi_bus_type = {
.pm = &mipi_dsi_device_pm_ops,
};

static int of_device_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

/**
* of_find_mipi_dsi_device_by_node() - find the MIPI DSI device matching a
* device tree node
Expand All @@ -110,7 +105,7 @@ struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np)
{
struct device *dev;

dev = bus_find_device(&mipi_dsi_bus_type, NULL, np, of_device_match);
dev = bus_find_device_by_of_node(&mipi_dsi_bus_type, np);

return dev ? to_mipi_dsi_device(dev) : NULL;
}
Expand Down
7 changes: 1 addition & 6 deletions drivers/i2c/i2c-core-of.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ void of_i2c_register_devices(struct i2c_adapter *adap)
of_node_put(bus);
}

static int of_dev_node_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

static int of_dev_or_parent_node_match(struct device *dev, const void *data)
{
if (dev->of_node == data)
Expand All @@ -135,7 +130,7 @@ struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
struct device *dev;
struct i2c_client *client;

dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
dev = bus_find_device_by_of_node(&i2c_bus_type, node);
if (!dev)
return NULL;

Expand Down
14 changes: 2 additions & 12 deletions drivers/mfd/altera-sysmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,6 @@ static struct regmap_config altr_sysmgr_regmap_cfg = {
.use_single_write = true,
};

/**
* sysmgr_match_phandle
* Matching function used by driver_find_device().
* Return: True if match is found, otherwise false.
*/
static int sysmgr_match_phandle(struct device *dev, const void *data)
{
return dev->of_node == (const struct device_node *)data;
}

/**
* altr_sysmgr_regmap_lookup_by_phandle
* Find the sysmgr previous configured in probe() and return regmap property.
Expand All @@ -117,8 +107,8 @@ struct regmap *altr_sysmgr_regmap_lookup_by_phandle(struct device_node *np,
if (!sysmgr_np)
return ERR_PTR(-ENODEV);

dev = driver_find_device(&altr_sysmgr_driver.driver, NULL,
(void *)sysmgr_np, sysmgr_match_phandle);
dev = driver_find_device_by_of_node(&altr_sysmgr_driver.driver,
(void *)sysmgr_np);
of_node_put(sysmgr_np);
if (!dev)
return ERR_PTR(-EPROBE_DEFER);
Expand Down
7 changes: 1 addition & 6 deletions drivers/mux/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,12 @@ int mux_control_deselect(struct mux_control *mux)
}
EXPORT_SYMBOL_GPL(mux_control_deselect);

static int of_dev_node_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

/* Note this function returns a reference to the mux_chip dev. */
static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
{
struct device *dev;

dev = class_find_device(&mux_class, NULL, np, of_dev_node_match);
dev = class_find_device_by_of_node(&mux_class, np);

return dev ? to_mux_chip(dev) : NULL;
}
Expand Down
9 changes: 1 addition & 8 deletions drivers/net/phy/mdio_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,6 @@ static struct class mdio_bus_class = {
};

#if IS_ENABLED(CONFIG_OF_MDIO)
/* Helper function for of_mdio_find_bus */
static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
{
return dev->of_node == mdio_bus_np;
}
/**
* of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
* @mdio_bus_np: Pointer to the mii_bus.
Expand All @@ -287,9 +282,7 @@ struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
if (!mdio_bus_np)
return NULL;

d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
of_mdio_bus_match);

d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np);
return d ? to_mii_bus(d) : NULL;
}
EXPORT_SYMBOL(of_mdio_find_bus);
Expand Down
7 changes: 1 addition & 6 deletions drivers/nvmem/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,14 @@ static struct bus_type nvmem_bus_type = {
.name = "nvmem",
};

static int of_nvmem_match(struct device *dev, const void *nvmem_np)
{
return dev->of_node == nvmem_np;
}

static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
{
struct device *d;

if (!nvmem_np)
return NULL;

d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match);
d = bus_find_device_by_of_node(&nvmem_bus_type, nvmem_np);

if (!d)
return NULL;
Expand Down
8 changes: 1 addition & 7 deletions drivers/of/of_mdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,6 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
}
EXPORT_SYMBOL(of_mdiobus_register);

/* Helper function for of_phy_find_device */
static int of_phy_match(struct device *dev, const void *phy_np)
{
return dev->of_node == phy_np;
}

/**
* of_phy_find_device - Give a PHY node, find the phy_device
* @phy_np: Pointer to the phy's device tree node
Expand All @@ -301,7 +295,7 @@ struct phy_device *of_phy_find_device(struct device_node *phy_np)
if (!phy_np)
return NULL;

d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
d = bus_find_device_by_of_node(&mdio_bus_type, phy_np);
if (d) {
mdiodev = to_mdio_device(d);
if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
Expand Down
7 changes: 1 addition & 6 deletions drivers/of/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ static const struct of_device_id of_skipped_node_table[] = {
{} /* Empty terminated list */
};

static int of_dev_node_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

/**
* of_find_device_by_node - Find the platform_device associated with a node
* @np: Pointer to device tree node
Expand All @@ -55,7 +50,7 @@ struct platform_device *of_find_device_by_node(struct device_node *np)
{
struct device *dev;

dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
dev = bus_find_device_by_of_node(&platform_bus_type, np);
return dev ? to_platform_device(dev) : NULL;
}
EXPORT_SYMBOL(of_find_device_by_node);
Expand Down
7 changes: 1 addition & 6 deletions drivers/regulator/of_regulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,16 +460,11 @@ struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
return NULL;
}

static int of_node_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
{
struct device *dev;

dev = class_find_device(&regulator_class, NULL, np, of_node_match);
dev = class_find_device_by_of_node(&regulator_class, np);

return dev ? dev_to_rdev(dev) : NULL;
}
Expand Down
20 changes: 4 additions & 16 deletions drivers/spi/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3652,37 +3652,25 @@ EXPORT_SYMBOL_GPL(spi_write_then_read);
/*-------------------------------------------------------------------------*/

#if IS_ENABLED(CONFIG_OF)
static int __spi_of_device_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

/* must call put_device() when done with returned spi_device device */
struct spi_device *of_find_spi_device_by_node(struct device_node *node)
{
struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
__spi_of_device_match);
struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);

return dev ? to_spi_device(dev) : NULL;
}
EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
#endif /* IS_ENABLED(CONFIG_OF) */

#if IS_ENABLED(CONFIG_OF_DYNAMIC)
static int __spi_of_controller_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

/* the spi controllers are not using spi_bus, so we find it with another way */
static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
{
struct device *dev;

dev = class_find_device(&spi_master_class, NULL, node,
__spi_of_controller_match);
dev = class_find_device_by_of_node(&spi_master_class, node);
if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
dev = class_find_device(&spi_slave_class, NULL, node,
__spi_of_controller_match);
dev = class_find_device_by_of_node(&spi_slave_class, node);
if (!dev)
return NULL;

Expand Down
37 changes: 37 additions & 0 deletions include/linux/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ static inline struct device *bus_find_device_by_name(struct bus_type *bus,
return bus_find_device(bus, start, name, device_match_name);
}

/**
* bus_find_device_by_of_node : device iterator for locating a particular device
* matching the of_node.
* @bus: bus type
* @np: of_node of the device to match.
*/
static inline struct device *
bus_find_device_by_of_node(struct bus_type *bus, const struct device_node *np)
{
return bus_find_device(bus, NULL, np, device_match_of_node);
}

struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
struct device *hint);
int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
Expand Down Expand Up @@ -366,6 +378,19 @@ static inline struct device *driver_find_device_by_name(struct device_driver *dr
return driver_find_device(drv, NULL, name, device_match_name);
}

/**
* driver_find_device_by_of_node- device iterator for locating a particular device
* by of_node pointer.
* @driver: the driver we're iterating
* @np: of_node pointer to match.
*/
static inline struct device *
driver_find_device_by_of_node(struct device_driver *drv,
const struct device_node *np)
{
return driver_find_device(drv, NULL, np, device_match_of_node);
}

void driver_deferred_probe_add(struct device *dev);
int driver_deferred_probe_check_state(struct device *dev);
int driver_deferred_probe_check_state_continue(struct device *dev);
Expand Down Expand Up @@ -507,6 +532,18 @@ static inline struct device *class_find_device_by_name(struct class *class,
return class_find_device(class, NULL, name, device_match_name);
}

/**
* class_find_device_by_of_node : device iterator for locating a particular device
* matching the of_node.
* @class: class type
* @np: of_node of the device to match.
*/
static inline struct device *
class_find_device_by_of_node(struct class *class, const struct device_node *np)
{
return class_find_device(class, NULL, np, device_match_of_node);
}

struct class_attribute {
struct attribute attr;
ssize_t (*show)(struct class *class, struct class_attribute *attr,
Expand Down
9 changes: 2 additions & 7 deletions sound/soc/rockchip/rk3399_gru_sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,6 @@ static const struct dailink_match_data dailink_match[] = {
},
};

static int of_dev_node_match(struct device *dev, const void *data)
{
return dev->of_node == data;
}

static int rockchip_sound_codec_node_match(struct device_node *np_codec)
{
struct device *dev;
Expand All @@ -438,8 +433,8 @@ static int rockchip_sound_codec_node_match(struct device_node *np_codec)
continue;

if (dailink_match[i].bus_type) {
dev = bus_find_device(dailink_match[i].bus_type, NULL,
np_codec, of_dev_node_match);
dev = bus_find_device_by_of_node(dailink_match[i].bus_type,
np_codec);
if (!dev)
continue;
put_device(dev);
Expand Down

0 comments on commit cfba5de

Please sign in to comment.