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