add support for special LED brightness values
[lede.git] / target / linux / adm5120-2.6 / 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@freemail.hu>
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  * ZyXEL boards
75  */
76 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
77 LED_ARRAY(p334) { /* FIXME: untested */
78         LED_INV(ADM5120_GPIO_xxxx, "power",     NULL ),
79         LED_INV(ADM5120_GPIO_xxxx, "lan1",      NULL ),
80         LED_INV(ADM5120_GPIO_xxxx, "lan2",      NULL ),
81         LED_INV(ADM5120_GPIO_xxxx, "lan3",      NULL ),
82         LED_INV(ADM5120_GPIO_xxxx, "lan4",      NULL ),
83         LED_INV(ADM5120_GPIO_xxxx, "wan",       NULL ),
84 };
85 #endif
86
87 LED_ARRAY(p334wt) {
88         LED_INV(ADM5120_GPIO_PIN2, "power",     NULL ),
89         LED_INV(ADM5120_GPIO_P3L0, "lan1",      NULL ),
90         LED_INV(ADM5120_GPIO_P2L0, "lan2",      NULL ),
91         LED_INV(ADM5120_GPIO_P1L0, "lan3",      NULL ),
92         LED_INV(ADM5120_GPIO_P0L0, "lan4",      NULL ),
93         LED_INV(ADM5120_GPIO_P4L0, "wan",       NULL ),
94         LED_INV(ADM5120_GPIO_P4L2, "wlan",      NULL ),
95         LED_INV(ADM5120_GPIO_P2L2, "otist",     NULL ),
96         LED_INV(ADM5120_GPIO_P1L2, "hidden",    NULL ),
97 };
98
99 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
100 LED_ARRAY(p335) { /* FIXME: untested */
101         LED_INV(ADM5120_GPIO_PIN2, "power",     NULL ),
102         LED_INV(ADM5120_GPIO_P3L0, "lan1",      NULL ),
103         LED_INV(ADM5120_GPIO_P2L0, "lan2",      NULL ),
104         LED_INV(ADM5120_GPIO_P1L0, "lan3",      NULL ),
105         LED_INV(ADM5120_GPIO_P0L0, "lan4",      NULL ),
106         LED_INV(ADM5120_GPIO_P4L0, "wan",       NULL ),
107         LED_INV(ADM5120_GPIO_P4L2, "wlan",      NULL ),
108         LED_INV(ADM5120_GPIO_P2L2, "otist",     NULL ),
109         LED_INV(ADM5120_GPIO_xxxx, "usb",       NULL ),
110 };
111 #endif
112
113 /*
114  * Mikrotik boards
115  */
116 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
117 LED_ARRAY(rb100) { /* FIXME: untested */
118         LED_STD(ADM5120_GPIO_PIN6, "power",     NULL ),
119         LED_STD(ADM5120_GPIO_PIN3, "user",      NULL ),
120 };
121 #endif
122
123 LED_ARRAY(rb133) {
124         LED_STD(ADM5120_GPIO_PIN6, "power",     NULL ),
125         LED_STD(ADM5120_GPIO_PIN5, "user",      NULL ),
126 };
127
128 /*
129  * Compex boards
130  */
131 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
132 LED_ARRAY(np27g) { /* FIXME: untested */
133         LED_STD(ADM5120_GPIO_xxxx, "lan1",      NULL ),
134         LED_STD(ADM5120_GPIO_xxxx, "lan2",      NULL ),
135         LED_STD(ADM5120_GPIO_xxxx, "lan3",      NULL ),
136         LED_STD(ADM5120_GPIO_xxxx, "lan4",      NULL ),
137         LED_STD(ADM5120_GPIO_xxxx, "wan_cond",  NULL ),
138         LED_STD(ADM5120_GPIO_xxxx, "wlan",      NULL ),
139         LED_STD(ADM5120_GPIO_xxxx, "wan_act",   NULL ),
140         LED_STD(ADM5120_GPIO_xxxx, "usb1",      NULL ),
141         LED_STD(ADM5120_GPIO_xxxx, "usb2",      NULL ),
142         LED_INV(ADM5120_GPIO_PIN2, "power",     NULL ),
143         LED_STD(ADM5120_GPIO_xxxx, "diag",      NULL ),
144 };
145 #endif
146
147 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
148 LED_ARRAY(np28g) { /* FIXME: untested */
149         LED_STD(ADM5120_GPIO_xxxx, "lan1",      NULL ),
150         LED_STD(ADM5120_GPIO_xxxx, "lan2",      NULL ),
151         LED_STD(ADM5120_GPIO_xxxx, "lan3",      NULL ),
152         LED_STD(ADM5120_GPIO_xxxx, "wan",       NULL ),
153         LED_STD(ADM5120_GPIO_xxxx, "wlan",      NULL ),
154         LED_STD(ADM5120_GPIO_xxxx, "usb1",      NULL ),
155         LED_STD(ADM5120_GPIO_xxxx, "usb2",      NULL ),
156         LED_STD(ADM5120_GPIO_xxxx, "usb3",      NULL ),
157         LED_STD(ADM5120_GPIO_xxxx, "usb4",      NULL ),
158         LED_INV(ADM5120_GPIO_PIN2, "power",     NULL ),
159         LED_STD(ADM5120_GPIO_xxxx, "diag",      NULL ),
160 };
161 #endif
162
163 LED_ARRAY(wp54g) {
164         LED_INV(ADM5120_GPIO_PIN2, "diag",      NULL ),
165         LED_INV(ADM5120_GPIO_PIN6, "wlan",      NULL ),
166         LED_INV(ADM5120_GPIO_PIN7, "wan",       NULL ),
167         LED_INV(ADM5120_GPIO_P0L0, "lan1",      NULL ),
168         LED_INV(ADM5120_GPIO_P1L0, "lan2",      NULL ),
169 };
170
171 LED_ARRAY(generic) {
172 #if defined(CONFIG_LEDS_ADM5120_DIAG)
173         LED_STD(ADM5120_GPIO_PIN0, "gpio0",     NULL ),
174         LED_STD(ADM5120_GPIO_PIN1, "gpio1",     NULL ),
175         LED_STD(ADM5120_GPIO_PIN2, "gpio2",     NULL ),
176         LED_STD(ADM5120_GPIO_PIN3, "gpio3",     NULL ),
177         LED_STD(ADM5120_GPIO_PIN4, "gpio4",     NULL ),
178         LED_STD(ADM5120_GPIO_PIN5, "gpio5",     NULL ),
179         LED_STD(ADM5120_GPIO_PIN6, "gpio6",     NULL ),
180         LED_STD(ADM5120_GPIO_PIN7, "gpio7",     NULL ),
181         LED_STD(ADM5120_GPIO_P0L0, "port0led0", NULL ),
182         LED_STD(ADM5120_GPIO_P0L1, "port0led1", NULL ),
183         LED_STD(ADM5120_GPIO_P0L2, "port0led2", NULL ),
184         LED_STD(ADM5120_GPIO_P1L0, "port1led0", NULL ),
185         LED_STD(ADM5120_GPIO_P1L1, "port1led1", NULL ),
186         LED_STD(ADM5120_GPIO_P1L2, "port1led2", NULL ),
187         LED_STD(ADM5120_GPIO_P2L0, "port2led0", NULL ),
188         LED_STD(ADM5120_GPIO_P2L1, "port2led1", NULL ),
189         LED_STD(ADM5120_GPIO_P2L2, "port2led2", NULL ),
190         LED_STD(ADM5120_GPIO_P3L0, "port3led0", NULL ),
191         LED_STD(ADM5120_GPIO_P3L1, "port3led1", NULL ),
192         LED_STD(ADM5120_GPIO_P3L2, "port3led2", NULL ),
193         LED_STD(ADM5120_GPIO_P4L0, "port4led0", NULL ),
194         LED_STD(ADM5120_GPIO_P4L1, "port4led1", NULL ),
195         LED_STD(ADM5120_GPIO_P4L2, "port4led2", NULL ),
196 #endif
197 };
198
199 #define MACH_DATA(m, n) {                               \
200         .machtype       = (m),                          \
201         .count          = ARRAY_SIZE(n ## _leds),       \
202         .data           = n ## _leds                    \
203 }
204
205 static struct mach_data machines[] __initdata = {
206         MACH_DATA(MACH_ADM5120_GENERIC, generic),
207         MACH_DATA(MACH_ADM5120_P334WT,  p334wt),
208         MACH_DATA(MACH_ADM5120_WP54AG,  wp54g),
209         MACH_DATA(MACH_ADM5120_WP54G,   wp54g),
210         MACH_DATA(MACH_ADM5120_WP54G_WRT, wp54g),
211         MACH_DATA(MACH_ADM5120_WPP54AG, wp54g),
212         MACH_DATA(MACH_ADM5120_WPP54G,  wp54g),
213         MACH_DATA(MACH_ADM5120_RB_133,  rb133),
214         MACH_DATA(MACH_ADM5120_RB_133C, rb133),
215 #if defined(CONFIG_LEDS_ADM5120_EXPERIMENTAL)
216         MACH_DATA(MACH_ADM5120_P334,    p334),
217         MACH_DATA(MACH_ADM5120_P335,    p335),
218         MACH_DATA(MACH_ADM5120_RB_111,  rb100),
219         MACH_DATA(MACH_ADM5120_RB_112,  rb100),
220         MACH_DATA(MACH_ADM5120_NP27G,   np27g),
221         MACH_DATA(MACH_ADM5120_NP28G,   np28g),
222         MACH_DATA(MACH_ADM5120_NP28GHS, np28g),
223 #endif
224 };
225
226 static struct adm5120_leddev * __init
227 create_leddev(struct gpio_led_platform_data *data)
228 {
229         struct adm5120_leddev *p;
230
231         p = kzalloc(sizeof(*p), GFP_KERNEL);
232         if (p == NULL)
233                 return NULL;
234
235         memcpy(&p->pdata, data, sizeof(p->pdata));
236         p->pdev.dev.platform_data = &p->pdata;
237
238         return p;
239 }
240
241 static void
242 destroy_leddev(struct adm5120_leddev *led)
243 {
244         if (led)
245                 kfree(led);
246 }
247
248 static struct mach_data * __init
249 adm5120_leds_findmach(unsigned long machtype)
250 {
251         struct mach_data *mach;
252         int i;
253
254         mach = NULL;
255         for (i=0; i<ARRAY_SIZE(machines); i++) {
256                 if (machines[i].machtype == machtype) {
257                         mach = &machines[i];
258                         break;
259                 }
260         };
261
262 #if defined(CONFIG_LEDS_ADM5120_DIAG)
263         if (mach == NULL)
264                 mach = machines;
265 #endif
266
267         return mach;
268 }
269
270 static int __init
271 adm5120_leds_init(void)
272 {
273         struct mach_data *mach;
274         int i, ret;
275
276         if (mips_machgroup != MACH_GROUP_ADM5120) {
277                 ret = -EINVAL;
278                 goto err;
279         }
280
281         mach = adm5120_leds_findmach(mips_machtype);
282         if (mach == NULL) {
283                 /* the board is not yet supported */
284                 ret = -EINVAL;
285                 goto err;
286         }
287
288         for (i=0; i < mach->count; i++) {
289                 led_devs[i] = create_leddev(&mach->data[i]);
290                 if (led_devs[i] == NULL) {
291                         ret = -ENOMEM;
292                         goto err_destroy;
293                 }
294                 led_devs[i]->pdev.name = "gpio-led";
295                 led_devs[i]->pdev.id = i;
296         }
297
298         for (i=0; i < mach->count; i++) {
299                 ret = platform_device_register(&led_devs[i]->pdev);
300                 if (ret)
301                         goto err_unregister;
302         }
303
304         led_count = mach->count;
305         return 0;
306
307 err_unregister:
308         for (i--; i>=0; i--)
309                 platform_device_unregister(&led_devs[i]->pdev);
310
311 err_destroy:
312         for (i=0; i<led_count; i++)
313                 destroy_leddev(led_devs[i]);
314 err:
315         return ret;
316 }
317
318 static void __exit
319 adm5120_leds_exit(void)
320 {
321         int i;
322
323         for (i=0; i < led_count; i++) {
324                 platform_device_unregister(&led_devs[i]->pdev);
325                 destroy_leddev(led_devs[i]);
326         }
327 }
328
329 module_init(adm5120_leds_init);
330 module_exit(adm5120_leds_exit);
331
332 MODULE_AUTHOR("Gabor Juhos <juhosg@freemail.hu>");
333 MODULE_DESCRIPTION(DRV_DESC);
334 MODULE_LICENSE("GPL");
335