aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2008-12-19 21:18:16 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2009-01-06 16:52:42 -0500
commit99f41131b8d82cde1a84fec5b93ce31632552f0b (patch)
treeaad7856ee5dd07d21fd14edb4a06fca2c4b5a201 /drivers
parentadc567e8a9c25f08e91eb18b83bdaa5ff9705919 (diff)
Staging: android: add timed_gpio driver
driver for GPIOs that turn back off after a delay From: Mike Lockwood <lockwood@android.com> Signed-off-by: Brian Swetland <swetland@google.com> Cc: Robert Love <rlove@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/staging/android/Kconfig5
-rw-r--r--drivers/staging/android/Makefile1
-rw-r--r--drivers/staging/android/android_timed_gpio.h31
-rw-r--r--drivers/staging/android/timed_gpio.c177
4 files changed, 214 insertions, 0 deletions
diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
index 7728b84bf07c..502c283eedcc 100644
--- a/drivers/staging/android/Kconfig
+++ b/drivers/staging/android/Kconfig
@@ -72,4 +72,9 @@ config ANDROID_RAM_CONSOLE_EARLY_SIZE
72 default 0 72 default 0
73 depends on ANDROID_RAM_CONSOLE_EARLY_INIT 73 depends on ANDROID_RAM_CONSOLE_EARLY_INIT
74 74
75config ANDROID_TIMED_GPIO
76 tristate "Android timed gpio driver"
77 depends on GENERIC_GPIO
78 default n
79
75endmenu 80endmenu
diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
index 357abd73142a..d9b1525550f7 100644
--- a/drivers/staging/android/Makefile
+++ b/drivers/staging/android/Makefile
@@ -2,3 +2,4 @@ obj-$(CONFIG_ANDROID) += android.o
2obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o 2obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o
3obj-$(CONFIG_ANDROID_LOGGER) += logger.o 3obj-$(CONFIG_ANDROID_LOGGER) += logger.o
4obj-$(CONFIG_ANDROID_RAM_CONSOLE) += ram_console.o 4obj-$(CONFIG_ANDROID_RAM_CONSOLE) += ram_console.o
5obj-$(CONFIG_ANDROID_TIMED_GPIO) += timed_gpio.o
diff --git a/drivers/staging/android/android_timed_gpio.h b/drivers/staging/android/android_timed_gpio.h
new file mode 100644
index 000000000000..a38a96c2b378
--- /dev/null
+++ b/drivers/staging/android/android_timed_gpio.h
@@ -0,0 +1,31 @@
1/* include/linux/android_timed_gpio.h
2 *
3 * Copyright (C) 2008 Google, Inc.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
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.
13 *
14*/
15
16#ifndef _LINUX_ANDROID_TIMED_GPIO_H
17#define _LINUX_ANDROID_TIMED_GPIO_H
18
19struct timed_gpio {
20 const char *name;
21 unsigned gpio;
22 int max_timeout;
23 u8 active_low;
24};
25
26struct timed_gpio_platform_data {
27 int num_gpios;
28 struct timed_gpio *gpios;
29};
30
31#endif
diff --git a/drivers/staging/android/timed_gpio.c b/drivers/staging/android/timed_gpio.c
new file mode 100644
index 000000000000..faaead1a7124
--- /dev/null
+++ b/drivers/staging/android/timed_gpio.c
@@ -0,0 +1,177 @@
1/* drivers/android/timed_gpio.c
2 *
3 * Copyright (C) 2008 Google, Inc.
4 * Author: Mike Lockwood <lockwood@android.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <linux/hrtimer.h>
20#include <linux/err.h>
21#include <asm/arch/gpio.h>
22
23#include "android_timed_gpio.h"
24
25
26static struct class *timed_gpio_class;
27
28struct timed_gpio_data {
29 struct device *dev;
30 struct hrtimer timer;
31 spinlock_t lock;
32 unsigned gpio;
33 int max_timeout;
34 u8 active_low;
35};
36
37static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
38{
39 struct timed_gpio_data *gpio_data = container_of(timer, struct timed_gpio_data, timer);
40
41 gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? 1 : 0);
42 return HRTIMER_NORESTART;
43}
44
45static ssize_t gpio_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
46{
47 struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
48 int remaining;
49
50 if (hrtimer_active(&gpio_data->timer)) {
51 ktime_t r = hrtimer_get_remaining(&gpio_data->timer);
52 remaining = r.tv.sec * 1000 + r.tv.nsec / 1000000;
53 } else
54 remaining = 0;
55
56 return sprintf(buf, "%d\n", remaining);
57}
58
59static ssize_t gpio_enable_store(
60 struct device *dev, struct device_attribute *attr,
61 const char *buf, size_t size)
62{
63 struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
64 int value;
65 unsigned long flags;
66
67 sscanf(buf, "%d", &value);
68
69 spin_lock_irqsave(&gpio_data->lock, flags);
70
71 /* cancel previous timer and set GPIO according to value */
72 hrtimer_cancel(&gpio_data->timer);
73 gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? !value : !!value);
74
75 if (value > 0) {
76 if (value > gpio_data->max_timeout)
77 value = gpio_data->max_timeout;
78
79 hrtimer_start(&gpio_data->timer,
80 ktime_set(value / 1000, (value % 1000) * 1000000),
81 HRTIMER_MODE_REL);
82 }
83
84 spin_unlock_irqrestore(&gpio_data->lock, flags);
85
86 return size;
87}
88
89static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, gpio_enable_show, gpio_enable_store);
90
91static int android_timed_gpio_probe(struct platform_device *pdev)
92{
93 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
94 struct timed_gpio *cur_gpio;
95 struct timed_gpio_data *gpio_data, *gpio_dat;
96 int i, ret = 0;
97
98 if (!pdata)
99 return -EBUSY;
100
101 gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios, GFP_KERNEL);
102 if (!gpio_data)
103 return -ENOMEM;
104
105 for (i = 0; i < pdata->num_gpios; i++) {
106 cur_gpio = &pdata->gpios[i];
107 gpio_dat = &gpio_data[i];
108
109 hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
110 gpio_dat->timer.function = gpio_timer_func;
111 spin_lock_init(&gpio_dat->lock);
112
113 gpio_dat->gpio = cur_gpio->gpio;
114 gpio_dat->max_timeout = cur_gpio->max_timeout;
115 gpio_dat->active_low = cur_gpio->active_low;
116 gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low);
117
118 gpio_dat->dev = device_create(timed_gpio_class, &pdev->dev, 0, "%s", cur_gpio->name);
119 if (unlikely(IS_ERR(gpio_dat->dev)))
120 return PTR_ERR(gpio_dat->dev);
121
122 dev_set_drvdata(gpio_dat->dev, gpio_dat);
123 ret = device_create_file(gpio_dat->dev, &dev_attr_enable);
124 if (ret)
125 return ret;
126 }
127
128 platform_set_drvdata(pdev, gpio_data);
129
130 return 0;
131}
132
133static int android_timed_gpio_remove(struct platform_device *pdev)
134{
135 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
136 struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev);
137 int i;
138
139 for (i = 0; i < pdata->num_gpios; i++) {
140 device_remove_file(gpio_data[i].dev, &dev_attr_enable);
141 device_unregister(gpio_data[i].dev);
142 }
143
144 kfree(gpio_data);
145
146 return 0;
147}
148
149static struct platform_driver android_timed_gpio_driver = {
150 .probe = android_timed_gpio_probe,
151 .remove = android_timed_gpio_remove,
152 .driver = {
153 .name = "android-timed-gpio",
154 .owner = THIS_MODULE,
155 },
156};
157
158static int __init android_timed_gpio_init(void)
159{
160 timed_gpio_class = class_create(THIS_MODULE, "timed_output");
161 if (IS_ERR(timed_gpio_class))
162 return PTR_ERR(timed_gpio_class);
163 return platform_driver_register(&android_timed_gpio_driver);
164}
165
166static void __exit android_timed_gpio_exit(void)
167{
168 class_destroy(timed_gpio_class);
169 platform_driver_unregister(&android_timed_gpio_driver);
170}
171
172module_init(android_timed_gpio_init);
173module_exit(android_timed_gpio_exit);
174
175MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
176MODULE_DESCRIPTION("Android timed gpio driver");
177MODULE_LICENSE("GPL");