Skip to contentSkip to conversation

spi: struct spi_device constification

PATCH Applied Geert Uytterhoeven 3 participants 9 messages Mar 13, 2023 – Mar 16, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover spi: struct spi_device constification

Hi all,

After noticing new cases of casting away constness, I went over all spi_get_*() functions and their callers, and made the following changes:
1. Make all pointer parameters const where possible,
2. Remove unneeded casts, some not even related to constness.

Patches 1-5 do not have any dependencies. Patch 6 depends on patch 5 (and current spi/for-next).

Thanks for your comments!

Geert Uytterhoeven (6):
mfd: rsmu_spi: Remove unneeded casts of void *
spi: fsl-dspi: Remove unneeded cast to same type
spi: Constify spi_get_ctldata()'s spi parameter
spi: Constify spi_get_drvdata()'s spi parameter
spi: Constify spi parameters of chip select APIs
spi: sh-msiof: Remove casts to drop constness

 drivers/mfd/rsmu_spi.c     | 4 ++--
 drivers/spi/spi-fsl-dspi.c | 2 +-
 drivers/spi/spi-sh-msiof.c | 4 ++--
 include/linux/spi/spi.h    | 8 ++++----
 4 files changed, 9 insertions(+), 9 deletions(-)
Signature · 12 lines
2.34.1

Gr{oetje,eeting}s,

						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
							    -- Linus Torvalds
#1 lore raw

PATCH 1/6 mfd: rsmu_spi: Remove unneeded casts of void *

There is no need to cast a "void *" to a different type of pointer.

---
 drivers/mfd/rsmu_spi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mfd/rsmu_spi.c +2 -2
index d2f3d8f1e05af054..2428aaa9aaed9e43 100644
@@ -129,7 +129,7 @@ static int rsmu_write_page_register(struct rsmu_ddata *rsmu, u16 reg)
 
 static int rsmu_reg_read(void *context, unsigned int reg, unsigned int *val)
 {
-	struct rsmu_ddata *rsmu = spi_get_drvdata((struct spi_device *)context);
+	struct rsmu_ddata *rsmu = spi_get_drvdata(context);
 	u8 addr = (u8)(reg & RSMU_LOWER_ADDR_MASK);
 	int err;
 
@@ -146,7 +146,7 @@ static int rsmu_reg_read(void *context, unsigned int reg, unsigned int *val)
 
 static int rsmu_reg_write(void *context, unsigned int reg, unsigned int val)
 {
-	struct rsmu_ddata *rsmu = spi_get_drvdata((struct spi_device *)context);
+	struct rsmu_ddata *rsmu = spi_get_drvdata(context);
 	u8 addr = (u8)(reg & RSMU_LOWER_ADDR_MASK);
 	u8 data = (u8)val;
 	int err;
Signature · 1 line
2.34.1
maintainer Applied
#2 lore raw

Re: [PATCH 1/6] mfd: rsmu_spi: Remove unneeded casts of void *

Quoted 6 lines · Geert Uytterhoeven

There is no need to cast a "void *" to a different type of pointer.

---
 drivers/mfd/rsmu_spi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Applied, thanks

--
Lee Jones [李琼斯]

#3 lore raw

PATCH 2/6 spi: fsl-dspi: Remove unneeded cast to same type

In reply to Geert Uytterhoeven

There is never a need to cast a pointer to the same pointer type.

---
 drivers/spi/spi-fsl-dspi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-fsl-dspi.c +1 -1
index ee42285b5c5243a5..4339485d202c05c7 100644
@@ -1068,7 +1068,7 @@ static int dspi_setup(struct spi_device *spi)
 
 static void dspi_cleanup(struct spi_device *spi)
 {
-	struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
+	struct chip_data *chip = spi_get_ctldata(spi);
 
 	dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
 		spi->controller->bus_num, spi_get_chipselect(spi, 0));
Signature · 1 line
2.34.1
#4 lore raw

PATCH 3/6 spi: Constify spi_get_ctldata()'s spi parameter

In reply to Geert Uytterhoeven

The "spi" parameter of spi_get_ctldata() can be const.

---
 include/linux/spi/spi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/spi/spi.h +1 -1
index bdb35a91b4bf0141..6097d2f5126624eb 100644
@@ -244,7 +244,7 @@ static inline void spi_dev_put(struct spi_device *spi)
 }
 
 /* ctldata is for the bus_controller driver's runtime state */
-static inline void *spi_get_ctldata(struct spi_device *spi)
+static inline void *spi_get_ctldata(const struct spi_device *spi)
 {
 	return spi->controller_state;
 }
Signature · 1 line
2.34.1
#5 lore raw

PATCH 4/6 spi: Constify spi_get_drvdata()'s spi parameter

In reply to Geert Uytterhoeven

The "spi" parameter of spi_get_drvdata() can be const. dev_get_drvdata() has been taking a const pointer since commit 7d1d8999b4bec0ba ("i2c: Constify i2c_get_clientdata's parameter").

---
 include/linux/spi/spi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/spi/spi.h +1 -1
index 6097d2f5126624eb..e09a61dd34594330 100644
@@ -261,7 +261,7 @@ static inline void spi_set_drvdata(struct spi_device *spi, void *data)
 	dev_set_drvdata(&spi->dev, data);
 }
 
-static inline void *spi_get_drvdata(struct spi_device *spi)
+static inline void *spi_get_drvdata(const struct spi_device *spi)
 {
 	return dev_get_drvdata(&spi->dev);
 }
Signature · 1 line
2.34.1
#6 lore raw

PATCH 5/6 spi: Constify spi parameters of chip select APIs

In reply to Geert Uytterhoeven

The "spi" parameters of spi_get_chipselect() and spi_get_csgpiod() can be const.

Fixes 303feb3cc06ac066 ("spi: Add APIs in spi core to set/get spi->chip_select and spi->cs_gpiod")
---
 include/linux/spi/spi.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/spi/spi.h +2 -2
index e09a61dd34594330..74bff5a2f53d36cc 100644
@@ -266,7 +266,7 @@ static inline void *spi_get_drvdata(const struct spi_device *spi)
 	return dev_get_drvdata(&spi->dev);
 }
 
-static inline u8 spi_get_chipselect(struct spi_device *spi, u8 idx)
+static inline u8 spi_get_chipselect(const struct spi_device *spi, u8 idx)
 {
 	return spi->chip_select;
 }
@@ -276,7 +276,7 @@ static inline void spi_set_chipselect(struct spi_device *spi, u8 idx, u8 chipsel
 	spi->chip_select = chipselect;
 }
 
-static inline struct gpio_desc *spi_get_csgpiod(struct spi_device *spi, u8 idx)
+static inline struct gpio_desc *spi_get_csgpiod(const struct spi_device *spi, u8 idx)
 {
 	return spi->cs_gpiod;
 }
Signature · 1 line
2.34.1
#7 lore raw

PATCH 6/6 spi: sh-msiof: Remove casts to drop constness

In reply to Geert Uytterhoeven

Now the chip select APIs take const pointers, there is no longer a need to cast away constness.

Fixes 9e264f3f85a56cc1 ("spi: Replace all spi->chip_select and spi->cs_gpiod references with function call")
---
 drivers/spi/spi-sh-msiof.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/spi/spi-sh-msiof.c +2 -2
index 50498c4eb661d09f..55dfe2fd89c8ba4c 100644
@@ -587,11 +587,11 @@ static int sh_msiof_prepare_message(struct spi_controller *ctlr,
 	u32 ss, cs_high;
 
 	/* Configure pins before asserting CS */
-	if (spi_get_csgpiod((struct spi_device *)spi, 0)) {
+	if (spi_get_csgpiod(spi, 0)) {
 		ss = ctlr->unused_native_cs;
 		cs_high = p->native_cs_high;
 	} else {
-		ss = spi_get_chipselect((struct spi_device *)spi, 0);
+		ss = spi_get_chipselect(spi, 0);
 		cs_high = !!(spi->mode & SPI_CS_HIGH);
 	}
 	sh_msiof_spi_set_pin_regs(p, ss, !!(spi->mode & SPI_CPOL),
Signature · 1 line
2.34.1
maintainer
#8 lore raw

Re: (subset) [PATCH 0/6] spi: struct spi_device constification

In reply to Geert Uytterhoeven
Quoted 8 lines · Geert Uytterhoeven

Hi all,

After noticing new cases of casting away constness, I went over all spi_get_*() functions and their callers, and made the following changes:
1. Make all pointer parameters const where possible,
2. Remove unneeded casts, some not even related to constness.

[...]

Applied to

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

Thanks!

[2/6] spi: fsl-dspi: Remove unneeded cast to same type

      commit: 9c8400e3795decefe6390646ece820a310b37b79
[3/6] spi: Constify spi_get_ctldata()'s spi parameter
      commit: cc4b15670340315fb0b25d886c06bffb5f128f02
[4/6] spi: Constify spi_get_drvdata()'s spi parameter
      commit: 38dca04d659a422d842f7edcecd32253c7a6fb5e
[5/6] spi: Constify spi parameters of chip select APIs
      commit: d2f19eec510424caa55ea949f016ddabe2d8173a
[6/6] spi: sh-msiof: Remove casts to drop constness
      commit: 7859ad5a401b73bf2ddfa274950b5722492c3150

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