diff mbox

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

Message ID 1335347102-14905-4-git-send-email-thierry.reding@avionic-design.de
State Not Applicable, archived
Headers show

Commit Message

Thierry Reding April 25, 2012, 9:45 a.m. UTC
This function resolves an OF device node to an I2C adapter registered
with the I2C core.

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

Comments

Stephen Warren April 25, 2012, 3:36 p.m. UTC | #1
On 04/25/2012 03:45 AM, Thierry Reding wrote:
> This function resolves an OF device node to an I2C adapter registered
> with the I2C core.

I think this is doing the same thing as a patch I posted recently:
http://www.spinics.net/lists/linux-i2c/msg07808.html

What's the advantage of one way over the other?
--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Thierry Reding April 25, 2012, 4:39 p.m. UTC | #2
* Stephen Warren wrote:
> On 04/25/2012 03:45 AM, Thierry Reding wrote:
> > This function resolves an OF device node to an I2C adapter registered
> > with the I2C core.
> 
> I think this is doing the same thing as a patch I posted recently:
> http://www.spinics.net/lists/linux-i2c/msg07808.html

I wasn't aware of that patch.

> What's the advantage of one way over the other?

Both are fine I think. Your version is shorter but mine may be a little
faster since it doesn't iterate over all devices on the bus. Since the
lookup doesn't happen very frequently this isn't much of an issue.

Thierry
diff mbox

Patch

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 */