diff mbox

[4/4] tty/serial: sh-sci: remove uneeded IS_ERR_OR_NULL calls

Message ID CACRpkdYeFyr4yjQjDAUpPJwuD_NW2y=wXnzt1xSNoOX0SFCLmw@mail.gmail.com
State New
Headers show

Commit Message

Linus Walleij March 16, 2017, 3:18 p.m. UTC
On Mon, Mar 6, 2017 at 11:02 AM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> On Mon, Mar 06, 2017 at 10:53:27AM +0100, Geert Uytterhoeven wrote:
>> On Mon, Mar 6, 2017 at 10:30 AM, Uwe Kleine-König

>> > I wouldn't want to code this in each driver (something like:
>> >
>> >         if (IS_ENABLED(GPIOLIB) || device_is_instantiated_by_dt(dev) || device_is_instantiated_by_acpi(dev))
>> >                 gpios = mctrl_gpio_init(...);
>> >         else
>> >                 gpios = NULL;
>> >
>> > ). Putting this into GPIOLIB is the right approach, and so this is
>> > another argument for HALFGPIOLIB. This would fix mctrl_gpio_init en
>> > passant.
>>
>> Do we have platforms where DT=y || ACPI=y, but GPIOLIB=n?
>> Ah, x86 ;-)
>
> Yeah, and I think rm -r arch/x86 won't be acceptable :-) I assume you
> can also configure some arm or powerpc systems without GPIOLIB.
>
>> Anyway, for sh-sci.c, platforms either have DT and GPIOLIB, or they do not
>> need mctrl-gpio.
>
> So we're in agreement now that HALFGPIOLIB is the way to go?
> Linus, what do you think?

OK modem lines over GPIO.

So the problem is that GPIOLIB is needed (obviously) for mctrl_gpio_init() to
work properly, and then there are some stubs in
drivers/tty/serial/serial_mctrl_gpio.h
for !GPIOLIB.

And this whole discussion is all about that !GPIOLIB case really,
whether DT, ACPI, SFI or board files machine data is used doesn't
really matter.

We're talking about:

> git grep mctrl_gpio_init
drivers/tty/serial/atmel_serial.c:      atmel_port->gpios =
mctrl_gpio_init(&atmel_port->uart, 0);
drivers/tty/serial/clps711x.c:  s->gpios =
mctrl_gpio_init_noauto(&pdev->dev, 0);
drivers/tty/serial/etraxfs-uart.c:      up->gpios =
mctrl_gpio_init_noauto(&pdev->dev, 0);
drivers/tty/serial/imx.c:       sport->gpios = mctrl_gpio_init(&sport->port, 0);
drivers/tty/serial/mxs-auart.c: s->gpios = mctrl_gpio_init_noauto(dev, 0);
drivers/tty/serial/sh-sci.c:    sciport->gpios =
mctrl_gpio_init(&sciport->port, 0);

Atmel, ARM, ETRAX, ARM, ARM, Super-H, all have GPIOLIB.
Right now no x86, correct?

They actually all even do things like this in Kconfig:

config SERIAL_ATMEL
(...)
        select SERIAL_MCTRL_GPIO if GPIOLIB

What stops us from removing all the stubs in
drivers/tty/serial/serial_mctrl_gpio.h
and just make SERIAL_MCTRL_GPIO depends on GPIOLIB?

From 2ecc70acc510784d953add707f2a5acfebe484c2 Mon Sep 17 00:00:00 2001
From: Linus Walleij <linus.walleij@linaro.org>
Date: Thu, 16 Mar 2017 16:18:21 +0100
Subject: [PATCH] stab at mctrl

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/tty/serial/Kconfig             |  1 +
 drivers/tty/serial/serial_mctrl_gpio.h | 55 ----------------------------------
 2 files changed, 1 insertion(+), 55 deletions(-)

 #endif

Comments

Uwe Kleine-König March 16, 2017, 4:37 p.m. UTC | #1
On Thu, Mar 16, 2017 at 04:18:52PM +0100, Linus Walleij wrote:
> On Mon, Mar 6, 2017 at 11:02 AM, Uwe Kleine-König
> <u.kleine-koenig@pengutronix.de> wrote:
> > On Mon, Mar 06, 2017 at 10:53:27AM +0100, Geert Uytterhoeven wrote:
> >> On Mon, Mar 6, 2017 at 10:30 AM, Uwe Kleine-König
> 
> >> > I wouldn't want to code this in each driver (something like:
> >> >
> >> >         if (IS_ENABLED(GPIOLIB) || device_is_instantiated_by_dt(dev) || device_is_instantiated_by_acpi(dev))
> >> >                 gpios = mctrl_gpio_init(...);
> >> >         else
> >> >                 gpios = NULL;
> >> >
> >> > ). Putting this into GPIOLIB is the right approach, and so this is
> >> > another argument for HALFGPIOLIB. This would fix mctrl_gpio_init en
> >> > passant.
> >>
> >> Do we have platforms where DT=y || ACPI=y, but GPIOLIB=n?
> >> Ah, x86 ;-)
> >
> > Yeah, and I think rm -r arch/x86 won't be acceptable :-) I assume you
> > can also configure some arm or powerpc systems without GPIOLIB.
> >
> >> Anyway, for sh-sci.c, platforms either have DT and GPIOLIB, or they do not
> >> need mctrl-gpio.
> >
> > So we're in agreement now that HALFGPIOLIB is the way to go?
> > Linus, what do you think?
> 
> OK modem lines over GPIO.
> 
> So the problem is that GPIOLIB is needed (obviously) for mctrl_gpio_init() to
> work properly, and then there are some stubs in
> drivers/tty/serial/serial_mctrl_gpio.h
> for !GPIOLIB.
> 
> And this whole discussion is all about that !GPIOLIB case really,
> whether DT, ACPI, SFI or board files machine data is used doesn't
> really matter.
> 
> We're talking about:
> 
> > git grep mctrl_gpio_init
> drivers/tty/serial/atmel_serial.c:      atmel_port->gpios =
> mctrl_gpio_init(&atmel_port->uart, 0);
> drivers/tty/serial/clps711x.c:  s->gpios =
> mctrl_gpio_init_noauto(&pdev->dev, 0);
> drivers/tty/serial/etraxfs-uart.c:      up->gpios =
> mctrl_gpio_init_noauto(&pdev->dev, 0);
> drivers/tty/serial/imx.c:       sport->gpios = mctrl_gpio_init(&sport->port, 0);
> drivers/tty/serial/mxs-auart.c: s->gpios = mctrl_gpio_init_noauto(dev, 0);
> drivers/tty/serial/sh-sci.c:    sciport->gpios =
> mctrl_gpio_init(&sciport->port, 0);
> 
> Atmel, ARM, ETRAX, ARM, ARM, Super-H, all have GPIOLIB.
> Right now no x86, correct?
> 
> They actually all even do things like this in Kconfig:
> 
> config SERIAL_ATMEL
> (...)
>         select SERIAL_MCTRL_GPIO if GPIOLIB
> 
> What stops us from removing all the stubs in
> drivers/tty/serial/serial_mctrl_gpio.h
> and just make SERIAL_MCTRL_GPIO depends on GPIOLIB?

Would be ok for me, too. People seem to object to that though.

Best regards
Uwe
Geert Uytterhoeven March 16, 2017, 4:38 p.m. UTC | #2
Hi Linus,

On Thu, Mar 16, 2017 at 4:18 PM, Linus Walleij <linus.walleij@linaro.org> wrote:
> On Mon, Mar 6, 2017 at 11:02 AM, Uwe Kleine-König
> <u.kleine-koenig@pengutronix.de> wrote:
>> On Mon, Mar 06, 2017 at 10:53:27AM +0100, Geert Uytterhoeven wrote:
>>> On Mon, Mar 6, 2017 at 10:30 AM, Uwe Kleine-König
>>> > I wouldn't want to code this in each driver (something like:
>>> >
>>> >         if (IS_ENABLED(GPIOLIB) || device_is_instantiated_by_dt(dev) || device_is_instantiated_by_acpi(dev))
>>> >                 gpios = mctrl_gpio_init(...);
>>> >         else
>>> >                 gpios = NULL;
>>> >
>>> > ). Putting this into GPIOLIB is the right approach, and so this is
>>> > another argument for HALFGPIOLIB. This would fix mctrl_gpio_init en
>>> > passant.
>>>
>>> Do we have platforms where DT=y || ACPI=y, but GPIOLIB=n?
>>> Ah, x86 ;-)
>>
>> Yeah, and I think rm -r arch/x86 won't be acceptable :-) I assume you
>> can also configure some arm or powerpc systems without GPIOLIB.
>>
>>> Anyway, for sh-sci.c, platforms either have DT and GPIOLIB, or they do not
>>> need mctrl-gpio.
>>
>> So we're in agreement now that HALFGPIOLIB is the way to go?
>> Linus, what do you think?
>
> OK modem lines over GPIO.
>
> So the problem is that GPIOLIB is needed (obviously) for mctrl_gpio_init() to
> work properly, and then there are some stubs in
> drivers/tty/serial/serial_mctrl_gpio.h
> for !GPIOLIB.
>
> And this whole discussion is all about that !GPIOLIB case really,
> whether DT, ACPI, SFI or board files machine data is used doesn't
> really matter.
>
> We're talking about:
>
>> git grep mctrl_gpio_init
> drivers/tty/serial/atmel_serial.c:      atmel_port->gpios =
> mctrl_gpio_init(&atmel_port->uart, 0);
> drivers/tty/serial/clps711x.c:  s->gpios =
> mctrl_gpio_init_noauto(&pdev->dev, 0);
> drivers/tty/serial/etraxfs-uart.c:      up->gpios =
> mctrl_gpio_init_noauto(&pdev->dev, 0);
> drivers/tty/serial/imx.c:       sport->gpios = mctrl_gpio_init(&sport->port, 0);
> drivers/tty/serial/mxs-auart.c: s->gpios = mctrl_gpio_init_noauto(dev, 0);
> drivers/tty/serial/sh-sci.c:    sciport->gpios =
> mctrl_gpio_init(&sciport->port, 0);
>
> Atmel, ARM, ETRAX, ARM, ARM, Super-H, all have GPIOLIB.
> Right now no x86, correct?

It's not that black-and-white. Some of SuperH have GPIOLIB, other parts don't.

> They actually all even do things like this in Kconfig:
>
> config SERIAL_ATMEL
> (...)
>         select SERIAL_MCTRL_GPIO if GPIOLIB
>
> What stops us from removing all the stubs in
> drivers/tty/serial/serial_mctrl_gpio.h
> and just make SERIAL_MCTRL_GPIO depends on GPIOLIB?

Removing the stubs implies adding #ifdefs to the drivers that need
to handle the !SERIAL_MCTRL_GPIO case.

E.g. I don't want to break the sh-sci serial driver on SuperH platforms that
(a) don't select GPIOLIB, and
(b) don't use mtrl_gpio.

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
Geert Uytterhoeven March 20, 2017, 9:56 a.m. UTC | #3
Hi Linus,

On Thu, Mar 16, 2017 at 5:38 PM, Geert Uytterhoeven
<geert@linux-m68k.org> wrote:
>> They actually all even do things like this in Kconfig:
>>
>> config SERIAL_ATMEL
>> (...)
>>         select SERIAL_MCTRL_GPIO if GPIOLIB
>>
>> What stops us from removing all the stubs in
>> drivers/tty/serial/serial_mctrl_gpio.h
>> and just make SERIAL_MCTRL_GPIO depends on GPIOLIB?
>
> Removing the stubs implies adding #ifdefs to the drivers that need
> to handle the !SERIAL_MCTRL_GPIO case.
>
> E.g. I don't want to break the sh-sci serial driver on SuperH platforms that
> (a) don't select GPIOLIB, and
> (b) don't use mtrl_gpio.

Alternatively, after commit 22c403676dbbb7c6 ("gpio: return NULL from
gpiod_get_optional when GPIOLIB is disabled") we might just drop the
dependency of SERIAL_MCTRL_GPIO on GPIOLIB? Then no special handling
is needed in drivers, as all GPIOs will just be considered not present.

Documentation/gpio/consumer.txt rightfully sates:
| Note that gpio_get*_optional() functions (and their managed variants), unlike
| the rest of gpiolib API, also return NULL when gpiolib support is disabled.
| This is helpful to driver authors, since they do not need to special case
| -ENOSYS return codes.  System integrators should however be careful to enable
| gpiolib on systems that need it.

drivers/tty/serial/serial_mctrl_gpio.o already compiles fine if
CONFIG_GPIOLIB=n, which reduces its size by ca. 25%.

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
Geert Uytterhoeven March 20, 2017, 10:03 a.m. UTC | #4
On Mon, Mar 20, 2017 at 10:56 AM, Geert Uytterhoeven
<geert@linux-m68k.org> wrote:
> drivers/tty/serial/serial_mctrl_gpio.o already compiles fine if
> CONFIG_GPIOLIB=n, which reduces its size by ca. 25%.

That is: after removing the #ifdef and dummies in serial_mctrl_gpio.h

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
Uwe Kleine-König March 20, 2017, 10:31 a.m. UTC | #5
Hello Geert,

On Mon, Mar 20, 2017 at 10:56:14AM +0100, Geert Uytterhoeven wrote:
> Documentation/gpio/consumer.txt rightfully sates:
> | Note that gpio_get*_optional() functions (and their managed variants), unlike
> | the rest of gpiolib API, also return NULL when gpiolib support is disabled.
> | This is helpful to driver authors, since they do not need to special case
> | -ENOSYS return codes.  System integrators should however be careful to enable
> | gpiolib on systems that need it.

I cannot find this paragraph in Documentation/gpio/consumer.txt:

	$ git grep -e 'ENOSYS' v4.11-rc3 -- Documentation/gpio/
	<void>

Best regards
Uwe
Geert Uytterhoeven March 20, 2017, 10:38 a.m. UTC | #6
Hi Uwe,

On Mon, Mar 20, 2017 at 11:31 AM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> On Mon, Mar 20, 2017 at 10:56:14AM +0100, Geert Uytterhoeven wrote:
>> Documentation/gpio/consumer.txt rightfully sates:
>> | Note that gpio_get*_optional() functions (and their managed variants), unlike
>> | the rest of gpiolib API, also return NULL when gpiolib support is disabled.
>> | This is helpful to driver authors, since they do not need to special case
>> | -ENOSYS return codes.  System integrators should however be careful to enable
>> | gpiolib on systems that need it.
>
> I cannot find this paragraph in Documentation/gpio/consumer.txt:
>
>         $ git grep -e 'ENOSYS' v4.11-rc3 -- Documentation/gpio/
>         <void>

It was added by commit 22c403676dbbb7c6 ("gpio: return NULL from
gpiod_get_optional when GPIOLIB is disabled")

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
Uwe Kleine-König March 20, 2017, 11:07 a.m. UTC | #7
Hello Geert,

On Mon, Mar 20, 2017 at 11:38:52AM +0100, Geert Uytterhoeven wrote:
> On Mon, Mar 20, 2017 at 11:31 AM, Uwe Kleine-König
> <u.kleine-koenig@pengutronix.de> wrote:
> > On Mon, Mar 20, 2017 at 10:56:14AM +0100, Geert Uytterhoeven wrote:
> >> Documentation/gpio/consumer.txt rightfully sates:
> >> | Note that gpio_get*_optional() functions (and their managed variants), unlike
> >> | the rest of gpiolib API, also return NULL when gpiolib support is disabled.
> >> | This is helpful to driver authors, since they do not need to special case
> >> | -ENOSYS return codes.  System integrators should however be careful to enable
> >> | gpiolib on systems that need it.
> >
> > I cannot find this paragraph in Documentation/gpio/consumer.txt:
> >
> >         $ git grep -e 'ENOSYS' v4.11-rc3 -- Documentation/gpio/
> >         <void>
> 
> It was added by commit 22c403676dbbb7c6 ("gpio: return NULL from
> gpiod_get_optional when GPIOLIB is disabled")

Ah, that's in next.

I still think this is wrong and I'm a bit disappointed that Linus merged
this patch (without saying so in the thread even) as I thought Linus and
I agreed on this being a bad idea.

Best regards
Uwe
Linus Walleij March 23, 2017, 9:32 a.m. UTC | #8
On Mon, Mar 20, 2017 at 12:07 PM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> Hello Geert,
>
> On Mon, Mar 20, 2017 at 11:38:52AM +0100, Geert Uytterhoeven wrote:
>> On Mon, Mar 20, 2017 at 11:31 AM, Uwe Kleine-König
>> <u.kleine-koenig@pengutronix.de> wrote:
>> > On Mon, Mar 20, 2017 at 10:56:14AM +0100, Geert Uytterhoeven wrote:
>> >> Documentation/gpio/consumer.txt rightfully sates:
>> >> | Note that gpio_get*_optional() functions (and their managed variants), unlike
>> >> | the rest of gpiolib API, also return NULL when gpiolib support is disabled.
>> >> | This is helpful to driver authors, since they do not need to special case
>> >> | -ENOSYS return codes.  System integrators should however be careful to enable
>> >> | gpiolib on systems that need it.
>> >
>> > I cannot find this paragraph in Documentation/gpio/consumer.txt:
>> >
>> >         $ git grep -e 'ENOSYS' v4.11-rc3 -- Documentation/gpio/
>> >         <void>
>>
>> It was added by commit 22c403676dbbb7c6 ("gpio: return NULL from
>> gpiod_get_optional when GPIOLIB is disabled")
>
> Ah, that's in next.
>
> I still think this is wrong and I'm a bit disappointed that Linus merged
> this patch (without saying so in the thread even) as I thought Linus and
> I agreed on this being a bad idea.

I think it is not good, but what we have before this patch is worse.

I.e. it is the lesser evil.

Before this patch the API is inconsistent: it gives NULL if GPIOLIB
is defined and -ENOSYS if GPIOLIB is not defined.

After this patch it gives NULL in both cases, and that is at least
consistent.

It would be great if someone steps in and fix it so as to return
relevant errors (or error pointers rather) in both cases. However
I guess it will lead a bunch of fallouts in the consumers, albeit
they are not super-many.

Yours,
Linus Walleij
Uwe Kleine-König March 23, 2017, 10:10 a.m. UTC | #9
On Thu, Mar 23, 2017 at 10:32:01AM +0100, Linus Walleij wrote:
> On Mon, Mar 20, 2017 at 12:07 PM, Uwe Kleine-König
> <u.kleine-koenig@pengutronix.de> wrote:
> > Hello Geert,
> >
> > On Mon, Mar 20, 2017 at 11:38:52AM +0100, Geert Uytterhoeven wrote:
> >> On Mon, Mar 20, 2017 at 11:31 AM, Uwe Kleine-König
> >> <u.kleine-koenig@pengutronix.de> wrote:
> >> > On Mon, Mar 20, 2017 at 10:56:14AM +0100, Geert Uytterhoeven wrote:
> >> >> Documentation/gpio/consumer.txt rightfully sates:
> >> >> | Note that gpio_get*_optional() functions (and their managed variants), unlike
> >> >> | the rest of gpiolib API, also return NULL when gpiolib support is disabled.
> >> >> | This is helpful to driver authors, since they do not need to special case
> >> >> | -ENOSYS return codes.  System integrators should however be careful to enable
> >> >> | gpiolib on systems that need it.
> >> >
> >> > I cannot find this paragraph in Documentation/gpio/consumer.txt:
> >> >
> >> >         $ git grep -e 'ENOSYS' v4.11-rc3 -- Documentation/gpio/
> >> >         <void>
> >>
> >> It was added by commit 22c403676dbbb7c6 ("gpio: return NULL from
> >> gpiod_get_optional when GPIOLIB is disabled")
> >
> > Ah, that's in next.
> >
> > I still think this is wrong and I'm a bit disappointed that Linus merged
> > this patch (without saying so in the thread even) as I thought Linus and
> > I agreed on this being a bad idea.
> 
> I think it is not good, but what we have before this patch is worse.
> 
> I.e. it is the lesser evil.
> 
> Before this patch the API is inconsistent: it gives NULL if GPIOLIB
> is defined and -ENOSYS if GPIOLIB is not defined.

So old is: it worked as intended with GPIOLIB and produced an error
without GPIOLIB even if no GPIO is there and NULL would be right.
 
> After this patch it gives NULL in both cases, and that is at least
> consistent.

And new is: with GPIOLIB it still works as intended and gives you NULL
even if a GPIO is there and so -ENOSYS would be right.

I admit that most of the time with GPIOLIB NULL is the right answer
because probably there is no GPIO to handle. But if there is a GPIO you
run in hard to debug problems instead of being able to see the error
code and act accordingly.

write(2) and close(2) succeed most of the time, too. Still it's not a
good idea to not check the return value. Or let the kernel return
success unconditionally.

So you exchanged many obvious and easy to fix problems with a few hard
ones. I don't agree that's a good idea, but you seem to be willing to
try it. Good luck.

Best regards
Uwe
Geert Uytterhoeven March 23, 2017, 10:20 a.m. UTC | #10
Hi Uwe,

On Thu, Mar 23, 2017 at 11:10 AM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> On Thu, Mar 23, 2017 at 10:32:01AM +0100, Linus Walleij wrote:
>> On Mon, Mar 20, 2017 at 12:07 PM, Uwe Kleine-König
>> <u.kleine-koenig@pengutronix.de> wrote:
>> > On Mon, Mar 20, 2017 at 11:38:52AM +0100, Geert Uytterhoeven wrote:
>> >> On Mon, Mar 20, 2017 at 11:31 AM, Uwe Kleine-König
>> >> <u.kleine-koenig@pengutronix.de> wrote:
>> >> > On Mon, Mar 20, 2017 at 10:56:14AM +0100, Geert Uytterhoeven wrote:
>> >> >> Documentation/gpio/consumer.txt rightfully sates:
>> >> >> | Note that gpio_get*_optional() functions (and their managed variants), unlike
>> >> >> | the rest of gpiolib API, also return NULL when gpiolib support is disabled.
>> >> >> | This is helpful to driver authors, since they do not need to special case
>> >> >> | -ENOSYS return codes.  System integrators should however be careful to enable
>> >> >> | gpiolib on systems that need it.
>> >> >
>> >> > I cannot find this paragraph in Documentation/gpio/consumer.txt:
>> >> >
>> >> >         $ git grep -e 'ENOSYS' v4.11-rc3 -- Documentation/gpio/
>> >> >         <void>
>> >>
>> >> It was added by commit 22c403676dbbb7c6 ("gpio: return NULL from
>> >> gpiod_get_optional when GPIOLIB is disabled")
>> >
>> > Ah, that's in next.
>> >
>> > I still think this is wrong and I'm a bit disappointed that Linus merged
>> > this patch (without saying so in the thread even) as I thought Linus and
>> > I agreed on this being a bad idea.
>>
>> I think it is not good, but what we have before this patch is worse.
>>
>> I.e. it is the lesser evil.
>>
>> Before this patch the API is inconsistent: it gives NULL if GPIOLIB
>> is defined and -ENOSYS if GPIOLIB is not defined.
>
> So old is: it worked as intended with GPIOLIB and produced an error
> without GPIOLIB even if no GPIO is there and NULL would be right.
>
>> After this patch it gives NULL in both cases, and that is at least
>> consistent.
>
> And new is: with GPIOLIB it still works as intended and gives you NULL
> even if a GPIO is there and so -ENOSYS would be right.

If I forget to enable a driver, the corresponding class device is also not
there.

> I admit that most of the time with GPIOLIB NULL is the right answer
> because probably there is no GPIO to handle. But if there is a GPIO you
> run in hard to debug problems instead of being able to see the error
> code and act accordingly.

But having the error breaks setups where the GPIO is optional and does
not exist.

Make sure to enable all drivers and subsystems you need when building
your kernel. That's always true. And may indeed be hard to debug (e.g. what
kernel options do I need to make systemd work?).

> write(2) and close(2) succeed most of the time, too. Still it's not a
> good idea to not check the return value. Or let the kernel return
> success unconditionally.

Writing all bytes passed in the buffer is "optional" in another sense than
an "optional" GPIO: you must retry the write, while you can continue if
an optional GPIO is not present.

> So you exchanged many obvious and easy to fix problems with a few hard
> ones. I don't agree that's a good idea, but you seem to be willing to
> try it. Good luck.

Yeah, before drivers had to explicitly ignore -ENOSYS if they want to
support platforms with and without GPIOLIB. Bad...

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
Uwe Kleine-König March 23, 2017, 11:11 a.m. UTC | #11
Hello,

On Thu, Mar 23, 2017 at 11:20:39AM +0100, Geert Uytterhoeven wrote:
> But having the error breaks setups where the GPIO is optional and does
> not exist.

so the right way forward is to check harder in the situation where
-ENOSYS was returned before to determine if there is really no GPIO to
be used. "Oh, there are hints that there is no GPIO (GPIOLIB=n), so lets
assume there isn't." is wrong.

Can we please properly fix the problem instead of papering over it?

> Make sure to enable all drivers and subsystems you need when building
> your kernel. That's always true. And may indeed be hard to debug (e.g. what
> kernel options do I need to make systemd work?).

It's worse here. If you forget to enable a driver the device isn't bound
and that's obvious to diagnose. When ignoring an optional GPIO there
might be a device that claims to work but fails to do so. (e.g. you
write to memory, write() returns 0, but the data never landed there.)

> > write(2) and close(2) succeed most of the time, too. Still it's not a
> > good idea to not check the return value. Or let the kernel return
> > success unconditionally.
> 
> Writing all bytes passed in the buffer is "optional" in another sense than
> an "optional" GPIO: you must retry the write, while you can continue if
> an optional GPIO is not present.

And that is the point. You can continue *iff* the optional GPIO is not
present. The patch in question removes the ability to determine if that
GPIO is present and claims it is not present.

> > So you exchanged many obvious and easy to fix problems with a few hard
> > ones. I don't agree that's a good idea, but you seem to be willing to
> > try it. Good luck.
> 
> Yeah, before drivers had to explicitly ignore -ENOSYS if they want to
> support platforms with and without GPIOLIB. Bad...

Doing things right is sometimes not maximally easy. But that is no
excuse to do it wrong.

Best regards
Uwe
Geert Uytterhoeven March 23, 2017, 12:03 p.m. UTC | #12
Hi Uwe,

On Thu, Mar 23, 2017 at 12:11 PM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
>> Make sure to enable all drivers and subsystems you need when building
>> your kernel. That's always true. And may indeed be hard to debug (e.g. what
>> kernel options do I need to make systemd work?).
>
> It's worse here. If you forget to enable a driver the device isn't bound
> and that's obvious to diagnose. When ignoring an optional GPIO there
> might be a device that claims to work but fails to do so. (e.g. you
> write to memory, write() returns 0, but the data never landed there.)
>
>> > write(2) and close(2) succeed most of the time, too. Still it's not a
>> > good idea to not check the return value. Or let the kernel return
>> > success unconditionally.
>>
>> Writing all bytes passed in the buffer is "optional" in another sense than
>> an "optional" GPIO: you must retry the write, while you can continue if
>> an optional GPIO is not present.
>
> And that is the point. You can continue *iff* the optional GPIO is not
> present. The patch in question removes the ability to determine if that
> GPIO is present and claims it is not present.

If you forget to enable a driver/subsystem, you sometimes cannot determine
if the device is present or not neither.

Hence it boils down to "knowing" if there is a GPIO or not.
So, when can there be a GPIO?
  1. The GPIO is described in DT.
     => Not an issue, as DT GPIO implies GPIOLIB,
  2. The GPIO is described in legacy platform data.
     => The platform code should make sure GPIOLIB is selected when needed.

Issue solved?

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
Uwe Kleine-König March 23, 2017, 12:34 p.m. UTC | #13
On Thu, Mar 23, 2017 at 01:03:56PM +0100, Geert Uytterhoeven wrote:
> Hi Uwe,
> 
> On Thu, Mar 23, 2017 at 12:11 PM, Uwe Kleine-König
> <u.kleine-koenig@pengutronix.de> wrote:
> >> Make sure to enable all drivers and subsystems you need when building
> >> your kernel. That's always true. And may indeed be hard to debug (e.g. what
> >> kernel options do I need to make systemd work?).
> >
> > It's worse here. If you forget to enable a driver the device isn't bound
> > and that's obvious to diagnose. When ignoring an optional GPIO there
> > might be a device that claims to work but fails to do so. (e.g. you
> > write to memory, write() returns 0, but the data never landed there.)
> >
> >> > write(2) and close(2) succeed most of the time, too. Still it's not a
> >> > good idea to not check the return value. Or let the kernel return
> >> > success unconditionally.
> >>
> >> Writing all bytes passed in the buffer is "optional" in another sense than
> >> an "optional" GPIO: you must retry the write, while you can continue if
> >> an optional GPIO is not present.
> >
> > And that is the point. You can continue *iff* the optional GPIO is not
> > present. The patch in question removes the ability to determine if that
> > GPIO is present and claims it is not present.
> 
> If you forget to enable a driver/subsystem, you sometimes cannot determine
> if the device is present or not neither.
> 
> Hence it boils down to "knowing" if there is a GPIO or not.
> So, when can there be a GPIO?
>   1. The GPIO is described in DT.
>      => Not an issue, as DT GPIO implies GPIOLIB,
>   2. The GPIO is described in legacy platform data.
>      => The platform code should make sure GPIOLIB is selected when needed.
> 
> Issue solved?

I like it better to not rely on platform code to do the right thing.

Maybe we can make gpiod_get_optional look like this:

	if (!dev->of_node && isnt_a_acpi_device(dev) && !IS_ENABLED(GPIOLIB))
		return NULL;
	else
		return -ENOSYS;

I don't know how isnt_a_acpi_device looks like, probably it involves
CONFIG_ACPI and/or dev->acpi_node.

This should be safe and still comfortable for legacy platforms, isn't it?

Best regards
Uwe
Geert Uytterhoeven March 23, 2017, 12:44 p.m. UTC | #14
Hi Uwe,

On Thu, Mar 23, 2017 at 1:34 PM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> On Thu, Mar 23, 2017 at 01:03:56PM +0100, Geert Uytterhoeven wrote:
>> On Thu, Mar 23, 2017 at 12:11 PM, Uwe Kleine-König
>> <u.kleine-koenig@pengutronix.de> wrote:
>> >> Make sure to enable all drivers and subsystems you need when building
>> >> your kernel. That's always true. And may indeed be hard to debug (e.g. what
>> >> kernel options do I need to make systemd work?).
>> >
>> > It's worse here. If you forget to enable a driver the device isn't bound
>> > and that's obvious to diagnose. When ignoring an optional GPIO there
>> > might be a device that claims to work but fails to do so. (e.g. you
>> > write to memory, write() returns 0, but the data never landed there.)
>> >
>> >> > write(2) and close(2) succeed most of the time, too. Still it's not a
>> >> > good idea to not check the return value. Or let the kernel return
>> >> > success unconditionally.
>> >>
>> >> Writing all bytes passed in the buffer is "optional" in another sense than
>> >> an "optional" GPIO: you must retry the write, while you can continue if
>> >> an optional GPIO is not present.
>> >
>> > And that is the point. You can continue *iff* the optional GPIO is not
>> > present. The patch in question removes the ability to determine if that
>> > GPIO is present and claims it is not present.
>>
>> If you forget to enable a driver/subsystem, you sometimes cannot determine
>> if the device is present or not neither.
>>
>> Hence it boils down to "knowing" if there is a GPIO or not.
>> So, when can there be a GPIO?
>>   1. The GPIO is described in DT.
>>      => Not an issue, as DT GPIO implies GPIOLIB,
>>   2. The GPIO is described in legacy platform data.
>>      => The platform code should make sure GPIOLIB is selected when needed.
>>
>> Issue solved?
>
> I like it better to not rely on platform code to do the right thing.

;-)

> Maybe we can make gpiod_get_optional look like this:
>
>         if (!dev->of_node && isnt_a_acpi_device(dev) && !IS_ENABLED(GPIOLIB))
>                 return NULL;
>         else
>                 return -ENOSYS;
>
> I don't know how isnt_a_acpi_device looks like, probably it involves
> CONFIG_ACPI and/or dev->acpi_node.
>
> This should be safe and still comfortable for legacy platforms, isn't it?

Yes, that should do the trick.

No feedback from me about ACPI.

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
Linus Walleij March 23, 2017, 1:37 p.m. UTC | #15
On Thu, Mar 23, 2017 at 11:10 AM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:

> So you exchanged many obvious and easy to fix problems with a few hard
> ones. I don't agree that's a good idea, but you seem to be willing to
> try it. Good luck.

I think instead of going to sarcastic remarks you can say you NACK the
patch and suggest that it be reverted?

The problem I have here as maintainer is that both you and Dmitry are
very smart people and I have a great deal of trust invested in both of you.

When two valued contributors give me very different advice I get a bit
confused and maybe the best option is not to change anything at all
right now, and just revert Dmitry's patch.

git grep -e 'gpio.*optional(' | wc -l
gives 154 use sites outside drivers/gpio, so it is not impossible to fix
this if we want a good and strict order to it. I'm just a bit overworked to
do it myself right now.

What do you all say, is it better to revert Dmitry's patch and instead go
around and fix the consumers to do it correctly everywhere, after
hammering down the exact semantics?

Yours,
Linus Walleij
Linus Walleij March 23, 2017, 1:41 p.m. UTC | #16
On Thu, Mar 23, 2017 at 1:34 PM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:

> Maybe we can make gpiod_get_optional look like this:
>
>         if (!dev->of_node && isnt_a_acpi_device(dev) && !IS_ENABLED(GPIOLIB))
>                 return NULL;
>         else
>                 return -ENOSYS;
>
> I don't know how isnt_a_acpi_device looks like, probably it involves
> CONFIG_ACPI and/or dev->acpi_node.
>
> This should be safe and still comfortable for legacy platforms, isn't it?

I like the looks of this.

Can we revert Dmitry's patch and apply something like this instead?

Dmitry, how do you feel about this?

Yours,
Linus Walleij
Dmitry Torokhov March 23, 2017, 2:43 p.m. UTC | #17
On Thu, Mar 23, 2017 at 02:41:53PM +0100, Linus Walleij wrote:
> On Thu, Mar 23, 2017 at 1:34 PM, Uwe Kleine-König
> <u.kleine-koenig@pengutronix.de> wrote:
> 
> > Maybe we can make gpiod_get_optional look like this:
> >
> >         if (!dev->of_node && isnt_a_acpi_device(dev) && !IS_ENABLED(GPIOLIB))
> >                 return NULL;
> >         else
> >                 return -ENOSYS;
> >
> > I don't know how isnt_a_acpi_device looks like, probably it involves
> > CONFIG_ACPI and/or dev->acpi_node.
> >
> > This should be safe and still comfortable for legacy platforms, isn't it?
> 
> I like the looks of this.
> 
> Can we revert Dmitry's patch and apply something like this instead?
> 
> Dmitry, how do you feel about this?

I frankly do not see the point. It still makes driver code more complex
for no good reason. I also think that not having optional GPIO is not an
error, so returning value from error space is not correct. NULL is value
from another space altogether.

Uwe seems to be concerned about case that I find extremely unlikely. We
are talking about a system that does not have GPIO support and behaves
just fine, with the exception that it actually has (physically) a
*single* GPIO, and that GPIO happens to be optional in a single driver,
but in this particular system is actually needed (but that need
manifests in a non-obvious way). And we have system integrator that has
no idea what they are doing (no schematic, etc).

I think that if there is one optional GPIO there will be mandatiry GPIOs
in such system as well and selection of GPIOLIB will be forced early on
in board bringup.

Thanks.
Dmitry Torokhov March 23, 2017, 3:44 p.m. UTC | #18
On Thu, Mar 23, 2017 at 07:43:25AM -0700, Dmitry Torokhov wrote:
> On Thu, Mar 23, 2017 at 02:41:53PM +0100, Linus Walleij wrote:
> > On Thu, Mar 23, 2017 at 1:34 PM, Uwe Kleine-König
> > <u.kleine-koenig@pengutronix.de> wrote:
> > 
> > > Maybe we can make gpiod_get_optional look like this:
> > >
> > >         if (!dev->of_node && isnt_a_acpi_device(dev) && !IS_ENABLED(GPIOLIB))
> > >                 return NULL;
> > >         else
> > >                 return -ENOSYS;
> > >
> > > I don't know how isnt_a_acpi_device looks like, probably it involves
> > > CONFIG_ACPI and/or dev->acpi_node.
> > >
> > > This should be safe and still comfortable for legacy platforms, isn't it?
> > 
> > I like the looks of this.
> > 
> > Can we revert Dmitry's patch and apply something like this instead?
> > 
> > Dmitry, how do you feel about this?
> 
> I frankly do not see the point. It still makes driver code more complex
> for no good reason. I also think that not having optional GPIO is not an
> error, so returning value from error space is not correct. NULL is value
> from another space altogether.
> 
> Uwe seems to be concerned about case that I find extremely unlikely. We
> are talking about a system that does not have GPIO support and behaves
> just fine, with the exception that it actually has (physically) a
> *single* GPIO, and that GPIO happens to be optional in a single driver,
> but in this particular system is actually needed (but that need
> manifests in a non-obvious way). And we have system integrator that has
> no idea what they are doing (no schematic, etc).
> 
> I think that if there is one optional GPIO there will be mandatiry GPIOs
> in such system as well and selection of GPIOLIB will be forced early on
> in board bringup.

One more thing: if we keep reporting -ENOSYS in case of !CONFIG_GPIOLIB,
then most of the non platform-sepcific drivers will eventually gain code
silently coping with this -ENOSYS:

	data->gpiod = gpiod_getptional(...);
	if (IS_ERR(data->gpiod)) {
		error = PTR_ERR(data->gpiod);
		if (error != -ENOSYS)
			return error;

		data->gpiod = NULL; /* This GPIO _is_ optional */
	}

which will negate Uwe's claim that it will help debugging issues.

Thanks.
Dmitry Torokhov March 23, 2017, 3:55 p.m. UTC | #19
On Thu, Mar 23, 2017 at 12:11:06PM +0100, Uwe Kleine-König wrote:
> Hello,
> 
> On Thu, Mar 23, 2017 at 11:20:39AM +0100, Geert Uytterhoeven wrote:
> > But having the error breaks setups where the GPIO is optional and does
> > not exist.
> 
> so the right way forward is to check harder in the situation where
> -ENOSYS was returned before to determine if there is really no GPIO to
> be used. "Oh, there are hints that there is no GPIO (GPIOLIB=n), so lets
> assume there isn't." is wrong.
> 
> Can we please properly fix the problem instead of papering over it?

I think I once already said what would need to _attempt_ to fix it
"properly". You would need to implement custom parsing of ACPI tables in
GPIOLIB (what if they disable ACPI by mistake?), do the same for OF,
call board's manufacturer hotline to ensure that they indeed did not
forget to describe GPIOs, etc, etc.

Or you could trust that person responsible for selecting kernel
configuration has a clue, and if GPIOLIB is disabled it was disabled for
a reason.
Uwe Kleine-König March 23, 2017, 7:10 p.m. UTC | #20
On Thu, Mar 23, 2017 at 08:44:41AM -0700, Dmitry Torokhov wrote:
> On Thu, Mar 23, 2017 at 07:43:25AM -0700, Dmitry Torokhov wrote:
> > On Thu, Mar 23, 2017 at 02:41:53PM +0100, Linus Walleij wrote:
> > > On Thu, Mar 23, 2017 at 1:34 PM, Uwe Kleine-König
> > > <u.kleine-koenig@pengutronix.de> wrote:
> > > 
> > > > Maybe we can make gpiod_get_optional look like this:
> > > >
> > > >         if (!dev->of_node && isnt_a_acpi_device(dev) && !IS_ENABLED(GPIOLIB))
> > > >                 return NULL;
> > > >         else
> > > >                 return -ENOSYS;
> > > >
> > > > I don't know how isnt_a_acpi_device looks like, probably it involves
> > > > CONFIG_ACPI and/or dev->acpi_node.
> > > >
> > > > This should be safe and still comfortable for legacy platforms, isn't it?
> > > 
> > > I like the looks of this.
> > > 
> > > Can we revert Dmitry's patch and apply something like this instead?
> > > 
> > > Dmitry, how do you feel about this?
> > 
> > I frankly do not see the point. It still makes driver code more complex

Note that this code is in the gpio header, and not in driver code. So
the driver just does

	gpiod = gpiod_get_optional(...)
	if (IS_ERR(gpiod))
		return PTR_ERR(gpiod);

(as it is supposed to do now). I think that's nice.

> > for no good reason. I also think that not having optional GPIO is not an
> > error, so returning value from error space is not correct. NULL is value
> > from another space altogether.

It seems you didn't understand my concern.

> > Uwe seems to be concerned about case that I find extremely unlikely. We
> > are talking about a system that does not have GPIO support and behaves
> > just fine, with the exception that it actually has (physically) a
> > *single* GPIO, and that GPIO happens to be optional in a single driver,
> > but in this particular system is actually needed (but that need
> > manifests in a non-obvious way). And we have system integrator that has
> > no idea what they are doing (no schematic, etc).

IMHO this is not an excuse to have lax error checking.

> > I think that if there is one optional GPIO there will be mandatiry GPIOs
> > in such system as well and selection of GPIOLIB will be forced early on
> > in board bringup.
> 
> One more thing: if we keep reporting -ENOSYS in case of !CONFIG_GPIOLIB,
> then most of the non platform-sepcific drivers will eventually gain code
> silently coping with this -ENOSYS:
> 
> 	data->gpiod = gpiod_getptional(...);
> 	if (IS_ERR(data->gpiod)) {
> 		error = PTR_ERR(data->gpiod);
> 		if (error != -ENOSYS)
> 			return error;
> 
> 		data->gpiod = NULL; /* This GPIO _is_ optional */

Do you agree that this is wrong? Yes, it behaves right for most cases.
But there are cases where it behaves wrong and so it needs fixing.

Best regards
Uwe
Dmitry Torokhov March 23, 2017, 7:58 p.m. UTC | #21
On Thu, Mar 23, 2017 at 08:10:20PM +0100, Uwe Kleine-König wrote:
> On Thu, Mar 23, 2017 at 08:44:41AM -0700, Dmitry Torokhov wrote:
> > On Thu, Mar 23, 2017 at 07:43:25AM -0700, Dmitry Torokhov wrote:
> > > On Thu, Mar 23, 2017 at 02:41:53PM +0100, Linus Walleij wrote:
> > > > On Thu, Mar 23, 2017 at 1:34 PM, Uwe Kleine-König
> > > > <u.kleine-koenig@pengutronix.de> wrote:
> > > > 
> > > > > Maybe we can make gpiod_get_optional look like this:
> > > > >
> > > > >         if (!dev->of_node && isnt_a_acpi_device(dev) && !IS_ENABLED(GPIOLIB))
> > > > >                 return NULL;
> > > > >         else
> > > > >                 return -ENOSYS;
> > > > >
> > > > > I don't know how isnt_a_acpi_device looks like, probably it involves
> > > > > CONFIG_ACPI and/or dev->acpi_node.
> > > > >
> > > > > This should be safe and still comfortable for legacy platforms, isn't it?
> > > > 
> > > > I like the looks of this.
> > > > 
> > > > Can we revert Dmitry's patch and apply something like this instead?
> > > > 
> > > > Dmitry, how do you feel about this?
> > > 
> > > I frankly do not see the point. It still makes driver code more complex
> 
> Note that this code is in the gpio header, and not in driver code. So
> the driver just does
> 
> 	gpiod = gpiod_get_optional(...)
> 	if (IS_ERR(gpiod))
> 		return PTR_ERR(gpiod);
> 
> (as it is supposed to do now). I think that's nice.

Except that it breaks if !CONFIG_GPIOLIB which is legitimate config in
many cases. Can I have a DT platform or ACPI platform that does not
expose any GPIOs for kernel to control and thus want to disable GPIOLIB?
I can't see why not.

> 
> > > for no good reason. I also think that not having optional GPIO is not an
> > > error, so returning value from error space is not correct. NULL is value
> > > from another space altogether.
> 
> It seems you didn't understand my concern.

Likewise.

> 
> > > Uwe seems to be concerned about case that I find extremely unlikely. We
> > > are talking about a system that does not have GPIO support and behaves
> > > just fine, with the exception that it actually has (physically) a
> > > *single* GPIO, and that GPIO happens to be optional in a single driver,
> > > but in this particular system is actually needed (but that need
> > > manifests in a non-obvious way). And we have system integrator that has
> > > no idea what they are doing (no schematic, etc).
> 
> IMHO this is not an excuse to have lax error checking.

IMO it is not an error altogether, so there is no lax checking.

> 
> > > I think that if there is one optional GPIO there will be mandatiry GPIOs
> > > in such system as well and selection of GPIOLIB will be forced early on
> > > in board bringup.
> > 
> > One more thing: if we keep reporting -ENOSYS in case of !CONFIG_GPIOLIB,
> > then most of the non platform-sepcific drivers will eventually gain code
> > silently coping with this -ENOSYS:
> > 
> > 	data->gpiod = gpiod_getptional(...);
> > 	if (IS_ERR(data->gpiod)) {
> > 		error = PTR_ERR(data->gpiod);
> > 		if (error != -ENOSYS)
> > 			return error;
> > 
> > 		data->gpiod = NULL; /* This GPIO _is_ optional */
> 
> Do you agree that this is wrong? Yes, it behaves right for most cases.
> But there are cases where it behaves wrong and so it needs fixing.

I think by now it should be obvious that I do not find it wrong. In fact
that is what I, as a maintainer of drivers that supposed to work on
multitude of platforms, will be forced to do, if the change to stop
reporting -ENOSYS gets reverted. A generic driver has no way to know
that kernel configuration or firmware configuration is wrong and should
not be trusted (except for piling up horrendous DMI hacks in some
cases on X86).

Thanks.
Geert Uytterhoeven March 24, 2017, 8:29 a.m. UTC | #22
Hi Uwe,

On Fri, Mar 24, 2017 at 9:00 AM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> Subject: [PATCH] gpiod: let get_optional return NULL in some cases with GPIOLIB disabled
>
> People disagree if gpiod_get_optional should return NULL or
> ERR_PTR(-ENOSYS) if GPIOLIB is disabled. The argument for NULL is that
> the person who decided to disable GPIOLIB is assumed to know that there
> is no GPIO. The reason to stick to ERR_PTR(-ENOSYS) is that it might
> introduce hard to debug problems if that decision is wrong.
>
> So this patch introduces a compromise and let gpiod_get_optional (and
> its variants) return NULL if the device in question cannot have an
> associated GPIO because it is neither instantiated by a device tree nor
> by ACPI.
>
> This should handle most cases that are argued about.
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  include/linux/gpio/consumer.h | 55 ++++++++++++++++++++++++++++++++++++-------
>  1 file changed, 46 insertions(+), 9 deletions(-)
>
> diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
> index fb0fde686cb1..0ca29889290d 100644
> --- a/include/linux/gpio/consumer.h
> +++ b/include/linux/gpio/consumer.h
> @@ -161,20 +161,48 @@ gpiod_get_index(struct device *dev,
>         return ERR_PTR(-ENOSYS);
>  }
>
> -static inline struct gpio_desc *__must_check
> -gpiod_get_optional(struct device *dev, const char *con_id,
> -                  enum gpiod_flags flags)
> +static inline bool __gpiod_no_optional_possible(struct device *dev)
>  {
> -       return ERR_PTR(-ENOSYS);
> +       /*
> +        * gpiod_get_optional et al can only provide a GPIO if at least one of
> +        * the backends for specifing a GPIO is available. These are device
> +        * tree, ACPI and gpiolib's lookup tables. The latter isn't available if
> +        * GPIOLIB is disabled (which is the case here).
> +        * So if the provided device is unrelated to device tree and ACPI, we
> +        * can be sure that there is no optional GPIO and let gpiod_get_optional
> +        * safely return NULL.
> +        * Otherwise there is still a chance that there is no GPIO but we cannot
> +        * be sure without having to enable a part of GPIOLIB (i.e. the lookup
> +        * part). So lets play safe and return an error. (Though there are also
> +        * arguments that returning NULL then would be beneficial.)
> +        */
> +
> +       if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
> +               return false;

At first sight, I though this was OK:

  1. On ARM with DT, we can assume CONFIG_GPIOLOB=y.

  2. I managed to configure an SH kernel with CONFIG_GPIOLOB=n, CONFIG_OF=y,
     and CONFIG_SERIAL_SH_SCI=y, but since SH boards with SH-SCI UARTs do
     not use DT (yet), the check for dev->of_node (false) should handle
     that.

  3. However, I managed to do the same for h8300, which does use DT. Hence
     if mctrl_gpio would start relying on gpiod_get_optional(), this would
     break the sh-sci driver on h8300 :-(
     Note that h8300 doesn't have any GPIO drivers (yet?), so
     CONFIG_GPIPOLIB=n makes perfect sense!

So I'm afraid the only option is to always return NULL, and put the
responsability on the shoulders of the system integrator...

> +       if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_COMPANION(dev))
> +               return false;

No comments about the ACPI case.

>  static inline struct gpio_desc *__must_check
>  gpiod_get_index_optional(struct device *dev, const char *con_id,
>                          unsigned int index, enum gpiod_flags flags)
>  {
> +       if (__gpiod_no_optional_possible(dev))
> +               return NULL;
> +
>         return ERR_PTR(-ENOSYS);

Regardless of the above, given you use the exact same construct in four
locations, what about letting __gpiod_no_optional_possible() return the NULL
or ERR_PTR itself, and renaming it to e.g. __gpiod_no_optional_return_value()?

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
Uwe Kleine-König March 24, 2017, 8:39 a.m. UTC | #23
Hello Geert,

On Fri, Mar 24, 2017 at 09:29:02AM +0100, Geert Uytterhoeven wrote:
> On Fri, Mar 24, 2017 at 9:00 AM, Uwe Kleine-König
> <u.kleine-koenig@pengutronix.de> wrote:
> > From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > Subject: [PATCH] gpiod: let get_optional return NULL in some cases with GPIOLIB disabled
> >
> > People disagree if gpiod_get_optional should return NULL or
> > ERR_PTR(-ENOSYS) if GPIOLIB is disabled. The argument for NULL is that
> > the person who decided to disable GPIOLIB is assumed to know that there
> > is no GPIO. The reason to stick to ERR_PTR(-ENOSYS) is that it might
> > introduce hard to debug problems if that decision is wrong.
> >
> > So this patch introduces a compromise and let gpiod_get_optional (and
> > its variants) return NULL if the device in question cannot have an
> > associated GPIO because it is neither instantiated by a device tree nor
> > by ACPI.
> >
> > This should handle most cases that are argued about.
> >
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > ---
> >  include/linux/gpio/consumer.h | 55 ++++++++++++++++++++++++++++++++++++-------
> >  1 file changed, 46 insertions(+), 9 deletions(-)
> >
> > diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
> > index fb0fde686cb1..0ca29889290d 100644
> > --- a/include/linux/gpio/consumer.h
> > +++ b/include/linux/gpio/consumer.h
> > @@ -161,20 +161,48 @@ gpiod_get_index(struct device *dev,
> >         return ERR_PTR(-ENOSYS);
> >  }
> >
> > -static inline struct gpio_desc *__must_check
> > -gpiod_get_optional(struct device *dev, const char *con_id,
> > -                  enum gpiod_flags flags)
> > +static inline bool __gpiod_no_optional_possible(struct device *dev)
> >  {
> > -       return ERR_PTR(-ENOSYS);
> > +       /*
> > +        * gpiod_get_optional et al can only provide a GPIO if at least one of
> > +        * the backends for specifing a GPIO is available. These are device
> > +        * tree, ACPI and gpiolib's lookup tables. The latter isn't available if
> > +        * GPIOLIB is disabled (which is the case here).
> > +        * So if the provided device is unrelated to device tree and ACPI, we
> > +        * can be sure that there is no optional GPIO and let gpiod_get_optional
> > +        * safely return NULL.
> > +        * Otherwise there is still a chance that there is no GPIO but we cannot
> > +        * be sure without having to enable a part of GPIOLIB (i.e. the lookup
> > +        * part). So lets play safe and return an error. (Though there are also
> > +        * arguments that returning NULL then would be beneficial.)
> > +        */
> > +
> > +       if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
> > +               return false;
> 
> At first sight, I though this was OK:
> 
>   1. On ARM with DT, we can assume CONFIG_GPIOLOB=y.
> 
>   2. I managed to configure an SH kernel with CONFIG_GPIOLOB=n, CONFIG_OF=y,
>      and CONFIG_SERIAL_SH_SCI=y, but since SH boards with SH-SCI UARTs do
>      not use DT (yet), the check for dev->of_node (false) should handle
>      that.
> 
>   3. However, I managed to do the same for h8300, which does use DT. Hence
>      if mctrl_gpio would start relying on gpiod_get_optional(), this would
>      break the sh-sci driver on h8300 :-(
>      Note that h8300 doesn't have any GPIO drivers (yet?), so
>      CONFIG_GPIPOLIB=n makes perfect sense!

Thanks for your efforts.

> So I'm afraid the only option is to always return NULL, and put the
> responsability on the shoulders of the system integrator...

The gpio lines could be provided by an i2c gpio adapter, right? So IMHO
you don't need platform gpios to justify -ENODEV. So I guess that's a
case where we don't come to an agreement.

> > +       if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_COMPANION(dev))
> > +               return false;
> 
> No comments about the ACPI case.
> 
> >  static inline struct gpio_desc *__must_check
> >  gpiod_get_index_optional(struct device *dev, const char *con_id,
> >                          unsigned int index, enum gpiod_flags flags)
> >  {
> > +       if (__gpiod_no_optional_possible(dev))
> > +               return NULL;
> > +
> >         return ERR_PTR(-ENOSYS);
> 
> Regardless of the above, given you use the exact same construct in four
> locations, what about letting __gpiod_no_optional_possible() return the NULL
> or ERR_PTR itself, and renaming it to e.g. __gpiod_no_optional_return_value()?

I thought about that but didn't find a good name and so considered it
more clear this way. Another optimisation would be to unconditionally
define get_optional in terms of get_index_optional which would simplify
my patch a bit.

I'd consider __gpiod_optional_return_value a better name than
__gpiod_no_optional_return_value but I'm still not convinced.

Best regards
Uwe
Linus Walleij March 24, 2017, 8:58 a.m. UTC | #24
On Thu, Mar 23, 2017 at 8:10 PM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> On Thu, Mar 23, 2017 at 08:44:41AM -0700, Dmitry Torokhov wrote:
>> On Thu, Mar 23, 2017 at 07:43:25AM -0700, Dmitry Torokhov wrote:
>> > On Thu, Mar 23, 2017 at 02:41:53PM +0100, Linus Walleij wrote:
>> > > On Thu, Mar 23, 2017 at 1:34 PM, Uwe Kleine-König
>> > > <u.kleine-koenig@pengutronix.de> wrote:
>> > >
>> > > > Maybe we can make gpiod_get_optional look like this:
>> > > >
>> > > >         if (!dev->of_node && isnt_a_acpi_device(dev) && !IS_ENABLED(GPIOLIB))
>> > > >                 return NULL;
>> > > >         else
>> > > >                 return -ENOSYS;
>> > > >
>> > > > I don't know how isnt_a_acpi_device looks like, probably it involves
>> > > > CONFIG_ACPI and/or dev->acpi_node.
>> > > >
>> > > > This should be safe and still comfortable for legacy platforms, isn't it?
>> > >
>> > > I like the looks of this.
>> > >
>> > > Can we revert Dmitry's patch and apply something like this instead?
>> > >
>> > > Dmitry, how do you feel about this?
>> >
>> > I frankly do not see the point. It still makes driver code more complex
>
> Note that this code is in the gpio header, and not in driver code. So
> the driver just does
>
>         gpiod = gpiod_get_optional(...)
>         if (IS_ERR(gpiod))
>                 return PTR_ERR(gpiod);
>
> (as it is supposed to do now). I think that's nice.

It does look nice. Compare this to what we must do for optional regulators:

        st->reg = devm_regulator_get_optional(&spi->dev, "vref");
        if (IS_ERR(st->reg)) {
                /* Any other error indicates that the regulator does exist */
                if (PTR_ERR(st->reg) != -ENODEV)
                        return PTR_ERR(st->reg);
                /* Use internal reference */
        }

So for optional regulators we get -ENODEV if we don't have it,
and then proceed on an alternate path, such as using an internal
reference voltage.

However the fact that regulators and GPIO optionals are already
handled differently is creating "cognitive dissonance" or what I
should call it.

Yours,
Linus Walleij
Geert Uytterhoeven March 24, 2017, 8:59 a.m. UTC | #25
Hi Uwe,

On Fri, Mar 24, 2017 at 9:39 AM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> On Fri, Mar 24, 2017 at 09:29:02AM +0100, Geert Uytterhoeven wrote:
>> On Fri, Mar 24, 2017 at 9:00 AM, Uwe Kleine-König
>> <u.kleine-koenig@pengutronix.de> wrote:
>> > From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
>> > Subject: [PATCH] gpiod: let get_optional return NULL in some cases with GPIOLIB disabled
>> >
>> > People disagree if gpiod_get_optional should return NULL or
>> > ERR_PTR(-ENOSYS) if GPIOLIB is disabled. The argument for NULL is that
>> > the person who decided to disable GPIOLIB is assumed to know that there
>> > is no GPIO. The reason to stick to ERR_PTR(-ENOSYS) is that it might
>> > introduce hard to debug problems if that decision is wrong.
>> >
>> > So this patch introduces a compromise and let gpiod_get_optional (and
>> > its variants) return NULL if the device in question cannot have an
>> > associated GPIO because it is neither instantiated by a device tree nor
>> > by ACPI.
>> >
>> > This should handle most cases that are argued about.
>> >
>> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
>> > ---
>> >  include/linux/gpio/consumer.h | 55 ++++++++++++++++++++++++++++++++++++-------
>> >  1 file changed, 46 insertions(+), 9 deletions(-)
>> >
>> > diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
>> > index fb0fde686cb1..0ca29889290d 100644
>> > --- a/include/linux/gpio/consumer.h
>> > +++ b/include/linux/gpio/consumer.h
>> > @@ -161,20 +161,48 @@ gpiod_get_index(struct device *dev,
>> >         return ERR_PTR(-ENOSYS);
>> >  }
>> >
>> > -static inline struct gpio_desc *__must_check
>> > -gpiod_get_optional(struct device *dev, const char *con_id,
>> > -                  enum gpiod_flags flags)
>> > +static inline bool __gpiod_no_optional_possible(struct device *dev)
>> >  {
>> > -       return ERR_PTR(-ENOSYS);
>> > +       /*
>> > +        * gpiod_get_optional et al can only provide a GPIO if at least one of
>> > +        * the backends for specifing a GPIO is available. These are device
>> > +        * tree, ACPI and gpiolib's lookup tables. The latter isn't available if
>> > +        * GPIOLIB is disabled (which is the case here).
>> > +        * So if the provided device is unrelated to device tree and ACPI, we
>> > +        * can be sure that there is no optional GPIO and let gpiod_get_optional
>> > +        * safely return NULL.
>> > +        * Otherwise there is still a chance that there is no GPIO but we cannot
>> > +        * be sure without having to enable a part of GPIOLIB (i.e. the lookup
>> > +        * part). So lets play safe and return an error. (Though there are also
>> > +        * arguments that returning NULL then would be beneficial.)
>> > +        */
>> > +
>> > +       if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
>> > +               return false;
>>
>> At first sight, I though this was OK:
>>
>>   1. On ARM with DT, we can assume CONFIG_GPIOLOB=y.
>>
>>   2. I managed to configure an SH kernel with CONFIG_GPIOLOB=n, CONFIG_OF=y,
>>      and CONFIG_SERIAL_SH_SCI=y, but since SH boards with SH-SCI UARTs do
>>      not use DT (yet), the check for dev->of_node (false) should handle
>>      that.
>>
>>   3. However, I managed to do the same for h8300, which does use DT. Hence
>>      if mctrl_gpio would start relying on gpiod_get_optional(), this would
>>      break the sh-sci driver on h8300 :-(
>>      Note that h8300 doesn't have any GPIO drivers (yet?), so
>>      CONFIG_GPIPOLIB=n makes perfect sense!
>
> Thanks for your efforts.

You're welcome.

>> So I'm afraid the only option is to always return NULL, and put the
>> responsability on the shoulders of the system integrator...
>
> The gpio lines could be provided by an i2c gpio adapter, right? So IMHO
> you don't need platform gpios to justify -ENODEV. So I guess that's a
> case where we don't come to an agreement.

While you can enable I2C without further dependencies, no I2C GPIO expander
will be offered... unless you have enabled CONFIG_GPIOLIB first.

>> >  static inline struct gpio_desc *__must_check
>> >  gpiod_get_index_optional(struct device *dev, const char *con_id,
>> >                          unsigned int index, enum gpiod_flags flags)
>> >  {
>> > +       if (__gpiod_no_optional_possible(dev))
>> > +               return NULL;
>> > +
>> >         return ERR_PTR(-ENOSYS);
>>
>> Regardless of the above, given you use the exact same construct in four
>> locations, what about letting __gpiod_no_optional_possible() return the NULL
>> or ERR_PTR itself, and renaming it to e.g. __gpiod_no_optional_return_value()?
>
> I thought about that but didn't find a good name and so considered it
> more clear this way. Another optimisation would be to unconditionally
> define get_optional in terms of get_index_optional which would simplify
> my patch a bit.
>
> I'd consider __gpiod_optional_return_value a better name than
> __gpiod_no_optional_return_value but I'm still not convinced.

No hard feelings about the name from my side.

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
Uwe Kleine-König March 24, 2017, 9:15 a.m. UTC | #26
On Fri, Mar 24, 2017 at 09:59:04AM +0100, Geert Uytterhoeven wrote:
> Hi Uwe,
> 
> On Fri, Mar 24, 2017 at 9:39 AM, Uwe Kleine-König
> <u.kleine-koenig@pengutronix.de> wrote:
> > On Fri, Mar 24, 2017 at 09:29:02AM +0100, Geert Uytterhoeven wrote:
> >> On Fri, Mar 24, 2017 at 9:00 AM, Uwe Kleine-König
> >> <u.kleine-koenig@pengutronix.de> wrote:
> >> > From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> >> > Subject: [PATCH] gpiod: let get_optional return NULL in some cases with GPIOLIB disabled
> >> >
> >> > People disagree if gpiod_get_optional should return NULL or
> >> > ERR_PTR(-ENOSYS) if GPIOLIB is disabled. The argument for NULL is that
> >> > the person who decided to disable GPIOLIB is assumed to know that there
> >> > is no GPIO. The reason to stick to ERR_PTR(-ENOSYS) is that it might
> >> > introduce hard to debug problems if that decision is wrong.
> >> >
> >> > So this patch introduces a compromise and let gpiod_get_optional (and
> >> > its variants) return NULL if the device in question cannot have an
> >> > associated GPIO because it is neither instantiated by a device tree nor
> >> > by ACPI.
> >> >
> >> > This should handle most cases that are argued about.
> >> >
> >> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> >> > ---
> >> >  include/linux/gpio/consumer.h | 55 ++++++++++++++++++++++++++++++++++++-------
> >> >  1 file changed, 46 insertions(+), 9 deletions(-)
> >> >
> >> > diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
> >> > index fb0fde686cb1..0ca29889290d 100644
> >> > --- a/include/linux/gpio/consumer.h
> >> > +++ b/include/linux/gpio/consumer.h
> >> > @@ -161,20 +161,48 @@ gpiod_get_index(struct device *dev,
> >> >         return ERR_PTR(-ENOSYS);
> >> >  }
> >> >
> >> > -static inline struct gpio_desc *__must_check
> >> > -gpiod_get_optional(struct device *dev, const char *con_id,
> >> > -                  enum gpiod_flags flags)
> >> > +static inline bool __gpiod_no_optional_possible(struct device *dev)
> >> >  {
> >> > -       return ERR_PTR(-ENOSYS);
> >> > +       /*
> >> > +        * gpiod_get_optional et al can only provide a GPIO if at least one of
> >> > +        * the backends for specifing a GPIO is available. These are device
> >> > +        * tree, ACPI and gpiolib's lookup tables. The latter isn't available if
> >> > +        * GPIOLIB is disabled (which is the case here).
> >> > +        * So if the provided device is unrelated to device tree and ACPI, we
> >> > +        * can be sure that there is no optional GPIO and let gpiod_get_optional
> >> > +        * safely return NULL.
> >> > +        * Otherwise there is still a chance that there is no GPIO but we cannot
> >> > +        * be sure without having to enable a part of GPIOLIB (i.e. the lookup
> >> > +        * part). So lets play safe and return an error. (Though there are also
> >> > +        * arguments that returning NULL then would be beneficial.)
> >> > +        */
> >> > +
> >> > +       if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
> >> > +               return false;
> >>
> >> At first sight, I though this was OK:
> >>
> >>   1. On ARM with DT, we can assume CONFIG_GPIOLOB=y.
> >>
> >>   2. I managed to configure an SH kernel with CONFIG_GPIOLOB=n, CONFIG_OF=y,
> >>      and CONFIG_SERIAL_SH_SCI=y, but since SH boards with SH-SCI UARTs do
> >>      not use DT (yet), the check for dev->of_node (false) should handle
> >>      that.
> >>
> >>   3. However, I managed to do the same for h8300, which does use DT. Hence
> >>      if mctrl_gpio would start relying on gpiod_get_optional(), this would
> >>      break the sh-sci driver on h8300 :-(
> >>      Note that h8300 doesn't have any GPIO drivers (yet?), so
> >>      CONFIG_GPIPOLIB=n makes perfect sense!
> >
> > Thanks for your efforts.
> 
> You're welcome.
> 
> >> So I'm afraid the only option is to always return NULL, and put the
> >> responsability on the shoulders of the system integrator...
> >
> > The gpio lines could be provided by an i2c gpio adapter, right? So IMHO
> > you don't need platform gpios to justify -ENODEV. So I guess that's a
> > case where we don't come to an agreement.
> 
> While you can enable I2C without further dependencies, no I2C GPIO expander
> will be offered... unless you have enabled CONFIG_GPIOLIB first.

And that is expected, still the device tree could reference such a GPIO
and thus create a situation where Dmitry's and my judgement disagree.

So I think my suggestion is the best we could do now. It minimizes the
number of cases where we disagree. The next best thing would be to
implement that half gpiolib stuff (i.e. do the full lookup to be sure
there is no gpio) but this comes at a price: We need some time to
implement it and it adds a bit to the kernel size.

Best regards
Uwe
Geert Uytterhoeven March 24, 2017, 9:44 a.m. UTC | #27
Hi Uwe,

On Fri, Mar 24, 2017 at 10:15 AM, Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> On Fri, Mar 24, 2017 at 09:59:04AM +0100, Geert Uytterhoeven wrote:
>> On Fri, Mar 24, 2017 at 9:39 AM, Uwe Kleine-König
>> <u.kleine-koenig@pengutronix.de> wrote:
>> > On Fri, Mar 24, 2017 at 09:29:02AM +0100, Geert Uytterhoeven wrote:
>> >> On Fri, Mar 24, 2017 at 9:00 AM, Uwe Kleine-König
>> >> <u.kleine-koenig@pengutronix.de> wrote:
>> >> > From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
>> >> > Subject: [PATCH] gpiod: let get_optional return NULL in some cases with GPIOLIB disabled
>> >> >
>> >> > People disagree if gpiod_get_optional should return NULL or
>> >> > ERR_PTR(-ENOSYS) if GPIOLIB is disabled. The argument for NULL is that
>> >> > the person who decided to disable GPIOLIB is assumed to know that there
>> >> > is no GPIO. The reason to stick to ERR_PTR(-ENOSYS) is that it might
>> >> > introduce hard to debug problems if that decision is wrong.
>> >> >
>> >> > So this patch introduces a compromise and let gpiod_get_optional (and
>> >> > its variants) return NULL if the device in question cannot have an
>> >> > associated GPIO because it is neither instantiated by a device tree nor
>> >> > by ACPI.
>> >> >
>> >> > This should handle most cases that are argued about.
>> >> >
>> >> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
>> >> > ---
>> >> >  include/linux/gpio/consumer.h | 55 ++++++++++++++++++++++++++++++++++++-------
>> >> >  1 file changed, 46 insertions(+), 9 deletions(-)
>> >> >
>> >> > diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
>> >> > index fb0fde686cb1..0ca29889290d 100644
>> >> > --- a/include/linux/gpio/consumer.h
>> >> > +++ b/include/linux/gpio/consumer.h
>> >> > @@ -161,20 +161,48 @@ gpiod_get_index(struct device *dev,
>> >> >         return ERR_PTR(-ENOSYS);
>> >> >  }
>> >> >
>> >> > -static inline struct gpio_desc *__must_check
>> >> > -gpiod_get_optional(struct device *dev, const char *con_id,
>> >> > -                  enum gpiod_flags flags)
>> >> > +static inline bool __gpiod_no_optional_possible(struct device *dev)
>> >> >  {
>> >> > -       return ERR_PTR(-ENOSYS);
>> >> > +       /*
>> >> > +        * gpiod_get_optional et al can only provide a GPIO if at least one of
>> >> > +        * the backends for specifing a GPIO is available. These are device
>> >> > +        * tree, ACPI and gpiolib's lookup tables. The latter isn't available if
>> >> > +        * GPIOLIB is disabled (which is the case here).
>> >> > +        * So if the provided device is unrelated to device tree and ACPI, we
>> >> > +        * can be sure that there is no optional GPIO and let gpiod_get_optional
>> >> > +        * safely return NULL.
>> >> > +        * Otherwise there is still a chance that there is no GPIO but we cannot
>> >> > +        * be sure without having to enable a part of GPIOLIB (i.e. the lookup
>> >> > +        * part). So lets play safe and return an error. (Though there are also
>> >> > +        * arguments that returning NULL then would be beneficial.)
>> >> > +        */
>> >> > +
>> >> > +       if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
>> >> > +               return false;
>> >>
>> >> At first sight, I though this was OK:
>> >>
>> >>   1. On ARM with DT, we can assume CONFIG_GPIOLOB=y.
>> >>
>> >>   2. I managed to configure an SH kernel with CONFIG_GPIOLOB=n, CONFIG_OF=y,
>> >>      and CONFIG_SERIAL_SH_SCI=y, but since SH boards with SH-SCI UARTs do
>> >>      not use DT (yet), the check for dev->of_node (false) should handle
>> >>      that.
>> >>
>> >>   3. However, I managed to do the same for h8300, which does use DT. Hence
>> >>      if mctrl_gpio would start relying on gpiod_get_optional(), this would
>> >>      break the sh-sci driver on h8300 :-(
>> >>      Note that h8300 doesn't have any GPIO drivers (yet?), so
>> >>      CONFIG_GPIPOLIB=n makes perfect sense!
>> >
>> > Thanks for your efforts.
>>
>> You're welcome.
>>
>> >> So I'm afraid the only option is to always return NULL, and put the
>> >> responsability on the shoulders of the system integrator...
>> >
>> > The gpio lines could be provided by an i2c gpio adapter, right? So IMHO
>> > you don't need platform gpios to justify -ENODEV. So I guess that's a
>> > case where we don't come to an agreement.
>>
>> While you can enable I2C without further dependencies, no I2C GPIO expander
>> will be offered... unless you have enabled CONFIG_GPIOLIB first.
>
> And that is expected, still the device tree could reference such a GPIO
> and thus create a situation where Dmitry's and my judgement disagree.

If the device tree references such a GPIO, and CONFIG_GPIOLIB is not set,
the I2C GPIO expander device will not be bound.
Frank Rowand's DT scripts (http://elinux.org/Device_Tree_frowand) will come
to the rescue, and inform the user which driver(s) to enable.

> So I think my suggestion is the best we could do now. It minimizes the
> number of cases where we disagree. The next best thing would be to
> implement that half gpiolib stuff (i.e. do the full lookup to be sure
> there is no gpio) but this comes at a price: We need some time to
> implement it and it adds a bit to the kernel size.

So I still have to handle -ENOSYS in sh-sci.c, to avoid regressions...

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
Uwe Kleine-König March 24, 2017, 10:01 a.m. UTC | #28
Hello Geert,

On Fri, Mar 24, 2017 at 10:44:50AM +0100, Geert Uytterhoeven wrote:
> On Fri, Mar 24, 2017 at 10:15 AM, Uwe Kleine-König
> <u.kleine-koenig@pengutronix.de> wrote:
> > On Fri, Mar 24, 2017 at 09:59:04AM +0100, Geert Uytterhoeven wrote:
> >> On Fri, Mar 24, 2017 at 9:39 AM, Uwe Kleine-König
> >> <u.kleine-koenig@pengutronix.de> wrote:
> >> > On Fri, Mar 24, 2017 at 09:29:02AM +0100, Geert Uytterhoeven wrote:
> >> >> On Fri, Mar 24, 2017 at 9:00 AM, Uwe Kleine-König
> >> >> <u.kleine-koenig@pengutronix.de> wrote:
> >> >> > From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> >> >> > Subject: [PATCH] gpiod: let get_optional return NULL in some cases with GPIOLIB disabled
> >> >> >
> >> >> > People disagree if gpiod_get_optional should return NULL or
> >> >> > ERR_PTR(-ENOSYS) if GPIOLIB is disabled. The argument for NULL is that
> >> >> > the person who decided to disable GPIOLIB is assumed to know that there
> >> >> > is no GPIO. The reason to stick to ERR_PTR(-ENOSYS) is that it might
> >> >> > introduce hard to debug problems if that decision is wrong.
> >> >> >
> >> >> > So this patch introduces a compromise and let gpiod_get_optional (and
> >> >> > its variants) return NULL if the device in question cannot have an
> >> >> > associated GPIO because it is neither instantiated by a device tree nor
> >> >> > by ACPI.
> >> >> >
> >> >> > This should handle most cases that are argued about.
> >> >> >
> >> >> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> >> >> > ---
> >> >> >  include/linux/gpio/consumer.h | 55 ++++++++++++++++++++++++++++++++++++-------
> >> >> >  1 file changed, 46 insertions(+), 9 deletions(-)
> >> >> >
> >> >> > diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
> >> >> > index fb0fde686cb1..0ca29889290d 100644
> >> >> > --- a/include/linux/gpio/consumer.h
> >> >> > +++ b/include/linux/gpio/consumer.h
> >> >> > @@ -161,20 +161,48 @@ gpiod_get_index(struct device *dev,
> >> >> >         return ERR_PTR(-ENOSYS);
> >> >> >  }
> >> >> >
> >> >> > -static inline struct gpio_desc *__must_check
> >> >> > -gpiod_get_optional(struct device *dev, const char *con_id,
> >> >> > -                  enum gpiod_flags flags)
> >> >> > +static inline bool __gpiod_no_optional_possible(struct device *dev)
> >> >> >  {
> >> >> > -       return ERR_PTR(-ENOSYS);
> >> >> > +       /*
> >> >> > +        * gpiod_get_optional et al can only provide a GPIO if at least one of
> >> >> > +        * the backends for specifing a GPIO is available. These are device
> >> >> > +        * tree, ACPI and gpiolib's lookup tables. The latter isn't available if
> >> >> > +        * GPIOLIB is disabled (which is the case here).
> >> >> > +        * So if the provided device is unrelated to device tree and ACPI, we
> >> >> > +        * can be sure that there is no optional GPIO and let gpiod_get_optional
> >> >> > +        * safely return NULL.
> >> >> > +        * Otherwise there is still a chance that there is no GPIO but we cannot
> >> >> > +        * be sure without having to enable a part of GPIOLIB (i.e. the lookup
> >> >> > +        * part). So lets play safe and return an error. (Though there are also
> >> >> > +        * arguments that returning NULL then would be beneficial.)
> >> >> > +        */
> >> >> > +
> >> >> > +       if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
> >> >> > +               return false;
> >> >>
> >> >> At first sight, I though this was OK:
> >> >>
> >> >>   1. On ARM with DT, we can assume CONFIG_GPIOLOB=y.
> >> >>
> >> >>   2. I managed to configure an SH kernel with CONFIG_GPIOLOB=n, CONFIG_OF=y,
> >> >>      and CONFIG_SERIAL_SH_SCI=y, but since SH boards with SH-SCI UARTs do
> >> >>      not use DT (yet), the check for dev->of_node (false) should handle
> >> >>      that.
> >> >>
> >> >>   3. However, I managed to do the same for h8300, which does use DT. Hence
> >> >>      if mctrl_gpio would start relying on gpiod_get_optional(), this would
> >> >>      break the sh-sci driver on h8300 :-(
> >> >>      Note that h8300 doesn't have any GPIO drivers (yet?), so
> >> >>      CONFIG_GPIPOLIB=n makes perfect sense!
> >>
> >> >> So I'm afraid the only option is to always return NULL, and put the
> >> >> responsability on the shoulders of the system integrator...
> >> >
> >> > The gpio lines could be provided by an i2c gpio adapter, right? So IMHO
> >> > you don't need platform gpios to justify -ENODEV. So I guess that's a
> >> > case where we don't come to an agreement.
> >>
> >> While you can enable I2C without further dependencies, no I2C GPIO expander
> >> will be offered... unless you have enabled CONFIG_GPIOLIB first.
> >
> > And that is expected, still the device tree could reference such a GPIO
> > and thus create a situation where Dmitry's and my judgement disagree.
> 
> If the device tree references such a GPIO, and CONFIG_GPIOLIB is not set,
> the I2C GPIO expander device will not be bound.
> Frank Rowand's DT scripts (http://elinux.org/Device_Tree_frowand) will come
> to the rescue, and inform the user which driver(s) to enable.
> 
> > So I think my suggestion is the best we could do now. It minimizes the
> > number of cases where we disagree. The next best thing would be to
> > implement that half gpiolib stuff (i.e. do the full lookup to be sure
> > there is no gpio) but this comes at a price: We need some time to
> > implement it and it adds a bit to the kernel size.
> 
> So I still have to handle -ENOSYS in sh-sci.c, to avoid regressions...

How much would it hurt to enable GPIOLIB there now? I assume that the
platform has GPIOs and it's only an intermediate step that there is no
driver for it at present? At some point you have to bite the bullet.
Given that mctrl_gpio is a usage of GPIOs that might be now?

Best regards
Uwe
diff mbox

Patch

diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
index 6117ac8da48f..39833d009c18 100644
--- a/drivers/tty/serial/Kconfig
+++ b/drivers/tty/serial/Kconfig
@@ -1681,5 +1681,6 @@  endmenu

 config SERIAL_MCTRL_GPIO
     tristate
+    depends on GPIOLIB

 endif # TTY
diff --git a/drivers/tty/serial/serial_mctrl_gpio.h
b/drivers/tty/serial/serial_mctrl_gpio.h
index fa000bcff217..ba8f8e531d56 100644
--- a/drivers/tty/serial/serial_mctrl_gpio.h
+++ b/drivers/tty/serial/serial_mctrl_gpio.h
@@ -40,8 +40,6 @@  enum mctrl_gpio_idx {
  */
 struct mctrl_gpios;

-#ifdef CONFIG_GPIOLIB
-
 /*
  * Set state of the modem control output lines via GPIOs.
  */
@@ -101,57 +99,4 @@  void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios);
  */
 void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios);

-#else /* GPIOLIB */
-
-static inline
-void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
-{
-}
-
-static inline
-unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
-{
-    return *mctrl;
-}
-
-static inline unsigned int
-mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
-{
-    return *mctrl;
-}
-
-static inline
-struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
-                      enum mctrl_gpio_idx gidx)
-{
-    return ERR_PTR(-ENOSYS);
-}
-
-static inline
-struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
-{
-    return ERR_PTR(-ENOSYS);
-}
-
-static inline
-struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev,
unsigned int idx)
-{
-    return ERR_PTR(-ENOSYS);
-}
-
-static inline
-void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
-{
-}
-
-static inline void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
-{
-}
-
-static inline void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios)
-{
-}
-
-#endif /* GPIOLIB */
-