Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame^] | 1 | /* linux/drivers/char/scx200_gpio.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
| 3 | National Semiconductor SCx200 GPIO driver. Allows a user space |
| 4 | process to play with the GPIO pins. |
| 5 | |
| 6 | Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */ |
| 7 | |
| 8 | #include <linux/config.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <asm/uaccess.h> |
| 15 | #include <asm/io.h> |
| 16 | |
| 17 | #include <linux/scx200_gpio.h> |
| 18 | |
| 19 | #define NAME "scx200_gpio" |
| 20 | |
| 21 | MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>"); |
| 22 | MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver"); |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | |
| 25 | static int major = 0; /* default to dynamic major */ |
| 26 | module_param(major, int, 0); |
| 27 | MODULE_PARM_DESC(major, "Major device number"); |
| 28 | |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame^] | 29 | static ssize_t scx200_gpio_write(struct file *file, const char __user *data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | size_t len, loff_t *ppos) |
| 31 | { |
| 32 | unsigned m = iminor(file->f_dentry->d_inode); |
| 33 | size_t i; |
| 34 | |
| 35 | for (i = 0; i < len; ++i) { |
| 36 | char c; |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame^] | 37 | if (get_user(c, data + i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | return -EFAULT; |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame^] | 39 | switch (c) { |
| 40 | case '0': |
| 41 | scx200_gpio_set(m, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | break; |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame^] | 43 | case '1': |
| 44 | scx200_gpio_set(m, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | break; |
| 46 | case 'O': |
| 47 | printk(KERN_INFO NAME ": GPIO%d output enabled\n", m); |
| 48 | scx200_gpio_configure(m, ~1, 1); |
| 49 | break; |
| 50 | case 'o': |
| 51 | printk(KERN_INFO NAME ": GPIO%d output disabled\n", m); |
| 52 | scx200_gpio_configure(m, ~1, 0); |
| 53 | break; |
| 54 | case 'T': |
| 55 | printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m); |
| 56 | scx200_gpio_configure(m, ~2, 2); |
| 57 | break; |
| 58 | case 't': |
| 59 | printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m); |
| 60 | scx200_gpio_configure(m, ~2, 0); |
| 61 | break; |
| 62 | case 'P': |
| 63 | printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m); |
| 64 | scx200_gpio_configure(m, ~4, 4); |
| 65 | break; |
| 66 | case 'p': |
| 67 | printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m); |
| 68 | scx200_gpio_configure(m, ~4, 0); |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return len; |
| 74 | } |
| 75 | |
| 76 | static ssize_t scx200_gpio_read(struct file *file, char __user *buf, |
| 77 | size_t len, loff_t *ppos) |
| 78 | { |
| 79 | unsigned m = iminor(file->f_dentry->d_inode); |
| 80 | int value; |
| 81 | |
| 82 | value = scx200_gpio_get(m); |
| 83 | if (put_user(value ? '1' : '0', buf)) |
| 84 | return -EFAULT; |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame^] | 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | static int scx200_gpio_open(struct inode *inode, struct file *file) |
| 90 | { |
| 91 | unsigned m = iminor(inode); |
| 92 | if (m > 63) |
| 93 | return -EINVAL; |
| 94 | return nonseekable_open(inode, file); |
| 95 | } |
| 96 | |
| 97 | static int scx200_gpio_release(struct inode *inode, struct file *file) |
| 98 | { |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static struct file_operations scx200_gpio_fops = { |
| 104 | .owner = THIS_MODULE, |
| 105 | .write = scx200_gpio_write, |
| 106 | .read = scx200_gpio_read, |
| 107 | .open = scx200_gpio_open, |
| 108 | .release = scx200_gpio_release, |
| 109 | }; |
| 110 | |
| 111 | static int __init scx200_gpio_init(void) |
| 112 | { |
| 113 | int r; |
| 114 | |
| 115 | printk(KERN_DEBUG NAME ": NatSemi SCx200 GPIO Driver\n"); |
| 116 | |
| 117 | if (!scx200_gpio_present()) { |
| 118 | printk(KERN_ERR NAME ": no SCx200 gpio pins available\n"); |
| 119 | return -ENODEV; |
| 120 | } |
| 121 | |
| 122 | r = register_chrdev(major, NAME, &scx200_gpio_fops); |
| 123 | if (r < 0) { |
| 124 | printk(KERN_ERR NAME ": unable to register character device\n"); |
| 125 | return r; |
| 126 | } |
| 127 | if (!major) { |
| 128 | major = r; |
| 129 | printk(KERN_DEBUG NAME ": got dynamic major %d\n", major); |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static void __exit scx200_gpio_cleanup(void) |
| 136 | { |
| 137 | unregister_chrdev(major, NAME); |
| 138 | } |
| 139 | |
| 140 | module_init(scx200_gpio_init); |
| 141 | module_exit(scx200_gpio_cleanup); |