Skip to contentSkip to conversation

spi: pxa2xx: use min() instead of min_t()

PATCH Superseded Andy Shevchenko 3 participants 4 messages Feb 23 – Feb 25

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

PATCH 1/1 spi: pxa2xx: use min() instead of min_t()

min_t(int, a, b) casts an 'unsigned long' to 'int'. This might lead to the cases when big number is wrongly chosen. On the other hand, the SPI transfer speed rate is unsigned and driver uses signed type for an unknown reason. Change the type of the SPI transfer speed to be unsigned and convert to use min() instead of min_t().

---
 drivers/spi/spi-pxa2xx.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/spi/spi-pxa2xx.c +4 -5
index f7881a31e4cc..23e40f410814 100644
@@ -796,7 +796,7 @@ static irqreturn_t ssp_int(int irq, void *dev_id)
  * The function calculates parameters for all cases and chooses the one closest
  * to the asked baud rate.
  */
-static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
+static unsigned int quark_x1000_get_clk_div(unsigned long rate, u32 *dds)
 {
 	unsigned long xtal = 200000000;
 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
@@ -885,12 +885,12 @@ static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
 	return q - 1;
 }
 
-static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
+static unsigned int ssp_get_clk_div(struct driver_data *drv_data, unsigned long rate)
 {
 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
 	const struct ssp_device *ssp = drv_data->ssp;
 
-	rate = min_t(int, ssp_clk, rate);
+	rate = min(ssp_clk, rate);
 
 	/*
 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
@@ -902,8 +902,7 @@ static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
 }
 
-static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
-					   int rate)
+static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data, unsigned long rate)
 {
 	struct chip_data *chip =
 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
Signature · 1 line
2.50.1
#1 lore raw
Quoted 5 lines · On Mon, 23 Feb 2026 15:44:37 +0100 Andy Shevchenko…

min_t(int, a, b) casts an 'unsigned long' to 'int'. This might lead to the cases when big number is wrongly chosen. On the other hand, the SPI transfer speed rate is unsigned and driver uses signed type for an unknown reason. Change the type of the SPI transfer speed to be unsigned and convert to use min() instead of min_t().

I think you should use 'unsigned int' or 'u32' not 'unsigned long'. There is no reason for these fields to have different sizes on 32 and 64bit.

Were spi to support 4GHz clocks (fat chance on a pcb track) you'd need to use u64.

David

Quoted 44 lines
---
 drivers/spi/spi-pxa2xx.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/spi/spi-pxa2xx.c +4 -5
index f7881a31e4cc..23e40f410814 100644
@@ -796,7 +796,7 @@ static irqreturn_t ssp_int(int irq, void *dev_id)
  * The function calculates parameters for all cases and chooses the one closest
  * to the asked baud rate.
  */
-static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
+static unsigned int quark_x1000_get_clk_div(unsigned long rate, u32 *dds)
 {
 	unsigned long xtal = 200000000;
 	unsigned long fref = xtal / 2;		/* mandatory division by 2,
@@ -885,12 +885,12 @@ static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
 	return q - 1;
 }
 
-static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
+static unsigned int ssp_get_clk_div(struct driver_data *drv_data, unsigned long rate)
 {
 	unsigned long ssp_clk = drv_data->controller->max_speed_hz;
 	const struct ssp_device *ssp = drv_data->ssp;
 
-	rate = min_t(int, ssp_clk, rate);
+	rate = min(ssp_clk, rate);
 
 	/*
 	 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
@@ -902,8 +902,7 @@ static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
 		return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
 }
 
-static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
-					   int rate)
+static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data, unsigned long rate)
 {
 	struct chip_data *chip =
 		spi_get_ctldata(drv_data->controller->cur_msg->spi);
author reviewer
#2 lore raw
Quoted 9 lines · David Laight
Quoted 5 lines · On Mon, 23 Feb 2026 15:44:37 +0100 Andy Shevchenko…

min_t(int, a, b) casts an 'unsigned long' to 'int'. This might lead to the cases when big number is wrongly chosen. On the other hand, the SPI transfer speed rate is unsigned and driver uses signed type for an unknown reason. Change the type of the SPI transfer speed to be unsigned and convert to use min() instead of min_t().

Quoted 5 lines

I think you should use 'unsigned int' or 'u32' not 'unsigned long'. There is no reason for these fields to have different sizes on 32 and 64bit.

Were spi to support 4GHz clocks (fat chance on a pcb track) you'd need to use u64.

Fair enough. I will correct in v2.
Thanks for review.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw
In reply to Andy Shevchenko
Quoted 8 lines · Andy Shevchenko

min_t(int, a, b) casts an 'unsigned long' to 'int'. This might lead to the cases when big number is wrongly chosen. On the other hand, the SPI transfer speed rate is unsigned and driver uses signed type for an unknown reason. Change the type of the SPI transfer speed to be unsigned and convert to use min() instead of min_t().

[...]

Applied to

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

Thanks!

[1/1] spi: pxa2xx: use min() instead of min_t()

      commit: 507a071d9868cb60e4e76f8a06fc8eb014f59ae4

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