add proper led support to ifxmips
[openwrt.git] / target / linux / ifxmips / files / drivers / char / ifxmips_led.c
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15  *
16  *   Copyright (C) 2006 infineon
17  *   Copyright (C) 2007 John Crispin <blogic@openwrt.org> 
18  *
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/version.h>
24 #include <linux/types.h>
25 #include <linux/fs.h>
26 #include <linux/init.h>
27 #include <linux/platform_device.h>
28 #include <asm/uaccess.h>
29 #include <asm/unistd.h>
30 #include <linux/errno.h>
31 #include <asm/ifxmips/ifxmips.h>
32 #include <asm/ifxmips/ifxmips_gpio.h>
33 #include <asm/ifxmips/ifxmips_pmu.h>
34 #include <linux/leds.h>
35 #include <linux/delay.h>
36
37 #define DRVNAME                                                 "ifxmips_led"
38
39 #define IFXMIPS_LED_CLK_EDGE                    IFXMIPS_LED_FALLING
40 //#define IFXMIPS_LED_CLK_EDGE                  IFXMIPS_LED_RISING
41
42 #define IFXMIPS_LED_SPEED                               IFXMIPS_LED_8HZ
43
44 #define IFXMIPS_LED_GPIO_PORT                   0
45
46 #define IFXMIPS_MAX_LED                                 24
47
48 struct ifxmips_led {
49         struct led_classdev cdev;
50         u8 bit;
51 };
52
53 void
54 ifxmips_led_set (unsigned int led)
55 {
56         led &= 0xffffff;
57         writel(readl(IFXMIPS_LED_CPU0) | led, IFXMIPS_LED_CPU0);
58 }
59 EXPORT_SYMBOL(ifxmips_led_set);
60
61 void
62 ifxmips_led_clear (unsigned int led)
63 {
64         led = ~(led & 0xffffff);
65         writel(readl(IFXMIPS_LED_CPU0) & led, IFXMIPS_LED_CPU0);
66 }
67 EXPORT_SYMBOL(ifxmips_led_clear);
68
69 void
70 ifxmips_led_blink_set (unsigned int led)
71 {
72         led &= 0xffffff;
73         writel(readl(IFXMIPS_LED_CON0) | led, IFXMIPS_LED_CON0);
74 }
75 EXPORT_SYMBOL(ifxmips_led_blink_set);
76
77 void
78 ifxmips_led_blink_clear (unsigned int led)
79 {
80         led = ~(led & 0xffffff);
81         writel(readl(IFXMIPS_LED_CON0) & led, IFXMIPS_LED_CON0);
82 }
83 EXPORT_SYMBOL(ifxmips_led_blink_clear);
84
85 void
86 ifxmips_ledapi_set(struct led_classdev *led_cdev, enum led_brightness value)
87 {
88         struct ifxmips_led *led_dev = container_of(led_cdev, struct ifxmips_led, cdev);
89
90         if(value)
91                 ifxmips_led_set(1 << led_dev->bit);
92         else
93                 ifxmips_led_clear(1 << led_dev->bit);
94 }
95
96 void
97 ifxmips_led_setup_gpio (void)
98 {
99         int i = 0;
100
101         /* we need to setup pins SH,D,ST (4,5,6) */
102         for (i = 4; i < 7; i++)
103         {
104                 ifxmips_port_set_altsel0(IFXMIPS_LED_GPIO_PORT, i);
105                 ifxmips_port_clear_altsel1(IFXMIPS_LED_GPIO_PORT, i);
106                 ifxmips_port_set_dir_out(IFXMIPS_LED_GPIO_PORT, i);
107                 ifxmips_port_set_open_drain(IFXMIPS_LED_GPIO_PORT, i);
108         }
109 }
110
111 static int
112 ifxmips_led_probe(struct platform_device *dev)
113 {
114         int i = 0;
115
116         ifxmips_led_setup_gpio();
117
118         writel(0, IFXMIPS_LED_AR);
119         writel(0, IFXMIPS_LED_CPU0);
120         writel(0, IFXMIPS_LED_CPU1);
121         writel(LED_CON0_SWU, IFXMIPS_LED_CON0);
122         writel(0, IFXMIPS_LED_CON1);
123
124         /* setup the clock edge that the shift register is triggered on */
125         writel(readl(IFXMIPS_LED_CON0) & ~IFXMIPS_LED_EDGE_MASK, IFXMIPS_LED_CON0);
126         writel(readl(IFXMIPS_LED_CON0) | IFXMIPS_LED_CLK_EDGE, IFXMIPS_LED_CON0);
127
128         /* per default leds 15-0 are set */
129         writel(IFXMIPS_LED_GROUP1 | IFXMIPS_LED_GROUP0, IFXMIPS_LED_CON1);
130
131         /* leds are update periodically by the FPID */
132         writel(readl(IFXMIPS_LED_CON1) & ~IFXMIPS_LED_UPD_MASK, IFXMIPS_LED_CON1);
133         writel(readl(IFXMIPS_LED_CON1) | IFXMIPS_LED_UPD_SRC_FPI, IFXMIPS_LED_CON1);
134
135         /* set led update speed */
136         writel(readl(IFXMIPS_LED_CON1) & ~IFXMIPS_LED_MASK, IFXMIPS_LED_CON1);
137         writel(readl(IFXMIPS_LED_CON1) | IFXMIPS_LED_SPEED, IFXMIPS_LED_CON1);
138
139         /* adsl 0 and 1 leds are updated by the arc */
140         writel(readl(IFXMIPS_LED_CON0) | IFXMIPS_LED_ADSL_SRC, IFXMIPS_LED_CON0);
141
142         /* per default, the leds are turned on */
143         ifxmips_pmu_enable(IFXMIPS_PMU_PWDCR_LED);
144
145         for(i = 0; i < IFXMIPS_MAX_LED; i++)
146         {
147                 struct ifxmips_led *tmp = kzalloc(sizeof(struct ifxmips_led), GFP_KERNEL);
148                 tmp->cdev.brightness_set = ifxmips_ledapi_set;
149                 tmp->cdev.name = kmalloc(sizeof("ifxmips:led:00"), GFP_KERNEL);
150                 sprintf(tmp->cdev.name, "ifxmips:led:%02d", i);
151                 tmp->cdev.default_trigger = NULL;
152                 tmp->bit = i;
153                 led_classdev_register(&dev->dev, &tmp->cdev);
154         }
155
156         return 0;
157 }
158
159 static int
160 ifxmips_led_remove(struct platform_device *pdev)
161 {
162         return 0;
163 }
164
165 static struct
166 platform_driver ifxmips_led_driver = {
167         .probe = ifxmips_led_probe,
168         .remove = ifxmips_led_remove,
169         .driver = {
170                 .name = DRVNAME,
171                 .owner = THIS_MODULE,
172         },
173 };
174
175 int __init
176 ifxmips_led_init (void)
177 {
178         int ret = platform_driver_register(&ifxmips_led_driver);
179         if (ret)
180                 printk(KERN_INFO "ifxmips_led: Error registering platfom driver!");
181
182         return ret;
183 }
184
185 void __exit
186 ifxmips_led_exit (void)
187 {
188         platform_driver_unregister(&ifxmips_led_driver);
189 }
190
191 module_init(ifxmips_led_init);
192 module_exit(ifxmips_led_exit);