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