linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs()
@ 2014-01-14 11:36 Geert Uytterhoeven
  2014-01-14 12:52 ` Mark Brown
  0 siblings, 1 reply; 9+ messages in thread
From: Geert Uytterhoeven @ 2014-01-14 11:36 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Geert Uytterhoeven

From: Geert Uytterhoeven <geert+renesas@linux-m68k.org>

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).

Signed-off-by: Geert Uytterhoeven <geert+renesas@linux-m68k.org>
---
 drivers/spi/spi.c |   11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index a86569e1f178..eb20169e84e8 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -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);
 }
 
 /*
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs()
  2014-01-14 11:36 [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs() Geert Uytterhoeven
@ 2014-01-14 12:52 ` Mark Brown
  2014-01-14 13:23   ` Geert Uytterhoeven
  0 siblings, 1 reply; 9+ messages in thread
From: Mark Brown @ 2014-01-14 12:52 UTC (permalink / raw)
  To: Geert Uytterhoeven; +Cc: linux-spi, linux-kernel, Geert Uytterhoeven

[-- Attachment #1: Type: text/plain, Size: 693 bytes --]

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

>  {
> -	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);
>  }

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.

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs()
  2014-01-14 12:52 ` Mark Brown
@ 2014-01-14 13:23   ` Geert Uytterhoeven
  2014-01-14 13:45     ` Mark Brown
  0 siblings, 1 reply; 9+ messages in thread
From: Geert Uytterhoeven @ 2014-01-14 13:23 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Geert Uytterhoeven

Hi Mark,

On Tue, Jan 14, 2014 at 1:52 PM, Mark Brown <broonie@kernel.org> wrote:
>On Tue, Jan 14, 2014 at 12:36:51PM +0100, Geert Uytterhoeven wrote:
>
>> 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.

>> 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.

> 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 -- geert@linux-m68k.org

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs()
  2014-01-14 13:23   ` Geert Uytterhoeven
@ 2014-01-14 13:45     ` Mark Brown
  2014-01-14 14:44       ` Geert Uytterhoeven
  0 siblings, 1 reply; 9+ messages in thread
From: Mark Brown @ 2014-01-14 13:45 UTC (permalink / raw)
  To: Geert Uytterhoeven; +Cc: linux-spi, linux-kernel, Geert Uytterhoeven

[-- Attachment #1: Type: text/plain, Size: 991 bytes --]

On Tue, Jan 14, 2014 at 02:23:37PM +0100, Geert Uytterhoeven wrote:
> On Tue, Jan 14, 2014 at 1:52 PM, Mark Brown <broonie@kernel.org> wrote:
> >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.

> > 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...

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs()
  2014-01-14 13:45     ` Mark Brown
@ 2014-01-14 14:44       ` Geert Uytterhoeven
  2014-01-14 14:50         ` Mark Brown
  0 siblings, 1 reply; 9+ messages in thread
From: Geert Uytterhoeven @ 2014-01-14 14:44 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Geert Uytterhoeven

On Tue, Jan 14, 2014 at 2:45 PM, Mark Brown <broonie@kernel.org> wrote:
> On Tue, Jan 14, 2014 at 02:23:37PM +0100, Geert Uytterhoeven wrote:
>> On Tue, Jan 14, 2014 at 1:52 PM, Mark Brown <broonie@kernel.org> wrote:
>> >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.

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)...

>> > 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 -- geert@linux-m68k.org

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs()
  2014-01-14 14:44       ` Geert Uytterhoeven
@ 2014-01-14 14:50         ` Mark Brown
  2014-01-14 15:18           ` Geert Uytterhoeven
  0 siblings, 1 reply; 9+ messages in thread
From: Mark Brown @ 2014-01-14 14:50 UTC (permalink / raw)
  To: Geert Uytterhoeven; +Cc: linux-spi, linux-kernel, Geert Uytterhoeven

[-- Attachment #1: Type: text/plain, Size: 1326 bytes --]

On Tue, Jan 14, 2014 at 03:44:43PM +0100, Geert Uytterhoeven wrote:
> On Tue, Jan 14, 2014 at 2:45 PM, Mark Brown <broonie@kernel.org> wrote:

> > 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.

> >> > 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?

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs()
  2014-01-14 14:50         ` Mark Brown
@ 2014-01-14 15:18           ` Geert Uytterhoeven
  2014-01-17 18:03             ` Mark Brown
  0 siblings, 1 reply; 9+ messages in thread
From: Geert Uytterhoeven @ 2014-01-14 15:18 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Geert Uytterhoeven

Hi Mark,

On Tue, Jan 14, 2014 at 3:50 PM, Mark Brown <broonie@kernel.org> wrote:
> On Tue, Jan 14, 2014 at 03:44:43PM +0100, Geert Uytterhoeven wrote:
>> On Tue, Jan 14, 2014 at 2:45 PM, Mark Brown <broonie@kernel.org> wrote:
>> >> > 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?

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 -- geert@linux-m68k.org

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs()
  2014-01-14 15:18           ` Geert Uytterhoeven
@ 2014-01-17 18:03             ` Mark Brown
  2014-01-17 19:04               ` Geert Uytterhoeven
  0 siblings, 1 reply; 9+ messages in thread
From: Mark Brown @ 2014-01-17 18:03 UTC (permalink / raw)
  To: Geert Uytterhoeven; +Cc: linux-spi, linux-kernel, Geert Uytterhoeven

[-- Attachment #1: Type: text/plain, Size: 163 bytes --]

On Tue, Jan 14, 2014 at 04:18:06PM +0100, Geert Uytterhoeven wrote:

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

Yes.

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs()
  2014-01-17 18:03             ` Mark Brown
@ 2014-01-17 19:04               ` Geert Uytterhoeven
  0 siblings, 0 replies; 9+ messages in thread
From: Geert Uytterhoeven @ 2014-01-17 19:04 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Geert Uytterhoeven

On Fri, Jan 17, 2014 at 7:03 PM, Mark Brown <broonie@kernel.org> wrote:
>>   2. Make the documentation match the code.
>>       It seems this is what you prefer?
>
> Yes.

Thanks, will do.

-- 
Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-01-17 19:04 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-14 11:36 [PATCH/RFC] spi: core: Fix logic mismatch in spi_master.set_cs() Geert Uytterhoeven
2014-01-14 12:52 ` Mark Brown
2014-01-14 13:23   ` Geert Uytterhoeven
2014-01-14 13:45     ` Mark Brown
2014-01-14 14:44       ` Geert Uytterhoeven
2014-01-14 14:50         ` Mark Brown
2014-01-14 15:18           ` Geert Uytterhoeven
2014-01-17 18:03             ` Mark Brown
2014-01-17 19:04               ` Geert Uytterhoeven

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).