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