firmware-utils: bump to git HEAD
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0916-leds-Add-the-actpwr-trigger.patch
1 From 6586d75915287dd336b71e17826f037be7926ebe 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
5
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.
9
10 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
11 ---
12 drivers/leds/trigger/Kconfig | 11 ++
13 drivers/leds/trigger/Makefile | 1 +
14 drivers/leds/trigger/ledtrig-actpwr.c | 191 ++++++++++++++++++++++++++
15 3 files changed, 203 insertions(+)
16 create mode 100644 drivers/leds/trigger/ledtrig-actpwr.c
17
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.
22 If unsure, say N
23
24 +config LEDS_TRIGGER_ACTPWR
25 + tristate "ACT/PWR Input Trigger"
26 + depends on LEDS_TRIGGERS
27 + help
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.
33 + If unsure, say N.
34 +
35 endif # LEDS_TRIGGERS
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
43 --- /dev/null
44 +++ b/drivers/leds/trigger/ledtrig-actpwr.c
45 @@ -0,0 +1,191 @@
46 +// SPDX-License-Identifier: GPL-2.0
47 +/*
48 + * Activity/power trigger
49 + *
50 + * Copyright (C) 2020 Raspberry Pi (Trading) Ltd.
51 + *
52 + * Based on Atsushi Nemoto's ledtrig-heartbeat.c, although there may be
53 + * nothing left of the original now.
54 + */
55 +
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>
61 +#include "../leds.h"
62 +
63 +enum {
64 + TRIG_ACT,
65 + TRIG_PWR,
66 +
67 + TRIG_COUNT
68 +};
69 +
70 +struct actpwr_trig_src {
71 + const char *name;
72 + int interval;
73 + bool invert;
74 +};
75 +
76 +struct actpwr_vled {
77 + struct led_classdev cdev;
78 + struct actpwr_trig_data *parent;
79 + enum led_brightness value;
80 + unsigned int interval;
81 + bool invert;
82 +};
83 +
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;
89 + int next_active;
90 +};
91 +
92 +static int actpwr_trig_activate(struct led_classdev *led_cdev);
93 +static void actpwr_trig_deactivate(struct led_classdev *led_cdev);
94 +
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 },
98 +};
99 +
100 +static struct actpwr_trig_data actpwr_data = {
101 + {
102 + .name = "actpwr",
103 + .activate = actpwr_trig_activate,
104 + .deactivate = actpwr_trig_deactivate,
105 + }
106 +};
107 +
108 +static void actpwr_brightness_set(struct led_classdev *led_cdev,
109 + enum led_brightness value)
110 +{
111 + struct actpwr_vled *vled = container_of(led_cdev, struct actpwr_vled,
112 + cdev);
113 + struct actpwr_trig_data *trig = vled->parent;
114 +
115 + if (vled->invert)
116 + value = !value;
117 + vled->value = value;
118 +
119 + if (vled == trig->active)
120 + led_trigger_event(&trig->trig, value);
121 +}
122 +
123 +static int actpwr_brightness_set_blocking(struct led_classdev *led_cdev,
124 + enum led_brightness value)
125 +{
126 + actpwr_brightness_set(led_cdev, value);
127 + return 0;
128 +}
129 +
130 +static enum led_brightness actpwr_brightness_get(struct led_classdev *led_cdev)
131 +{
132 + struct actpwr_vled *vled = container_of(led_cdev, struct actpwr_vled,
133 + cdev);
134 +
135 + return vled->value;
136 +}
137 +
138 +static void actpwr_trig_cycle(struct timer_list *t)
139 +{
140 + struct actpwr_trig_data *trig = &actpwr_data;
141 + struct actpwr_vled *active;
142 + enum led_brightness value;
143 +
144 + active = &trig->virt_leds[trig->next_active];
145 + trig->active = active;
146 + trig->next_active = (trig->next_active + 1) % TRIG_COUNT;
147 +
148 + led_trigger_event(&trig->trig, active->value);
149 +
150 + mod_timer(&trig->timer, jiffies + msecs_to_jiffies(active->interval));
151 +}
152 +
153 +static int actpwr_trig_activate(struct led_classdev *led_cdev)
154 +{
155 + struct actpwr_trig_data *trig = &actpwr_data;
156 +
157 + /* Start the timer if this is the first LED */
158 + if (!trig->active)
159 + actpwr_trig_cycle(&trig->timer);
160 + else
161 + led_set_brightness_nosleep(led_cdev, trig->active->value);
162 +
163 + return 0;
164 +}
165 +
166 +static void actpwr_trig_deactivate(struct led_classdev *led_cdev)
167 +{
168 + struct actpwr_trig_data *trig = &actpwr_data;
169 +
170 + if (list_empty(&trig->trig.led_cdevs)) {
171 + del_timer_sync(&trig->timer);
172 + trig->active = NULL;
173 + }
174 +}
175 +
176 +static int __init actpwr_trig_init(void)
177 +{
178 + struct actpwr_trig_data *trig = &actpwr_data;
179 + int ret = 0;
180 + int i;
181 +
182 + timer_setup(&trig->timer, actpwr_trig_cycle, 0);
183 +
184 + /* Register one "LED" for each source trigger */
185 + for (i = 0; i < TRIG_COUNT; i++)
186 + {
187 + struct actpwr_vled *vled = &trig->virt_leds[i];
188 + struct led_classdev *cdev = &vled->cdev;
189 + const struct actpwr_trig_src *src = &actpwr_trig_sources[i];
190 +
191 + vled->parent = trig;
192 + vled->interval = src->interval;
193 + vled->invert = src->invert;
194 + cdev->name = src->name;
195 + cdev->brightness_set = actpwr_brightness_set;
196 + cdev->brightness_set_blocking = actpwr_brightness_set_blocking;
197 + cdev->brightness_get = actpwr_brightness_get;
198 + cdev->default_trigger = src->name;
199 + ret = led_classdev_register(NULL, cdev);
200 + if (ret)
201 + goto error_classdev;
202 + }
203 +
204 + ret = led_trigger_register(&trig->trig);
205 + if (ret)
206 + goto error_classdev;
207 +
208 + return 0;
209 +
210 +error_classdev:
211 + while (i > 0)
212 + {
213 + i--;
214 + led_classdev_unregister(&trig->virt_leds[i].cdev);
215 + }
216 +
217 + return ret;
218 +}
219 +
220 +static void __exit actpwr_trig_exit(void)
221 +{
222 + int i;
223 +
224 + led_trigger_unregister(&actpwr_data.trig);
225 + for (i = 0; i < TRIG_COUNT; i++)
226 + {
227 + led_classdev_unregister(&actpwr_data.virt_leds[i].cdev);
228 + }
229 +}
230 +
231 +module_init(actpwr_trig_init);
232 +module_exit(actpwr_trig_exit);
233 +
234 +MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.com>");
235 +MODULE_DESCRIPTION("ACT/PWR LED trigger");
236 +MODULE_LICENSE("GPL v2");