blob: 442367b3f5dc4325bbfa3c77799f14300676cb25 [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
8#include <linux/config.h>
Jim Cromie979b5ec2006-06-27 02:54:14 -07009#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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 Cromie979b5ec2006-06-27 02:54:14 -070015#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
17#include <asm/io.h>
18
Jim Cromie7d7f2122006-06-27 02:54:14 -070019#include <linux/types.h>
20#include <linux/cdev.h>
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/scx200_gpio.h>
Jim Cromiefe3a1682006-06-27 02:54:18 -070023#include <linux/nsc_gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define NAME "scx200_gpio"
Jim Cromie979b5ec2006-06-27 02:54:14 -070026#define DEVNAME NAME
27
28static struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
31MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver");
32MODULE_LICENSE("GPL");
33
34static int major = 0; /* default to dynamic major */
35module_param(major, int, 0);
36MODULE_PARM_DESC(major, "Major device number");
37
Jim Cromie0e41ef32006-06-27 02:54:20 -070038extern void nsc_gpio_dump(unsigned index);
Jim Cromie7d7f2122006-06-27 02:54:14 -070039
Jim Cromie1a66fdf2006-06-27 02:54:20 -070040extern ssize_t nsc_gpio_write(struct file *file, const char __user *data,
41 size_t len, loff_t *ppos);
42
43extern ssize_t nsc_gpio_read(struct file *file, char __user *buf,
44 size_t len, loff_t *ppos);
45
Jim Cromiefe3a1682006-06-27 02:54:18 -070046struct nsc_gpio_ops scx200_access = {
47 .owner = THIS_MODULE,
48 .gpio_config = scx200_gpio_configure,
Jim Cromie0e41ef32006-06-27 02:54:20 -070049 .gpio_dump = nsc_gpio_dump,
Jim Cromiefe3a1682006-06-27 02:54:18 -070050 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int scx200_gpio_open(struct inode *inode, struct file *file)
59{
60 unsigned m = iminor(inode);
Jim Cromiec3dc8072006-06-27 02:54:18 -070061 file->private_data = &scx200_access;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (m > 63)
64 return -EINVAL;
65 return nonseekable_open(inode, file);
66}
67
68static int scx200_gpio_release(struct inode *inode, struct file *file)
69{
70 return 0;
71}
72
73
74static struct file_operations scx200_gpio_fops = {
75 .owner = THIS_MODULE,
Jim Cromie1a66fdf2006-06-27 02:54:20 -070076 .write = nsc_gpio_write,
77 .read = nsc_gpio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .open = scx200_gpio_open,
79 .release = scx200_gpio_release,
80};
81
Jim Cromie7d7f2122006-06-27 02:54:14 -070082struct cdev *scx200_devices;
Jim Cromie979b5ec2006-06-27 02:54:14 -070083static int num_pins = 32;
Jim Cromie7d7f2122006-06-27 02:54:14 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int __init scx200_gpio_init(void)
86{
Jim Cromie7d7f2122006-06-27 02:54:14 -070087 int rc, i;
88 dev_t dev = MKDEV(major, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (!scx200_gpio_present()) {
Jim Cromie7d7f2122006-06-27 02:54:14 -070091 printk(KERN_ERR NAME ": no SCx200 gpio present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -ENODEV;
93 }
Jim Cromie979b5ec2006-06-27 02:54:14 -070094
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
Jim Cromie7d7f2122006-06-27 02:54:14 -0700104 if (major)
Jim Cromie979b5ec2006-06-27 02:54:14 -0700105 rc = register_chrdev_region(dev, num_pins, "scx200_gpio");
Jim Cromie7d7f2122006-06-27 02:54:14 -0700106 else {
Jim Cromie979b5ec2006-06-27 02:54:14 -0700107 rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio");
Jim Cromie7d7f2122006-06-27 02:54:14 -0700108 major = MAJOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Jim Cromie7d7f2122006-06-27 02:54:14 -0700110 if (rc < 0) {
Jim Cromie979b5ec2006-06-27 02:54:14 -0700111 dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
112 goto undo_platform_device_add;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700113 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700114 scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL);
Jim Cromie7d7f2122006-06-27 02:54:14 -0700115 if (!scx200_devices) {
116 rc = -ENOMEM;
Jim Cromie979b5ec2006-06-27 02:54:14 -0700117 goto undo_chrdev_region;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700118 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700119 for (i = 0; i < num_pins; i++) {
Jim Cromie7d7f2122006-06-27 02:54:14 -0700120 struct cdev *cdev = &scx200_devices[i];
121 cdev_init(cdev, &scx200_gpio_fops);
122 cdev->owner = THIS_MODULE;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700123 rc = cdev_add(cdev, MKDEV(major, i), 1);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700124 /* tolerate 'minor' errors */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700125 if (rc)
Jim Cromie979b5ec2006-06-27 02:54:14 -0700126 dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
Jim Cromie979b5ec2006-06-27 02:54:14 -0700129 return 0; /* succeed */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700130
Jim Cromie979b5ec2006-06-27 02:54:14 -0700131undo_chrdev_region:
132 unregister_chrdev_region(dev, num_pins);
133undo_platform_device_add:
134 platform_device_put(pdev);
135undo_malloc:
136 kfree(pdev);
Jim Cromie7d7f2122006-06-27 02:54:14 -0700137 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static void __exit scx200_gpio_cleanup(void)
141{
Jim Cromie7d7f2122006-06-27 02:54:14 -0700142 kfree(scx200_devices);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700143 unregister_chrdev_region(MKDEV(major, 0), num_pins);
144 platform_device_put(pdev);
145 platform_device_unregister(pdev);
146 /* kfree(pdev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149module_init(scx200_gpio_init);
150module_exit(scx200_gpio_cleanup);