eef514669837c9bf400e20d07a279be5c8481f84
[openwrt/staging/wigyori.git] / target / linux / ifxmips / files / arch / mips / ifxmips / gpio.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) 2004 btxu Generate from INCA-IP project
17 * Copyright (C) 2005 Jin-Sze.Sow Comments edited
18 * Copyright (C) 2006 Huang Xiaogang Modification & verification on Danube chip
19 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
20 */
21
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/proc_fs.h>
26 #include <linux/init.h>
27 #include <linux/ioctl.h>
28 #include <linux/timer.h>
29 #include <linux/module.h>
30 #include <linux/timer.h>
31 #include <linux/interrupt.h>
32 #include <linux/kobject.h>
33 #include <linux/workqueue.h>
34 #include <linux/skbuff.h>
35 #include <linux/netlink.h>
36 #include <linux/platform_device.h>
37 #include <net/sock.h>
38 #include <asm/uaccess.h>
39 #include <asm/semaphore.h>
40 #include <asm/uaccess.h>
41 #include <asm/ifxmips/ifxmips.h>
42
43 #define MAX_PORTS 2
44 #define PINS_PER_PORT 16
45
46 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
47
48 unsigned int rst_port = 1;
49 unsigned int rst_pin = 15;
50 static struct timer_list rst_button_timer;
51
52 extern struct sock *uevent_sock;
53 extern u64 uevent_next_seqnum(void);
54 static unsigned long seen;
55 static int pressed = 0;
56
57 struct event_t {
58 struct work_struct wq;
59 int set;
60 unsigned long jiffies;
61 };
62 #endif
63
64 #define IFXMIPS_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
65 int
66 ifxmips_port_reserve_pin(unsigned int port, unsigned int pin)
67 {
68 IFXMIPS_GPIO_SANITY;
69 printk("%s : call to obseleted function\n", __func__);
70 return 0;
71 }
72 EXPORT_SYMBOL(ifxmips_port_reserve_pin);
73
74 int
75 ifxmips_port_free_pin(unsigned int port, unsigned int pin)
76 {
77 IFXMIPS_GPIO_SANITY;
78 printk("%s : call to obseleted function\n", __func__);
79 return 0;
80 }
81 EXPORT_SYMBOL(ifxmips_port_free_pin);
82
83 int
84 ifxmips_port_set_open_drain(unsigned int port, unsigned int pin)
85 {
86 IFXMIPS_GPIO_SANITY;
87 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD + (port * 0xC)) | (1 << pin),
88 IFXMIPS_GPIO_P0_OD + (port * 0xC));
89 return 0;
90 }
91 EXPORT_SYMBOL(ifxmips_port_set_open_drain);
92
93 int
94 ifxmips_port_clear_open_drain(unsigned int port, unsigned int pin)
95 {
96 IFXMIPS_GPIO_SANITY;
97 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD + (port * 0xC)) & ~(1 << pin),
98 IFXMIPS_GPIO_P0_OD + (port * 0xC));
99 return 0;
100 }
101 EXPORT_SYMBOL(ifxmips_port_clear_open_drain);
102
103 int
104 ifxmips_port_set_pudsel(unsigned int port, unsigned int pin)
105 {
106 IFXMIPS_GPIO_SANITY;
107 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC)) | (1 << pin),
108 IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC));
109 return 0;
110 }
111 EXPORT_SYMBOL(ifxmips_port_set_pudsel);
112
113 int
114 ifxmips_port_clear_pudsel (unsigned int port, unsigned int pin)
115 {
116 IFXMIPS_GPIO_SANITY;
117 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC)) & ~(1 << pin),
118 IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC));
119 return 0;
120 }
121 EXPORT_SYMBOL(ifxmips_port_clear_pudsel);
122
123 int
124 ifxmips_port_set_puden(unsigned int port, unsigned int pin)
125 {
126 IFXMIPS_GPIO_SANITY;
127 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN + (port * 0xC)) | (1 << pin),
128 IFXMIPS_GPIO_P0_PUDEN + (port * 0xC));
129 return 0;
130 }
131 EXPORT_SYMBOL(ifxmips_port_set_puden);
132
133 int
134 ifxmips_port_clear_puden(unsigned int port, unsigned int pin)
135 {
136 IFXMIPS_GPIO_SANITY;
137 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN + (port * 0xC)) & ~(1 << pin),
138 IFXMIPS_GPIO_P0_PUDEN + (port * 0xC));
139 return 0;
140 }
141 EXPORT_SYMBOL(ifxmips_port_clear_puden);
142
143 int
144 ifxmips_port_set_stoff(unsigned int port, unsigned int pin)
145 {
146 IFXMIPS_GPIO_SANITY;
147 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF + (port * 0xC)) | (1 << pin),
148 IFXMIPS_GPIO_P0_STOFF + (port * 0xC));
149 return 0;
150 }
151 EXPORT_SYMBOL(ifxmips_port_set_stoff);
152
153 int
154 ifxmips_port_clear_stoff(unsigned int port, unsigned int pin)
155 {
156 IFXMIPS_GPIO_SANITY;
157 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF + (port * 0xC)) & ~(1 << pin),
158 IFXMIPS_GPIO_P0_STOFF + (port * 0xC));
159 return 0;
160 }
161 EXPORT_SYMBOL(ifxmips_port_clear_stoff);
162
163 int
164 ifxmips_port_set_dir_out(unsigned int port, unsigned int pin)
165 {
166 IFXMIPS_GPIO_SANITY;
167 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR + (port * 0xC)) | (1 << pin),
168 IFXMIPS_GPIO_P0_DIR + (port * 0xC));
169 return 0;
170 }
171 EXPORT_SYMBOL(ifxmips_port_set_dir_out);
172
173 int
174 ifxmips_port_set_dir_in(unsigned int port, unsigned int pin)
175 {
176 IFXMIPS_GPIO_SANITY;
177 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR + (port * 0xC)) & ~(1 << pin),
178 IFXMIPS_GPIO_P0_DIR + (port * 0xC));
179 return 0;
180 }
181 EXPORT_SYMBOL(ifxmips_port_set_dir_in);
182
183 int
184 ifxmips_port_set_output(unsigned int port, unsigned int pin)
185 {
186 IFXMIPS_GPIO_SANITY;
187 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT + (port * 0xC)) | (1 << pin),
188 IFXMIPS_GPIO_P0_OUT + (port * 0xC));
189 return 0;
190 }
191 EXPORT_SYMBOL(ifxmips_port_set_output);
192
193 int
194 ifxmips_port_clear_output(unsigned int port, unsigned int pin)
195 {
196 IFXMIPS_GPIO_SANITY;
197 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT + (port * 0xC)) & ~(1 << pin),
198 IFXMIPS_GPIO_P0_OUT + (port * 0xC));
199 return 0;
200 }
201 EXPORT_SYMBOL(ifxmips_port_clear_output);
202
203 int
204 ifxmips_port_get_input(unsigned int port, unsigned int pin)
205 {
206 IFXMIPS_GPIO_SANITY;
207 if (ifxmips_r32(IFXMIPS_GPIO_P0_IN + (port * 0xC)) & (1 << pin))
208 return 0;
209 else
210 return 1;
211 }
212 EXPORT_SYMBOL(ifxmips_port_get_input);
213
214 int
215 ifxmips_port_set_altsel0(unsigned int port, unsigned int pin)
216 {
217 IFXMIPS_GPIO_SANITY;
218 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC)) | (1 << pin),
219 IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC));
220 return 0;
221 }
222 EXPORT_SYMBOL(ifxmips_port_set_altsel0);
223
224 int
225 ifxmips_port_clear_altsel0(unsigned int port, unsigned int pin)
226 {
227 IFXMIPS_GPIO_SANITY;
228 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC)) & ~(1 << pin),
229 IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC));
230 return 0;
231 }
232 EXPORT_SYMBOL(ifxmips_port_clear_altsel0);
233
234 int
235 ifxmips_port_set_altsel1(unsigned int port, unsigned int pin)
236 {
237 IFXMIPS_GPIO_SANITY;
238 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC)) | (1 << pin),
239 IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC));
240 return 0;
241 }
242 EXPORT_SYMBOL(ifxmips_port_set_altsel1);
243
244 int
245 ifxmips_port_clear_altsel1(unsigned int port, unsigned int pin)
246 {
247 IFXMIPS_GPIO_SANITY;
248 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC)) & ~(1 << pin),
249 IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC));
250 return 0;
251 }
252 EXPORT_SYMBOL(ifxmips_port_clear_altsel1);
253
254 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
255 static inline void
256 add_msg(struct sk_buff *skb, char *msg)
257 {
258 char *scratch;
259 scratch = skb_put(skb, strlen(msg) + 1);
260 sprintf(scratch, msg);
261 }
262
263 static void
264 hotplug_button(struct work_struct *wq)
265 {
266 struct sk_buff *skb;
267 struct event_t *event;
268 size_t len;
269 char *scratch, *s;
270 char buf[128];
271
272 event = container_of(wq, struct event_t, wq);
273 if(!uevent_sock)
274 goto done;
275
276 s = event->set ? "pressed" : "released";
277 len = strlen(s) + 2;
278 skb = alloc_skb(len + 2048, GFP_KERNEL);
279 if(!skb)
280 goto done;
281
282 scratch = skb_put(skb, len);
283 sprintf(scratch, "%s@",s);
284 add_msg(skb, "HOME=/");
285 add_msg(skb, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
286 add_msg(skb, "SUBSYSTEM=button");
287 add_msg(skb, "BUTTON=reset");
288 add_msg(skb, (event->set ? "ACTION=pressed" : "ACTION=released"));
289 sprintf(buf, "SEEN=%ld", (event->jiffies - seen)/HZ);
290 add_msg(skb, buf);
291 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
292 add_msg(skb, buf);
293
294 NETLINK_CB(skb).dst_group = 1;
295 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
296 done:
297 kfree(event);
298 }
299
300 static void
301 reset_button_poll(unsigned long unused)
302 {
303 struct event_t *event;
304
305 rst_button_timer.expires = jiffies + (HZ / 4);
306 add_timer(&rst_button_timer);
307
308 if (pressed != ifxmips_port_get_input(rst_port, rst_pin))
309 {
310 if(pressed)
311 pressed = 0;
312 else
313 pressed = 1;
314 event = (struct event_t *) kzalloc(sizeof(struct event_t), GFP_ATOMIC);
315 if (!event)
316 {
317 printk("Could not alloc hotplug event\n");
318 return;
319 }
320 event->set = pressed;
321 event->jiffies = jiffies;
322 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
323 schedule_work(&event->wq);
324 seen = jiffies;
325 }
326 }
327 #endif
328
329 static int
330 ifxmips_gpio_probe(struct platform_device *dev)
331 {
332 int retval = 0;
333
334 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
335 rst_port = dev->resource[0].start;
336 rst_pin = dev->resource[0].end;
337 ifxmips_port_set_open_drain(rst_port, rst_pin);
338 ifxmips_port_clear_altsel0(rst_port, rst_pin);
339 ifxmips_port_clear_altsel1(rst_port, rst_pin);
340 ifxmips_port_set_dir_in(rst_port, rst_pin);
341 seen = jiffies;
342 init_timer(&rst_button_timer);
343 rst_button_timer.function = reset_button_poll;
344 rst_button_timer.expires = jiffies + HZ;
345 add_timer(&rst_button_timer);
346 #endif
347 return retval;
348 }
349
350 static int
351 ifxmips_gpio_remove(struct platform_device *pdev)
352 {
353 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
354 del_timer_sync(&rst_button_timer);
355 #endif
356 return 0;
357 }
358
359 static struct
360 platform_driver ifxmips_gpio_driver = {
361 .probe = ifxmips_gpio_probe,
362 .remove = ifxmips_gpio_remove,
363 .driver = {
364 .name = "ifxmips_gpio",
365 .owner = THIS_MODULE,
366 },
367 };
368
369 int __init
370 ifxmips_gpio_init(void)
371 {
372 int ret = platform_driver_register(&ifxmips_gpio_driver);
373 if (ret)
374 printk(KERN_INFO "ifxmips_gpio : Error registering platfom driver!");
375 return ret;
376 }
377
378 void __exit
379 ifxmips_gpio_exit(void)
380 {
381 platform_driver_unregister(&ifxmips_gpio_driver);
382 }
383
384 module_init(ifxmips_gpio_init);
385 module_exit(ifxmips_gpio_exit);