blob: 9f701dcba95c74777c2944d6b99f6bc1c784a6a8 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Jim Cromie62c83cd2006-06-27 02:54:13 -07002/* linux/drivers/char/scx200_gpio.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4 National Semiconductor SCx200 GPIO driver. Allows a user space
5 process to play with the GPIO pins.
6
7 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */
8
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 Torvalds7c0f6ba2016-12-24 11:46:01 -080016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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
Jim Cromieae2d1f22006-07-14 00:24:16 -070025#define DRVNAME "scx200_gpio"
Jim Cromie979b5ec2006-06-27 02:54:14 -070026
27static struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
Jim Cromieae2d1f22006-07-14 00:24:16 -070030MODULE_DESCRIPTION("NatSemi/AMD SCx200 GPIO Pin Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070031MODULE_LICENSE("GPL");
32
33static int major = 0; /* default to dynamic major */
34module_param(major, int, 0);
35MODULE_PARM_DESC(major, "Major device number");
36
Jim Cromieae2d1f22006-07-14 00:24:16 -070037#define MAX_PINS 32 /* 64 later, when known ok */
38
Jim Cromie2e8f7a32006-07-14 00:24:26 -070039struct nsc_gpio_ops scx200_gpio_ops = {
Jim Cromiefe3a1682006-06-27 02:54:18 -070040 .owner = THIS_MODULE,
41 .gpio_config = scx200_gpio_configure,
Jim Cromie0e41ef32006-06-27 02:54:20 -070042 .gpio_dump = nsc_gpio_dump,
Jim Cromiefe3a1682006-06-27 02:54:18 -070043 .gpio_get = scx200_gpio_get,
44 .gpio_set = scx200_gpio_set,
Jim Cromiefe3a1682006-06-27 02:54:18 -070045 .gpio_change = scx200_gpio_change,
46 .gpio_current = scx200_gpio_current
47};
Chris Boot58012cd2006-09-29 01:59:07 -070048EXPORT_SYMBOL_GPL(scx200_gpio_ops);
Jim Cromiefe3a1682006-06-27 02:54:18 -070049
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 Cromie2e8f7a32006-07-14 00:24:26 -070053 file->private_data = &scx200_gpio_ops;
Jim Cromiec3dc8072006-06-27 02:54:18 -070054
Jim Cromieae2d1f22006-07-14 00:24:16 -070055 if (m >= MAX_PINS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 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
Jim Cromie2e8f7a32006-07-14 00:24:26 -070065static const struct file_operations scx200_gpio_fileops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .owner = THIS_MODULE,
Jim Cromie1a66fdf2006-06-27 02:54:20 -070067 .write = nsc_gpio_write,
68 .read = nsc_gpio_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .open = scx200_gpio_open,
70 .release = scx200_gpio_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +020071 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Jim Cromiec8ad9682006-09-29 01:59:05 -070074static struct cdev scx200_gpio_cdev; /* use 1 cdev for all pins */
Jim Cromie7d7f2122006-06-27 02:54:14 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int __init scx200_gpio_init(void)
77{
Jim Cromie635adb62006-07-14 00:24:16 -070078 int rc;
Jim Cromieae2d1f22006-07-14 00:24:16 -070079 dev_t devid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (!scx200_gpio_present()) {
Jim Cromieae2d1f22006-07-14 00:24:16 -070082 printk(KERN_ERR DRVNAME ": no SCx200 gpio present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return -ENODEV;
84 }
Jim Cromie979b5ec2006-06-27 02:54:14 -070085
86 /* support dev_dbg() with pdev->dev */
Jim Cromieae2d1f22006-07-14 00:24:16 -070087 pdev = platform_device_alloc(DRVNAME, 0);
Jim Cromie979b5ec2006-06-27 02:54:14 -070088 if (!pdev)
89 return -ENOMEM;
90
91 rc = platform_device_add(pdev);
92 if (rc)
93 goto undo_malloc;
94
Jim Cromief31000e2006-06-27 02:54:23 -070095 /* nsc_gpio uses dev_dbg(), so needs this */
Jim Cromie2e8f7a32006-07-14 00:24:26 -070096 scx200_gpio_ops.dev = &pdev->dev;
Jim Cromief31000e2006-06-27 02:54:23 -070097
Jim Cromieae2d1f22006-07-14 00:24:16 -070098 if (major) {
99 devid = MKDEV(major, 0);
100 rc = register_chrdev_region(devid, MAX_PINS, "scx200_gpio");
101 } else {
102 rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");
103 major = MAJOR(devid);
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 Cromie635adb62006-07-14 00:24:16 -0700109
Jim Cromie2e8f7a32006-07-14 00:24:26 -0700110 cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
Jim Cromie635adb62006-07-14 00:24:16 -0700111 cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Jim Cromie979b5ec2006-06-27 02:54:14 -0700113 return 0; /* succeed */
Jim Cromie7d7f2122006-06-27 02:54:14 -0700114
Jim Cromie979b5ec2006-06-27 02:54:14 -0700115undo_platform_device_add:
Ingo Molnar1017f6a2006-06-30 01:55:29 -0700116 platform_device_del(pdev);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700117undo_malloc:
Ingo Molnar1017f6a2006-06-30 01:55:29 -0700118 platform_device_put(pdev);
119
Jim Cromie7d7f2122006-06-27 02:54:14 -0700120 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static void __exit scx200_gpio_cleanup(void)
124{
Jim Cromie635adb62006-07-14 00:24:16 -0700125 cdev_del(&scx200_gpio_cdev);
126 /* cdev_put(&scx200_gpio_cdev); */
127
Jim Cromieae2d1f22006-07-14 00:24:16 -0700128 unregister_chrdev_region(MKDEV(major, 0), MAX_PINS);
Jim Cromie979b5ec2006-06-27 02:54:14 -0700129 platform_device_unregister(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132module_init(scx200_gpio_init);
133module_exit(scx200_gpio_cleanup);