Skip to contentSkip to conversation

spidev: A few cleanups

PATCH In review Andy Shevchenko 3 participants 8 messages Aug 24, 2023 – Sep 12, 2023 SPI

lore
Share & tools
lore
#0 lore raw

cover spidev: A few cleanups

A few cleanups to the spidev.c to utilize existing APIs and make it use less amount of Lines of Code. No functional change intended.

Andy Shevchenko (3):
spidev: Decrease indentation level in spidev_ioctl() SPI_IOC_RD_MODE*
spidev: Switch to use spi_get_csgpiod()
spidev: Simplify SPI_IOC_RD_MODE* cases in spidev_ioctl()

 drivers/spi/spidev.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
Signature · 1 line
2.40.0.1.gaa8946217a0b
#1 lore raw

PATCH 1/3 spidev: Decrease indentation level in spidev_ioctl() SPI_IOC_RD_MODE*

Instead of defining a local controller variable inside an indented block, move the definition to the top of spidev_ioctl() and reuse it in the SPI_IOC_RD_MODE* and SPI_IOC_WR_MODE* cases.

This drops unneeded indentation and reduces amount of LoCs.

---
 drivers/spi/spidev.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/spi/spidev.c +6 -8
index d13dc15cc191..dc516f0ca71f 100644
@@ -357,6 +357,7 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	int			retval = 0;
 	struct spidev_data	*spidev;
 	struct spi_device	*spi;
+	struct spi_controller	*ctlr;
 	u32			tmp;
 	unsigned		n_ioc;
 	struct spi_ioc_transfer	*ioc;
@@ -376,6 +377,8 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 		return -ESHUTDOWN;
 	}
 
+	ctlr = spi->controller;
+
 	/* use the buffer lock here for triple duty:
 	 *  - prevent I/O (from us) so calling spi_setup() is safe;
 	 *  - prevent concurrent SPI_IOC_WR_* from morphing
@@ -390,13 +393,9 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	case SPI_IOC_RD_MODE32:
 		tmp = spi->mode;
 
-		{
-			struct spi_controller *ctlr = spi->controller;
-
-			if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
-			    ctlr->cs_gpiods[spi_get_chipselect(spi, 0)])
-				tmp &= ~SPI_CS_HIGH;
-		}
+		if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
+		    ctlr->cs_gpiods[spi_get_chipselect(spi, 0)])
+			tmp &= ~SPI_CS_HIGH;
 
 		if (cmd == SPI_IOC_RD_MODE)
 			retval = put_user(tmp & SPI_MODE_MASK,
@@ -424,7 +423,6 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 		else
 			retval = get_user(tmp, (u32 __user *)arg);
 		if (retval == 0) {
-			struct spi_controller *ctlr = spi->controller;
 			u32	save = spi->mode;
 
 			if (tmp & ~SPI_MODE_MASK) {
Signature · 1 line
2.40.0.1.gaa8946217a0b
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH v1 1/3] spidev: Decrease indentation level in spidev_ioctl() SPI_IOC_RD_MODE*

Hi!

Quoted 7 lines · Andy Shevchenko

Instead of defining a local controller variable inside an indented block, move the definition to the top of spidev_ioctl() and reuse it in the SPI_IOC_RD_MODE* and SPI_IOC_WR_MODE* cases.

This drops unneeded indentation and reduces amount of LoCs.

Quoted 50 lines

---
 drivers/spi/spidev.c | 14 ++++++--------

 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/spi/spidev.c +1 -0
index d13dc15cc191..dc516f0ca71f 100644
@@ -357,6 +357,7 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
         int                     retval = 0;
         struct spidev_data      *spidev;
         struct spi_device       *spi;
+       struct spi_controller   *ctlr;
         u32                     tmp;
         unsigned                n_ioc;
         struct spi_ioc_transfer *ioc;
@@ -376,6 +377,8 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                 return -ESHUTDOWN;
         }


+       ctlr = spi->controller;
+
        /* use the buffer lock here for triple duty:
         *  - prevent I/O (from us) so calling spi_setup() is safe; *  - prevent concurrent SPI_IOC_WR_* from morphing

drivers/spi/spidev.c +0 -0
@@ -390,13 +393,9 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

        case SPI_IOC_RD_MODE32:
                tmp = spi->mode;

-               {
-                       struct spi_controller *ctlr = spi->controller; -
-                       if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods && -                           ctlr->cs_gpiods[spi_get_chipselect(spi, 0)]) -                               tmp &= ~SPI_CS_HIGH;
-               }
+               if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods && +                   ctlr->cs_gpiods[spi_get_chipselect(spi, 0)]) +                       tmp &= ~SPI_CS_HIGH;

                if (cmd == SPI_IOC_RD_MODE)
                        retval = put_user(tmp & SPI_MODE_MASK,

drivers/spi/spidev.c +0 -0
@@ -424,7 +423,6 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

                else
                        retval = get_user(tmp, (u32 __user *)arg); if (retval == 0) {
-                       struct spi_controller *ctlr = spi->controller; u32     save = spi->mode;

                        if (tmp & ~SPI_MODE_MASK) {

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
#3 lore raw

PATCH 2/3 spidev: Switch to use spi_get_csgpiod()

In reply to Andy Shevchenko

spidev_ioctl() checks if there is an SPI chip select is driven by GPIO. Instead of current code, we can call spi_get_csgpiod().

---
 drivers/spi/spidev.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/spi/spidev.c +2 -4
index dc516f0ca71f..e324b42c658c 100644
@@ -393,8 +393,7 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	case SPI_IOC_RD_MODE32:
 		tmp = spi->mode;
 
-		if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
-		    ctlr->cs_gpiods[spi_get_chipselect(spi, 0)])
+		if (ctlr->use_gpio_descriptors && spi_get_csgpiod(spi, 0))
 			tmp &= ~SPI_CS_HIGH;
 
 		if (cmd == SPI_IOC_RD_MODE)
@@ -430,8 +429,7 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 				break;
 			}
 
-			if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
-			    ctlr->cs_gpiods[spi_get_chipselect(spi, 0)])
+			if (ctlr->use_gpio_descriptors && spi_get_csgpiod(spi, 0))
 				tmp |= SPI_CS_HIGH;
 
 			tmp |= spi->mode & ~SPI_MODE_MASK;
Signature · 1 line
2.40.0.1.gaa8946217a0b
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH v1 2/3] spidev: Switch to use spi_get_csgpiod()

Hi!

Quoted 1 line · Andy Shevchenko

spidev_ioctl() checks if there is an SPI chip select is driven by GPIO.

                                                     ^^^ ?
Quoted 3 lines

Instead of current code, we can call spi_get_csgpiod().

Quoted 28 lines

---
 drivers/spi/spidev.c | 6 ++----

 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/spi/spidev.c +0 -0
index dc516f0ca71f..e324b42c658c 100644
@@ -393,8 +393,7 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

        case SPI_IOC_RD_MODE32:
                tmp = spi->mode;

-               if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods && -                   ctlr->cs_gpiods[spi_get_chipselect(spi, 0)]) +               if (ctlr->use_gpio_descriptors && spi_get_csgpiod(spi, 0)) tmp &= ~SPI_CS_HIGH;

                if (cmd == SPI_IOC_RD_MODE)

drivers/spi/spidev.c +0 -0
@@ -430,8 +429,7 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                                 break;
                         }


-                       if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods && -                           ctlr->cs_gpiods[spi_get_chipselect(spi, 0)]) +                       if (ctlr->use_gpio_descriptors && spi_get_csgpiod(spi, 0)) tmp |= SPI_CS_HIGH;

                        tmp |= spi->mode & ~SPI_MODE_MASK;

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
#5 lore raw

PATCH 3/3 spidev: Simplify SPI_IOC_RD_MODE* cases in spidev_ioctl()

In reply to Andy Shevchenko

The temporary variable tmp is not used outside of the SPI_IOC_RD_MODE* cases, hence we can optimize its use.

---
 drivers/spi/spidev.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/spi/spidev.c +3 -5
index e324b42c658c..c5450217528b 100644
@@ -391,17 +391,15 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	/* read requests */
 	case SPI_IOC_RD_MODE:
 	case SPI_IOC_RD_MODE32:
-		tmp = spi->mode;
+		tmp = spi->mode & SPI_MODE_MASK;
 
 		if (ctlr->use_gpio_descriptors && spi_get_csgpiod(spi, 0))
 			tmp &= ~SPI_CS_HIGH;
 
 		if (cmd == SPI_IOC_RD_MODE)
-			retval = put_user(tmp & SPI_MODE_MASK,
-					  (__u8 __user *)arg);
+			retval = put_user(tmp, (__u8 __user *)arg);
 		else
-			retval = put_user(tmp & SPI_MODE_MASK,
-					  (__u32 __user *)arg);
+			retval = put_user(tmp, (__u32 __user *)arg);
 		break;
 	case SPI_IOC_RD_LSB_FIRST:
 		retval = put_user((spi->mode & SPI_LSB_FIRST) ?  1 : 0,
Signature · 1 line
2.40.0.1.gaa8946217a0b
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH v1 3/3] spidev: Simplify SPI_IOC_RD_MODE* cases in spidev_ioctl()

Hi!

Quoted 5 lines · Andy Shevchenko

The temporary variable tmp is not used outside of the SPI_IOC_RD_MODE* cases, hence we can optimize its use.

Quoted 29 lines

---
 drivers/spi/spidev.c | 8 +++-----

 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/spi/spidev.c +0 -0
index e324b42c658c..c5450217528b 100644
@@ -391,17 +391,15 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
         /* read requests */

        case SPI_IOC_RD_MODE:
        case SPI_IOC_RD_MODE32:
-               tmp = spi->mode;
+               tmp = spi->mode & SPI_MODE_MASK;

                if (ctlr->use_gpio_descriptors && spi_get_csgpiod(spi, 0)) tmp &= ~SPI_CS_HIGH;

                if (cmd == SPI_IOC_RD_MODE)
-                       retval = put_user(tmp & SPI_MODE_MASK, -                                         (__u8 __user *)arg); +                       retval = put_user(tmp, (__u8 __user *)arg); else
-                       retval = put_user(tmp & SPI_MODE_MASK, -                                         (__u32 __user *)arg); +                       retval = put_user(tmp, (__u32 __user *)arg); break;
        case SPI_IOC_RD_LSB_FIRST:
                retval = put_user((spi->mode & SPI_LSB_FIRST) ?  1 : 0,

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
maintainer
#7 lore raw
In reply to Andy Shevchenko
Quoted 9 lines · Andy Shevchenko

A few cleanups to the spidev.c to utilize existing APIs and make it use less amount of Lines of Code. No functional change intended.

Andy Shevchenko (3):
spidev: Decrease indentation level in spidev_ioctl() SPI_IOC_RD_MODE*
spidev: Switch to use spi_get_csgpiod()
spidev: Simplify SPI_IOC_RD_MODE* cases in spidev_ioctl()

[...]

Applied to

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

Thanks!

[1/3] spidev: Decrease indentation level in spidev_ioctl() SPI_IOC_RD_MODE*

      commit: 12c8d7a76cd6100a2f35b9ef4b87d11128b9105b
[2/3] spidev: Switch to use spi_get_csgpiod()
      commit: 193a7f9e1a78f69c913bb26ca4500f6edad1e8ff
[3/3] spidev: Simplify SPI_IOC_RD_MODE* cases in spidev_ioctl()
      commit: 764246c7feda01f46b1a243cfa15ad5627874ef9

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