[PATCH] chardev: GPIO for SCx200 & PC-8736x: dispatch via vtable
[deliverable/linux.git] / drivers / char / scx200_gpio.c
1 /* linux/drivers/char/scx200_gpio.c
2
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/device.h>
10 #include <linux/fs.h>
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <asm/uaccess.h>
17 #include <asm/io.h>
18
19 #include <linux/types.h>
20 #include <linux/cdev.h>
21
22 #include <linux/scx200_gpio.h>
23 #include <linux/nsc_gpio.h>
24
25 #define NAME "scx200_gpio"
26 #define DEVNAME NAME
27
28 static struct platform_device *pdev;
29
30 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
31 MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver");
32 MODULE_LICENSE("GPL");
33
34 static int major = 0; /* default to dynamic major */
35 module_param(major, int, 0);
36 MODULE_PARM_DESC(major, "Major device number");
37
38 extern void scx200_gpio_dump(unsigned index);
39
40 struct 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
52 static ssize_t scx200_gpio_write(struct file *file, const char __user *data,
53 size_t len, loff_t *ppos)
54 {
55 unsigned m = iminor(file->f_dentry->d_inode);
56 struct nsc_gpio_ops *amp = file->private_data;
57 size_t i;
58 int err = 0;
59
60 for (i = 0; i < len; ++i) {
61 char c;
62 if (get_user(c, data + i))
63 return -EFAULT;
64 switch (c) {
65 case '0':
66 amp->gpio_set(m, 0);
67 break;
68 case '1':
69 amp->gpio_set(m, 1);
70 break;
71 case 'O':
72 printk(KERN_INFO NAME ": GPIO%d output enabled\n", m);
73 amp->gpio_config(m, ~1, 1);
74 break;
75 case 'o':
76 printk(KERN_INFO NAME ": GPIO%d output disabled\n", m);
77 amp->gpio_config(m, ~1, 0);
78 break;
79 case 'T':
80 printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m);
81 amp->gpio_config(m, ~2, 2);
82 break;
83 case 't':
84 printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m);
85 amp->gpio_config(m, ~2, 0);
86 break;
87 case 'P':
88 printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m);
89 amp->gpio_config(m, ~4, 4);
90 break;
91 case 'p':
92 printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m);
93 amp->gpio_config(m, ~4, 0);
94 break;
95
96 case 'v':
97 /* View Current pin settings */
98 amp->gpio_dump(m);
99 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++;
108 }
109 }
110 if (err)
111 return -EINVAL; /* full string handled, report error */
112
113 return len;
114 }
115
116 static 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;
121 struct nsc_gpio_ops *amp = file->private_data;
122
123 value = amp->gpio_get(m);
124 if (put_user(value ? '1' : '0', buf))
125 return -EFAULT;
126
127 return 1;
128 }
129
130 static int scx200_gpio_open(struct inode *inode, struct file *file)
131 {
132 unsigned m = iminor(inode);
133 file->private_data = &scx200_access;
134
135 if (m > 63)
136 return -EINVAL;
137 return nonseekable_open(inode, file);
138 }
139
140 static int scx200_gpio_release(struct inode *inode, struct file *file)
141 {
142 return 0;
143 }
144
145
146 static 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
154 struct cdev *scx200_devices;
155 static int num_pins = 32;
156
157 static int __init scx200_gpio_init(void)
158 {
159 int rc, i;
160 dev_t dev = MKDEV(major, 0);
161
162 if (!scx200_gpio_present()) {
163 printk(KERN_ERR NAME ": no SCx200 gpio present\n");
164 return -ENODEV;
165 }
166
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
176 if (major)
177 rc = register_chrdev_region(dev, num_pins, "scx200_gpio");
178 else {
179 rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio");
180 major = MAJOR(dev);
181 }
182 if (rc < 0) {
183 dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
184 goto undo_platform_device_add;
185 }
186 scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL);
187 if (!scx200_devices) {
188 rc = -ENOMEM;
189 goto undo_chrdev_region;
190 }
191 for (i = 0; i < num_pins; i++) {
192 struct cdev *cdev = &scx200_devices[i];
193 cdev_init(cdev, &scx200_gpio_fops);
194 cdev->owner = THIS_MODULE;
195 rc = cdev_add(cdev, MKDEV(major, i), 1);
196 /* tolerate 'minor' errors */
197 if (rc)
198 dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
199 }
200
201 return 0; /* succeed */
202
203 undo_chrdev_region:
204 unregister_chrdev_region(dev, num_pins);
205 undo_platform_device_add:
206 platform_device_put(pdev);
207 undo_malloc:
208 kfree(pdev);
209 return rc;
210 }
211
212 static void __exit scx200_gpio_cleanup(void)
213 {
214 kfree(scx200_devices);
215 unregister_chrdev_region(MKDEV(major, 0), num_pins);
216 platform_device_put(pdev);
217 platform_device_unregister(pdev);
218 /* kfree(pdev); */
219 }
220
221 module_init(scx200_gpio_init);
222 module_exit(scx200_gpio_cleanup);
This page took 0.051656 seconds and 6 git commands to generate.