Skip to content

Commit

Permalink
driver core: Add fwnode_to_dev() to look up device from fwnode
Browse files Browse the repository at this point in the history
It's often useful to look up a device that corresponds to a fwnode. So
add an API to do that irrespective of the bus on which the device has
been added to.

Signed-off-by: Saravana Kannan <saravanak@google.com>
Link: https://lore.kernel.org/r/20190904211126.47518-2-saravanak@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Saravana Kannan authored and Greg Kroah-Hartman committed Oct 4, 2019
1 parent 54ecb8f commit 372a67c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions drivers/base/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,10 @@ int device_add(struct device *dev)
BUS_NOTIFY_ADD_DEVICE, dev);

kobject_uevent(&dev->kobj, KOBJ_ADD);

if (dev->fwnode && !dev->fwnode->dev)
dev->fwnode->dev = dev;

bus_probe_device(dev);
if (parent)
klist_add_tail(&dev->p->knode_parent,
Expand Down Expand Up @@ -2342,6 +2346,9 @@ void device_del(struct device *dev)
kill_device(dev);
device_unlock(dev);

if (dev->fwnode && dev->fwnode->dev == dev)
dev->fwnode->dev = NULL;

/* Notify clients of device removal. This call must come
* before dpm_sysfs_remove().
*/
Expand Down
2 changes: 2 additions & 0 deletions include/linux/fwnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct device;
struct fwnode_handle {
struct fwnode_handle *secondary;
const struct fwnode_operations *ops;
struct device *dev;
};

/**
Expand Down Expand Up @@ -123,5 +124,6 @@ struct fwnode_operations {
if (fwnode_has_op(fwnode, op)) \
(fwnode)->ops->op(fwnode, ## __VA_ARGS__); \
} while (false)
#define get_dev_from_fwnode(fwnode) get_device((fwnode)->dev)

#endif

0 comments on commit 372a67c

Please sign in to comment.