Commit 2e19ba66 authored by Fabrice Gasnier's avatar Fabrice Gasnier Committed by Jonathan Cameron

iio: adc: stm32-dfsdm: make spi_master_freq more accurate

Current ckout divider may be set to a value that makes ckout to exceed
spi-max-frequency. Rather use lower value (e.g. round up divider when
ckout isn't accurate).

Also when the SPI clock isn't accurate, 'spi_master_freq' is filled in
with expected frequency. Use computed value instead to be more accurate:
- e.g. source clock / (CKOUTDIV + 1)

Enforce checks on the divider: ckoutdiv range can be from 1-255 to provide
divider of 2-256.
Signed-off-by: default avatarFabrice Gasnier <fabrice.gasnier@st.com>
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 57565828
...@@ -199,7 +199,7 @@ static int stm32_dfsdm_parse_of(struct platform_device *pdev, ...@@ -199,7 +199,7 @@ static int stm32_dfsdm_parse_of(struct platform_device *pdev,
{ {
struct device_node *node = pdev->dev.of_node; struct device_node *node = pdev->dev.of_node;
struct resource *res; struct resource *res;
unsigned long clk_freq; unsigned long clk_freq, divider;
unsigned int spi_freq, rem; unsigned int spi_freq, rem;
int ret; int ret;
...@@ -243,13 +243,20 @@ static int stm32_dfsdm_parse_of(struct platform_device *pdev, ...@@ -243,13 +243,20 @@ static int stm32_dfsdm_parse_of(struct platform_device *pdev,
return 0; return 0;
} }
priv->spi_clk_out_div = div_u64_rem(clk_freq, spi_freq, &rem) - 1; divider = div_u64_rem(clk_freq, spi_freq, &rem);
if (!priv->spi_clk_out_div) { /* Round up divider when ckout isn't precise, not to exceed spi_freq */
/* spi_clk_out_div == 0 means ckout is OFF */ if (rem)
divider++;
/* programmable divider is in range of [2:256] */
if (divider < 2 || divider > 256) {
dev_err(&pdev->dev, "spi-max-frequency not achievable\n"); dev_err(&pdev->dev, "spi-max-frequency not achievable\n");
return -EINVAL; return -EINVAL;
} }
priv->dfsdm.spi_master_freq = spi_freq;
/* SPI clock output divider is: divider = CKOUTDIV + 1 */
priv->spi_clk_out_div = divider - 1;
priv->dfsdm.spi_master_freq = clk_freq / (priv->spi_clk_out_div + 1);
if (rem) { if (rem) {
dev_warn(&pdev->dev, "SPI clock not accurate\n"); dev_warn(&pdev->dev, "SPI clock not accurate\n");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment