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_param(major, int, 0);
27 MODULE_PARM_DESC(major, "Major device number");
29 static ssize_t scx200_gpio_write(struct file *file, const char __user *data,
30 size_t len, loff_t *ppos)
32 unsigned m = iminor(file->f_dentry->d_inode);
35 for (i = 0; i < len; ++i) {
37 if (get_user(c, data+i))
42 scx200_gpio_set(m, 0);
45 scx200_gpio_set(m, 1);
48 printk(KERN_INFO NAME ": GPIO%d output enabled\n", m);
49 scx200_gpio_configure(m, ~1, 1);
52 printk(KERN_INFO NAME ": GPIO%d output disabled\n", m);
53 scx200_gpio_configure(m, ~1, 0);
56 printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m);
57 scx200_gpio_configure(m, ~2, 2);
60 printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m);
61 scx200_gpio_configure(m, ~2, 0);
64 printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m);
65 scx200_gpio_configure(m, ~4, 4);
68 printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m);
69 scx200_gpio_configure(m, ~4, 0);
77 static ssize_t scx200_gpio_read(struct file *file, char __user *buf,
78 size_t len, loff_t *ppos)
80 unsigned m = iminor(file->f_dentry->d_inode);
83 value = scx200_gpio_get(m);
84 if (put_user(value ? '1' : '0', buf))
90 static int scx200_gpio_open(struct inode *inode, struct file *file)
92 unsigned m = iminor(inode);
95 return nonseekable_open(inode, file);
98 static int scx200_gpio_release(struct inode *inode, struct file *file)
104 static struct file_operations scx200_gpio_fops = {
105 .owner = THIS_MODULE,
106 .write = scx200_gpio_write,
107 .read = scx200_gpio_read,
108 .open = scx200_gpio_open,
109 .release = scx200_gpio_release,
112 static int __init scx200_gpio_init(void)
116 printk(KERN_DEBUG NAME ": NatSemi SCx200 GPIO Driver\n");
118 if (!scx200_gpio_present()) {
119 printk(KERN_ERR NAME ": no SCx200 gpio pins available\n");
123 r = register_chrdev(major, NAME, &scx200_gpio_fops);
125 printk(KERN_ERR NAME ": unable to register character device\n");
130 printk(KERN_DEBUG NAME ": got dynamic major %d\n", major);
136 static void __exit scx200_gpio_cleanup(void)
138 unregister_chrdev(major, NAME);
141 module_init(scx200_gpio_init);
142 module_exit(scx200_gpio_cleanup);
146 compile-command: "make -k -C ../.. SUBDIRS=drivers/char modules"