Skip to contentSkip to conversation

i2c: core: add of_get_i2c_adapter_by_node

RFC PATCH In review Frkuska, Joshua 3 participants 3 messages Jun 29, 2015 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 i2c: core: add of_get_i2c_adapter_by_node

This adds of_get_i2c_adapter_by_node which both finds and properly increments the adapter reference count.

This is different from of_find_i2c_device_by_node which does not increment the adapter reference counter.

---
 drivers/i2c/i2c-core.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/i2c/i2c-core.c +20 -0
index 987c124..97caec6 100644
@@ -1347,6 +1347,26 @@ struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
     return i2c_verify_adapter(dev);
 }
 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
+
+/* must call put_device() when done with returned i2c_adapter device */
+struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
+{
+    struct device *dev;
+    struct i2c_adapter *adapter;
+
+    dev = bus_find_device(&i2c_bus_type, NULL, node,
+                of_dev_node_match);
+    if (!dev)
+        return NULL;
+
+    adapter = i2c_verify_adapter(dev);
+
+    if (adapter && !try_module_get(adapter->owner))
+        adapter = NULL;
+
+    return adapter;
+}
+EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
 #else
 static void of_i2c_register_devices(struct i2c_adapter *adap) { }
 #endif /* CONFIG_OF */
Signature · 3 lines
Joshua Frkuska | Embedded Software Engineer
Mentor Embedded Software Division
Mentor Graphics Japan Co., ltd.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

Hi Wolfram,

Quoted 11 lines · Wolfram Sang
Quoted 8 lines · Joshua

This adds of_get_i2c_adapter_by_node which both finds and properly increments the adapter reference count.

This is different from of_find_i2c_device_by_node which does not increment the adapter reference counter.

What is your use case?

the usecase is to avoid double getting of a pointer to the wanted struct i2c_adapter with incrementing its user counter at once, now it is done in two stages, namely find adapter and get adapter:

------8<------

struct i2c_adapter *ddc;
int id;
...

- ddc = of_find_i2c_adapter_by_node(ddc_node);
- if (ddc) {
- id = i2c_adapter_id(dcc);
- dcc = i2c_get_adapter(id);
+ dcc = of_get_i2c_adapter_by_node(ddc);
+ if (dcc)
+ put_device(&ddc->dev);
put_device(&ddc->dev);
- }

...
i2c_put_adapter(ddc);

------8<------

Since i2c adapted is locked by client, it might have sense to add put_device() hidden inside of_get_i2c_adapter_by_node().

Quoted 2 lines

Which code will be using this function?

At the moment there is an intention to add clients in drivers/gpu/drm/*, who should lock i2c bus driver to sustain DDC lines.

By the way to the current version the correspondent change in include/linux/i2c.h is missing:

------8<------

include/linux/i2c.h +0 -0
index e83a738..555ad9c 100644
@@ -638,6 +638,9 @@ extern struct i2c_client

*of_find_i2c_device_by_node(struct device_node *node);
/* must call put_device() when done with returned i2c_adapter device */ extern struct i2c_adapter *of_find_i2c_adapter_by_node(struct
device_node *node);

+/* must call put_device() when done with returned i2c_adapter device */ +extern struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node);
+
#else

static inline struct i2c_client *of_find_i2c_device_by_node(struct
device_node *node)
@@ -649,6 +652,11 @@ static inline struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node
{
return NULL;
}
+
+static inline struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
+{
+ return NULL;
+}
#endif /* CONFIG_OF */

#endif /* _LINUX_I2C_H */

------8<------

--
With best wishes,
Vladimir