1 /* linux/drivers/char/nsc_gpio.c
3 National Semiconductor common GPIO device-file/VFS methods.
4 Allows a user space process to control the GPIO pins.
6 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
7 Copyright (c) 2005 Jim Cromie <jim.cromie@gmail.com>
10 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/nsc_gpio.h>
17 #include <linux/platform_device.h>
18 #include <asm/uaccess.h>
21 #define NAME "nsc_gpio"
23 void nsc_gpio_dump(struct nsc_gpio_ops *amp, unsigned index)
25 /* retrieve current config w/o changing it */
26 u32 config = amp->gpio_config(index, ~0, 0);
28 /* user requested via 'v' command, so its INFO */
29 dev_info(amp->dev, "io%02u: 0x%04x %s %s %s %s %s %s %s\tio:%d/%d\n",
31 (config & 1) ? "OE" : "TS", /* output-enabled/tristate */
32 (config & 2) ? "PP" : "OD", /* push pull / open drain */
33 (config & 4) ? "PUE" : "PUD", /* pull up enabled/disabled */
34 (config & 8) ? "LOCKED" : "", /* locked / unlocked */
35 (config & 16) ? "LEVEL" : "EDGE",/* level/edge input */
36 (config & 32) ? "HI" : "LO", /* trigger on rise/fall edge */
37 (config & 64) ? "DEBOUNCE" : "", /* debounce */
39 amp->gpio_get(index), amp->gpio_current(index));
42 ssize_t nsc_gpio_write(struct file *file, const char __user *data,
43 size_t len, loff_t *ppos)
45 unsigned m = iminor(file->f_dentry->d_inode);
46 struct nsc_gpio_ops *amp = file->private_data;
47 struct device *dev = amp->dev;
51 for (i = 0; i < len; ++i) {
53 if (get_user(c, data + i))
63 dev_dbg(dev, "GPIO%d output enabled\n", m);
64 amp->gpio_config(m, ~1, 1);
67 dev_dbg(dev, "GPIO%d output disabled\n", m);
68 amp->gpio_config(m, ~1, 0);
71 dev_dbg(dev, "GPIO%d output is push pull\n", m);
72 amp->gpio_config(m, ~2, 2);
75 dev_dbg(dev, "GPIO%d output is open drain\n", m);
76 amp->gpio_config(m, ~2, 0);
79 dev_dbg(dev, "GPIO%d pull up enabled\n", m);
80 amp->gpio_config(m, ~4, 4);
83 dev_dbg(dev, "GPIO%d pull up disabled\n", m);
84 amp->gpio_config(m, ~4, 0);
87 /* View Current pin settings */
88 amp->gpio_dump(amp, m);
91 /* end of settings string, do nothing */
94 dev_err(dev, "io%2d bad setting: chr<0x%2x>\n",
100 return -EINVAL; /* full string handled, report error */
105 ssize_t nsc_gpio_read(struct file *file, char __user * buf,
106 size_t len, loff_t * ppos)
108 unsigned m = iminor(file->f_dentry->d_inode);
110 struct nsc_gpio_ops *amp = file->private_data;
112 value = amp->gpio_get(m);
113 if (put_user(value ? '1' : '0', buf))
119 /* common file-ops routines for both scx200_gpio and pc87360_gpio */
120 EXPORT_SYMBOL(nsc_gpio_write);
121 EXPORT_SYMBOL(nsc_gpio_read);
122 EXPORT_SYMBOL(nsc_gpio_dump);
124 static int __init nsc_gpio_init(void)
126 printk(KERN_DEBUG NAME " initializing\n");
130 static void __exit nsc_gpio_cleanup(void)
132 printk(KERN_DEBUG NAME " cleanup\n");
135 module_init(nsc_gpio_init);
136 module_exit(nsc_gpio_cleanup);
138 MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
139 MODULE_DESCRIPTION("NatSemi GPIO Common Methods");
140 MODULE_LICENSE("GPL");