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)  * ledtrig-gio.c - LED Trigger Based on GPIO events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.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) struct gpio_trig_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	struct led_classdev *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	unsigned desired_brightness;	/* desired brightness when led is on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	unsigned inverted;		/* true when gpio is inverted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	unsigned gpio;			/* gpio that triggers the leds */
^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 irqreturn_t gpio_trig_irq(int irq, void *_led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct led_classdev *led = _led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	tmp = gpio_get_value_cansleep(gpio_data->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (gpio_data->inverted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		tmp = !tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		if (gpio_data->desired_brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			led_set_brightness_nosleep(gpio_data->led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 					   gpio_data->desired_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			led_set_brightness_nosleep(gpio_data->led, LED_FULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		led_set_brightness_nosleep(gpio_data->led, LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static ssize_t gpio_trig_brightness_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return sprintf(buf, "%u\n", gpio_data->desired_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static ssize_t gpio_trig_brightness_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		struct device_attribute *attr, const char *buf, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	unsigned desired_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	ret = sscanf(buf, "%u", &desired_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (ret < 1 || desired_brightness > 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		dev_err(dev, "invalid value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	gpio_data->desired_brightness = desired_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		gpio_trig_brightness_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static ssize_t gpio_trig_inverted_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return sprintf(buf, "%u\n", gpio_data->inverted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static ssize_t gpio_trig_inverted_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		struct device_attribute *attr, const char *buf, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct led_classdev *led = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	unsigned long inverted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	ret = kstrtoul(buf, 10, &inverted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (inverted > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	gpio_data->inverted = inverted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	/* After inverting, we need to update the LED. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (gpio_is_valid(gpio_data->gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		gpio_trig_irq(0, led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		gpio_trig_inverted_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static ssize_t gpio_trig_gpio_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return sprintf(buf, "%u\n", gpio_data->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static ssize_t gpio_trig_gpio_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		struct device_attribute *attr, const char *buf, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct led_classdev *led = led_trigger_get_led(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	unsigned gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	ret = sscanf(buf, "%u", &gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (ret < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		dev_err(dev, "couldn't read gpio number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return -EINVAL;
^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) 	if (gpio_data->gpio == gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (!gpio_is_valid(gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		if (gpio_is_valid(gpio_data->gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			free_irq(gpio_to_irq(gpio_data->gpio), led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		gpio_data->gpio = gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			| IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		dev_err(dev, "request_irq failed with error %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (gpio_is_valid(gpio_data->gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			free_irq(gpio_to_irq(gpio_data->gpio), led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		gpio_data->gpio = gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		/* After changing the GPIO, we need to update the LED. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		gpio_trig_irq(0, led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return ret ? ret : n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static struct attribute *gpio_trig_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	&dev_attr_desired_brightness.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	&dev_attr_inverted.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	&dev_attr_gpio.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ATTRIBUTE_GROUPS(gpio_trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int gpio_trig_activate(struct led_classdev *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct gpio_trig_data *gpio_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (!gpio_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	gpio_data->led = led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	gpio_data->gpio = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	led_set_trigger_data(led, gpio_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static void gpio_trig_deactivate(struct led_classdev *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (gpio_is_valid(gpio_data->gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		free_irq(gpio_to_irq(gpio_data->gpio), led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	kfree(gpio_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static struct led_trigger gpio_led_trigger = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	.name		= "gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	.activate	= gpio_trig_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	.deactivate	= gpio_trig_deactivate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	.groups		= gpio_trig_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) module_led_trigger(gpio_led_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) MODULE_DESCRIPTION("GPIO LED trigger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) MODULE_LICENSE("GPL v2");