Re: [PATCH] gpio: tegra186: Add support for T186 GPIO

From: Thierry Reding
Date: Thu Nov 24 2016 - 10:02:38 EST


On Thu, Nov 24, 2016 at 12:06:16PM +0530, Laxman Dewangan wrote:
>
> On Thursday 24 November 2016 01:10 AM, Thierry Reding wrote:
> > * PGP Signed by an unknown key
> >
> > On Wed, Nov 23, 2016 at 02:25:51PM +0100, Linus Walleij wrote:
> > >
> > > This is already possible and several drivers are doing this.
> > >
> > > Everything, all kernel users and all character device users, end up
> > > calling gpiod_request().
> > It looks like I stumbled across the only case where this isn't true.
> > What I was seeing, and which ultimately led me to implement the compact
> > numberspace is that gpiochip_add_data() calls ->get_direction() directly
> > without first going through ->request(). We'd have to guard that one
> > case as well in order for this to work.
> >
> In T186, we have 8 pins per PORT and for some of ports, all pins are not
> available. Like Port A has 7 pins valid (0 to 6) and port E have 8 pins (0
> to 7). The great part is that each port has valid pins start from 0.

From what I can tell that's not true. We don't always have 8 pins per
port. That's just the definition that we use to simplify the external
numbering of GPIOs.

> So just having the number of valid pins for each port as part of SOC data
> will help to find out whether GPIO exist or not.
>
> int port = GPIO_PORT(offset);
> int pin = GPIO_PIN(offset);
>
> if (pin >= tgi->soc->port[port].valid_pins)
> return false;

Yes, that's exactly what tegra186_gpio_of_xlate() does, because its job
is to translate from the (sparse) numbering defined in the bindings to
the internal compact numbering.

>
> Similar logic can be used for APIs which can get called without
> gpio_request().

No it doesn't. There are currently a couple of cases where these
functions get called without first requesting the GPIO. These are all
internal to gpiolib as far as I can tell, but they all assume that a
GPIO with an index that's within the range from 0 to chip->ngpio will
exist.

On Tegra186 that's not the case, so we have to add code to every
callback to check whether or not a given pin exists. And the subsystem
is not equipped to deal with that properly, because it doesn't allow all
callbacks to return an error.

So, like I said in the other subthread the only way to make this sparse
number scheme work correctly is to implement ->request() and make sure
that all calls to GPIO operations are properly guarded with a call to
->request().

I still don't like very much how we'd be registering GPIOs that don't
exist physically, not only because we waste memory and CPU cycles, but
also because the driver becomes more prone to errors. If ever somebody
added new direct calls to one of the operations in the gpiolib core
without guarding them with ->request() our driver would likely break.

If we absolutely have to use the same numbering internally as in DT, we
need to at least make sure to properly handle accesses to them.

Thierry

Attachment: signature.asc
Description: PGP signature