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> |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 9 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -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> |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/uaccess.h> |
| 17 | #include <asm/io.h> |
| 18 | |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 19 | #include <linux/types.h> |
| 20 | #include <linux/cdev.h> |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/scx200_gpio.h> |
Jim Cromie | fe3a168 | 2006-06-27 02:54:18 -0700 | [diff] [blame] | 23 | #include <linux/nsc_gpio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #define NAME "scx200_gpio" |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 26 | #define DEVNAME NAME |
| 27 | |
| 28 | static struct platform_device *pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>"); |
| 31 | MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
| 34 | static int major = 0; /* default to dynamic major */ |
| 35 | module_param(major, int, 0); |
| 36 | MODULE_PARM_DESC(major, "Major device number"); |
| 37 | |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 38 | extern void scx200_gpio_dump(unsigned index); |
| 39 | |
Jim Cromie | fe3a168 | 2006-06-27 02:54:18 -0700 | [diff] [blame] | 40 | struct nsc_gpio_ops scx200_access = { |
| 41 | .owner = THIS_MODULE, |
| 42 | .gpio_config = scx200_gpio_configure, |
| 43 | .gpio_dump = scx200_gpio_dump, |
| 44 | .gpio_get = scx200_gpio_get, |
| 45 | .gpio_set = scx200_gpio_set, |
| 46 | .gpio_set_high = scx200_gpio_set_high, |
| 47 | .gpio_set_low = scx200_gpio_set_low, |
| 48 | .gpio_change = scx200_gpio_change, |
| 49 | .gpio_current = scx200_gpio_current |
| 50 | }; |
| 51 | |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame] | 52 | 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] | 53 | size_t len, loff_t *ppos) |
| 54 | { |
| 55 | unsigned m = iminor(file->f_dentry->d_inode); |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 56 | struct nsc_gpio_ops *amp = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | size_t i; |
Jim Cromie | 9550a33 | 2006-06-27 02:54:16 -0700 | [diff] [blame] | 58 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | for (i = 0; i < len; ++i) { |
| 61 | char c; |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame] | 62 | if (get_user(c, data + i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return -EFAULT; |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame] | 64 | switch (c) { |
| 65 | case '0': |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 66 | amp->gpio_set(m, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | break; |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame] | 68 | case '1': |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 69 | amp->gpio_set(m, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | break; |
| 71 | case 'O': |
| 72 | printk(KERN_INFO NAME ": GPIO%d output enabled\n", m); |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 73 | amp->gpio_config(m, ~1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | break; |
| 75 | case 'o': |
| 76 | printk(KERN_INFO NAME ": GPIO%d output disabled\n", m); |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 77 | amp->gpio_config(m, ~1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | break; |
| 79 | case 'T': |
| 80 | printk(KERN_INFO NAME ": GPIO%d output is push pull\n", m); |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 81 | amp->gpio_config(m, ~2, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | break; |
| 83 | case 't': |
| 84 | printk(KERN_INFO NAME ": GPIO%d output is open drain\n", m); |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 85 | amp->gpio_config(m, ~2, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | break; |
| 87 | case 'P': |
| 88 | printk(KERN_INFO NAME ": GPIO%d pull up enabled\n", m); |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 89 | amp->gpio_config(m, ~4, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | break; |
| 91 | case 'p': |
| 92 | printk(KERN_INFO NAME ": GPIO%d pull up disabled\n", m); |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 93 | amp->gpio_config(m, ~4, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | break; |
Jim Cromie | 9550a33 | 2006-06-27 02:54:16 -0700 | [diff] [blame] | 95 | |
| 96 | case 'v': |
| 97 | /* View Current pin settings */ |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 98 | amp->gpio_dump(m); |
Jim Cromie | 9550a33 | 2006-06-27 02:54:16 -0700 | [diff] [blame] | 99 | break; |
| 100 | case '\n': |
| 101 | /* end of settings string, do nothing */ |
| 102 | break; |
| 103 | default: |
| 104 | printk(KERN_ERR NAME |
| 105 | ": GPIO-%2d bad setting: chr<0x%2x>\n", m, |
| 106 | (int)c); |
| 107 | err++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
Jim Cromie | 9550a33 | 2006-06-27 02:54:16 -0700 | [diff] [blame] | 110 | if (err) |
| 111 | return -EINVAL; /* full string handled, report error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | return len; |
| 114 | } |
| 115 | |
| 116 | static ssize_t scx200_gpio_read(struct file *file, char __user *buf, |
| 117 | size_t len, loff_t *ppos) |
| 118 | { |
| 119 | unsigned m = iminor(file->f_dentry->d_inode); |
| 120 | int value; |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 121 | struct nsc_gpio_ops *amp = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 123 | value = amp->gpio_get(m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (put_user(value ? '1' : '0', buf)) |
| 125 | return -EFAULT; |
Jim Cromie | 62c83cd | 2006-06-27 02:54:13 -0700 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | static int scx200_gpio_open(struct inode *inode, struct file *file) |
| 131 | { |
| 132 | unsigned m = iminor(inode); |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame^] | 133 | file->private_data = &scx200_access; |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | if (m > 63) |
| 136 | return -EINVAL; |
| 137 | return nonseekable_open(inode, file); |
| 138 | } |
| 139 | |
| 140 | static int scx200_gpio_release(struct inode *inode, struct file *file) |
| 141 | { |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | static struct file_operations scx200_gpio_fops = { |
| 147 | .owner = THIS_MODULE, |
| 148 | .write = scx200_gpio_write, |
| 149 | .read = scx200_gpio_read, |
| 150 | .open = scx200_gpio_open, |
| 151 | .release = scx200_gpio_release, |
| 152 | }; |
| 153 | |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 154 | struct cdev *scx200_devices; |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 155 | static int num_pins = 32; |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | static int __init scx200_gpio_init(void) |
| 158 | { |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 159 | int rc, i; |
| 160 | dev_t dev = MKDEV(major, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (!scx200_gpio_present()) { |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 163 | printk(KERN_ERR NAME ": no SCx200 gpio present\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return -ENODEV; |
| 165 | } |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 166 | |
| 167 | /* support dev_dbg() with pdev->dev */ |
| 168 | pdev = platform_device_alloc(DEVNAME, 0); |
| 169 | if (!pdev) |
| 170 | return -ENOMEM; |
| 171 | |
| 172 | rc = platform_device_add(pdev); |
| 173 | if (rc) |
| 174 | goto undo_malloc; |
| 175 | |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 176 | if (major) |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 177 | rc = register_chrdev_region(dev, num_pins, "scx200_gpio"); |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 178 | else { |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 179 | rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio"); |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 180 | major = MAJOR(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 182 | if (rc < 0) { |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 183 | dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc); |
| 184 | goto undo_platform_device_add; |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 185 | } |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 186 | scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL); |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 187 | if (!scx200_devices) { |
| 188 | rc = -ENOMEM; |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 189 | goto undo_chrdev_region; |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 190 | } |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 191 | for (i = 0; i < num_pins; i++) { |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 192 | struct cdev *cdev = &scx200_devices[i]; |
| 193 | cdev_init(cdev, &scx200_gpio_fops); |
| 194 | cdev->owner = THIS_MODULE; |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 195 | rc = cdev_add(cdev, MKDEV(major, i), 1); |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 196 | /* tolerate 'minor' errors */ |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 197 | if (rc) |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 198 | dev_err(&pdev->dev, "Error %d on minor %d", rc, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 201 | return 0; /* succeed */ |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 202 | |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 203 | undo_chrdev_region: |
| 204 | unregister_chrdev_region(dev, num_pins); |
| 205 | undo_platform_device_add: |
| 206 | platform_device_put(pdev); |
| 207 | undo_malloc: |
| 208 | kfree(pdev); |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 209 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void __exit scx200_gpio_cleanup(void) |
| 213 | { |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 214 | kfree(scx200_devices); |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 215 | unregister_chrdev_region(MKDEV(major, 0), num_pins); |
| 216 | platform_device_put(pdev); |
| 217 | platform_device_unregister(pdev); |
| 218 | /* kfree(pdev); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | module_init(scx200_gpio_init); |
| 222 | module_exit(scx200_gpio_cleanup); |