Staging: android: timed_gpio: Rename android_timed_gpio to timed_gpio
[linux-2.6/mini2440.git] / drivers / staging / android / timed_gpio.c
blobbea68c9fc942ec4f63281f19bc72081671ab79da
1 /* drivers/misc/timed_gpio.c
3 * Copyright (C) 2008 Google, Inc.
4 * Author: Mike Lockwood <lockwood@android.com>
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.
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.
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>
23 #include "timed_gpio.h"
26 static struct class *timed_gpio_class;
28 struct 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;
37 static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
39 struct timed_gpio_data *gpio_data = container_of(timer, struct timed_gpio_data, timer);
41 gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? 1 : 0);
42 return HRTIMER_NORESTART;
45 static ssize_t gpio_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
47 struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
48 int remaining;
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;
56 return sprintf(buf, "%d\n", remaining);
59 static ssize_t gpio_enable_store(
60 struct device *dev, struct device_attribute *attr,
61 const char *buf, size_t size)
63 struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
64 int value;
65 unsigned long flags;
67 sscanf(buf, "%d", &value);
69 spin_lock_irqsave(&gpio_data->lock, flags);
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);
75 if (value > 0) {
76 if (value > gpio_data->max_timeout)
77 value = gpio_data->max_timeout;
79 hrtimer_start(&gpio_data->timer,
80 ktime_set(value / 1000, (value % 1000) * 1000000),
81 HRTIMER_MODE_REL);
84 spin_unlock_irqrestore(&gpio_data->lock, flags);
86 return size;
89 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, gpio_enable_show, gpio_enable_store);
91 static int timed_gpio_probe(struct platform_device *pdev)
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;
98 if (!pdata)
99 return -EBUSY;
101 gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios, GFP_KERNEL);
102 if (!gpio_data)
103 return -ENOMEM;
105 for (i = 0; i < pdata->num_gpios; i++) {
106 cur_gpio = &pdata->gpios[i];
107 gpio_dat = &gpio_data[i];
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);
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);
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);
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;
128 platform_set_drvdata(pdev, gpio_data);
130 return 0;
133 static int timed_gpio_remove(struct platform_device *pdev)
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;
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);
144 kfree(gpio_data);
146 return 0;
149 static struct platform_driver timed_gpio_driver = {
150 .probe = timed_gpio_probe,
151 .remove = timed_gpio_remove,
152 .driver = {
153 .name = "timed-gpio",
154 .owner = THIS_MODULE,
158 static int __init timed_gpio_init(void)
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(&timed_gpio_driver);
166 static void __exit timed_gpio_exit(void)
168 class_destroy(timed_gpio_class);
169 platform_driver_unregister(&timed_gpio_driver);
172 module_init(timed_gpio_init);
173 module_exit(timed_gpio_exit);
175 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
176 MODULE_DESCRIPTION("timed gpio driver");
177 MODULE_LICENSE("GPL");