scx200_gpio.c 3.4 KB
Newer Older
1
/* linux/drivers/char/scx200_gpio.c
L
Linus Torvalds 已提交
2 3 4 5 6 7

   National Semiconductor SCx200 GPIO driver.  Allows a user space
   process to play with the GPIO pins.

   Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */

8
#include <linux/device.h>
L
Linus Torvalds 已提交
9 10 11 12 13
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
14
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
15 16 17
#include <asm/uaccess.h>
#include <asm/io.h>

18 19 20
#include <linux/types.h>
#include <linux/cdev.h>

L
Linus Torvalds 已提交
21
#include <linux/scx200_gpio.h>
22
#include <linux/nsc_gpio.h>
L
Linus Torvalds 已提交
23

24
#define DRVNAME "scx200_gpio"
25 26

static struct platform_device *pdev;
L
Linus Torvalds 已提交
27 28

MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
29
MODULE_DESCRIPTION("NatSemi/AMD SCx200 GPIO Pin Driver");
L
Linus Torvalds 已提交
30 31 32 33 34 35
MODULE_LICENSE("GPL");

static int major = 0;		/* default to dynamic major */
module_param(major, int, 0);
MODULE_PARM_DESC(major, "Major device number");

36 37
#define MAX_PINS 32		/* 64 later, when known ok */

38 39 40
struct nsc_gpio_ops scx200_access = {
	.owner		= THIS_MODULE,
	.gpio_config	= scx200_gpio_configure,
41
	.gpio_dump	= nsc_gpio_dump,
42 43 44 45 46 47 48
	.gpio_get	= scx200_gpio_get,
	.gpio_set	= scx200_gpio_set,
	.gpio_set_high	= scx200_gpio_set_high,
	.gpio_set_low	= scx200_gpio_set_low,
	.gpio_change	= scx200_gpio_change,
	.gpio_current	= scx200_gpio_current
};
49
EXPORT_SYMBOL(scx200_access);
50

L
Linus Torvalds 已提交
51 52 53
static int scx200_gpio_open(struct inode *inode, struct file *file)
{
	unsigned m = iminor(inode);
54 55
	file->private_data = &scx200_access;

56
	if (m >= MAX_PINS)
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66
		return -EINVAL;
	return nonseekable_open(inode, file);
}

static int scx200_gpio_release(struct inode *inode, struct file *file)
{
	return 0;
}


67
static const struct file_operations scx200_gpio_fops = {
L
Linus Torvalds 已提交
68
	.owner   = THIS_MODULE,
69 70
	.write   = nsc_gpio_write,
	.read    = nsc_gpio_read,
L
Linus Torvalds 已提交
71 72 73 74
	.open    = scx200_gpio_open,
	.release = scx200_gpio_release,
};

75 76
struct cdev *scx200_devices;

L
Linus Torvalds 已提交
77 78
static int __init scx200_gpio_init(void)
{
79
	int rc, i;
80
	dev_t devid;
L
Linus Torvalds 已提交
81 82

	if (!scx200_gpio_present()) {
83
		printk(KERN_ERR DRVNAME ": no SCx200 gpio present\n");
L
Linus Torvalds 已提交
84 85
		return -ENODEV;
	}
86 87

	/* support dev_dbg() with pdev->dev */
88
	pdev = platform_device_alloc(DRVNAME, 0);
89 90 91 92 93 94 95
	if (!pdev)
		return -ENOMEM;

	rc = platform_device_add(pdev);
	if (rc)
		goto undo_malloc;

96 97 98
	/* nsc_gpio uses dev_dbg(), so needs this */
	scx200_access.dev = &pdev->dev;

99 100 101 102 103 104
	if (major) {
		devid = MKDEV(major, 0);
		rc = register_chrdev_region(devid, MAX_PINS, "scx200_gpio");
	} else {
		rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");
		major = MAJOR(devid);
L
Linus Torvalds 已提交
105
	}
106
	if (rc < 0) {
107 108
		dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
		goto undo_platform_device_add;
109
	}
110
	scx200_devices = kzalloc(MAX_PINS * sizeof(struct cdev), GFP_KERNEL);
111 112
	if (!scx200_devices) {
		rc = -ENOMEM;
113
		goto undo_chrdev_region;
114
	}
115
	for (i = 0; i < MAX_PINS; i++) {
116 117 118 119
		struct cdev *cdev = &scx200_devices[i];
		cdev_init(cdev, &scx200_gpio_fops);
		cdev->owner = THIS_MODULE;
		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);
L
Linus Torvalds 已提交
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 133
	platform_device_put(pdev);

134
	return rc;
L
Linus Torvalds 已提交
135 136 137 138
}

static void __exit scx200_gpio_cleanup(void)
{
139
	kfree(scx200_devices);
140
	unregister_chrdev_region(MKDEV(major, 0), MAX_PINS);
141
	platform_device_unregister(pdev);
L
Linus Torvalds 已提交
142 143 144 145
}

module_init(scx200_gpio_init);
module_exit(scx200_gpio_cleanup);