Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ledtrig-gio.c - LED Trigger Based on GPIO events
  4 *
  5 * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
  6 * Copyright 2023 Linus Walleij <linus.walleij@linaro.org>
 
 
 
 
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <linux/init.h>
 12#include <linux/gpio/consumer.h>
 13#include <linux/interrupt.h>
 
 14#include <linux/leds.h>
 15#include <linux/slab.h>
 16#include "../leds.h"
 17
 18struct gpio_trig_data {
 19	struct led_classdev *led;
 
 
 20	unsigned desired_brightness;	/* desired brightness when led is on */
 21	struct gpio_desc *gpiod;	/* gpio that triggers the led */
 
 22};
 23
 24static irqreturn_t gpio_trig_irq(int irq, void *_led)
 25{
 26	struct led_classdev *led = _led;
 27	struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
 
 
 
 
 
 
 
 
 
 
 
 28	int tmp;
 29
 30	tmp = gpiod_get_value_cansleep(gpio_data->gpiod);
 
 
 
 
 
 
 31	if (tmp) {
 32		if (gpio_data->desired_brightness)
 33			led_set_brightness_nosleep(gpio_data->led,
 34					   gpio_data->desired_brightness);
 35		else
 36			led_set_brightness_nosleep(gpio_data->led, LED_FULL);
 37	} else {
 38		led_set_brightness_nosleep(gpio_data->led, LED_OFF);
 39	}
 40
 41	return IRQ_HANDLED;
 42}
 43
 44static ssize_t desired_brightness_show(struct device *dev,
 45		struct device_attribute *attr, char *buf)
 46{
 47	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
 
 48
 49	return sysfs_emit(buf, "%u\n", gpio_data->desired_brightness);
 50}
 51
 52static ssize_t desired_brightness_store(struct device *dev,
 53		struct device_attribute *attr, const char *buf, size_t n)
 54{
 55	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
 56	u8 desired_brightness;
 
 57	int ret;
 58
 59	ret = kstrtou8(buf, 10, &desired_brightness);
 60	if (ret)
 61		return ret;
 
 
 62
 63	gpio_data->desired_brightness = desired_brightness;
 64
 65	return n;
 66}
 67static DEVICE_ATTR_RW(desired_brightness);
 
 68
 69static struct attribute *gpio_trig_attrs[] = {
 70	&dev_attr_desired_brightness.attr,
 71	NULL
 72};
 73ATTRIBUTE_GROUPS(gpio_trig);
 74
 75static int gpio_trig_activate(struct led_classdev *led)
 
 
 
 
 76{
 77	struct gpio_trig_data *gpio_data;
 78	struct device *dev = led->dev;
 
 79	int ret;
 80
 81	gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
 82	if (!gpio_data)
 83		return -ENOMEM;
 84
 85	/*
 86	 * The generic property "trigger-sources" is followed,
 87	 * and we hope that this is a GPIO.
 88	 */
 89	gpio_data->gpiod = gpiod_get_optional(dev, "trigger-sources", GPIOD_IN);
 90	if (IS_ERR(gpio_data->gpiod)) {
 91		ret = PTR_ERR(gpio_data->gpiod);
 92		kfree(gpio_data);
 93		return ret;
 94	}
 95	if (!gpio_data->gpiod) {
 96		dev_err(dev, "no valid GPIO for the trigger\n");
 97		kfree(gpio_data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 98		return -EINVAL;
 99	}
100
101	gpiod_set_consumer_name(gpio_data->gpiod, "led-trigger");
 
102
103	gpio_data->led = led;
104	led_set_trigger_data(led, gpio_data);
 
 
 
 
105
106	ret = request_threaded_irq(gpiod_to_irq(gpio_data->gpiod), NULL, gpio_trig_irq,
107			IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
108			| IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
109	if (ret) {
110		dev_err(dev, "request_irq failed with error %d\n", ret);
111		gpiod_put(gpio_data->gpiod);
112		kfree(gpio_data);
113		return ret;
 
114	}
115
116	/* Finally update the LED to initial status */
117	gpio_trig_irq(0, led);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
119	return 0;
 
120}
121
122static void gpio_trig_deactivate(struct led_classdev *led)
123{
124	struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
125
126	free_irq(gpiod_to_irq(gpio_data->gpiod), led);
127	gpiod_put(gpio_data->gpiod);
128	kfree(gpio_data);
 
 
 
 
 
 
 
129}
130
131static struct led_trigger gpio_led_trigger = {
132	.name		= "gpio",
133	.activate	= gpio_trig_activate,
134	.deactivate	= gpio_trig_deactivate,
135	.groups		= gpio_trig_groups,
136};
137module_led_trigger(gpio_led_trigger);
 
 
 
 
 
 
 
 
 
 
 
138
139MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
140MODULE_DESCRIPTION("GPIO LED trigger");
141MODULE_LICENSE("GPL v2");
v4.6
 
  1/*
  2 * ledtrig-gio.c - LED Trigger Based on GPIO events
  3 *
  4 * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License version 2 as
  8 * published by the Free Software Foundation.
  9 *
 10 */
 11
 12#include <linux/module.h>
 13#include <linux/kernel.h>
 14#include <linux/init.h>
 15#include <linux/gpio.h>
 16#include <linux/interrupt.h>
 17#include <linux/workqueue.h>
 18#include <linux/leds.h>
 19#include <linux/slab.h>
 20#include "../leds.h"
 21
 22struct gpio_trig_data {
 23	struct led_classdev *led;
 24	struct work_struct work;
 25
 26	unsigned desired_brightness;	/* desired brightness when led is on */
 27	unsigned inverted;		/* true when gpio is inverted */
 28	unsigned gpio;			/* gpio that triggers the leds */
 29};
 30
 31static irqreturn_t gpio_trig_irq(int irq, void *_led)
 32{
 33	struct led_classdev *led = _led;
 34	struct gpio_trig_data *gpio_data = led->trigger_data;
 35
 36	/* just schedule_work since gpio_get_value can sleep */
 37	schedule_work(&gpio_data->work);
 38
 39	return IRQ_HANDLED;
 40};
 41
 42static void gpio_trig_work(struct work_struct *work)
 43{
 44	struct gpio_trig_data *gpio_data = container_of(work,
 45			struct gpio_trig_data, work);
 46	int tmp;
 47
 48	if (!gpio_data->gpio)
 49		return;
 50
 51	tmp = gpio_get_value_cansleep(gpio_data->gpio);
 52	if (gpio_data->inverted)
 53		tmp = !tmp;
 54
 55	if (tmp) {
 56		if (gpio_data->desired_brightness)
 57			led_set_brightness_nosleep(gpio_data->led,
 58					   gpio_data->desired_brightness);
 59		else
 60			led_set_brightness_nosleep(gpio_data->led, LED_FULL);
 61	} else {
 62		led_set_brightness_nosleep(gpio_data->led, LED_OFF);
 63	}
 
 
 64}
 65
 66static ssize_t gpio_trig_brightness_show(struct device *dev,
 67		struct device_attribute *attr, char *buf)
 68{
 69	struct led_classdev *led = dev_get_drvdata(dev);
 70	struct gpio_trig_data *gpio_data = led->trigger_data;
 71
 72	return sprintf(buf, "%u\n", gpio_data->desired_brightness);
 73}
 74
 75static ssize_t gpio_trig_brightness_store(struct device *dev,
 76		struct device_attribute *attr, const char *buf, size_t n)
 77{
 78	struct led_classdev *led = dev_get_drvdata(dev);
 79	struct gpio_trig_data *gpio_data = led->trigger_data;
 80	unsigned desired_brightness;
 81	int ret;
 82
 83	ret = sscanf(buf, "%u", &desired_brightness);
 84	if (ret < 1 || desired_brightness > 255) {
 85		dev_err(dev, "invalid value\n");
 86		return -EINVAL;
 87	}
 88
 89	gpio_data->desired_brightness = desired_brightness;
 90
 91	return n;
 92}
 93static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
 94		gpio_trig_brightness_store);
 95
 96static ssize_t gpio_trig_inverted_show(struct device *dev,
 97		struct device_attribute *attr, char *buf)
 98{
 99	struct led_classdev *led = dev_get_drvdata(dev);
100	struct gpio_trig_data *gpio_data = led->trigger_data;
101
102	return sprintf(buf, "%u\n", gpio_data->inverted);
103}
104
105static ssize_t gpio_trig_inverted_store(struct device *dev,
106		struct device_attribute *attr, const char *buf, size_t n)
107{
108	struct led_classdev *led = dev_get_drvdata(dev);
109	struct gpio_trig_data *gpio_data = led->trigger_data;
110	unsigned long inverted;
111	int ret;
112
113	ret = kstrtoul(buf, 10, &inverted);
114	if (ret < 0)
 
 
 
 
 
 
 
 
 
 
115		return ret;
116
117	if (inverted > 1)
118		return -EINVAL;
119
120	gpio_data->inverted = inverted;
121
122	/* After inverting, we need to update the LED. */
123	schedule_work(&gpio_data->work);
124
125	return n;
126}
127static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
128		gpio_trig_inverted_store);
129
130static ssize_t gpio_trig_gpio_show(struct device *dev,
131		struct device_attribute *attr, char *buf)
132{
133	struct led_classdev *led = dev_get_drvdata(dev);
134	struct gpio_trig_data *gpio_data = led->trigger_data;
135
136	return sprintf(buf, "%u\n", gpio_data->gpio);
137}
138
139static ssize_t gpio_trig_gpio_store(struct device *dev,
140		struct device_attribute *attr, const char *buf, size_t n)
141{
142	struct led_classdev *led = dev_get_drvdata(dev);
143	struct gpio_trig_data *gpio_data = led->trigger_data;
144	unsigned gpio;
145	int ret;
146
147	ret = sscanf(buf, "%u", &gpio);
148	if (ret < 1) {
149		dev_err(dev, "couldn't read gpio number\n");
150		flush_work(&gpio_data->work);
151		return -EINVAL;
152	}
153
154	if (gpio_data->gpio == gpio)
155		return n;
156
157	if (!gpio) {
158		if (gpio_data->gpio != 0)
159			free_irq(gpio_to_irq(gpio_data->gpio), led);
160		gpio_data->gpio = 0;
161		return n;
162	}
163
164	ret = request_irq(gpio_to_irq(gpio), gpio_trig_irq,
165			IRQF_SHARED | IRQF_TRIGGER_RISING
166			| IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
167	if (ret) {
168		dev_err(dev, "request_irq failed with error %d\n", ret);
169	} else {
170		if (gpio_data->gpio != 0)
171			free_irq(gpio_to_irq(gpio_data->gpio), led);
172		gpio_data->gpio = gpio;
173	}
174
175	return ret ? ret : n;
176}
177static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
178
179static void gpio_trig_activate(struct led_classdev *led)
180{
181	struct gpio_trig_data *gpio_data;
182	int ret;
183
184	gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
185	if (!gpio_data)
186		return;
187
188	ret = device_create_file(led->dev, &dev_attr_gpio);
189	if (ret)
190		goto err_gpio;
191
192	ret = device_create_file(led->dev, &dev_attr_inverted);
193	if (ret)
194		goto err_inverted;
195
196	ret = device_create_file(led->dev, &dev_attr_desired_brightness);
197	if (ret)
198		goto err_brightness;
199
200	gpio_data->led = led;
201	led->trigger_data = gpio_data;
202	INIT_WORK(&gpio_data->work, gpio_trig_work);
203	led->activated = true;
204
205	return;
206
207err_brightness:
208	device_remove_file(led->dev, &dev_attr_inverted);
209
210err_inverted:
211	device_remove_file(led->dev, &dev_attr_gpio);
212
213err_gpio:
214	kfree(gpio_data);
215}
216
217static void gpio_trig_deactivate(struct led_classdev *led)
218{
219	struct gpio_trig_data *gpio_data = led->trigger_data;
220
221	if (led->activated) {
222		device_remove_file(led->dev, &dev_attr_gpio);
223		device_remove_file(led->dev, &dev_attr_inverted);
224		device_remove_file(led->dev, &dev_attr_desired_brightness);
225		flush_work(&gpio_data->work);
226		if (gpio_data->gpio != 0)
227			free_irq(gpio_to_irq(gpio_data->gpio), led);
228		kfree(gpio_data);
229		led->activated = false;
230	}
231}
232
233static struct led_trigger gpio_led_trigger = {
234	.name		= "gpio",
235	.activate	= gpio_trig_activate,
236	.deactivate	= gpio_trig_deactivate,
 
237};
238
239static int __init gpio_trig_init(void)
240{
241	return led_trigger_register(&gpio_led_trigger);
242}
243module_init(gpio_trig_init);
244
245static void __exit gpio_trig_exit(void)
246{
247	led_trigger_unregister(&gpio_led_trigger);
248}
249module_exit(gpio_trig_exit);
250
251MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
252MODULE_DESCRIPTION("GPIO LED trigger");
253MODULE_LICENSE("GPL");