blob: da930c72bc740bdff2167037ca6381b823efebd0 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Jim Cromie1ca5df02006-06-27 02:54:19 -07002/* linux/drivers/char/nsc_gpio.c
3
4 National Semiconductor common GPIO device-file/VFS methods.
5 Allows a user space process to control the GPIO pins.
6
7 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
8 Copyright (c) 2005 Jim Cromie <jim.cromie@gmail.com>
9*/
10
Jim Cromie1ca5df02006-06-27 02:54:19 -070011#include <linux/fs.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>
Jim Cromief31000e2006-06-27 02:54:23 -070017#include <linux/platform_device.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080018#include <linux/uaccess.h>
Jim Cromie1ca5df02006-06-27 02:54:19 -070019#include <asm/io.h>
20
21#define NAME "nsc_gpio"
22
Jim Cromief31000e2006-06-27 02:54:23 -070023void nsc_gpio_dump(struct nsc_gpio_ops *amp, unsigned index)
Jim Cromie0e41ef32006-06-27 02:54:20 -070024{
Jim Cromief31000e2006-06-27 02:54:23 -070025 /* retrieve current config w/o changing it */
26 u32 config = amp->gpio_config(index, ~0, 0);
27
28 /* user requested via 'v' command, so its INFO */
Jim Cromie23916a82006-06-27 02:54:26 -070029 dev_info(amp->dev, "io%02u: 0x%04x %s %s %s %s %s %s %s\tio:%d/%d\n",
Jim Cromief31000e2006-06-27 02:54:23 -070030 index, config,
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 */
Jim Cromie23916a82006-06-27 02:54:26 -070037 (config & 64) ? "DEBOUNCE" : "", /* debounce */
38
39 amp->gpio_get(index), amp->gpio_current(index));
Jim Cromie0e41ef32006-06-27 02:54:20 -070040}
41
Jim Cromie1a66fdf2006-06-27 02:54:20 -070042ssize_t nsc_gpio_write(struct file *file, const char __user *data,
43 size_t len, loff_t *ppos)
44{
Al Viro496ad9a2013-01-23 17:07:38 -050045 unsigned m = iminor(file_inode(file));
Jim Cromie1a66fdf2006-06-27 02:54:20 -070046 struct nsc_gpio_ops *amp = file->private_data;
Jim Cromief31000e2006-06-27 02:54:23 -070047 struct device *dev = amp->dev;
Jim Cromie1a66fdf2006-06-27 02:54:20 -070048 size_t i;
49 int err = 0;
50
51 for (i = 0; i < len; ++i) {
52 char c;
53 if (get_user(c, data + i))
54 return -EFAULT;
55 switch (c) {
56 case '0':
57 amp->gpio_set(m, 0);
58 break;
59 case '1':
60 amp->gpio_set(m, 1);
61 break;
62 case 'O':
Jim Cromief31000e2006-06-27 02:54:23 -070063 dev_dbg(dev, "GPIO%d output enabled\n", m);
Jim Cromie1a66fdf2006-06-27 02:54:20 -070064 amp->gpio_config(m, ~1, 1);
65 break;
66 case 'o':
Jim Cromief31000e2006-06-27 02:54:23 -070067 dev_dbg(dev, "GPIO%d output disabled\n", m);
Jim Cromie1a66fdf2006-06-27 02:54:20 -070068 amp->gpio_config(m, ~1, 0);
69 break;
70 case 'T':
Jim Cromieabecb6d2006-07-14 00:24:26 -070071 dev_dbg(dev, "GPIO%d output is push pull\n", m);
Jim Cromie1a66fdf2006-06-27 02:54:20 -070072 amp->gpio_config(m, ~2, 2);
73 break;
74 case 't':
Jim Cromieabecb6d2006-07-14 00:24:26 -070075 dev_dbg(dev, "GPIO%d output is open drain\n", m);
Jim Cromie1a66fdf2006-06-27 02:54:20 -070076 amp->gpio_config(m, ~2, 0);
77 break;
78 case 'P':
Jim Cromief31000e2006-06-27 02:54:23 -070079 dev_dbg(dev, "GPIO%d pull up enabled\n", m);
Jim Cromie1a66fdf2006-06-27 02:54:20 -070080 amp->gpio_config(m, ~4, 4);
81 break;
82 case 'p':
Jim Cromief31000e2006-06-27 02:54:23 -070083 dev_dbg(dev, "GPIO%d pull up disabled\n", m);
Jim Cromie1a66fdf2006-06-27 02:54:20 -070084 amp->gpio_config(m, ~4, 0);
85 break;
Jim Cromie1a66fdf2006-06-27 02:54:20 -070086 case 'v':
87 /* View Current pin settings */
Jim Cromief31000e2006-06-27 02:54:23 -070088 amp->gpio_dump(amp, m);
Jim Cromie1a66fdf2006-06-27 02:54:20 -070089 break;
90 case '\n':
91 /* end of settings string, do nothing */
92 break;
93 default:
Jim Cromief31000e2006-06-27 02:54:23 -070094 dev_err(dev, "io%2d bad setting: chr<0x%2x>\n",
95 m, (int)c);
Jim Cromie1a66fdf2006-06-27 02:54:20 -070096 err++;
97 }
98 }
99 if (err)
100 return -EINVAL; /* full string handled, report error */
101
102 return len;
103}
104
105ssize_t nsc_gpio_read(struct file *file, char __user * buf,
106 size_t len, loff_t * ppos)
107{
Al Viro496ad9a2013-01-23 17:07:38 -0500108 unsigned m = iminor(file_inode(file));
Jim Cromie1a66fdf2006-06-27 02:54:20 -0700109 int value;
110 struct nsc_gpio_ops *amp = file->private_data;
111
112 value = amp->gpio_get(m);
113 if (put_user(value ? '1' : '0', buf))
114 return -EFAULT;
115
116 return 1;
117}
118
Jim Cromie0e41ef32006-06-27 02:54:20 -0700119/* common file-ops routines for both scx200_gpio and pc87360_gpio */
Jim Cromie1a66fdf2006-06-27 02:54:20 -0700120EXPORT_SYMBOL(nsc_gpio_write);
121EXPORT_SYMBOL(nsc_gpio_read);
Jim Cromie0e41ef32006-06-27 02:54:20 -0700122EXPORT_SYMBOL(nsc_gpio_dump);
Jim Cromie1ca5df02006-06-27 02:54:19 -0700123
124static int __init nsc_gpio_init(void)
125{
126 printk(KERN_DEBUG NAME " initializing\n");
127 return 0;
128}
129
130static void __exit nsc_gpio_cleanup(void)
131{
132 printk(KERN_DEBUG NAME " cleanup\n");
133}
134
Jim Cromie1ca5df02006-06-27 02:54:19 -0700135module_init(nsc_gpio_init);
136module_exit(nsc_gpio_cleanup);
Jim Cromie1a66fdf2006-06-27 02:54:20 -0700137
138MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
139MODULE_DESCRIPTION("NatSemi GPIO Common Methods");
140MODULE_LICENSE("GPL");