9bd3af6e8d8a3d03c397c4b11cd9a66bbadb6cab
[openwrt/svn-archive/archive.git] / target / linux / adm5120 / files / drivers / leds / leds-adm5120.c
1 /*
2 * $Id$
3 *
4 * ADM5120 GPIO LED devices
5 *
6 * Copyright (C) 2007,2008 OpenWrt.org
7 * Copyright (C) 2007,2008 Gabor Juhos <juhosg at openwrt.org>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 *
24 */
25
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/platform_device.h>
29 #include <linux/leds.h>
30 #include <linux/err.h>
31
32 #include <linux/io.h>
33 #include <linux/leds.h>
34
35 #include <asm/bootinfo.h>
36 #include <asm/gpio.h>
37
38 #include <adm5120_info.h>
39
40 #define NUM_LEDS_MAX 23
41 #define DRV_DESC "LED driver for the ADM5120 based boards"
42
43 #define ADM5120_GPIO_xxxx 0x100 /* an unknown pin */
44
45 struct mach_data {
46 unsigned long machtype;
47 unsigned nr_leds;
48 struct gpio_led *leds;
49 };
50
51 struct adm5120_leddev {
52 struct platform_device pdev;
53 struct gpio_led led;
54 struct gpio_led_platform_data pdata;
55 };
56
57 static int led_count;
58 static struct adm5120_leddev *led_devs[NUM_LEDS_MAX];
59
60 #define LED_ARRAY(n) \
61 static struct gpio_led n ## _leds [] __initdata =
62
63 #define LED_DATA(n, t, g, a) { \
64 .name = (n), \
65 .default_trigger = (t), \
66 .gpio = (g), \
67 .active_low = (a) \
68 }
69
70 #define LED_STD(g, n, t) LED_DATA((n), (t), (g), 0)
71 #define LED_INV(g, n, t) LED_DATA((n), (t), (g), 1)
72
73 /*
74 * Compex boards
75 */
76 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
77 LED_ARRAY(np27g) { /* FIXME: untested */
78 LED_STD(ADM5120_GPIO_xxxx, "lan1", NULL),
79 LED_STD(ADM5120_GPIO_xxxx, "lan2", NULL),
80 LED_STD(ADM5120_GPIO_xxxx, "lan3", NULL),
81 LED_STD(ADM5120_GPIO_xxxx, "lan4", NULL),
82 LED_STD(ADM5120_GPIO_xxxx, "wan_cond", NULL),
83 LED_STD(ADM5120_GPIO_xxxx, "wlan", NULL),
84 LED_STD(ADM5120_GPIO_xxxx, "wan_act", NULL),
85 LED_STD(ADM5120_GPIO_xxxx, "usb1", NULL),
86 LED_STD(ADM5120_GPIO_xxxx, "usb2", NULL),
87 LED_INV(ADM5120_GPIO_PIN2, "power", NULL),
88 LED_STD(ADM5120_GPIO_xxxx, "diag", NULL),
89 };
90 #endif
91
92 LED_ARRAY(np28g) {
93 LED_INV(ADM5120_GPIO_PIN2, "diag", NULL),
94 LED_INV(ADM5120_GPIO_PIN3, "power", NULL),
95 LED_INV(ADM5120_GPIO_PIN6, "wan_cond", NULL),
96 LED_INV(ADM5120_GPIO_PIN7, "wifi", NULL),
97 LED_INV(ADM5120_GPIO_P0L2, "usb1", NULL),
98 LED_INV(ADM5120_GPIO_P1L0, "lan1", NULL),
99 LED_INV(ADM5120_GPIO_P1L2, "usb2", NULL),
100 LED_INV(ADM5120_GPIO_P2L0, "lan2", NULL),
101 LED_INV(ADM5120_GPIO_P2L2, "usb3", NULL),
102 LED_INV(ADM5120_GPIO_P3L0, "lan3", NULL),
103 LED_INV(ADM5120_GPIO_P3L2, "usb4", NULL),
104 LED_INV(ADM5120_GPIO_P4L0, "wan", NULL),
105 };
106
107 LED_ARRAY(wp54g) {
108 LED_INV(ADM5120_GPIO_PIN2, "diag", NULL),
109 LED_INV(ADM5120_GPIO_PIN6, "wlan", NULL),
110 LED_INV(ADM5120_GPIO_PIN7, "wan", NULL),
111 LED_INV(ADM5120_GPIO_P0L0, "lan1", NULL),
112 LED_INV(ADM5120_GPIO_P1L0, "lan2", NULL),
113 };
114
115 /*
116 * Edimax boards
117 */
118 LED_ARRAY(br6104k) {
119 LED_STD(ADM5120_GPIO_PIN0, "power", NULL),
120 LED_INV(ADM5120_GPIO_P0L1, "wan_speed", NULL),
121 LED_INV(ADM5120_GPIO_P0L0, "wan_lnkact", NULL),
122 LED_INV(ADM5120_GPIO_P1L1, "lan1_speed", NULL),
123 LED_INV(ADM5120_GPIO_P1L0, "lan1_lnkact", NULL),
124 LED_INV(ADM5120_GPIO_P2L1, "lan2_speed", NULL),
125 LED_INV(ADM5120_GPIO_P2L0, "lan2_lnkact", NULL),
126 LED_INV(ADM5120_GPIO_P3L1, "lan3_speed", NULL),
127 LED_INV(ADM5120_GPIO_P3L0, "lan3_lnkact", NULL),
128 LED_INV(ADM5120_GPIO_P4L1, "lan4_speed", NULL),
129 LED_INV(ADM5120_GPIO_P4L0, "lan4_lnkact", NULL),
130 };
131
132 LED_ARRAY(br61x4wg) {
133 LED_STD(ADM5120_GPIO_PIN0, "power", NULL),
134 LED_STD(ADM5120_GPIO_PIN5, "wlan", NULL),
135 LED_INV(ADM5120_GPIO_P0L1, "wan_speed", NULL),
136 LED_INV(ADM5120_GPIO_P0L0, "wan_lnkact", NULL),
137 LED_INV(ADM5120_GPIO_P1L1, "lan1_speed", NULL),
138 LED_INV(ADM5120_GPIO_P1L0, "lan1_lnkact", NULL),
139 LED_INV(ADM5120_GPIO_P2L1, "lan2_speed", NULL),
140 LED_INV(ADM5120_GPIO_P2L0, "lan2_lnkact", NULL),
141 LED_INV(ADM5120_GPIO_P3L1, "lan3_speed", NULL),
142 LED_INV(ADM5120_GPIO_P3L0, "lan3_lnkact", NULL),
143 LED_INV(ADM5120_GPIO_P4L1, "lan4_speed", NULL),
144 LED_INV(ADM5120_GPIO_P4L0, "lan4_lnkact", NULL),
145 };
146
147 /*
148 * Mikrotik boards
149 */
150 LED_ARRAY(rb100) {
151 LED_STD(ADM5120_GPIO_PIN3, "user", NULL),
152 LED_INV(ADM5120_GPIO_P0L1, "lan_speed", NULL),
153 LED_INV(ADM5120_GPIO_P0L0, "lan_lnkact", NULL),
154 };
155
156 LED_ARRAY(rb133) {
157 LED_STD(ADM5120_GPIO_PIN6, "power", NULL),
158 LED_STD(ADM5120_GPIO_PIN5, "user", NULL),
159 LED_INV(ADM5120_GPIO_P2L1, "lan1_speed", NULL), /* untested */
160 LED_INV(ADM5120_GPIO_P2L0, "lan1_lnkact", NULL), /* untested */
161 };
162
163 LED_ARRAY(rb133c) {
164 LED_STD(ADM5120_GPIO_PIN6, "power", NULL),
165 LED_STD(ADM5120_GPIO_PIN5, "user", NULL),
166 LED_INV(ADM5120_GPIO_P2L1, "lan1_speed", NULL), /* untested */
167 LED_INV(ADM5120_GPIO_P2L0, "lan1_lnkact", NULL), /* untested */
168 LED_INV(ADM5120_GPIO_P1L1, "lan2_speed", NULL), /* untested */
169 LED_INV(ADM5120_GPIO_P1L0, "lan2_lnkact", NULL), /* untested */
170 LED_INV(ADM5120_GPIO_P0L1, "lan3_speed", NULL), /* untested */
171 LED_INV(ADM5120_GPIO_P0L0, "lan3_lnkact", NULL), /* untested */
172 };
173
174 LED_ARRAY(rb153) {
175 LED_STD(ADM5120_GPIO_PIN5, "user", NULL),
176 LED_INV(ADM5120_GPIO_P0L1, "lan1_speed", NULL),
177 LED_INV(ADM5120_GPIO_P0L0, "lan1_lnkact", NULL),
178 LED_INV(ADM5120_GPIO_P1L1, "lan5_speed", NULL),
179 LED_INV(ADM5120_GPIO_P1L0, "lan5_lnkact", NULL),
180 LED_INV(ADM5120_GPIO_P2L1, "lan4_speed", NULL),
181 LED_INV(ADM5120_GPIO_P2L0, "lan4_lnkact", NULL),
182 LED_INV(ADM5120_GPIO_P3L1, "lan3_speed", NULL),
183 LED_INV(ADM5120_GPIO_P3L0, "lan3_lnkact", NULL),
184 LED_INV(ADM5120_GPIO_P4L1, "lan2_speed", NULL),
185 LED_INV(ADM5120_GPIO_P4L0, "lan2_lnkact", NULL),
186 };
187
188 /*
189 * ZyXEL boards
190 */
191 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
192 LED_ARRAY(p334) { /* FIXME: untested */
193 LED_INV(ADM5120_GPIO_xxxx, "power", NULL),
194 LED_INV(ADM5120_GPIO_xxxx, "lan1", NULL),
195 LED_INV(ADM5120_GPIO_xxxx, "lan2", NULL),
196 LED_INV(ADM5120_GPIO_xxxx, "lan3", NULL),
197 LED_INV(ADM5120_GPIO_xxxx, "lan4", NULL),
198 LED_INV(ADM5120_GPIO_xxxx, "wan", NULL),
199 };
200 #endif
201
202 LED_ARRAY(p334wt) {
203 LED_INV(ADM5120_GPIO_PIN2, "power", NULL),
204 LED_INV(ADM5120_GPIO_P3L0, "lan1", NULL),
205 LED_INV(ADM5120_GPIO_P2L0, "lan2", NULL),
206 LED_INV(ADM5120_GPIO_P1L0, "lan3", NULL),
207 LED_INV(ADM5120_GPIO_P0L0, "lan4", NULL),
208 LED_INV(ADM5120_GPIO_P4L0, "wan", NULL),
209 LED_INV(ADM5120_GPIO_P4L2, "wlan", NULL),
210 LED_INV(ADM5120_GPIO_P2L2, "otist", NULL),
211 LED_INV(ADM5120_GPIO_P1L2, "hidden", NULL),
212 };
213
214 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
215 LED_ARRAY(p335) { /* FIXME: untested */
216 LED_INV(ADM5120_GPIO_PIN2, "power", NULL),
217 LED_INV(ADM5120_GPIO_P3L0, "lan1", NULL),
218 LED_INV(ADM5120_GPIO_P2L0, "lan2", NULL),
219 LED_INV(ADM5120_GPIO_P1L0, "lan3", NULL),
220 LED_INV(ADM5120_GPIO_P0L0, "lan4", NULL),
221 LED_INV(ADM5120_GPIO_P4L0, "wan", NULL),
222 LED_INV(ADM5120_GPIO_P4L2, "wlan", NULL),
223 LED_INV(ADM5120_GPIO_P2L2, "otist", NULL),
224 LED_INV(ADM5120_GPIO_xxxx, "usb", NULL),
225 };
226 #endif
227
228 /*
229 * Generic board
230 */
231 LED_ARRAY(generic) {
232 #if defined(CONFIG_LEDS_ADM5120_DIAG)
233 LED_STD(ADM5120_GPIO_PIN0, "gpio0", NULL),
234 LED_STD(ADM5120_GPIO_PIN1, "gpio1", NULL),
235 LED_STD(ADM5120_GPIO_PIN2, "gpio2", NULL),
236 LED_STD(ADM5120_GPIO_PIN3, "gpio3", NULL),
237 LED_STD(ADM5120_GPIO_PIN4, "gpio4", NULL),
238 LED_STD(ADM5120_GPIO_PIN5, "gpio5", NULL),
239 LED_STD(ADM5120_GPIO_PIN6, "gpio6", NULL),
240 LED_STD(ADM5120_GPIO_PIN7, "gpio7", NULL),
241 LED_STD(ADM5120_GPIO_P0L0, "port0led0", NULL),
242 LED_STD(ADM5120_GPIO_P0L1, "port0led1", NULL),
243 LED_STD(ADM5120_GPIO_P0L2, "port0led2", NULL),
244 LED_STD(ADM5120_GPIO_P1L0, "port1led0", NULL),
245 LED_STD(ADM5120_GPIO_P1L1, "port1led1", NULL),
246 LED_STD(ADM5120_GPIO_P1L2, "port1led2", NULL),
247 LED_STD(ADM5120_GPIO_P2L0, "port2led0", NULL),
248 LED_STD(ADM5120_GPIO_P2L1, "port2led1", NULL),
249 LED_STD(ADM5120_GPIO_P2L2, "port2led2", NULL),
250 LED_STD(ADM5120_GPIO_P3L0, "port3led0", NULL),
251 LED_STD(ADM5120_GPIO_P3L1, "port3led1", NULL),
252 LED_STD(ADM5120_GPIO_P3L2, "port3led2", NULL),
253 LED_STD(ADM5120_GPIO_P4L0, "port4led0", NULL),
254 LED_STD(ADM5120_GPIO_P4L1, "port4led1", NULL),
255 LED_STD(ADM5120_GPIO_P4L2, "port4led2", NULL),
256 #endif
257 };
258
259 #define MACH_DATA(m, n) { \
260 .machtype = (m), \
261 .nr_leds = ARRAY_SIZE(n ## _leds), \
262 .leds = n ## _leds \
263 }
264
265 static struct mach_data machines[] __initdata = {
266 MACH_DATA(MACH_ADM5120_GENERIC, generic),
267 /* Compex */
268 MACH_DATA(MACH_ADM5120_NP28G, np28g),
269 MACH_DATA(MACH_ADM5120_NP28GHS, np28g),
270 MACH_DATA(MACH_ADM5120_WP54AG, wp54g),
271 MACH_DATA(MACH_ADM5120_WP54G, wp54g),
272 MACH_DATA(MACH_ADM5120_WP54G_WRT, wp54g),
273 MACH_DATA(MACH_ADM5120_WPP54AG, wp54g),
274 MACH_DATA(MACH_ADM5120_WPP54G, wp54g),
275 /* Edimax */
276 MACH_DATA(MACH_ADM5120_BR6104K, br6104k),
277 MACH_DATA(MACH_ADM5120_BR61x4WG, br61x4wg),
278 /* Mikrotik */
279 MACH_DATA(MACH_ADM5120_RB_111, rb100),
280 MACH_DATA(MACH_ADM5120_RB_112, rb100),
281 MACH_DATA(MACH_ADM5120_RB_133, rb133),
282 MACH_DATA(MACH_ADM5120_RB_133C, rb133c),
283 MACH_DATA(MACH_ADM5120_RB_153, rb153),
284 /* ZyXEL */
285 MACH_DATA(MACH_ADM5120_P334WT, p334wt),
286 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
287 /* untested */
288 MACH_DATA(MACH_ADM5120_P334, p334),
289 MACH_DATA(MACH_ADM5120_P335, p335),
290 MACH_DATA(MACH_ADM5120_NP27G, np27g),
291 #endif
292 };
293
294 static struct adm5120_leddev * __init
295 create_leddev(int id, struct gpio_led *led)
296 {
297 struct adm5120_leddev *p;
298
299 p = kzalloc(sizeof(*p), GFP_KERNEL);
300 if (p == NULL)
301 return NULL;
302
303 memcpy(&p->led, led, sizeof(p->led));
304 p->pdev.name = "leds-gpio";
305 p->pdev.id = id;
306 p->pdev.dev.platform_data = &p->pdata;
307 p->pdata.num_leds = 1;
308 p->pdata.leds = &p->led;
309
310 return p;
311 }
312
313 static struct mach_data * __init
314 adm5120_leds_findmach(unsigned long machtype)
315 {
316 struct mach_data *mach;
317 int i;
318
319 mach = NULL;
320 for (i = 0; i < ARRAY_SIZE(machines); i++) {
321 if (machines[i].machtype == machtype) {
322 mach = &machines[i];
323 break;
324 }
325 };
326
327 #if defined(CONFIG_LEDS_ADM5120_DIAG)
328 if (mach == NULL)
329 mach = machines;
330 #endif
331
332 return mach;
333 }
334
335 static int __init
336 adm5120_leds_init(void)
337 {
338 struct mach_data *mach;
339 int i, ret;
340
341 if (mips_machgroup != MACH_GROUP_ADM5120) {
342 ret = -EINVAL;
343 goto err;
344 }
345
346 mach = adm5120_leds_findmach(mips_machtype);
347 if (mach == NULL) {
348 printk(KERN_ERR "leds-adm5120: unsupported board\n");
349 ret = -EINVAL;
350 goto err;
351 }
352
353 for (i = 0; i < mach->nr_leds; i++) {
354 led_devs[i] = create_leddev(i, &mach->leds[i]);
355 if (led_devs[i] == NULL) {
356 ret = -ENOMEM;
357 goto err_destroy;
358 }
359 }
360
361 for (i = 0; i < mach->nr_leds; i++) {
362 ret = platform_device_register(&led_devs[i]->pdev);
363 if (ret)
364 goto err_unregister;
365 }
366
367 led_count = mach->nr_leds;
368 return 0;
369
370 err_unregister:
371 for (i--; i >= 0; i--)
372 platform_device_unregister(&led_devs[i]->pdev);
373
374 err_destroy:
375 for (i = 0; i < led_count; i++)
376 kfree(led_devs[i]);
377 err:
378 return ret;
379 }
380
381 static void __exit
382 adm5120_leds_exit(void)
383 {
384 int i;
385
386 for (i = 0; i < led_count; i++) {
387 platform_device_unregister(&led_devs[i]->pdev);
388 kfree(led_devs[i]);
389 }
390 }
391
392 module_init(adm5120_leds_init);
393 module_exit(adm5120_leds_exit);
394
395 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
396 MODULE_DESCRIPTION(DRV_DESC);
397 MODULE_LICENSE("GPL");
398