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