18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ledtrig-gio.c - LED Trigger Based on GPIO events
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/gpio.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/leds.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include "../leds.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistruct gpio_trig_data {
188c2ecf20Sopenharmony_ci	struct led_classdev *led;
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	unsigned desired_brightness;	/* desired brightness when led is on */
218c2ecf20Sopenharmony_ci	unsigned inverted;		/* true when gpio is inverted */
228c2ecf20Sopenharmony_ci	unsigned gpio;			/* gpio that triggers the leds */
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic irqreturn_t gpio_trig_irq(int irq, void *_led)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	struct led_classdev *led = _led;
288c2ecf20Sopenharmony_ci	struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
298c2ecf20Sopenharmony_ci	int tmp;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	tmp = gpio_get_value_cansleep(gpio_data->gpio);
328c2ecf20Sopenharmony_ci	if (gpio_data->inverted)
338c2ecf20Sopenharmony_ci		tmp = !tmp;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	if (tmp) {
368c2ecf20Sopenharmony_ci		if (gpio_data->desired_brightness)
378c2ecf20Sopenharmony_ci			led_set_brightness_nosleep(gpio_data->led,
388c2ecf20Sopenharmony_ci					   gpio_data->desired_brightness);
398c2ecf20Sopenharmony_ci		else
408c2ecf20Sopenharmony_ci			led_set_brightness_nosleep(gpio_data->led, LED_FULL);
418c2ecf20Sopenharmony_ci	} else {
428c2ecf20Sopenharmony_ci		led_set_brightness_nosleep(gpio_data->led, LED_OFF);
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic ssize_t gpio_trig_brightness_show(struct device *dev,
498c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", gpio_data->desired_brightness);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic ssize_t gpio_trig_brightness_store(struct device *dev,
578c2ecf20Sopenharmony_ci		struct device_attribute *attr, const char *buf, size_t n)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
608c2ecf20Sopenharmony_ci	unsigned desired_brightness;
618c2ecf20Sopenharmony_ci	int ret;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	ret = sscanf(buf, "%u", &desired_brightness);
648c2ecf20Sopenharmony_ci	if (ret < 1 || desired_brightness > 255) {
658c2ecf20Sopenharmony_ci		dev_err(dev, "invalid value\n");
668c2ecf20Sopenharmony_ci		return -EINVAL;
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	gpio_data->desired_brightness = desired_brightness;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	return n;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_cistatic DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
748c2ecf20Sopenharmony_ci		gpio_trig_brightness_store);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic ssize_t gpio_trig_inverted_show(struct device *dev,
778c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", gpio_data->inverted);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic ssize_t gpio_trig_inverted_store(struct device *dev,
858c2ecf20Sopenharmony_ci		struct device_attribute *attr, const char *buf, size_t n)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct led_classdev *led = led_trigger_get_led(dev);
888c2ecf20Sopenharmony_ci	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
898c2ecf20Sopenharmony_ci	unsigned long inverted;
908c2ecf20Sopenharmony_ci	int ret;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	ret = kstrtoul(buf, 10, &inverted);
938c2ecf20Sopenharmony_ci	if (ret < 0)
948c2ecf20Sopenharmony_ci		return ret;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (inverted > 1)
978c2ecf20Sopenharmony_ci		return -EINVAL;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	gpio_data->inverted = inverted;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* After inverting, we need to update the LED. */
1028c2ecf20Sopenharmony_ci	if (gpio_is_valid(gpio_data->gpio))
1038c2ecf20Sopenharmony_ci		gpio_trig_irq(0, led);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	return n;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_cistatic DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
1088c2ecf20Sopenharmony_ci		gpio_trig_inverted_store);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic ssize_t gpio_trig_gpio_show(struct device *dev,
1118c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", gpio_data->gpio);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic ssize_t gpio_trig_gpio_store(struct device *dev,
1198c2ecf20Sopenharmony_ci		struct device_attribute *attr, const char *buf, size_t n)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct led_classdev *led = led_trigger_get_led(dev);
1228c2ecf20Sopenharmony_ci	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
1238c2ecf20Sopenharmony_ci	unsigned gpio;
1248c2ecf20Sopenharmony_ci	int ret;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	ret = sscanf(buf, "%u", &gpio);
1278c2ecf20Sopenharmony_ci	if (ret < 1) {
1288c2ecf20Sopenharmony_ci		dev_err(dev, "couldn't read gpio number\n");
1298c2ecf20Sopenharmony_ci		return -EINVAL;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	if (gpio_data->gpio == gpio)
1338c2ecf20Sopenharmony_ci		return n;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (!gpio_is_valid(gpio)) {
1368c2ecf20Sopenharmony_ci		if (gpio_is_valid(gpio_data->gpio))
1378c2ecf20Sopenharmony_ci			free_irq(gpio_to_irq(gpio_data->gpio), led);
1388c2ecf20Sopenharmony_ci		gpio_data->gpio = gpio;
1398c2ecf20Sopenharmony_ci		return n;
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq,
1438c2ecf20Sopenharmony_ci			IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
1448c2ecf20Sopenharmony_ci			| IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
1458c2ecf20Sopenharmony_ci	if (ret) {
1468c2ecf20Sopenharmony_ci		dev_err(dev, "request_irq failed with error %d\n", ret);
1478c2ecf20Sopenharmony_ci	} else {
1488c2ecf20Sopenharmony_ci		if (gpio_is_valid(gpio_data->gpio))
1498c2ecf20Sopenharmony_ci			free_irq(gpio_to_irq(gpio_data->gpio), led);
1508c2ecf20Sopenharmony_ci		gpio_data->gpio = gpio;
1518c2ecf20Sopenharmony_ci		/* After changing the GPIO, we need to update the LED. */
1528c2ecf20Sopenharmony_ci		gpio_trig_irq(0, led);
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	return ret ? ret : n;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_cistatic DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic struct attribute *gpio_trig_attrs[] = {
1608c2ecf20Sopenharmony_ci	&dev_attr_desired_brightness.attr,
1618c2ecf20Sopenharmony_ci	&dev_attr_inverted.attr,
1628c2ecf20Sopenharmony_ci	&dev_attr_gpio.attr,
1638c2ecf20Sopenharmony_ci	NULL
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(gpio_trig);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int gpio_trig_activate(struct led_classdev *led)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct gpio_trig_data *gpio_data;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
1728c2ecf20Sopenharmony_ci	if (!gpio_data)
1738c2ecf20Sopenharmony_ci		return -ENOMEM;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	gpio_data->led = led;
1768c2ecf20Sopenharmony_ci	gpio_data->gpio = -ENOENT;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	led_set_trigger_data(led, gpio_data);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	return 0;
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic void gpio_trig_deactivate(struct led_classdev *led)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (gpio_is_valid(gpio_data->gpio))
1888c2ecf20Sopenharmony_ci		free_irq(gpio_to_irq(gpio_data->gpio), led);
1898c2ecf20Sopenharmony_ci	kfree(gpio_data);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic struct led_trigger gpio_led_trigger = {
1938c2ecf20Sopenharmony_ci	.name		= "gpio",
1948c2ecf20Sopenharmony_ci	.activate	= gpio_trig_activate,
1958c2ecf20Sopenharmony_ci	.deactivate	= gpio_trig_deactivate,
1968c2ecf20Sopenharmony_ci	.groups		= gpio_trig_groups,
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_cimodule_led_trigger(gpio_led_trigger);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ciMODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
2018c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("GPIO LED trigger");
2028c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
203