From bf2d2eae4e75c0f8ab2c4fa2383968831bd625aa Mon Sep 17 00:00:00 2001 From: Otto Esko Date: Sat, 4 Dec 2021 18:51:54 +0200 Subject: [PATCH] recipes-bsp: Add support for gpio-shutdown - Feature is enabled with ENABLE_GPIO_SHUTDOWN variable - Include the gpio-shutdown overlay - If using SysV init include the additional keymap and inittab entries to bind the KEY_POWER event - Systemd init does not require any additional bindings Signed-off-by: Otto Esko --- conf/machine/include/rpi-base.inc | 2 ++ recipes-bsp/bootfiles/rpi-config_git.bb | 18 +++++++++++ .../files/bind_gpio_shutdown.tab | 2 ++ .../files/gpio-shutdown-keymap.sh | 13 ++++++++ recipes-bsp/gpio-shutdown/gpio-shutdown.bb | 31 +++++++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 recipes-bsp/gpio-shutdown/files/bind_gpio_shutdown.tab create mode 100644 recipes-bsp/gpio-shutdown/files/gpio-shutdown-keymap.sh create mode 100644 recipes-bsp/gpio-shutdown/gpio-shutdown.bb diff --git a/conf/machine/include/rpi-base.inc b/conf/machine/include/rpi-base.inc index 0e65d00..2c7f80d 100644 --- a/conf/machine/include/rpi-base.inc +++ b/conf/machine/include/rpi-base.inc @@ -23,6 +23,7 @@ RPI_KERNEL_DEVICETREE_OVERLAYS ?= " \ overlays/gpio-ir-tx.dtbo \ overlays/gpio-key.dtbo \ overlays/gpio-poweroff.dtbo \ + overlays/gpio-shutdown.dtbo \ overlays/hifiberry-amp.dtbo \ overlays/hifiberry-dac.dtbo \ overlays/hifiberry-dacplus.dtbo \ @@ -97,6 +98,7 @@ MACHINE_FEATURES_BACKFILL_CONSIDERED = "rtc" MACHINE_EXTRA_RRECOMMENDS += "kernel-modules udev-rules-rpi" MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS += "${@oe.utils.conditional('ENABLE_I2C', '1', 'kernel-module-i2c-dev kernel-module-i2c-bcm2708', '', d)}" MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS += "${@oe.utils.conditional('ENABLE_IR', '1', 'kernel-module-gpio-ir kernel-module-gpio-ir-tx', '', d)}" +MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS += "${@oe.utils.conditional('ENABLE_GPIO_SHUTDOWN', '1', 'gpio-shutdown kernel-module-gpio-keys', '', d)}" SERIAL_CONSOLES_CHECK ??= "${SERIAL_CONSOLES}" diff --git a/recipes-bsp/bootfiles/rpi-config_git.bb b/recipes-bsp/bootfiles/rpi-config_git.bb index f78c6d7..d9d8bb0 100644 --- a/recipes-bsp/bootfiles/rpi-config_git.bb +++ b/recipes-bsp/bootfiles/rpi-config_git.bb @@ -34,6 +34,8 @@ ENABLE_UART ??= "" WM8960="${@bb.utils.contains("MACHINE_FEATURES", "wm8960", "1", "0", d)}" +GPIO_SHUTDOWN_PIN ??= "" + inherit deploy nopackages do_deploy() { @@ -253,6 +255,22 @@ do_deploy() { echo "dtoverlay=mcp2515-can0,oscillator=${CAN_OSCILLATOR},interrupt=25" >>$CONFIG fi + + if [ "${ENABLE_GPIO_SHUTDOWN}" = "1" ]; then + if ([ "${ENABLE_I2C}" = "1" ] || [ "${PITFT}" = "1" ]) && [ -z "${GPIO_SHUTDOWN_PIN}" ]; then + # By default GPIO shutdown uses the same pin as the (master) I2C SCL. + # If I2C is configured and an alternative pin is not configured for + # gpio-shutdown, there is a configuration conflict. + bbfatal "I2C and gpio-shutdown are both enabled and using the same pins!" + fi + echo "# Enable gpio-shutdown" >> $CONFIG + if [ -z "${GPIO_SHUTDOWN_PIN}" ]; then + echo "dtoverlay=gpio-shutdown" >> $CONFIG + else + echo "dtoverlay=gpio-shutdown,gpio_pin=${GPIO_SHUTDOWN_PIN}" >> $CONFIG + fi + fi + # Append extra config if the user has provided any printf "${RPI_EXTRA_CONFIG}\n" >> $CONFIG diff --git a/recipes-bsp/gpio-shutdown/files/bind_gpio_shutdown.tab b/recipes-bsp/gpio-shutdown/files/bind_gpio_shutdown.tab new file mode 100644 index 0000000..00cc485 --- /dev/null +++ b/recipes-bsp/gpio-shutdown/files/bind_gpio_shutdown.tab @@ -0,0 +1,2 @@ +# Action on special keypress (Key Power) +kb::kbrequest:/sbin/shutdown -t1 -a -h -P now diff --git a/recipes-bsp/gpio-shutdown/files/gpio-shutdown-keymap.sh b/recipes-bsp/gpio-shutdown/files/gpio-shutdown-keymap.sh new file mode 100644 index 0000000..bae50da --- /dev/null +++ b/recipes-bsp/gpio-shutdown/files/gpio-shutdown-keymap.sh @@ -0,0 +1,13 @@ +#!/bin/sh +## +# Bind the gpio-shutdown keycode as Keyboard signal and load it to the +# keymap during startup. +## +case "$1" in + start) + # Inject the gpio keycode to keymap + echo "keycode 116 = KeyboardSignal" | loadkeys + ;; + *) + ;; +esac diff --git a/recipes-bsp/gpio-shutdown/gpio-shutdown.bb b/recipes-bsp/gpio-shutdown/gpio-shutdown.bb new file mode 100644 index 0000000..0682489 --- /dev/null +++ b/recipes-bsp/gpio-shutdown/gpio-shutdown.bb @@ -0,0 +1,31 @@ +SUMMARY = "GPIO shutdown bindings for SysV init" +LICENSE = "GPLv2" +LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/GPL-2.0-only;md5=801f80980d171dd6425610833a22dbe6" + +SRC_URI = "file://bind_gpio_shutdown.tab \ + file://gpio-shutdown-keymap.sh \ +" + +inherit update-rc.d + +INITSCRIPT_NAME = "gpio-shutdown-keymap.sh" +# Run only once during startup +INITSCRIPT_PARAMS = "start 99 S ." + +do_install() { + # The files are only needed if using SysV init. + if ${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', 'true', 'false', d)}; then + install -d ${D}${sysconfdir} \ + ${D}${sysconfdir}/inittab.d \ + ${D}${sysconfdir}/init.d + + install -m 0755 ${WORKDIR}/gpio-shutdown-keymap.sh ${D}${sysconfdir}/init.d/ + install -m 0755 ${WORKDIR}/bind_gpio_shutdown.tab ${D}${sysconfdir}/inittab.d/ + elif ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false', d)}; then + # Systemd init does not require any configuration. + # Note: cannot have an empty branch, hence the redundant dir install. + install -d ${D}${sysconfdir} + else + bbwarn "Not using sysvinit or systemd. The gpio-shutdown may require additional configuration." + fi +}