1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f07fb521SKim, Milo /*
3f07fb521SKim, Milo * One-shot LED Trigger
4f07fb521SKim, Milo *
5f07fb521SKim, Milo * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
6f07fb521SKim, Milo *
7f07fb521SKim, Milo * Based on ledtrig-timer.c by Richard Purdie <rpurdie@openedhand.com>
8f07fb521SKim, Milo */
9f07fb521SKim, Milo
10f07fb521SKim, Milo #include <linux/module.h>
11f07fb521SKim, Milo #include <linux/kernel.h>
12f07fb521SKim, Milo #include <linux/init.h>
13f07fb521SKim, Milo #include <linux/device.h>
14f07fb521SKim, Milo #include <linux/ctype.h>
15f07fb521SKim, Milo #include <linux/slab.h>
16f07fb521SKim, Milo #include <linux/leds.h>
17f07fb521SKim, Milo #include "../leds.h"
18f07fb521SKim, Milo
19f07fb521SKim, Milo #define DEFAULT_DELAY 100
20f07fb521SKim, Milo
21f07fb521SKim, Milo struct oneshot_trig_data {
22f07fb521SKim, Milo unsigned int invert;
23f07fb521SKim, Milo };
24f07fb521SKim, Milo
led_shot(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)25f07fb521SKim, Milo static ssize_t led_shot(struct device *dev,
26f07fb521SKim, Milo struct device_attribute *attr, const char *buf, size_t size)
27f07fb521SKim, Milo {
2881d55332SUwe Kleine-König struct led_classdev *led_cdev = led_trigger_get_led(dev);
2981d55332SUwe Kleine-König struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
30f07fb521SKim, Milo
31f07fb521SKim, Milo led_blink_set_oneshot(led_cdev,
32f07fb521SKim, Milo &led_cdev->blink_delay_on, &led_cdev->blink_delay_off,
33f07fb521SKim, Milo oneshot_data->invert);
34f07fb521SKim, Milo
35f07fb521SKim, Milo /* content is ignored */
36f07fb521SKim, Milo return size;
37f07fb521SKim, Milo }
led_invert_show(struct device * dev,struct device_attribute * attr,char * buf)38f07fb521SKim, Milo static ssize_t led_invert_show(struct device *dev,
39f07fb521SKim, Milo struct device_attribute *attr, char *buf)
40f07fb521SKim, Milo {
4181d55332SUwe Kleine-König struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
42f07fb521SKim, Milo
43f07fb521SKim, Milo return sprintf(buf, "%u\n", oneshot_data->invert);
44f07fb521SKim, Milo }
45f07fb521SKim, Milo
led_invert_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)46f07fb521SKim, Milo static ssize_t led_invert_store(struct device *dev,
47f07fb521SKim, Milo struct device_attribute *attr, const char *buf, size_t size)
48f07fb521SKim, Milo {
4981d55332SUwe Kleine-König struct led_classdev *led_cdev = led_trigger_get_led(dev);
5081d55332SUwe Kleine-König struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
51f07fb521SKim, Milo unsigned long state;
52f07fb521SKim, Milo int ret;
53f07fb521SKim, Milo
54f07fb521SKim, Milo ret = kstrtoul(buf, 0, &state);
55f07fb521SKim, Milo if (ret)
56f07fb521SKim, Milo return ret;
57f07fb521SKim, Milo
58f07fb521SKim, Milo oneshot_data->invert = !!state;
59f07fb521SKim, Milo
60f07fb521SKim, Milo if (oneshot_data->invert)
6181fe8e5bSJacek Anaszewski led_set_brightness_nosleep(led_cdev, LED_FULL);
62f07fb521SKim, Milo else
6381fe8e5bSJacek Anaszewski led_set_brightness_nosleep(led_cdev, LED_OFF);
64f07fb521SKim, Milo
65f07fb521SKim, Milo return size;
66f07fb521SKim, Milo }
67f07fb521SKim, Milo
led_delay_on_show(struct device * dev,struct device_attribute * attr,char * buf)68f07fb521SKim, Milo static ssize_t led_delay_on_show(struct device *dev,
69f07fb521SKim, Milo struct device_attribute *attr, char *buf)
70f07fb521SKim, Milo {
7181d55332SUwe Kleine-König struct led_classdev *led_cdev = led_trigger_get_led(dev);
72f07fb521SKim, Milo
73f07fb521SKim, Milo return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
74f07fb521SKim, Milo }
75f07fb521SKim, Milo
led_delay_on_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)76f07fb521SKim, Milo static ssize_t led_delay_on_store(struct device *dev,
77f07fb521SKim, Milo struct device_attribute *attr, const char *buf, size_t size)
78f07fb521SKim, Milo {
7981d55332SUwe Kleine-König struct led_classdev *led_cdev = led_trigger_get_led(dev);
80f07fb521SKim, Milo unsigned long state;
81f07fb521SKim, Milo int ret;
82f07fb521SKim, Milo
83f07fb521SKim, Milo ret = kstrtoul(buf, 0, &state);
84f07fb521SKim, Milo if (ret)
85f07fb521SKim, Milo return ret;
86f07fb521SKim, Milo
87f07fb521SKim, Milo led_cdev->blink_delay_on = state;
88f07fb521SKim, Milo
89f07fb521SKim, Milo return size;
90f07fb521SKim, Milo }
9181d55332SUwe Kleine-König
led_delay_off_show(struct device * dev,struct device_attribute * attr,char * buf)92f07fb521SKim, Milo static ssize_t led_delay_off_show(struct device *dev,
93f07fb521SKim, Milo struct device_attribute *attr, char *buf)
94f07fb521SKim, Milo {
9581d55332SUwe Kleine-König struct led_classdev *led_cdev = led_trigger_get_led(dev);
96f07fb521SKim, Milo
97f07fb521SKim, Milo return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
98f07fb521SKim, Milo }
99f07fb521SKim, Milo
led_delay_off_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)100f07fb521SKim, Milo static ssize_t led_delay_off_store(struct device *dev,
101f07fb521SKim, Milo struct device_attribute *attr, const char *buf, size_t size)
102f07fb521SKim, Milo {
10381d55332SUwe Kleine-König struct led_classdev *led_cdev = led_trigger_get_led(dev);
104f07fb521SKim, Milo unsigned long state;
105f07fb521SKim, Milo int ret;
106f07fb521SKim, Milo
107f07fb521SKim, Milo ret = kstrtoul(buf, 0, &state);
108f07fb521SKim, Milo if (ret)
109f07fb521SKim, Milo return ret;
110f07fb521SKim, Milo
111f07fb521SKim, Milo led_cdev->blink_delay_off = state;
112f07fb521SKim, Milo
113f07fb521SKim, Milo return size;
114f07fb521SKim, Milo }
115f07fb521SKim, Milo
116f07fb521SKim, Milo static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
117f07fb521SKim, Milo static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
118f07fb521SKim, Milo static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
119f07fb521SKim, Milo static DEVICE_ATTR(shot, 0200, NULL, led_shot);
120f07fb521SKim, Milo
12181d55332SUwe Kleine-König static struct attribute *oneshot_trig_attrs[] = {
12281d55332SUwe Kleine-König &dev_attr_delay_on.attr,
12381d55332SUwe Kleine-König &dev_attr_delay_off.attr,
12481d55332SUwe Kleine-König &dev_attr_invert.attr,
12581d55332SUwe Kleine-König &dev_attr_shot.attr,
12681d55332SUwe Kleine-König NULL
12781d55332SUwe Kleine-König };
12881d55332SUwe Kleine-König ATTRIBUTE_GROUPS(oneshot_trig);
12981d55332SUwe Kleine-König
pattern_init(struct led_classdev * led_cdev)1309c9ab51eSKrzysztof Kozlowski static void pattern_init(struct led_classdev *led_cdev)
1319c9ab51eSKrzysztof Kozlowski {
1329c9ab51eSKrzysztof Kozlowski u32 *pattern;
1339c9ab51eSKrzysztof Kozlowski unsigned int size = 0;
1349c9ab51eSKrzysztof Kozlowski
1359c9ab51eSKrzysztof Kozlowski pattern = led_get_default_pattern(led_cdev, &size);
1369c9ab51eSKrzysztof Kozlowski if (!pattern)
1379c9ab51eSKrzysztof Kozlowski goto out_default;
1389c9ab51eSKrzysztof Kozlowski
1399c9ab51eSKrzysztof Kozlowski if (size != 2) {
1409c9ab51eSKrzysztof Kozlowski dev_warn(led_cdev->dev,
1419c9ab51eSKrzysztof Kozlowski "Expected 2 but got %u values for delays pattern\n",
1429c9ab51eSKrzysztof Kozlowski size);
1439c9ab51eSKrzysztof Kozlowski goto out_default;
1449c9ab51eSKrzysztof Kozlowski }
1459c9ab51eSKrzysztof Kozlowski
1469c9ab51eSKrzysztof Kozlowski led_cdev->blink_delay_on = pattern[0];
1479c9ab51eSKrzysztof Kozlowski led_cdev->blink_delay_off = pattern[1];
1489c9ab51eSKrzysztof Kozlowski kfree(pattern);
1499c9ab51eSKrzysztof Kozlowski
1509c9ab51eSKrzysztof Kozlowski return;
1519c9ab51eSKrzysztof Kozlowski
1529c9ab51eSKrzysztof Kozlowski out_default:
1539c9ab51eSKrzysztof Kozlowski kfree(pattern);
1549c9ab51eSKrzysztof Kozlowski led_cdev->blink_delay_on = DEFAULT_DELAY;
1559c9ab51eSKrzysztof Kozlowski led_cdev->blink_delay_off = DEFAULT_DELAY;
1569c9ab51eSKrzysztof Kozlowski }
1579c9ab51eSKrzysztof Kozlowski
oneshot_trig_activate(struct led_classdev * led_cdev)1582282e125SUwe Kleine-König static int oneshot_trig_activate(struct led_classdev *led_cdev)
159f07fb521SKim, Milo {
160f07fb521SKim, Milo struct oneshot_trig_data *oneshot_data;
161f07fb521SKim, Milo
162f07fb521SKim, Milo oneshot_data = kzalloc(sizeof(*oneshot_data), GFP_KERNEL);
163f07fb521SKim, Milo if (!oneshot_data)
16481d55332SUwe Kleine-König return -ENOMEM;
165f07fb521SKim, Milo
16681d55332SUwe Kleine-König led_set_trigger_data(led_cdev, oneshot_data);
167f07fb521SKim, Milo
1689c9ab51eSKrzysztof Kozlowski if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) {
1699c9ab51eSKrzysztof Kozlowski pattern_init(led_cdev);
1709c9ab51eSKrzysztof Kozlowski /*
1719c9ab51eSKrzysztof Kozlowski * Mark as initialized even on pattern_init() error because
1729c9ab51eSKrzysztof Kozlowski * any consecutive call to it would produce the same error.
1739c9ab51eSKrzysztof Kozlowski */
1749c9ab51eSKrzysztof Kozlowski led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER;
1759c9ab51eSKrzysztof Kozlowski }
176f07fb521SKim, Milo
1772282e125SUwe Kleine-König return 0;
178f07fb521SKim, Milo }
179f07fb521SKim, Milo
oneshot_trig_deactivate(struct led_classdev * led_cdev)180f07fb521SKim, Milo static void oneshot_trig_deactivate(struct led_classdev *led_cdev)
181f07fb521SKim, Milo {
18281d55332SUwe Kleine-König struct oneshot_trig_data *oneshot_data = led_get_trigger_data(led_cdev);
183f07fb521SKim, Milo
184f07fb521SKim, Milo kfree(oneshot_data);
185f07fb521SKim, Milo
186f07fb521SKim, Milo /* Stop blinking */
187f07fb521SKim, Milo led_set_brightness(led_cdev, LED_OFF);
188f07fb521SKim, Milo }
189f07fb521SKim, Milo
190f07fb521SKim, Milo static struct led_trigger oneshot_led_trigger = {
191f07fb521SKim, Milo .name = "oneshot",
192f07fb521SKim, Milo .activate = oneshot_trig_activate,
193f07fb521SKim, Milo .deactivate = oneshot_trig_deactivate,
19481d55332SUwe Kleine-König .groups = oneshot_trig_groups,
195f07fb521SKim, Milo };
19681d55332SUwe Kleine-König module_led_trigger(oneshot_led_trigger);
197f07fb521SKim, Milo
198f07fb521SKim, Milo MODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>");
199f07fb521SKim, Milo MODULE_DESCRIPTION("One-shot LED trigger");
200fb65ee94SUwe Kleine-König MODULE_LICENSE("GPL v2");
201