blob: e7665c1ad1346c465921e2338e021ff06870e5a9 [file] [log] [blame]
Jim Cromie62c83cd2006-06-27 02:54:13 -07001/* linux/drivers/char/scx200_gpio.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
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
Jim Cromie979b5ec2006-06-27 02:54:14 -07008#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/fs.h>
10#include <linux/module.h>
11#include <linux/errno.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
Jim Cromie979b5ec2006-06-27 02:54:14 -070014#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/uaccess.h>
16#include <asm/io.h>
17
Jim Cromie7d7f2122006-06-27 02:54:14 -070018#include <linux/types.h>
19#include <linux/cdev.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/scx200_gpio.h>
Jim Cromiefe3a1682006-06-27 02:54:18 -070022#include <linux/nsc_gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jim Cromieae2d1f22006-07-14 00:24:16 -070024#define DRVNAME "scx200_gpio"
Jim Cromie979b5ec2006-06-27 02:54:14 -070025
26static struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
Jim Cromieae2d1f22006-07-14 00:24:16 -070029MODULE_DESCRIPTION("NatSemi/AMD SCx200 GPIO Pin Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070030MODULE_LICENSE("GPL");
31
32static int major = 0; /* default to dynamic major */
33module_param(major, int, 0);
34MODULE_PARM_DESC(major, "Major device number");
35
Jim Cromieae2d1f22006-07-14 00:24:16 -070036#define MAX_PINS 32 /* 64 later, when known ok */
37
Jim Cromiefe3a1682006-06-27 02:54:18 -070038struct nsc_gpio_ops scx200_access = {
39 .owner = THIS_MODULE,
40 .gpio_config = scx200_gpio_configure,
Jim Cromie0e41ef32006-06-27 02:54:20 -070041 .gpio_dump = nsc_gpio_dump,
Jim Cromiefe3a1682006-06-27 02:54:18 -070042 .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};
Jim Cromieae2d1f22006-07-14 00:24:16 -070049EXPORT_SYMBOL(scx200_access);
Jim Cromiefe3a1682006-06-27 02:54:18 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int scx200_gpio_open(struct inode *inode, struct file *file)
52{
53 unsigned m = iminor(inode);
Jim Cromiec3dc8072006-06-27 02:54:18 -070054 file->private_data = &scx200_access;
55
Jim Cromieae2d1f22006-07-14 00:24:16 -070056 if (m >= MAX_PINS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return -EINVAL;
58 return nonseekable_open(inode, file);
59}
60
61static int scx200_gpio_release(struct inode *inode, struct file *file)
62{
63 return 0;
64}
65
66
Arjan van de Ven62322d22006-07-03 00:24:21 -070067static const struct file_operations scx200_gpio_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .owner = THIS_MODULE,
Jim Cromie1a66fdf2006-06-27 02:54:20 -070069 .write = nsc_gpio_write,
70 .read = nsc_gpio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .open = scx200_gpio_open,
72 .release = scx200_gpio_release,
73};
74
Jim Cromie7d7f2122006-06-27 02:54:14 -070075struct cdev *scx200_devices;
Jim Cromie7d7f2122006-06-27 02:54:14 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int __init scx200_gpio_init(void)
78{
Jim Cromie7d7f2122006-06-27 02:54:14 -070079 int rc, i;
Jim Cromieae2d1f22006-07-14 00:24:16 -070080 dev_t devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (!scx200_gpio_present()) {
Jim Cromieae2d1f22006-07-14 00:24:16 -070083 printk(KERN_ERR DRVNAME ": no SCx200 gpio present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return -ENODEV;
85 }
Jim Cromie979b5ec2006-06-27 02:54:14 -070086
87 /* support dev_dbg() with pdev->dev */
Jim Cromieae2d1f22006-07-14 00:24:16 -070088 pdev = platform_device_alloc(DRVNAME, 0);
Jim Cromie979b5ec2006-06-27 02:54:14 -070089 if (!pdev)
90 return -ENOMEM;
91
92 rc = platform_device_add(pdev);
93 if (rc)
94 goto undo_malloc;
95
Jim Cromief31000e2006-06-27 02:54:23 -070096 /* nsc_gpio uses dev_dbg(), so needs this */
97 scx200_access.dev = &pdev->dev;
98
Jim Cromieae2d1f22006-07-14 00:24:16 -070099 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Jim Cromie7d7f2122006-06-27 02:54:14 -0700106 if (rc < 0) {
Jim Cromie979b5ec2006-06-27 02:54:14 -0700107 dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
108 goto undo_platform_device_add;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700109 }
Jim Cromieae2d1f22006-07-14 00:24:16 -0700110 scx200_devices = kzalloc(MAX_PINS * sizeof(struct cdev), GFP_KERNEL);
Jim Cromie7d7f2122006-06-27 02:54:14 -0700111 if (!scx200_devices) {
112 rc = -ENOMEM;
Jim Cromie979b5ec2006-06-27 02:54:14 -0700113 goto undo_chrdev_region;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700114 }
Jim Cromieae2d1f22006-07-14 00:24:16 -0700115 for (i = 0; i < MAX_PINS; i++) {
Jim Cromie7d7f2122006-06-27 02:54:14 -0700116 struct cdev *cdev = &scx200_devices[i];
117 cdev_init(cdev, &scx200_gpio_fops);
118 cdev->owner = THIS_MODULE;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700119 rc = cdev_add(cdev, MKDEV(major, i), 1);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700120 /* tolerate 'minor' errors */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700121 if (rc)
Jim Cromie979b5ec2006-06-27 02:54:14 -0700122 dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Jim Cromie979b5ec2006-06-27 02:54:14 -0700125 return 0; /* succeed */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700126
Jim Cromie979b5ec2006-06-27 02:54:14 -0700127undo_chrdev_region:
Jim Cromieae2d1f22006-07-14 00:24:16 -0700128 unregister_chrdev_region(devid, MAX_PINS);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700129undo_platform_device_add:
Ingo Molnar1017f6a2006-06-30 01:55:29 -0700130 platform_device_del(pdev);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700131undo_malloc:
Ingo Molnar1017f6a2006-06-30 01:55:29 -0700132 platform_device_put(pdev);
133
Jim Cromie7d7f2122006-06-27 02:54:14 -0700134 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
137static void __exit scx200_gpio_cleanup(void)
138{
Jim Cromie7d7f2122006-06-27 02:54:14 -0700139 kfree(scx200_devices);
Jim Cromieae2d1f22006-07-14 00:24:16 -0700140 unregister_chrdev_region(MKDEV(major, 0), MAX_PINS);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700141 platform_device_unregister(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144module_init(scx200_gpio_init);
145module_exit(scx200_gpio_cleanup);