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.
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.
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.
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>
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>
38 #include <asm/uaccess.h>
39 #include <asm/semaphore.h>
40 #include <asm/uaccess.h>
41 #include <asm/ifxmips/ifxmips.h>
44 #define PINS_PER_PORT 16
46 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
48 unsigned int rst_port
= 1;
49 unsigned int rst_pin
= 15;
50 static struct timer_list rst_button_timer
;
52 extern struct sock
*uevent_sock
;
53 extern u64
uevent_next_seqnum(void);
54 static unsigned long seen
;
55 static int pressed
= 0;
58 struct work_struct wq
;
60 unsigned long jiffies
;
64 #define IFXMIPS_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
66 ifxmips_port_reserve_pin(unsigned int port
, unsigned int pin
)
69 printk("%s : call to obseleted function\n", __func__
);
72 EXPORT_SYMBOL(ifxmips_port_reserve_pin
);
75 ifxmips_port_free_pin(unsigned int port
, unsigned int pin
)
78 printk("%s : call to obseleted function\n", __func__
);
81 EXPORT_SYMBOL(ifxmips_port_free_pin
);
84 ifxmips_port_set_open_drain(unsigned int port
, unsigned int pin
)
87 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD
+ (port
* 0xC)) | (1 << pin
),
88 IFXMIPS_GPIO_P0_OD
+ (port
* 0xC));
91 EXPORT_SYMBOL(ifxmips_port_set_open_drain
);
94 ifxmips_port_clear_open_drain(unsigned int port
, unsigned int pin
)
97 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD
+ (port
* 0xC)) & ~(1 << pin
),
98 IFXMIPS_GPIO_P0_OD
+ (port
* 0xC));
101 EXPORT_SYMBOL(ifxmips_port_clear_open_drain
);
104 ifxmips_port_set_pudsel(unsigned int port
, unsigned int pin
)
107 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC)) | (1 << pin
),
108 IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC));
111 EXPORT_SYMBOL(ifxmips_port_set_pudsel
);
114 ifxmips_port_clear_pudsel (unsigned int port
, unsigned int pin
)
117 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC)) & ~(1 << pin
),
118 IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC));
121 EXPORT_SYMBOL(ifxmips_port_clear_pudsel
);
124 ifxmips_port_set_puden(unsigned int port
, unsigned int pin
)
127 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC)) | (1 << pin
),
128 IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC));
131 EXPORT_SYMBOL(ifxmips_port_set_puden
);
134 ifxmips_port_clear_puden(unsigned int port
, unsigned int pin
)
137 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC)) & ~(1 << pin
),
138 IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC));
141 EXPORT_SYMBOL(ifxmips_port_clear_puden
);
144 ifxmips_port_set_stoff(unsigned int port
, unsigned int pin
)
147 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC)) | (1 << pin
),
148 IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC));
151 EXPORT_SYMBOL(ifxmips_port_set_stoff
);
154 ifxmips_port_clear_stoff(unsigned int port
, unsigned int pin
)
157 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC)) & ~(1 << pin
),
158 IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC));
161 EXPORT_SYMBOL(ifxmips_port_clear_stoff
);
164 ifxmips_port_set_dir_out(unsigned int port
, unsigned int pin
)
167 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC)) | (1 << pin
),
168 IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC));
171 EXPORT_SYMBOL(ifxmips_port_set_dir_out
);
174 ifxmips_port_set_dir_in(unsigned int port
, unsigned int pin
)
177 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC)) & ~(1 << pin
),
178 IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC));
181 EXPORT_SYMBOL(ifxmips_port_set_dir_in
);
184 ifxmips_port_set_output(unsigned int port
, unsigned int pin
)
187 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC)) | (1 << pin
),
188 IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC));
191 EXPORT_SYMBOL(ifxmips_port_set_output
);
194 ifxmips_port_clear_output(unsigned int port
, unsigned int pin
)
197 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC)) & ~(1 << pin
),
198 IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC));
201 EXPORT_SYMBOL(ifxmips_port_clear_output
);
204 ifxmips_port_get_input(unsigned int port
, unsigned int pin
)
207 if (ifxmips_r32(IFXMIPS_GPIO_P0_IN
+ (port
* 0xC)) & (1 << pin
))
212 EXPORT_SYMBOL(ifxmips_port_get_input
);
215 ifxmips_port_set_altsel0(unsigned int port
, unsigned int pin
)
218 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC)) | (1 << pin
),
219 IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC));
222 EXPORT_SYMBOL(ifxmips_port_set_altsel0
);
225 ifxmips_port_clear_altsel0(unsigned int port
, unsigned int pin
)
228 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC)) & ~(1 << pin
),
229 IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC));
232 EXPORT_SYMBOL(ifxmips_port_clear_altsel0
);
235 ifxmips_port_set_altsel1(unsigned int port
, unsigned int pin
)
238 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC)) | (1 << pin
),
239 IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC));
242 EXPORT_SYMBOL(ifxmips_port_set_altsel1
);
245 ifxmips_port_clear_altsel1(unsigned int port
, unsigned int pin
)
248 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC)) & ~(1 << pin
),
249 IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC));
252 EXPORT_SYMBOL(ifxmips_port_clear_altsel1
);
254 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
256 add_msg(struct sk_buff
*skb
, char *msg
)
259 scratch
= skb_put(skb
, strlen(msg
) + 1);
260 sprintf(scratch
, msg
);
264 hotplug_button(struct work_struct
*wq
)
267 struct event_t
*event
;
272 event
= container_of(wq
, struct event_t
, wq
);
276 s
= event
->set
? "pressed" : "released";
278 skb
= alloc_skb(len
+ 2048, GFP_KERNEL
);
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
);
291 snprintf(buf
, 128, "SEQNUM=%llu", uevent_next_seqnum());
294 NETLINK_CB(skb
).dst_group
= 1;
295 netlink_broadcast(uevent_sock
, skb
, 0, 1, GFP_KERNEL
);
301 reset_button_poll(unsigned long unused
)
303 struct event_t
*event
;
305 rst_button_timer
.expires
= jiffies
+ (HZ
/ 4);
306 add_timer(&rst_button_timer
);
308 if (pressed
!= ifxmips_port_get_input(rst_port
, rst_pin
))
314 event
= (struct event_t
*) kzalloc(sizeof(struct event_t
), GFP_ATOMIC
);
317 printk("Could not alloc hotplug event\n");
320 event
->set
= pressed
;
321 event
->jiffies
= jiffies
;
322 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
);
323 schedule_work(&event
->wq
);
330 ifxmips_gpio_probe(struct platform_device
*dev
)
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
);
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
);
351 ifxmips_gpio_remove(struct platform_device
*pdev
)
353 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
354 del_timer_sync(&rst_button_timer
);
360 platform_driver ifxmips_gpio_driver
= {
361 .probe
= ifxmips_gpio_probe
,
362 .remove
= ifxmips_gpio_remove
,
364 .name
= "ifxmips_gpio",
365 .owner
= THIS_MODULE
,
370 ifxmips_gpio_init(void)
372 int ret
= platform_driver_register(&ifxmips_gpio_driver
);
374 printk(KERN_INFO
"ifxmips_gpio : Error registering platfom driver!");
379 ifxmips_gpio_exit(void)
381 platform_driver_unregister(&ifxmips_gpio_driver
);
384 module_init(ifxmips_gpio_init
);
385 module_exit(ifxmips_gpio_exit
);