[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220318160059.328208-3-clement.leger@bootlin.com>
Date: Fri, 18 Mar 2022 17:00:48 +0100
From: Clément Léger <clement.leger@...tlin.com>
To: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Daniel Scally <djrscally@...il.com>,
Heikki Krogerus <heikki.krogerus@...ux.intel.com>,
Sakari Ailus <sakari.ailus@...ux.intel.com>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"'Rafael J . Wysocki '" <rafael@...nel.org>,
Wolfram Sang <wsa@...nel.org>, Peter Rosin <peda@...ntia.se>,
Russell King <linux@...linux.org.uk>,
Andrew Lunn <andrew@...n.ch>,
Heiner Kallweit <hkallweit1@...il.com>,
"David S . Miller" <davem@...emloft.net>,
Jakub Kicinski <kuba@...nel.org>,
Paolo Abeni <pabeni@...hat.com>
Cc: Hans de Goede <hdegoede@...hat.com>,
Thomas Petazzoni <thomas.petazzoni@...tlin.com>,
Alexandre Belloni <alexandre.belloni@...tlin.com>,
Allan Nielsen <allan.nielsen@...rochip.com>,
linux-kernel@...r.kernel.org, linux-acpi@...r.kernel.org,
linux-i2c@...r.kernel.org, netdev@...r.kernel.org,
Clément Léger <clement.leger@...tlin.com>
Subject: [PATCH 2/6] i2c: fwnode: add fwnode_find_i2c_adapter_by_node()
Add fwnode_find_i2c_adapter_by_node() which allows to retrieve a i2c
adapter using a fwnode. Since dev_fwnode() uses the fwnode provided by
the of_node member of the device, this will also work for devices were
the of_node has been set and not the fwnode field.
Signed-off-by: Clément Léger <clement.leger@...tlin.com>
---
drivers/i2c/Makefile | 1 +
drivers/i2c/i2c-core-fwnode.c | 41 +++++++++++++++++++++++++++++++++++
include/linux/i2c.h | 2 ++
3 files changed, 44 insertions(+)
create mode 100644 drivers/i2c/i2c-core-fwnode.c
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile
index c1d493dc9bac..c9c97675163e 100644
--- a/drivers/i2c/Makefile
+++ b/drivers/i2c/Makefile
@@ -6,6 +6,7 @@
obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o
obj-$(CONFIG_I2C) += i2c-core.o
i2c-core-objs := i2c-core-base.o i2c-core-smbus.o
+i2c-core-objs += i2c-core-fwnode.o
i2c-core-$(CONFIG_ACPI) += i2c-core-acpi.o
i2c-core-$(CONFIG_I2C_SLAVE) += i2c-core-slave.o
i2c-core-$(CONFIG_OF) += i2c-core-of.o
diff --git a/drivers/i2c/i2c-core-fwnode.c b/drivers/i2c/i2c-core-fwnode.c
new file mode 100644
index 000000000000..2404c2477a80
--- /dev/null
+++ b/drivers/i2c/i2c-core-fwnode.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Linux I2C core fwnode support code
+ *
+ * Copyright (C) 2022 Microchip
+ */
+
+#include <linux/device.h>
+#include <linux/i2c.h>
+
+#include "i2c-core.h"
+
+static int fwnode_dev_or_parent_node_match(struct device *dev, const void *data)
+{
+ if (device_match_fwnode(dev, data))
+ return 1;
+
+ /* For ACPI device node, we do not want to match the parent */
+ if (!is_acpi_device_node(dev_fwnode(dev)) && dev->parent)
+ return device_match_fwnode(dev->parent, data);
+
+ return 0;
+}
+
+struct i2c_adapter *fwnode_find_i2c_adapter_by_node(struct fwnode_handle *node)
+{
+ struct device *dev;
+ struct i2c_adapter *adapter;
+
+ dev = bus_find_device(&i2c_bus_type, NULL, node,
+ fwnode_dev_or_parent_node_match);
+ if (!dev)
+ return NULL;
+
+ adapter = i2c_verify_adapter(dev);
+ if (!adapter)
+ put_device(dev);
+
+ return adapter;
+}
+EXPORT_SYMBOL(fwnode_find_i2c_adapter_by_node);
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 7d4f52ceb7b5..9b480a8b0a76 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -967,6 +967,8 @@ int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
#endif /* I2C */
+struct i2c_adapter *fwnode_find_i2c_adapter_by_node(struct fwnode_handle *node);
+
#if IS_ENABLED(CONFIG_OF)
/* must call put_device() when done with returned i2c_client device */
struct i2c_client *of_find_i2c_device_by_node(struct device_node *node);
--
2.34.1
Powered by blists - more mailing lists