Skip to contentSkip to conversation

soundwire: add master_device/driver support

RFC PATCH In review Pierre-Louis Bossart 2 participants 6 messages Feb 1, 2020 – Feb 15, 2020 SoundWire

lore
Share & tools
lore
#0 lore raw

cover soundwire: add master_device/driver support

The SoundWire master representation needs to evolve to take into account:

a) a May 2019 recommendation from Greg KH to remove platform devices

b) the need on Intel platforms to support hardware startup only once the power rail dependencies are settled. The SoundWire master is not an independent IP at all.

c) the need to deal with external wakes handled by the PCI subsystem in specific low-power modes

In case it wasn't clear, the SoundWire subsystem is currently unusable with v5.5 on devices hitting the shelves very soon (race conditions, power management, suspend/resume, etc). v5.6 will only provide interface changes and no functional improvement. We've circled on the same concepts since September 2019, and I hope this direction is now aligned with the suggestions from Vinod Koul and that we can target v5.7 as the 'SoundWire just works(tm)' version.

This series is provided as an RFC since it depends on patches already for review.

Pierre-Louis Bossart (2):
soundwire: bus_type: add master_device/driver support
soundwire: intel: transition to sdw_master_device/driver support

 drivers/soundwire/Makefile         |   2 +-
 drivers/soundwire/bus_type.c       | 141 +++++++++++++-
 drivers/soundwire/intel.c          |  96 ++++++----
 drivers/soundwire/intel.h          |   8 +-
 drivers/soundwire/intel_init.c     | 283 ++++++++++++++++++++++-------
 drivers/soundwire/master.c         | 119 ++++++++++++
 drivers/soundwire/slave.c          |   7 +-
 include/linux/soundwire/sdw.h      |  83 +++++++++
 include/linux/soundwire/sdw_type.h |  36 +++-
 9 files changed, 662 insertions(+), 113 deletions(-)
 create mode 100644 drivers/soundwire/master.c
Signature · 1 line
2.20.1
#1 lore raw

RFC PATCH 1/2 soundwire: bus_type: add master_device/driver support

In the existing SoundWire code, Master Devices are not explicitly represented - only SoundWire Slave Devices are exposed (the use of capital letters follows the SoundWire specification conventions).

The SoundWire Master Device provides the clock, synchronization information and command/control channels. When multiple links are supported, a Controller may expose more than one Master Device; they are typically embedded inside a larger audio cluster (be it in an SOC/chipset or an external audio codec), and we need to describe it using the Linux device and driver model. This will allow for configuration functions to account for external dependencies such as power rails, clock sources or wake-up mechanisms. This transition will also allow for better sysfs support without the reference count issues mentioned in the initial reviews.

In this patch, we convert the existing code to use an explicit sdw_slave_type, then define new objects (sdw_master_device and sdw_master_driver).

A parent (such as the Intel audio controller or its equivalent on Qualcomm devices) would use sdw_master_device_add() to create the device, passing a driver name as a parameter. The master device would be released when device_unregister() is invoked by the parent.

Note that since there is no standard for the Master host-facing interface, so the bus matching relies on a simple string matching (as previously done with platform devices).

The 'Master Device' driver exposes callbacks for probe/startup/shutdown/remove/process_wake. The startup, shutdown, and process wake need to be called by the parent directly (using wrappers), while the probe/remove are handled by the SoundWire bus core upon device creation and release.

Additional callbacks will be added in the future for e.g. autonomous clock stop modes.

---
 drivers/soundwire/Makefile         |   2 +-
 drivers/soundwire/bus_type.c       | 141 +++++++++++++++++++++++++++--
 drivers/soundwire/master.c         | 119 ++++++++++++++++++++++++
 drivers/soundwire/slave.c          |   7 +-
 include/linux/soundwire/sdw.h      |  83 +++++++++++++++++
 include/linux/soundwire/sdw_type.h |  36 +++++++-
 6 files changed, 377 insertions(+), 11 deletions(-)
 create mode 100644 drivers/soundwire/master.c
drivers/soundwire/Makefile +1 -1
index e2cdff990e9f..7319918e0aec 100644
@@ -4,7 +4,7 @@
 #
 
 #Bus Objs
-soundwire-bus-objs := bus_type.o bus.o slave.o mipi_disco.o stream.o
+soundwire-bus-objs := bus_type.o bus.o master.o slave.o mipi_disco.o stream.o
 obj-$(CONFIG_SOUNDWIRE) += soundwire-bus.o
 
 ifdef CONFIG_DEBUG_FS
drivers/soundwire/bus_type.c +133 -8

172 lines not on this page · raw message

drivers/soundwire/master.c new file +119 -0

119 lines not on this page · raw message

drivers/soundwire/slave.c +6 -1
index aace57fae7f8..7ca4f2d9bfa6 100644
@@ -14,6 +14,11 @@ static void sdw_slave_release(struct device *dev)
 	kfree(slave);
 }
 
+struct device_type sdw_slave_type = {
+	.name =		"sdw_slave",
+	.release =	sdw_slave_release,
+};
+
 static int sdw_slave_add(struct sdw_bus *bus,
 			 struct sdw_slave_id *id, struct fwnode_handle *fwnode)
 {
@@ -41,9 +46,9 @@ static int sdw_slave_add(struct sdw_bus *bus,
 			     id->class_id, id->unique_id);
 	}
 
-	slave->dev.release = sdw_slave_release;
 	slave->dev.bus = &sdw_bus_type;
 	slave->dev.of_node = of_node_get(to_of_node(fwnode));
+	slave->dev.type = &sdw_slave_type;
 	slave->bus = bus;
 	slave->status = SDW_SLAVE_UNATTACHED;
 	init_completion(&slave->enumeration_complete);
include/linux/soundwire/sdw.h +83 -0

101 lines not on this page · raw message

include/linux/soundwire/sdw_type.h +35 -1
index aaa7f4267c14..331ba58bda27 100644
@@ -5,16 +5,36 @@
 #define __SOUNDWIRE_TYPES_H
 
 extern struct bus_type sdw_bus_type;
+extern struct device_type sdw_slave_type;
+extern struct device_type sdw_master_type;
+
+static inline int is_sdw_slave(const struct device *dev)
+{
+	return dev->type == &sdw_slave_type;
+}
 
 #define drv_to_sdw_driver(_drv) container_of(_drv, struct sdw_driver, driver)
 
 #define sdw_register_driver(drv) \
 	__sdw_register_driver(drv, THIS_MODULE)
 
+static inline int is_sdw_master_device(const struct device *dev)
+{
+	return dev->type == &sdw_master_type;
+}
+
+#define drv_to_sdw_master_driver(_drv) \
+	container_of(_drv, struct sdw_master_driver, driver)
+
+#define sdw_register_master_driver(drv) \
+	__sdw_register_master_driver(drv, THIS_MODULE)
+
 int __sdw_register_driver(struct sdw_driver *drv, struct module *owner);
 void sdw_unregister_driver(struct sdw_driver *drv);
 
-int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size);
+int __sdw_register_master_driver(struct sdw_master_driver *mdrv,
+				 struct module *owner);
+void sdw_unregister_master_driver(struct sdw_master_driver *mdrv);
 
 /**
  * module_sdw_driver() - Helper macro for registering a Soundwire driver
@@ -27,4 +47,18 @@ int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size);
 #define module_sdw_driver(__sdw_driver) \
 	module_driver(__sdw_driver, sdw_register_driver, \
 			sdw_unregister_driver)
+
+/**
+ * module_sdw_master_driver() - Helper macro for registering a Soundwire
+ * Master driver
+ * @__sdw_master_driver: soundwire Master driver struct
+ *
+ * Helper macro for Soundwire Master drivers which do not do anything special in
+ * module init/exit. This eliminates a lot of boilerplate. Each module may only
+ * use this macro once, and calling it replaces module_init() and module_exit()
+ */
+#define module_sdw_master_driver(__sdw_master_driver) \
+	module_driver(__sdw_master_driver, sdw_register_master_driver, \
+			sdw_unregister_master_driver)
+
 #endif /* __SOUNDWIRE_TYPES_H */
Signature · 1 line
2.20.1
#2 lore raw

RFC PATCH 2/2 soundwire: intel: transition to sdw_master_device/driver support

In reply to Pierre-Louis Bossart

Use sdw_master_device and driver instead of platform devices

In addition, rather than a plain-vanilla init/exit, this patch provides 3 steps in the initialization (ACPI scan, probe, startup) which makes it easier to detect platform support for SoundWire, allocate required resources as early as possible, and conversely help make the startup() callback lighter-weight with only hardware register setup.

---
 drivers/soundwire/intel.c      |  96 +++++++----
 drivers/soundwire/intel.h      |   8 +-
 drivers/soundwire/intel_init.c | 283 +++++++++++++++++++++++++--------
 3 files changed, 285 insertions(+), 102 deletions(-)
drivers/soundwire/intel.c +61 -35
index e0c1fff7c4a0..b0599e3d9950 100644
@@ -17,6 +17,7 @@
 #include <linux/soundwire/sdw_registers.h>
 #include <linux/soundwire/sdw.h>
 #include <linux/soundwire/sdw_intel.h>
+#include <linux/soundwire/sdw_type.h>
 #include "cadence_master.h"
 #include "bus.h"
 #include "intel.h"
@@ -92,8 +93,6 @@
 #define SDW_ALH_STRMZCFG_DMAT		GENMASK(7, 0)
 #define SDW_ALH_STRMZCFG_CHN		GENMASK(19, 16)
 
-#define SDW_INTEL_QUIRK_MASK_BUS_DISABLE	BIT(1)
-
 enum intel_pdi_type {
 	INTEL_PDI_IN = 0,
 	INTEL_PDI_OUT = 1,
@@ -1082,24 +1081,23 @@ static int intel_init(struct sdw_intel *sdw)
 /*
  * probe and init
  */
-static int intel_probe(struct platform_device *pdev)
+static int intel_master_probe(struct sdw_master_device *md, void *link_ctx)
 {
-	struct sdw_cdns_stream_config config;
 	struct sdw_intel *sdw;
 	int ret;
 
-	sdw = devm_kzalloc(&pdev->dev, sizeof(*sdw), GFP_KERNEL);
+	sdw = devm_kzalloc(&md->dev, sizeof(*sdw), GFP_KERNEL);
 	if (!sdw)
 		return -ENOMEM;
 
-	sdw->instance = pdev->id;
-	sdw->link_res = dev_get_platdata(&pdev->dev);
-	sdw->cdns.dev = &pdev->dev;
+	sdw->instance = md->link_id;
+	sdw->link_res = link_ctx;
+	sdw->cdns.dev = &md->dev;
 	sdw->cdns.registers = sdw->link_res->registers;
-	sdw->cdns.instance = sdw->instance;
+	sdw->cdns.instance = md->link_id;
 	sdw->cdns.msg_count = 0;
-	sdw->cdns.bus.dev = &pdev->dev;
-	sdw->cdns.bus.link_id = pdev->id;
+	sdw->cdns.bus.dev = &md->dev;
+	sdw->cdns.bus.link_id = md->link_id;
 
 	sdw_cdns_probe(&sdw->cdns);
 
@@ -1107,16 +1105,52 @@ static int intel_probe(struct platform_device *pdev)
 	sdw_intel_ops.read_prop = intel_prop_read;
 	sdw->cdns.bus.ops = &sdw_intel_ops;
 
-	platform_set_drvdata(pdev, sdw);
+	md->pdata = sdw;
+
+	/* set driver data, accessed by snd_soc_dai_set_drvdata() */
+	dev_set_drvdata(&md->dev, &sdw->cdns);
 
 	ret = sdw_add_bus_master(&sdw->cdns.bus);
 	if (ret) {
-		dev_err(&pdev->dev, "sdw_add_bus_master fail: %d\n", ret);
+		dev_err(&md->dev, "sdw_add_bus_master fail: %d\n", ret);
 		return ret;
 	}
 
 	if (sdw->cdns.bus.prop.hw_disabled) {
-		dev_info(&pdev->dev, "SoundWire master %d is disabled, ignoring\n",
+		dev_info(&md->dev, "SoundWire master %d is disabled, ignoring\n",
+			 sdw->cdns.bus.link_id);
+		return 0;
+	}
+
+	/* Acquire IRQ */
+	ret = request_threaded_irq(sdw->link_res->irq,
+				   sdw_cdns_irq, sdw_cdns_thread,
+				   IRQF_SHARED, KBUILD_MODNAME, &sdw->cdns);
+	if (ret < 0) {
+		dev_err(sdw->cdns.dev, "unable to grab IRQ %d, disabling device\n",
+			sdw->link_res->irq);
+		goto err_init;
+	}
+
+	complete(&md->probe_complete);
+
+	return 0;
+
+err_init:
+	sdw_delete_bus_master(&sdw->cdns.bus);
+	return ret;
+}
+
+static int intel_master_startup(struct sdw_master_device *md)
+{
+	struct sdw_cdns_stream_config config;
+	struct sdw_intel *sdw;
+	int ret;
+
+	sdw = md->pdata;
+
+	if (sdw->cdns.bus.prop.hw_disabled) {
+		dev_info(&md->dev, "SoundWire master %d is disabled, ignoring\n",
 			 sdw->cdns.bus.link_id);
 		return 0;
 	}
@@ -1134,16 +1168,6 @@ static int intel_probe(struct platform_device *pdev)
 
 	intel_pdi_ch_update(sdw);
 
-	/* Acquire IRQ */
-	ret = request_threaded_irq(sdw->link_res->irq,
-				   sdw_cdns_irq, sdw_cdns_thread,
-				   IRQF_SHARED, KBUILD_MODNAME, &sdw->cdns);
-	if (ret < 0) {
-		dev_err(sdw->cdns.dev, "unable to grab IRQ %d, disabling device\n",
-			sdw->link_res->irq);
-		goto err_init;
-	}
-
 	ret = sdw_cdns_enable_interrupt(&sdw->cdns, true);
 	if (ret < 0) {
 		dev_err(sdw->cdns.dev, "cannot enable interrupts\n");
@@ -1170,17 +1194,17 @@ static int intel_probe(struct platform_device *pdev)
 
 err_interrupt:
 	sdw_cdns_enable_interrupt(&sdw->cdns, false);
-	free_irq(sdw->link_res->irq, sdw);
 err_init:
+	free_irq(sdw->link_res->irq, sdw);
 	sdw_delete_bus_master(&sdw->cdns.bus);
 	return ret;
 }
 
-static int intel_remove(struct platform_device *pdev)
+static int intel_master_remove(struct sdw_master_device *md)
 {
 	struct sdw_intel *sdw;
 
-	sdw = platform_get_drvdata(pdev);
+	sdw = md->pdata;
 
 	if (!sdw->cdns.bus.prop.hw_disabled) {
 		intel_debugfs_exit(sdw);
@@ -1193,17 +1217,19 @@ static int intel_remove(struct platform_device *pdev)
 	return 0;
 }
 
-static struct platform_driver sdw_intel_drv = {
-	.probe = intel_probe,
-	.remove = intel_remove,
-	.driver = {
-		.name = "int-sdw",
 
+struct sdw_master_driver intel_sdw_driver = {
+	.driver = {
+		.name = "intel-master",
+		.owner = THIS_MODULE,
+		.bus = &sdw_bus_type,
 	},
+	.probe = intel_master_probe,
+	.startup = intel_master_startup,
+	.remove = intel_master_remove,
 };
-
-module_platform_driver(sdw_intel_drv);
+module_sdw_master_driver(intel_sdw_driver);
 
 MODULE_LICENSE("Dual BSD/GPL");
-MODULE_ALIAS("platform:int-sdw");
+MODULE_ALIAS("sdw:intel-master");
 MODULE_DESCRIPTION("Intel Soundwire Master Driver");
drivers/soundwire/intel.h +6 -2
index 38b7c125fb10..795d6213eba5 100644
@@ -7,7 +7,7 @@
 /**
  * struct sdw_intel_link_res - Soundwire Intel link resource structure,
  * typically populated by the controller driver.
- * @pdev: platform_device
+ * @md: master device
  * @mmio_base: mmio base of SoundWire registers
  * @registers: Link IO registers base
  * @shim: Audio shim pointer
@@ -17,7 +17,7 @@
  * @dev: device implementing hw_params and free callbacks
  */
 struct sdw_intel_link_res {
-	struct platform_device *pdev;
+	struct sdw_master_device *md;
 	void __iomem *mmio_base; /* not strictly needed, useful for debug */
 	void __iomem *registers;
 	void __iomem *shim;
@@ -27,4 +27,8 @@ struct sdw_intel_link_res {
 	struct device *dev;
 };
 
+#define SDW_INTEL_QUIRK_MASK_BUS_DISABLE      BIT(1)
+
+#define SDW_INTEL_MASTER_PROBE_TIMEOUT 2000
+
 #endif /* __SDW_INTEL_LOCAL_H */
drivers/soundwire/intel_init.c +218 -65

402 lines not on this page · raw message

Signature · 1 line
2.20.1
Reviewed-by on the v1 series
#3 lore raw
In reply to Pierre-Louis Bossart
Quoted 19 lines · Pierre-Louis Bossart

The SoundWire master representation needs to evolve to take into account:

a) a May 2019 recommendation from Greg KH to remove platform devices

b) the need on Intel platforms to support hardware startup only once the power rail dependencies are settled. The SoundWire master is not an independent IP at all.

c) the need to deal with external wakes handled by the PCI subsystem in specific low-power modes

In case it wasn't clear, the SoundWire subsystem is currently unusable with v5.5 on devices hitting the shelves very soon (race conditions, power management, suspend/resume, etc). v5.6 will only provide interface changes and no functional improvement. We've circled on the same concepts since September 2019, and I hope this direction is now aligned with the suggestions from Vinod Koul and that we can target v5.7 as the 'SoundWire just works(tm)' version.

Quoted 2 lines

This series is provided as an RFC since it depends on patches already for review.

Both of these look sane to me, nice work!

#4 lore raw
Quoted 20 lines · Greg KH
Quoted 19 lines · Pierre-Louis Bossart

The SoundWire master representation needs to evolve to take into account:

a) a May 2019 recommendation from Greg KH to remove platform devices

b) the need on Intel platforms to support hardware startup only once the power rail dependencies are settled. The SoundWire master is not an independent IP at all.

c) the need to deal with external wakes handled by the PCI subsystem in specific low-power modes

In case it wasn't clear, the SoundWire subsystem is currently unusable with v5.5 on devices hitting the shelves very soon (race conditions, power management, suspend/resume, etc). v5.6 will only provide interface changes and no functional improvement. We've circled on the same concepts since September 2019, and I hope this direction is now aligned with the suggestions from Vinod Koul and that we can target v5.7 as the 'SoundWire just works(tm)' version.

Quoted 6 lines
Quoted 2 lines

This series is provided as an RFC since it depends on patches already for review.

Both of these look sane to me, nice work!

Thanks Greg, I really appreciate the review and educational guidance on the use of platform devices!

If that's alright with Vinod and you, I'd like to defer the integration of these patches a bit so that we can improve them a bit e.g.

- I realized earlier this week there's a potential issue with the 'shutdown' callback, what I did looks duplicated with 'shutdown' called from the device core and by the parent.

- it would make sense to add support for this 'master device' in the Qualcomm drivers as well, so that we are aligned with a similar device hierarchy (PCI -> sdw_master_device and DT/plaforms ->sdw_master_device).

- we probably want to add the sysfs patches that started this discussion on the platform devices. This would be very useful to debug new platforms, we depend on BIOS/firmware and we can't always know the actual settings just by looking at the DSDT static contents (multiple tests and overridden values).

My preference in terms of integration in drivers/soundwire would be

1. Intel DAI cleanup (only one kfree missing, will resubmit v3 today)

2. [PATCH 00/10] soundwire: bus: fix race conditions, add suspend-resume this series solves a lot of issues and should go first.

3. ASoC/SOF integration (still with platform devices)
I narrowed this down to 6 patches, that would help me submit the rest of the ASoC/SOF patches in Mark Brown's tree. That would be Intel specific. This step would be the first where everything SoundWire-related can be enabled in a build, and while we've caught a lot of cross-compilation issues it's likely some bots will find corner cases to keep us busy.

4. master_device/driver transition: these updated patches removing platform devices + sysfs support + Qualcomm support (the last point would depend on the workload/support of Qualcomm/Linaro folks, I don't want to commit on their behalf).

5. New SoundWire functionality for Intel platforms (clock-stop/restart and synchronized links). The code would be only located in drivers/soundwire and be easier to handle. For the synchronized links we still have a bit of validation work to do so it should really come last.

Would this work for everyone?

Thanks,
-Pierre

#5 lore raw
Quoted 20 lines · Pierre-Louis Bossart

My preference in terms of integration in drivers/soundwire would be

1. Intel DAI cleanup (only one kfree missing, will resubmit v3 today)

2. [PATCH 00/10] soundwire: bus: fix race conditions, add suspend-resume this series solves a lot of issues and should go first.

3. ASoC/SOF integration (still with platform devices)
I narrowed this down to 6 patches, that would help me submit the rest of the ASoC/SOF patches in Mark Brown's tree. That would be Intel specific. This step would be the first where everything SoundWire-related can be enabled in a build, and while we've caught a lot of cross-compilation issues it's likely some bots will find corner cases to keep us busy.

4. master_device/driver transition: these updated patches removing platform devices + sysfs support + Qualcomm support (the last point would depend on the workload/support of Qualcomm/Linaro folks, I don't want to commit on their behalf).

Quoted 6 lines

5. New SoundWire functionality for Intel platforms (clock-stop/restart and synchronized links). The code would be only located in drivers/soundwire and be easier to handle. For the synchronized links we still have a bit of validation work to do so it should really come last.

Would this work for everyone?

Sounds reasonable to me, but patches would show it best to see if there are any issues :)

thanks,

greg k-h