1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
7 * Based on ledtrig-timer.c by Richard Purdie <rpurdie@openedhand.com>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/ctype.h>
15 #include <linux/slab.h>
16 #include <linux/leds.h>
19 #define DEFAULT_DELAY 100
21 struct oneshot_trig_data
{
25 static ssize_t
led_shot(struct device
*dev
,
26 struct device_attribute
*attr
, const char *buf
, size_t size
)
28 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
29 struct oneshot_trig_data
*oneshot_data
= led_trigger_get_drvdata(dev
);
31 led_blink_set_oneshot(led_cdev
,
32 &led_cdev
->blink_delay_on
, &led_cdev
->blink_delay_off
,
33 oneshot_data
->invert
);
35 /* content is ignored */
38 static ssize_t
led_invert_show(struct device
*dev
,
39 struct device_attribute
*attr
, char *buf
)
41 struct oneshot_trig_data
*oneshot_data
= led_trigger_get_drvdata(dev
);
43 return sprintf(buf
, "%u\n", oneshot_data
->invert
);
46 static ssize_t
led_invert_store(struct device
*dev
,
47 struct device_attribute
*attr
, const char *buf
, size_t size
)
49 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
50 struct oneshot_trig_data
*oneshot_data
= led_trigger_get_drvdata(dev
);
54 ret
= kstrtoul(buf
, 0, &state
);
58 oneshot_data
->invert
= !!state
;
60 if (oneshot_data
->invert
)
61 led_set_brightness_nosleep(led_cdev
, LED_FULL
);
63 led_set_brightness_nosleep(led_cdev
, LED_OFF
);
68 static ssize_t
led_delay_on_show(struct device
*dev
,
69 struct device_attribute
*attr
, char *buf
)
71 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
73 return sprintf(buf
, "%lu\n", led_cdev
->blink_delay_on
);
76 static ssize_t
led_delay_on_store(struct device
*dev
,
77 struct device_attribute
*attr
, const char *buf
, size_t size
)
79 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
83 ret
= kstrtoul(buf
, 0, &state
);
87 led_cdev
->blink_delay_on
= state
;
92 static ssize_t
led_delay_off_show(struct device
*dev
,
93 struct device_attribute
*attr
, char *buf
)
95 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
97 return sprintf(buf
, "%lu\n", led_cdev
->blink_delay_off
);
100 static ssize_t
led_delay_off_store(struct device
*dev
,
101 struct device_attribute
*attr
, const char *buf
, size_t size
)
103 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
107 ret
= kstrtoul(buf
, 0, &state
);
111 led_cdev
->blink_delay_off
= state
;
116 static DEVICE_ATTR(delay_on
, 0644, led_delay_on_show
, led_delay_on_store
);
117 static DEVICE_ATTR(delay_off
, 0644, led_delay_off_show
, led_delay_off_store
);
118 static DEVICE_ATTR(invert
, 0644, led_invert_show
, led_invert_store
);
119 static DEVICE_ATTR(shot
, 0200, NULL
, led_shot
);
121 static struct attribute
*oneshot_trig_attrs
[] = {
122 &dev_attr_delay_on
.attr
,
123 &dev_attr_delay_off
.attr
,
124 &dev_attr_invert
.attr
,
128 ATTRIBUTE_GROUPS(oneshot_trig
);
130 static void pattern_init(struct led_classdev
*led_cdev
)
133 unsigned int size
= 0;
135 pattern
= led_get_default_pattern(led_cdev
, &size
);
140 dev_warn(led_cdev
->dev
,
141 "Expected 2 but got %u values for delays pattern\n",
146 led_cdev
->blink_delay_on
= pattern
[0];
147 led_cdev
->blink_delay_off
= pattern
[1];
154 led_cdev
->blink_delay_on
= DEFAULT_DELAY
;
155 led_cdev
->blink_delay_off
= DEFAULT_DELAY
;
158 static int oneshot_trig_activate(struct led_classdev
*led_cdev
)
160 struct oneshot_trig_data
*oneshot_data
;
162 oneshot_data
= kzalloc(sizeof(*oneshot_data
), GFP_KERNEL
);
166 led_set_trigger_data(led_cdev
, oneshot_data
);
168 if (led_cdev
->flags
& LED_INIT_DEFAULT_TRIGGER
) {
169 pattern_init(led_cdev
);
171 * Mark as initialized even on pattern_init() error because
172 * any consecutive call to it would produce the same error.
174 led_cdev
->flags
&= ~LED_INIT_DEFAULT_TRIGGER
;
180 static void oneshot_trig_deactivate(struct led_classdev
*led_cdev
)
182 struct oneshot_trig_data
*oneshot_data
= led_get_trigger_data(led_cdev
);
187 led_set_brightness(led_cdev
, LED_OFF
);
190 static struct led_trigger oneshot_led_trigger
= {
192 .activate
= oneshot_trig_activate
,
193 .deactivate
= oneshot_trig_deactivate
,
194 .groups
= oneshot_trig_groups
,
196 module_led_trigger(oneshot_led_trigger
);
198 MODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>");
199 MODULE_DESCRIPTION("One-shot LED trigger");
200 MODULE_LICENSE("GPL v2");