Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * One-shot LED Trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Based on ledtrig-timer.c by Richard Purdie <rpurdie@openedhand.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "../leds.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define DEFAULT_DELAY 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) struct oneshot_trig_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	unsigned int invert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static ssize_t led_shot(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	led_blink_set_oneshot(led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 			&led_cdev->blink_delay_on, &led_cdev->blink_delay_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 			oneshot_data->invert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	/* content is ignored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static ssize_t led_invert_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	return sprintf(buf, "%u\n", oneshot_data->invert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static ssize_t led_invert_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	ret = kstrtoul(buf, 0, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	oneshot_data->invert = !!state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (oneshot_data->invert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		led_set_brightness_nosleep(led_cdev, LED_FULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		led_set_brightness_nosleep(led_cdev, LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static ssize_t led_delay_on_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static ssize_t led_delay_on_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	ret = kstrtoul(buf, 0, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	led_cdev->blink_delay_on = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static ssize_t led_delay_off_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static ssize_t led_delay_off_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	ret = kstrtoul(buf, 0, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	led_cdev->blink_delay_off = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static DEVICE_ATTR(shot, 0200, NULL, led_shot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static struct attribute *oneshot_trig_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	&dev_attr_delay_on.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	&dev_attr_delay_off.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	&dev_attr_invert.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	&dev_attr_shot.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ATTRIBUTE_GROUPS(oneshot_trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void pattern_init(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	u32 *pattern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	unsigned int size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	pattern = led_get_default_pattern(led_cdev, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (!pattern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		goto out_default;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (size != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		dev_warn(led_cdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			 "Expected 2 but got %u values for delays pattern\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			 size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		goto out_default;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	led_cdev->blink_delay_on = pattern[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	led_cdev->blink_delay_off = pattern[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	kfree(pattern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) out_default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	kfree(pattern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	led_cdev->blink_delay_on = DEFAULT_DELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	led_cdev->blink_delay_off = DEFAULT_DELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int oneshot_trig_activate(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct oneshot_trig_data *oneshot_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	oneshot_data = kzalloc(sizeof(*oneshot_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (!oneshot_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	led_set_trigger_data(led_cdev, oneshot_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		pattern_init(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		 * Mark as initialized even on pattern_init() error because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		 * any consecutive call to it would produce the same error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static void oneshot_trig_deactivate(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	struct oneshot_trig_data *oneshot_data = led_get_trigger_data(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	kfree(oneshot_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	/* Stop blinking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	led_set_brightness(led_cdev, LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static struct led_trigger oneshot_led_trigger = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	.name     = "oneshot",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	.activate = oneshot_trig_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	.deactivate = oneshot_trig_deactivate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	.groups = oneshot_trig_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) module_led_trigger(oneshot_led_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) MODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) MODULE_DESCRIPTION("One-shot LED trigger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) MODULE_LICENSE("GPL v2");