bcm27xx: add support for linux v5.15
[openwrt/staging/chunkeey.git] / target / linux / bcm27xx / patches-5.15 / 950-0273-leds-Add-the-actpwr-trigger.patch
1 From 989c695cd0822269a5eedef3f10700d87316291e 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 | 190 ++++++++++++++++++++++++++
15 3 files changed, 202 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 @@ -160,4 +160,15 @@ config LEDS_TRIGGER_TTY
21
22 When build as a module this driver will be called ledtrig-tty.
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 @@ -17,3 +17,4 @@ obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += led
39 obj-$(CONFIG_LEDS_TRIGGER_PATTERN) += ledtrig-pattern.o
40 obj-$(CONFIG_LEDS_TRIGGER_AUDIO) += ledtrig-audio.o
41 obj-$(CONFIG_LEDS_TRIGGER_TTY) += ledtrig-tty.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,190 @@
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 +
143 + active = &trig->virt_leds[trig->next_active];
144 + trig->active = active;
145 + trig->next_active = (trig->next_active + 1) % TRIG_COUNT;
146 +
147 + led_trigger_event(&trig->trig, active->value);
148 +
149 + mod_timer(&trig->timer, jiffies + msecs_to_jiffies(active->interval));
150 +}
151 +
152 +static int actpwr_trig_activate(struct led_classdev *led_cdev)
153 +{
154 + struct actpwr_trig_data *trig = &actpwr_data;
155 +
156 + /* Start the timer if this is the first LED */
157 + if (!trig->active)
158 + actpwr_trig_cycle(&trig->timer);
159 + else
160 + led_set_brightness_nosleep(led_cdev, trig->active->value);
161 +
162 + return 0;
163 +}
164 +
165 +static void actpwr_trig_deactivate(struct led_classdev *led_cdev)
166 +{
167 + struct actpwr_trig_data *trig = &actpwr_data;
168 +
169 + if (list_empty(&trig->trig.led_cdevs)) {
170 + del_timer_sync(&trig->timer);
171 + trig->active = NULL;
172 + }
173 +}
174 +
175 +static int __init actpwr_trig_init(void)
176 +{
177 + struct actpwr_trig_data *trig = &actpwr_data;
178 + int ret = 0;
179 + int i;
180 +
181 + timer_setup(&trig->timer, actpwr_trig_cycle, 0);
182 +
183 + /* Register one "LED" for each source trigger */
184 + for (i = 0; i < TRIG_COUNT; i++)
185 + {
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];
189 +
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);
199 + if (ret)
200 + goto error_classdev;
201 + }
202 +
203 + ret = led_trigger_register(&trig->trig);
204 + if (ret)
205 + goto error_classdev;
206 +
207 + return 0;
208 +
209 +error_classdev:
210 + while (i > 0)
211 + {
212 + i--;
213 + led_classdev_unregister(&trig->virt_leds[i].cdev);
214 + }
215 +
216 + return ret;
217 +}
218 +
219 +static void __exit actpwr_trig_exit(void)
220 +{
221 + int i;
222 +
223 + led_trigger_unregister(&actpwr_data.trig);
224 + for (i = 0; i < TRIG_COUNT; i++)
225 + {
226 + led_classdev_unregister(&actpwr_data.virt_leds[i].cdev);
227 + }
228 +}
229 +
230 +module_init(actpwr_trig_init);
231 +module_exit(actpwr_trig_exit);
232 +
233 +MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.com>");
234 +MODULE_DESCRIPTION("ACT/PWR LED trigger");
235 +MODULE_LICENSE("GPL v2");