mbox series

[0/3] spi: SPI bus multiplexer

Message ID 20190412050213.17698-1-chris.packham@alliedtelesis.co.nz
Headers show
Series spi: SPI bus multiplexer | expand

Message

Chris Packham April 12, 2019, 5:02 a.m. UTC
Hi All,

I have a hardware design where a single SPI chip select is steered by a
GPIO being asserted or de-asserted. On older kernels I was able to
(ab)use a gpio-hog and cs-gpios to deal with this.

Unfortunately recent changes have stopped my hacks from working. I've
tried adapting cs-gpios to work with my particular hardware but I came
to the realisation that the current cs-gpios support assumes a 1:1
mapping of gpio to SPI device whereas my hardware used the state of the
gpio selecting the device i.e. a 1:2 mapping.

This is my attempt to implement a driver to deal with this. One nice
property is that it is pretty much self contained. The only change to
the core SPI infrastructure is exposing a function I needed to lookup
the spi_controller instance.

Chris Packham (3):
  dt-bindings: spi: Add spi-mux-gpio
  spi: Make of_find_spi_controller_by_node visible
  spi: Add SPI bus gpio multiplexer

 .../devicetree/bindings/spi/spi-mux-gpio.txt  |  46 +++++
 drivers/spi/Kconfig                           |   7 +
 drivers/spi/Makefile                          |   1 +
 drivers/spi/spi-mux-gpio.c                    | 169 ++++++++++++++++++
 drivers/spi/spi.c                             |   7 +-
 include/linux/spi/spi.h                       |   7 +
 6 files changed, 234 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/spi/spi-mux-gpio.txt
 create mode 100644 drivers/spi/spi-mux-gpio.c

Comments

Mark Brown April 12, 2019, 8:29 a.m. UTC | #1
On Fri, Apr 12, 2019 at 05:02:10PM +1200, Chris Packham wrote:

> Unfortunately recent changes have stopped my hacks from working. I've
> tried adapting cs-gpios to work with my particular hardware but I came
> to the realisation that the current cs-gpios support assumes a 1:1
> mapping of gpio to SPI device whereas my hardware used the state of the
> gpio selecting the device i.e. a 1:2 mapping.

> This is my attempt to implement a driver to deal with this. One nice
> property is that it is pretty much self contained. The only change to
> the core SPI infrastructure is exposing a function I needed to lookup
> the spi_controller instance.

Why not implement the device that demuxes the GPIOs you're using for
chip select as a GPIO controller?  Presumably it might get used for
things other than chip selects.
Chris Packham April 28, 2019, 10:28 p.m. UTC | #2
On 12/04/19 8:29 PM, Mark Brown wrote:
> On Fri, Apr 12, 2019 at 05:02:10PM +1200, Chris Packham wrote:
> 
>> Unfortunately recent changes have stopped my hacks from working. I've
>> tried adapting cs-gpios to work with my particular hardware but I came
>> to the realisation that the current cs-gpios support assumes a 1:1
>> mapping of gpio to SPI device whereas my hardware used the state of the
>> gpio selecting the device i.e. a 1:2 mapping.
> 
>> This is my attempt to implement a driver to deal with this. One nice
>> property is that it is pretty much self contained. The only change to
>> the core SPI infrastructure is exposing a function I needed to lookup
>> the spi_controller instance.
> 
> Why not implement the device that demuxes the GPIOs you're using for
> chip select as a GPIO controller?  Presumably it might get used for
> things other than chip selects.
> 

Hmm a gpio-gpio driver. Interesting.

One other problem that I encounter is the interaction between cs-gpio 
and SPI_MASTER_GPIO_SS. Having cs-gpio automatically sets SPI_CS_HIGH 
which has the undesired side-effect that now my real chip select is 
inverted. I actually wonder if this change breaks commit 8eee6b9dd30d 
("spi: Add Flag to Enable Slave Select with GPIO Chip Select.") since 
now there is an extra inversion on the CS enable.
Mark Brown May 2, 2019, 2:45 a.m. UTC | #3
On Sun, Apr 28, 2019 at 10:28:16PM +0000, Chris Packham wrote:

> One other problem that I encounter is the interaction between cs-gpio 
> and SPI_MASTER_GPIO_SS. Having cs-gpio automatically sets SPI_CS_HIGH 
> which has the undesired side-effect that now my real chip select is 
> inverted. I actually wonder if this change breaks commit 8eee6b9dd30d 
> ("spi: Add Flag to Enable Slave Select with GPIO Chip Select.") since 
> now there is an extra inversion on the CS enable.

That sounds like a framework bug which should just be fixed - we
shouldn't be disrupting users of real chip selects when using a GPIO
chip select.  Depending on the hardware we might need a chip select
assigned that isn't connected to anything for use while the GPIOs are
doing the real work but otherwise we shouldn't be breaking things.