Skip to contentSkip to conversation

rtc: ds1343: Force SPI chip select to be active high

PATCH RESEND In review Ian Abbott 3 participants 7 messages Jul 10, 2024 – Jul 24, 2024

Subsystems (2)
lore
Share & tools
lore

Ian Abbott

author
#0 lore raw

PATCH RESEND 1/1 rtc: ds1343: Force SPI chip select to be active high

Commit 3b52093dc917 ("rtc: ds1343: Do not hardcode SPI mode flags") bit-flips (^=) the existing SPI_CS_HIGH setting in the SPI mode during device probe. This will set it to the wrong value if the spi-cs-high property has been set in the devicetree node. Just force it to be set active high and get rid of some commentary that attempted to explain why flipping the bit was the correct choice.

Fixes 3b52093dc917 ("rtc: ds1343: Do not hardcode SPI mode flags")
Cc <[email protected]> # 5.6+
Cc Linus Walleij <[email protected]>
Cc Mark Brown <[email protected]>
---
 drivers/rtc/rtc-ds1343.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/rtc/rtc-ds1343.c +3 -6
index ed5a6ba89a3e..484b5756b55c 100644
@@ -361,13 +361,10 @@ static int ds1343_probe(struct spi_device *spi)
 	if (!priv)
 		return -ENOMEM;
 
-	/* RTC DS1347 works in spi mode 3 and
-	 * its chip select is active high. Active high should be defined as
-	 * "inverse polarity" as GPIO-based chip selects can be logically
-	 * active high but inverted by the GPIO library.
+	/*
+	 * RTC DS1347 works in spi mode 3 and its chip select is active high.
 	 */
-	spi->mode |= SPI_MODE_3;
-	spi->mode ^= SPI_CS_HIGH;
+	spi->mode |= SPI_MODE_3 | SPI_CS_HIGH;
 	spi->bits_per_word = 8;
 	res = spi_setup(spi);
 	if (res)
Signature · 1 line
2.43.0
#1 lore raw

Hello,

Quoted 24 lines · Ian Abbott

Commit 3b52093dc917 ("rtc: ds1343: Do not hardcode SPI mode flags") bit-flips (^=) the existing SPI_CS_HIGH setting in the SPI mode during device probe. This will set it to the wrong value if the spi-cs-high property has been set in the devicetree node. Just force it to be set active high and get rid of some commentary that attempted to explain why flipping the bit was the correct choice.

Fixes 3b52093dc917 ("rtc: ds1343: Do not hardcode SPI mode flags")
Cc <[email protected]> # 5.6+
Cc Linus Walleij <[email protected]>
Cc Mark Brown <[email protected]>
---
 drivers/rtc/rtc-ds1343.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/rtc/rtc-ds1343.c +0 -0
index ed5a6ba89a3e..484b5756b55c 100644
@@ -361,13 +361,10 @@ static int ds1343_probe(struct spi_device *spi)
 	if (!priv)
 		return -ENOMEM;
 

drivers/rtc/rtc-ds1343.c static int ds1343_probe(struct spi_device *spi) · line 367

Quoted 10 lines
drivers/rtc/rtc-ds1343.c +3 -6
-	/* RTC DS1347 works in spi mode 3 and
-	 * its chip select is active high. Active high should be defined as
-	 * "inverse polarity" as GPIO-based chip selects can be logically
-	 * active high but inverted by the GPIO library.
+	/*
+	 * RTC DS1347 works in spi mode 3 and its chip select is active high.
 	 */
-	spi->mode |= SPI_MODE_3;
-	spi->mode ^= SPI_CS_HIGH;
+	spi->mode |= SPI_MODE_3 | SPI_CS_HIGH;

Linus being the gpio maintainer and Mark being the SPI maintainer, I'm pretty sure this was correct at the time.

Are you sure you are not missing an active high/low flag on a gpio definition?

Quoted 6 lines
drivers/rtc/rtc-ds1343.c +0 -0
 	spi->bits_per_word = 8;
 	res = spi_setup(spi);
 	if (res)
Signature · 1 line
2.43.0
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author
#2 lore raw

Greetings,

Quoted 27 lines · Alexandre Belloni

Hello,

Quoted 24 lines · Ian Abbott

Commit 3b52093dc917 ("rtc: ds1343: Do not hardcode SPI mode flags") bit-flips (^=) the existing SPI_CS_HIGH setting in the SPI mode during device probe. This will set it to the wrong value if the spi-cs-high property has been set in the devicetree node. Just force it to be set active high and get rid of some commentary that attempted to explain why flipping the bit was the correct choice.

Fixes 3b52093dc917 ("rtc: ds1343: Do not hardcode SPI mode flags")
Cc <[email protected]> # 5.6+
Cc Linus Walleij <[email protected]>
Cc Mark Brown <[email protected]>
---
  drivers/rtc/rtc-ds1343.c | 9 +++------
  1 file changed, 3 insertions(+), 6 deletions(-)
drivers/rtc/rtc-ds1343.c +0 -0
index ed5a6ba89a3e..484b5756b55c 100644
@@ -361,13 +361,10 @@ static int ds1343_probe(struct spi_device *spi)
  	if (!priv)
  		return -ENOMEM;
 
Quoted 13 lines

drivers/rtc/rtc-ds1343.c static int ds1343_probe(struct spi_device *spi) · line 367

Quoted 10 lines
drivers/rtc/rtc-ds1343.c +3 -6
-	/* RTC DS1347 works in spi mode 3 and
-	 * its chip select is active high. Active high should be defined as
-	 * "inverse polarity" as GPIO-based chip selects can be logically
-	 * active high but inverted by the GPIO library.
+	/*
+	 * RTC DS1347 works in spi mode 3 and its chip select is active high.
  	 */
-	spi->mode |= SPI_MODE_3;
-	spi->mode ^= SPI_CS_HIGH;
+	spi->mode |= SPI_MODE_3 | SPI_CS_HIGH;

Linus being the gpio maintainer and Mark being the SPI maintainer, I'm pretty sure this was correct at the time.

I'm not convinced. What value of spi->mode & SPI_CS_HIGH do you think the device should end up using? (I think it should end up using SPI_CS_HIGH, which was the case before commit 3b52093dc917, because the RTC chip requires active high CS.) What do you think the value of spi->mode & SPI_CS_HIGH will be *before* the ^= operation? (For devicetree, that depends on the spi-cs-high property.)

I think the devicetree node for the RTC device ought to be setting spi-cs-high but cannot do so at the moment because the driver clobbers it.

Quoted 2 lines

Are you sure you are not missing an active high/low flag on a gpio definition?

The CS might be internal to the SPI controller, not using a GPIO line (cs-gpios property). SPI peripheral device drivers shouldn't care if CS is using GPIO or not.

Quoted 8 lines
Quoted 7 lines
drivers/rtc/rtc-ds1343.c +0 -0
  	spi->bits_per_word = 8;
  	res = spi_setup(spi);
  	if (res)
Signature · 1 line
2.43.0
Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-
#3 lore raw
Quoted 2 lines · Ian Abbott

I think the devicetree node for the RTC device ought to be setting spi-cs-high but cannot do so at the moment because the driver clobbers it.

Specifying spi-cs-high in the device tree should almost always be redundant or a mistake, if the device needs a high chip select then we already know that from the compatible. The property is adding nothing but potential confusion, in the normal course of affairs the driver should just specify the configuration it needs for the bus.

author
#4 lore raw
Quoted 10 lines · Mark Brown
Quoted 2 lines · Ian Abbott

I think the devicetree node for the RTC device ought to be setting spi-cs-high but cannot do so at the moment because the driver clobbers it.

Specifying spi-cs-high in the device tree should almost always be redundant or a mistake, if the device needs a high chip select then we already know that from the compatible. The property is adding nothing but potential confusion, in the normal course of affairs the driver should just specify the configuration it needs for the bus.

So spi->mode |= SPI_CS_HIGH; is safer than spi->mode ^= SPI_CS_HIGH;?

Regarding spi-cs-high in the device tree, what about the compatibility table for spi-cs-high and cs-gpio active level in "Documentation/devicetree/bindings/spi/spi-controller.yaml"?

Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-
#5 lore raw
Quoted 3 lines · Ian Abbott

Regarding spi-cs-high in the device tree, what about the compatibility table for spi-cs-high and cs-gpio active level in "Documentation/devicetree/bindings/spi/spi-controller.yaml"?

Specifying spi-cs-high should be equivalent to setting the mode in the driver, it's just a redundant way of saying the same thing.

author
#6 lore raw
In reply to Alexandre Belloni

Greetings,

Quoted 41 lines · Alexandre Belloni

Hello,

Quoted 24 lines · Ian Abbott

Commit 3b52093dc917 ("rtc: ds1343: Do not hardcode SPI mode flags") bit-flips (^=) the existing SPI_CS_HIGH setting in the SPI mode during device probe. This will set it to the wrong value if the spi-cs-high property has been set in the devicetree node. Just force it to be set active high and get rid of some commentary that attempted to explain why flipping the bit was the correct choice.

Fixes 3b52093dc917 ("rtc: ds1343: Do not hardcode SPI mode flags")
Cc <[email protected]> # 5.6+
Cc Linus Walleij <[email protected]>
Cc Mark Brown <[email protected]>
---
  drivers/rtc/rtc-ds1343.c | 9 +++------
  1 file changed, 3 insertions(+), 6 deletions(-)
drivers/rtc/rtc-ds1343.c +0 -0
index ed5a6ba89a3e..484b5756b55c 100644
@@ -361,13 +361,10 @@ static int ds1343_probe(struct spi_device *spi)
  	if (!priv)
  		return -ENOMEM;
 

drivers/rtc/rtc-ds1343.c static int ds1343_probe(struct spi_device *spi) · line 367

Quoted 10 lines
drivers/rtc/rtc-ds1343.c +3 -6
-	/* RTC DS1347 works in spi mode 3 and
-	 * its chip select is active high. Active high should be defined as
-	 * "inverse polarity" as GPIO-based chip selects can be logically
-	 * active high but inverted by the GPIO library.
+	/*
+	 * RTC DS1347 works in spi mode 3 and its chip select is active high.
  	 */
-	spi->mode |= SPI_MODE_3;
-	spi->mode ^= SPI_CS_HIGH;
+	spi->mode |= SPI_MODE_3 | SPI_CS_HIGH;

Linus being the gpio maintainer and Mark being the SPI maintainer, I'm pretty sure this was correct at the time.

Quoted 10 lines

Are you sure you are not missing an active high/low flag on a gpio definition?

Quoted 6 lines
drivers/rtc/rtc-ds1343.c +0 -0
  	spi->bits_per_word = 8;
  	res = spi_setup(spi);
  	if (res)
Signature · 1 line
2.43.0

I now have an actual SPI controller using cs-gpios with a DS1343 connected. I have tested all 8 combinations of: GPIO_ACTIVE_LOW/GPIO_ACTIVE_HIGH, with/without spi-cs-high, without/with my patch. Here are my results and observations:

                                 Final        Final
GPIO_ACTIVE  spi-cs-high  Patcb  GPIO active  CS high  Works?
===========  ===========  =====  ===========  =======  =======
   LOW           No        No      LOW         Yes      No
   LOW           No        Yes     LOW         Yes      No
   LOW           Yes       No      HIGH(1)     No       Yes(3)
   LOW           Yes       Yes     HIGH(1)     Yes      Yes
   HIGH          No        No      LOW(2)      Yes      No
   HIGH          No        Yes     LOW(2)      Yes      No
   HIGH          Yes       No      HIGH        No       Yes(3)
   HIGH          Yes       Yes     HIGH        Yes      Yes

The "Final GPIO active" column refers to the GPIO active state after any quirks have been applied. The "Final CS High" column refers to whether SPI_CS_HIGH is set in spi->mode when spi_setup() is called.

Notes:

(1) GPIO was forced active high by of_gpio_flags_quirks() before RTC device probed.

(2) GPIO was forced active low by of_gpio_flags_quirks() before RTC device probed.

(3) Works if cs-gpios being used, but probably will not work for SPI controllers that do not use cs-gpios because SPI_CS_HIGH is not set.

In summary:

1. Without the patch, the RTC device node requires the spi-cs-high property to be present if the SPI controller uses cs-gpios, and requires the spi-cs-high property to be omitted if the SPI controller does not use cs-gpios. (I think that is a confusing situation.)

2. With the patch, the RTC device node requires the spi-cs-high property to be present if the SPI controller uses cs-gpios, and it does not care about the spi-cs-high property if the SPI controller does not use cs-gpios. (I therefore think that the patch should be applied so that the device node can just set spi-cs-high without caring whether ht SPI controller uses cs-gpios or not.)

Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-