From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andy Shevchenko Subject: Re: [PATCH] spi: dw: Fix dynamic speed change Date: Wed, 05 Nov 2014 11:57:03 +0200 Message-ID: <1415181423.472.15.camel@linux.intel.com> References: <1415140617-2028-1-git-send-email-tthayer@opensource.altera.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, feng.tang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org, setka-3PjVBYxTQDg@public.gmane.org, tthayer-EIB2kfCEclfQT0dZR+AlfA@public.gmane.org To: tthayer-yzvPICuk2ABMcg4IHK0kFoH6Mc4MB0Vx@public.gmane.org Return-path: In-Reply-To: <1415140617-2028-1-git-send-email-tthayer-yzvPICuk2ABMcg4IHK0kFoH6Mc4MB0Vx@public.gmane.org> Sender: linux-spi-owner-u79uwXL29TY76Z2rM5mHXA@public.gmane.org List-ID: On Tue, 2014-11-04 at 16:36 -0600, tthayer-yzvPICuk2ABMcg4IHK0kFoH6Mc4MB0Vx@public.gmane.org wrote: > From: Thor Thayer >=20 > Changing SPI transfer speed using a utility such as spi_config with > spidev updates chip->speed_hz which is compared to the next transfer > speed. >=20 > When speed_hz is not declared in a SPI transfer, the transfer speed i= s > not updated for the next read/write on /dev/spidevX.Y. The element > spi_transfer->speed_hz is filled with spi->max_speed_hz. The test of > if (transfer->speed_hz !=3D speed) doesn't work because the chip->spe= ed_hz > matches transfer->speed_hz and the clock divider is not updated. >=20 > This fix: On each transfer update the clock divider, compare to the > previous clock divider and update if necessary. This fixes another > bug where the clock divider calculation at the top of the > pump_transfers() function could be an odd-number. >=20 My intention is to use SPI core API as much as possible. Thus, pump_transfers() I think should be gone in future. Instead of doing an additional work can you provide a helper function to set speed_hz and call it from pump_transfers()? > Reported-by: Vlastimil Setka > Signed-off-by: Vlastimil Setka > Signed-off-by: Thor Thayer > --- > drivers/spi/spi-dw.c | 33 ++++++++++++++++++--------------- > 1 file changed, 18 insertions(+), 15 deletions(-) >=20 > diff --git a/drivers/spi/spi-dw.c b/drivers/spi/spi-dw.c > index 72e12ba..3456b34 100644 > --- a/drivers/spi/spi-dw.c > +++ b/drivers/spi/spi-dw.c > @@ -376,9 +376,6 @@ static void pump_transfers(unsigned long data) > chip =3D dws->cur_chip; > spi =3D message->spi; > =20 > - if (unlikely(!chip->clk_div)) > - chip->clk_div =3D dws->max_freq / chip->speed_hz; > - > if (message->state =3D=3D ERROR_STATE) { > message->status =3D -EIO; > goto early_exit; > @@ -415,21 +412,27 @@ static void pump_transfers(unsigned long data) > =20 > cr0 =3D chip->cr0; > =20 > - /* Handle per transfer options for bpw and speed */ > - if (transfer->speed_hz) { > - speed =3D chip->speed_hz; > + /* Always calculate the desired clock divider */ > + speed =3D transfer->speed_hz ? transfer->speed_hz : chip->speed_hz; > + > + if (speed > dws->max_freq) { > + dev_err(&spi->dev, "Unsupported SPI freq: %d Hz\n", speed); > + message->status =3D -EIO; > + goto early_exit; > + } > + > + /* clk_div doesn't support odd number */ > + clk_div =3D dws->max_freq / speed; > + clk_div =3D (clk_div + 1) & 0xfffe; > =20 > - if (transfer->speed_hz !=3D speed) { > - speed =3D transfer->speed_hz; > + /* Determine if the clock divider changed, if so update chip struct= */ Maybe "=E2=80=A6update speed_hz" ? > + if (clk_div !=3D chip->clk_div) > + chip->clk_div =3D clk_div; > + else > + clk_div =3D 0; /* Prevent register programming below */ > =20 > - /* clk_div doesn't support odd number */ > - clk_div =3D dws->max_freq / speed; > - clk_div =3D (clk_div + 1) & 0xfffe; > + chip->speed_hz =3D speed; > =20 > - chip->speed_hz =3D speed; > - chip->clk_div =3D clk_div; > - } > - } > if (transfer->bits_per_word) { > bits =3D transfer->bits_per_word; > dws->n_bytes =3D dws->dma_width =3D bits >> 3; --=20 Andy Shevchenko Intel Finland Oy -- To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org More majordomo info at http://vger.kernel.org/majordomo-info.html