[PATCH] spi: Fix the logic around the maximum speed

Miquel Raynal
2021-02-03T17:05:09Z

Commit 9326e4f1e5dd ("spi: Limit the spi device max speed to controller's max speed")
rationaly introduced a new condition on which the device's maximum
speed would be set to the controller's one: if the device advertizes a
higher speed than the one supported by the controller.

However, it seems that the SPI core does not enforce controllers to
always advertize a maximum speed value. Other similar conditions in
the SPI core always check that the controller->max_speed_hz was set
before doing anything else.

Not doing this check here breaks SPI controller drivers which do not
advertize a maximum speed:
- the controller maximum speed is 0
- the child device max speed is set to an apparently coeherent value
- in this case the child device maximum speed is set to 0

Either (1) all controller drivers should set max_speed_hz or (2) the
child device maximum speed should not be updated in this particular
situation. Perhaps there is a rationale for not enforcing (1), but in
any case as a fix it is safer to use solution (2).

Fixes: 9326e4f1e5dd ("spi: Limit the spi device max speed to controller's max speed")
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
drivers/spi/spi.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 51d7c004fbab..f59bf5094adb 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -3378,8 +3378,9 @@ int spi_setup(struct spi_device *spi)
if (status)
return status;

- if (!spi->max_speed_hz ||
- spi->max_speed_hz > spi->controller->max_speed_hz)
+ if (spi->controller->max_speed_hz &&
+ (!spi->max_speed_hz ||
+ spi->max_speed_hz > spi->controller->max_speed_hz))
spi->max_speed_hz = spi->controller->max_speed_hz;

mutex_lock(&spi->controller->io_mutex);
--
2.20.1