blob: 15dfb95ebc7ec0720524af1ab82350ab6cd92863 [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>
Jim Cromie979b5ec2006-06-27 02:54:14 -07009#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fs.h>
11#include <linux/module.h>
12#include <linux/errno.h>
13#include <linux/kernel.h>
14#include <linux/init.h>
Jim Cromie979b5ec2006-06-27 02:54:14 -070015#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
17#include <asm/io.h>
18
Jim Cromie7d7f2122006-06-27 02:54:14 -070019#include <linux/types.h>
20#include <linux/cdev.h>
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/scx200_gpio.h>
Jim Cromiefe3a1682006-06-27 02:54:18 -070023#include <linux/nsc_gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define NAME "scx200_gpio"
Jim Cromie979b5ec2006-06-27 02:54:14 -070026#define DEVNAME NAME
27
28static struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
31MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver");
32MODULE_LICENSE("GPL");
33
34static int major = 0; /* default to dynamic major */
35module_param(major, int, 0);
36MODULE_PARM_DESC(major, "Major device number");
37
Jim Cromie7d7f2122006-06-27 02:54:14 -070038extern void scx200_gpio_dump(unsigned index);
39
Jim Cromiefe3a1682006-06-27 02:54:18 -070040struct nsc_gpio_ops scx200_access = {
41 .owner = THIS_MODULE,
42 .gpio_config = scx200_gpio_configure,
43 .gpio_dump = scx200_gpio_dump,
44 .gpio_get = scx200_gpio_get,
45 .gpio_set = scx200_gpio_set,
46 .gpio_set_high = scx200_gpio_set_high,
47 .gpio_set_low = scx200_gpio_set_low,
48 .gpio_change = scx200_gpio_change,
49 .gpio_current = scx200_gpio_current
50};
51
Jim Cromie62c83cd2006-06-27 02:54:13 -070052static ssize_t scx200_gpio_write(struct file *file, const char __user *data,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 size_t len, loff_t *ppos)
54{
55 unsigned m = iminor(file->f_dentry->d_inode);
Jim Cromiec3dc8072006-06-27 02:54:18 -070056 struct nsc_gpio_ops *amp = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 size_t i;
Jim Cromie9550a332006-06-27 02:54:16 -070058 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 for (i = 0; i < len; ++i) {
61 char c;
Jim Cromie62c83cd2006-06-27 02:54:13 -070062 if (get_user(c, data + i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return -EFAULT;
Jim Cromie62c83cd2006-06-27 02:54:13 -070064 switch (c) {
65 case '0':
Jim Cromiec3dc8072006-06-27 02:54:18 -070066 amp->gpio_set(m, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 break;
Jim Cromie62c83cd2006-06-27 02:54:13 -070068 case '1':
Jim Cromiec3dc8072006-06-27 02:54:18 -070069 amp->gpio_set(m, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 break;
71 case 'O':
72 printk(KERN_INFO NAME ": GPIO%d output enabled\n", m);
Jim Cromiec3dc8072006-06-27 02:54:18 -070073 amp->gpio_config(m, ~1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 case 'o':
76 printk(KERN_INFO NAME ": GPIO%d output disabled\n", m);
Jim Cromiec3dc8072006-06-27 02:54:18 -070077 amp->gpio_config(m, ~1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79 case 'T':
80 printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m);
Jim Cromiec3dc8072006-06-27 02:54:18 -070081 amp->gpio_config(m, ~2, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
83 case 't':
84 printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m);
Jim Cromiec3dc8072006-06-27 02:54:18 -070085 amp->gpio_config(m, ~2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 break;
87 case 'P':
88 printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m);
Jim Cromiec3dc8072006-06-27 02:54:18 -070089 amp->gpio_config(m, ~4, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 break;
91 case 'p':
92 printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m);
Jim Cromiec3dc8072006-06-27 02:54:18 -070093 amp->gpio_config(m, ~4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 break;
Jim Cromie9550a332006-06-27 02:54:16 -070095
96 case 'v':
97 /* View Current pin settings */
Jim Cromiec3dc8072006-06-27 02:54:18 -070098 amp->gpio_dump(m);
Jim Cromie9550a332006-06-27 02:54:16 -070099 break;
100 case '\n':
101 /* end of settings string, do nothing */
102 break;
103 default:
104 printk(KERN_ERR NAME
105 ": GPIO-%2d bad setting: chr<0x%2x>\n", m,
106 (int)c);
107 err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 }
Jim Cromie9550a332006-06-27 02:54:16 -0700110 if (err)
111 return -EINVAL; /* full string handled, report error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 return len;
114}
115
116static ssize_t scx200_gpio_read(struct file *file, char __user *buf,
117 size_t len, loff_t *ppos)
118{
119 unsigned m = iminor(file->f_dentry->d_inode);
120 int value;
Jim Cromiec3dc8072006-06-27 02:54:18 -0700121 struct nsc_gpio_ops *amp = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jim Cromiec3dc8072006-06-27 02:54:18 -0700123 value = amp->gpio_get(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (put_user(value ? '1' : '0', buf))
125 return -EFAULT;
Jim Cromie62c83cd2006-06-27 02:54:13 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return 1;
128}
129
130static int scx200_gpio_open(struct inode *inode, struct file *file)
131{
132 unsigned m = iminor(inode);
Jim Cromiec3dc8072006-06-27 02:54:18 -0700133 file->private_data = &scx200_access;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (m > 63)
136 return -EINVAL;
137 return nonseekable_open(inode, file);
138}
139
140static int scx200_gpio_release(struct inode *inode, struct file *file)
141{
142 return 0;
143}
144
145
146static struct file_operations scx200_gpio_fops = {
147 .owner = THIS_MODULE,
148 .write = scx200_gpio_write,
149 .read = scx200_gpio_read,
150 .open = scx200_gpio_open,
151 .release = scx200_gpio_release,
152};
153
Jim Cromie7d7f2122006-06-27 02:54:14 -0700154struct cdev *scx200_devices;
Jim Cromie979b5ec2006-06-27 02:54:14 -0700155static int num_pins = 32;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static int __init scx200_gpio_init(void)
158{
Jim Cromie7d7f2122006-06-27 02:54:14 -0700159 int rc, i;
160 dev_t dev = MKDEV(major, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!scx200_gpio_present()) {
Jim Cromie7d7f2122006-06-27 02:54:14 -0700163 printk(KERN_ERR NAME ": no SCx200 gpio present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -ENODEV;
165 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700166
167 /* support dev_dbg() with pdev->dev */
168 pdev = platform_device_alloc(DEVNAME, 0);
169 if (!pdev)
170 return -ENOMEM;
171
172 rc = platform_device_add(pdev);
173 if (rc)
174 goto undo_malloc;
175
Jim Cromie7d7f2122006-06-27 02:54:14 -0700176 if (major)
Jim Cromie979b5ec2006-06-27 02:54:14 -0700177 rc = register_chrdev_region(dev, num_pins, "scx200_gpio");
Jim Cromie7d7f2122006-06-27 02:54:14 -0700178 else {
Jim Cromie979b5ec2006-06-27 02:54:14 -0700179 rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio");
Jim Cromie7d7f2122006-06-27 02:54:14 -0700180 major = MAJOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Jim Cromie7d7f2122006-06-27 02:54:14 -0700182 if (rc < 0) {
Jim Cromie979b5ec2006-06-27 02:54:14 -0700183 dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
184 goto undo_platform_device_add;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700185 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700186 scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL);
Jim Cromie7d7f2122006-06-27 02:54:14 -0700187 if (!scx200_devices) {
188 rc = -ENOMEM;
Jim Cromie979b5ec2006-06-27 02:54:14 -0700189 goto undo_chrdev_region;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700190 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700191 for (i = 0; i < num_pins; i++) {
Jim Cromie7d7f2122006-06-27 02:54:14 -0700192 struct cdev *cdev = &scx200_devices[i];
193 cdev_init(cdev, &scx200_gpio_fops);
194 cdev->owner = THIS_MODULE;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700195 rc = cdev_add(cdev, MKDEV(major, i), 1);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700196 /* tolerate 'minor' errors */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700197 if (rc)
Jim Cromie979b5ec2006-06-27 02:54:14 -0700198 dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
Jim Cromie979b5ec2006-06-27 02:54:14 -0700201 return 0; /* succeed */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700202
Jim Cromie979b5ec2006-06-27 02:54:14 -0700203undo_chrdev_region:
204 unregister_chrdev_region(dev, num_pins);
205undo_platform_device_add:
206 platform_device_put(pdev);
207undo_malloc:
208 kfree(pdev);
Jim Cromie7d7f2122006-06-27 02:54:14 -0700209 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212static void __exit scx200_gpio_cleanup(void)
213{
Jim Cromie7d7f2122006-06-27 02:54:14 -0700214 kfree(scx200_devices);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700215 unregister_chrdev_region(MKDEV(major, 0), num_pins);
216 platform_device_put(pdev);
217 platform_device_unregister(pdev);
218 /* kfree(pdev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221module_init(scx200_gpio_init);
222module_exit(scx200_gpio_cleanup);