Skip to content

Commit e0fe700

Browse files
geertubroonie
authored andcommitted
spi: rspi: Use requested instead of maximum bit rate
Currently, the RSPI driver always tries to use the maximum configured bit rate for communicating with a slave device, even if the transfer(s) in the current message specify a lower rate. Use the mininum rate specified in the message instead. Rename rspi_data.max_speed_hz accordingly. Signed-off-by: Geert Uytterhoeven <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 9ec8ade commit e0fe700

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

drivers/spi/spi-rspi.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179

180180
struct rspi_data {
181181
void __iomem *addr;
182-
u32 max_speed_hz;
182+
u32 speed_hz;
183183
struct spi_controller *ctlr;
184184
struct platform_device *pdev;
185185
wait_queue_head_t wait;
@@ -258,8 +258,7 @@ static int rspi_set_config_register(struct rspi_data *rspi, int access_size)
258258
rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
259259

260260
/* Sets transfer bit rate */
261-
spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk),
262-
2 * rspi->max_speed_hz) - 1;
261+
spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->speed_hz) - 1;
263262
rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
264263

265264
/* Disable dummy transmission, set 16-bit word access, 1 frame */
@@ -299,14 +298,14 @@ static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size)
299298

300299
clksrc = clk_get_rate(rspi->clk);
301300
while (div < 3) {
302-
if (rspi->max_speed_hz >= clksrc/4) /* 4=(CLK/2)/2 */
301+
if (rspi->speed_hz >= clksrc/4) /* 4=(CLK/2)/2 */
303302
break;
304303
div++;
305304
clksrc /= 2;
306305
}
307306

308307
/* Sets transfer bit rate */
309-
spbr = DIV_ROUND_UP(clksrc, 2 * rspi->max_speed_hz) - 1;
308+
spbr = DIV_ROUND_UP(clksrc, 2 * rspi->speed_hz) - 1;
310309
rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
311310
rspi->spcmd |= div << 2;
312311

@@ -341,7 +340,7 @@ static int qspi_set_config_register(struct rspi_data *rspi, int access_size)
341340
rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
342341

343342
/* Sets transfer bit rate */
344-
spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->max_speed_hz);
343+
spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->speed_hz);
345344
rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
346345

347346
/* Disable dummy transmission, set byte access */
@@ -949,9 +948,24 @@ static int rspi_prepare_message(struct spi_controller *ctlr,
949948
{
950949
struct rspi_data *rspi = spi_controller_get_devdata(ctlr);
951950
struct spi_device *spi = msg->spi;
951+
const struct spi_transfer *xfer;
952952
int ret;
953953

954-
rspi->max_speed_hz = spi->max_speed_hz;
954+
/*
955+
* As the Bit Rate Register must not be changed while the device is
956+
* active, all transfers in a message must use the same bit rate.
957+
* In theory, the sequencer could be enabled, and each Command Register
958+
* could divide the base bit rate by a different value.
959+
* However, most RSPI variants do not have Transfer Data Length
960+
* Multiplier Setting Registers, so each sequence step would be limited
961+
* to a single word, making this feature unsuitable for large
962+
* transfers, which would gain most from it.
963+
*/
964+
rspi->speed_hz = spi->max_speed_hz;
965+
list_for_each_entry(xfer, &msg->transfers, transfer_list) {
966+
if (xfer->speed_hz < rspi->speed_hz)
967+
rspi->speed_hz = xfer->speed_hz;
968+
}
955969

956970
rspi->spcmd = SPCMD_SSLKP;
957971
if (spi->mode & SPI_CPOL)

0 commit comments

Comments
 (0)