[PATCH RFC] leds: Add support for per-LED device triggers
From: Ondrej Jirman
Date: Thu Jul 02 2020 - 10:47:43 EST
Some LED controllers may come with an internal HW triggering mechanism
for the LED and an ability to switch between user control of the LED,
or the internal control. One such example is AXP20X PMIC, that allows
wither for user control of the LED, or for internal control based on
the state of the battery charger.
Add support for registering per-LED device trigger.
Names of private triggers need to be globally unique, but may clash
with other private triggers. This is enforced during trigger
registration. Developers can register private triggers just like
the normal triggers, by setting private_led to a classdev
of the LED the trigger is associated with.
Signed-off-by: Ondrej Jirman <megous@xxxxxxxxxx>
---
drivers/leds/led-triggers.c | 12 +++++++++---
include/linux/leds.h | 3 +++
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c
index 79e30d2cb7a5..3011e2658404 100644
--- a/drivers/leds/led-triggers.c
+++ b/drivers/leds/led-triggers.c
@@ -50,7 +50,8 @@ ssize_t led_trigger_write(struct file *filp, struct kobject *kobj,
down_read(&triggers_list_lock);
list_for_each_entry(trig, &trigger_list, next_trig) {
- if (sysfs_streq(buf, trig->name)) {
+ if (sysfs_streq(buf, trig->name) &&
+ (!trig->private_led || trig->private_led == led_cdev)) {
down_write(&led_cdev->trigger_lock);
led_trigger_set(led_cdev, trig);
up_write(&led_cdev->trigger_lock);
@@ -96,6 +97,9 @@ static int led_trigger_format(char *buf, size_t size,
bool hit = led_cdev->trigger &&
!strcmp(led_cdev->trigger->name, trig->name);
+ if (trig->private_led && trig->private_led != led_cdev)
+ continue;
+
len += led_trigger_snprintf(buf + len, size - len,
" %s%s%s", hit ? "[" : "",
trig->name, hit ? "]" : "");
@@ -243,7 +247,8 @@ void led_trigger_set_default(struct led_classdev *led_cdev)
down_read(&triggers_list_lock);
down_write(&led_cdev->trigger_lock);
list_for_each_entry(trig, &trigger_list, next_trig) {
- if (!strcmp(led_cdev->default_trigger, trig->name)) {
+ if (!strcmp(led_cdev->default_trigger, trig->name) &&
+ (!trig->private_led || trig->private_led == led_cdev)) {
led_cdev->flags |= LED_INIT_DEFAULT_TRIGGER;
led_trigger_set(led_cdev, trig);
break;
@@ -280,7 +285,8 @@ int led_trigger_register(struct led_trigger *trig)
down_write(&triggers_list_lock);
/* Make sure the trigger's name isn't already in use */
list_for_each_entry(_trig, &trigger_list, next_trig) {
- if (!strcmp(_trig->name, trig->name)) {
+ if (!strcmp(_trig->name, trig->name) &&
+ (!_trig->private_led || _trig->private_led == trig->private_led)) {
up_write(&triggers_list_lock);
return -EEXIST;
}
diff --git a/include/linux/leds.h b/include/linux/leds.h
index 2451962d1ec5..0d7577c752ad 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -345,6 +345,9 @@ struct led_trigger {
int (*activate)(struct led_classdev *led_cdev);
void (*deactivate)(struct led_classdev *led_cdev);
+ /* LED-private triggers have the LED device set here. */
+ struct led_classdev *private_led;
+
/* LEDs under control by this trigger (for simple triggers) */
rwlock_t leddev_list_lock;
struct list_head led_cdevs;
--
2.27.0