Skip to contentSkip to conversation

spi: Assume GPIO CS active high in ACPI case

PATCH Superseded Andy Shevchenko 3 participants 4 messages May 10, 2021 – May 11, 2021 SPI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: Assume GPIO CS active high in ACPI case

Currently GPIO CS handling, when descriptors are in use, doesn't take into consideration that in ACPI case the default polarity is active high and can't be altered. Instead we have to use the per-chip definition provided by SPISerialBus() resource.

Fixes 766c6b63aa04 ("spi: fix client driver breakages when using GPIO descriptors")
Cc Liguang Zhang <[email protected]>
Cc Jay Fang <[email protected]>
Cc Sven Van Asbroeck <[email protected]>
---
 drivers/spi/spi.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/spi/spi.c +14 -3
index 956dce3aafca..f56e0212ee97 100644
@@ -820,12 +820,23 @@ static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
 
 	if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
 		if (!(spi->mode & SPI_NO_CS)) {
+			/*
+			 * Historically ACPI has no means of the GPIO polarity and thus
+			 * the SPISerialBus() resource defines it on the per-chip basis.
+			 * In order to avoid a chain of negations, the GPIO polarity is
+			 * considered being Active High. Even for the cases when _DSD()
+			 * is involved (in the updated versions of ACPI) the GPIO CS
+			 * polarity must be defined Active High to avoid ambiguity.
+			 * That's why we use enable, that takes SPI_CS_HIGH into account.
+			 */
+			bool value = has_acpi_companion(&spi->dev) ? !enable : activate;
+
 			if (spi->cs_gpiod)
-				/* polarity handled by gpiolib */
-				gpiod_set_value_cansleep(spi->cs_gpiod, activate);
+				/* Polarity handled by GPIO library */
+				gpiod_set_value_cansleep(spi->cs_gpiod, value);
 			else
 				/*
-				 * invert the enable line, as active low is
+				 * Invert the enable line, as active low is
 				 * default for SPI.
 				 */
 				gpio_set_value_cansleep(spi->cs_gpio, !enable);
Signature · 1 line
2.30.2
#1 lore raw

Hi Andy, see below.

drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable, bool force) · line 832

Quoted 13 lines · Andy Shevchenko
        if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
                if (!(spi->mode & SPI_NO_CS)) {
drivers/spi/spi.c +10 -0
+                       /*
+                        * Historically ACPI has no means of the GPIO polarity and thus
+                        * the SPISerialBus() resource defines it on the per-chip basis.
+                        * In order to avoid a chain of negations, the GPIO polarity is
+                        * considered being Active High. Even for the cases when _DSD()
+                        * is involved (in the updated versions of ACPI) the GPIO CS
+                        * polarity must be defined Active High to avoid ambiguity.
+                        * That's why we use enable, that takes SPI_CS_HIGH into account.
+                        */
+                       bool value = has_acpi_companion(&spi->dev) ? !enable : activate;

There are three booleans involved now: "value", "enable" and "activate". It might be quite hard for someone reading this code later, to work out what's going on? I've got to admit that my previous choice of "enable1" was also not perfect, to say the least...

AFAIK there are two basic concepts in this function:
- enable: indicates chip-select enabled or disabled (independent of gpio polarity)
- level : indicates the required level of the chip-select gpio

So maybe we can simplify like this?

static void spi_set_cs(struct spi_device *spi, bool enable, bool force) {
bool level = (spi->mode & SPI_CS_HIGH) ? enable : !enable;

if cs_gpio => use level : gpio_set_value_cansleep(level)
else if cs_gpiod(acpi) => use level : gpiod_set_value_cansleep(level)
else if cs_gpiod(_) => use enable :
gpiod_set_value_cansleep(enable);
}

maintainer
#2 lore raw
In reply to Andy Shevchenko

drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable, bool force) · line 832

Quoted 1 line · Andy Shevchenko
drivers/spi/spi.c +1 -0
+			bool value = has_acpi_companion(&spi->dev) ? !enable : activate;

Please write normal conditional statements to improve legibility.

drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable, bool force) · line 840

Quoted 10 lines
drivers/spi/spi.c +3 -3
 			if (spi->cs_gpiod)
-				/* polarity handled by gpiolib */
-				gpiod_set_value_cansleep(spi->cs_gpiod, activate);
+				/* Polarity handled by GPIO library */
+				gpiod_set_value_cansleep(spi->cs_gpiod, value);
 			else
 				/*
-				 * invert the enable line, as active low is
+				 * Invert the enable line, as active low is
 				 * default for SPI.

The change would be clearer with the documentation formatting changes split out from the rest of it.

#3 lore raw
Quoted 5 lines · Mark Brown

drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable, bool force) · line 832

Quoted 1 line · Andy Shevchenko
drivers/spi/spi.c +1 -0
+			bool value = has_acpi_companion(&spi->dev) ? !enable : activate;

Please write normal conditional statements to improve legibility.

OK!

Quoted 5 lines
Quoted 5 lines
drivers/spi/spi.c +2 -2
 			if (spi->cs_gpiod)
-				/* polarity handled by gpiolib */
-				gpiod_set_value_cansleep(spi->cs_gpiod, activate);
+				/* Polarity handled by GPIO library */
+				gpiod_set_value_cansleep(spi->cs_gpiod, value);
Quoted 8 lines

drivers/spi/spi.c static void spi_set_cs(struct spi_device *spi, bool enable, bool force) · line 840

Quoted 5 lines
drivers/spi/spi.c +1 -1
 			else
 				/*
-				 * invert the enable line, as active low is
+				 * Invert the enable line, as active low is
 				 * default for SPI.

The change would be clearer with the documentation formatting changes split out from the rest of it.

Will remove these changes from a fix in v2.

P.S> something is odd about the series I have submitted, i.e. half of the patches simply didn't make it. I would wait with any new submission until it will be clarified, otherwise it's a chance to miss more patches without any reason why.

Signature · 2 lines
With Best Regards,
Andy Shevchenko