Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 1 | /* linux/drivers/char/nsc_gpio.c |
| 2 | |
| 3 | National Semiconductor common GPIO device-file/VFS methods. |
| 4 | Allows a user space process to control the GPIO pins. |
| 5 | |
| 6 | Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> |
| 7 | Copyright (c) 2005 Jim Cromie <jim.cromie@gmail.com> |
| 8 | */ |
| 9 | |
Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 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> |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 17 | #include <linux/uaccess.h> |
Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | |
| 20 | #define NAME "nsc_gpio" |
| 21 | |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 22 | void nsc_gpio_dump(struct nsc_gpio_ops *amp, unsigned index) |
Jim Cromie | 0e41ef3 | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 23 | { |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 24 | /* retrieve current config w/o changing it */ |
| 25 | u32 config = amp->gpio_config(index, ~0, 0); |
| 26 | |
| 27 | /* user requested via 'v' command, so its INFO */ |
Jim Cromie | 23916a8 | 2006-06-27 02:54:26 -0700 | [diff] [blame] | 28 | dev_info(amp->dev, "io%02u: 0x%04x %s %s %s %s %s %s %s\tio:%d/%d\n", |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 29 | index, config, |
| 30 | (config & 1) ? "OE" : "TS", /* output-enabled/tristate */ |
| 31 | (config & 2) ? "PP" : "OD", /* push pull / open drain */ |
| 32 | (config & 4) ? "PUE" : "PUD", /* pull up enabled/disabled */ |
| 33 | (config & 8) ? "LOCKED" : "", /* locked / unlocked */ |
| 34 | (config & 16) ? "LEVEL" : "EDGE",/* level/edge input */ |
| 35 | (config & 32) ? "HI" : "LO", /* trigger on rise/fall edge */ |
Jim Cromie | 23916a8 | 2006-06-27 02:54:26 -0700 | [diff] [blame] | 36 | (config & 64) ? "DEBOUNCE" : "", /* debounce */ |
| 37 | |
| 38 | amp->gpio_get(index), amp->gpio_current(index)); |
Jim Cromie | 0e41ef3 | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 41 | ssize_t nsc_gpio_write(struct file *file, const char __user *data, |
| 42 | size_t len, loff_t *ppos) |
| 43 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 44 | unsigned m = iminor(file_inode(file)); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 45 | struct nsc_gpio_ops *amp = file->private_data; |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 46 | struct device *dev = amp->dev; |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 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': |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 62 | dev_dbg(dev, "GPIO%d output enabled\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 63 | amp->gpio_config(m, ~1, 1); |
| 64 | break; |
| 65 | case 'o': |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 66 | dev_dbg(dev, "GPIO%d output disabled\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 67 | amp->gpio_config(m, ~1, 0); |
| 68 | break; |
| 69 | case 'T': |
Jim Cromie | abecb6d | 2006-07-14 00:24:26 -0700 | [diff] [blame] | 70 | dev_dbg(dev, "GPIO%d output is push pull\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 71 | amp->gpio_config(m, ~2, 2); |
| 72 | break; |
| 73 | case 't': |
Jim Cromie | abecb6d | 2006-07-14 00:24:26 -0700 | [diff] [blame] | 74 | dev_dbg(dev, "GPIO%d output is open drain\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 75 | amp->gpio_config(m, ~2, 0); |
| 76 | break; |
| 77 | case 'P': |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 78 | dev_dbg(dev, "GPIO%d pull up enabled\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 79 | amp->gpio_config(m, ~4, 4); |
| 80 | break; |
| 81 | case 'p': |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 82 | dev_dbg(dev, "GPIO%d pull up disabled\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 83 | amp->gpio_config(m, ~4, 0); |
| 84 | break; |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 85 | case 'v': |
| 86 | /* View Current pin settings */ |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 87 | amp->gpio_dump(amp, m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 88 | break; |
| 89 | case '\n': |
| 90 | /* end of settings string, do nothing */ |
| 91 | break; |
| 92 | default: |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 93 | dev_err(dev, "io%2d bad setting: chr<0x%2x>\n", |
| 94 | m, (int)c); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 95 | err++; |
| 96 | } |
| 97 | } |
| 98 | if (err) |
| 99 | return -EINVAL; /* full string handled, report error */ |
| 100 | |
| 101 | return len; |
| 102 | } |
| 103 | |
| 104 | ssize_t nsc_gpio_read(struct file *file, char __user * buf, |
| 105 | size_t len, loff_t * ppos) |
| 106 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 107 | unsigned m = iminor(file_inode(file)); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 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 | |
Jim Cromie | 0e41ef3 | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 118 | /* common file-ops routines for both scx200_gpio and pc87360_gpio */ |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 119 | EXPORT_SYMBOL(nsc_gpio_write); |
| 120 | EXPORT_SYMBOL(nsc_gpio_read); |
Jim Cromie | 0e41ef3 | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 121 | EXPORT_SYMBOL(nsc_gpio_dump); |
Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 122 | |
| 123 | static int __init nsc_gpio_init(void) |
| 124 | { |
| 125 | printk(KERN_DEBUG NAME " initializing\n"); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static void __exit nsc_gpio_cleanup(void) |
| 130 | { |
| 131 | printk(KERN_DEBUG NAME " cleanup\n"); |
| 132 | } |
| 133 | |
Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 134 | module_init(nsc_gpio_init); |
| 135 | module_exit(nsc_gpio_cleanup); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 136 | |
| 137 | MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>"); |
| 138 | MODULE_DESCRIPTION("NatSemi GPIO Common Methods"); |
| 139 | MODULE_LICENSE("GPL"); |