]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/scx200_gpio.c
e7665c1ad1346c465921e2338e021ff06870e5a9
[linux-2.6-omap-h63xx.git] / drivers / char / scx200_gpio.c
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/device.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 <linux/platform_device.h>
15 #include <asm/uaccess.h>
16 #include <asm/io.h>
17
18 #include <linux/types.h>
19 #include <linux/cdev.h>
20
21 #include <linux/scx200_gpio.h>
22 #include <linux/nsc_gpio.h>
23
24 #define DRVNAME "scx200_gpio"
25
26 static struct platform_device *pdev;
27
28 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
29 MODULE_DESCRIPTION("NatSemi/AMD SCx200 GPIO Pin Driver");
30 MODULE_LICENSE("GPL");
31
32 static int major = 0;           /* default to dynamic major */
33 module_param(major, int, 0);
34 MODULE_PARM_DESC(major, "Major device number");
35
36 #define MAX_PINS 32             /* 64 later, when known ok */
37
38 struct nsc_gpio_ops scx200_access = {
39         .owner          = THIS_MODULE,
40         .gpio_config    = scx200_gpio_configure,
41         .gpio_dump      = nsc_gpio_dump,
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 EXPORT_SYMBOL(scx200_access);
50
51 static int scx200_gpio_open(struct inode *inode, struct file *file)
52 {
53         unsigned m = iminor(inode);
54         file->private_data = &scx200_access;
55
56         if (m >= MAX_PINS)
57                 return -EINVAL;
58         return nonseekable_open(inode, file);
59 }
60
61 static int scx200_gpio_release(struct inode *inode, struct file *file)
62 {
63         return 0;
64 }
65
66
67 static const struct file_operations scx200_gpio_fops = {
68         .owner   = THIS_MODULE,
69         .write   = nsc_gpio_write,
70         .read    = nsc_gpio_read,
71         .open    = scx200_gpio_open,
72         .release = scx200_gpio_release,
73 };
74
75 struct cdev *scx200_devices;
76
77 static int __init scx200_gpio_init(void)
78 {
79         int rc, i;
80         dev_t devid;
81
82         if (!scx200_gpio_present()) {
83                 printk(KERN_ERR DRVNAME ": no SCx200 gpio present\n");
84                 return -ENODEV;
85         }
86
87         /* support dev_dbg() with pdev->dev */
88         pdev = platform_device_alloc(DRVNAME, 0);
89         if (!pdev)
90                 return -ENOMEM;
91
92         rc = platform_device_add(pdev);
93         if (rc)
94                 goto undo_malloc;
95
96         /* nsc_gpio uses dev_dbg(), so needs this */
97         scx200_access.dev = &pdev->dev;
98
99         if (major) {
100                 devid = MKDEV(major, 0);
101                 rc = register_chrdev_region(devid, MAX_PINS, "scx200_gpio");
102         } else {
103                 rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");
104                 major = MAJOR(devid);
105         }
106         if (rc < 0) {
107                 dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
108                 goto undo_platform_device_add;
109         }
110         scx200_devices = kzalloc(MAX_PINS * sizeof(struct cdev), GFP_KERNEL);
111         if (!scx200_devices) {
112                 rc = -ENOMEM;
113                 goto undo_chrdev_region;
114         }
115         for (i = 0; i < MAX_PINS; i++) {
116                 struct cdev *cdev = &scx200_devices[i];
117                 cdev_init(cdev, &scx200_gpio_fops);
118                 cdev->owner = THIS_MODULE;
119                 rc = cdev_add(cdev, MKDEV(major, i), 1);
120                 /* tolerate 'minor' errors */
121                 if (rc)
122                         dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
123         }
124
125         return 0; /* succeed */
126
127 undo_chrdev_region:
128         unregister_chrdev_region(devid, MAX_PINS);
129 undo_platform_device_add:
130         platform_device_del(pdev);
131 undo_malloc:
132         platform_device_put(pdev);
133
134         return rc;
135 }
136
137 static void __exit scx200_gpio_cleanup(void)
138 {
139         kfree(scx200_devices);
140         unregister_chrdev_region(MKDEV(major, 0), MAX_PINS);
141         platform_device_unregister(pdev);
142 }
143
144 module_init(scx200_gpio_init);
145 module_exit(scx200_gpio_cleanup);