Thanks Richard for the patch, very interesting.
sdw_prep_deprep_slave_ports() cannot use the port interrupt to signal
CP_SM completion because it is called while holding the bus lock, which
blocks the alert handler from running.
Change to polling the PREPARESTATUS register and remove the
infrastructure that was implemented for the previous interrupt waiting.
I am afraid the entire PORT_READY support is completely untested at the moment: all the existing codecs use the simpler state machine, e.g.
dpn[i].simple_ch_prep_sm = true;
So my main question is: how did you test this change? Is this on a platform already supported upstream? yes/no answer is fine, no details necessary.
I am also not fully clear on the locking issue.
Is the problem that sdw_handle_slave_status() uses a mutex_lock(&bus->bus_lock), which is already held in sdw_prepare_stream
so dealing with an ALERT status while doing pre-deprep would cause a self-inflicted deadlock?
If yes, that's a more general problem that would need to be fixed. this lock is taken in ALL stream operations, not just prepare/deprepare.
If e.g. a jack detection was signaled during any stream reconfiguration, we would also not be able to deal with the information, would we?
the purpose of the lock in sdw_handle_slave_status() was to check if devices attach or fall-off the bus before handling their status. The command/control protocol is always operational so nothing prevents the interrupt from being handled.
There's also something odd about directly polling on the status bits. The status bits will be used to signal the ALERT condition which is transmitted to the host during PING frames. This solution would result in the host noticing an interrupt: host controllers typically have a sticky status where all changes in PING frames are detected and used as a trigger for interrupt processing. In this case the interrupt would still be handled, but the sdw_handle_slave_status() would be deferred until the stream prepare is complete, and at that point the interrupt processing would not see any sources. It's not illegal but it's a bit odd to cause empty interrupts to be handled.
It might be a better solution to fix conflicts between stream reconfiguration and interrupts. I don't have a turn-key proposal but the suggested patch feels like a work-around.
maybe using mutex_is_locked()?
If we can't figure something out, then at least the PORT_READY mask should not be set, i.e. this code would need to be removed:
...
A new configuration field 'ch_prep_poll_us' is added to struct
sdw_dpn_prop so that the peripheral driver may select a polling interval.
If this is left at zero a default interval of 500 usec is used.
we already have a 'ch_prep_timeout' that's defined, do you need to redefine a new field?
why not just read once at the end of that timeout? It's a prepare operation, there's no requirement to be fast, is there?