1 /* linux/drivers/char/scx200_gpio.c
3 National Semiconductor SCx200 GPIO driver. Allows a user space
4 process to play with the GPIO pins.
6 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */
8 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <asm/uaccess.h>
17 #include <linux/scx200_gpio.h>
19 #define NAME "scx200_gpio"
21 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
22 MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver");
23 MODULE_LICENSE("GPL");
25 static int major = 0; /* default to dynamic major */
26 MODULE_PARM(major, "i");
27 MODULE_PARM_DESC(major, "Major device number");
29 static ssize_t scx200_gpio_write(struct file *file, const char *data,
30 size_t len, loff_t *ppos)
32 unsigned m = iminor(file->f_dentry->d_inode);
35 if (ppos != &file->f_pos)
38 for (i = 0; i < len; ++i) {
40 if (get_user(c, data+i))
45 scx200_gpio_set(m, 0);
48 scx200_gpio_set(m, 1);
51 printk(KERN_INFO NAME ": GPIO%d output enabled\n", m);
52 scx200_gpio_configure(m, ~1, 1);
55 printk(KERN_INFO NAME ": GPIO%d output disabled\n", m);
56 scx200_gpio_configure(m, ~1, 0);
59 printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m);
60 scx200_gpio_configure(m, ~2, 2);
63 printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m);
64 scx200_gpio_configure(m, ~2, 0);
67 printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m);
68 scx200_gpio_configure(m, ~4, 4);
71 printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m);
72 scx200_gpio_configure(m, ~4, 0);
80 static ssize_t scx200_gpio_read(struct file *file, char *buf,
81 size_t len, loff_t *ppos)
83 unsigned m = iminor(file->f_dentry->d_inode);
86 if (ppos != &file->f_pos)
89 value = scx200_gpio_get(m);
90 if (put_user(value ? '1' : '0', buf))
96 static int scx200_gpio_open(struct inode *inode, struct file *file)
98 unsigned m = iminor(inode);
104 static int scx200_gpio_release(struct inode *inode, struct file *file)
110 static struct file_operations scx200_gpio_fops = {
111 .owner = THIS_MODULE,
112 .write = scx200_gpio_write,
113 .read = scx200_gpio_read,
114 .open = scx200_gpio_open,
115 .release = scx200_gpio_release,
118 static int __init scx200_gpio_init(void)
122 printk(KERN_DEBUG NAME ": NatSemi SCx200 GPIO Driver\n");
124 if (!scx200_gpio_present()) {
125 printk(KERN_ERR NAME ": no SCx200 gpio pins available\n");
129 r = register_chrdev(major, NAME, &scx200_gpio_fops);
131 printk(KERN_ERR NAME ": unable to register character device\n");
136 printk(KERN_DEBUG NAME ": got dynamic major %d\n", major);
142 static void __exit scx200_gpio_cleanup(void)
144 unregister_chrdev(major, NAME);
147 module_init(scx200_gpio_init);
148 module_exit(scx200_gpio_cleanup);
152 compile-command: "make -k -C ../.. SUBDIRS=drivers/char modules"