linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Elwell <phil@raspberrypi.org>
To: Linus Walleij <linus.walleij@linaro.org>,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>,
	linux-gpio@vger.kernel.org
Subject: Multiple SPI MCP23S17s sharing a CS line
Date: Wed, 27 Nov 2019 11:20:12 +0000	[thread overview]
Message-ID: <d8f95334-789e-995b-9abf-7bb15abb1daa@raspberrypi.org> (raw)

Hi,

We have a user asking for help to construct a Device Tree overlay to use
the GPIOs exported by multiple MCP23S17s sharing an SPI Chip Select 
line. This is a special feature of the MCP devices whereby the SPI bus
is treated rather like an I2C bus with each device assigned a unique
address. His problem is in constructing gpiospecs to refer to all of the
GPIOs.

The mcp23s08 driver claims to support this feature, and there is a DT
property ("microchip,spi-present-mask") to declare which addresses are
used buy the devices. I've spent an hour or so looking at the driver 
code and crawling through the kernel GPIO infrastructure, and I don't 
think it's possible. Here's my logic:

1. Although all devices that are found are presented as a single SPI 
device, they are each registered as independent gpio_chips.

2. A gpio_chip has (amongst other things) a pointer to is Device Tree 
node, a number of GPIOs, and a function that translates from the 
"gpiospec" descriptors found in the device tree to an index into the 
GPIOs exposed by the device. Note that it doesn't include the base of a 
range - all gpio_chips have GPIOs indexed from 0.

3. The mcp23s08 doesn't specify a translation function, so it inherits 
the default of_gpio_simple_xlate that checks that the first parameter is 
less than the number of GPIOs the device supports, and returns it.

4. When another DT node refers to a GPIO, it does so using a gpiospec. 
This is passed to gpiochip_find, which uses 
of_gpiochip_match_node_and_xlate to filter through all the registered 
GPIO controllers to find one which:

i) has a DT node which matches the first cell (e.g. "&gpio"),
ii) has a translation function, and
iii) the translation function succeeds.

5. Continuing point 2, all the MCP devices that share an SPI CS/CE 
inherit the same DT node, which means they will all pass test i). Test 
ii) is trivially passed by all of the devices. Since they all have the 
same translation function (of_gpiochip_match_node_and_xlate), and they 
are all given the same gpiospec to translate, and you aren't meant to 
mix different size variants, the sharing devices will also either all 
pass or all fail the test iii).

Therefore I think it's impossible to construct gpiospecs to refer to all 
of the GPIOs exported by MCP23S17s in this configuration. Please explain
where I've gone wrong.

Thanks,

Phil Elwell

             reply	other threads:[~2019-11-27 11:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-27 11:20 Phil Elwell [this message]
2019-11-27 12:36 ` Multiple SPI MCP23S17s sharing a CS line Linus Walleij
2019-11-28  4:11   ` Phil Reid
2019-11-28  9:17     ` Linus Walleij
2019-11-28 13:44       ` Jan Kundrát
2019-11-28  9:25   ` Phil Elwell
2019-11-28 12:24     ` Linus Walleij

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d8f95334-789e-995b-9abf-7bb15abb1daa@raspberrypi.org \
    --to=phil@raspberrypi.org \
    --cc=bgolaszewski@baylibre.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).