Skip to content

Commit 0c2a2ae

Browse files
tkoellerglikely
authored andcommitted
spi/davinci: Fix clock prescale factor computation
Computation of the clock prescaler value returned bogus results if the requested SPI clock was impossible to set. It now sets either the maximum or minimum clock frequency, as appropriate. Signed-off-by: Thomas Koeller <[email protected]> Signed-off-by: Grant Likely <[email protected]>
1 parent 41c4221 commit 0c2a2ae

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

drivers/spi/davinci_spi.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static int davinci_spi_setup_transfer(struct spi_device *spi,
301301
struct davinci_spi *davinci_spi;
302302
struct davinci_spi_platform_data *pdata;
303303
u8 bits_per_word = 0;
304-
u32 hz = 0, prescale;
304+
u32 hz = 0, prescale = 0, clkspeed;
305305

306306
davinci_spi = spi_master_get_devdata(spi->master);
307307
pdata = davinci_spi->pdata;
@@ -338,10 +338,16 @@ static int davinci_spi_setup_transfer(struct spi_device *spi,
338338
set_fmt_bits(davinci_spi->base, bits_per_word & 0x1f,
339339
spi->chip_select);
340340

341-
prescale = ((clk_get_rate(davinci_spi->clk) / hz) - 1) & 0xff;
341+
clkspeed = clk_get_rate(davinci_spi->clk);
342+
if (hz > clkspeed / 2)
343+
prescale = 1 << 8;
344+
if (hz < clkspeed / 256)
345+
prescale = 255 << 8;
346+
if (!prescale)
347+
prescale = ((clkspeed / hz - 1) << 8) & 0x0000ff00;
342348

343349
clear_fmt_bits(davinci_spi->base, 0x0000ff00, spi->chip_select);
344-
set_fmt_bits(davinci_spi->base, prescale << 8, spi->chip_select);
350+
set_fmt_bits(davinci_spi->base, prescale, spi->chip_select);
345351

346352
return 0;
347353
}

0 commit comments

Comments
 (0)