All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Mack <daniel@zonque.org>
To: zbr@ioremap.net, robh+dt@kernel.org, mark.rutland@arm.com,
	szabolcs.gyurko@tlt.hu
Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	Daniel Mack <daniel@zonque.org>
Subject: [PATCH RFC 3/4] w1: core: provide helper to look up w1 slaves through devicetree nodes
Date: Tue, 19 Jun 2018 23:27:43 +0200	[thread overview]
Message-ID: <20180619212744.794-4-daniel@zonque.org> (raw)
In-Reply-To: <20180619212744.794-1-daniel@zonque.org>

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@zonque.org>
---
 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


  parent reply	other threads:[~2018-06-19 21:28 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-19 21:27 [PATCH RFC 0/4] Adding DT functionality to w1 busses Daniel Mack
2018-06-19 21:27 ` [PATCH RFC 1/4] dt-bindings: w1: document sub-node bindings for DS2760 Daniel Mack
2018-06-26 21:33   ` Rob Herring
2018-06-19 21:27 ` [PATCH RFC 2/4] w1: core: match sub-nodes of bus masters in devicetree Daniel Mack
2018-06-19 21:27 ` Daniel Mack [this message]
2018-06-19 21:27 ` [PATCH RFC 4/4] w1: ds2760: add devicetree matching glue Daniel Mack
2018-06-26 21:39 ` [PATCH RFC 0/4] Adding DT functionality to w1 busses Rob Herring
2018-06-27 19:12   ` Daniel Mack

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180619212744.794-4-daniel@zonque.org \
    --to=daniel@zonque.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=robh+dt@kernel.org \
    --cc=szabolcs.gyurko@tlt.hu \
    --cc=zbr@ioremap.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.