3

Where can I hook into linux to add custom soft power down?

That's the question. Here's why I need to know:

I am trying to make a rapsberry pi soft power-down. More precisely I want the pi to be switched off (power supply disconnected) when put into Runlevel 0 (aka sudo poweroff).

I know that the Pi doesn't have the hardware to do this so I'm going to build some custom electronics which will toggle the power supply to the Pi. I'm going to use a GPIO pin which will allow software on the pi to signal my electronics and toggle (off) and have a button to toggle it back on.

So far, so good.

My problem is WHERE do I put the code to send a signal through the GPIO?

Two things spring to mind:

  • Maybe I should write a program and wrap it in a script in /etc/init for systemd to use.
  • Maybe I need to write a kernel module. But if so where / how do I hook in to the shutdown sequence?

I can't just guess. If I get this wrong the PI will power-down too soon and not finish shutting down linux. But I've spent hours on google and not found documentation for this.

I know this is possible because it's exactly what happens with ACPI. When a PC / Laptop shuts down, the motherboard will signal the powersupply to cut [most of] the power. Something in the operating system (linux) must be triggering this. So where does ACPI soft off hook into the shutdown sequence on linux?

Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
Philip Couling
  • 13,581
  • 5
  • 53
  • 85
  • First of all, which exactly Raspberry Pi board you use? Could you find the schematics for it and link it here? As I understand it, you should power off the power supply from kernel (using GPIO line). But details may depend on how it is done / will be done on physical level, hence you should first investigate the schematics and come up with idea how exactly to cut the power. Also, you should be aware that ARM boards usually don't use ACPI, the use Device Tree instead, which won't really help you to achieve that task (aside from describing GPIO line in it). – Sam Protsenko Jul 16 '17 at 13:49
  • As for in-kernel poweroff sequence: see [this](https://unix.stackexchange.com/questions/122557/how-does-the-system-shutdown-of-a-linux-kernel-work-internally) question. You can look for last executing function there, and it should be the place where cutting the power should take the place. `pm_power_off()` looks like the place you are looking for. – Sam Protsenko Jul 16 '17 at 14:05
  • See also [this](https://stackoverflow.com/questions/41596661/how-to-make-linux-power-off-when-halt-is-run) question. – Sam Protsenko Jul 16 '17 at 14:11
  • 1
    Take a look at [drivers/power/reset/gpio-poweroff.c](http://elixir.free-electrons.com/linux/v4.12/source/drivers/power/reset/gpio-poweroff.c), maybe you can reuse it for your task. The documentation for this driver is here: [Documentation/devicetree/bindings/power/reset/gpio-poweroff.txt](http://elixir.free-electrons.com/linux/v4.12/source/Documentation/devicetree/bindings/power/reset/gpio-poweroff.txt). You can try to describe that `gpio-poweroff` in you device tree file (or in device tree overlay) after soldering that GPIO to reset pin of PM IC, and it could be enough. – Sam Protsenko Jul 16 '17 at 14:51
  • @SamProtsenko for clarity the raspberry pi cannot self power off. As far as I'm aware that is a fact for all models of Raspberry pi (at time of writing). My board happens to be a Model 3B. I only reference ACPI because it is a mechanism that has been hooked into the linux kernel at the same place I need to hook my code in. My plan is to put some electronics between the USB phone charger and the pi to cut (or supply) power. I can easily set it up so that switching a single GPIO pin from low to hi will cur the power. – Philip Couling Jul 17 '17 at 08:44
  • @SamProtsenko Thanks I think you've pointed me at what I need. Looks like I can write a kernel module to do this. The module will register it's GPIO setting code as a function pointer `pm_power_off`. The kernel will call as the last thing it does. – Philip Couling Jul 17 '17 at 08:49
  • Future readers may wish to refer to https://unix.stackexchange.com/questions/122557/how-does-the-system-shutdown-of-a-linux-kernel-work-internally – Philip Couling Jul 13 '18 at 09:25

0 Answers0