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> |
| 17 | #include <asm/uaccess.h> |
| 18 | #include <asm/io.h> |
| 19 | |
| 20 | #define NAME "nsc_gpio" |
| 21 | |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame^] | 22 | ssize_t nsc_gpio_write(struct file *file, const char __user *data, |
| 23 | size_t len, loff_t *ppos) |
| 24 | { |
| 25 | unsigned m = iminor(file->f_dentry->d_inode); |
| 26 | struct nsc_gpio_ops *amp = file->private_data; |
| 27 | size_t i; |
| 28 | int err = 0; |
| 29 | |
| 30 | for (i = 0; i < len; ++i) { |
| 31 | char c; |
| 32 | if (get_user(c, data + i)) |
| 33 | return -EFAULT; |
| 34 | switch (c) { |
| 35 | case '0': |
| 36 | amp->gpio_set(m, 0); |
| 37 | break; |
| 38 | case '1': |
| 39 | amp->gpio_set(m, 1); |
| 40 | break; |
| 41 | case 'O': |
| 42 | printk(KERN_INFO NAME ": GPIO%d output enabled\n", m); |
| 43 | amp->gpio_config(m, ~1, 1); |
| 44 | break; |
| 45 | case 'o': |
| 46 | printk(KERN_INFO NAME ": GPIO%d output disabled\n", m); |
| 47 | amp->gpio_config(m, ~1, 0); |
| 48 | break; |
| 49 | case 'T': |
| 50 | printk(KERN_INFO NAME ": GPIO%d output is push pull\n", |
| 51 | m); |
| 52 | amp->gpio_config(m, ~2, 2); |
| 53 | break; |
| 54 | case 't': |
| 55 | printk(KERN_INFO NAME ": GPIO%d output is open drain\n", |
| 56 | m); |
| 57 | amp->gpio_config(m, ~2, 0); |
| 58 | break; |
| 59 | case 'P': |
| 60 | printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m); |
| 61 | amp->gpio_config(m, ~4, 4); |
| 62 | break; |
| 63 | case 'p': |
| 64 | printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m); |
| 65 | amp->gpio_config(m, ~4, 0); |
| 66 | break; |
| 67 | |
| 68 | case 'v': |
| 69 | /* View Current pin settings */ |
| 70 | amp->gpio_dump(m); |
| 71 | break; |
| 72 | case '\n': |
| 73 | /* end of settings string, do nothing */ |
| 74 | break; |
| 75 | default: |
| 76 | printk(KERN_ERR NAME |
| 77 | ": GPIO-%2d bad setting: chr<0x%2x>\n", m, |
| 78 | (int)c); |
| 79 | err++; |
| 80 | } |
| 81 | } |
| 82 | if (err) |
| 83 | return -EINVAL; /* full string handled, report error */ |
| 84 | |
| 85 | return len; |
| 86 | } |
| 87 | |
| 88 | ssize_t nsc_gpio_read(struct file *file, char __user * buf, |
| 89 | size_t len, loff_t * ppos) |
| 90 | { |
| 91 | unsigned m = iminor(file->f_dentry->d_inode); |
| 92 | int value; |
| 93 | struct nsc_gpio_ops *amp = file->private_data; |
| 94 | |
| 95 | value = amp->gpio_get(m); |
| 96 | if (put_user(value ? '1' : '0', buf)) |
| 97 | return -EFAULT; |
| 98 | |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | /* common routines for both scx200_gpio and pc87360_gpio */ |
| 103 | EXPORT_SYMBOL(nsc_gpio_write); |
| 104 | EXPORT_SYMBOL(nsc_gpio_read); |
Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 105 | |
| 106 | static int __init nsc_gpio_init(void) |
| 107 | { |
| 108 | printk(KERN_DEBUG NAME " initializing\n"); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void __exit nsc_gpio_cleanup(void) |
| 113 | { |
| 114 | printk(KERN_DEBUG NAME " cleanup\n"); |
| 115 | } |
| 116 | |
Jim Cromie | 1ca5df0 | 2006-06-27 02:54:19 -0700 | [diff] [blame] | 117 | module_init(nsc_gpio_init); |
| 118 | module_exit(nsc_gpio_cleanup); |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame^] | 119 | |
| 120 | MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>"); |
| 121 | MODULE_DESCRIPTION("NatSemi GPIO Common Methods"); |
| 122 | MODULE_LICENSE("GPL"); |