Re: [alsa-devel] [RESEND PATCH v4 2/4] soundwire: core: add device tree support for slave devices

From: Srinivas Kandagatla
Date: Fri Aug 23 2019 - 11:47:11 EST




On 23/08/2019 16:44, Pierre-Louis Bossart wrote:


On 8/22/19 6:37 PM, Srinivas Kandagatla wrote:
This patch adds support to parsing device tree based
SoundWire slave devices.

Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@xxxxxxxxxx>
---
 drivers/soundwire/bus.c | 2 ++
 drivers/soundwire/bus.h | 1 +
 drivers/soundwire/slave.c | 52 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+)

diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c
index 49f64b2115b9..c2eaeb5c38ed 100644
--- a/drivers/soundwire/bus.c
+++ b/drivers/soundwire/bus.c
@@ -77,6 +77,8 @@ int sdw_add_bus_master(struct sdw_bus *bus)
ÂÂÂÂÂÂ */
ÂÂÂÂÂ if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
ÂÂÂÂÂÂÂÂÂ ret = sdw_acpi_find_slaves(bus);
+ÂÂÂ else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
+ÂÂÂÂÂÂÂ ret = sdw_of_find_slaves(bus);
ÂÂÂÂÂ else
ÂÂÂÂÂÂÂÂÂ ret = -ENOTSUPP; /* No ACPI/DT so error out */
diff --git a/drivers/soundwire/bus.h b/drivers/soundwire/bus.h
index 3048ca153f22..ee46befedbd1 100644
--- a/drivers/soundwire/bus.h
+++ b/drivers/soundwire/bus.h
@@ -15,6 +15,7 @@ static inline int sdw_acpi_find_slaves(struct sdw_bus *bus)
 }
 #endif
+int sdw_of_find_slaves(struct sdw_bus *bus);
 void sdw_extract_slave_id(struct sdw_bus *bus,
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ u64 addr, struct sdw_slave_id *id);
diff --git a/drivers/soundwire/slave.c b/drivers/soundwire/slave.c
index f39a5815e25d..3ef265d2ee89 100644
--- a/drivers/soundwire/slave.c
+++ b/drivers/soundwire/slave.c
@@ -2,6 +2,7 @@
 // Copyright(c) 2015-17 Intel Corporation.
 #include <linux/acpi.h>
+#include <linux/of.h>
 #include <linux/soundwire/sdw.h>
 #include <linux/soundwire/sdw_type.h>
 #include "bus.h"
@@ -35,6 +36,7 @@ static int sdw_slave_add(struct sdw_bus *bus,
ÂÂÂÂÂ slave->dev.release = sdw_slave_release;
ÂÂÂÂÂ slave->dev.bus = &sdw_bus_type;
+ÂÂÂ slave->dev.of_node = of_node_get(to_of_node(fwnode));
ÂÂÂÂÂ slave->bus = bus;
ÂÂÂÂÂ slave->status = SDW_SLAVE_UNATTACHED;
ÂÂÂÂÂ slave->dev_num = 0;
@@ -112,3 +114,53 @@ int sdw_acpi_find_slaves(struct sdw_bus *bus)
 }
 #endif
+
+/*
+ * sdw_of_find_slaves() - Find Slave devices in master device tree node
+ * @bus: SDW bus instance
+ *
+ * Scans Master DT node for SDW child Slave devices and registers it.
+ */
+int sdw_of_find_slaves(struct sdw_bus *bus)
+{
+ÂÂÂ struct device *dev = bus->dev;
+ÂÂÂ struct device_node *node;
+
+ÂÂÂ for_each_child_of_node(bus->dev->of_node, node) {
+ÂÂÂÂÂÂÂ int link_id, sdw_version, ret, len;
+ÂÂÂÂÂÂÂ const char *compat = NULL;
+ÂÂÂÂÂÂÂ struct sdw_slave_id id;
+ÂÂÂÂÂÂÂ const __be32 *addr;
+
+ÂÂÂÂÂÂÂ compat = of_get_property(node, "compatible", NULL);
+ÂÂÂÂÂÂÂ if (!compat)
+ÂÂÂÂÂÂÂÂÂÂÂ continue;
+
+ÂÂÂÂÂÂÂ ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &id.mfg_id, &id.part_id, &id.class_id);
+
+ÂÂÂÂÂÂÂ if (ret != 4) {
+ÂÂÂÂÂÂÂÂÂÂÂ dev_err(dev, "Invalid compatible string found %s\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ compat);
+ÂÂÂÂÂÂÂÂÂÂÂ continue;
+ÂÂÂÂÂÂÂ }
+
+ÂÂÂÂÂÂÂ addr = of_get_property(node, "reg", &len);
+ÂÂÂÂÂÂÂ if (!addr || (len < 2 * sizeof(u32))) {
+ÂÂÂÂÂÂÂÂÂÂÂ dev_err(dev, "Invalid Instance and Link ID\n");
+ÂÂÂÂÂÂÂÂÂÂÂ continue;
+ÂÂÂÂÂÂÂ }
+
+ÂÂÂÂÂÂÂ id.unique_id = be32_to_cpup(addr++);
+ÂÂÂÂÂÂÂ link_id = be32_to_cpup(addr);

So here the link ID is obviously not in the address, so you are not using the MIPI spec as we discussed initially?

No, Rob rejected that approach (https://lkml.org/lkml/2019/8/22/490) with the fact that compatible string should be constant for each instance.

--srini

+ÂÂÂÂÂÂÂ id.sdw_version = sdw_version;
+
+ÂÂÂÂÂÂÂ /* Check for link_id match */
+ÂÂÂÂÂÂÂ if (link_id != bus->link_id)
+ÂÂÂÂÂÂÂÂÂÂÂ continue;
+
+ÂÂÂÂÂÂÂ sdw_slave_add(bus, &id, of_fwnode_handle(node));
+ÂÂÂ }
+
+ÂÂÂ return 0;
+}