1
2
3
4
5
6
7
8
9
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/nsc_gpio.h>
16#include <linux/platform_device.h>
17#include <asm/uaccess.h>
18#include <asm/io.h>
19
20#define NAME "nsc_gpio"
21
22void nsc_gpio_dump(struct nsc_gpio_ops *amp, unsigned index)
23{
24
25 u32 config = amp->gpio_config(index, ~0, 0);
26
27
28 dev_info(amp->dev, "io%02u: 0x%04x %s %s %s %s %s %s %s\tio:%d/%d\n",
29 index, config,
30 (config & 1) ? "OE" : "TS",
31 (config & 2) ? "PP" : "OD",
32 (config & 4) ? "PUE" : "PUD",
33 (config & 8) ? "LOCKED" : "",
34 (config & 16) ? "LEVEL" : "EDGE",
35 (config & 32) ? "HI" : "LO",
36 (config & 64) ? "DEBOUNCE" : "",
37
38 amp->gpio_get(index), amp->gpio_current(index));
39}
40
41ssize_t nsc_gpio_write(struct file *file, const char __user *data,
42 size_t len, loff_t *ppos)
43{
44 unsigned m = iminor(file_inode(file));
45 struct nsc_gpio_ops *amp = file->private_data;
46 struct device *dev = amp->dev;
47 size_t i;
48 int err = 0;
49
50 for (i = 0; i < len; ++i) {
51 char c;
52 if (get_user(c, data + i))
53 return -EFAULT;
54 switch (c) {
55 case '0':
56 amp->gpio_set(m, 0);
57 break;
58 case '1':
59 amp->gpio_set(m, 1);
60 break;
61 case 'O':
62 dev_dbg(dev, "GPIO%d output enabled\n", m);
63 amp->gpio_config(m, ~1, 1);
64 break;
65 case 'o':
66 dev_dbg(dev, "GPIO%d output disabled\n", m);
67 amp->gpio_config(m, ~1, 0);
68 break;
69 case 'T':
70 dev_dbg(dev, "GPIO%d output is push pull\n", m);
71 amp->gpio_config(m, ~2, 2);
72 break;
73 case 't':
74 dev_dbg(dev, "GPIO%d output is open drain\n", m);
75 amp->gpio_config(m, ~2, 0);
76 break;
77 case 'P':
78 dev_dbg(dev, "GPIO%d pull up enabled\n", m);
79 amp->gpio_config(m, ~4, 4);
80 break;
81 case 'p':
82 dev_dbg(dev, "GPIO%d pull up disabled\n", m);
83 amp->gpio_config(m, ~4, 0);
84 break;
85 case 'v':
86
87 amp->gpio_dump(amp, m);
88 break;
89 case '\n':
90
91 break;
92 default:
93 dev_err(dev, "io%2d bad setting: chr<0x%2x>\n",
94 m, (int)c);
95 err++;
96 }
97 }
98 if (err)
99 return -EINVAL;
100
101 return len;
102}
103
104ssize_t nsc_gpio_read(struct file *file, char __user * buf,
105 size_t len, loff_t * ppos)
106{
107 unsigned m = iminor(file_inode(file));
108 int value;
109 struct nsc_gpio_ops *amp = file->private_data;
110
111 value = amp->gpio_get(m);
112 if (put_user(value ? '1' : '0', buf))
113 return -EFAULT;
114
115 return 1;
116}
117
118
119EXPORT_SYMBOL(nsc_gpio_write);
120EXPORT_SYMBOL(nsc_gpio_read);
121EXPORT_SYMBOL(nsc_gpio_dump);
122
123static int __init nsc_gpio_init(void)
124{
125 printk(KERN_DEBUG NAME " initializing\n");
126 return 0;
127}
128
129static void __exit nsc_gpio_cleanup(void)
130{
131 printk(KERN_DEBUG NAME " cleanup\n");
132}
133
134module_init(nsc_gpio_init);
135module_exit(nsc_gpio_cleanup);
136
137MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
138MODULE_DESCRIPTION("NatSemi GPIO Common Methods");
139MODULE_LICENSE("GPL");
140