blob: 507a5bf567cbf61d7dce71e5b3bd1946df8e3ddc [file] [log] [blame]
Jim Cromie62c83cd2006-06-27 02:54:13 -07001/* linux/drivers/char/scx200_gpio.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3 National Semiconductor SCx200 GPIO driver. Allows a user space
4 process to play with the GPIO pins.
5
6 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */
7
8#include <linux/config.h>
9#include <linux/fs.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>
15#include <asm/io.h>
16
Jim Cromie7d7f2122006-06-27 02:54:14 -070017#include <linux/types.h>
18#include <linux/cdev.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/scx200_gpio.h>
21
22#define NAME "scx200_gpio"
23
24MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
25MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver");
26MODULE_LICENSE("GPL");
27
28static int major = 0; /* default to dynamic major */
29module_param(major, int, 0);
30MODULE_PARM_DESC(major, "Major device number");
31
Jim Cromie7d7f2122006-06-27 02:54:14 -070032extern void scx200_gpio_dump(unsigned index);
33
Jim Cromie62c83cd2006-06-27 02:54:13 -070034static ssize_t scx200_gpio_write(struct file *file, const char __user *data,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 size_t len, loff_t *ppos)
36{
37 unsigned m = iminor(file->f_dentry->d_inode);
38 size_t i;
39
40 for (i = 0; i < len; ++i) {
41 char c;
Jim Cromie62c83cd2006-06-27 02:54:13 -070042 if (get_user(c, data + i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return -EFAULT;
Jim Cromie62c83cd2006-06-27 02:54:13 -070044 switch (c) {
45 case '0':
46 scx200_gpio_set(m, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 break;
Jim Cromie62c83cd2006-06-27 02:54:13 -070048 case '1':
49 scx200_gpio_set(m, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 break;
51 case 'O':
52 printk(KERN_INFO NAME ": GPIO%d output enabled\n", m);
53 scx200_gpio_configure(m, ~1, 1);
54 break;
55 case 'o':
56 printk(KERN_INFO NAME ": GPIO%d output disabled\n", m);
57 scx200_gpio_configure(m, ~1, 0);
58 break;
59 case 'T':
60 printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m);
61 scx200_gpio_configure(m, ~2, 2);
62 break;
63 case 't':
64 printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m);
65 scx200_gpio_configure(m, ~2, 0);
66 break;
67 case 'P':
68 printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m);
69 scx200_gpio_configure(m, ~4, 4);
70 break;
71 case 'p':
72 printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m);
73 scx200_gpio_configure(m, ~4, 0);
74 break;
75 }
76 }
77
78 return len;
79}
80
81static ssize_t scx200_gpio_read(struct file *file, char __user *buf,
82 size_t len, loff_t *ppos)
83{
84 unsigned m = iminor(file->f_dentry->d_inode);
85 int value;
86
87 value = scx200_gpio_get(m);
88 if (put_user(value ? '1' : '0', buf))
89 return -EFAULT;
Jim Cromie62c83cd2006-06-27 02:54:13 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 1;
92}
93
94static int scx200_gpio_open(struct inode *inode, struct file *file)
95{
96 unsigned m = iminor(inode);
97 if (m > 63)
98 return -EINVAL;
99 return nonseekable_open(inode, file);
100}
101
102static int scx200_gpio_release(struct inode *inode, struct file *file)
103{
104 return 0;
105}
106
107
108static struct file_operations scx200_gpio_fops = {
109 .owner = THIS_MODULE,
110 .write = scx200_gpio_write,
111 .read = scx200_gpio_read,
112 .open = scx200_gpio_open,
113 .release = scx200_gpio_release,
114};
115
Jim Cromie7d7f2122006-06-27 02:54:14 -0700116struct cdev *scx200_devices;
117int num_devs = 32;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int __init scx200_gpio_init(void)
120{
Jim Cromie7d7f2122006-06-27 02:54:14 -0700121 int rc, i;
122 dev_t dev = MKDEV(major, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 printk(KERN_DEBUG NAME ": NatSemi SCx200 GPIO Driver\n");
125
126 if (!scx200_gpio_present()) {
Jim Cromie7d7f2122006-06-27 02:54:14 -0700127 printk(KERN_ERR NAME ": no SCx200 gpio present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -ENODEV;
129 }
Jim Cromie7d7f2122006-06-27 02:54:14 -0700130 if (major)
131 rc = register_chrdev_region(dev, num_devs, "scx200_gpio");
132 else {
133 rc = alloc_chrdev_region(&dev, 0, num_devs, "scx200_gpio");
134 major = MAJOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Jim Cromie7d7f2122006-06-27 02:54:14 -0700136 if (rc < 0) {
137 printk(KERN_ERR NAME ": SCx200 chrdev_region: %d\n", rc);
138 return rc;
139 }
140 scx200_devices = kzalloc(num_devs * sizeof(struct cdev), GFP_KERNEL);
141 if (!scx200_devices) {
142 rc = -ENOMEM;
143 goto fail_malloc;
144 }
145 for (i = 0; i < num_devs; i++) {
146 struct cdev *cdev = &scx200_devices[i];
147 cdev_init(cdev, &scx200_gpio_fops);
148 cdev->owner = THIS_MODULE;
149 cdev->ops = &scx200_gpio_fops;
150 rc = cdev_add(cdev, MKDEV(major, i), 1);
151 /* Fail gracefully if need be */
152 if (rc)
153 printk(KERN_ERR NAME "Error %d on minor %d", rc, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
Jim Cromie7d7f2122006-06-27 02:54:14 -0700156 return 0; /* succeed */
157
158fail_malloc:
159 unregister_chrdev_region(dev, num_devs);
160 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163static void __exit scx200_gpio_cleanup(void)
164{
Jim Cromie7d7f2122006-06-27 02:54:14 -0700165 kfree(scx200_devices);
166 unregister_chrdev_region(MKDEV(major, 0), num_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169module_init(scx200_gpio_init);
170module_exit(scx200_gpio_cleanup);