[RFC v2 3/5] i2c: Add of_i2c_get_adapter() function

Thierry Reding thierry.reding at avionic-design.de
Wed Apr 25 19:45:00 EST 2012


This function resolves an OF device node to an I2C adapter registered
with the I2C core.

Signed-off-by: Thierry Reding <thierry.reding at avionic-design.de>
---
 drivers/i2c/i2c-core.c |   34 ++++++++++++++++++++++++++++++++++
 include/linux/of_i2c.h |    7 +++++++
 2 files changed, 41 insertions(+)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index feb7dc3..e73b044 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1627,6 +1627,40 @@ void i2c_put_adapter(struct i2c_adapter *adap)
 }
 EXPORT_SYMBOL(i2c_put_adapter);
 
+#ifdef CONFIG_OF
+static int of_i2c_match_node(int id, void *p, void *data)
+{
+	struct device_node *node = data;
+	struct i2c_adapter *adapter = p;
+
+	if (adapter->dev.of_node == node)
+		return id;
+
+	return 0;
+}
+
+struct i2c_adapter *of_i2c_get_adapter(struct device_node *node)
+{
+	struct i2c_adapter *adapter = NULL;
+	int err;
+
+	mutex_lock(&core_lock);
+
+	err = idr_for_each(&i2c_adapter_idr, of_i2c_match_node, node);
+	if (err < 0)
+		goto out;
+
+	adapter = idr_find(&i2c_adapter_idr, err);
+	if (adapter && !try_module_get(adapter->owner))
+		adapter = NULL;
+
+out:
+	mutex_unlock(&core_lock);
+	return adapter;
+}
+EXPORT_SYMBOL(of_i2c_get_adapter);
+#endif
+
 /* The SMBus parts */
 
 #define POLY    (0x1070U << 3)
diff --git a/include/linux/of_i2c.h b/include/linux/of_i2c.h
index 0efe8d4..a35c497 100644
--- a/include/linux/of_i2c.h
+++ b/include/linux/of_i2c.h
@@ -20,11 +20,18 @@ extern void of_i2c_register_devices(struct i2c_adapter *adap);
 /* must call put_device() when done with returned i2c_client device */
 extern struct i2c_client *of_find_i2c_device_by_node(struct device_node *node);
 
+extern struct i2c_adapter *of_i2c_get_adapter(struct device_node *node);
+
 #else
 static inline void of_i2c_register_devices(struct i2c_adapter *adap)
 {
 	return;
 }
+
+static inline struct i2c_adapter *of_i2c_get_adapter(struct device_node *node)
+{
+	return NULL;
+}
 #endif /* CONFIG_OF_I2C */
 
 #endif /* __LINUX_OF_I2C_H */
-- 
1.7.10



More information about the devicetree-discuss mailing list