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> | |
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 | ||
7d7f2126 JC |
17 | #include <linux/types.h> |
18 | #include <linux/cdev.h> | |
19 | ||
1da177e4 LT |
20 | #include <linux/scx200_gpio.h> |
21 | ||
22 | #define NAME "scx200_gpio" | |
23 | ||
24 | MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>"); | |
25 | MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver"); | |
26 | MODULE_LICENSE("GPL"); | |
27 | ||
28 | static int major = 0; /* default to dynamic major */ | |
29 | module_param(major, int, 0); | |
30 | MODULE_PARM_DESC(major, "Major device number"); | |
31 | ||
7d7f2126 JC |
32 | extern void scx200_gpio_dump(unsigned index); |
33 | ||
62c83cde | 34 | static ssize_t scx200_gpio_write(struct file *file, const char __user *data, |
1da177e4 LT |
35 | 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; | |
62c83cde | 42 | if (get_user(c, data + i)) |
1da177e4 | 43 | return -EFAULT; |
62c83cde JC |
44 | switch (c) { |
45 | case '0': | |
46 | scx200_gpio_set(m, 0); | |
1da177e4 | 47 | break; |
62c83cde JC |
48 | case '1': |
49 | scx200_gpio_set(m, 1); | |
1da177e4 LT |
50 | 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 | ||
81 | static 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; | |
62c83cde | 90 | |
1da177e4 LT |
91 | return 1; |
92 | } | |
93 | ||
94 | static 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 | ||
102 | static int scx200_gpio_release(struct inode *inode, struct file *file) | |
103 | { | |
104 | return 0; | |
105 | } | |
106 | ||
107 | ||
108 | static 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 | ||
7d7f2126 JC |
116 | struct cdev *scx200_devices; |
117 | int num_devs = 32; | |
118 | ||
1da177e4 LT |
119 | static int __init scx200_gpio_init(void) |
120 | { | |
7d7f2126 JC |
121 | int rc, i; |
122 | dev_t dev = MKDEV(major, 0); | |
1da177e4 LT |
123 | |
124 | printk(KERN_DEBUG NAME ": NatSemi SCx200 GPIO Driver\n"); | |
125 | ||
126 | if (!scx200_gpio_present()) { | |
7d7f2126 | 127 | printk(KERN_ERR NAME ": no SCx200 gpio present\n"); |
1da177e4 LT |
128 | return -ENODEV; |
129 | } | |
7d7f2126 JC |
130 | 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); | |
1da177e4 | 135 | } |
7d7f2126 JC |
136 | 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); | |
1da177e4 LT |
154 | } |
155 | ||
7d7f2126 JC |
156 | return 0; /* succeed */ |
157 | ||
158 | fail_malloc: | |
159 | unregister_chrdev_region(dev, num_devs); | |
160 | return rc; | |
1da177e4 LT |
161 | } |
162 | ||
163 | static void __exit scx200_gpio_cleanup(void) | |
164 | { | |
7d7f2126 JC |
165 | kfree(scx200_devices); |
166 | unregister_chrdev_region(MKDEV(major, 0), num_devs); | |
1da177e4 LT |
167 | } |
168 | ||
169 | module_init(scx200_gpio_init); | |
170 | module_exit(scx200_gpio_cleanup); |