2 * linux/drivers/char/ar7_gpio.c
4 * Copyright (C) 2007 OpenWrt.org
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/device.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/platform_device.h>
28 #include <asm/uaccess.h>
31 #include <linux/types.h>
32 #include <linux/cdev.h>
36 #define DRVNAME "ar7_gpio"
37 #define LONGNAME "TI AR7 GPIOs Driver"
39 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
40 MODULE_DESCRIPTION(LONGNAME);
41 MODULE_LICENSE("GPL");
43 static int ar7_gpio_major = 0;
45 static ssize_t ar7_gpio_write(struct file *file, const char __user *buf,
46 size_t len, loff_t *ppos)
48 int pin = iminor(file->f_dentry->d_inode);
51 for (i = 0; i < len; ++i) {
53 if (get_user(c, buf + i))
57 gpio_set_value(pin, 0);
60 gpio_set_value(pin, 1);
64 ar7_gpio_disable(pin);
73 gpio_direction_input(pin);
78 gpio_direction_output(pin);
88 static ssize_t ar7_gpio_read(struct file *file, char __user * buf,
89 size_t len, loff_t * ppos)
91 int pin = iminor(file->f_dentry->d_inode);
94 value = gpio_get_value(pin);
95 if (put_user(value ? '1' : '0', buf))
101 static int ar7_gpio_open(struct inode *inode, struct file *file)
103 int m = iminor(inode);
105 if (m >= AR7_GPIO_MAX)
108 return nonseekable_open(inode, file);
111 static int ar7_gpio_release(struct inode *inode, struct file *file)
116 static const struct file_operations ar7_gpio_fops = {
117 .owner = THIS_MODULE,
118 .write = ar7_gpio_write,
119 .read = ar7_gpio_read,
120 .open = ar7_gpio_open,
121 .release = ar7_gpio_release,
125 static struct platform_device *ar7_gpio_device;
127 static int __init ar7_gpio_init(void)
131 ar7_gpio_device = platform_device_alloc(DRVNAME, -1);
132 if (!ar7_gpio_device)
135 rc = platform_device_add(ar7_gpio_device);
139 rc = register_chrdev(ar7_gpio_major, DRVNAME, &ar7_gpio_fops);
148 platform_device_put(ar7_gpio_device);
153 static void __exit ar7_gpio_exit(void)
155 unregister_chrdev(ar7_gpio_major, DRVNAME);
156 platform_device_unregister(ar7_gpio_device);
159 module_init(ar7_gpio_init);
160 module_exit(ar7_gpio_exit);