Commit | Line | Data |
---|---|---|
62c83cde | 1 | /* linux/drivers/char/scx200_gpio.c |
1da177e4 LT |
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> | |
979b5ec3 | 9 | #include <linux/device.h> |
1da177e4 LT |
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> | |
979b5ec3 | 15 | #include <linux/platform_device.h> |
1da177e4 LT |
16 | #include <asm/uaccess.h> |
17 | #include <asm/io.h> | |
18 | ||
7d7f2126 JC |
19 | #include <linux/types.h> |
20 | #include <linux/cdev.h> | |
21 | ||
1da177e4 | 22 | #include <linux/scx200_gpio.h> |
fe3a168a | 23 | #include <linux/nsc_gpio.h> |
1da177e4 LT |
24 | |
25 | #define NAME "scx200_gpio" | |
979b5ec3 JC |
26 | #define DEVNAME NAME |
27 | ||
28 | static struct platform_device *pdev; | |
1da177e4 LT |
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 | ||
7d7f2126 JC |
38 | extern void scx200_gpio_dump(unsigned index); |
39 | ||
fe3a168a JC |
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 | ||
62c83cde | 52 | static ssize_t scx200_gpio_write(struct file *file, const char __user *data, |
1da177e4 LT |
53 | size_t len, loff_t *ppos) |
54 | { | |
55 | unsigned m = iminor(file->f_dentry->d_inode); | |
c3dc8071 | 56 | struct nsc_gpio_ops *amp = file->private_data; |
1da177e4 | 57 | size_t i; |
9550a339 | 58 | int err = 0; |
1da177e4 LT |
59 | |
60 | for (i = 0; i < len; ++i) { | |
61 | char c; | |
62c83cde | 62 | if (get_user(c, data + i)) |
1da177e4 | 63 | return -EFAULT; |
62c83cde JC |
64 | switch (c) { |
65 | case '0': | |
c3dc8071 | 66 | amp->gpio_set(m, 0); |
1da177e4 | 67 | break; |
62c83cde | 68 | case '1': |
c3dc8071 | 69 | amp->gpio_set(m, 1); |
1da177e4 LT |
70 | break; |
71 | case 'O': | |
72 | printk(KERN_INFO NAME ": GPIO%d output enabled\n", m); | |
c3dc8071 | 73 | amp->gpio_config(m, ~1, 1); |
1da177e4 LT |
74 | break; |
75 | case 'o': | |
76 | printk(KERN_INFO NAME ": GPIO%d output disabled\n", m); | |
c3dc8071 | 77 | amp->gpio_config(m, ~1, 0); |
1da177e4 LT |
78 | break; |
79 | case 'T': | |
80 | printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m); | |
c3dc8071 | 81 | amp->gpio_config(m, ~2, 2); |
1da177e4 LT |
82 | break; |
83 | case 't': | |
84 | printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m); | |
c3dc8071 | 85 | amp->gpio_config(m, ~2, 0); |
1da177e4 LT |
86 | break; |
87 | case 'P': | |
88 | printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m); | |
c3dc8071 | 89 | amp->gpio_config(m, ~4, 4); |
1da177e4 LT |
90 | break; |
91 | case 'p': | |
92 | printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m); | |
c3dc8071 | 93 | amp->gpio_config(m, ~4, 0); |
1da177e4 | 94 | break; |
9550a339 JC |
95 | |
96 | case 'v': | |
97 | /* View Current pin settings */ | |
c3dc8071 | 98 | amp->gpio_dump(m); |
9550a339 JC |
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++; | |
1da177e4 LT |
108 | } |
109 | } | |
9550a339 JC |
110 | if (err) |
111 | return -EINVAL; /* full string handled, report error */ | |
1da177e4 LT |
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; | |
c3dc8071 | 121 | struct nsc_gpio_ops *amp = file->private_data; |
1da177e4 | 122 | |
c3dc8071 | 123 | value = amp->gpio_get(m); |
1da177e4 LT |
124 | if (put_user(value ? '1' : '0', buf)) |
125 | return -EFAULT; | |
62c83cde | 126 | |
1da177e4 LT |
127 | return 1; |
128 | } | |
129 | ||
130 | static int scx200_gpio_open(struct inode *inode, struct file *file) | |
131 | { | |
132 | unsigned m = iminor(inode); | |
c3dc8071 JC |
133 | file->private_data = &scx200_access; |
134 | ||
1da177e4 LT |
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 | ||
7d7f2126 | 154 | struct cdev *scx200_devices; |
979b5ec3 | 155 | static int num_pins = 32; |
7d7f2126 | 156 | |
1da177e4 LT |
157 | static int __init scx200_gpio_init(void) |
158 | { | |
7d7f2126 JC |
159 | int rc, i; |
160 | dev_t dev = MKDEV(major, 0); | |
1da177e4 | 161 | |
1da177e4 | 162 | if (!scx200_gpio_present()) { |
7d7f2126 | 163 | printk(KERN_ERR NAME ": no SCx200 gpio present\n"); |
1da177e4 LT |
164 | return -ENODEV; |
165 | } | |
979b5ec3 JC |
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 | ||
7d7f2126 | 176 | if (major) |
979b5ec3 | 177 | rc = register_chrdev_region(dev, num_pins, "scx200_gpio"); |
7d7f2126 | 178 | else { |
979b5ec3 | 179 | rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio"); |
7d7f2126 | 180 | major = MAJOR(dev); |
1da177e4 | 181 | } |
7d7f2126 | 182 | if (rc < 0) { |
979b5ec3 JC |
183 | dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc); |
184 | goto undo_platform_device_add; | |
7d7f2126 | 185 | } |
979b5ec3 | 186 | scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL); |
7d7f2126 JC |
187 | if (!scx200_devices) { |
188 | rc = -ENOMEM; | |
979b5ec3 | 189 | goto undo_chrdev_region; |
7d7f2126 | 190 | } |
979b5ec3 | 191 | for (i = 0; i < num_pins; i++) { |
7d7f2126 JC |
192 | struct cdev *cdev = &scx200_devices[i]; |
193 | cdev_init(cdev, &scx200_gpio_fops); | |
194 | cdev->owner = THIS_MODULE; | |
7d7f2126 | 195 | rc = cdev_add(cdev, MKDEV(major, i), 1); |
979b5ec3 | 196 | /* tolerate 'minor' errors */ |
7d7f2126 | 197 | if (rc) |
979b5ec3 | 198 | dev_err(&pdev->dev, "Error %d on minor %d", rc, i); |
1da177e4 LT |
199 | } |
200 | ||
979b5ec3 | 201 | return 0; /* succeed */ |
7d7f2126 | 202 | |
979b5ec3 JC |
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); | |
7d7f2126 | 209 | return rc; |
1da177e4 LT |
210 | } |
211 | ||
212 | static void __exit scx200_gpio_cleanup(void) | |
213 | { | |
7d7f2126 | 214 | kfree(scx200_devices); |
979b5ec3 JC |
215 | unregister_chrdev_region(MKDEV(major, 0), num_pins); |
216 | platform_device_put(pdev); | |
217 | platform_device_unregister(pdev); | |
218 | /* kfree(pdev); */ | |
1da177e4 LT |
219 | } |
220 | ||
221 | module_init(scx200_gpio_init); | |
222 | module_exit(scx200_gpio_cleanup); |