Simple
Posts: 34
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Combining overlays for single GPIO use

Sun Sep 15, 2019 2:36 pm

Hello
Question for the engineers here...

I use two overlays in a project being

dtoverlay=gpio-shutdown
dtoverlay=gpio-poweroff

and I am wondering what the possibility of combining the functionality of both overlays into one overlay, as to use just one GPIO pin, could be..
The function would be something like..

At boot -
set shared gpio pin as input and run 'gpio-shutdown' function
When triggered-
set shared gpio pin as output and run 'gpio-poweroff' function

It seems to me that these two can be combined and more of use for those of us that either want either function or both
Personally, I can make them both work for my use on the same gpio pin

Thanks
S

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2582
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Combining overlays for single GPIO use

Mon Sep 16, 2019 8:53 am

Overlays don't provide any functionality themselves, they just enable and configure functionality provided by device drivers. The gpio-poweroff overlays makes use of the gpio-poweroff driver, whereas the gpio-shutdown overlay configures the gpio-keys driver to generate KEY_POWER. Both of those drivers try to claim the GPIO line, and only one is allow to succeed, so it isn't possible to combine the two functions on a single pin in the way you would like.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2582
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Combining overlays for single GPIO use

Mon Sep 16, 2019 10:22 am

OK, I've got some good news and some less-good news. The Linux gpiolib subsystem has a new flag - GPIOD_FLAGS_BIT_NONEXCLUSIVE - that allows two drivers to claim the same GPIO. It is intended for use with regulators, and there are warning comments about the hacky nature and how it may only work in a limited set of circumstances, but it does seem to allow the functionality you require. You activate it by setting bit 4 of the flags field, which is exposed by both overlays as "active_low":

Code: Select all

dtoverlay=gpio-shutdown,gpio_pin=3,active_low=17
dtoverlay=gpio-poweroff,gpiopin=3,input,active_low=17
If you don't want an active low pin, use "active_low=16" instead.

A logic analyser on the shutdown pin shows that it seems to work, but you will need to design your external electronics so as not to mistake the shutdown signal as the poweroff signall (this is one reason to keep them separate).

The less-good news is that the feature only appeared in Linux 4.20. However, the two commits involved are easy to back-port so I've added them to rpi-4.19.y. A kernel containing them should be available by "sudo rpi-update" in the next few days, and will appear in Raspbian kernel package some time later.

Simple
Posts: 34
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Combining overlays for single GPIO use

Tue Sep 17, 2019 3:53 pm

Wow thanks Phil

I've got a few PCB's in the works that I'll have to get up and tested ASAP
Thanks for the helps, I'll be in touch
S

Simple
Posts: 34
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Combining overlays for single GPIO use

Sun Dec 29, 2019 7:13 pm

So far, I have experienced a high output using GPIO16, when what I need is a low.
I have tried a few different settings and the only way that I have gotten a low is to set
dtoverlay=gpio-poweroff,gpiopin=16,active_low=0

My desired flow is to have the GPIO16 be set as a high triggered input for
dtoverlay=gpio-shutdown
and when the input is triggered, set GPIO16 as a high outputting output for
dtoverlay=gpio-poweroff

For whatever reason, I am instead only experiencing a constant high, apparently, output.
Any more insights?

Thanks again
Scott

Simple
Posts: 34
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Combining overlays for single GPIO use

Fri Jan 03, 2020 5:06 pm

Have to put this one on the shelf for the time. Looks promising although I don't have the programming skills to know this stuff enough quite yet.

User avatar
Laguna
Posts: 4
Joined: Mon Jun 17, 2019 11:44 am

Re: Combining overlays for single GPIO use

Mon Feb 10, 2020 2:46 pm

Both of those drivers try to claim the GPIO line so it isn't possible to combine the two functions on a single pin in the way you would like.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2582
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Combining overlays for single GPIO use

Mon Feb 10, 2020 2:51 pm

Once again, I've covered that part already:
The Linux gpiolib subsystem has a new flag - GPIOD_FLAGS_BIT_NONEXCLUSIVE - that allows two drivers to claim the same GPIO.
Laguna, I'm getting tired of responding to your posts where you've failed to read the preceding thread and made a contribution that helps nobody.

Return to “Device Tree”