blob: 45083e5dd23b9584a6553cd6c02c0aa524e20a27 [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 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};
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static int scx200_gpio_open(struct inode *inode, struct file *file)
51{
52 unsigned m = iminor(inode);
Jim Cromiec3dc8072006-06-27 02:54:18 -070053 file->private_data = &scx200_access;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (m > 63)
56 return -EINVAL;
57 return nonseekable_open(inode, file);
58}
59
60static int scx200_gpio_release(struct inode *inode, struct file *file)
61{
62 return 0;
63}
64
65
66static struct file_operations scx200_gpio_fops = {
67 .owner = THIS_MODULE,
Jim Cromie1a66fdf2006-06-27 02:54:20 -070068 .write = nsc_gpio_write,
69 .read = nsc_gpio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .open = scx200_gpio_open,
71 .release = scx200_gpio_release,
72};
73
Jim Cromie7d7f2122006-06-27 02:54:14 -070074struct cdev *scx200_devices;
Jim Cromie979b5ec2006-06-27 02:54:14 -070075static int num_pins = 32;
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;
80 dev_t dev = MKDEV(major, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (!scx200_gpio_present()) {
Jim Cromie7d7f2122006-06-27 02:54:14 -070083 printk(KERN_ERR NAME ": 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 */
88 pdev = platform_device_alloc(DEVNAME, 0);
89 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 Cromie7d7f2122006-06-27 02:54:14 -070099 if (major)
Jim Cromie979b5ec2006-06-27 02:54:14 -0700100 rc = register_chrdev_region(dev, num_pins, "scx200_gpio");
Jim Cromie7d7f2122006-06-27 02:54:14 -0700101 else {
Jim Cromie979b5ec2006-06-27 02:54:14 -0700102 rc = alloc_chrdev_region(&dev, 0, num_pins, "scx200_gpio");
Jim Cromie7d7f2122006-06-27 02:54:14 -0700103 major = MAJOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Jim Cromie7d7f2122006-06-27 02:54:14 -0700105 if (rc < 0) {
Jim Cromie979b5ec2006-06-27 02:54:14 -0700106 dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
107 goto undo_platform_device_add;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700108 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700109 scx200_devices = kzalloc(num_pins * sizeof(struct cdev), GFP_KERNEL);
Jim Cromie7d7f2122006-06-27 02:54:14 -0700110 if (!scx200_devices) {
111 rc = -ENOMEM;
Jim Cromie979b5ec2006-06-27 02:54:14 -0700112 goto undo_chrdev_region;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700113 }
Jim Cromie979b5ec2006-06-27 02:54:14 -0700114 for (i = 0; i < num_pins; i++) {
Jim Cromie7d7f2122006-06-27 02:54:14 -0700115 struct cdev *cdev = &scx200_devices[i];
116 cdev_init(cdev, &scx200_gpio_fops);
117 cdev->owner = THIS_MODULE;
Jim Cromie7d7f2122006-06-27 02:54:14 -0700118 rc = cdev_add(cdev, MKDEV(major, i), 1);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700119 /* tolerate 'minor' errors */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700120 if (rc)
Jim Cromie979b5ec2006-06-27 02:54:14 -0700121 dev_err(&pdev->dev, "Error %d on minor %d", rc, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Jim Cromie979b5ec2006-06-27 02:54:14 -0700124 return 0; /* succeed */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700125
Jim Cromie979b5ec2006-06-27 02:54:14 -0700126undo_chrdev_region:
127 unregister_chrdev_region(dev, num_pins);
128undo_platform_device_add:
Ingo Molnar1017f6a2006-06-30 01:55:29 -0700129 platform_device_del(pdev);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700130undo_malloc:
Ingo Molnar1017f6a2006-06-30 01:55:29 -0700131 platform_device_put(pdev);
132
Jim Cromie7d7f2122006-06-27 02:54:14 -0700133 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static void __exit scx200_gpio_cleanup(void)
137{
Jim Cromie7d7f2122006-06-27 02:54:14 -0700138 kfree(scx200_devices);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700139 unregister_chrdev_region(MKDEV(major, 0), num_pins);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700140 platform_device_unregister(pdev);
141 /* kfree(pdev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144module_init(scx200_gpio_init);
145module_exit(scx200_gpio_cleanup);