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>
35 #define DRVNAME "ar7_gpio"
36 #define LONGNAME "TI AR7 GPIOs Driver"
38 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
39 MODULE_DESCRIPTION(LONGNAME);
40 MODULE_LICENSE("GPL");
42 static int ar7_gpio_major = 0;
44 static ssize_t ar7_gpio_write(struct file *file, const char __user *buf,
45 size_t len, loff_t *ppos)
47 int pin = iminor(file->f_dentry->d_inode);
50 for (i = 0; i < len; ++i) {
52 if (get_user(c, buf + i))
56 gpio_set_value(pin, 0);
59 gpio_set_value(pin, 1);
63 ar7_gpio_disable(pin);
72 gpio_direction_input(pin);
77 gpio_direction_output(pin);
87 static ssize_t ar7_gpio_read(struct file *file, char __user * buf,
88 size_t len, loff_t * ppos)
90 int pin = iminor(file->f_dentry->d_inode);
93 value = gpio_get_value(pin);
94 if (put_user(value ? '1' : '0', buf))
100 static int ar7_gpio_open(struct inode *inode, struct file *file)
102 int m = iminor(inode);
104 if (m >= AR7_GPIO_MAX)
107 return nonseekable_open(inode, file);
110 static int ar7_gpio_release(struct inode *inode, struct file *file)
115 static const struct file_operations ar7_gpio_fops = {
116 .owner = THIS_MODULE,
117 .write = ar7_gpio_write,
118 .read = ar7_gpio_read,
119 .open = ar7_gpio_open,
120 .release = ar7_gpio_release,
124 static struct platform_device *ar7_gpio_device;
126 static int __init ar7_gpio_init(void)
130 ar7_gpio_device = platform_device_alloc(DRVNAME, -1);
131 if (!ar7_gpio_device)
134 rc = platform_device_add(ar7_gpio_device);
138 rc = register_chrdev(ar7_gpio_major, DRVNAME, &ar7_gpio_fops);
149 platform_device_put(ar7_gpio_device);
154 static void __exit ar7_gpio_exit(void)
156 unregister_chrdev(ar7_gpio_major, DRVNAME);
157 platform_device_unregister(ar7_gpio_device);
160 module_init(ar7_gpio_init);
161 module_exit(ar7_gpio_exit);