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