| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* linux/drivers/char/scx200_gpio.c | 
|  | 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 |  | 
|  | 29 | static ssize_t scx200_gpio_write(struct file *file, const char __user *data, | 
|  | 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; | 
|  | 37 | if (get_user(c, data+i)) | 
|  | 38 | return -EFAULT; | 
|  | 39 | switch (c) | 
|  | 40 | { | 
|  | 41 | case '0': | 
|  | 42 | scx200_gpio_set(m, 0); | 
|  | 43 | break; | 
|  | 44 | case '1': | 
|  | 45 | scx200_gpio_set(m, 1); | 
|  | 46 | break; | 
|  | 47 | case 'O': | 
|  | 48 | printk(KERN_INFO NAME ": GPIO%d output enabled\n", m); | 
|  | 49 | scx200_gpio_configure(m, ~1, 1); | 
|  | 50 | break; | 
|  | 51 | case 'o': | 
|  | 52 | printk(KERN_INFO NAME ": GPIO%d output disabled\n", m); | 
|  | 53 | scx200_gpio_configure(m, ~1, 0); | 
|  | 54 | break; | 
|  | 55 | case 'T': | 
|  | 56 | printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m); | 
|  | 57 | scx200_gpio_configure(m, ~2, 2); | 
|  | 58 | break; | 
|  | 59 | case 't': | 
|  | 60 | printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m); | 
|  | 61 | scx200_gpio_configure(m, ~2, 0); | 
|  | 62 | break; | 
|  | 63 | case 'P': | 
|  | 64 | printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m); | 
|  | 65 | scx200_gpio_configure(m, ~4, 4); | 
|  | 66 | break; | 
|  | 67 | case 'p': | 
|  | 68 | printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m); | 
|  | 69 | scx200_gpio_configure(m, ~4, 0); | 
|  | 70 | break; | 
|  | 71 | } | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | return len; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | static ssize_t scx200_gpio_read(struct file *file, char __user *buf, | 
|  | 78 | size_t len, loff_t *ppos) | 
|  | 79 | { | 
|  | 80 | unsigned m = iminor(file->f_dentry->d_inode); | 
|  | 81 | int value; | 
|  | 82 |  | 
|  | 83 | value = scx200_gpio_get(m); | 
|  | 84 | if (put_user(value ? '1' : '0', buf)) | 
|  | 85 | return -EFAULT; | 
|  | 86 |  | 
|  | 87 | return 1; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | static int scx200_gpio_open(struct inode *inode, struct file *file) | 
|  | 91 | { | 
|  | 92 | unsigned m = iminor(inode); | 
|  | 93 | if (m > 63) | 
|  | 94 | return -EINVAL; | 
|  | 95 | return nonseekable_open(inode, file); | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | static int scx200_gpio_release(struct inode *inode, struct file *file) | 
|  | 99 | { | 
|  | 100 | return 0; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 |  | 
|  | 104 | static struct file_operations scx200_gpio_fops = { | 
|  | 105 | .owner   = THIS_MODULE, | 
|  | 106 | .write   = scx200_gpio_write, | 
|  | 107 | .read    = scx200_gpio_read, | 
|  | 108 | .open    = scx200_gpio_open, | 
|  | 109 | .release = scx200_gpio_release, | 
|  | 110 | }; | 
|  | 111 |  | 
|  | 112 | static int __init scx200_gpio_init(void) | 
|  | 113 | { | 
|  | 114 | int r; | 
|  | 115 |  | 
|  | 116 | printk(KERN_DEBUG NAME ": NatSemi SCx200 GPIO Driver\n"); | 
|  | 117 |  | 
|  | 118 | if (!scx200_gpio_present()) { | 
|  | 119 | printk(KERN_ERR NAME ": no SCx200 gpio pins available\n"); | 
|  | 120 | return -ENODEV; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | r = register_chrdev(major, NAME, &scx200_gpio_fops); | 
|  | 124 | if (r < 0) { | 
|  | 125 | printk(KERN_ERR NAME ": unable to register character device\n"); | 
|  | 126 | return r; | 
|  | 127 | } | 
|  | 128 | if (!major) { | 
|  | 129 | major = r; | 
|  | 130 | printk(KERN_DEBUG NAME ": got dynamic major %d\n", major); | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | return 0; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | static void __exit scx200_gpio_cleanup(void) | 
|  | 137 | { | 
|  | 138 | unregister_chrdev(major, NAME); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | module_init(scx200_gpio_init); | 
|  | 142 | module_exit(scx200_gpio_cleanup); | 
|  | 143 |  | 
|  | 144 | /* | 
|  | 145 | Local variables: | 
|  | 146 | compile-command: "make -k -C ../.. SUBDIRS=drivers/char modules" | 
|  | 147 | c-basic-offset: 8 | 
|  | 148 | End: | 
|  | 149 | */ |