blob: ed0db8ed825fba37911263b709a9fbca8de1c701 [file] [log] [blame]
Felipe Balbi17354bf2009-02-17 13:18:11 +02001/*
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.
Felipe Balbi17354bf2009-02-17 13:18:11 +02009 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/gpio.h>
15#include <linux/interrupt.h>
Felipe Balbi17354bf2009-02-17 13:18:11 +020016#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Kim, Milof07fb522013-02-20 00:36:01 -080018#include "../leds.h"
Felipe Balbi17354bf2009-02-17 13:18:11 +020019
20struct gpio_trig_data {
21 struct led_classdev *led;
Felipe Balbi17354bf2009-02-17 13:18:11 +020022
23 unsigned desired_brightness; /* desired brightness when led is on */
24 unsigned inverted; /* true when gpio is inverted */
25 unsigned gpio; /* gpio that triggers the leds */
26};
27
28static irqreturn_t gpio_trig_irq(int irq, void *_led)
29{
30 struct led_classdev *led = _led;
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020031 struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
Felipe Balbi17354bf2009-02-17 13:18:11 +020032 int tmp;
33
Lothar Waßmann914ae252014-09-09 00:40:32 -070034 tmp = gpio_get_value_cansleep(gpio_data->gpio);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070035 if (gpio_data->inverted)
36 tmp = !tmp;
Felipe Balbi17354bf2009-02-17 13:18:11 +020037
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070038 if (tmp) {
39 if (gpio_data->desired_brightness)
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020040 led_set_brightness_nosleep(gpio_data->led,
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070041 gpio_data->desired_brightness);
42 else
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020043 led_set_brightness_nosleep(gpio_data->led, LED_FULL);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070044 } else {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020045 led_set_brightness_nosleep(gpio_data->led, LED_OFF);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070046 }
Jan Kiszka71c17b02017-05-26 15:17:47 +020047
48 return IRQ_HANDLED;
Felipe Balbi17354bf2009-02-17 13:18:11 +020049}
50
51static ssize_t gpio_trig_brightness_show(struct device *dev,
52 struct device_attribute *attr, char *buf)
53{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020054 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +020055
56 return sprintf(buf, "%u\n", gpio_data->desired_brightness);
57}
58
59static ssize_t gpio_trig_brightness_store(struct device *dev,
60 struct device_attribute *attr, const char *buf, size_t n)
61{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020062 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +020063 unsigned desired_brightness;
64 int ret;
65
66 ret = sscanf(buf, "%u", &desired_brightness);
67 if (ret < 1 || desired_brightness > 255) {
68 dev_err(dev, "invalid value\n");
69 return -EINVAL;
70 }
71
72 gpio_data->desired_brightness = desired_brightness;
73
74 return n;
75}
76static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
77 gpio_trig_brightness_store);
78
79static ssize_t gpio_trig_inverted_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
81{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020082 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +020083
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080084 return sprintf(buf, "%u\n", gpio_data->inverted);
Felipe Balbi17354bf2009-02-17 13:18:11 +020085}
86
87static ssize_t gpio_trig_inverted_store(struct device *dev,
88 struct device_attribute *attr, const char *buf, size_t n)
89{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020090 struct led_classdev *led = led_trigger_get_led(dev);
91 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080092 unsigned long inverted;
Felipe Balbi17354bf2009-02-17 13:18:11 +020093 int ret;
94
Jingoo Hane8941922012-10-23 05:27:52 -070095 ret = kstrtoul(buf, 10, &inverted);
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080096 if (ret < 0)
97 return ret;
Felipe Balbi17354bf2009-02-17 13:18:11 +020098
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080099 if (inverted > 1)
100 return -EINVAL;
101
102 gpio_data->inverted = inverted;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200103
Thadeu Lima de Souza Cascardocc674c82009-08-26 14:29:32 -0700104 /* After inverting, we need to update the LED. */
Jan Kiszka71c17b02017-05-26 15:17:47 +0200105 gpio_trig_irq(0, led);
Thadeu Lima de Souza Cascardocc674c82009-08-26 14:29:32 -0700106
Felipe Balbi17354bf2009-02-17 13:18:11 +0200107 return n;
108}
109static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
110 gpio_trig_inverted_store);
111
112static ssize_t gpio_trig_gpio_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
114{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200115 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200116
117 return sprintf(buf, "%u\n", gpio_data->gpio);
118}
119
120static ssize_t gpio_trig_gpio_store(struct device *dev,
121 struct device_attribute *attr, const char *buf, size_t n)
122{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200123 struct led_classdev *led = led_trigger_get_led(dev);
124 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200125 unsigned gpio;
126 int ret;
127
128 ret = sscanf(buf, "%u", &gpio);
129 if (ret < 1) {
130 dev_err(dev, "couldn't read gpio number\n");
Felipe Balbi17354bf2009-02-17 13:18:11 +0200131 return -EINVAL;
132 }
133
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700134 if (gpio_data->gpio == gpio)
135 return n;
136
Felipe Balbi17354bf2009-02-17 13:18:11 +0200137 if (!gpio) {
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700138 if (gpio_data->gpio != 0)
139 free_irq(gpio_to_irq(gpio_data->gpio), led);
140 gpio_data->gpio = 0;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200141 return n;
142 }
143
Jan Kiszka71c17b02017-05-26 15:17:47 +0200144 ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq,
145 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
Felipe Balbi17354bf2009-02-17 13:18:11 +0200146 | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700147 if (ret) {
Felipe Balbi17354bf2009-02-17 13:18:11 +0200148 dev_err(dev, "request_irq failed with error %d\n", ret);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700149 } else {
150 if (gpio_data->gpio != 0)
151 free_irq(gpio_to_irq(gpio_data->gpio), led);
152 gpio_data->gpio = gpio;
Jan Kiszka71afe3c2017-05-26 15:17:46 +0200153 /* After changing the GPIO, we need to update the LED. */
Jan Kiszka71c17b02017-05-26 15:17:47 +0200154 gpio_trig_irq(0, led);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700155 }
Felipe Balbi17354bf2009-02-17 13:18:11 +0200156
157 return ret ? ret : n;
158}
159static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
160
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200161static struct attribute *gpio_trig_attrs[] = {
162 &dev_attr_desired_brightness.attr,
163 &dev_attr_inverted.attr,
164 &dev_attr_gpio.attr,
165 NULL
166};
167ATTRIBUTE_GROUPS(gpio_trig);
168
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200169static int gpio_trig_activate(struct led_classdev *led)
Felipe Balbi17354bf2009-02-17 13:18:11 +0200170{
171 struct gpio_trig_data *gpio_data;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200172
173 gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
174 if (!gpio_data)
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200175 return -ENOMEM;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200176
177 gpio_data->led = led;
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200178 led_set_trigger_data(led, gpio_data);
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200179
180 return 0;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200181}
182
183static void gpio_trig_deactivate(struct led_classdev *led)
184{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200185 struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200186
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200187 if (gpio_data->gpio != 0)
188 free_irq(gpio_to_irq(gpio_data->gpio), led);
189 kfree(gpio_data);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200190}
191
192static struct led_trigger gpio_led_trigger = {
193 .name = "gpio",
194 .activate = gpio_trig_activate,
195 .deactivate = gpio_trig_deactivate,
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200196 .groups = gpio_trig_groups,
Felipe Balbi17354bf2009-02-17 13:18:11 +0200197};
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200198module_led_trigger(gpio_led_trigger);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200199
200MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
201MODULE_DESCRIPTION("GPIO LED trigger");
Uwe Kleine-König033692e2018-07-02 22:05:20 +0200202MODULE_LICENSE("GPL v2");