1 From 136ae921cea7e7a189ee99d126253631f880ded0 Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.com>
3 Date: Mon, 13 Jul 2020 10:33:19 +0100
4 Subject: [PATCH] leds: Add the actpwr trigger
6 The actpwr trigger is a meta trigger that cycles between an inverted
7 mmc0 and default-on. It is written in a way that could fairly easily
8 be generalised to support alternative sets of source triggers.
10 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
12 drivers/leds/trigger/Kconfig | 11 ++
13 drivers/leds/trigger/Makefile | 1 +
14 drivers/leds/trigger/ledtrig-actpwr.c | 190 ++++++++++++++++++++++++++
15 3 files changed, 202 insertions(+)
16 create mode 100644 drivers/leds/trigger/ledtrig-actpwr.c
18 --- a/drivers/leds/trigger/Kconfig
19 +++ b/drivers/leds/trigger/Kconfig
20 @@ -151,4 +151,15 @@ config LEDS_TRIGGER_AUDIO
21 the audio mute and mic-mute changes.
24 +config LEDS_TRIGGER_ACTPWR
25 + tristate "ACT/PWR Input Trigger"
26 + depends on LEDS_TRIGGERS
28 + This trigger is intended for platforms that have one software-
29 + controllable LED and no dedicated activity or power LEDs, hence the
30 + need to make the one LED perform both functions. It cycles between
31 + default-on and an inverted mmc0 every 500ms, guaranteeing that it is
32 + on for at least half of the time.
36 --- a/drivers/leds/trigger/Makefile
37 +++ b/drivers/leds/trigger/Makefile
38 @@ -16,3 +16,4 @@ obj-$(CONFIG_LEDS_TRIGGER_PANIC) += ledt
39 obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o
40 obj-$(CONFIG_LEDS_TRIGGER_PATTERN) += ledtrig-pattern.o
41 obj-$(CONFIG_LEDS_TRIGGER_AUDIO) += ledtrig-audio.o
42 +obj-$(CONFIG_LEDS_TRIGGER_ACTPWR) += ledtrig-actpwr.o
44 +++ b/drivers/leds/trigger/ledtrig-actpwr.c
46 +// SPDX-License-Identifier: GPL-2.0
48 + * Activity/power trigger
50 + * Copyright (C) 2020 Raspberry Pi (Trading) Ltd.
52 + * Based on Atsushi Nemoto's ledtrig-heartbeat.c, although there may be
53 + * nothing left of the original now.
56 +#include <linux/module.h>
57 +#include <linux/kernel.h>
58 +#include <linux/init.h>
59 +#include <linux/timer.h>
60 +#include <linux/leds.h>
70 +struct actpwr_trig_src {
77 + struct led_classdev cdev;
78 + struct actpwr_trig_data *parent;
79 + enum led_brightness value;
80 + unsigned int interval;
84 +struct actpwr_trig_data {
85 + struct led_trigger trig;
86 + struct actpwr_vled virt_leds[TRIG_COUNT];
87 + struct actpwr_vled *active;
88 + struct timer_list timer;
92 +static int actpwr_trig_activate(struct led_classdev *led_cdev);
93 +static void actpwr_trig_deactivate(struct led_classdev *led_cdev);
95 +static const struct actpwr_trig_src actpwr_trig_sources[TRIG_COUNT] = {
96 + [TRIG_ACT] = { "mmc0", 500, true },
97 + [TRIG_PWR] = { "default-on", 500, false },
100 +static struct actpwr_trig_data actpwr_data = {
103 + .activate = actpwr_trig_activate,
104 + .deactivate = actpwr_trig_deactivate,
108 +static void actpwr_brightness_set(struct led_classdev *led_cdev,
109 + enum led_brightness value)
111 + struct actpwr_vled *vled = container_of(led_cdev, struct actpwr_vled,
113 + struct actpwr_trig_data *trig = vled->parent;
117 + vled->value = value;
119 + if (vled == trig->active)
120 + led_trigger_event(&trig->trig, value);
123 +static int actpwr_brightness_set_blocking(struct led_classdev *led_cdev,
124 + enum led_brightness value)
126 + actpwr_brightness_set(led_cdev, value);
130 +static enum led_brightness actpwr_brightness_get(struct led_classdev *led_cdev)
132 + struct actpwr_vled *vled = container_of(led_cdev, struct actpwr_vled,
135 + return vled->value;
138 +static void actpwr_trig_cycle(struct timer_list *t)
140 + struct actpwr_trig_data *trig = &actpwr_data;
141 + struct actpwr_vled *active;
143 + active = &trig->virt_leds[trig->next_active];
144 + trig->active = active;
145 + trig->next_active = (trig->next_active + 1) % TRIG_COUNT;
147 + led_trigger_event(&trig->trig, active->value);
149 + mod_timer(&trig->timer, jiffies + msecs_to_jiffies(active->interval));
152 +static int actpwr_trig_activate(struct led_classdev *led_cdev)
154 + struct actpwr_trig_data *trig = &actpwr_data;
156 + /* Start the timer if this is the first LED */
158 + actpwr_trig_cycle(&trig->timer);
160 + led_set_brightness_nosleep(led_cdev, trig->active->value);
165 +static void actpwr_trig_deactivate(struct led_classdev *led_cdev)
167 + struct actpwr_trig_data *trig = &actpwr_data;
169 + if (list_empty(&trig->trig.led_cdevs)) {
170 + del_timer_sync(&trig->timer);
171 + trig->active = NULL;
175 +static int __init actpwr_trig_init(void)
177 + struct actpwr_trig_data *trig = &actpwr_data;
181 + timer_setup(&trig->timer, actpwr_trig_cycle, 0);
183 + /* Register one "LED" for each source trigger */
184 + for (i = 0; i < TRIG_COUNT; i++)
186 + struct actpwr_vled *vled = &trig->virt_leds[i];
187 + struct led_classdev *cdev = &vled->cdev;
188 + const struct actpwr_trig_src *src = &actpwr_trig_sources[i];
190 + vled->parent = trig;
191 + vled->interval = src->interval;
192 + vled->invert = src->invert;
193 + cdev->name = src->name;
194 + cdev->brightness_set = actpwr_brightness_set;
195 + cdev->brightness_set_blocking = actpwr_brightness_set_blocking;
196 + cdev->brightness_get = actpwr_brightness_get;
197 + cdev->default_trigger = src->name;
198 + ret = led_classdev_register(NULL, cdev);
200 + goto error_classdev;
203 + ret = led_trigger_register(&trig->trig);
205 + goto error_classdev;
213 + led_classdev_unregister(&trig->virt_leds[i].cdev);
219 +static void __exit actpwr_trig_exit(void)
223 + led_trigger_unregister(&actpwr_data.trig);
224 + for (i = 0; i < TRIG_COUNT; i++)
226 + led_classdev_unregister(&actpwr_data.virt_leds[i].cdev);
230 +module_init(actpwr_trig_init);
231 +module_exit(actpwr_trig_exit);
233 +MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.com>");
234 +MODULE_DESCRIPTION("ACT/PWR LED trigger");
235 +MODULE_LICENSE("GPL v2");