Commit | Line | Data |
---|---|---|
62c83cde | 1 | /* linux/drivers/char/scx200_gpio.c |
1da177e4 LT |
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> | |
979b5ec3 | 9 | #include <linux/device.h> |
1da177e4 LT |
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> | |
979b5ec3 | 15 | #include <linux/platform_device.h> |
1da177e4 LT |
16 | #include <asm/uaccess.h> |
17 | #include <asm/io.h> | |
18 | ||
7d7f2126 JC |
19 | #include <linux/types.h> |
20 | #include <linux/cdev.h> | |
21 | ||
1da177e4 | 22 | #include <linux/scx200_gpio.h> |
fe3a168a | 23 | #include <linux/nsc_gpio.h> |
1da177e4 LT |
24 | |
25 | #define NAME "scx200_gpio" | |
979b5ec3 JC |
26 | #define DEVNAME NAME |
27 | ||
28 | static struct platform_device *pdev; | |
1da177e4 LT |
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 | ||
7d7f2126 JC |
38 | extern void scx200_gpio_dump(unsigned index); |
39 | ||
1a66fdf0 JC |
40 | extern ssize_t nsc_gpio_write(struct file *file, const char __user *data, |
41 | size_t len, loff_t *ppos); | |
42 | ||
43 | extern ssize_t nsc_gpio_read(struct file *file, char __user *buf, | |
44 | size_t len, loff_t *ppos); | |
45 | ||
fe3a168a JC |
46 | struct nsc_gpio_ops scx200_access = { |
47 | .owner = THIS_MODULE, | |
48 | .gpio_config = scx200_gpio_configure, | |
49 | .gpio_dump = scx200_gpio_dump, | |
50 | .gpio_get = scx200_gpio_get, | |
51 | .gpio_set = scx200_gpio_set, | |
52 | .gpio_set_high = scx200_gpio_set_high, | |
53 | .gpio_set_low = scx200_gpio_set_low, | |
54 | .gpio_change = scx200_gpio_change, | |
55 | .gpio_current = scx200_gpio_current | |
56 | }; | |
57 | ||
1da177e4 LT |
58 | static int scx200_gpio_open(struct inode *inode, struct file *file) |
59 | { | |
60 | unsigned m = iminor(inode); | |
c3dc8071 JC |
61 | file->private_data = &scx200_access; |
62 | ||
1da177e4 LT |
63 | if (m > 63) |
64 | return -EINVAL; | |
65 | return nonseekable_open(inode, file); | |
66 | } | |
67 | ||
68 | static int scx200_gpio_release(struct inode *inode, struct file *file) | |
69 | { | |
70 | return 0; | |
71 | } | |
72 | ||
73 | ||
74 | static struct file_operations scx200_gpio_fops = { | |
75 | .owner = THIS_MODULE, | |
1a66fdf0 JC |
76 | .write = nsc_gpio_write, |
77 | .read = nsc_gpio_read, | |
1da177e4 LT |
78 | .open = scx200_gpio_open, |
79 | .release = scx200_gpio_release, | |
80 | }; | |
81 | ||
7d7f2126 | 82 | struct cdev *scx200_devices; |
979b5ec3 | 83 | static int num_pins = 32; |
7d7f2126 | 84 | |
1da177e4 LT |
85 | static int __init scx200_gpio_init(void) |
86 | { | |
7d7f2126 JC |
87 | int rc, i; |
88 | dev_t dev = MKDEV(major, 0); | |
1da177e4 | 89 | |
1da177e4 | 90 | if (!scx200_gpio_present()) { |
7d7f2126 | 91 | printk(KERN_ERR NAME ": no SCx200 gpio present\n"); |
1da177e4 LT |
92 | return -ENODEV; |
93 | } | |
979b5ec3 JC |
94 | |
95 | /* support dev_dbg() with pdev->dev */ | |
96 | pdev = platform_device_alloc(DEVNAME, 0); | |
97 | if (!pdev) | |
98 | return -ENOMEM; | |
99 | ||
100 | rc = platform_device_add(pdev); | |
101 | if (rc) | |
102 | goto undo_malloc; | |
103 | ||
7d7f2126 | 104 | if (major) |
979b5ec3 | 105 | rc = register_chrdev_region(dev, num_pins, "scx200_gpio"); |
7d7f2126 | 106 | else { |
979b5ec3 | 107 | rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio"); |
7d7f2126 | 108 | major = MAJOR(dev); |
1da177e4 | 109 | } |
7d7f2126 | 110 | if (rc < 0) { |
979b5ec3 JC |
111 | dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc); |
112 | goto undo_platform_device_add; | |
7d7f2126 | 113 | } |
979b5ec3 | 114 | scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL); |
7d7f2126 JC |
115 | if (!scx200_devices) { |
116 | rc = -ENOMEM; | |
979b5ec3 | 117 | goto undo_chrdev_region; |
7d7f2126 | 118 | } |
979b5ec3 | 119 | for (i = 0; i < num_pins; i++) { |
7d7f2126 JC |
120 | struct cdev *cdev = &scx200_devices[i]; |
121 | cdev_init(cdev, &scx200_gpio_fops); | |
122 | cdev->owner = THIS_MODULE; | |
7d7f2126 | 123 | rc = cdev_add(cdev, MKDEV(major, i), 1); |
979b5ec3 | 124 | /* tolerate 'minor' errors */ |
7d7f2126 | 125 | if (rc) |
979b5ec3 | 126 | dev_err(&pdev->dev, "Error %d on minor %d", rc, i); |
1da177e4 LT |
127 | } |
128 | ||
979b5ec3 | 129 | return 0; /* succeed */ |
7d7f2126 | 130 | |
979b5ec3 JC |
131 | undo_chrdev_region: |
132 | unregister_chrdev_region(dev, num_pins); | |
133 | undo_platform_device_add: | |
134 | platform_device_put(pdev); | |
135 | undo_malloc: | |
136 | kfree(pdev); | |
7d7f2126 | 137 | return rc; |
1da177e4 LT |
138 | } |
139 | ||
140 | static void __exit scx200_gpio_cleanup(void) | |
141 | { | |
7d7f2126 | 142 | kfree(scx200_devices); |
979b5ec3 JC |
143 | unregister_chrdev_region(MKDEV(major, 0), num_pins); |
144 | platform_device_put(pdev); | |
145 | platform_device_unregister(pdev); | |
146 | /* kfree(pdev); */ | |
1da177e4 LT |
147 | } |
148 | ||
149 | module_init(scx200_gpio_init); | |
150 | module_exit(scx200_gpio_cleanup); |