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 <linux/uaccess.h>
30 #include <linux/types.h>
31 #include <linux/cdev.h>
34 #define DRVNAME "ar7_gpio"
35 #define LONGNAME "TI AR7 GPIOs Driver"
37 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
38 MODULE_DESCRIPTION(LONGNAME);
39 MODULE_LICENSE("GPL");
41 static int ar7_gpio_major;
43 static ssize_t ar7_gpio_write(struct file *file, const char __user *buf,
44 size_t len, loff_t *ppos)
46 int pin = iminor(file->f_dentry->d_inode);
49 for (i = 0; i < len; ++i) {
51 if (get_user(c, buf + i))
55 gpio_set_value(pin, 0);
58 gpio_set_value(pin, 1);
62 ar7_gpio_disable(pin);
71 gpio_direction_input(pin);
76 gpio_direction_output(pin, 0);
86 static ssize_t ar7_gpio_read(struct file *file, char __user *buf,
87 size_t len, loff_t *ppos)
89 int pin = iminor(file->f_dentry->d_inode);
92 value = gpio_get_value(pin);
93 if (put_user(value ? '1' : '0', buf))
99 static int ar7_gpio_open(struct inode *inode, struct file *file)
101 int m = iminor(inode);
103 if (m >= AR7_GPIO_MAX)
106 return nonseekable_open(inode, file);
109 static int ar7_gpio_release(struct inode *inode, struct file *file)
114 static const struct file_operations ar7_gpio_fops = {
115 .owner = THIS_MODULE,
116 .write = ar7_gpio_write,
117 .read = ar7_gpio_read,
118 .open = ar7_gpio_open,
119 .release = ar7_gpio_release,
123 static struct platform_device *ar7_gpio_device;
125 static int __init ar7_gpio_init(void)
129 ar7_gpio_device = platform_device_alloc(DRVNAME, -1);
130 if (!ar7_gpio_device)
133 rc = platform_device_add(ar7_gpio_device);
137 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);