powerpc/fadump: Do not allow hot-remove memory from fadump reserved area.
[linux/fpc-iii.git] / drivers / leds / trigger / ledtrig-gpio.c
blobed0db8ed825fba37911263b709a9fbca8de1c701
1 /*
2 * ledtrig-gio.c - LED Trigger Based on GPIO events
4 * Copyright 2009 Felipe Balbi <me@felipebalbi.com>
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 */
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>
16 #include <linux/leds.h>
17 #include <linux/slab.h>
18 #include "../leds.h"
20 struct gpio_trig_data {
21 struct led_classdev *led;
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 */
28 static irqreturn_t gpio_trig_irq(int irq, void *_led)
30 struct led_classdev *led = _led;
31 struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
32 int tmp;
34 tmp = gpio_get_value_cansleep(gpio_data->gpio);
35 if (gpio_data->inverted)
36 tmp = !tmp;
38 if (tmp) {
39 if (gpio_data->desired_brightness)
40 led_set_brightness_nosleep(gpio_data->led,
41 gpio_data->desired_brightness);
42 else
43 led_set_brightness_nosleep(gpio_data->led, LED_FULL);
44 } else {
45 led_set_brightness_nosleep(gpio_data->led, LED_OFF);
48 return IRQ_HANDLED;
51 static ssize_t gpio_trig_brightness_show(struct device *dev,
52 struct device_attribute *attr, char *buf)
54 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
56 return sprintf(buf, "%u\n", gpio_data->desired_brightness);
59 static ssize_t gpio_trig_brightness_store(struct device *dev,
60 struct device_attribute *attr, const char *buf, size_t n)
62 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
63 unsigned desired_brightness;
64 int ret;
66 ret = sscanf(buf, "%u", &desired_brightness);
67 if (ret < 1 || desired_brightness > 255) {
68 dev_err(dev, "invalid value\n");
69 return -EINVAL;
72 gpio_data->desired_brightness = desired_brightness;
74 return n;
76 static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
77 gpio_trig_brightness_store);
79 static ssize_t gpio_trig_inverted_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
82 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
84 return sprintf(buf, "%u\n", gpio_data->inverted);
87 static ssize_t gpio_trig_inverted_store(struct device *dev,
88 struct device_attribute *attr, const char *buf, size_t n)
90 struct led_classdev *led = led_trigger_get_led(dev);
91 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
92 unsigned long inverted;
93 int ret;
95 ret = kstrtoul(buf, 10, &inverted);
96 if (ret < 0)
97 return ret;
99 if (inverted > 1)
100 return -EINVAL;
102 gpio_data->inverted = inverted;
104 /* After inverting, we need to update the LED. */
105 gpio_trig_irq(0, led);
107 return n;
109 static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
110 gpio_trig_inverted_store);
112 static ssize_t gpio_trig_gpio_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
115 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
117 return sprintf(buf, "%u\n", gpio_data->gpio);
120 static ssize_t gpio_trig_gpio_store(struct device *dev,
121 struct device_attribute *attr, const char *buf, size_t n)
123 struct led_classdev *led = led_trigger_get_led(dev);
124 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
125 unsigned gpio;
126 int ret;
128 ret = sscanf(buf, "%u", &gpio);
129 if (ret < 1) {
130 dev_err(dev, "couldn't read gpio number\n");
131 return -EINVAL;
134 if (gpio_data->gpio == gpio)
135 return n;
137 if (!gpio) {
138 if (gpio_data->gpio != 0)
139 free_irq(gpio_to_irq(gpio_data->gpio), led);
140 gpio_data->gpio = 0;
141 return n;
144 ret = request_threaded_irq(gpio_to_irq(gpio), NULL, gpio_trig_irq,
145 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING
146 | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
147 if (ret) {
148 dev_err(dev, "request_irq failed with error %d\n", ret);
149 } else {
150 if (gpio_data->gpio != 0)
151 free_irq(gpio_to_irq(gpio_data->gpio), led);
152 gpio_data->gpio = gpio;
153 /* After changing the GPIO, we need to update the LED. */
154 gpio_trig_irq(0, led);
157 return ret ? ret : n;
159 static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
161 static struct attribute *gpio_trig_attrs[] = {
162 &dev_attr_desired_brightness.attr,
163 &dev_attr_inverted.attr,
164 &dev_attr_gpio.attr,
165 NULL
167 ATTRIBUTE_GROUPS(gpio_trig);
169 static int gpio_trig_activate(struct led_classdev *led)
171 struct gpio_trig_data *gpio_data;
173 gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
174 if (!gpio_data)
175 return -ENOMEM;
177 gpio_data->led = led;
178 led_set_trigger_data(led, gpio_data);
180 return 0;
183 static void gpio_trig_deactivate(struct led_classdev *led)
185 struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
187 if (gpio_data->gpio != 0)
188 free_irq(gpio_to_irq(gpio_data->gpio), led);
189 kfree(gpio_data);
192 static struct led_trigger gpio_led_trigger = {
193 .name = "gpio",
194 .activate = gpio_trig_activate,
195 .deactivate = gpio_trig_deactivate,
196 .groups = gpio_trig_groups,
198 module_led_trigger(gpio_led_trigger);
200 MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
201 MODULE_DESCRIPTION("GPIO LED trigger");
202 MODULE_LICENSE("GPL v2");