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) 2005 infineon
17 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
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 <asm/semaphore.h>
28 #include <asm/uaccess.h>
29 #include <asm/ifxmips/ifxmips.h>
30 #include <asm/ifxmips/ifxmips_ioctl.h>
33 #define PINS_PER_PORT 16
35 static unsigned int ifxmips_gpio_major
= 0;
37 /* TODO do we need this ? */
38 static struct semaphore port_sem
;
40 /* TODO do we really need this ? return in a define is forbidden by coding style */
41 #define IFXMIPS_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
44 ifxmips_port_reserve_pin (unsigned int port
, unsigned int pin
)
47 printk("%s : call to obseleted function\n", __func__
);
51 EXPORT_SYMBOL(ifxmips_port_reserve_pin
);
54 ifxmips_port_free_pin (unsigned int port
, unsigned int pin
)
57 printk("%s : call to obseleted function\n", __func__
);
61 EXPORT_SYMBOL(ifxmips_port_free_pin
);
64 ifxmips_port_set_open_drain (unsigned int port
, unsigned int pin
)
67 writel(readl(IFXMIPS_GPIO_P0_OD
+ (port
* 0x30)) | (1 << pin
), IFXMIPS_GPIO_P0_OD
);
71 EXPORT_SYMBOL(ifxmips_port_set_open_drain
);
74 ifxmips_port_clear_open_drain (unsigned int port
, unsigned int pin
)
77 writel(readl(IFXMIPS_GPIO_P0_OD
+ (port
* 0x30)) & ~(1 << pin
), IFXMIPS_GPIO_P0_OD
);
81 EXPORT_SYMBOL(ifxmips_port_clear_open_drain
);
84 ifxmips_port_set_pudsel (unsigned int port
, unsigned int pin
)
87 writel(readl(IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0x30)) | (1 << pin
), IFXMIPS_GPIO_P0_PUDSEL
);
91 EXPORT_SYMBOL(ifxmips_port_set_pudsel
);
94 ifxmips_port_clear_pudsel (unsigned int port
, unsigned int pin
)
97 writel(readl(IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0x30)) & ~(1 << pin
), IFXMIPS_GPIO_P0_PUDSEL
);
101 EXPORT_SYMBOL(ifxmips_port_clear_pudsel
);
104 ifxmips_port_set_puden (unsigned int port
, unsigned int pin
)
107 writel(readl(IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0x30)) | (1 << pin
), IFXMIPS_GPIO_P0_PUDEN
);
111 EXPORT_SYMBOL(ifxmips_port_set_puden
);
114 ifxmips_port_clear_puden (unsigned int port
, unsigned int pin
)
117 writel(readl(IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0x30)) & ~(1 << pin
), IFXMIPS_GPIO_P0_PUDEN
);
121 EXPORT_SYMBOL(ifxmips_port_clear_puden
);
124 ifxmips_port_set_stoff (unsigned int port
, unsigned int pin
)
127 writel(readl(IFXMIPS_GPIO_P0_STOFF
+ (port
* 0x30)) | (1 << pin
), IFXMIPS_GPIO_P0_STOFF
);
131 EXPORT_SYMBOL(ifxmips_port_set_stoff
);
134 ifxmips_port_clear_stoff (unsigned int port
, unsigned int pin
)
137 writel(readl(IFXMIPS_GPIO_P0_STOFF
+ (port
* 0x30)) & ~(1 << pin
), IFXMIPS_GPIO_P0_STOFF
);
141 EXPORT_SYMBOL(ifxmips_port_clear_stoff
);
144 ifxmips_port_set_dir_out (unsigned int port
, unsigned int pin
)
147 writel(readl(IFXMIPS_GPIO_P0_DIR
+ (port
* 0x30)) | (1 << pin
), IFXMIPS_GPIO_P0_DIR
);
151 EXPORT_SYMBOL(ifxmips_port_set_dir_out
);
154 ifxmips_port_set_dir_in (unsigned int port
, unsigned int pin
)
157 writel(readl(IFXMIPS_GPIO_P0_DIR
+ (port
* 0x30)) & ~(1 << pin
), IFXMIPS_GPIO_P0_DIR
);
161 EXPORT_SYMBOL(ifxmips_port_set_dir_in
);
164 ifxmips_port_set_output (unsigned int port
, unsigned int pin
)
167 writel(readl(IFXMIPS_GPIO_P0_OUT
+ (port
* 0x30)) | (1 << pin
), IFXMIPS_GPIO_P0_OUT
);
171 EXPORT_SYMBOL(ifxmips_port_set_output
);
174 ifxmips_port_clear_output (unsigned int port
, unsigned int pin
)
177 writel(readl(IFXMIPS_GPIO_P0_OUT
+ (port
* 0x30)) & ~(1 << pin
), IFXMIPS_GPIO_P0_OUT
);
181 EXPORT_SYMBOL(ifxmips_port_clear_output
);
184 ifxmips_port_get_input (unsigned int port
, unsigned int pin
)
188 if (readl(IFXMIPS_GPIO_P0_IN
+ (port
* 0x30)) & (1 << pin
))
193 EXPORT_SYMBOL(ifxmips_port_get_input
);
196 ifxmips_port_set_altsel0 (unsigned int port
, unsigned int pin
)
199 writel(readl(IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0x30)) | (1 << pin
), IFXMIPS_GPIO_P0_ALTSEL0
);
203 EXPORT_SYMBOL(ifxmips_port_set_altsel0
);
206 ifxmips_port_clear_altsel0 (unsigned int port
, unsigned int pin
)
209 writel(readl(IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0x30)) & ~(1 << pin
), IFXMIPS_GPIO_P0_ALTSEL0
);
213 EXPORT_SYMBOL(ifxmips_port_clear_altsel0
);
216 ifxmips_port_set_altsel1 (unsigned int port
, unsigned int pin
)
219 writel(readl(IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0x30)) | (1 << pin
), IFXMIPS_GPIO_P0_ALTSEL1
);
223 EXPORT_SYMBOL(ifxmips_port_set_altsel1
);
226 ifxmips_port_clear_altsel1 (unsigned int port
, unsigned int pin
)
229 writel(readl(IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0x30)) & ~(1 << pin
), IFXMIPS_GPIO_P0_ALTSEL1
);
233 EXPORT_SYMBOL(ifxmips_port_clear_altsel1
);
235 long ifxmips_port_read_procmem_helper(char* tag
, u32
* in_reg
, char *buf
)
240 len
= sprintf(buf
, "\n%s: ", tag
);
243 for (t
= 0; t
< 32; t
++) {
245 len
= len
+ sprintf(buf
+ len
, "X");
247 len
= len
+ sprintf(buf
+ len
, " ");
255 ifxmips_port_read_procmem (char *buf
, char **start
, off_t offset
, int count
,
256 int *eof
, void *data
)
258 long len
= sprintf (buf
, "\nIFXMips Port Settings\n");
260 len
+= sprintf (buf
+ len
,
262 len
+= sprintf (buf
+ len
,
263 " 10987654321098765432109876543210\n");
264 len
+= sprintf (buf
+ len
,
265 "----------------------------------------\n");
267 len
+= ifxmips_port_read_procmem_helper("P0-OUT", IFXMIPS_GPIO_P0_OUT
, &buf
[len
]);
268 len
+= ifxmips_port_read_procmem_helper("P1-OUT", IFXMIPS_GPIO_P1_OUT
, &buf
[len
]);
269 len
+= ifxmips_port_read_procmem_helper("P0-IN ", IFXMIPS_GPIO_P0_IN
, &buf
[len
]);
270 len
+= ifxmips_port_read_procmem_helper("P1-IN ", IFXMIPS_GPIO_P1_IN
, &buf
[len
]);
271 len
+= ifxmips_port_read_procmem_helper("P0-DIR", IFXMIPS_GPIO_P0_DIR
, &buf
[len
]);
272 len
+= ifxmips_port_read_procmem_helper("P1-DIR", IFXMIPS_GPIO_P1_DIR
, &buf
[len
]);
273 len
+= ifxmips_port_read_procmem_helper("P0-STO ", IFXMIPS_GPIO_P0_STOFF
, &buf
[len
]);
274 len
+= ifxmips_port_read_procmem_helper("P1-STO ", IFXMIPS_GPIO_P1_STOFF
, &buf
[len
]);
275 len
+= ifxmips_port_read_procmem_helper("P0-PUDE", IFXMIPS_GPIO_P0_PUDEN
, &buf
[len
]);
276 len
+= ifxmips_port_read_procmem_helper("P1-PUDE", IFXMIPS_GPIO_P1_PUDEN
, &buf
[len
]);
277 len
+= ifxmips_port_read_procmem_helper("P0-OD ", IFXMIPS_GPIO_P0_OD
, &buf
[len
]);
278 len
+= ifxmips_port_read_procmem_helper("P1-OD ", IFXMIPS_GPIO_P1_OD
, &buf
[len
]);
279 len
+= ifxmips_port_read_procmem_helper("P0-PUDS", IFXMIPS_GPIO_P0_PUDSEL
, &buf
[len
]);
280 len
+= ifxmips_port_read_procmem_helper("P1-PUDS", IFXMIPS_GPIO_P1_PUDSEL
, &buf
[len
]);
281 len
+= ifxmips_port_read_procmem_helper("P0-ALT0", IFXMIPS_GPIO_P0_ALTSEL0
, &buf
[len
]);
282 len
+= ifxmips_port_read_procmem_helper("P1-ALT0", IFXMIPS_GPIO_P1_ALTSEL0
, &buf
[len
]);
283 len
+= ifxmips_port_read_procmem_helper("P0-ALT1", IFXMIPS_GPIO_P0_ALTSEL1
, &buf
[len
]);
284 len
+= ifxmips_port_read_procmem_helper("P1-ALT1", IFXMIPS_GPIO_P1_ALTSEL1
, &buf
[len
]);
285 len
= len
+ sprintf (buf
+ len
, "\n\n");
293 ifxmips_port_open (struct inode
*inode
, struct file
*filep
)
299 ifxmips_port_release (struct inode
*inode
, struct file
*filelp
)
305 ifxmips_port_ioctl (struct inode
*inode
, struct file
*filp
,
306 unsigned int cmd
, unsigned long arg
)
309 volatile struct ifxmips_port_ioctl_parm parm
;
311 if (_IOC_TYPE (cmd
) != IFXMIPS_PORT_IOC_MAGIC
)
314 if (_IOC_DIR (cmd
) & _IOC_WRITE
) {
317 sizeof (struct ifxmips_port_ioctl_parm
)))
319 ret
= copy_from_user ((void *) &parm
, (void *) arg
,
320 sizeof (struct ifxmips_port_ioctl_parm
));
322 if (_IOC_DIR (cmd
) & _IOC_READ
) {
325 sizeof (struct ifxmips_port_ioctl_parm
)))
329 if (down_trylock (&port_sem
) != 0)
333 case IFXMIPS_PORT_IOCOD
:
334 if (parm
.value
== 0x00)
335 ifxmips_port_clear_open_drain(parm
.port
, parm
.pin
);
337 ifxmips_port_set_open_drain(parm
.port
, parm
.pin
);
340 case IFXMIPS_PORT_IOCPUDSEL
:
341 if (parm
.value
== 0x00)
342 ifxmips_port_clear_pudsel(parm
.port
, parm
.pin
);
344 ifxmips_port_set_pudsel(parm
.port
, parm
.pin
);
347 case IFXMIPS_PORT_IOCPUDEN
:
348 if (parm
.value
== 0x00)
349 ifxmips_port_clear_puden(parm
.port
, parm
.pin
);
351 ifxmips_port_set_puden(parm
.port
, parm
.pin
);
354 case IFXMIPS_PORT_IOCSTOFF
:
355 if (parm
.value
== 0x00)
356 ifxmips_port_clear_stoff(parm
.port
, parm
.pin
);
358 ifxmips_port_set_stoff(parm
.port
, parm
.pin
);
361 case IFXMIPS_PORT_IOCDIR
:
362 if (parm
.value
== 0x00)
363 ifxmips_port_set_dir_in(parm
.port
, parm
.pin
);
365 ifxmips_port_set_dir_out(parm
.port
, parm
.pin
);
368 case IFXMIPS_PORT_IOCOUTPUT
:
369 if (parm
.value
== 0x00)
370 ifxmips_port_clear_output(parm
.port
, parm
.pin
);
372 ifxmips_port_set_output(parm
.port
, parm
.pin
);
375 case IFXMIPS_PORT_IOCALTSEL0
:
376 if (parm
.value
== 0x00)
377 ifxmips_port_clear_altsel0(parm
.port
, parm
.pin
);
379 ifxmips_port_set_altsel0(parm
.port
, parm
.pin
);
382 case IFXMIPS_PORT_IOCALTSEL1
:
383 if (parm
.value
== 0x00)
384 ifxmips_port_clear_altsel1(parm
.port
, parm
.pin
);
386 ifxmips_port_set_altsel1(parm
.port
, parm
.pin
);
389 case IFXMIPS_PORT_IOCINPUT
:
390 parm
.value
= ifxmips_port_get_input(parm
.port
, parm
.pin
);
391 copy_to_user((void*)arg
, (void*)&parm
,
392 sizeof(struct ifxmips_port_ioctl_parm
));
404 static struct file_operations port_fops
= {
405 .open
= ifxmips_port_open
,
406 .release
= ifxmips_port_release
,
407 .ioctl
= ifxmips_port_ioctl
411 ifxmips_gpio_init (void)
415 sema_init (&port_sem
, 1);
417 ifxmips_gpio_major
= register_chrdev(0, "ifxmips_gpio", &port_fops
);
418 if (!ifxmips_gpio_major
)
420 printk("ifxmips-port: Error! Could not register port device. #%d\n", ifxmips_gpio_major
);
425 create_proc_read_entry("ifxmips_gpio", 0, NULL
,
426 ifxmips_port_read_procmem
, NULL
);
428 printk("registered ifxmips gpio driver\n");
435 ifxmips_gpio_exit (void)
437 unregister_chrdev(ifxmips_gpio_major
, "ifxmips_gpio");
438 remove_proc_entry("ifxmips_gpio", NULL
);
441 module_init(ifxmips_gpio_init
);
442 module_exit(ifxmips_gpio_exit
);