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)  * LED Kernel Timer Trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2005-2006 Openedhand Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: 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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static ssize_t led_delay_on_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static ssize_t led_delay_on_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	ret = kstrtoul(buf, 10, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	led_cdev->blink_delay_on = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static ssize_t led_delay_off_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static ssize_t led_delay_off_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct led_classdev *led_cdev = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	ret = kstrtoul(buf, 10, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	led_cdev->blink_delay_off = state;
^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 DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static struct attribute *timer_trig_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	&dev_attr_delay_on.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	&dev_attr_delay_off.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) ATTRIBUTE_GROUPS(timer_trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static void pattern_init(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	u32 *pattern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	unsigned int size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	pattern = led_get_default_pattern(led_cdev, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (!pattern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (size != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		dev_warn(led_cdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			 "Expected 2 but got %u values for delays pattern\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			 size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	led_cdev->blink_delay_on = pattern[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	led_cdev->blink_delay_off = pattern[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/* led_blink_set() called by caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	kfree(pattern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int timer_trig_activate(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		pattern_init(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		 * Mark as initialized even on pattern_init() error because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		 * any consecutive call to it would produce the same error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	 * If "set brightness to 0" is pending in workqueue, we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	 * want that to be reordered after blink_set()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	flush_work(&led_cdev->set_brightness_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	led_blink_set(led_cdev, &led_cdev->blink_delay_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		      &led_cdev->blink_delay_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static void timer_trig_deactivate(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* Stop blinking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	led_set_brightness(led_cdev, LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static struct led_trigger timer_led_trigger = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	.name     = "timer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	.activate = timer_trig_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	.deactivate = timer_trig_deactivate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	.groups = timer_trig_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) module_led_trigger(timer_led_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) MODULE_DESCRIPTION("Timer LED trigger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) MODULE_LICENSE("GPL v2");