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 | |
| 10 | #include <linux/config.h> |
| 11 | #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 Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 18 | #include <asm/uaccess.h> |
| 19 | #include <asm/io.h> |
| 20 | |
| 21 | #define NAME "nsc_gpio" |
| 22 | |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 23 | void nsc_gpio_dump(struct nsc_gpio_ops *amp, unsigned index) |
Jim Cromie | 0e41ef3 | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 24 | { |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 25 | /* 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 Cromie | 23916a8 | 2006-06-27 02:54:26 -0700 | [diff] [blame] | 29 | 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] | 30 | 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 Cromie | 23916a8 | 2006-06-27 02:54:26 -0700 | [diff] [blame] | 37 | (config & 64) ? "DEBOUNCE" : "", /* debounce */ |
| 38 | |
| 39 | amp->gpio_get(index), amp->gpio_current(index)); |
Jim Cromie | 0e41ef3 | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 42 | ssize_t nsc_gpio_write(struct file *file, const char __user *data, |
| 43 | size_t len, loff_t *ppos) |
| 44 | { |
| 45 | unsigned m = iminor(file->f_dentry->d_inode); |
| 46 | struct nsc_gpio_ops *amp = file->private_data; |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 47 | struct device *dev = amp->dev; |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 48 | 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 Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 63 | dev_dbg(dev, "GPIO%d output enabled\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 64 | amp->gpio_config(m, ~1, 1); |
| 65 | break; |
| 66 | case 'o': |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 67 | dev_dbg(dev, "GPIO%d output disabled\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 68 | amp->gpio_config(m, ~1, 0); |
| 69 | break; |
| 70 | case 'T': |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 71 | dev_dbg(dev, "GPIO%d output is push pull\n", |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 72 | m); |
| 73 | amp->gpio_config(m, ~2, 2); |
| 74 | break; |
| 75 | case 't': |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 76 | dev_dbg(dev, "GPIO%d output is open drain\n", |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 77 | m); |
| 78 | amp->gpio_config(m, ~2, 0); |
| 79 | break; |
| 80 | case 'P': |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 81 | dev_dbg(dev, "GPIO%d pull up enabled\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 82 | amp->gpio_config(m, ~4, 4); |
| 83 | break; |
| 84 | case 'p': |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 85 | dev_dbg(dev, "GPIO%d pull up disabled\n", m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 86 | amp->gpio_config(m, ~4, 0); |
| 87 | break; |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 88 | case 'v': |
| 89 | /* View Current pin settings */ |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 90 | amp->gpio_dump(amp, m); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 91 | break; |
| 92 | case '\n': |
| 93 | /* end of settings string, do nothing */ |
| 94 | break; |
| 95 | default: |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 96 | dev_err(dev, "io%2d bad setting: chr<0x%2x>\n", |
| 97 | m, (int)c); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 98 | err++; |
| 99 | } |
| 100 | } |
| 101 | if (err) |
| 102 | return -EINVAL; /* full string handled, report error */ |
| 103 | |
| 104 | return len; |
| 105 | } |
| 106 | |
| 107 | ssize_t nsc_gpio_read(struct file *file, char __user * buf, |
| 108 | size_t len, loff_t * ppos) |
| 109 | { |
| 110 | unsigned m = iminor(file->f_dentry->d_inode); |
| 111 | int value; |
| 112 | struct nsc_gpio_ops *amp = file->private_data; |
| 113 | |
| 114 | value = amp->gpio_get(m); |
| 115 | if (put_user(value ? '1' : '0', buf)) |
| 116 | return -EFAULT; |
| 117 | |
| 118 | return 1; |
| 119 | } |
| 120 | |
Jim Cromie | 0e41ef3 | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 121 | /* common file-ops routines for both scx200_gpio and pc87360_gpio */ |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 122 | EXPORT_SYMBOL(nsc_gpio_write); |
| 123 | EXPORT_SYMBOL(nsc_gpio_read); |
Jim Cromie | 0e41ef3 | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 124 | EXPORT_SYMBOL(nsc_gpio_dump); |
Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 125 | |
| 126 | static int __init nsc_gpio_init(void) |
| 127 | { |
| 128 | printk(KERN_DEBUG NAME " initializing\n"); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static void __exit nsc_gpio_cleanup(void) |
| 133 | { |
| 134 | printk(KERN_DEBUG NAME " cleanup\n"); |
| 135 | } |
| 136 | |
Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 137 | module_init(nsc_gpio_init); |
| 138 | module_exit(nsc_gpio_cleanup); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 139 | |
| 140 | MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>"); |
| 141 | MODULE_DESCRIPTION("NatSemi GPIO Common Methods"); |
| 142 | MODULE_LICENSE("GPL"); |