1 From 7c0d40384b0648030d5202114d90239b8db7d4e0 Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.com>
3 Date: Wed, 2 Aug 2023 11:30:48 +0100
4 Subject: [PATCH] Input: Add raspberrypi-button firmware driver
6 Raspberry Pi 5s have a power/suspend button that is only accessible to
7 the firmware. Add a driver to read it and generate key events.
9 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
11 drivers/input/misc/Kconfig | 10 ++
12 drivers/input/misc/Makefile | 1 +
13 drivers/input/misc/raspberrypi-button.c | 138 +++++++++++++++++++++
14 include/soc/bcm2835/raspberrypi-firmware.h | 1 +
15 4 files changed, 150 insertions(+)
16 create mode 100644 drivers/input/misc/raspberrypi-button.c
18 --- a/drivers/input/misc/Kconfig
19 +++ b/drivers/input/misc/Kconfig
20 @@ -918,6 +918,16 @@ config INPUT_RT5120_PWRKEY
21 To compile this driver as a module, choose M here. the module will
22 be called rt5120-pwrkey.
24 +config INPUT_RASPBERRYPI_BUTTON
25 + tristate "Raspberry Pi button support"
26 + depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && !RASPBERRYPI_FIRMWARE)
28 + This enables support for firmware-controlled buttons on Raspberry
31 + To compile this driver as a module, choose M here. the module will
32 + be called raspberrypi-button.
34 config INPUT_STPMIC1_ONKEY
35 tristate "STPMIC1 PMIC Onkey support"
36 depends on MFD_STPMIC1
37 --- a/drivers/input/misc/Makefile
38 +++ b/drivers/input/misc/Makefile
39 @@ -70,6 +70,7 @@ obj-$(CONFIG_INPUT_RAVE_SP_PWRBUTTON) +=
40 obj-$(CONFIG_INPUT_RB532_BUTTON) += rb532_button.o
41 obj-$(CONFIG_INPUT_REGULATOR_HAPTIC) += regulator-haptic.o
42 obj-$(CONFIG_INPUT_RETU_PWRBUTTON) += retu-pwrbutton.o
43 +obj-$(CONFIG_INPUT_RASPBERRYPI_BUTTON) += raspberrypi-button.o
44 obj-$(CONFIG_INPUT_RT5120_PWRKEY) += rt5120-pwrkey.o
45 obj-$(CONFIG_INPUT_AXP20X_PEK) += axp20x-pek.o
46 obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER) += rotary_encoder.o
48 +++ b/drivers/input/misc/raspberrypi-button.c
50 +// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
52 + * Driver for Raspberry Pi power button
54 + * Copyright (C) 2023 Raspberry Pi Ltd.
56 + * This driver is based on drivers/hwmon/raspberrypi-hwmon.c and
57 + * input/misc/pm8941-pwrkey.c/ - see original files for copyright information
60 +#include <linux/delay.h>
61 +#include <linux/devm-helpers.h>
62 +#include <dt-bindings/input/raspberrypi-button.h>
63 +#include <linux/errno.h>
64 +#include <linux/input.h>
65 +#include <linux/kernel.h>
66 +#include <linux/ktime.h>
67 +#include <linux/module.h>
68 +#include <linux/of.h>
69 +#include <linux/of_address.h>
70 +#include <linux/of_device.h>
71 +#include <linux/platform_device.h>
72 +#include <linux/workqueue.h>
73 +#include <soc/bcm2835/raspberrypi-firmware.h>
77 + struct rpi_firmware *fw;
78 + struct input_dev *input;
79 + struct delayed_work poll_work;
80 + unsigned long poll_rate;
86 +static void button_poll(struct work_struct *work)
88 + struct rpi_button *button;
92 + button = container_of(work, struct rpi_button,
95 + value = BIT(button->id);
96 + err = rpi_firmware_property(button->fw, RPI_FIRMWARE_GET_BUTTONS_PRESSED,
97 + &value, sizeof(value));
99 + dev_err_once(button->dev, "GET_BUTTON_PRESSED not implemented?\n");
103 + if (value & BIT(button->id)) {
104 + input_event(button->input, EV_KEY, button->code, 1);
105 + input_sync(button->input);
106 + input_event(button->input, EV_KEY, button->code, 0);
107 + input_sync(button->input);
110 + schedule_delayed_work(&button->poll_work, button->poll_rate);
113 +static int rpi_button_probe(struct platform_device *pdev)
115 + struct device *dev = &pdev->dev;
116 + struct rpi_button *button;
119 + button = devm_kzalloc(dev, sizeof(*button), GFP_KERNEL);
125 + /* Get the firmware pointer from our parent */
126 + button->fw = dev_get_drvdata(dev->parent);
128 + if (device_property_read_u32(dev, "id", &button->id))
129 + button->id = RASPBERRYPI_BUTTON_POWER;
131 + if (device_property_read_string(dev, "label", &button->name))
132 + button->name = "raspberrypi-button";
134 + if (device_property_read_u32(dev, "linux,code", &button->code)) {
135 + dev_err(&pdev->dev, "no linux,code property\n");
139 + button->input = devm_input_allocate_device(dev);
140 + if (!button->input) {
141 + dev_dbg(&pdev->dev, "unable to allocate input device\n");
145 + input_set_capability(button->input, EV_KEY, button->code);
147 + button->input->name = button->name;
148 + button->input->phys = "raspberrypi-button/input0";
149 + button->input->dev.parent = dev;
150 + button->poll_rate = HZ;
152 + err = input_register_device(button->input);
154 + dev_err(&pdev->dev, "failed to register input device: %d\n",
159 + err = devm_delayed_work_autocancel(dev, &button->poll_work,
164 + platform_set_drvdata(pdev, button);
165 + schedule_delayed_work(&button->poll_work, button->poll_rate);
170 +static const struct of_device_id rpi_button_match[] = {
171 + { .compatible = "raspberrypi,firmware-button", },
174 +MODULE_DEVICE_TABLE(of, rpi_button_match);
176 +static struct platform_driver rpi_button_driver = {
177 + .probe = rpi_button_probe,
179 + .name = "raspberrypi-button",
180 + .of_match_table = of_match_ptr(rpi_button_match),
183 +module_platform_driver(rpi_button_driver);
185 +MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.com>");
186 +MODULE_DESCRIPTION("Raspberry Pi button driver");
187 +MODULE_LICENSE("Dual BSD/GPL");
188 --- a/include/soc/bcm2835/raspberrypi-firmware.h
189 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
190 @@ -98,6 +98,7 @@ enum rpi_firmware_property_tag {
191 RPI_FIRMWARE_GET_REBOOT_FLAGS = 0x00030064,
192 RPI_FIRMWARE_SET_REBOOT_FLAGS = 0x00038064,
193 RPI_FIRMWARE_NOTIFY_DISPLAY_DONE = 0x00030066,
194 + RPI_FIRMWARE_GET_BUTTONS_PRESSED = 0x00030088,
197 RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE = 0x00040001,