Add SoundWire detach helper
Subsystems (4)
v3 cover Add SoundWire detach helper
Currently, if a SoundWire driver takes an action that causes the device to detach from the bus it needs to be careful. There will be a period of time until the detach is recognised by the core, if the driver immediately calls sdw_slave_wait_for_init() it is possible for that to complete before the detach has been recognised. Leaving the driver with a device that is potentially not ready for what follows.
To avoid this race add a new helper that drivers can use to inform the core the device will detach. This clears the enumeration completions ensuring that sdw_slave_wait_for_init() can't complete until the device re-attaches.
This is the third part of a longer series of moving these slave attach/deatach operations into the core:
https://lore.kernel.org/linux-sound/[email protected]/
https://lore.kernel.org/linux-sound/[email protected]/
Changes since v2:
- Updated comment on cs42l43_soft_reset()
Changes since v1:
- Correct kerneldoc
Charles Keepax (2):
soundwire: Add a helper function to indicate a device will detach
mfd: cs42l43: Move to core detach helper
drivers/mfd/cs42l43-sdw.c | 2 -- drivers/mfd/cs42l43.c | 21 ++++----------------- drivers/soundwire/bus.h | 6 ------ include/linux/mfd/cs42l43.h | 1 - include/linux/soundwire/sdw.h | 26 ++++++++++++++++++++++++++ 5 files changed, 30 insertions(+), 26 deletions(-)
Signature · 1 line
2.47.3
PATCH v3 1/2 soundwire: Add a helper function to indicate a device will detach
SoundWire devices will sometimes need to reset themselves, typical times might include when taking over the device to ensure a consistent state or after downloading a firmware update to boot into the new version. Often such a reset will also result in the device detaching from the SoundWire bus, in this case the driver has to be careful. Naively calling sdw_slave_wait_for_init() after doing the reset could immediately succeed if the bus hasn't seen the detach yet, causing the driver to proceed attempting to communicate with the device whilst it is detached.
Add a helper function that a driver can call to indicate it is about to initiate an action that will cause the device to detach from the bus. This will reinit the completions in the peripheral for device attach, ensuring that the next sdw_slave_wait_for_init() will not complete before the detach has been seen by the core.
---
No changes since v2.
drivers/soundwire/bus.h | 6 ------ include/linux/soundwire/sdw.h | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-)
drivers/soundwire/bus.h +0 -6
@@ -231,12 +231,6 @@ static inline void sdw_fill_port_params(struct sdw_port_params *params,int sdw_bread_no_pm_unlocked (struct sdw_bus *bus,u16 dev_num,u32 addr);int sdw_bwrite_no_pm_unlocked (struct sdw_bus *bus,u16 dev_num,u32 addr,u8 value); -/* -* At the moment we only track Master-initiated hw_reset. -* Additional fields can be added as needed -*/ -#define SDW_UNATTACH_REQUEST_MASTER_RESET BIT(0) -void sdw_clear_slave_status (struct sdw_bus *bus,u32 request);int sdw_slave_modalias (const struct sdw_slave *slave,char *buf,size_t size); void sdw_compute_slave_ports(struct sdw_master_runtime *m_rt,
include/linux/soundwire/sdw.h +26 -0
@@ -25,6 +25,13 @@ struct device_node;struct sdw_bus ;struct sdw_slave ; +/* +* At the moment we only track Master-initiated/Slave-initiated hw_reset. +* Additional fields can be added as needed +*/ +#define SDW_UNATTACH_REQUEST_MASTER_RESET BIT(0) +#define SDW_UNATTACH_REQUEST_SLAVE_RESET BIT(1) +/* SDW spec defines and enums, as defined by MIPI 1.1. Spec */ /* SDW Broadcast Device Number */
@@ -1218,6 +1225,25 @@ static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u#endif /* CONFIG_SOUNDWIRE */ +/** +* sdw_slave_signal_unattach - Signal that the peripheral is about to detach +* @slave: Pointer to the SoundWire peripheral. +* +* Inform the core that the peripheral is about to detach from the bus, this +* is usually due to some implementation defined reset mechanism. After this +* sdw_slave_wait_for_init() should be called to wait for the device to +* return. +*/ +static inline void sdw_slave_signal_unattach (struct sdw_slave *slave) +{ +if (!slave) +return ; + + slave->unattach_request =SDW_UNATTACH_REQUEST_SLAVE_RESET ; +reinit_completion (&slave->enumeration_complete); +reinit_completion (&slave->initialization_complete); +} +/** * sdw_slave_wait_for_init - Wait for device initialisation * @slave: Pointer to the SoundWire peripheral.
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v3 1/2] soundwire: Add a helper function to indicate a device will detach
Sashiko has reviewed this patch and found no issues.
Quoted 16 lines
SoundWire devices will sometimes need to reset themselves, typical times might include when taking over the device to ensure a consistent state or after downloading a firmware update to boot into the new version. Often such a reset will also result in the device detaching from the SoundWire bus, in this case the driver has to be careful. Naively calling
sdw_slave_wait_for_init()after doing the reset could immediately succeed if the bus hasn't seen the detach yet, causing the driver to proceed attempting to communicate with the device whilst it is detached.Add a helper function that a driver can call to indicate it is about to initiate an action that will cause the device to detach from the bus. This will reinit the completions in the peripheral for device attach, ensuring that the next
sdw_slave_wait_for_init()will not complete before the detach has been seen by the core.
Quoted 3 lines
Reviewed-by Pierre-Louis Bossart <[email protected]>Acked-by Vinod Koul <[email protected]>Signed-off-by Charles Keepax <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 2/2 mfd: cs42l43: Move to core detach helper
In reply to Charles KeepaxNow the core has a helper to signal a device will detach from the SoundWire bus, there is no need for the cs42l43 driver code to separately track that. Switch to using the new core helper.
---
Changes since v2:
- Updated comment on cs42l43_soft_reset()
drivers/mfd/cs42l43-sdw.c | 2 -- drivers/mfd/cs42l43.c | 21 ++++----------------- include/linux/mfd/cs42l43.h | 1 - 3 files changed, 4 insertions(+), 20 deletions(-)
drivers/mfd/cs42l43-sdw.c +0 -2
@@ -102,8 +102,6 @@ static int cs42l43_sdw_update_status(struct sdw_slave *sdw, enum sdw_slave_statubreak ;case SDW_SLAVE_UNATTACHED :dev_dbg (cs42l43->dev,"Device detach\n" ); - -complete (&cs42l43->device_detach);break ;default :break ;
drivers/mfd/cs42l43.c +4 -17
@@ -30,7 +30,6 @@#define CS42L43_RESET_DELAY_MS 20#define CS42L43_SDW_ATTACH_TIMEOUT_MS 5000 -#define CS42L43_SDW_DETACH_TIMEOUT_MS 100#define CS42L43_MCU_BOOT_STAGE1 1#define CS42L43_MCU_BOOT_STAGE2 2
@@ -545,9 +544,9 @@ static const struct mfd_cell cs42l43_devs[] = { };/* -* If the device is connected over Soundwire, as well as soft resetting the -* device, this function will also way for the device to detach from the bus -* before returning. +* If the device is connected over SoundWire, as well as soft resetting the +* device, this function will inform the SoundWire core the device is about +* to detach from the bus. */ static int cs42l43_soft_reset (struct cs42l43 *cs42l43) {
@@ -555,7 +554,7 @@ static int cs42l43_soft_reset(struct cs42l43 *cs42l43) { CS42L43_SFT_RESET, CS42L43_SFT_RESET_VAL }, }; -reinit_completion (&cs42l43->device_detach); +sdw_slave_signal_unattach (cs42l43->sdw);/* * Apply cache only because the soft reset will cause the device to
@@ -566,17 +565,6 @@ static int cs42l43_soft_reset(struct cs42l43 *cs42l43)msleep (CS42L43_RESET_DELAY_MS ); -if (cs42l43->sdw) { -unsigned long timeout =msecs_to_jiffies (CS42L43_SDW_DETACH_TIMEOUT_MS ); -unsigned long time; - - time =wait_for_completion_timeout (&cs42l43->device_detach, timeout); -if (!time) { -dev_err (cs42l43->dev,"Timed out waiting for device detach\n" ); -return -ETIMEDOUT ; - } - } -return -EAGAIN ; }
@@ -1125,7 +1113,6 @@ int cs42l43_dev_probe(struct cs42l43 *cs42l43)dev_set_drvdata (cs42l43->dev, cs42l43);mutex_init (&cs42l43->pll_lock); -init_completion (&cs42l43->device_detach);init_completion (&cs42l43->firmware_download);INIT_WORK (&cs42l43->boot_work, cs42l43_boot_work);
include/linux/mfd/cs42l43.h +0 -1
@@ -86,7 +86,6 @@ struct cs42l43 {struct regmap_irq_chip_data *irq_data;struct work_struct boot_work; -struct completion device_detach;struct completion firmware_download;int firmware_error;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v3 2/2] mfd: cs42l43: Move to core detach helper
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
Now the core has a helper to signal a device will detach from the SoundWire bus, there is no need for the cs42l43 driver code to separately track that. Switch to using the new core helper.
Reviewed-by Pierre-Louis Bossart <[email protected]>Signed-off-by Charles Keepax <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2