History

Merge tag 'leds-for-5.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds

Pull LED updates from Jacek Anaszewski: - finalize previously announced support for initialization of pattern triggers from Device Tree - fix for null deref on firmware load failure in leds-lp55xx-common.c * tag 'leds-for-5.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds: leds: lp55xx: fix null deref on firmware load failure leds: trigger: timer: Add initialization from Device Tree leds: trigger: oneshot: Add initialization from Device Tree leds: trigger: pattern: Add pattern initialization from Device Tree leds: Add helper for getting default pattern from Device Tree dt-bindings: leds: Add pattern initialization from Device Tree

Linus Torvalds committed 7 years ago

Commit cd2a3bf2 parents 7629bac 5ddb0869 files · +259 −71Browse files

Unified · Split

9 files
  1. Documentation/ABI/testing/sysfs-class-led-trigger-pattern +3 −48
  2. Documentation/devicetree/bindings/leds/common.txt +12 −0
  3. Documentation/devicetree/bindings/leds/leds-trigger-pattern.txt +49 −0
  4. drivers/leds/led-core.c +30 −0
  5. drivers/leds/leds-lp55xx-common.c +2 −2
  6. drivers/leds/trigger/ledtrig-oneshot.c +36 −2
  7. drivers/leds/trigger/ledtrig-pattern.c +80 −19
  8. drivers/leds/trigger/ledtrig-timer.c +34 −0
  9. include/linux/leds.h +13 −0
Documentation/ABI/testing/sysfs-class-led-trigger-pattern+3−48
@@ -7,55 +7,10 @@ Description: · 6 unmodified lines
7 timer. It can do gradual dimming and step change of brightness.7 timer. It can do gradual dimming and step change of brightness.
8 8
9 The pattern is given by a series of tuples, of brightness and9 The pattern is given by a series of tuples, of brightness and
10 duration (ms). The LED is expected to traverse the series and10 duration (ms).
11 each brightness value for the specified duration. Duration of
12 0 means brightness should immediately change to new value, and
13 writing malformed pattern deactivates any active one.
14 11
15 1. For gradual dimming, the dimming interval now is set as 5012 The exact format is described in:
16 milliseconds. So the tuple with duration less than dimming13 Documentation/devicetree/bindings/leds/leds-trigger-pattern.txt
17 interval (50ms) is treated as a step change of brightness,
18 i.e. the subsequent brightness will be applied without adding
19 intervening dimming intervals.
20
21 The gradual dimming format of the software pattern values should be:
22 "brightness_1 duration_1 brightness_2 duration_2 brightness_3
23 duration_3 ...". For example:
24
25 echo 0 1000 255 2000 > pattern
26
27 It will make the LED go gradually from zero-intensity to max (255)
28 intensity in 1000 milliseconds, then back to zero intensity in 2000
29 milliseconds:
30
31 LED brightness
32 ^
33 255-| / \ / \ /
34 | / \ / \ /
35 | / \ / \ /
36 | / \ / \ /
37 0-| / \/ \/
38 +---0----1----2----3----4----5----6------------> time (s)
39
40 2. To make the LED go instantly from one brightness value to another,
41 we should use zero-time lengths (the brightness must be same as
42 the previous tuple's). So the format should be:
43 "brightness_1 duration_1 brightness_1 0 brightness_2 duration_2
44 brightness_2 0 ...". For example:
45
46 echo 0 1000 0 0 255 2000 255 0 > pattern
47
48 It will make the LED stay off for one second, then stay at max brightness
49 for two seconds:
50
51 LED brightness
52 ^
53 255-| +---------+ +---------+
54 | | | | |
55 | | | | |
56 | | | | |
57 0-| -----+ +----+ +----
58 +---0----1----2----3----4----5----6------------> time (s)
59 14
60What: /sys/class/leds/<led>/hw_pattern15What: /sys/class/leds/<led>/hw_pattern
61Date: September 201816Date: September 2018
Documentation/devicetree/bindings/leds/common.txt+12−0
@@ -37,6 +37,18 @@ Optional properties for child nodes: · 36 unmodified lines
37 "ide-disk" - LED indicates IDE disk activity (deprecated),37 "ide-disk" - LED indicates IDE disk activity (deprecated),
38 in new implementations use "disk-activity"38 in new implementations use "disk-activity"
39 "timer" - LED flashes at a fixed, configurable rate39 "timer" - LED flashes at a fixed, configurable rate
40 "pattern" - LED alters the brightness for the specified duration with one
41 software timer (requires "led-pattern" property)
42
43- led-pattern : Array of integers with default pattern for certain triggers.
44 Each trigger may parse this property differently:
45 - one-shot : two numbers specifying delay on and delay off (in ms),
46 - timer : two numbers specifying delay on and delay off (in ms),
47 - pattern : the pattern is given by a series of tuples, of
48 brightness and duration (in ms). The exact format is
49 described in:
50 Documentation/devicetree/bindings/leds/leds-trigger-pattern.txt
51
40 52
41- led-max-microamp : Maximum LED supply current in microamperes. This property53- led-max-microamp : Maximum LED supply current in microamperes. This property
42 can be made mandatory for the board configurations54 can be made mandatory for the board configurations
Documentation/devicetree/bindings/leds/leds-trigger-pattern.txt+49−0
@@ -0,0 +1,49 @@
1* Pattern format for LED pattern trigger
2
3The pattern is given by a series of tuples, of brightness and duration (ms).
4The LED is expected to traverse the series and each brightness value for the
5specified duration. Duration of 0 means brightness should immediately change to
6new value, and writing malformed pattern deactivates any active one.
7
81. For gradual dimming, the dimming interval now is set as 50 milliseconds. So
9the tuple with duration less than dimming interval (50ms) is treated as a step
10change of brightness, i.e. the subsequent brightness will be applied without
11adding intervening dimming intervals.
12
13The gradual dimming format of the software pattern values should be:
14"brightness_1 duration_1 brightness_2 duration_2 brightness_3 duration_3 ...".
15For example (using sysfs interface):
16
17echo 0 1000 255 2000 > pattern
18
19It will make the LED go gradually from zero-intensity to max (255) intensity in
201000 milliseconds, then back to zero intensity in 2000 milliseconds:
21
22LED brightness
23 ^
24255-| / \ / \ /
25 | / \ / \ /
26 | / \ / \ /
27 | / \ / \ /
28 0-| / \/ \/
29 +---0----1----2----3----4----5----6------------> time (s)
30
312. To make the LED go instantly from one brightness value to another, we should
32use zero-time lengths (the brightness must be same as the previous tuple's). So
33the format should be: "brightness_1 duration_1 brightness_1 0 brightness_2
34duration_2 brightness_2 0 ...".
35For example (using sysfs interface):
36
37echo 0 1000 0 0 255 2000 255 0 > pattern
38
39It will make the LED stay off for one second, then stay at max brightness for
40two seconds:
41
42LED brightness
43 ^
44255-| +---------+ +---------+
45 | | | | |
46 | | | | |
47 | | | | |
48 0-| -----+ +----+ +----
49 +---0----1----2----3----4----5----6------------> time (s)
drivers/leds/led-core.c+30−0
@@ -16,7 +16,9 @@ · 15 unmodified lines
16#include <linux/list.h>16#include <linux/list.h>
17#include <linux/module.h>17#include <linux/module.h>
18#include <linux/mutex.h>18#include <linux/mutex.h>
19#include <linux/of.h>
19#include <linux/rwsem.h>20#include <linux/rwsem.h>
21#include <linux/slab.h>
20#include "leds.h"22#include "leds.h"
21 23
22DECLARE_RWSEM(leds_list_lock);24DECLARE_RWSEM(leds_list_lock);
@@ -310,6 +312,34 @@ int led_update_brightness(struct led_classdev *led_cdev) · 287 unmodified lines
310}312}
311EXPORT_SYMBOL_GPL(led_update_brightness);313EXPORT_SYMBOL_GPL(led_update_brightness);
312 314
315u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
316{
317 struct device_node *np = dev_of_node(led_cdev->dev);
318 u32 *pattern;
319 int count;
320
321 if (!np)
322 return NULL;
323
324 count = of_property_count_u32_elems(np, "led-pattern");
325 if (count < 0)
326 return NULL;
327
328 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
329 if (!pattern)
330 return NULL;
331
332 if (of_property_read_u32_array(np, "led-pattern", pattern, count)) {
333 kfree(pattern);
334 return NULL;
335 }
336
337 *size = count;
338
339 return pattern;
340}
341EXPORT_SYMBOL_GPL(led_get_default_pattern);
342
313/* Caller must ensure led_cdev->led_access held */343/* Caller must ensure led_cdev->led_access held */
314void led_sysfs_disable(struct led_classdev *led_cdev)344void led_sysfs_disable(struct led_classdev *led_cdev)
315{345{
drivers/leds/leds-lp55xx-common.c+2−2
@@ -201,7 +201,7 @@ static void lp55xx_firmware_loaded(const struct firmware *fw, void *context) · 200 unmodified lines
201 201
202 if (!fw) {202 if (!fw) {
203 dev_err(dev, "firmware request failed\n");203 dev_err(dev, "firmware request failed\n");
204 goto out;204 return;
205 }205 }
206 206
207 /* handling firmware data is chip dependent */207 /* handling firmware data is chip dependent */
@@ -214,9 +214,9 @@ static void lp55xx_firmware_loaded(const struct firmware *fw, void *context) · 6 unmodified lines
214 214
215 mutex_unlock(&chip->lock);215 mutex_unlock(&chip->lock);
216 216
217out:
218 /* firmware should be released for other channel use */217 /* firmware should be released for other channel use */
219 release_firmware(chip->fw);218 release_firmware(chip->fw);
219 chip->fw = NULL;
220}220}
221 221
222static int lp55xx_request_firmware(struct lp55xx_chip *chip)222static int lp55xx_request_firmware(struct lp55xx_chip *chip)
drivers/leds/trigger/ledtrig-oneshot.c+36−2
@@ -130,6 +130,34 @@ static struct attribute *oneshot_trig_attrs[] = { · 129 unmodified lines
130};130};
131ATTRIBUTE_GROUPS(oneshot_trig);131ATTRIBUTE_GROUPS(oneshot_trig);
132 132
133static void pattern_init(struct led_classdev *led_cdev)
134{
135 u32 *pattern;
136 unsigned int size = 0;
137
138 pattern = led_get_default_pattern(led_cdev, &size);
139 if (!pattern)
140 goto out_default;
141
142 if (size != 2) {
143 dev_warn(led_cdev->dev,
144 "Expected 2 but got %u values for delays pattern\n",
145 size);
146 goto out_default;
147 }
148
149 led_cdev->blink_delay_on = pattern[0];
150 led_cdev->blink_delay_off = pattern[1];
151 kfree(pattern);
152
153 return;
154
155out_default:
156 kfree(pattern);
157 led_cdev->blink_delay_on = DEFAULT_DELAY;
158 led_cdev->blink_delay_off = DEFAULT_DELAY;
159}
160
133static int oneshot_trig_activate(struct led_classdev *led_cdev)161static int oneshot_trig_activate(struct led_classdev *led_cdev)
134{162{
135 struct oneshot_trig_data *oneshot_data;163 struct oneshot_trig_data *oneshot_data;
@@ -140,8 +168,14 @@ static int oneshot_trig_activate(struct led_classdev *led_cdev) · 4 unmodified lines
140 168
141 led_set_trigger_data(led_cdev, oneshot_data);169 led_set_trigger_data(led_cdev, oneshot_data);
142 170
143 led_cdev->blink_delay_on = DEFAULT_DELAY;171 if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) {
144 led_cdev->blink_delay_off = DEFAULT_DELAY;172 pattern_init(led_cdev);
173 /*
174 * Mark as initialized even on pattern_init() error because
175 * any consecutive call to it would produce the same error.
176 */
177 led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER;
178 }
145 179
146 return 0;180 return 0;
147}181}
drivers/leds/trigger/ledtrig-pattern.c+80−19
@@ -220,22 +220,10 @@ out: · 219 unmodified lines
220 return count;220 return count;
221}221}
222 222
223static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev,223static int pattern_trig_store_patterns_string(struct pattern_trig_data *data,
224 const char *buf, size_t count,224 const char *buf, size_t count)
225 bool hw_pattern)
226{225{
227 struct pattern_trig_data *data = led_cdev->trigger_data;226 int ccount, cr, offset = 0;
228 int ccount, cr, offset = 0, err = 0;
229
230 mutex_lock(&data->lock);
231
232 del_timer_sync(&data->timer);
233
234 if (data->is_hw_pattern)
235 led_cdev->pattern_clear(led_cdev);
236
237 data->is_hw_pattern = hw_pattern;
238 data->npatterns = 0;
239 227
240 while (offset < count - 1 && data->npatterns < MAX_PATTERNS) {228 while (offset < count - 1 && data->npatterns < MAX_PATTERNS) {
241 cr = 0;229 cr = 0;
@@ -244,14 +232,54 @@ static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev, · 2 unmodified lines
244 &data->patterns[data->npatterns].delta_t, &cr);232 &data->patterns[data->npatterns].delta_t, &cr);
245 if (ccount != 2) {233 if (ccount != 2) {
246 data->npatterns = 0;234 data->npatterns = 0;
247 err = -EINVAL;235 return -EINVAL;
248 goto out;
249 }236 }
250 237
251 offset += cr;238 offset += cr;
252 data->npatterns++;239 data->npatterns++;
253 }240 }
254 241
242 return 0;
243}
244
245static int pattern_trig_store_patterns_int(struct pattern_trig_data *data,
246 const u32 *buf, size_t count)
247{
248 unsigned int i;
249
250 for (i = 0; i < count; i += 2) {
251 data->patterns[data->npatterns].brightness = buf[i];
252 data->patterns[data->npatterns].delta_t = buf[i + 1];
253 data->npatterns++;
254 }
255
256 return 0;
257}
258
259static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev,
260 const char *buf, const u32 *buf_int,
261 size_t count, bool hw_pattern)
262{
263 struct pattern_trig_data *data = led_cdev->trigger_data;
264 int err = 0;
265
266 mutex_lock(&data->lock);
267
268 del_timer_sync(&data->timer);
269
270 if (data->is_hw_pattern)
271 led_cdev->pattern_clear(led_cdev);
272
273 data->is_hw_pattern = hw_pattern;
274 data->npatterns = 0;
275
276 if (buf)
277 err = pattern_trig_store_patterns_string(data, buf, count);
278 else
279 err = pattern_trig_store_patterns_int(data, buf_int, count);
280 if (err)
281 goto out;
282
255 err = pattern_trig_start_pattern(led_cdev);283 err = pattern_trig_start_pattern(led_cdev);
256 if (err)284 if (err)
257 data->npatterns = 0;285 data->npatterns = 0;
@@ -275,7 +303,7 @@ static ssize_t pattern_store(struct device *dev, struct device_attribute *attr, · 17 unmodified lines
275{303{
276 struct led_classdev *led_cdev = dev_get_drvdata(dev);304 struct led_classdev *led_cdev = dev_get_drvdata(dev);
277 305
278 return pattern_trig_store_patterns(led_cdev, buf, count, false);306 return pattern_trig_store_patterns(led_cdev, buf, NULL, count, false);
279}307}
280 308
281static DEVICE_ATTR_RW(pattern);309static DEVICE_ATTR_RW(pattern);
@@ -295,7 +323,7 @@ static ssize_t hw_pattern_store(struct device *dev, · 13 unmodified lines
295{323{
296 struct led_classdev *led_cdev = dev_get_drvdata(dev);324 struct led_classdev *led_cdev = dev_get_drvdata(dev);
297 325
298 return pattern_trig_store_patterns(led_cdev, buf, count, true);326 return pattern_trig_store_patterns(led_cdev, buf, NULL, count, true);
299}327}
300 328
301static DEVICE_ATTR_RW(hw_pattern);329static DEVICE_ATTR_RW(hw_pattern);
@@ -331,6 +359,30 @@ static const struct attribute_group *pattern_trig_groups[] = { · 29 unmodified lines
331 NULL,359 NULL,
332};360};
333 361
362static void pattern_init(struct led_classdev *led_cdev)
363{
364 unsigned int size = 0;
365 u32 *pattern;
366 int err;
367
368 pattern = led_get_default_pattern(led_cdev, &size);
369 if (!pattern)
370 return;
371
372 if (size % 2) {
373 dev_warn(led_cdev->dev, "Expected pattern of tuples\n");
374 goto out;
375 }
376
377 err = pattern_trig_store_patterns(led_cdev, NULL, pattern, size, false);
378 if (err < 0)
379 dev_warn(led_cdev->dev,
380 "Pattern initialization failed with error %d\n", err);
381
382out:
383 kfree(pattern);
384}
385
334static int pattern_trig_activate(struct led_classdev *led_cdev)386static int pattern_trig_activate(struct led_classdev *led_cdev)
335{387{
336 struct pattern_trig_data *data;388 struct pattern_trig_data *data;
@@ -354,6 +406,15 @@ static int pattern_trig_activate(struct led_classdev *led_cdev) · 17 unmodified lines
354 timer_setup(&data->timer, pattern_trig_timer_function, 0);406 timer_setup(&data->timer, pattern_trig_timer_function, 0);
355 led_cdev->activated = true;407 led_cdev->activated = true;
356 408
409 if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) {
410 pattern_init(led_cdev);
411 /*
412 * Mark as initialized even on pattern_init() error because
413 * any consecutive call to it would produce the same error.
414 */
415 led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER;
416 }
417
357 return 0;418 return 0;
358}419}
359 420
drivers/leds/trigger/ledtrig-timer.c+34−0
@@ -15,6 +15,7 @@ · 14 unmodified lines
15#include <linux/init.h>15#include <linux/init.h>
16#include <linux/device.h>16#include <linux/device.h>
17#include <linux/ctype.h>17#include <linux/ctype.h>
18#include <linux/slab.h>
18#include <linux/leds.h>19#include <linux/leds.h>
19 20
20static ssize_t led_delay_on_show(struct device *dev,21static ssize_t led_delay_on_show(struct device *dev,
@@ -77,8 +78,41 @@ static struct attribute *timer_trig_attrs[] = { · 56 unmodified lines
77};78};
78ATTRIBUTE_GROUPS(timer_trig);79ATTRIBUTE_GROUPS(timer_trig);
79 80
81static void pattern_init(struct led_classdev *led_cdev)
82{
83 u32 *pattern;
84 unsigned int size = 0;
85
86 pattern = led_get_default_pattern(led_cdev, &size);
87 if (!pattern)
88 return;
89
90 if (size != 2) {
91 dev_warn(led_cdev->dev,
92 "Expected 2 but got %u values for delays pattern\n",
93 size);
94 goto out;
95 }
96
97 led_cdev->blink_delay_on = pattern[0];
98 led_cdev->blink_delay_off = pattern[1];
99 /* led_blink_set() called by caller */
100
101out:
102 kfree(pattern);
103}
104
80static int timer_trig_activate(struct led_classdev *led_cdev)105static int timer_trig_activate(struct led_classdev *led_cdev)
81{106{
107 if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) {
108 pattern_init(led_cdev);
109 /*
110 * Mark as initialized even on pattern_init() error because
111 * any consecutive call to it would produce the same error.
112 */
113 led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER;
114 }
115
82 led_blink_set(led_cdev, &led_cdev->blink_delay_on,116 led_blink_set(led_cdev, &led_cdev->blink_delay_on,
83 &led_cdev->blink_delay_off);117 &led_cdev->blink_delay_off);
84 118
include/linux/leds.h+13−0
@@ -218,6 +218,19 @@ extern int led_set_brightness_sync(struct led_classdev *led_cdev, · 217 unmodified lines
218 */218 */
219extern int led_update_brightness(struct led_classdev *led_cdev);219extern int led_update_brightness(struct led_classdev *led_cdev);
220 220
221/**
222 * led_get_default_pattern - return default pattern
223 *
224 * @led_cdev: the LED to get default pattern for
225 * @size: pointer for storing the number of elements in returned array,
226 * modified only if return != NULL
227 *
228 * Return: Allocated array of integers with default pattern from device tree
229 * or NULL. Caller is responsible for kfree().
230 */
231extern u32 *led_get_default_pattern(struct led_classdev *led_cdev,
232 unsigned int *size);
233
221/**234/**
222 * led_sysfs_disable - disable LED sysfs interface235 * led_sysfs_disable - disable LED sysfs interface
223 * @led_cdev: the LED to set236 * @led_cdev: the LED to set

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t