Skip to contentSkip to conversation

spi: core: Fix logic mismatch in spi_master.set_cs()

PATCH/RFC Geert Uytterhoeven 2 participants 9 messages Jan 14, 2014 – Jan 17, 2014 SPI

lore
Share & tools
lore
#0 lore raw

From: Geert Uytterhoeven <[email protected]>

The documentation for spi_master.set_cs() says:

assert or deassert chip select, true to assert

i.e. its "enable" parameter uses assertion-level logic.

This does not match the implementation of spi_set_cs(), which calls spi_master.set_cs() with the wanted logical value of the chip select line, i.e. "false" to assert an active low chip select, and "true" to assert an active high chip select.

Correct the implementation to use assertion-level logic.

For GPIO-based chip selects, active high chip selects are still handled in spi_set_cs(), as this is a direct GPIO level.
For SPI controller-based chip selects, active high chip selects must be handled by the SPI master driver, if supported (some SPI controllers have configurable chip select polarity).

---
 drivers/spi/spi.c |   11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/spi/spi.c +5 -6
index a86569e1f178..eb20169e84e8 100644
@@ -561,13 +561,12 @@ int spi_register_board_info(struct spi_board_info const *info, unsigned n)
 
 static void spi_set_cs(struct spi_device *spi, bool enable)
 {
-	if (spi->mode & SPI_CS_HIGH)
-		enable = !enable;
-
-	if (spi->cs_gpio >= 0)
+	if (spi->cs_gpio >= 0) {
+		if (spi->mode & SPI_CS_HIGH)
+			enable = !enable;
 		gpio_set_value(spi->cs_gpio, !enable);
-	else if (spi->master->set_cs)
-		spi->master->set_cs(spi, !enable);
+	} else if (spi->master->set_cs)
+		spi->master->set_cs(spi, enable);
 }
 
 /*
Signature · 6 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer
#1 lore raw
Quoted 4 lines · Geert Uytterhoeven

{

drivers/spi/spi.c +0 -3
-	if (spi->mode & SPI_CS_HIGH)
-		enable = !enable;
-

drivers/spi/spi.c int spi_register_board_info(struct spi_board_info const *info, unsigned n) · line 570

Quoted 10 lines
drivers/spi/spi.c +5 -3
-	if (spi->cs_gpio >= 0)
+	if (spi->cs_gpio >= 0) {
+		if (spi->mode & SPI_CS_HIGH)
+			enable = !enable;
 		gpio_set_value(spi->cs_gpio, !enable);
-	else if (spi->master->set_cs)
-		spi->master->set_cs(spi, !enable);
+	} else if (spi->master->set_cs)
+		spi->master->set_cs(spi, enable);
 }

Coding style, braces on all branches of an if statement.

This also pushes the handling of CS_HIGH back out into the driver which doesn't seem like it's helping anything. Flipping the sense of enable when calling set_cs() is probably OK though.

#2 lore raw

Hi Mark,

Quoted 6 lines · Mark Brown
Quoted 4 lines · Geert Uytterhoeven

The documentation for spi_master.set_cs() says:

assert or deassert chip select, true to assert

Quoted 8 lines
Quoted 5 lines

i.e. its "enable" parameter uses assertion-level logic.

For SPI controller-based chip selects, active high chip selects must be handled by the SPI master driver, if supported (some SPI controllers have configurable chip select polarity).

This also pushes the handling of CS_HIGH back out into the driver which doesn't seem like it's helping anything. Flipping the sense of enable

It depends: on hardware with separate register bits for chip select polarity and chip select assertion it avoids having to invert the enable value a second time.

Quoted 1 line

when calling set_cs() is probably OK though.

Just flipping the sense of enable still needs a documentation update.

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
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#3 lore raw
Quoted 9 lines · Geert Uytterhoeven
Quoted 4 lines · Mark Brown

On Tue, Jan 14, 2014 at 12:36:51PM +0100, Geert Uytterhoeven wrote:

This also pushes the handling of CS_HIGH back out into the driver which doesn't seem like it's helping anything. Flipping the sense of enable

It depends: on hardware with separate register bits for chip select polarity and chip select assertion it avoids having to invert the enable value a second time.

If we're manually setting /CS it really makes no difference what the chip thinks the polarity is - something that is controlling /CS autonomously can't implement this operation and something that can just set it at any time doesn't need to worry if the chip thinks it's asserted or not.

Quoted 3 lines
Quoted 1 line

when calling set_cs() is probably OK though.

Just flipping the sense of enable still needs a documentation update.

Huh? Why were you updating the code then...

#4 lore raw
Quoted 11 lines · Mark Brown
Quoted 9 lines · Geert Uytterhoeven
Quoted 4 lines · Mark Brown

On Tue, Jan 14, 2014 at 12:36:51PM +0100, Geert Uytterhoeven wrote:

This also pushes the handling of CS_HIGH back out into the driver which doesn't seem like it's helping anything. Flipping the sense of enable

It depends: on hardware with separate register bits for chip select polarity and chip select assertion it avoids having to invert the enable value a second time.

Quoted 5 lines

If we're manually setting /CS it really makes no difference what the chip thinks the polarity is - something that is controlling /CS autonomously can't implement this operation and something that can just set it at any time doesn't need to worry if the chip thinks it's asserted or not.

Doh, so I'm the only one where it does matter, as RSPI has separate Slave Select Signal Polarity (high/low) and Slave Select Output Setting (enable/disable)...

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

when calling set_cs() is probably OK though.

Just flipping the sense of enable still needs a documentation update.

Huh? Why were you updating the code then...

"true to assert" in the documentation means that enable is true when enabling the chip select.
Currently the value of enable depends on SPI_CS_HIGH. Just "flipping the sense of enable" doesn't change that dependency.

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

maintainer
#5 lore raw
Quoted 11 lines · Geert Uytterhoeven
Quoted 5 lines · Mark Brown

If we're manually setting /CS it really makes no difference what the chip thinks the polarity is - something that is controlling /CS autonomously can't implement this operation and something that can just set it at any time doesn't need to worry if the chip thinks it's asserted or not.

Doh, so I'm the only one where it does matter, as RSPI has separate Slave Select Signal Polarity (high/low) and Slave Select Output Setting (enable/disable)...

I'm sure there's other hardware out there which has such control, it's just that there's no value in actually using the polarity select if we have manual control over the enable. All you're doing is adding complexity in drivers.

Quoted 10 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

when calling set_cs() is probably OK though.

Just flipping the sense of enable still needs a documentation update.

Huh? Why were you updating the code then...

"true to assert" in the documentation means that enable is true when enabling the chip select.
Currently the value of enable depends on SPI_CS_HIGH. Just "flipping the sense of enable" doesn't change that dependency.

Oh, so the code update was purely about factoring that out of the core?

#6 lore raw

Hi Mark,

Quoted 6 lines · Mark Brown
Quoted 4 lines · Geert Uytterhoeven
Quoted 3 lines · Mark Brown
Quoted 3 lines
Quoted 1 line

when calling set_cs() is probably OK though.

Just flipping the sense of enable still needs a documentation update.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Huh? Why were you updating the code then...

"true to assert" in the documentation means that enable is true when enabling the chip select.
Currently the value of enable depends on SPI_CS_HIGH. Just "flipping the sense of enable" doesn't change that dependency.

Oh, so the code update was purely about factoring that out of the core?

No, it was about fixing the mismatch between code and documentation.

There are two ways to correct the mismatch:
1. Make the code match the documentation.
That's what I did, as it avoids a double conditional inversion on RSPI.
2. Make the documentation match the code.
It seems this is what you prefer?
So "assert or deassert chip select, true to assert" has to be replaced by "Set the logical level of the chip select line"? Or do you have a better suggestion?

Thanks!

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
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#7 lore raw
Quoted 2 lines · Geert Uytterhoeven

2. Make the documentation match the code.
It seems this is what you prefer?

Yes.

#8 lore raw
Quoted 4 lines · Mark Brown
Quoted 2 lines

2. Make the documentation match the code.
It seems this is what you prefer?

Yes.

Thanks, will do.

Signature · 14 lines
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
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html