1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED Kernel Timer Trigger
5 * Copyright 2005-2006 Openedhand Ltd.
7 * Author: 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>
18 static ssize_t
led_delay_on_show(struct device
*dev
,
19 struct device_attribute
*attr
, char *buf
)
21 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
23 return sprintf(buf
, "%lu\n", led_cdev
->blink_delay_on
);
26 static ssize_t
led_delay_on_store(struct device
*dev
,
27 struct device_attribute
*attr
, const char *buf
, size_t size
)
29 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
33 ret
= kstrtoul(buf
, 10, &state
);
37 led_blink_set(led_cdev
, &state
, &led_cdev
->blink_delay_off
);
38 led_cdev
->blink_delay_on
= state
;
43 static ssize_t
led_delay_off_show(struct device
*dev
,
44 struct device_attribute
*attr
, char *buf
)
46 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
48 return sprintf(buf
, "%lu\n", led_cdev
->blink_delay_off
);
51 static ssize_t
led_delay_off_store(struct device
*dev
,
52 struct device_attribute
*attr
, const char *buf
, size_t size
)
54 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
58 ret
= kstrtoul(buf
, 10, &state
);
62 led_blink_set(led_cdev
, &led_cdev
->blink_delay_on
, &state
);
63 led_cdev
->blink_delay_off
= state
;
68 static DEVICE_ATTR(delay_on
, 0644, led_delay_on_show
, led_delay_on_store
);
69 static DEVICE_ATTR(delay_off
, 0644, led_delay_off_show
, led_delay_off_store
);
71 static struct attribute
*timer_trig_attrs
[] = {
72 &dev_attr_delay_on
.attr
,
73 &dev_attr_delay_off
.attr
,
76 ATTRIBUTE_GROUPS(timer_trig
);
78 static void pattern_init(struct led_classdev
*led_cdev
)
81 unsigned int size
= 0;
83 pattern
= led_get_default_pattern(led_cdev
, &size
);
88 dev_warn(led_cdev
->dev
,
89 "Expected 2 but got %u values for delays pattern\n",
94 led_cdev
->blink_delay_on
= pattern
[0];
95 led_cdev
->blink_delay_off
= pattern
[1];
96 /* led_blink_set() called by caller */
102 static int timer_trig_activate(struct led_classdev
*led_cdev
)
104 if (led_cdev
->flags
& LED_INIT_DEFAULT_TRIGGER
) {
105 pattern_init(led_cdev
);
107 * Mark as initialized even on pattern_init() error because
108 * any consecutive call to it would produce the same error.
110 led_cdev
->flags
&= ~LED_INIT_DEFAULT_TRIGGER
;
113 led_blink_set(led_cdev
, &led_cdev
->blink_delay_on
,
114 &led_cdev
->blink_delay_off
);
119 static void timer_trig_deactivate(struct led_classdev
*led_cdev
)
122 led_set_brightness(led_cdev
, LED_OFF
);
125 static struct led_trigger timer_led_trigger
= {
127 .activate
= timer_trig_activate
,
128 .deactivate
= timer_trig_deactivate
,
129 .groups
= timer_trig_groups
,
131 module_led_trigger(timer_led_trigger
);
133 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
134 MODULE_DESCRIPTION("Timer LED trigger");
135 MODULE_LICENSE("GPL v2");