blob: a1a56c5c8a84be6908d42aeaa8a0b63d257ae3a8 [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>
23
24#define NAME "scx200_gpio"
Jim Cromie979b5ec2006-06-27 02:54:14 -070025#define DEVNAME NAME
26
27static struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
30MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver");
31MODULE_LICENSE("GPL");
32
33static int major = 0; /* default to dynamic major */
34module_param(major, int, 0);
35MODULE_PARM_DESC(major, "Major device number");
36
Jim Cromie7d7f2122006-06-27 02:54:14 -070037extern void scx200_gpio_dump(unsigned index);
38
Jim Cromie62c83cd2006-06-27 02:54:13 -070039static ssize_t scx200_gpio_write(struct file *file, const char __user *data,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 size_t len, loff_t *ppos)
41{
42 unsigned m = iminor(file->f_dentry->d_inode);
43 size_t i;
Jim Cromie9550a332006-06-27 02:54:16 -070044 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 for (i = 0; i < len; ++i) {
47 char c;
Jim Cromie62c83cd2006-06-27 02:54:13 -070048 if (get_user(c, data + i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return -EFAULT;
Jim Cromie62c83cd2006-06-27 02:54:13 -070050 switch (c) {
51 case '0':
52 scx200_gpio_set(m, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 break;
Jim Cromie62c83cd2006-06-27 02:54:13 -070054 case '1':
55 scx200_gpio_set(m, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 break;
57 case 'O':
58 printk(KERN_INFO NAME ": GPIO%d output enabled\n", m);
59 scx200_gpio_configure(m, ~1, 1);
60 break;
61 case 'o':
62 printk(KERN_INFO NAME ": GPIO%d output disabled\n", m);
63 scx200_gpio_configure(m, ~1, 0);
64 break;
65 case 'T':
66 printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m);
67 scx200_gpio_configure(m, ~2, 2);
68 break;
69 case 't':
70 printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m);
71 scx200_gpio_configure(m, ~2, 0);
72 break;
73 case 'P':
74 printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m);
75 scx200_gpio_configure(m, ~4, 4);
76 break;
77 case 'p':
78 printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m);
79 scx200_gpio_configure(m, ~4, 0);
80 break;
Jim Cromie9550a332006-06-27 02:54:16 -070081
82 case 'v':
83 /* View Current pin settings */
84 scx200_gpio_dump(m);
85 break;
86 case '\n':
87 /* end of settings string, do nothing */
88 break;
89 default:
90 printk(KERN_ERR NAME
91 ": GPIO-%2d bad setting: chr<0x%2x>\n", m,
92 (int)c);
93 err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95 }
Jim Cromie9550a332006-06-27 02:54:16 -070096 if (err)
97 return -EINVAL; /* full string handled, report error */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 return len;
100}
101
102static ssize_t scx200_gpio_read(struct file *file, char __user *buf,
103 size_t len, loff_t *ppos)
104{
105 unsigned m = iminor(file->f_dentry->d_inode);
106 int value;
107
108 value = scx200_gpio_get(m);
109 if (put_user(value ? '1' : '0', buf))
110 return -EFAULT;
Jim Cromie62c83cd2006-06-27 02:54:13 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 1;
113}
114
115static int scx200_gpio_open(struct inode *inode, struct file *file)
116{
117 unsigned m = iminor(inode);
118 if (m > 63)
119 return -EINVAL;
120 return nonseekable_open(inode, file);
121}
122
123static int scx200_gpio_release(struct inode *inode, struct file *file)
124{
125 return 0;
126}
127
128
129static struct file_operations scx200_gpio_fops = {
130 .owner = THIS_MODULE,
131 .write = scx200_gpio_write,
132 .read = scx200_gpio_read,
133 .open = scx200_gpio_open,
134 .release = scx200_gpio_release,
135};
136
Jim Cromie7d7f2122006-06-27 02:54:14 -0700137struct cdev *scx200_devices;
Jim Cromie979b5ec2006-06-27 02:54:14 -0700138static int num_pins = 32;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static int __init scx200_gpio_init(void)
141{
Jim Cromie7d7f2122006-06-27 02:54:14 -0700142 int rc, i;
143 dev_t dev = MKDEV(major, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (!scx200_gpio_present()) {
Jim Cromie7d7f2122006-06-27 02:54:14 -0700146 printk(KERN_ERR NAME ": no SCx200 gpio present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -ENODEV;
148 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700149
150 /* support dev_dbg() with pdev->dev */
151 pdev = platform_device_alloc(DEVNAME, 0);
152 if (!pdev)
153 return -ENOMEM;
154
155 rc = platform_device_add(pdev);
156 if (rc)
157 goto undo_malloc;
158
Jim Cromie7d7f2122006-06-27 02:54:14 -0700159 if (major)
Jim Cromie979b5ec2006-06-27 02:54:14 -0700160 rc = register_chrdev_region(dev, num_pins, "scx200_gpio");
Jim Cromie7d7f2122006-06-27 02:54:14 -0700161 else {
Jim Cromie979b5ec2006-06-27 02:54:14 -0700162 rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio");
Jim Cromie7d7f2122006-06-27 02:54:14 -0700163 major = MAJOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Jim Cromie7d7f2122006-06-27 02:54:14 -0700165 if (rc < 0) {
Jim Cromie979b5ec2006-06-27 02:54:14 -0700166 dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
167 goto undo_platform_device_add;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700168 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700169 scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL);
Jim Cromie7d7f2122006-06-27 02:54:14 -0700170 if (!scx200_devices) {
171 rc = -ENOMEM;
Jim Cromie979b5ec2006-06-27 02:54:14 -0700172 goto undo_chrdev_region;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700173 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700174 for (i = 0; i < num_pins; i++) {
Jim Cromie7d7f2122006-06-27 02:54:14 -0700175 struct cdev *cdev = &scx200_devices[i];
176 cdev_init(cdev, &scx200_gpio_fops);
177 cdev->owner = THIS_MODULE;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700178 rc = cdev_add(cdev, MKDEV(major, i), 1);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700179 /* tolerate 'minor' errors */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700180 if (rc)
Jim Cromie979b5ec2006-06-27 02:54:14 -0700181 dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183
Jim Cromie979b5ec2006-06-27 02:54:14 -0700184 return 0; /* succeed */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700185
Jim Cromie979b5ec2006-06-27 02:54:14 -0700186undo_chrdev_region:
187 unregister_chrdev_region(dev, num_pins);
188undo_platform_device_add:
189 platform_device_put(pdev);
190undo_malloc:
191 kfree(pdev);
Jim Cromie7d7f2122006-06-27 02:54:14 -0700192 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static void __exit scx200_gpio_cleanup(void)
196{
Jim Cromie7d7f2122006-06-27 02:54:14 -0700197 kfree(scx200_devices);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700198 unregister_chrdev_region(MKDEV(major, 0), num_pins);
199 platform_device_put(pdev);
200 platform_device_unregister(pdev);
201 /* kfree(pdev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204module_init(scx200_gpio_init);
205module_exit(scx200_gpio_cleanup);