Skip to contentSkip to conversation

leds: backlight: Register with class backlight

RFC PATCH In review Guido Günther 2 participants 3 messages Nov 25, 2019 LED

lore
Share & tools
lore
#0 lore raw

cover leds: backlight: Register with class backlight

When using current LCD drivers as ledtrig backlight they're not registered with the backlight device class. This has two problems: they're usually not found by userspace since these tools usually look in /sys/class/backlight and they can't be used as backlight phandles in device tree for e.g. LCD panels.

This is an RFC if this is worthwhile at all? A current problem is that changing the LED brightness does currently not notify the class backlight so they can get out of sync but i could look into that if the approach makes sense.

Guido Günther (1):
leds: backlight: register with class backlight too

 drivers/leds/trigger/ledtrig-backlight.c | 54 ++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
Signature · 1 line
2.23.0
#1 lore raw

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

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.

---
 drivers/leds/trigger/ledtrig-backlight.c | 54 ++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/leds/trigger/ledtrig-backlight.c +54 -0
index 487577d22cfc..3967f7014b9a 100644
@@ -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);
 }
Signature · 1 line
2.23.0
#2 lore raw
In reply to Guido Günther

Hi Guido,

You might want to check the pending patch set [0].

Quoted 13 lines · Guido Günther

When using current LCD drivers as ledtrig backlight they're not registered with the backlight device class. This has two problems: they're usually not found by userspace since these tools usually look in /sys/class/backlight and they can't be used as backlight phandles in device tree for e.g. LCD panels.

This is an RFC if this is worthwhile at all? A current problem is that changing the LED brightness does currently not notify the class backlight so they can get out of sync but i could look into that if the approach makes sense.

Quoted 6 lines

Guido Günther (1):
leds: backlight: register with class backlight too

 drivers/leds/trigger/ledtrig-backlight.c | 54 ++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

[0]
https://lore.kernel.org/linux-leds/[email protected]/

Signature · 2 lines
Best regards,
Jacek Anaszewski