linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements
@ 2024-09-30  9:30 Stefan Wahren
  2024-09-30  9:30 ` [PATCH 1/3] spi: spi-fsl-lpspi: Adjust type of scldiv Stefan Wahren
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Stefan Wahren @ 2024-09-30  9:30 UTC (permalink / raw)
  To: Mark Brown, Carlos Song, Frank Li, Fabio Estevam, Shawn Guo
  Cc: linux-arm-kernel, linux-spi, imx @ lists . linux . dev,
	Pengutronix Kernel Team, Stefan Wahren

This series contains some improvements for spi-fsl-lpspi which are
related to the calculations within fsl_lpspi_set_bitrate.

Stefan Wahren (3):
  spi: spi-fsl-lpspi: Adjust type of scldiv
  spi: spi-fsl-lpspi: Fix specifiers in fsl_lpspi_set_bitrate
  spi: spi-fsl-lpspi: support effective_speed_hz

 drivers/spi/spi-fsl-lpspi.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

--
2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/3] spi: spi-fsl-lpspi: Adjust type of scldiv
  2024-09-30  9:30 [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements Stefan Wahren
@ 2024-09-30  9:30 ` Stefan Wahren
  2024-09-30 15:18   ` Frank Li
  2024-09-30  9:30 ` [PATCH 2/3] spi: spi-fsl-lpspi: Fix specifiers in fsl_lpspi_set_bitrate Stefan Wahren
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Stefan Wahren @ 2024-09-30  9:30 UTC (permalink / raw)
  To: Mark Brown, Carlos Song, Frank Li, Fabio Estevam, Shawn Guo
  Cc: linux-arm-kernel, linux-spi, imx @ lists . linux . dev,
	Pengutronix Kernel Team, Stefan Wahren

The target value of scldiv is just a byte, but its calculation in
fsl_lpspi_set_bitrate could be negative. So use an adequate type to store
the result and avoid overflows. After that this needs range check
adjustments, but this should make the code less opaque.

Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
---
 drivers/spi/spi-fsl-lpspi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c
index 977e8b55c82b..196cc68f2057 100644
--- a/drivers/spi/spi-fsl-lpspi.c
+++ b/drivers/spi/spi-fsl-lpspi.c
@@ -315,9 +315,10 @@ static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi)
 static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
 {
 	struct lpspi_config config = fsl_lpspi->config;
-	unsigned int perclk_rate, scldiv, div;
+	unsigned int perclk_rate, div;
 	u8 prescale_max;
 	u8 prescale;
+	int scldiv;

 	perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
 	prescale_max = fsl_lpspi->devtype_data->prescale_max;
@@ -338,13 +339,13 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)

 	for (prescale = 0; prescale <= prescale_max; prescale++) {
 		scldiv = div / (1 << prescale) - 2;
-		if (scldiv < 256) {
+		if (scldiv >= 0 && scldiv < 256) {
 			fsl_lpspi->config.prescale = prescale;
 			break;
 		}
 	}

-	if (scldiv >= 256)
+	if (scldiv < 0 || scldiv >= 256)
 		return -EINVAL;

 	writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),
--
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/3] spi: spi-fsl-lpspi: Fix specifiers in fsl_lpspi_set_bitrate
  2024-09-30  9:30 [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements Stefan Wahren
  2024-09-30  9:30 ` [PATCH 1/3] spi: spi-fsl-lpspi: Adjust type of scldiv Stefan Wahren
@ 2024-09-30  9:30 ` Stefan Wahren
  2024-09-30 15:19   ` Frank Li
  2024-09-30  9:30 ` [PATCH 3/3] spi: spi-fsl-lpspi: support effective_speed_hz Stefan Wahren
  2024-10-01 17:46 ` [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements Mark Brown
  3 siblings, 1 reply; 8+ messages in thread
From: Stefan Wahren @ 2024-09-30  9:30 UTC (permalink / raw)
  To: Mark Brown, Carlos Song, Frank Li, Fabio Estevam, Shawn Guo
  Cc: linux-arm-kernel, linux-spi, imx @ lists . linux . dev,
	Pengutronix Kernel Team, Stefan Wahren

Most of the parameters are unsigned, so fix the used format
specifiers in the debug message in fsl_lpspi_set_bitrate.

Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
---
 drivers/spi/spi-fsl-lpspi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c
index 196cc68f2057..3b5aa91079ae 100644
--- a/drivers/spi/spi-fsl-lpspi.c
+++ b/drivers/spi/spi-fsl-lpspi.c
@@ -351,7 +351,7 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
 	writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),
 					fsl_lpspi->base + IMX7ULP_CCR);

-	dev_dbg(fsl_lpspi->dev, "perclk=%d, speed=%d, prescale=%d, scldiv=%d\n",
+	dev_dbg(fsl_lpspi->dev, "perclk=%u, speed=%u, prescale=%u, scldiv=%d\n",
 		perclk_rate, config.speed_hz, prescale, scldiv);

 	return 0;
--
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/3] spi: spi-fsl-lpspi: support effective_speed_hz
  2024-09-30  9:30 [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements Stefan Wahren
  2024-09-30  9:30 ` [PATCH 1/3] spi: spi-fsl-lpspi: Adjust type of scldiv Stefan Wahren
  2024-09-30  9:30 ` [PATCH 2/3] spi: spi-fsl-lpspi: Fix specifiers in fsl_lpspi_set_bitrate Stefan Wahren
@ 2024-09-30  9:30 ` Stefan Wahren
  2024-09-30 15:21   ` Frank Li
  2024-10-01 17:46 ` [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements Mark Brown
  3 siblings, 1 reply; 8+ messages in thread
From: Stefan Wahren @ 2024-09-30  9:30 UTC (permalink / raw)
  To: Mark Brown, Carlos Song, Frank Li, Fabio Estevam, Shawn Guo
  Cc: linux-arm-kernel, linux-spi, imx @ lists . linux . dev,
	Pengutronix Kernel Team, Stefan Wahren

Setting spi_transfer->effective_speed_hz in transfer_one so that
SPI client driver can use it.

Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
---
 drivers/spi/spi-fsl-lpspi.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c
index 3b5aa91079ae..5d55ef4d4ba6 100644
--- a/drivers/spi/spi-fsl-lpspi.c
+++ b/drivers/spi/spi-fsl-lpspi.c
@@ -92,6 +92,7 @@ struct lpspi_config {
 	u8 prescale;
 	u16 mode;
 	u32 speed_hz;
+	u32 effective_speed_hz;
 };

 struct fsl_lpspi_data {
@@ -351,6 +352,9 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
 	writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),
 					fsl_lpspi->base + IMX7ULP_CCR);

+	fsl_lpspi->config.effective_speed_hz = perclk_rate / (scldiv + 2) *
+					       (1 << prescale);
+
 	dev_dbg(fsl_lpspi->dev, "perclk=%u, speed=%u, prescale=%u, scldiv=%d\n",
 		perclk_rate, config.speed_hz, prescale, scldiv);

@@ -750,6 +754,8 @@ static int fsl_lpspi_transfer_one(struct spi_controller *controller,
 	if (ret < 0)
 		return ret;

+	t->effective_speed_hz = fsl_lpspi->config.effective_speed_hz;
+
 	fsl_lpspi_set_cmd(fsl_lpspi);
 	fsl_lpspi->is_first_byte = false;

--
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] spi: spi-fsl-lpspi: Adjust type of scldiv
  2024-09-30  9:30 ` [PATCH 1/3] spi: spi-fsl-lpspi: Adjust type of scldiv Stefan Wahren
@ 2024-09-30 15:18   ` Frank Li
  0 siblings, 0 replies; 8+ messages in thread
From: Frank Li @ 2024-09-30 15:18 UTC (permalink / raw)
  To: Stefan Wahren
  Cc: Mark Brown, Carlos Song, Fabio Estevam, Shawn Guo,
	linux-arm-kernel, linux-spi, imx @ lists . linux . dev,
	Pengutronix Kernel Team

On Mon, Sep 30, 2024 at 11:30:54AM +0200, Stefan Wahren wrote:
> The target value of scldiv is just a byte, but its calculation in
> fsl_lpspi_set_bitrate could be negative. So use an adequate type to store
> the result and avoid overflows. After that this needs range check
> adjustments, but this should make the code less opaque.
>
> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
> ---
Reviewed-by: Frank Li <Frank.Li@nxp.com>
>  drivers/spi/spi-fsl-lpspi.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c
> index 977e8b55c82b..196cc68f2057 100644
> --- a/drivers/spi/spi-fsl-lpspi.c
> +++ b/drivers/spi/spi-fsl-lpspi.c
> @@ -315,9 +315,10 @@ static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi)
>  static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
>  {
>  	struct lpspi_config config = fsl_lpspi->config;
> -	unsigned int perclk_rate, scldiv, div;
> +	unsigned int perclk_rate, div;
>  	u8 prescale_max;
>  	u8 prescale;
> +	int scldiv;
>
>  	perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
>  	prescale_max = fsl_lpspi->devtype_data->prescale_max;
> @@ -338,13 +339,13 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
>
>  	for (prescale = 0; prescale <= prescale_max; prescale++) {
>  		scldiv = div / (1 << prescale) - 2;
> -		if (scldiv < 256) {
> +		if (scldiv >= 0 && scldiv < 256) {
>  			fsl_lpspi->config.prescale = prescale;
>  			break;
>  		}
>  	}
>
> -	if (scldiv >= 256)
> +	if (scldiv < 0 || scldiv >= 256)
>  		return -EINVAL;
>
>  	writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),
> --
> 2.34.1
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] spi: spi-fsl-lpspi: Fix specifiers in fsl_lpspi_set_bitrate
  2024-09-30  9:30 ` [PATCH 2/3] spi: spi-fsl-lpspi: Fix specifiers in fsl_lpspi_set_bitrate Stefan Wahren
@ 2024-09-30 15:19   ` Frank Li
  0 siblings, 0 replies; 8+ messages in thread
From: Frank Li @ 2024-09-30 15:19 UTC (permalink / raw)
  To: Stefan Wahren
  Cc: Mark Brown, Carlos Song, Fabio Estevam, Shawn Guo,
	linux-arm-kernel, linux-spi, imx @ lists . linux . dev,
	Pengutronix Kernel Team

On Mon, Sep 30, 2024 at 11:30:55AM +0200, Stefan Wahren wrote:
> Most of the parameters are unsigned, so fix the used format
> specifiers in the debug message in fsl_lpspi_set_bitrate.
>
> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
Reviewed-by: Frank Li <Frank.Li@nxp.com>
> ---
>  drivers/spi/spi-fsl-lpspi.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c
> index 196cc68f2057..3b5aa91079ae 100644
> --- a/drivers/spi/spi-fsl-lpspi.c
> +++ b/drivers/spi/spi-fsl-lpspi.c
> @@ -351,7 +351,7 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
>  	writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),
>  					fsl_lpspi->base + IMX7ULP_CCR);
>
> -	dev_dbg(fsl_lpspi->dev, "perclk=%d, speed=%d, prescale=%d, scldiv=%d\n",
> +	dev_dbg(fsl_lpspi->dev, "perclk=%u, speed=%u, prescale=%u, scldiv=%d\n",
>  		perclk_rate, config.speed_hz, prescale, scldiv);
>
>  	return 0;
> --
> 2.34.1
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] spi: spi-fsl-lpspi: support effective_speed_hz
  2024-09-30  9:30 ` [PATCH 3/3] spi: spi-fsl-lpspi: support effective_speed_hz Stefan Wahren
@ 2024-09-30 15:21   ` Frank Li
  0 siblings, 0 replies; 8+ messages in thread
From: Frank Li @ 2024-09-30 15:21 UTC (permalink / raw)
  To: Stefan Wahren
  Cc: Mark Brown, Carlos Song, Fabio Estevam, Shawn Guo,
	linux-arm-kernel, linux-spi, imx @ lists . linux . dev,
	Pengutronix Kernel Team

On Mon, Sep 30, 2024 at 11:30:56AM +0200, Stefan Wahren wrote:
> Setting spi_transfer->effective_speed_hz in transfer_one so that
> SPI client driver can use it.
>
> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>

Reviewed-by: Frank Li <Frank.Li@nxp.com>
> ---
>  drivers/spi/spi-fsl-lpspi.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c
> index 3b5aa91079ae..5d55ef4d4ba6 100644
> --- a/drivers/spi/spi-fsl-lpspi.c
> +++ b/drivers/spi/spi-fsl-lpspi.c
> @@ -92,6 +92,7 @@ struct lpspi_config {
>  	u8 prescale;
>  	u16 mode;
>  	u32 speed_hz;
> +	u32 effective_speed_hz;
>  };
>
>  struct fsl_lpspi_data {
> @@ -351,6 +352,9 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
>  	writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),
>  					fsl_lpspi->base + IMX7ULP_CCR);
>
> +	fsl_lpspi->config.effective_speed_hz = perclk_rate / (scldiv + 2) *
> +					       (1 << prescale);
> +
>  	dev_dbg(fsl_lpspi->dev, "perclk=%u, speed=%u, prescale=%u, scldiv=%d\n",
>  		perclk_rate, config.speed_hz, prescale, scldiv);
>
> @@ -750,6 +754,8 @@ static int fsl_lpspi_transfer_one(struct spi_controller *controller,
>  	if (ret < 0)
>  		return ret;
>
> +	t->effective_speed_hz = fsl_lpspi->config.effective_speed_hz;
> +
>  	fsl_lpspi_set_cmd(fsl_lpspi);
>  	fsl_lpspi->is_first_byte = false;
>
> --
> 2.34.1
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements
  2024-09-30  9:30 [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements Stefan Wahren
                   ` (2 preceding siblings ...)
  2024-09-30  9:30 ` [PATCH 3/3] spi: spi-fsl-lpspi: support effective_speed_hz Stefan Wahren
@ 2024-10-01 17:46 ` Mark Brown
  3 siblings, 0 replies; 8+ messages in thread
From: Mark Brown @ 2024-10-01 17:46 UTC (permalink / raw)
  To: Carlos Song, Frank Li, Fabio Estevam, Shawn Guo, Stefan Wahren
  Cc: linux-arm-kernel, linux-spi, imx @ lists . linux . dev,
	Pengutronix Kernel Team

On Mon, 30 Sep 2024 11:30:53 +0200, Stefan Wahren wrote:
> This series contains some improvements for spi-fsl-lpspi which are
> related to the calculations within fsl_lpspi_set_bitrate.
> 
> Stefan Wahren (3):
>   spi: spi-fsl-lpspi: Adjust type of scldiv
>   spi: spi-fsl-lpspi: Fix specifiers in fsl_lpspi_set_bitrate
>   spi: spi-fsl-lpspi: support effective_speed_hz
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/3] spi: spi-fsl-lpspi: Adjust type of scldiv
      commit: fa8ecda9876ac1e7b29257aa82af1fd0695496e2
[2/3] spi: spi-fsl-lpspi: Fix specifiers in fsl_lpspi_set_bitrate
      commit: 7086f49dc442837996c08350a4d590e790b499db
[3/3] spi: spi-fsl-lpspi: support effective_speed_hz
      commit: 667b5e803a94f1ce48ac85b3fef94891a8d40ccf

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-10-01 17:46 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-30  9:30 [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements Stefan Wahren
2024-09-30  9:30 ` [PATCH 1/3] spi: spi-fsl-lpspi: Adjust type of scldiv Stefan Wahren
2024-09-30 15:18   ` Frank Li
2024-09-30  9:30 ` [PATCH 2/3] spi: spi-fsl-lpspi: Fix specifiers in fsl_lpspi_set_bitrate Stefan Wahren
2024-09-30 15:19   ` Frank Li
2024-09-30  9:30 ` [PATCH 3/3] spi: spi-fsl-lpspi: support effective_speed_hz Stefan Wahren
2024-09-30 15:21   ` Frank Li
2024-10-01 17:46 ` [PATCH 0/3] spi: spi-fsl-lpspi: Some calculation improvements Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).