blob: 33cc99a1a16a135aa0796bcadbc9713064aa1980 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Felipe Balbi17354bf2009-02-17 13:18:11 +02002/*
3 * ledtrig-gio.c - LED Trigger Based on GPIO events
4 *
5 * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
Felipe Balbi17354bf2009-02-17 13:18:11 +02006 */
7
8#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/gpio.h>
12#include <linux/interrupt.h>
Felipe Balbi17354bf2009-02-17 13:18:11 +020013#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Kim, Milof07fb522013-02-20 00:36:01 -080015#include "../leds.h"
Felipe Balbi17354bf2009-02-17 13:18:11 +020016
17struct gpio_trig_data {
18 struct led_classdev *led;
Felipe Balbi17354bf2009-02-17 13:18:11 +020019
20 unsigned desired_brightness; /* desired brightness when led is on */
21 unsigned inverted; /* true when gpio is inverted */
22 unsigned gpio; /* gpio that triggers the leds */
23};
24
25static irqreturn_t gpio_trig_irq(int irq, void *_led)
26{
27 struct led_classdev *led = _led;
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020028 struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
Felipe Balbi17354bf2009-02-17 13:18:11 +020029 int tmp;
30
Lothar Waßmann914ae252014-09-09 00:40:32 -070031 tmp = gpio_get_value_cansleep(gpio_data->gpio);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070032 if (gpio_data->inverted)
33 tmp = !tmp;
Felipe Balbi17354bf2009-02-17 13:18:11 +020034
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070035 if (tmp) {
36 if (gpio_data->desired_brightness)
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020037 led_set_brightness_nosleep(gpio_data->led,
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070038 gpio_data->desired_brightness);
39 else
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020040 led_set_brightness_nosleep(gpio_data->led, LED_FULL);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070041 } else {
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +020042 led_set_brightness_nosleep(gpio_data->led, LED_OFF);
Thadeu Lima de Souza Cascardo74cbe202009-08-06 16:04:51 -070043 }
Jan Kiszka71c17b02017-05-26 15:17:47 +020044
45 return IRQ_HANDLED;
Felipe Balbi17354bf2009-02-17 13:18:11 +020046}
47
48static ssize_t gpio_trig_brightness_show(struct device *dev,
49 struct device_attribute *attr, char *buf)
50{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020051 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +020052
53 return sprintf(buf, "%u\n", gpio_data->desired_brightness);
54}
55
56static ssize_t gpio_trig_brightness_store(struct device *dev,
57 struct device_attribute *attr, const char *buf, size_t n)
58{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020059 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +020060 unsigned desired_brightness;
61 int ret;
62
63 ret = sscanf(buf, "%u", &desired_brightness);
64 if (ret < 1 || desired_brightness > 255) {
65 dev_err(dev, "invalid value\n");
66 return -EINVAL;
67 }
68
69 gpio_data->desired_brightness = desired_brightness;
70
71 return n;
72}
73static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
74 gpio_trig_brightness_store);
75
76static ssize_t gpio_trig_inverted_show(struct device *dev,
77 struct device_attribute *attr, char *buf)
78{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020079 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +020080
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080081 return sprintf(buf, "%u\n", gpio_data->inverted);
Felipe Balbi17354bf2009-02-17 13:18:11 +020082}
83
84static ssize_t gpio_trig_inverted_store(struct device *dev,
85 struct device_attribute *attr, const char *buf, size_t n)
86{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +020087 struct led_classdev *led = led_trigger_get_led(dev);
88 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080089 unsigned long inverted;
Felipe Balbi17354bf2009-02-17 13:18:11 +020090 int ret;
91
Jingoo Hane8941922012-10-23 05:27:52 -070092 ret = kstrtoul(buf, 10, &inverted);
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080093 if (ret < 0)
94 return ret;
Felipe Balbi17354bf2009-02-17 13:18:11 +020095
Janusz Krzysztofikcc587ec2011-01-20 14:44:29 -080096 if (inverted > 1)
97 return -EINVAL;
98
99 gpio_data->inverted = inverted;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200100
Thadeu Lima de Souza Cascardocc674c82009-08-26 14:29:32 -0700101 /* After inverting, we need to update the LED. */
Jan Kiszka71c17b02017-05-26 15:17:47 +0200102 gpio_trig_irq(0, led);
Thadeu Lima de Souza Cascardocc674c82009-08-26 14:29:32 -0700103
Felipe Balbi17354bf2009-02-17 13:18:11 +0200104 return n;
105}
106static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
107 gpio_trig_inverted_store);
108
109static ssize_t gpio_trig_gpio_show(struct device *dev,
110 struct device_attribute *attr, char *buf)
111{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200112 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200113
114 return sprintf(buf, "%u\n", gpio_data->gpio);
115}
116
117static ssize_t gpio_trig_gpio_store(struct device *dev,
118 struct device_attribute *attr, const char *buf, size_t n)
119{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200120 struct led_classdev *led = led_trigger_get_led(dev);
121 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200122 unsigned gpio;
123 int ret;
124
125 ret = sscanf(buf, "%u", &gpio);
126 if (ret < 1) {
127 dev_err(dev, "couldn't read gpio number\n");
Felipe Balbi17354bf2009-02-17 13:18:11 +0200128 return -EINVAL;
129 }
130
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700131 if (gpio_data->gpio == gpio)
132 return n;
133
Felipe Balbi17354bf2009-02-17 13:18:11 +0200134 if (!gpio) {
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700135 if (gpio_data->gpio != 0)
136 free_irq(gpio_to_irq(gpio_data->gpio), led);
137 gpio_data->gpio = 0;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200138 return n;
139 }
140
Jan Kiszka71c17b02017-05-26 15:17:47 +0200141 ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq,
142 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
Felipe Balbi17354bf2009-02-17 13:18:11 +0200143 | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700144 if (ret) {
Felipe Balbi17354bf2009-02-17 13:18:11 +0200145 dev_err(dev, "request_irq failed with error %d\n", ret);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700146 } else {
147 if (gpio_data->gpio != 0)
148 free_irq(gpio_to_irq(gpio_data->gpio), led);
149 gpio_data->gpio = gpio;
Jan Kiszka71afe3c2017-05-26 15:17:46 +0200150 /* After changing the GPIO, we need to update the LED. */
Jan Kiszka71c17b02017-05-26 15:17:47 +0200151 gpio_trig_irq(0, led);
Thadeu Lima de Souza Cascardo48cccd22009-08-26 14:29:31 -0700152 }
Felipe Balbi17354bf2009-02-17 13:18:11 +0200153
154 return ret ? ret : n;
155}
156static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
157
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200158static struct attribute *gpio_trig_attrs[] = {
159 &dev_attr_desired_brightness.attr,
160 &dev_attr_inverted.attr,
161 &dev_attr_gpio.attr,
162 NULL
163};
164ATTRIBUTE_GROUPS(gpio_trig);
165
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200166static int gpio_trig_activate(struct led_classdev *led)
Felipe Balbi17354bf2009-02-17 13:18:11 +0200167{
168 struct gpio_trig_data *gpio_data;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200169
170 gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
171 if (!gpio_data)
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200172 return -ENOMEM;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200173
174 gpio_data->led = led;
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200175 led_set_trigger_data(led, gpio_data);
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200176
177 return 0;
Felipe Balbi17354bf2009-02-17 13:18:11 +0200178}
179
180static void gpio_trig_deactivate(struct led_classdev *led)
181{
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200182 struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200183
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200184 if (gpio_data->gpio != 0)
185 free_irq(gpio_to_irq(gpio_data->gpio), led);
186 kfree(gpio_data);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200187}
188
189static struct led_trigger gpio_led_trigger = {
190 .name = "gpio",
191 .activate = gpio_trig_activate,
192 .deactivate = gpio_trig_deactivate,
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200193 .groups = gpio_trig_groups,
Felipe Balbi17354bf2009-02-17 13:18:11 +0200194};
Uwe Kleine-König9bfd7d92018-07-02 22:05:34 +0200195module_led_trigger(gpio_led_trigger);
Felipe Balbi17354bf2009-02-17 13:18:11 +0200196
197MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
198MODULE_DESCRIPTION("GPIO LED trigger");
Uwe Kleine-König033692e2018-07-02 22:05:20 +0200199MODULE_LICENSE("GPL v2");