Re: [RFC 2/2] gpiolib: Support for (output only) shared GPIO line

From: Linus Walleij
Date: Fri Nov 22 2019 - 07:23:26 EST


On Wed, Nov 20, 2019 at 2:34 PM Peter Ujfalusi <peter.ujfalusi@xxxxxx> wrote:

> This patch adds basic support for handling shared GPIO lines in the core.
> The line must be configured with a child node in DT.
> Based on the configuration the core will use different strategy to manage
> the shared line:
> refcounted low: Keep the line low as long as there is at least one low
> request is registered
> refcounted high: Keep the line high as long as there is at least one high
> request is registered
> pass through: all requests are allowed to go through without refcounting.
>
> The pass through mode is equivalent to how currently the
> GPIOD_FLAGS_BIT_NONEXCLUSIVE is handled.
>
> Signed-off-by: Peter Ujfalusi <peter.ujfalusi@xxxxxx>

This is a good start! Some ideas on how I'd like this to develop.

> drivers/gpio/gpiolib-of.c | 28 ++++++--
> drivers/gpio/gpiolib.c | 132 +++++++++++++++++++++++++++++++++++---

Please put this API under its own Kconfig option
and in its own file in
drivers/gpio/gpiolib-refcounted.c
local header in
drivers/gpio/gpiolib-refcounted.h
only built in if the appropriate Kconfig is selected.

Consumer header in
include/linux/gpio/reference-counted.h
And add external driver API to this last file.

> --- a/drivers/gpio/gpiolib-of.c

No commenting on this because as pointed out in the binding
patch I want this done by simply detecting the same GPIO
being referenced by several <&gpio N> phandles.

> diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
> index ca9bc1e4803c..0eec0857e3a8 100644
> --- a/drivers/gpio/gpiolib.h
> +++ b/drivers/gpio/gpiolib.h
> @@ -111,11 +111,18 @@ struct gpio_desc {
> #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
> #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
> #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */
> +#define FLAG_IS_SHARED 16 /* GPIO is shared */

This is a good way of flagging that this is a refcounted GPIO
I would call it FLAG_IS_REFERENCE_COUNTED as it is
more precise to what it means.

> +#define FLAG_REFCOUNT_LOW 17 /* Shared GPIO is refcounted for raw low */
> +#define FLAG_REFCOUNT_HIGH 18 /* Shared GPIO is refcounted for raw high */

Do not put this here, keep it in the local refcounted GPIO
struct gpio_refcount_desc.

> /* Connection label */
> const char *label;
> /* Name of the GPIO */
> const char *name;
> + /* Number of users of a shared GPIO */
> + int shared_users;
> + /* Reference counter for shared GPIO (low or high level) */
> + int level_refcount;

We can't expand this struct for everyone on the planet like this.

In the local header

drivers/gpio/gpiolib-refcount.h create something like:

struct gpio_refcount_desc {
struct gpio_desc *gd;
int shared_users;
int level_refcount;
};

This should be the opaque cookie returned to consumers of this new
refcounted API.

It defines the reference counted API as separate and optional from
the non-reference counted API, also using its own API.

The only effect on the core
gpiolib will be the single flag in struct gpio_desc; and some
calls into the shared code with stubs if the support for systems
that do not enable the reference counting API.

Yours,
Linus Walleij