[PATCH 1/2] Input: synaptics-rmi4 - support the LEDs of function $30

From: ahmad baghereslami

Date: Thu Dec 03 2026 - 07:51:30 EST


Function $30 can drive LEDs: query 0 says whether it has any, control 0
selects which of its GPIO/LED pins are LEDs (all of them when the
function has no GPIOs), a bit per pin in control 4 turns the LED on that
pin on, and control 6 holds a brightness byte per pin. rmi_f30 already
works out where these registers are, but does nothing with the LEDs.

Register an LED class device for each LED pin, named
"rmi4-XX.fn30::ledN" after the pin. Setting the brightness writes it to
control 6 and sets the pin's bit in control 4, and brightness 0 clears
that bit. The values are kept in ctrl_regs, so rmi_f30_config() puts
them back after a reset; a mutex keeps the two from interleaving. A
pin's LED starts with the brightness in control 6 if its bit in control
4 is already set.

The Synaptics TM2706-002 touchpad of the HP ZBook 17 G3 has an LED in
its top left corner that HP's Windows driver lights while the touchpad
is disabled. It is pin 3 of four, with the control registers at 0x55 on
page 1, and HP's driver uses brightness 0x0b.

Tested on an HP ZBook 17 G3 over SMBus with Ubuntu's 7.0.0 kernel: the
firmware leaves the LED off with brightness 0x05; brightness 1 lights it
dimly and 11, 40 and 255 brightly, 0 turns it off; the LED goes off when
the driver unbinds, and comes back after reloading the driver and after
suspend and resume. The touchpad and the pointing stick keep working.

Assisted-by: LLM
Signed-off-by: ahmad baghereslami <ahmad.b1995@xxxxxxxxx>
---
drivers/input/rmi4/rmi_f30.c | 113 ++++++++++++++++++++++++++++++++++-
1 file changed, 110 insertions(+), 3 deletions(-)

diff --git a/drivers/input/rmi4/rmi_f30.c b/drivers/input/rmi4/rmi_f30.c
index b2155c8e2..55428e9a9 100644
--- a/drivers/input/rmi4/rmi_f30.c
+++ b/drivers/input/rmi4/rmi_f30.c
@@ -6,6 +6,8 @@
#include <linux/kernel.h>
#include <linux/rmi.h>
#include <linux/input.h>
+#include <linux/leds.h>
+#include <linux/mutex.h>
#include <linux/slab.h>
#include "rmi_driver.h"

@@ -54,6 +56,15 @@ struct rmi_f30_ctrl_data {
u8 *regs;
};

+struct f30_data;
+
+struct rmi_f30_led {
+ struct led_classdev cdev;
+ struct rmi_function *fn;
+ struct f30_data *f30;
+ unsigned int pin;
+};
+
struct f30_data {
/* Query Data */
bool has_extended_pattern;
@@ -71,6 +82,8 @@ struct f30_data {
struct rmi_f30_ctrl_data ctrl[RMI_F30_CTRL_MAX_REG_BLOCKS];
u8 ctrl_regs[RMI_F30_CTRL_REGS_MAX_SIZE];
u32 ctrl_regs_size;
+ /* Serializes LED updates of ctrl_regs with writing them back. */
+ struct mutex ctrl_mutex;

u8 data_regs[RMI_F30_CTRL_MAX_BYTES];
u16 *gpioled_key_map;
@@ -182,8 +195,11 @@ static int rmi_f30_config(struct rmi_function *fn)
drv->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
} else {
/* Write Control Register values back to device */
- error = rmi_write_block(fn->rmi_dev, fn->fd.control_base_addr,
- f30->ctrl_regs, f30->ctrl_regs_size);
+ scoped_guard(mutex, &f30->ctrl_mutex)
+ error = rmi_write_block(fn->rmi_dev,
+ fn->fd.control_base_addr,
+ f30->ctrl_regs,
+ f30->ctrl_regs_size);
if (error) {
dev_err(&fn->dev,
"%s: Could not write control registers at 0x%x: %d\n",
@@ -363,6 +379,92 @@ static int rmi_f30_initialize(struct rmi_function *fn, struct f30_data *f30)
return 0;
}

+/*
+ * Control 4 has a bit per pin that turns the LED on that pin on, and
+ * control 6 a byte per pin with the brightness of its LED. The values are
+ * kept in ctrl_regs, which rmi_f30_config() writes back after a reset.
+ */
+static int rmi_f30_led_set(struct led_classdev *cdev,
+ enum led_brightness value)
+{
+ struct rmi_f30_led *led = container_of(cdev, struct rmi_f30_led, cdev);
+ struct rmi_device *rmi_dev = led->fn->rmi_dev;
+ struct rmi_f30_ctrl_data *on = &led->f30->ctrl[4];
+ struct rmi_f30_ctrl_data *level = &led->f30->ctrl[6];
+ unsigned int reg = led->pin >> 3;
+ u8 mask = BIT(led->pin & 0x07);
+ int error;
+
+ guard(mutex)(&led->f30->ctrl_mutex);
+
+ if (value) {
+ error = rmi_write(rmi_dev, level->address + led->pin, value);
+ if (error)
+ return error;
+
+ level->regs[led->pin] = value;
+ on->regs[reg] |= mask;
+ } else {
+ on->regs[reg] &= ~mask;
+ }
+
+ return rmi_write(rmi_dev, on->address + reg, on->regs[reg]);
+}
+
+static bool rmi_f30_is_led(struct f30_data *f30, unsigned int pin)
+{
+ /* Without GPIOs there is no LED select register: all pins are LEDs. */
+ if (!f30->has_gpio)
+ return true;
+
+ return f30->ctrl[0].regs[pin >> 3] & BIT(pin & 0x07);
+}
+
+static int rmi_f30_register_leds(struct rmi_function *fn, struct f30_data *f30)
+{
+ struct rmi_f30_led *led;
+ unsigned int pin;
+ int error;
+
+ if (!IS_REACHABLE(CONFIG_LEDS_CLASS) || !f30->has_led)
+ return 0;
+
+ for (pin = 0; pin < f30->gpioled_count; pin++) {
+ if (!rmi_f30_is_led(f30, pin))
+ continue;
+
+ led = devm_kzalloc(&fn->dev, sizeof(*led), GFP_KERNEL);
+ if (!led)
+ return -ENOMEM;
+
+ led->fn = fn;
+ led->f30 = f30;
+ led->pin = pin;
+ led->cdev.name = devm_kasprintf(&fn->dev, GFP_KERNEL, "%s::led%u",
+ dev_name(&fn->dev), pin);
+ if (!led->cdev.name)
+ return -ENOMEM;
+
+ led->cdev.max_brightness = U8_MAX;
+ led->cdev.brightness_set_blocking = rmi_f30_led_set;
+ if (f30->ctrl[4].regs[pin >> 3] & BIT(pin & 0x07))
+ led->cdev.brightness = f30->ctrl[6].regs[pin];
+
+ rmi_dbg(RMI_DEBUG_FN, &fn->dev,
+ "%s: LED on pin %u, on %#x, brightness %#x\n", __func__,
+ pin, f30->ctrl[4].regs[pin >> 3], f30->ctrl[6].regs[pin]);
+
+ error = devm_led_classdev_register(&fn->dev, &led->cdev);
+ if (error) {
+ dev_err(&fn->dev, "Failed to register LED %u: %d\n",
+ pin, error);
+ return error;
+ }
+ }
+
+ return 0;
+}
+
static int rmi_f30_probe(struct rmi_function *fn)
{
struct rmi_device *rmi_dev = fn->rmi_dev;
@@ -386,12 +488,17 @@ static int rmi_f30_probe(struct rmi_function *fn)

f30->input = drv_data->input;

+ error = devm_mutex_init(&fn->dev, &f30->ctrl_mutex);
+ if (error)
+ return error;
+
error = rmi_f30_initialize(fn, f30);
if (error)
return error;

dev_set_drvdata(&fn->dev, f30);
- return 0;
+
+ return rmi_f30_register_leds(fn, f30);
}

struct rmi_function_handler rmi_f30_handler = {
--
2.53.0