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