2 * Copyright (C) 2007 Nicolas Thill <nico@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/device.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/platform_device.h>
26 #include <linux/uaccess.h>
28 #include <linux/types.h>
29 #include <linux/cdev.h>
32 #define DRVNAME "ar7_gpio"
33 #define LONGNAME "TI AR7 GPIOs Driver"
35 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
36 MODULE_DESCRIPTION(LONGNAME);
37 MODULE_LICENSE("GPL");
39 static int ar7_gpio_major;
41 static ssize_t ar7_gpio_write(struct file *file, const char __user *buf,
42 size_t len, loff_t *ppos)
44 int pin = iminor(file->f_dentry->d_inode);
47 for (i = 0; i < len; ++i) {
49 if (get_user(c, buf + i))
53 gpio_set_value(pin, 0);
56 gpio_set_value(pin, 1);
60 ar7_gpio_disable(pin);
69 gpio_direction_input(pin);
74 gpio_direction_output(pin, 0);
84 static ssize_t ar7_gpio_read(struct file *file, char __user *buf,
85 size_t len, loff_t *ppos)
87 int pin = iminor(file->f_dentry->d_inode);
90 value = gpio_get_value(pin);
91 if (put_user(value ? '1' : '0', buf))
97 static int ar7_gpio_open(struct inode *inode, struct file *file)
99 int m = iminor(inode);
101 if (m >= (ar7_is_titan() ? TITAN_GPIO_MAX : AR7_GPIO_MAX))
104 return nonseekable_open(inode, file);
107 static int ar7_gpio_release(struct inode *inode, struct file *file)
112 static const struct file_operations ar7_gpio_fops = {
113 .owner = THIS_MODULE,
114 .write = ar7_gpio_write,
115 .read = ar7_gpio_read,
116 .open = ar7_gpio_open,
117 .release = ar7_gpio_release,
121 static struct platform_device *ar7_gpio_device;
123 static int __init ar7_gpio_char_init(void)
127 ar7_gpio_device = platform_device_alloc(DRVNAME, -1);
128 if (!ar7_gpio_device)
131 rc = platform_device_add(ar7_gpio_device);
135 rc = register_chrdev(ar7_gpio_major, DRVNAME, &ar7_gpio_fops);
146 platform_device_put(ar7_gpio_device);
151 static void __exit ar7_gpio_char_exit(void)
153 unregister_chrdev(ar7_gpio_major, DRVNAME);
154 platform_device_unregister(ar7_gpio_device);
157 module_init(ar7_gpio_char_init);
158 module_exit(ar7_gpio_char_exit);