[RFC PATCH 1/1] leds: backlight: register with class backlight too

From: Guido GÃnther
Date: Mon Nov 25 2019 - 09:50:59 EST


This allows it to be used as backlight device in device tree (e.g. to
link it to a LCD panel) and helps userspace since it shows up in
/sys/class/backlight then.

Signed-off-by: Guido GÃnther <agx@xxxxxxxxxxx>
---
drivers/leds/trigger/ledtrig-backlight.c | 54 ++++++++++++++++++++++++
1 file changed, 54 insertions(+)

diff --git a/drivers/leds/trigger/ledtrig-backlight.c b/drivers/leds/trigger/ledtrig-backlight.c
index 487577d22cfc..3967f7014b9a 100644
--- a/drivers/leds/trigger/ledtrig-backlight.c
+++ b/drivers/leds/trigger/ledtrig-backlight.c
@@ -22,6 +22,9 @@ struct bl_trig_notifier {
int brightness;
int old_status;
struct notifier_block notifier;
+#ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
+ struct backlight_device *backlight_device;
+#endif
unsigned invert;
};

@@ -98,11 +101,40 @@ static struct attribute *bl_trig_attrs[] = {
};
ATTRIBUTE_GROUPS(bl_trig);

+#ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
+static int bl_get_brightness(struct backlight_device *b)
+{
+ struct led_classdev *led = bl_get_data(b);
+
+ return led_get_brightness(led);
+}
+
+static int bl_update_status(struct backlight_device *b)
+{
+ struct led_classdev *led = bl_get_data(b);
+
+ if (b->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
+ led_set_brightness_nosleep(led, 0);
+ else
+ led_set_brightness_nosleep(led, b->props.brightness);
+
+ return 0;
+}
+
+static const struct backlight_ops backlight_ops = {
+ .get_brightness = bl_get_brightness,
+ .update_status = bl_update_status,
+};
+#endif
+
static int bl_trig_activate(struct led_classdev *led)
{
int ret;

struct bl_trig_notifier *n;
+#ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
+ struct backlight_properties props;
+#endif

n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
if (!n)
@@ -118,13 +150,35 @@ static int bl_trig_activate(struct led_classdev *led)
if (ret)
dev_err(led->dev, "unable to register backlight trigger\n");

+#ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
+ memset(&props, 0, sizeof(struct backlight_properties));
+ props.type = BACKLIGHT_PLATFORM;
+ props.max_brightness = LED_FULL;
+ props.brightness = led->brightness;
+ n->backlight_device = backlight_device_register(led->name,
+ led->dev,
+ led,
+ &backlight_ops,
+ &props);
+ if (IS_ERR(n->backlight_device)) {
+ ret = IS_ERR(n->backlight_device);
+ goto out;
+ }
+#endif
+
return 0;
+out:
+ fb_unregister_client(&n->notifier);
+ return ret;
}

static void bl_trig_deactivate(struct led_classdev *led)
{
struct bl_trig_notifier *n = led_get_trigger_data(led);

+#ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
+ backlight_device_unregister(n->backlight_device);
+#endif
fb_unregister_client(&n->notifier);
kfree(n);
}
--
2.23.0