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>
37 #include <linux/uaccess.h>
38 #include <linux/semaphore.h>
39 #include <linux/gpio.h>
46 #define PINS_PER_PORT 16
48 #define IFXMIPS_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
50 #define GPIO_TO_PORT(x) ((x > 15) ? (1) : (0))
51 #define GPIO_TO_GPIO(x) ((x > 15) ? (x - 16) : (x))
54 ifxmips_port_reserve_pin(unsigned int port
, unsigned int pin
)
57 printk(KERN_INFO
"%s : call to obseleted function\n", __func__
);
60 EXPORT_SYMBOL(ifxmips_port_reserve_pin
);
63 ifxmips_port_free_pin(unsigned int port
, unsigned int pin
)
66 printk(KERN_INFO
"%s : call to obseleted function\n", __func__
);
69 EXPORT_SYMBOL(ifxmips_port_free_pin
);
72 ifxmips_port_set_open_drain(unsigned int port
, unsigned int pin
)
75 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD
+ (port
* 0xC)) | (1 << pin
),
76 IFXMIPS_GPIO_P0_OD
+ (port
* 0xC));
79 EXPORT_SYMBOL(ifxmips_port_set_open_drain
);
82 ifxmips_port_clear_open_drain(unsigned int port
, unsigned int pin
)
85 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD
+ (port
* 0xC)) & ~(1 << pin
),
86 IFXMIPS_GPIO_P0_OD
+ (port
* 0xC));
89 EXPORT_SYMBOL(ifxmips_port_clear_open_drain
);
92 ifxmips_port_set_pudsel(unsigned int port
, unsigned int pin
)
95 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC)) | (1 << pin
),
96 IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC));
99 EXPORT_SYMBOL(ifxmips_port_set_pudsel
);
102 ifxmips_port_clear_pudsel(unsigned int port
, unsigned int pin
)
105 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC)) & ~(1 << pin
),
106 IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC));
109 EXPORT_SYMBOL(ifxmips_port_clear_pudsel
);
112 ifxmips_port_set_puden(unsigned int port
, unsigned int pin
)
115 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC)) | (1 << pin
),
116 IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC));
119 EXPORT_SYMBOL(ifxmips_port_set_puden
);
122 ifxmips_port_clear_puden(unsigned int port
, unsigned int pin
)
125 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC)) & ~(1 << pin
),
126 IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC));
129 EXPORT_SYMBOL(ifxmips_port_clear_puden
);
132 ifxmips_port_set_stoff(unsigned int port
, unsigned int pin
)
135 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC)) | (1 << pin
),
136 IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC));
139 EXPORT_SYMBOL(ifxmips_port_set_stoff
);
142 ifxmips_port_clear_stoff(unsigned int port
, unsigned int pin
)
145 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC)) & ~(1 << pin
),
146 IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC));
149 EXPORT_SYMBOL(ifxmips_port_clear_stoff
);
152 ifxmips_port_set_dir_out(unsigned int port
, unsigned int pin
)
155 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC)) | (1 << pin
),
156 IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC));
159 EXPORT_SYMBOL(ifxmips_port_set_dir_out
);
162 ifxmips_port_set_dir_in(unsigned int port
, unsigned int pin
)
165 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC)) & ~(1 << pin
),
166 IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC));
169 EXPORT_SYMBOL(ifxmips_port_set_dir_in
);
172 ifxmips_port_set_output(unsigned int port
, unsigned int pin
)
175 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC)) | (1 << pin
),
176 IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC));
179 EXPORT_SYMBOL(ifxmips_port_set_output
);
182 ifxmips_port_clear_output(unsigned int port
, unsigned int pin
)
185 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC)) & ~(1 << pin
),
186 IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC));
189 EXPORT_SYMBOL(ifxmips_port_clear_output
);
192 ifxmips_port_get_input(unsigned int port
, unsigned int pin
)
195 if (ifxmips_r32(IFXMIPS_GPIO_P0_IN
+ (port
* 0xC)) & (1 << pin
))
200 EXPORT_SYMBOL(ifxmips_port_get_input
);
203 ifxmips_port_set_altsel0(unsigned int port
, unsigned int pin
)
206 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC)) | (1 << pin
),
207 IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC));
210 EXPORT_SYMBOL(ifxmips_port_set_altsel0
);
213 ifxmips_port_clear_altsel0(unsigned int port
, unsigned int pin
)
216 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC)) & ~(1 << pin
),
217 IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC));
220 EXPORT_SYMBOL(ifxmips_port_clear_altsel0
);
223 ifxmips_port_set_altsel1(unsigned int port
, unsigned int pin
)
226 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC)) | (1 << pin
),
227 IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC));
230 EXPORT_SYMBOL(ifxmips_port_set_altsel1
);
233 ifxmips_port_clear_altsel1(unsigned int port
, unsigned int pin
)
236 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC)) & ~(1 << pin
),
237 IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC));
240 EXPORT_SYMBOL(ifxmips_port_clear_altsel1
);
243 ifxmips_gpio_set(struct gpio_chip
*chip
, unsigned int offset
, int value
)
245 int port
= GPIO_TO_PORT(offset
);
246 int gpio
= GPIO_TO_GPIO(offset
);
248 ifxmips_port_set_output(port
, gpio
);
250 ifxmips_port_clear_output(port
, gpio
);
254 ifxmips_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
256 int port
= GPIO_TO_PORT(offset
);
257 int gpio
= GPIO_TO_GPIO(offset
);
258 return ifxmips_port_get_input(port
, gpio
);
262 ifxmips_gpio_direction_input(struct gpio_chip
*chip
, unsigned int offset
)
264 int port
= GPIO_TO_PORT(offset
);
265 int gpio
= GPIO_TO_GPIO(offset
);
266 ifxmips_port_set_open_drain(port
, gpio
);
267 ifxmips_port_clear_altsel0(port
, gpio
);
268 ifxmips_port_clear_altsel1(port
, gpio
);
269 ifxmips_port_set_dir_in(port
, gpio
);
274 ifxmips_gpio_direction_output(struct gpio_chip
*chip
, unsigned int offset
, int value
)
276 int port
= GPIO_TO_PORT(offset
);
277 int gpio
= GPIO_TO_GPIO(offset
);
278 ifxmips_port_clear_open_drain(port
, gpio
);
279 ifxmips_port_clear_altsel0(port
, gpio
);
280 ifxmips_port_clear_altsel1(port
, gpio
);
281 ifxmips_port_set_dir_out(port
, gpio
);
282 ifxmips_gpio_set(chip
, offset
, value
);
287 gpio_to_irq(unsigned int gpio
)
291 EXPORT_SYMBOL(gpio_to_irq
);
296 .label
= "ifxmips-gpio",
297 .direction_input
= ifxmips_gpio_direction_input
,
298 .direction_output
= ifxmips_gpio_direction_output
,
299 .get
= ifxmips_gpio_get
,
300 .set
= ifxmips_gpio_set
,
306 ifxmips_gpio_probe(struct platform_device
*dev
)
308 gpiochip_add(&ifxmips_gpio_chip
);
313 ifxmips_gpio_remove(struct platform_device
*pdev
)
315 gpiochip_remove(&ifxmips_gpio_chip
);
319 static struct platform_driver
320 ifxmips_gpio_driver
= {
321 .probe
= ifxmips_gpio_probe
,
322 .remove
= ifxmips_gpio_remove
,
324 .name
= "ifxmips_gpio",
325 .owner
= THIS_MODULE
,
330 ifxmips_gpio_init(void)
332 int ret
= platform_driver_register(&ifxmips_gpio_driver
);
334 printk(KERN_INFO
"ifxmips_gpio : Error registering platfom driver!");
339 ifxmips_gpio_exit(void)
341 platform_driver_unregister(&ifxmips_gpio_driver
);
344 module_init(ifxmips_gpio_init
);
345 module_exit(ifxmips_gpio_exit
);