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 | fe3a168 | 2006-06-27 02:54:18 -0700 | [diff] [blame] | 38 | struct nsc_gpio_ops scx200_access = { |
| 39 | .owner = THIS_MODULE, |
| 40 | .gpio_config = scx200_gpio_configure, |
Jim Cromie | 0e41ef3 | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 41 | .gpio_dump = nsc_gpio_dump, |
Jim Cromie | fe3a168 | 2006-06-27 02:54:18 -0700 | [diff] [blame] | 42 | .gpio_get = scx200_gpio_get, |
| 43 | .gpio_set = scx200_gpio_set, |
| 44 | .gpio_set_high = scx200_gpio_set_high, |
| 45 | .gpio_set_low = scx200_gpio_set_low, |
| 46 | .gpio_change = scx200_gpio_change, |
| 47 | .gpio_current = scx200_gpio_current |
| 48 | }; |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static int scx200_gpio_open(struct inode *inode, struct file *file) |
| 51 | { |
| 52 | unsigned m = iminor(inode); |
Jim Cromie | c3dc807 | 2006-06-27 02:54:18 -0700 | [diff] [blame] | 53 | file->private_data = &scx200_access; |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | if (m > 63) |
| 56 | return -EINVAL; |
| 57 | return nonseekable_open(inode, file); |
| 58 | } |
| 59 | |
| 60 | static int scx200_gpio_release(struct inode *inode, struct file *file) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | static struct file_operations scx200_gpio_fops = { |
| 67 | .owner = THIS_MODULE, |
Jim Cromie | 1a66fdf | 2006-06-27 02:54:20 -0700 | [diff] [blame] | 68 | .write = nsc_gpio_write, |
| 69 | .read = nsc_gpio_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | .open = scx200_gpio_open, |
| 71 | .release = scx200_gpio_release, |
| 72 | }; |
| 73 | |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 74 | struct cdev *scx200_devices; |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 75 | static int num_pins = 32; |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static int __init scx200_gpio_init(void) |
| 78 | { |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 79 | int rc, i; |
| 80 | dev_t dev = MKDEV(major, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | if (!scx200_gpio_present()) { |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 83 | printk(KERN_ERR NAME ": no SCx200 gpio present\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return -ENODEV; |
| 85 | } |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 86 | |
| 87 | /* support dev_dbg() with pdev->dev */ |
| 88 | pdev = platform_device_alloc(DEVNAME, 0); |
| 89 | if (!pdev) |
| 90 | return -ENOMEM; |
| 91 | |
| 92 | rc = platform_device_add(pdev); |
| 93 | if (rc) |
| 94 | goto undo_malloc; |
| 95 | |
Jim Cromie | f31000e | 2006-06-27 02:54:23 -0700 | [diff] [blame] | 96 | /* nsc_gpio uses dev_dbg(), so needs this */ |
| 97 | scx200_access.dev = &pdev->dev; |
| 98 | |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 99 | if (major) |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 100 | rc = register_chrdev_region(dev, num_pins, "scx200_gpio"); |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 101 | else { |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 102 | rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio"); |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 103 | major = MAJOR(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 105 | if (rc < 0) { |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 106 | dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc); |
| 107 | goto undo_platform_device_add; |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 108 | } |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 109 | scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL); |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 110 | if (!scx200_devices) { |
| 111 | rc = -ENOMEM; |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 112 | goto undo_chrdev_region; |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 113 | } |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 114 | for (i = 0; i < num_pins; i++) { |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 115 | struct cdev *cdev = &scx200_devices[i]; |
| 116 | cdev_init(cdev, &scx200_gpio_fops); |
| 117 | cdev->owner = THIS_MODULE; |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 118 | rc = cdev_add(cdev, MKDEV(major, i), 1); |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 119 | /* tolerate 'minor' errors */ |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 120 | if (rc) |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 121 | dev_err(&pdev->dev, "Error %d on minor %d", rc, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 124 | return 0; /* succeed */ |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 125 | |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 126 | undo_chrdev_region: |
| 127 | unregister_chrdev_region(dev, num_pins); |
| 128 | undo_platform_device_add: |
Ingo Molnar | 1017f6a | 2006-06-30 01:55:29 -0700 | [diff] [blame^] | 129 | platform_device_del(pdev); |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 130 | undo_malloc: |
Ingo Molnar | 1017f6a | 2006-06-30 01:55:29 -0700 | [diff] [blame^] | 131 | platform_device_put(pdev); |
| 132 | |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 133 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void __exit scx200_gpio_cleanup(void) |
| 137 | { |
Jim Cromie | 7d7f212 | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 138 | kfree(scx200_devices); |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 139 | unregister_chrdev_region(MKDEV(major, 0), num_pins); |
Jim Cromie | 979b5ec | 2006-06-27 02:54:14 -0700 | [diff] [blame] | 140 | platform_device_unregister(pdev); |
| 141 | /* kfree(pdev); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | module_init(scx200_gpio_init); |
| 145 | module_exit(scx200_gpio_cleanup); |