drm/rockchip: Don't change hdmi reference clock rate
[drm/drm-misc.git] / drivers / leds / trigger / ledtrig-oneshot.c
blobbee3bd452abf094c57ac59f744d51f9be620e01b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * One-shot LED Trigger
5 * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
7 * Based on ledtrig-timer.c by Richard Purdie <rpurdie@openedhand.com>
8 */
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>
17 #include "../leds.h"
19 #define DEFAULT_DELAY 100
21 struct oneshot_trig_data {
22 unsigned int invert;
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 */
36 return size;
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);
51 unsigned long state;
52 int ret;
54 ret = kstrtoul(buf, 0, &state);
55 if (ret)
56 return ret;
58 oneshot_data->invert = !!state;
60 if (oneshot_data->invert)
61 led_set_brightness_nosleep(led_cdev, LED_FULL);
62 else
63 led_set_brightness_nosleep(led_cdev, LED_OFF);
65 return size;
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);
80 unsigned long state;
81 int ret;
83 ret = kstrtoul(buf, 0, &state);
84 if (ret)
85 return ret;
87 led_cdev->blink_delay_on = state;
89 return size;
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);
104 unsigned long state;
105 int ret;
107 ret = kstrtoul(buf, 0, &state);
108 if (ret)
109 return ret;
111 led_cdev->blink_delay_off = state;
113 return size;
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,
125 &dev_attr_shot.attr,
126 NULL
128 ATTRIBUTE_GROUPS(oneshot_trig);
130 static void pattern_init(struct led_classdev *led_cdev)
132 u32 *pattern;
133 unsigned int size = 0;
135 pattern = led_get_default_pattern(led_cdev, &size);
136 if (!pattern)
137 goto out_default;
139 if (size != 2) {
140 dev_warn(led_cdev->dev,
141 "Expected 2 but got %u values for delays pattern\n",
142 size);
143 goto out_default;
146 led_cdev->blink_delay_on = pattern[0];
147 led_cdev->blink_delay_off = pattern[1];
148 kfree(pattern);
150 return;
152 out_default:
153 kfree(pattern);
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);
163 if (!oneshot_data)
164 return -ENOMEM;
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;
177 return 0;
180 static void oneshot_trig_deactivate(struct led_classdev *led_cdev)
182 struct oneshot_trig_data *oneshot_data = led_get_trigger_data(led_cdev);
184 kfree(oneshot_data);
186 /* Stop blinking */
187 led_set_brightness(led_cdev, LED_OFF);
190 static struct led_trigger oneshot_led_trigger = {
191 .name = "oneshot",
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");