strip the kernel version suffix from target directories, except for brcm-2.4 (the...
[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 OpenWrt.org
7  *  Copyright (C) 2007 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/gpio_leds.h>
33
34 #include <asm/bootinfo.h>
35 #include <asm/io.h>
36 #include <asm/gpio.h>
37
38 #include <asm/mach-adm5120/adm5120_info.h>
39
40 #define NUM_LEDS_MAX    23
41
42 #define ADM5120_GPIO_xxxx       0x100   /* an unknown pin */
43
44 struct mach_data {
45         unsigned long machtype;
46         unsigned count;
47         struct gpio_led_platform_data *data;
48 };
49
50 struct adm5120_leddev {
51         struct platform_device pdev;
52         struct gpio_led_platform_data pdata;
53 };
54
55 static int led_count = 0;
56 static struct adm5120_leddev *led_devs[NUM_LEDS_MAX];
57
58 #define LED_ARRAY(n)                            \
59 static struct gpio_led_platform_data            \
60 n ## _leds [] __initdata =
61
62 #define LED_DATA(n,t,g,off,on) {                \
63         .name = (n),                            \
64         .trigger = (t),                         \
65         .gpio = (g),                            \
66         .value_off = (off),                     \
67         .value_on = (on)                        \
68         }
69
70 #define LED_STD(g,n,t)  LED_DATA((n),(t),(g), 0, 1)
71 #define LED_INV(g,n,t)  LED_DATA((n),(t),(g), 1, 0)
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 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
93 LED_ARRAY(np28g) { /* FIXME: untested */
94         LED_STD(ADM5120_GPIO_xxxx, "lan1",      NULL ),
95         LED_STD(ADM5120_GPIO_xxxx, "lan2",      NULL ),
96         LED_STD(ADM5120_GPIO_xxxx, "lan3",      NULL ),
97         LED_STD(ADM5120_GPIO_xxxx, "wan",       NULL ),
98         LED_STD(ADM5120_GPIO_xxxx, "wlan",      NULL ),
99         LED_STD(ADM5120_GPIO_xxxx, "usb1",      NULL ),
100         LED_STD(ADM5120_GPIO_xxxx, "usb2",      NULL ),
101         LED_STD(ADM5120_GPIO_xxxx, "usb3",      NULL ),
102         LED_STD(ADM5120_GPIO_xxxx, "usb4",      NULL ),
103         LED_INV(ADM5120_GPIO_PIN2, "power",     NULL ),
104         LED_STD(ADM5120_GPIO_xxxx, "diag",      NULL ),
105 };
106 #endif
107
108 LED_ARRAY(wp54g) {
109         LED_INV(ADM5120_GPIO_PIN2, "diag",      NULL ),
110         LED_INV(ADM5120_GPIO_PIN6, "wlan",      NULL ),
111         LED_INV(ADM5120_GPIO_PIN7, "wan",       NULL ),
112         LED_INV(ADM5120_GPIO_P0L0, "lan1",      NULL ),
113         LED_INV(ADM5120_GPIO_P1L0, "lan2",      NULL ),
114 };
115
116 /*
117  * Edimax boards
118  */
119 LED_ARRAY(br6104k) {
120         LED_STD(ADM5120_GPIO_PIN0, "power",             NULL ),
121         LED_INV(ADM5120_GPIO_P0L1, "wan_speed",         NULL ),
122         LED_INV(ADM5120_GPIO_P0L0, "wan_lnkact",        NULL ),
123         LED_INV(ADM5120_GPIO_P1L1, "lan1_speed",        NULL ),
124         LED_INV(ADM5120_GPIO_P1L0, "lan1_lnkact",       NULL ),
125         LED_INV(ADM5120_GPIO_P2L1, "lan2_speed",        NULL ),
126         LED_INV(ADM5120_GPIO_P2L0, "lan2_lnkact",       NULL ),
127         LED_INV(ADM5120_GPIO_P3L1, "lan3_speed",        NULL ),
128         LED_INV(ADM5120_GPIO_P3L0, "lan3_lnkact",       NULL ),
129         LED_INV(ADM5120_GPIO_P4L1, "lan4_speed",        NULL ),
130         LED_INV(ADM5120_GPIO_P4L0, "lan4_lnkact",       NULL ),
131 };
132
133 /*
134  * Mikrotik boards
135  */
136 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
137 LED_ARRAY(rb100) { /* FIXME: untested */
138         LED_STD(ADM5120_GPIO_PIN6, "power",     NULL ),
139         LED_STD(ADM5120_GPIO_PIN3, "user",      NULL ),
140 };
141 #endif
142
143 LED_ARRAY(rb133) {
144         LED_STD(ADM5120_GPIO_PIN6, "power",     NULL ),
145         LED_STD(ADM5120_GPIO_PIN5, "user",      NULL ),
146 };
147
148 /*
149  * ZyXEL boards
150  */
151 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
152 LED_ARRAY(p334) { /* FIXME: untested */
153         LED_INV(ADM5120_GPIO_xxxx, "power",     NULL ),
154         LED_INV(ADM5120_GPIO_xxxx, "lan1",      NULL ),
155         LED_INV(ADM5120_GPIO_xxxx, "lan2",      NULL ),
156         LED_INV(ADM5120_GPIO_xxxx, "lan3",      NULL ),
157         LED_INV(ADM5120_GPIO_xxxx, "lan4",      NULL ),
158         LED_INV(ADM5120_GPIO_xxxx, "wan",       NULL ),
159 };
160 #endif
161
162 LED_ARRAY(p334wt) {
163         LED_INV(ADM5120_GPIO_PIN2, "power",     NULL ),
164         LED_INV(ADM5120_GPIO_P3L0, "lan1",      NULL ),
165         LED_INV(ADM5120_GPIO_P2L0, "lan2",      NULL ),
166         LED_INV(ADM5120_GPIO_P1L0, "lan3",      NULL ),
167         LED_INV(ADM5120_GPIO_P0L0, "lan4",      NULL ),
168         LED_INV(ADM5120_GPIO_P4L0, "wan",       NULL ),
169         LED_INV(ADM5120_GPIO_P4L2, "wlan",      NULL ),
170         LED_INV(ADM5120_GPIO_P2L2, "otist",     NULL ),
171         LED_INV(ADM5120_GPIO_P1L2, "hidden",    NULL ),
172 };
173
174 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
175 LED_ARRAY(p335) { /* FIXME: untested */
176         LED_INV(ADM5120_GPIO_PIN2, "power",     NULL ),
177         LED_INV(ADM5120_GPIO_P3L0, "lan1",      NULL ),
178         LED_INV(ADM5120_GPIO_P2L0, "lan2",      NULL ),
179         LED_INV(ADM5120_GPIO_P1L0, "lan3",      NULL ),
180         LED_INV(ADM5120_GPIO_P0L0, "lan4",      NULL ),
181         LED_INV(ADM5120_GPIO_P4L0, "wan",       NULL ),
182         LED_INV(ADM5120_GPIO_P4L2, "wlan",      NULL ),
183         LED_INV(ADM5120_GPIO_P2L2, "otist",     NULL ),
184         LED_INV(ADM5120_GPIO_xxxx, "usb",       NULL ),
185 };
186 #endif
187
188 /*
189  * Generic board
190  */
191 LED_ARRAY(generic) {
192 #if defined(CONFIG_LEDS_ADM5120_DIAG)
193         LED_STD(ADM5120_GPIO_PIN0, "gpio0",     NULL ),
194         LED_STD(ADM5120_GPIO_PIN1, "gpio1",     NULL ),
195         LED_STD(ADM5120_GPIO_PIN2, "gpio2",     NULL ),
196         LED_STD(ADM5120_GPIO_PIN3, "gpio3",     NULL ),
197         LED_STD(ADM5120_GPIO_PIN4, "gpio4",     NULL ),
198         LED_STD(ADM5120_GPIO_PIN5, "gpio5",     NULL ),
199         LED_STD(ADM5120_GPIO_PIN6, "gpio6",     NULL ),
200         LED_STD(ADM5120_GPIO_PIN7, "gpio7",     NULL ),
201         LED_STD(ADM5120_GPIO_P0L0, "port0led0", NULL ),
202         LED_STD(ADM5120_GPIO_P0L1, "port0led1", NULL ),
203         LED_STD(ADM5120_GPIO_P0L2, "port0led2", NULL ),
204         LED_STD(ADM5120_GPIO_P1L0, "port1led0", NULL ),
205         LED_STD(ADM5120_GPIO_P1L1, "port1led1", NULL ),
206         LED_STD(ADM5120_GPIO_P1L2, "port1led2", NULL ),
207         LED_STD(ADM5120_GPIO_P2L0, "port2led0", NULL ),
208         LED_STD(ADM5120_GPIO_P2L1, "port2led1", NULL ),
209         LED_STD(ADM5120_GPIO_P2L2, "port2led2", NULL ),
210         LED_STD(ADM5120_GPIO_P3L0, "port3led0", NULL ),
211         LED_STD(ADM5120_GPIO_P3L1, "port3led1", NULL ),
212         LED_STD(ADM5120_GPIO_P3L2, "port3led2", NULL ),
213         LED_STD(ADM5120_GPIO_P4L0, "port4led0", NULL ),
214         LED_STD(ADM5120_GPIO_P4L1, "port4led1", NULL ),
215         LED_STD(ADM5120_GPIO_P4L2, "port4led2", NULL ),
216 #endif
217 };
218
219 #define MACH_DATA(m, n) {                               \
220         .machtype       = (m),                          \
221         .count          = ARRAY_SIZE(n ## _leds),       \
222         .data           = n ## _leds                    \
223 }
224
225 static struct mach_data machines[] __initdata = {
226         MACH_DATA(MACH_ADM5120_GENERIC, generic),
227         /* Compex */
228         MACH_DATA(MACH_ADM5120_WP54AG,  wp54g),
229         MACH_DATA(MACH_ADM5120_WP54G,   wp54g),
230         MACH_DATA(MACH_ADM5120_WP54G_WRT, wp54g),
231         MACH_DATA(MACH_ADM5120_WPP54AG, wp54g),
232         MACH_DATA(MACH_ADM5120_WPP54G,  wp54g),
233         /* Edimax */
234         MACH_DATA(MACH_ADM5120_BR6104K, br6104k),
235         /* Mikrotik */
236         MACH_DATA(MACH_ADM5120_RB_133,  rb133),
237         MACH_DATA(MACH_ADM5120_RB_133C, rb133),
238         /* ZyXEL */
239         MACH_DATA(MACH_ADM5120_P334WT,  p334wt),
240 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
241         /* untested */
242         MACH_DATA(MACH_ADM5120_P334,    p334),
243         MACH_DATA(MACH_ADM5120_P335,    p335),
244         MACH_DATA(MACH_ADM5120_RB_111,  rb100),
245         MACH_DATA(MACH_ADM5120_RB_112,  rb100),
246         MACH_DATA(MACH_ADM5120_NP27G,   np27g),
247         MACH_DATA(MACH_ADM5120_NP28G,   np28g),
248         MACH_DATA(MACH_ADM5120_NP28GHS, np28g),
249 #endif
250 };
251
252 static struct adm5120_leddev * __init
253 create_leddev(struct gpio_led_platform_data *data)
254 {
255         struct adm5120_leddev *p;
256
257         p = kzalloc(sizeof(*p), GFP_KERNEL);
258         if (p == NULL)
259                 return NULL;
260
261         memcpy(&p->pdata, data, sizeof(p->pdata));
262         p->pdev.dev.platform_data = &p->pdata;
263
264         return p;
265 }
266
267 static void
268 destroy_leddev(struct adm5120_leddev *led)
269 {
270         if (led)
271                 kfree(led);
272 }
273
274 static struct mach_data * __init
275 adm5120_leds_findmach(unsigned long machtype)
276 {
277         struct mach_data *mach;
278         int i;
279
280         mach = NULL;
281         for (i=0; i<ARRAY_SIZE(machines); i++) {
282                 if (machines[i].machtype == machtype) {
283                         mach = &machines[i];
284                         break;
285                 }
286         };
287
288 #if defined(CONFIG_LEDS_ADM5120_DIAG)
289         if (mach == NULL)
290                 mach = machines;
291 #endif
292
293         return mach;
294 }
295
296 static int __init
297 adm5120_leds_init(void)
298 {
299         struct mach_data *mach;
300         int i, ret;
301
302         if (mips_machgroup != MACH_GROUP_ADM5120) {
303                 ret = -EINVAL;
304                 goto err;
305         }
306
307         mach = adm5120_leds_findmach(mips_machtype);
308         if (mach == NULL) {
309                 /* the board is not yet supported */
310                 ret = -EINVAL;
311                 goto err;
312         }
313
314         for (i=0; i < mach->count; i++) {
315                 led_devs[i] = create_leddev(&mach->data[i]);
316                 if (led_devs[i] == NULL) {
317                         ret = -ENOMEM;
318                         goto err_destroy;
319                 }
320                 led_devs[i]->pdev.name = "gpio-led";
321                 led_devs[i]->pdev.id = i;
322         }
323
324         for (i=0; i < mach->count; i++) {
325                 ret = platform_device_register(&led_devs[i]->pdev);
326                 if (ret)
327                         goto err_unregister;
328         }
329
330         led_count = mach->count;
331         return 0;
332
333 err_unregister:
334         for (i--; i>=0; i--)
335                 platform_device_unregister(&led_devs[i]->pdev);
336
337 err_destroy:
338         for (i=0; i<led_count; i++)
339                 destroy_leddev(led_devs[i]);
340 err:
341         return ret;
342 }
343
344 static void __exit
345 adm5120_leds_exit(void)
346 {
347         int i;
348
349         for (i=0; i < led_count; i++) {
350                 platform_device_unregister(&led_devs[i]->pdev);
351                 destroy_leddev(led_devs[i]);
352         }
353 }
354
355 module_init(adm5120_leds_init);
356 module_exit(adm5120_leds_exit);
357
358 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
359 MODULE_DESCRIPTION(DRV_DESC);
360 MODULE_LICENSE("GPL");
361