[PATCH RFC 3/4] w1: core: provide helper to look up w1 slaves through devicetree nodes
From: Daniel Mack
Date: Tue Jun 19 2018 - 17:28:04 EST
This patch adds a helper called w1_of_get_slave() that allows users to refer
to w1 slave devices through phandles in devicetree nodes.
The implementation walks all master devices and all their slaves in order to
find the right slave device.
The API is stubbed out for !CONFIG_OF.
Signed-off-by: Daniel Mack <daniel@xxxxxxxxxx>
---
drivers/w1/w1.c | 37 +++++++++++++++++++++++++++++++++++++
include/linux/w1.h | 11 +++++++++++
2 files changed, 48 insertions(+)
diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index dc73d8c08438..693aa9be2cd9 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -1185,6 +1185,43 @@ int w1_process(void *data)
return 0;
}
+#ifdef CONFIG_OF
+struct w1_slave *w1_of_get_slave(struct device_node *np,
+ const char *name, int index)
+{
+ struct device_node *slave_node;
+ struct w1_master *master;
+ struct w1_slave *sl;
+ bool found = false;
+
+ slave_node = of_parse_phandle(np, name, index);
+ if (!slave_node)
+ return NULL;
+
+ mutex_lock(&w1_mlock);
+ list_for_each_entry(master, &w1_masters, w1_master_entry) {
+ mutex_lock(&master->list_mutex);
+ list_for_each_entry(sl, &master->slist, w1_slave_entry) {
+ if (sl->dev.of_node == slave_node) {
+ found = true;
+ break;
+ }
+ }
+ mutex_unlock(&master->list_mutex);
+
+ if (found)
+ break;
+ }
+ mutex_unlock(&w1_mlock);
+
+ if (!found)
+ return NULL;
+
+ return sl;
+}
+EXPORT_SYMBOL_GPL(w1_of_get_slave);
+#endif /* CONFIG_OF */
+
static int __init w1_init(void)
{
int retval;
diff --git a/include/linux/w1.h b/include/linux/w1.h
index 3111585c371f..c44dffe782f0 100644
--- a/include/linux/w1.h
+++ b/include/linux/w1.h
@@ -322,6 +322,17 @@ static inline struct w1_master* dev_to_w1_master(struct device *dev)
return container_of(dev, struct w1_master, dev);
}
+#ifdef CONFIG_OF
+struct w1_slave *w1_of_get_slave(struct device_node *np,
+ const char *name, int index);
+#else
+static inline struct w1_slave *w1_of_get_slave(struct device_node *np,
+ const char *name, int index)
+{
+ return NULL;
+}
+#endif /* CONFIG_OF */
+
#endif /* __KERNEL__ */
#endif /* __LINUX_W1_H */
--
2.17.1