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/danube/danube.h>
30 #include <asm/danube/danube_ioctl.h>
33 #define PINS_PER_PORT 16
35 static unsigned int danube_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 DANUBE_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
44 danube_port_reserve_pin (unsigned int port, unsigned int pin)
47 printk("%s : call to obseleted function\n", __func__);
51 EXPORT_SYMBOL(danube_port_reserve_pin);
54 danube_port_free_pin (unsigned int port, unsigned int pin)
57 printk("%s : call to obseleted function\n", __func__);
61 EXPORT_SYMBOL(danube_port_free_pin);
64 danube_port_set_open_drain (unsigned int port, unsigned int pin)
67 writel(readl(DANUBE_GPIO_P0_OD + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_OD);
71 EXPORT_SYMBOL(danube_port_set_open_drain);
74 danube_port_clear_open_drain (unsigned int port, unsigned int pin)
77 writel(readl(DANUBE_GPIO_P0_OD + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_OD);
81 EXPORT_SYMBOL(danube_port_clear_open_drain);
84 danube_port_set_pudsel (unsigned int port, unsigned int pin)
87 writel(readl(DANUBE_GPIO_P0_PUDSEL + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_PUDSEL);
91 EXPORT_SYMBOL(danube_port_set_pudsel);
94 danube_port_clear_pudsel (unsigned int port, unsigned int pin)
97 writel(readl(DANUBE_GPIO_P0_PUDSEL + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_PUDSEL);
101 EXPORT_SYMBOL(danube_port_clear_pudsel);
104 danube_port_set_puden (unsigned int port, unsigned int pin)
107 writel(readl(DANUBE_GPIO_P0_PUDEN + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_PUDEN);
111 EXPORT_SYMBOL(danube_port_set_puden);
114 danube_port_clear_puden (unsigned int port, unsigned int pin)
117 writel(readl(DANUBE_GPIO_P0_PUDEN + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_PUDEN);
121 EXPORT_SYMBOL(danube_port_clear_puden);
124 danube_port_set_stoff (unsigned int port, unsigned int pin)
127 writel(readl(DANUBE_GPIO_P0_STOFF + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_STOFF);
131 EXPORT_SYMBOL(danube_port_set_stoff);
134 danube_port_clear_stoff (unsigned int port, unsigned int pin)
137 writel(readl(DANUBE_GPIO_P0_STOFF + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_STOFF);
141 EXPORT_SYMBOL(danube_port_clear_stoff);
144 danube_port_set_dir_out (unsigned int port, unsigned int pin)
147 writel(readl(DANUBE_GPIO_P0_DIR + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_DIR);
151 EXPORT_SYMBOL(danube_port_set_dir_out);
154 danube_port_set_dir_in (unsigned int port, unsigned int pin)
157 writel(readl(DANUBE_GPIO_P0_DIR + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_DIR);
161 EXPORT_SYMBOL(danube_port_set_dir_in);
164 danube_port_set_output (unsigned int port, unsigned int pin)
167 writel(readl(DANUBE_GPIO_P0_OUT + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_OUT);
171 EXPORT_SYMBOL(danube_port_set_output);
174 danube_port_clear_output (unsigned int port, unsigned int pin)
177 writel(readl(DANUBE_GPIO_P0_OUT + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_OUT);
181 EXPORT_SYMBOL(danube_port_clear_output);
184 danube_port_get_input (unsigned int port, unsigned int pin)
188 if (readl(DANUBE_GPIO_P0_IN + (port * 0x30)) & (1 << pin))
193 EXPORT_SYMBOL(danube_port_get_input);
196 danube_port_set_altsel0 (unsigned int port, unsigned int pin)
199 writel(readl(DANUBE_GPIO_P0_ALTSEL0 + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_ALTSEL0);
203 EXPORT_SYMBOL(danube_port_set_altsel0);
206 danube_port_clear_altsel0 (unsigned int port, unsigned int pin)
209 writel(readl(DANUBE_GPIO_P0_ALTSEL0 + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_ALTSEL0);
213 EXPORT_SYMBOL(danube_port_clear_altsel0);
216 danube_port_set_altsel1 (unsigned int port, unsigned int pin)
219 writel(readl(DANUBE_GPIO_P0_ALTSEL1 + (port * 0x30)) | (1 << pin), DANUBE_GPIO_P0_ALTSEL1);
223 EXPORT_SYMBOL(danube_port_set_altsel1);
226 danube_port_clear_altsel1 (unsigned int port, unsigned int pin)
229 writel(readl(DANUBE_GPIO_P0_ALTSEL1 + (port * 0x30)) & ~(1 << pin), DANUBE_GPIO_P0_ALTSEL1);
233 EXPORT_SYMBOL(danube_port_clear_altsel1);
235 long danube_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 danube_port_read_procmem (char *buf, char **start, off_t offset, int count,
256 int *eof, void *data)
258 long len = sprintf (buf, "\nDanube 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 += danube_port_read_procmem_helper("P0-OUT", DANUBE_GPIO_P0_OUT, &buf[len]);
268 len += danube_port_read_procmem_helper("P1-OUT", DANUBE_GPIO_P1_OUT, &buf[len]);
269 len += danube_port_read_procmem_helper("P0-IN ", DANUBE_GPIO_P0_IN, &buf[len]);
270 len += danube_port_read_procmem_helper("P1-IN ", DANUBE_GPIO_P1_IN, &buf[len]);
271 len += danube_port_read_procmem_helper("P0-DIR", DANUBE_GPIO_P0_DIR, &buf[len]);
272 len += danube_port_read_procmem_helper("P1-DIR", DANUBE_GPIO_P1_DIR, &buf[len]);
273 len += danube_port_read_procmem_helper("P0-STO ", DANUBE_GPIO_P0_STOFF, &buf[len]);
274 len += danube_port_read_procmem_helper("P1-STO ", DANUBE_GPIO_P1_STOFF, &buf[len]);
275 len += danube_port_read_procmem_helper("P0-PUDE", DANUBE_GPIO_P0_PUDEN, &buf[len]);
276 len += danube_port_read_procmem_helper("P1-PUDE", DANUBE_GPIO_P1_PUDEN, &buf[len]);
277 len += danube_port_read_procmem_helper("P0-OD ", DANUBE_GPIO_P0_OD, &buf[len]);
278 len += danube_port_read_procmem_helper("P1-OD ", DANUBE_GPIO_P1_OD, &buf[len]);
279 len += danube_port_read_procmem_helper("P0-PUDS", DANUBE_GPIO_P0_PUDSEL, &buf[len]);
280 len += danube_port_read_procmem_helper("P1-PUDS", DANUBE_GPIO_P1_PUDSEL, &buf[len]);
281 len += danube_port_read_procmem_helper("P0-ALT0", DANUBE_GPIO_P0_ALTSEL0, &buf[len]);
282 len += danube_port_read_procmem_helper("P1-ALT0", DANUBE_GPIO_P1_ALTSEL0, &buf[len]);
283 len += danube_port_read_procmem_helper("P0-ALT1", DANUBE_GPIO_P0_ALTSEL1, &buf[len]);
284 len += danube_port_read_procmem_helper("P1-ALT1", DANUBE_GPIO_P1_ALTSEL1, &buf[len]);
285 len = len + sprintf (buf + len, "\n\n");
293 danube_port_open (struct inode *inode, struct file *filep)
299 danube_port_release (struct inode *inode, struct file *filelp)
305 danube_port_ioctl (struct inode *inode, struct file *filp,
306 unsigned int cmd, unsigned long arg)
309 volatile struct danube_port_ioctl_parm parm;
311 if (_IOC_TYPE (cmd) != DANUBE_PORT_IOC_MAGIC)
314 if (_IOC_DIR (cmd) & _IOC_WRITE) {
317 sizeof (struct danube_port_ioctl_parm)))
319 ret = copy_from_user ((void *) &parm, (void *) arg,
320 sizeof (struct danube_port_ioctl_parm));
322 if (_IOC_DIR (cmd) & _IOC_READ) {
325 sizeof (struct danube_port_ioctl_parm)))
329 if (down_trylock (&port_sem) != 0)
333 case DANUBE_PORT_IOCOD:
334 if (parm.value == 0x00)
335 danube_port_clear_open_drain(parm.port, parm.pin);
337 danube_port_set_open_drain(parm.port, parm.pin);
340 case DANUBE_PORT_IOCPUDSEL:
341 if (parm.value == 0x00)
342 danube_port_clear_pudsel(parm.port, parm.pin);
344 danube_port_set_pudsel(parm.port, parm.pin);
347 case DANUBE_PORT_IOCPUDEN:
348 if (parm.value == 0x00)
349 danube_port_clear_puden(parm.port, parm.pin);
351 danube_port_set_puden(parm.port, parm.pin);
354 case DANUBE_PORT_IOCSTOFF:
355 if (parm.value == 0x00)
356 danube_port_clear_stoff(parm.port, parm.pin);
358 danube_port_set_stoff(parm.port, parm.pin);
361 case DANUBE_PORT_IOCDIR:
362 if (parm.value == 0x00)
363 danube_port_set_dir_in(parm.port, parm.pin);
365 danube_port_set_dir_out(parm.port, parm.pin);
368 case DANUBE_PORT_IOCOUTPUT:
369 if (parm.value == 0x00)
370 danube_port_clear_output(parm.port, parm.pin);
372 danube_port_set_output(parm.port, parm.pin);
375 case DANUBE_PORT_IOCALTSEL0:
376 if (parm.value == 0x00)
377 danube_port_clear_altsel0(parm.port, parm.pin);
379 danube_port_set_altsel0(parm.port, parm.pin);
382 case DANUBE_PORT_IOCALTSEL1:
383 if (parm.value == 0x00)
384 danube_port_clear_altsel1(parm.port, parm.pin);
386 danube_port_set_altsel1(parm.port, parm.pin);
389 case DANUBE_PORT_IOCINPUT:
390 parm.value = danube_port_get_input(parm.port, parm.pin);
391 copy_to_user((void*)arg, (void*)&parm,
392 sizeof(struct danube_port_ioctl_parm));
404 static struct file_operations port_fops = {
405 .open = danube_port_open,
406 .release = danube_port_release,
407 .ioctl = danube_port_ioctl
411 danube_gpio_init (void)
415 sema_init (&port_sem, 1);
417 danube_gpio_major = register_chrdev(0, "danube_gpio", &port_fops);
418 if (!danube_gpio_major)
420 printk("danube-port: Error! Could not register port device. #%d\n", danube_gpio_major);
425 create_proc_read_entry("danube_gpio", 0, NULL,
426 danube_port_read_procmem, NULL);
428 printk("registered danube gpio driver\n");
435 danube_gpio_exit (void)
437 unregister_chrdev(danube_gpio_major, "danube_gpio");
438 remove_proc_entry("danube_gpio", NULL);
441 module_init(danube_gpio_init);
442 module_exit(danube_gpio_exit);