1 /* linux/drivers/char/scx200_gpio.c
3 National Semiconductor SCx200 GPIO driver. Allows a user space
4 process to play with the GPIO pins.
6 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> */
8 #include <linux/config.h>
9 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <asm/uaccess.h>
19 #include <linux/types.h>
20 #include <linux/cdev.h>
22 #include <linux/scx200_gpio.h>
23 #include <linux/nsc_gpio.h>
25 #define NAME "scx200_gpio"
28 static struct platform_device
*pdev
;
30 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
31 MODULE_DESCRIPTION("NatSemi SCx200 GPIO Pin Driver");
32 MODULE_LICENSE("GPL");
34 static int major
= 0; /* default to dynamic major */
35 module_param(major
, int, 0);
36 MODULE_PARM_DESC(major
, "Major device number");
38 extern void scx200_gpio_dump(unsigned index
);
40 struct nsc_gpio_ops scx200_access
= {
42 .gpio_config
= scx200_gpio_configure
,
43 .gpio_dump
= scx200_gpio_dump
,
44 .gpio_get
= scx200_gpio_get
,
45 .gpio_set
= scx200_gpio_set
,
46 .gpio_set_high
= scx200_gpio_set_high
,
47 .gpio_set_low
= scx200_gpio_set_low
,
48 .gpio_change
= scx200_gpio_change
,
49 .gpio_current
= scx200_gpio_current
52 static ssize_t
scx200_gpio_write(struct file
*file
, const char __user
*data
,
53 size_t len
, loff_t
*ppos
)
55 unsigned m
= iminor(file
->f_dentry
->d_inode
);
56 struct nsc_gpio_ops
*amp
= file
->private_data
;
60 for (i
= 0; i
< len
; ++i
) {
62 if (get_user(c
, data
+ i
))
72 printk(KERN_INFO NAME
": GPIO%d output enabled\n", m
);
73 amp
->gpio_config(m
, ~1, 1);
76 printk(KERN_INFO NAME
": GPIO%d output disabled\n", m
);
77 amp
->gpio_config(m
, ~1, 0);
80 printk(KERN_INFO NAME
": GPIO%d output is push pull\n", m
);
81 amp
->gpio_config(m
, ~2, 2);
84 printk(KERN_INFO NAME
": GPIO%d output is open drain\n", m
);
85 amp
->gpio_config(m
, ~2, 0);
88 printk(KERN_INFO NAME
": GPIO%d pull up enabled\n", m
);
89 amp
->gpio_config(m
, ~4, 4);
92 printk(KERN_INFO NAME
": GPIO%d pull up disabled\n", m
);
93 amp
->gpio_config(m
, ~4, 0);
97 /* View Current pin settings */
101 /* end of settings string, do nothing */
105 ": GPIO-%2d bad setting: chr<0x%2x>\n", m
,
111 return -EINVAL
; /* full string handled, report error */
116 static ssize_t
scx200_gpio_read(struct file
*file
, char __user
*buf
,
117 size_t len
, loff_t
*ppos
)
119 unsigned m
= iminor(file
->f_dentry
->d_inode
);
121 struct nsc_gpio_ops
*amp
= file
->private_data
;
123 value
= amp
->gpio_get(m
);
124 if (put_user(value
? '1' : '0', buf
))
130 static int scx200_gpio_open(struct inode
*inode
, struct file
*file
)
132 unsigned m
= iminor(inode
);
133 file
->private_data
= &scx200_access
;
137 return nonseekable_open(inode
, file
);
140 static int scx200_gpio_release(struct inode
*inode
, struct file
*file
)
146 static struct file_operations scx200_gpio_fops
= {
147 .owner
= THIS_MODULE
,
148 .write
= scx200_gpio_write
,
149 .read
= scx200_gpio_read
,
150 .open
= scx200_gpio_open
,
151 .release
= scx200_gpio_release
,
154 struct cdev
*scx200_devices
;
155 static int num_pins
= 32;
157 static int __init
scx200_gpio_init(void)
160 dev_t dev
= MKDEV(major
, 0);
162 if (!scx200_gpio_present()) {
163 printk(KERN_ERR NAME
": no SCx200 gpio present\n");
167 /* support dev_dbg() with pdev->dev */
168 pdev
= platform_device_alloc(DEVNAME
, 0);
172 rc
= platform_device_add(pdev
);
177 rc
= register_chrdev_region(dev
, num_pins
, "scx200_gpio");
179 rc
= alloc_chrdev_region(&dev
, 0, num_pins
, "scx200_gpio");
183 dev_err(&pdev
->dev
, "SCx200 chrdev_region err: %d\n", rc
);
184 goto undo_platform_device_add
;
186 scx200_devices
= kzalloc(num_pins
* sizeof(struct cdev
), GFP_KERNEL
);
187 if (!scx200_devices
) {
189 goto undo_chrdev_region
;
191 for (i
= 0; i
< num_pins
; i
++) {
192 struct cdev
*cdev
= &scx200_devices
[i
];
193 cdev_init(cdev
, &scx200_gpio_fops
);
194 cdev
->owner
= THIS_MODULE
;
195 rc
= cdev_add(cdev
, MKDEV(major
, i
), 1);
196 /* tolerate 'minor' errors */
198 dev_err(&pdev
->dev
, "Error %d on minor %d", rc
, i
);
201 return 0; /* succeed */
204 unregister_chrdev_region(dev
, num_pins
);
205 undo_platform_device_add
:
206 platform_device_put(pdev
);
212 static void __exit
scx200_gpio_cleanup(void)
214 kfree(scx200_devices
);
215 unregister_chrdev_region(MKDEV(major
, 0), num_pins
);
216 platform_device_put(pdev
);
217 platform_device_unregister(pdev
);
221 module_init(scx200_gpio_init
);
222 module_exit(scx200_gpio_cleanup
);