7efc032708848fdb0db962cdfedf8f94ebadcf05
[openwrt/staging/wigyori.git] / target / linux / ifxmips / files / drivers / leds / leds-ifxmips.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 <linux/uaccess.h>
29 #include <linux/unistd.h>
30 #include <linux/errno.h>
31 #include <linux/leds.h>
32 #include <linux/delay.h>
33
34 #include <asm/ifxmips/ifxmips.h>
35 #include <asm/ifxmips/ifxmips_gpio.h>
36 #include <asm/ifxmips/ifxmips_pmu.h>
37
38 #define DRVNAME "ifxmips_led"
39
40 #define IFXMIPS_LED_CLK_EDGE IFXMIPS_LED_FALLING
41 //#define IFXMIPS_LED_CLK_EDGE IFXMIPS_LED_RISING
42
43 #define IFXMIPS_LED_SPEED IFXMIPS_LED_8HZ
44
45 #define IFXMIPS_LED_GPIO_PORT 0
46
47 #define IFXMIPS_MAX_LED 24
48
49 struct ifxmips_led {
50 struct led_classdev cdev;
51 u8 bit;
52 };
53
54 void ifxmips_led_set (unsigned int led)
55 {
56 led &= 0xffffff;
57 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CPU0) | led, IFXMIPS_LED_CPU0);
58 }
59 EXPORT_SYMBOL(ifxmips_led_set);
60
61 void ifxmips_led_clear (unsigned int led)
62 {
63 led = ~(led & 0xffffff);
64 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CPU0) & led, IFXMIPS_LED_CPU0);
65 }
66 EXPORT_SYMBOL(ifxmips_led_clear);
67
68 void ifxmips_led_blink_set (unsigned int led)
69 {
70 led &= 0xffffff;
71 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CON0) | led, IFXMIPS_LED_CON0);
72 }
73 EXPORT_SYMBOL(ifxmips_led_blink_set);
74
75 void ifxmips_led_blink_clear (unsigned int led)
76 {
77 led = ~(led & 0xffffff);
78 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CON0) & led, IFXMIPS_LED_CON0);
79 }
80 EXPORT_SYMBOL(ifxmips_led_blink_clear);
81
82 void ifxmips_ledapi_set(struct led_classdev *led_cdev, enum led_brightness value)
83 {
84 struct ifxmips_led *led_dev = container_of(led_cdev, struct ifxmips_led, cdev);
85
86 if(value)
87 ifxmips_led_set(1 << led_dev->bit);
88 else
89 ifxmips_led_clear(1 << led_dev->bit);
90 }
91
92 void ifxmips_led_setup_gpio (void)
93 {
94 int i = 0;
95
96 /* we need to setup pins SH,D,ST (4,5,6) */
97 for (i = 4; i < 7; i++) {
98 ifxmips_port_set_altsel0(IFXMIPS_LED_GPIO_PORT, i);
99 ifxmips_port_clear_altsel1(IFXMIPS_LED_GPIO_PORT, i);
100 ifxmips_port_set_dir_out(IFXMIPS_LED_GPIO_PORT, i);
101 ifxmips_port_set_open_drain(IFXMIPS_LED_GPIO_PORT, i);
102 }
103 }
104
105 static int ifxmips_led_probe(struct platform_device *dev)
106 {
107 int i = 0;
108
109 ifxmips_led_setup_gpio();
110
111 ifxmips_w32(0, IFXMIPS_LED_AR);
112 ifxmips_w32(0, IFXMIPS_LED_CPU0);
113 ifxmips_w32(0, IFXMIPS_LED_CPU1);
114 ifxmips_w32(LED_CON0_SWU, IFXMIPS_LED_CON0);
115 ifxmips_w32(0, IFXMIPS_LED_CON1);
116
117 /* setup the clock edge that the shift register is triggered on */
118 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CON0) & ~IFXMIPS_LED_EDGE_MASK, IFXMIPS_LED_CON0);
119 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CON0) | IFXMIPS_LED_CLK_EDGE, IFXMIPS_LED_CON0);
120
121 /* per default leds 15-0 are set */
122 ifxmips_w32(IFXMIPS_LED_GROUP1 | IFXMIPS_LED_GROUP0, IFXMIPS_LED_CON1);
123
124 /* leds are update periodically by the FPID */
125 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CON1) & ~IFXMIPS_LED_UPD_MASK, IFXMIPS_LED_CON1);
126 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CON1) | IFXMIPS_LED_UPD_SRC_FPI, IFXMIPS_LED_CON1);
127
128 /* set led update speed */
129 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CON1) & ~IFXMIPS_LED_MASK, IFXMIPS_LED_CON1);
130 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CON1) | IFXMIPS_LED_SPEED, IFXMIPS_LED_CON1);
131
132 /* adsl 0 and 1 leds are updated by the arc */
133 ifxmips_w32(ifxmips_r32(IFXMIPS_LED_CON0) | IFXMIPS_LED_ADSL_SRC, IFXMIPS_LED_CON0);
134
135 /* per default, the leds are turned on */
136 ifxmips_pmu_enable(IFXMIPS_PMU_PWDCR_LED);
137
138 for (i = 0; i < IFXMIPS_MAX_LED; i++) {
139 struct ifxmips_led *tmp = kzalloc(sizeof(struct ifxmips_led), GFP_KERNEL);
140 tmp->cdev.brightness_set = ifxmips_ledapi_set;
141 tmp->cdev.name = kmalloc(sizeof("ifxmips:led:00"), GFP_KERNEL);
142 sprintf((char *)tmp->cdev.name, "ifxmips:led:%02d", i);
143 tmp->cdev.default_trigger = NULL;
144 tmp->bit = i;
145 led_classdev_register(&dev->dev, &tmp->cdev);
146 }
147
148 return 0;
149 }
150
151 static int ifxmips_led_remove(struct platform_device *pdev)
152 {
153 return 0;
154 }
155
156 static struct platform_driver ifxmips_led_driver = {
157 .probe = ifxmips_led_probe,
158 .remove = ifxmips_led_remove,
159 .driver = {
160 .name = DRVNAME,
161 .owner = THIS_MODULE,
162 },
163 };
164
165 int __init ifxmips_led_init (void)
166 {
167 int ret = platform_driver_register(&ifxmips_led_driver);
168 if (ret)
169 printk(KERN_INFO "ifxmips_led: Error registering platfom driver!");
170
171 return ret;
172 }
173
174 void __exit ifxmips_led_exit (void)
175 {
176 platform_driver_unregister(&ifxmips_led_driver);
177 }
178
179 module_init(ifxmips_led_init);
180 module_exit(ifxmips_led_exit);