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)  * Backlight emulation LED trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2008 (C) Rodolfo Giometti <giometti@linux.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright 2008 (C) Eurotech S.p.A. <info@eurotech.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.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/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "../leds.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define BLANK		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define UNBLANK		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) struct bl_trig_notifier {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct led_classdev *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	int brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	int old_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct notifier_block notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	unsigned invert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int fb_notifier_callback(struct notifier_block *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 				unsigned long event, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct bl_trig_notifier *n = container_of(p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 					struct bl_trig_notifier, notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct led_classdev *led = n->led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct fb_event *fb_event = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int *blank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int new_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	/* If we aren't interested in this event, skip it immediately ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (event != FB_EVENT_BLANK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	blank = fb_event->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	new_status = *blank ? BLANK : UNBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (new_status == n->old_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if ((n->old_status == UNBLANK) ^ n->invert) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		n->brightness = led->brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		led_set_brightness_nosleep(led, LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		led_set_brightness_nosleep(led, n->brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	n->old_status = new_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static ssize_t bl_trig_invert_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return sprintf(buf, "%u\n", n->invert);
^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 bl_trig_invert_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		struct device_attribute *attr, const char *buf, size_t num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct led_classdev *led = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	unsigned long invert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	ret = kstrtoul(buf, 10, &invert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (invert > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	n->invert = invert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* After inverting, we need to update the LED. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if ((n->old_status == BLANK) ^ n->invert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		led_set_brightness_nosleep(led, LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		led_set_brightness_nosleep(led, n->brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static struct attribute *bl_trig_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	&dev_attr_inverted.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) ATTRIBUTE_GROUPS(bl_trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int bl_trig_activate(struct led_classdev *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct bl_trig_notifier *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	led_set_trigger_data(led, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	n->led = led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	n->brightness = led->brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	n->old_status = UNBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	n->notifier.notifier_call = fb_notifier_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ret = fb_register_client(&n->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		dev_err(led->dev, "unable to register backlight trigger\n");
^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 bl_trig_deactivate(struct led_classdev *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct bl_trig_notifier *n = led_get_trigger_data(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	fb_unregister_client(&n->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	kfree(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static struct led_trigger bl_led_trigger = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	.name		= "backlight",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	.activate	= bl_trig_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.deactivate	= bl_trig_deactivate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	.groups		= bl_trig_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) module_led_trigger(bl_led_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) MODULE_DESCRIPTION("Backlight emulation LED trigger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) MODULE_LICENSE("GPL v2");