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