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

2012-04-26 Thread Stephen Warren
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?
___
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel


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

2012-04-25 Thread Thierry Reding
* 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
-- next part --
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: 



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

2012-04-25 Thread Thierry Reding
This function resolves an OF device node to an I2C adapter registered
with the I2C core.

Signed-off-by: Thierry Reding 
---
 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(_lock);
+
+   err = idr_for_each(_adapter_idr, of_i2c_match_node, node);
+   if (err < 0)
+   goto out;
+
+   adapter = idr_find(_adapter_idr, err);
+   if (adapter && !try_module_get(adapter->owner))
+   adapter = NULL;
+
+out:
+   mutex_unlock(_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



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

2012-04-25 Thread Stephen Warren
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?


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

2012-04-25 Thread Thierry Reding
This function resolves an OF device node to an I2C adapter registered
with the I2C core.

Signed-off-by: Thierry Reding thierry.red...@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

___
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel


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

2012-04-25 Thread Thierry Reding
* 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


pgpK0x1DckXRm.pgp
Description: PGP signature
___
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel