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
77 timer. It can do gradual dimming and step change of brightness.
88
99 The pattern is given by a series of tuples, of brightness and
10− duration (ms). The LED is expected to traverse the series and
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.
10+ duration (ms).
1411
15− 1. For gradual dimming, the dimming interval now is set as 50
16− milliseconds. So the tuple with duration less than dimming
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)
12+ The exact format is described in:
13+ Documentation/devicetree/bindings/leds/leds-trigger-pattern.txt
5914
6015 What: /sys/class/leds/<led>/hw_pattern
6116 Date: September 2018
Documentation/devicetree/bindings/leds/common.txt+12−0
@@ -37,6 +37,18 @@ Optional properties for child nodes: · 36 unmodified lines
3737 "ide-disk" - LED indicates IDE disk activity (deprecated),
3838 in new implementations use "disk-activity"
3939 "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+
4052
4153 - led-max-microamp : Maximum LED supply current in microamperes. This property
4254 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+
3+The pattern is given by a series of tuples, of brightness and duration (ms).
4+The LED is expected to traverse the series and each brightness value for the
5+specified duration. Duration of 0 means brightness should immediately change to
6+new value, and writing malformed pattern deactivates any active one.
7+
8+1. For gradual dimming, the dimming interval now is set as 50 milliseconds. So
9+the tuple with duration less than dimming interval (50ms) is treated as a step
10+change of brightness, i.e. the subsequent brightness will be applied without
11+adding intervening dimming intervals.
12+
13+The gradual dimming format of the software pattern values should be:
14+"brightness_1 duration_1 brightness_2 duration_2 brightness_3 duration_3 ...".
15+For example (using sysfs interface):
16+
17+echo 0 1000 255 2000 > pattern
18+
19+It will make the LED go gradually from zero-intensity to max (255) intensity in
20+1000 milliseconds, then back to zero intensity in 2000 milliseconds:
21+
22+LED brightness
23+ ^
24+255-| / \ / \ /
25+ | / \ / \ /
26+ | / \ / \ /
27+ | / \ / \ /
28+ 0-| / \/ \/
29+ +---0----1----2----3----4----5----6------------> time (s)
30+
31+2. To make the LED go instantly from one brightness value to another, we should
32+use zero-time lengths (the brightness must be same as the previous tuple's). So
33+the format should be: "brightness_1 duration_1 brightness_1 0 brightness_2
34+duration_2 brightness_2 0 ...".
35+For example (using sysfs interface):
36+
37+echo 0 1000 0 0 255 2000 255 0 > pattern
38+
39+It will make the LED stay off for one second, then stay at max brightness for
40+two seconds:
41+
42+LED brightness
43+ ^
44+255-| +---------+ +---------+
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
1616 #include <linux/list.h>
1717 #include <linux/module.h>
1818 #include <linux/mutex.h>
19+#include <linux/of.h>
1920 #include <linux/rwsem.h>
21+#include <linux/slab.h>
2022 #include "leds.h"
2123
2224 DECLARE_RWSEM(leds_list_lock);
@@ -310,6 +312,34 @@ int led_update_brightness(struct led_classdev *led_cdev) · 287 unmodified lines
310312 }
311313 EXPORT_SYMBOL_GPL(led_update_brightness);
312314
315+u32 *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+}
341+EXPORT_SYMBOL_GPL(led_get_default_pattern);
342+
313343 /* Caller must ensure led_cdev->led_access held */
314344 void led_sysfs_disable(struct led_classdev *led_cdev)
315345 {
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
201201
202202 if (!fw) {
203203 dev_err(dev, "firmware request failed\n");
204− goto out;
204+ return;
205205 }
206206
207207 /* handling firmware data is chip dependent */
@@ -214,9 +214,9 @@ static void lp55xx_firmware_loaded(const struct firmware *fw, void *context) · 6 unmodified lines
214214
215215 mutex_unlock(&chip->lock);
216216
217−out:
218217 /* firmware should be released for other channel use */
219218 release_firmware(chip->fw);
219+ chip->fw = NULL;
220220 }
221221
222222 static 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
130130 };
131131 ATTRIBUTE_GROUPS(oneshot_trig);
132132
133+static 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+
155+out_default:
156+ kfree(pattern);
157+ led_cdev->blink_delay_on = DEFAULT_DELAY;
158+ led_cdev->blink_delay_off = DEFAULT_DELAY;
159+}
160+
133161 static int oneshot_trig_activate(struct led_classdev *led_cdev)
134162 {
135163 struct oneshot_trig_data *oneshot_data;
@@ -140,8 +168,14 @@ static int oneshot_trig_activate(struct led_classdev *led_cdev) · 4 unmodified lines
140168
141169 led_set_trigger_data(led_cdev, oneshot_data);
142170
143− led_cdev->blink_delay_on = DEFAULT_DELAY;
144− led_cdev->blink_delay_off = DEFAULT_DELAY;
171+ if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) {
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+ }
145179
146180 return 0;
147181 }
drivers/leds/trigger/ledtrig-pattern.c+80−19
@@ -220,22 +220,10 @@ out: · 219 unmodified lines
220220 return count;
221221 }
222222
223−static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev,
224− const char *buf, size_t count,
225− bool hw_pattern)
223+static int pattern_trig_store_patterns_string(struct pattern_trig_data *data,
224+ const char *buf, size_t count)
226225 {
227− struct pattern_trig_data *data = led_cdev->trigger_data;
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;
226+ int ccount, cr, offset = 0;
239227
240228 while (offset < count - 1 && data->npatterns < MAX_PATTERNS) {
241229 cr = 0;
@@ -244,14 +232,54 @@ static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev, · 2 unmodified lines
244232 &data->patterns[data->npatterns].delta_t, &cr);
245233 if (ccount != 2) {
246234 data->npatterns = 0;
247− err = -EINVAL;
248− goto out;
235+ return -EINVAL;
249236 }
250237
251238 offset += cr;
252239 data->npatterns++;
253240 }
254241
242+ return 0;
243+}
244+
245+static 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+
259+static 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+
255283 err = pattern_trig_start_pattern(led_cdev);
256284 if (err)
257285 data->npatterns = 0;
@@ -275,7 +303,7 @@ static ssize_t pattern_store(struct device *dev, struct device_attribute *attr, · 17 unmodified lines
275303 {
276304 struct led_classdev *led_cdev = dev_get_drvdata(dev);
277305
278− return pattern_trig_store_patterns(led_cdev, buf, count, false);
306+ return pattern_trig_store_patterns(led_cdev, buf, NULL, count, false);
279307 }
280308
281309 static DEVICE_ATTR_RW(pattern);
@@ -295,7 +323,7 @@ static ssize_t hw_pattern_store(struct device *dev, · 13 unmodified lines
295323 {
296324 struct led_classdev *led_cdev = dev_get_drvdata(dev);
297325
298− return pattern_trig_store_patterns(led_cdev, buf, count, true);
326+ return pattern_trig_store_patterns(led_cdev, buf, NULL, count, true);
299327 }
300328
301329 static DEVICE_ATTR_RW(hw_pattern);
@@ -331,6 +359,30 @@ static const struct attribute_group *pattern_trig_groups[] = { · 29 unmodified lines
331359 NULL,
332360 };
333361
362+static 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+
382+out:
383+ kfree(pattern);
384+}
385+
334386 static int pattern_trig_activate(struct led_classdev *led_cdev)
335387 {
336388 struct pattern_trig_data *data;
@@ -354,6 +406,15 @@ static int pattern_trig_activate(struct led_classdev *led_cdev) · 17 unmodified lines
354406 timer_setup(&data->timer, pattern_trig_timer_function, 0);
355407 led_cdev->activated = true;
356408
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+
357418 return 0;
358419 }
359420
drivers/leds/trigger/ledtrig-timer.c+34−0
@@ -15,6 +15,7 @@ · 14 unmodified lines
1515 #include <linux/init.h>
1616 #include <linux/device.h>
1717 #include <linux/ctype.h>
18+#include <linux/slab.h>
1819 #include <linux/leds.h>
1920
2021 static ssize_t led_delay_on_show(struct device *dev,
@@ -77,8 +78,41 @@ static struct attribute *timer_trig_attrs[] = { · 56 unmodified lines
7778 };
7879 ATTRIBUTE_GROUPS(timer_trig);
7980
81+static 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+
101+out:
102+ kfree(pattern);
103+}
104+
80105 static int timer_trig_activate(struct led_classdev *led_cdev)
81106 {
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+
82116 led_blink_set(led_cdev, &led_cdev->blink_delay_on,
83117 &led_cdev->blink_delay_off);
84118
include/linux/leds.h+13−0
@@ -218,6 +218,19 @@ extern int led_set_brightness_sync(struct led_classdev *led_cdev, · 217 unmodified lines
218218 */
219219 extern int led_update_brightness(struct led_classdev *led_cdev);
220220
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+ */
231+extern u32 *led_get_default_pattern(struct led_classdev *led_cdev,
232+ unsigned int *size);
233+
221234 /**
222235 * led_sysfs_disable - disable LED sysfs interface
223236 * @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