Skip to content

Commit 967e353

Browse files
pelwellpopcornmix
authored andcommitted
leds: Add the actpwr trigger
The actpwr trigger is a meta trigger that cycles between an inverted mmc0 and default-on. It is written in a way that could fairly easily be generalised to support alternative sets of source triggers. Signed-off-by: Phil Elwell <phil@raspberrypi.com>
1 parent 5bf42ac commit 967e353

3 files changed

Lines changed: 202 additions & 0 deletions

File tree

‎drivers/leds/trigger/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,15 @@ config LEDS_TRIGGER_AUDIO
151151
the audio mute and mic-mute changes.
152152
If unsure, say N
153153

154+
config LEDS_TRIGGER_ACTPWR
155+
tristate "ACT/PWR Input Trigger"
156+
depends on LEDS_TRIGGERS
157+
help
158+
This trigger is intended for platforms that have one software-
159+
controllable LED and no dedicated activity or power LEDs, hence the
160+
need to make the one LED perform both functions. It cycles between
161+
default-on and an inverted mmc0 every 500ms, guaranteeing that it is
162+
on for at least half of the time.
163+
If unsure, say N.
164+
154165
endif # LEDS_TRIGGERS

‎drivers/leds/trigger/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ obj-$(CONFIG_LEDS_TRIGGER_PANIC) += ledtrig-panic.o
1616
obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o
1717
obj-$(CONFIG_LEDS_TRIGGER_PATTERN) += ledtrig-pattern.o
1818
obj-$(CONFIG_LEDS_TRIGGER_AUDIO) += ledtrig-audio.o
19+
obj-$(CONFIG_LEDS_TRIGGER_ACTPWR) += ledtrig-actpwr.o
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Activity/power trigger
4+
*
5+
* Copyright (C) 2020 Raspberry Pi (Trading) Ltd.
6+
*
7+
* Based on Atsushi Nemoto's ledtrig-heartbeat.c, although there may be
8+
* nothing left of the original now.
9+
*/
10+
11+
#include <linux/module.h>
12+
#include <linux/kernel.h>
13+
#include <linux/init.h>
14+
#include <linux/timer.h>
15+
#include <linux/leds.h>
16+
#include "../leds.h"
17+
18+
enum {
19+
TRIG_ACT,
20+
TRIG_PWR,
21+
22+
TRIG_COUNT
23+
};
24+
25+
struct actpwr_trig_src {
26+
const char *name;
27+
int interval;
28+
bool invert;
29+
};
30+
31+
struct actpwr_vled {
32+
struct led_classdev cdev;
33+
struct actpwr_trig_data *parent;
34+
enum led_brightness value;
35+
unsigned int interval;
36+
bool invert;
37+
};
38+
39+
struct actpwr_trig_data {
40+
struct led_trigger trig;
41+
struct actpwr_vled virt_leds[TRIG_COUNT];
42+
struct actpwr_vled *active;
43+
struct timer_list timer;
44+
int next_active;
45+
};
46+
47+
static int actpwr_trig_activate(struct led_classdev *led_cdev);
48+
static void actpwr_trig_deactivate(struct led_classdev *led_cdev);
49+
50+
static const struct actpwr_trig_src actpwr_trig_sources[TRIG_COUNT] = {
51+
[TRIG_ACT] = { "mmc0", 500, true },
52+
[TRIG_PWR] = { "default-on", 500, false },
53+
};
54+
55+
static struct actpwr_trig_data actpwr_data = {
56+
{
57+
.name = "actpwr",
58+
.activate = actpwr_trig_activate,
59+
.deactivate = actpwr_trig_deactivate,
60+
}
61+
};
62+
63+
static void actpwr_brightness_set(struct led_classdev *led_cdev,
64+
enum led_brightness value)
65+
{
66+
struct actpwr_vled *vled = container_of(led_cdev, struct actpwr_vled,
67+
cdev);
68+
struct actpwr_trig_data *trig = vled->parent;
69+
70+
if (vled->invert)
71+
value = !value;
72+
vled->value = value;
73+
74+
if (vled == trig->active)
75+
led_trigger_event(&trig->trig, value);
76+
}
77+
78+
static int actpwr_brightness_set_blocking(struct led_classdev *led_cdev,
79+
enum led_brightness value)
80+
{
81+
actpwr_brightness_set(led_cdev, value);
82+
return 0;
83+
}
84+
85+
static enum led_brightness actpwr_brightness_get(struct led_classdev *led_cdev)
86+
{
87+
struct actpwr_vled *vled = container_of(led_cdev, struct actpwr_vled,
88+
cdev);
89+
90+
return vled->value;
91+
}
92+
93+
static void actpwr_trig_cycle(struct timer_list *t)
94+
{
95+
struct actpwr_trig_data *trig = &actpwr_data;
96+
struct actpwr_vled *active;
97+
98+
active = &trig->virt_leds[trig->next_active];
99+
trig->active = active;
100+
trig->next_active = (trig->next_active + 1) % TRIG_COUNT;
101+
102+
led_trigger_event(&trig->trig, active->value);
103+
104+
mod_timer(&trig->timer, jiffies + msecs_to_jiffies(active->interval));
105+
}
106+
107+
static int actpwr_trig_activate(struct led_classdev *led_cdev)
108+
{
109+
struct actpwr_trig_data *trig = &actpwr_data;
110+
111+
/* Start the timer if this is the first LED */
112+
if (!trig->active)
113+
actpwr_trig_cycle(&trig->timer);
114+
else
115+
led_set_brightness_nosleep(led_cdev, trig->active->value);
116+
117+
return 0;
118+
}
119+
120+
static void actpwr_trig_deactivate(struct led_classdev *led_cdev)
121+
{
122+
struct actpwr_trig_data *trig = &actpwr_data;
123+
124+
if (list_empty(&trig->trig.led_cdevs)) {
125+
del_timer_sync(&trig->timer);
126+
trig->active = NULL;
127+
}
128+
}
129+
130+
static int __init actpwr_trig_init(void)
131+
{
132+
struct actpwr_trig_data *trig = &actpwr_data;
133+
int ret = 0;
134+
int i;
135+
136+
timer_setup(&trig->timer, actpwr_trig_cycle, 0);
137+
138+
/* Register one "LED" for each source trigger */
139+
for (i = 0; i < TRIG_COUNT; i++)
140+
{
141+
struct actpwr_vled *vled = &trig->virt_leds[i];
142+
struct led_classdev *cdev = &vled->cdev;
143+
const struct actpwr_trig_src *src = &actpwr_trig_sources[i];
144+
145+
vled->parent = trig;
146+
vled->interval = src->interval;
147+
vled->invert = src->invert;
148+
cdev->name = src->name;
149+
cdev->brightness_set = actpwr_brightness_set;
150+
cdev->brightness_set_blocking = actpwr_brightness_set_blocking;
151+
cdev->brightness_get = actpwr_brightness_get;
152+
cdev->default_trigger = src->name;
153+
ret = led_classdev_register(NULL, cdev);
154+
if (ret)
155+
goto error_classdev;
156+
}
157+
158+
ret = led_trigger_register(&trig->trig);
159+
if (ret)
160+
goto error_classdev;
161+
162+
return 0;
163+
164+
error_classdev:
165+
while (i > 0)
166+
{
167+
i--;
168+
led_classdev_unregister(&trig->virt_leds[i].cdev);
169+
}
170+
171+
return ret;
172+
}
173+
174+
static void __exit actpwr_trig_exit(void)
175+
{
176+
int i;
177+
178+
led_trigger_unregister(&actpwr_data.trig);
179+
for (i = 0; i < TRIG_COUNT; i++)
180+
{
181+
led_classdev_unregister(&actpwr_data.virt_leds[i].cdev);
182+
}
183+
}
184+
185+
module_init(actpwr_trig_init);
186+
module_exit(actpwr_trig_exit);
187+
188+
MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.com>");
189+
MODULE_DESCRIPTION("ACT/PWR LED trigger");
190+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)