bcm27xx: update 6.1 patches from RPi foundation
[openwrt/staging/xback.git] / target / linux / bcm27xx / patches-6.1 / 950-0925-Input-Add-raspberrypi-button-firmware-driver.patch
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
5
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.
8
9 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
10 ---
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
17
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.
23
24 +config INPUT_RASPBERRYPI_BUTTON
25 + tristate "Raspberry Pi button support"
26 + depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && !RASPBERRYPI_FIRMWARE)
27 + help
28 + This enables support for firmware-controlled buttons on Raspberry
29 + Pi devices.
30 +
31 + To compile this driver as a module, choose M here. the module will
32 + be called raspberrypi-button.
33 +
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
47 --- /dev/null
48 +++ b/drivers/input/misc/raspberrypi-button.c
49 @@ -0,0 +1,138 @@
50 +// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
51 +/*
52 + * Driver for Raspberry Pi power button
53 + *
54 + * Copyright (C) 2023 Raspberry Pi Ltd.
55 + *
56 + * This driver is based on drivers/hwmon/raspberrypi-hwmon.c and
57 + * input/misc/pm8941-pwrkey.c/ - see original files for copyright information
58 + */
59 +
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>
74 +
75 +struct rpi_button {
76 + struct device *dev;
77 + struct rpi_firmware *fw;
78 + struct input_dev *input;
79 + struct delayed_work poll_work;
80 + unsigned long poll_rate;
81 + const char *name;
82 + u32 id;
83 + u32 code;
84 +};
85 +
86 +static void button_poll(struct work_struct *work)
87 +{
88 + struct rpi_button *button;
89 + u32 value;
90 + int err;
91 +
92 + button = container_of(work, struct rpi_button,
93 + poll_work.work);
94 +
95 + value = BIT(button->id);
96 + err = rpi_firmware_property(button->fw, RPI_FIRMWARE_GET_BUTTONS_PRESSED,
97 + &value, sizeof(value));
98 + if (err) {
99 + dev_err_once(button->dev, "GET_BUTTON_PRESSED not implemented?\n");
100 + return;
101 + }
102 +
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);
108 + }
109 +
110 + schedule_delayed_work(&button->poll_work, button->poll_rate);
111 +}
112 +
113 +static int rpi_button_probe(struct platform_device *pdev)
114 +{
115 + struct device *dev = &pdev->dev;
116 + struct rpi_button *button;
117 + int err;
118 +
119 + button = devm_kzalloc(dev, sizeof(*button), GFP_KERNEL);
120 + if (!button)
121 + return -ENOMEM;
122 +
123 + button->dev = dev;
124 +
125 + /* Get the firmware pointer from our parent */
126 + button->fw = dev_get_drvdata(dev->parent);
127 +
128 + if (device_property_read_u32(dev, "id", &button->id))
129 + button->id = RASPBERRYPI_BUTTON_POWER;
130 +
131 + if (device_property_read_string(dev, "label", &button->name))
132 + button->name = "raspberrypi-button";
133 +
134 + if (device_property_read_u32(dev, "linux,code", &button->code)) {
135 + dev_err(&pdev->dev, "no linux,code property\n");
136 + return -EINVAL;
137 + }
138 +
139 + button->input = devm_input_allocate_device(dev);
140 + if (!button->input) {
141 + dev_dbg(&pdev->dev, "unable to allocate input device\n");
142 + return -ENOMEM;
143 + }
144 +
145 + input_set_capability(button->input, EV_KEY, button->code);
146 +
147 + button->input->name = button->name;
148 + button->input->phys = "raspberrypi-button/input0";
149 + button->input->dev.parent = dev;
150 + button->poll_rate = HZ;
151 +
152 + err = input_register_device(button->input);
153 + if (err) {
154 + dev_err(&pdev->dev, "failed to register input device: %d\n",
155 + err);
156 + return err;
157 + }
158 +
159 + err = devm_delayed_work_autocancel(dev, &button->poll_work,
160 + button_poll);
161 + if (err)
162 + return err;
163 +
164 + platform_set_drvdata(pdev, button);
165 + schedule_delayed_work(&button->poll_work, button->poll_rate);
166 +
167 + return 0;
168 +}
169 +
170 +static const struct of_device_id rpi_button_match[] = {
171 + { .compatible = "raspberrypi,firmware-button", },
172 + { }
173 +};
174 +MODULE_DEVICE_TABLE(of, rpi_button_match);
175 +
176 +static struct platform_driver rpi_button_driver = {
177 + .probe = rpi_button_probe,
178 + .driver = {
179 + .name = "raspberrypi-button",
180 + .of_match_table = of_match_ptr(rpi_button_match),
181 + },
182 +};
183 +module_platform_driver(rpi_button_driver);
184 +
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,
195
196 /* Dispmanx TAGS */
197 RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE = 0x00040001,