Commit 74cbe20294c9f54a0926d19235395cf8e22b7830
Committed by
Richard Purdie
1 parent
2fea09222a
Exists in
master
and in
7 other branches
leds: fix coding style in worker thread code for ledtrig-gpio.
[akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com> Signed-off-by: Samuel R. C. Vale <srcvale@holoscopio.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Richard Purdie <rpurdie@linux.intel.com>
Showing 1 changed file with 16 additions and 16 deletions Inline Diff
drivers/leds/ledtrig-gpio.c
1 | /* | 1 | /* |
2 | * ledtrig-gio.c - LED Trigger Based on GPIO events | 2 | * ledtrig-gio.c - LED Trigger Based on GPIO events |
3 | * | 3 | * |
4 | * Copyright 2009 Felipe Balbi <me@felipebalbi.com> | 4 | * Copyright 2009 Felipe Balbi <me@felipebalbi.com> |
5 | * | 5 | * |
6 | * This program is free software; you can redistribute it and/or modify | 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 | 7 | * it under the terms of the GNU General Public License version 2 as |
8 | * published by the Free Software Foundation. | 8 | * published by the Free Software Foundation. |
9 | * | 9 | * |
10 | */ | 10 | */ |
11 | 11 | ||
12 | #include <linux/module.h> | 12 | #include <linux/module.h> |
13 | #include <linux/kernel.h> | 13 | #include <linux/kernel.h> |
14 | #include <linux/init.h> | 14 | #include <linux/init.h> |
15 | #include <linux/gpio.h> | 15 | #include <linux/gpio.h> |
16 | #include <linux/interrupt.h> | 16 | #include <linux/interrupt.h> |
17 | #include <linux/workqueue.h> | 17 | #include <linux/workqueue.h> |
18 | #include <linux/leds.h> | 18 | #include <linux/leds.h> |
19 | #include "leds.h" | 19 | #include "leds.h" |
20 | 20 | ||
21 | struct gpio_trig_data { | 21 | struct gpio_trig_data { |
22 | struct led_classdev *led; | 22 | struct led_classdev *led; |
23 | struct work_struct work; | 23 | struct work_struct work; |
24 | 24 | ||
25 | unsigned desired_brightness; /* desired brightness when led is on */ | 25 | unsigned desired_brightness; /* desired brightness when led is on */ |
26 | unsigned inverted; /* true when gpio is inverted */ | 26 | unsigned inverted; /* true when gpio is inverted */ |
27 | unsigned gpio; /* gpio that triggers the leds */ | 27 | unsigned gpio; /* gpio that triggers the leds */ |
28 | }; | 28 | }; |
29 | 29 | ||
30 | static irqreturn_t gpio_trig_irq(int irq, void *_led) | 30 | static irqreturn_t gpio_trig_irq(int irq, void *_led) |
31 | { | 31 | { |
32 | struct led_classdev *led = _led; | 32 | struct led_classdev *led = _led; |
33 | struct gpio_trig_data *gpio_data = led->trigger_data; | 33 | struct gpio_trig_data *gpio_data = led->trigger_data; |
34 | 34 | ||
35 | /* just schedule_work since gpio_get_value can sleep */ | 35 | /* just schedule_work since gpio_get_value can sleep */ |
36 | schedule_work(&gpio_data->work); | 36 | schedule_work(&gpio_data->work); |
37 | 37 | ||
38 | return IRQ_HANDLED; | 38 | return IRQ_HANDLED; |
39 | }; | 39 | }; |
40 | 40 | ||
41 | static void gpio_trig_work(struct work_struct *work) | 41 | static void gpio_trig_work(struct work_struct *work) |
42 | { | 42 | { |
43 | struct gpio_trig_data *gpio_data = container_of(work, | 43 | struct gpio_trig_data *gpio_data = container_of(work, |
44 | struct gpio_trig_data, work); | 44 | struct gpio_trig_data, work); |
45 | int tmp; | 45 | int tmp; |
46 | 46 | ||
47 | if (!gpio_data->gpio) | 47 | if (!gpio_data->gpio) |
48 | return; | 48 | return; |
49 | 49 | ||
50 | tmp = gpio_get_value(gpio_data->gpio); | 50 | tmp = gpio_get_value(gpio_data->gpio); |
51 | if (gpio_data->inverted) | 51 | if (gpio_data->inverted) |
52 | tmp = !tmp; | 52 | tmp = !tmp; |
53 | 53 | ||
54 | if (tmp) { | 54 | if (tmp) { |
55 | if (gpio_data->desired_brightness) | 55 | if (gpio_data->desired_brightness) |
56 | led_set_brightness(gpio_data->led, | 56 | led_set_brightness(gpio_data->led, |
57 | gpio_data->desired_brightness); | 57 | gpio_data->desired_brightness); |
58 | else | 58 | else |
59 | led_set_brightness(gpio_data->led, LED_FULL); | 59 | led_set_brightness(gpio_data->led, LED_FULL); |
60 | } else { | 60 | } else { |
61 | led_set_brightness(gpio_data->led, LED_OFF); | 61 | led_set_brightness(gpio_data->led, LED_OFF); |
62 | } | 62 | } |
63 | } | 63 | } |
64 | 64 | ||
65 | static ssize_t gpio_trig_brightness_show(struct device *dev, | 65 | static ssize_t gpio_trig_brightness_show(struct device *dev, |
66 | struct device_attribute *attr, char *buf) | 66 | struct device_attribute *attr, char *buf) |
67 | { | 67 | { |
68 | struct led_classdev *led = dev_get_drvdata(dev); | 68 | struct led_classdev *led = dev_get_drvdata(dev); |
69 | struct gpio_trig_data *gpio_data = led->trigger_data; | 69 | struct gpio_trig_data *gpio_data = led->trigger_data; |
70 | 70 | ||
71 | return sprintf(buf, "%u\n", gpio_data->desired_brightness); | 71 | return sprintf(buf, "%u\n", gpio_data->desired_brightness); |
72 | } | 72 | } |
73 | 73 | ||
74 | static ssize_t gpio_trig_brightness_store(struct device *dev, | 74 | static ssize_t gpio_trig_brightness_store(struct device *dev, |
75 | struct device_attribute *attr, const char *buf, size_t n) | 75 | struct device_attribute *attr, const char *buf, size_t n) |
76 | { | 76 | { |
77 | struct led_classdev *led = dev_get_drvdata(dev); | 77 | struct led_classdev *led = dev_get_drvdata(dev); |
78 | struct gpio_trig_data *gpio_data = led->trigger_data; | 78 | struct gpio_trig_data *gpio_data = led->trigger_data; |
79 | unsigned desired_brightness; | 79 | unsigned desired_brightness; |
80 | int ret; | 80 | int ret; |
81 | 81 | ||
82 | ret = sscanf(buf, "%u", &desired_brightness); | 82 | ret = sscanf(buf, "%u", &desired_brightness); |
83 | if (ret < 1 || desired_brightness > 255) { | 83 | if (ret < 1 || desired_brightness > 255) { |
84 | dev_err(dev, "invalid value\n"); | 84 | dev_err(dev, "invalid value\n"); |
85 | return -EINVAL; | 85 | return -EINVAL; |
86 | } | 86 | } |
87 | 87 | ||
88 | gpio_data->desired_brightness = desired_brightness; | 88 | gpio_data->desired_brightness = desired_brightness; |
89 | 89 | ||
90 | return n; | 90 | return n; |
91 | } | 91 | } |
92 | static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show, | 92 | static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show, |
93 | gpio_trig_brightness_store); | 93 | gpio_trig_brightness_store); |
94 | 94 | ||
95 | static ssize_t gpio_trig_inverted_show(struct device *dev, | 95 | static ssize_t gpio_trig_inverted_show(struct device *dev, |
96 | struct device_attribute *attr, char *buf) | 96 | struct device_attribute *attr, char *buf) |
97 | { | 97 | { |
98 | struct led_classdev *led = dev_get_drvdata(dev); | 98 | struct led_classdev *led = dev_get_drvdata(dev); |
99 | struct gpio_trig_data *gpio_data = led->trigger_data; | 99 | struct gpio_trig_data *gpio_data = led->trigger_data; |
100 | 100 | ||
101 | return sprintf(buf, "%s\n", gpio_data->inverted ? "yes" : "no"); | 101 | return sprintf(buf, "%s\n", gpio_data->inverted ? "yes" : "no"); |
102 | } | 102 | } |
103 | 103 | ||
104 | static ssize_t gpio_trig_inverted_store(struct device *dev, | 104 | static ssize_t gpio_trig_inverted_store(struct device *dev, |
105 | struct device_attribute *attr, const char *buf, size_t n) | 105 | struct device_attribute *attr, const char *buf, size_t n) |
106 | { | 106 | { |
107 | struct led_classdev *led = dev_get_drvdata(dev); | 107 | struct led_classdev *led = dev_get_drvdata(dev); |
108 | struct gpio_trig_data *gpio_data = led->trigger_data; | 108 | struct gpio_trig_data *gpio_data = led->trigger_data; |
109 | unsigned inverted; | 109 | unsigned inverted; |
110 | int ret; | 110 | int ret; |
111 | 111 | ||
112 | ret = sscanf(buf, "%u", &inverted); | 112 | ret = sscanf(buf, "%u", &inverted); |
113 | if (ret < 1) { | 113 | if (ret < 1) { |
114 | dev_err(dev, "invalid value\n"); | 114 | dev_err(dev, "invalid value\n"); |
115 | return -EINVAL; | 115 | return -EINVAL; |
116 | } | 116 | } |
117 | 117 | ||
118 | gpio_data->inverted = !!inverted; | 118 | gpio_data->inverted = !!inverted; |
119 | 119 | ||
120 | /* After inverting, we need to update the LED. */ | 120 | /* After inverting, we need to update the LED. */ |
121 | schedule_work(&gpio_data->work); | 121 | schedule_work(&gpio_data->work); |
122 | 122 | ||
123 | return n; | 123 | return n; |
124 | } | 124 | } |
125 | static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show, | 125 | static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show, |
126 | gpio_trig_inverted_store); | 126 | gpio_trig_inverted_store); |
127 | 127 | ||
128 | static ssize_t gpio_trig_gpio_show(struct device *dev, | 128 | static ssize_t gpio_trig_gpio_show(struct device *dev, |
129 | struct device_attribute *attr, char *buf) | 129 | struct device_attribute *attr, char *buf) |
130 | { | 130 | { |
131 | struct led_classdev *led = dev_get_drvdata(dev); | 131 | struct led_classdev *led = dev_get_drvdata(dev); |
132 | struct gpio_trig_data *gpio_data = led->trigger_data; | 132 | struct gpio_trig_data *gpio_data = led->trigger_data; |
133 | 133 | ||
134 | return sprintf(buf, "%u\n", gpio_data->gpio); | 134 | return sprintf(buf, "%u\n", gpio_data->gpio); |
135 | } | 135 | } |
136 | 136 | ||
137 | static ssize_t gpio_trig_gpio_store(struct device *dev, | 137 | static ssize_t gpio_trig_gpio_store(struct device *dev, |
138 | struct device_attribute *attr, const char *buf, size_t n) | 138 | struct device_attribute *attr, const char *buf, size_t n) |
139 | { | 139 | { |
140 | struct led_classdev *led = dev_get_drvdata(dev); | 140 | struct led_classdev *led = dev_get_drvdata(dev); |
141 | struct gpio_trig_data *gpio_data = led->trigger_data; | 141 | struct gpio_trig_data *gpio_data = led->trigger_data; |
142 | unsigned gpio; | 142 | unsigned gpio; |
143 | int ret; | 143 | int ret; |
144 | 144 | ||
145 | ret = sscanf(buf, "%u", &gpio); | 145 | ret = sscanf(buf, "%u", &gpio); |
146 | if (ret < 1) { | 146 | if (ret < 1) { |
147 | dev_err(dev, "couldn't read gpio number\n"); | 147 | dev_err(dev, "couldn't read gpio number\n"); |
148 | flush_work(&gpio_data->work); | 148 | flush_work(&gpio_data->work); |
149 | return -EINVAL; | 149 | return -EINVAL; |
150 | } | 150 | } |
151 | 151 | ||
152 | if (gpio_data->gpio == gpio) | 152 | if (gpio_data->gpio == gpio) |
153 | return n; | 153 | return n; |
154 | 154 | ||
155 | if (!gpio) { | 155 | if (!gpio) { |
156 | if (gpio_data->gpio != 0) | 156 | if (gpio_data->gpio != 0) |
157 | free_irq(gpio_to_irq(gpio_data->gpio), led); | 157 | free_irq(gpio_to_irq(gpio_data->gpio), led); |
158 | gpio_data->gpio = 0; | 158 | gpio_data->gpio = 0; |
159 | return n; | 159 | return n; |
160 | } | 160 | } |
161 | 161 | ||
162 | ret = request_irq(gpio_to_irq(gpio), gpio_trig_irq, | 162 | ret = request_irq(gpio_to_irq(gpio), gpio_trig_irq, |
163 | IRQF_SHARED | IRQF_TRIGGER_RISING | 163 | IRQF_SHARED | IRQF_TRIGGER_RISING |
164 | | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led); | 164 | | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led); |
165 | if (ret) { | 165 | if (ret) { |
166 | dev_err(dev, "request_irq failed with error %d\n", ret); | 166 | dev_err(dev, "request_irq failed with error %d\n", ret); |
167 | } else { | 167 | } else { |
168 | if (gpio_data->gpio != 0) | 168 | if (gpio_data->gpio != 0) |
169 | free_irq(gpio_to_irq(gpio_data->gpio), led); | 169 | free_irq(gpio_to_irq(gpio_data->gpio), led); |
170 | gpio_data->gpio = gpio; | 170 | gpio_data->gpio = gpio; |
171 | } | 171 | } |
172 | 172 | ||
173 | return ret ? ret : n; | 173 | return ret ? ret : n; |
174 | } | 174 | } |
175 | static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store); | 175 | static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store); |
176 | 176 | ||
177 | static void gpio_trig_activate(struct led_classdev *led) | 177 | static void gpio_trig_activate(struct led_classdev *led) |
178 | { | 178 | { |
179 | struct gpio_trig_data *gpio_data; | 179 | struct gpio_trig_data *gpio_data; |
180 | int ret; | 180 | int ret; |
181 | 181 | ||
182 | gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL); | 182 | gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL); |
183 | if (!gpio_data) | 183 | if (!gpio_data) |
184 | return; | 184 | return; |
185 | 185 | ||
186 | ret = device_create_file(led->dev, &dev_attr_gpio); | 186 | ret = device_create_file(led->dev, &dev_attr_gpio); |
187 | if (ret) | 187 | if (ret) |
188 | goto err_gpio; | 188 | goto err_gpio; |
189 | 189 | ||
190 | ret = device_create_file(led->dev, &dev_attr_inverted); | 190 | ret = device_create_file(led->dev, &dev_attr_inverted); |
191 | if (ret) | 191 | if (ret) |
192 | goto err_inverted; | 192 | goto err_inverted; |
193 | 193 | ||
194 | ret = device_create_file(led->dev, &dev_attr_desired_brightness); | 194 | ret = device_create_file(led->dev, &dev_attr_desired_brightness); |
195 | if (ret) | 195 | if (ret) |
196 | goto err_brightness; | 196 | goto err_brightness; |
197 | 197 | ||
198 | gpio_data->led = led; | 198 | gpio_data->led = led; |
199 | led->trigger_data = gpio_data; | 199 | led->trigger_data = gpio_data; |
200 | INIT_WORK(&gpio_data->work, gpio_trig_work); | 200 | INIT_WORK(&gpio_data->work, gpio_trig_work); |
201 | 201 | ||
202 | return; | 202 | return; |
203 | 203 | ||
204 | err_brightness: | 204 | err_brightness: |
205 | device_remove_file(led->dev, &dev_attr_inverted); | 205 | device_remove_file(led->dev, &dev_attr_inverted); |
206 | 206 | ||
207 | err_inverted: | 207 | err_inverted: |
208 | device_remove_file(led->dev, &dev_attr_gpio); | 208 | device_remove_file(led->dev, &dev_attr_gpio); |
209 | 209 | ||
210 | err_gpio: | 210 | err_gpio: |
211 | kfree(gpio_data); | 211 | kfree(gpio_data); |
212 | } | 212 | } |
213 | 213 | ||
214 | static void gpio_trig_deactivate(struct led_classdev *led) | 214 | static void gpio_trig_deactivate(struct led_classdev *led) |
215 | { | 215 | { |
216 | struct gpio_trig_data *gpio_data = led->trigger_data; | 216 | struct gpio_trig_data *gpio_data = led->trigger_data; |
217 | 217 | ||
218 | if (gpio_data) { | 218 | if (gpio_data) { |
219 | device_remove_file(led->dev, &dev_attr_gpio); | 219 | device_remove_file(led->dev, &dev_attr_gpio); |
220 | device_remove_file(led->dev, &dev_attr_inverted); | 220 | device_remove_file(led->dev, &dev_attr_inverted); |
221 | device_remove_file(led->dev, &dev_attr_desired_brightness); | 221 | device_remove_file(led->dev, &dev_attr_desired_brightness); |
222 | flush_work(&gpio_data->work); | 222 | flush_work(&gpio_data->work); |
223 | if (gpio_data->gpio != 0) | 223 | if (gpio_data->gpio != 0) |
224 | free_irq(gpio_to_irq(gpio_data->gpio), led); | 224 | free_irq(gpio_to_irq(gpio_data->gpio), led); |
225 | kfree(gpio_data); | 225 | kfree(gpio_data); |
226 | } | 226 | } |
227 | } | 227 | } |
228 | 228 | ||
229 | static struct led_trigger gpio_led_trigger = { | 229 | static struct led_trigger gpio_led_trigger = { |
230 | .name = "gpio", | 230 | .name = "gpio", |
231 | .activate = gpio_trig_activate, | 231 | .activate = gpio_trig_activate, |
232 | .deactivate = gpio_trig_deactivate, | 232 | .deactivate = gpio_trig_deactivate, |
233 | }; | 233 | }; |
234 | 234 | ||
235 | static int __init gpio_trig_init(void) | 235 | static int __init gpio_trig_init(void) |
236 | { | 236 | { |
237 | return led_trigger_register(&gpio_led_trigger); | 237 | return led_trigger_register(&gpio_led_trigger); |
238 | } | 238 | } |
239 | module_init(gpio_trig_init); | 239 | module_init(gpio_trig_init); |
240 | 240 | ||
241 | static void __exit gpio_trig_exit(void) | 241 | static void __exit gpio_trig_exit(void) |
242 | { | 242 | { |
243 | led_trigger_unregister(&gpio_led_trigger); | 243 | led_trigger_unregister(&gpio_led_trigger); |
244 | } | 244 | } |
245 | module_exit(gpio_trig_exit); | 245 | module_exit(gpio_trig_exit); |
246 | 246 | ||
247 | MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>"); | 247 | MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>"); |
248 | MODULE_DESCRIPTION("GPIO LED trigger"); | 248 | MODULE_DESCRIPTION("GPIO LED trigger"); |
249 | MODULE_LICENSE("GPL"); | 249 | MODULE_LICENSE("GPL"); |
250 | 250 |