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