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 <linux/gpio.h>
23 #include "timed_gpio.h"
26 static struct class *timed_gpio_class;
28 struct timed_gpio_data {
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);
50 if (hrtimer_active(&gpio_data->timer)) {
51 ktime_t r = hrtimer_get_remaining(&gpio_data->timer);
52 struct timeval t = ktime_to_timeval(r);
53 remaining = t.tv_sec * 1000 + t.tv_usec / 1000;
57 return sprintf(buf, "%d\n", remaining);
60 static ssize_t gpio_enable_store(
61 struct device *dev, struct device_attribute *attr,
62 const char *buf, size_t size)
64 struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
68 sscanf(buf, "%d", &value);
70 spin_lock_irqsave(&gpio_data->lock, flags);
72 /* cancel previous timer and set GPIO according to value */
73 hrtimer_cancel(&gpio_data->timer);
74 gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? !value : !!value);
77 if (value > gpio_data->max_timeout)
78 value = gpio_data->max_timeout;
80 hrtimer_start(&gpio_data->timer,
81 ktime_set(value / 1000, (value % 1000) * 1000000),
85 spin_unlock_irqrestore(&gpio_data->lock, flags);
90 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, gpio_enable_show, gpio_enable_store);
92 static int timed_gpio_probe(struct platform_device *pdev)
94 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
95 struct timed_gpio *cur_gpio;
96 struct timed_gpio_data *gpio_data, *gpio_dat;
102 gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios, GFP_KERNEL);
106 for (i = 0; i < pdata->num_gpios; i++) {
107 cur_gpio = &pdata->gpios[i];
108 gpio_dat = &gpio_data[i];
110 hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
111 gpio_dat->timer.function = gpio_timer_func;
112 spin_lock_init(&gpio_dat->lock);
114 gpio_dat->gpio = cur_gpio->gpio;
115 gpio_dat->max_timeout = cur_gpio->max_timeout;
116 gpio_dat->active_low = cur_gpio->active_low;
117 gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low);
119 gpio_dat->dev = device_create(timed_gpio_class, &pdev->dev, 0, "%s", cur_gpio->name);
120 if (unlikely(IS_ERR(gpio_dat->dev)))
121 return PTR_ERR(gpio_dat->dev);
123 dev_set_drvdata(gpio_dat->dev, gpio_dat);
124 ret = device_create_file(gpio_dat->dev, &dev_attr_enable);
129 platform_set_drvdata(pdev, gpio_data);
134 static int timed_gpio_remove(struct platform_device *pdev)
136 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
137 struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev);
140 for (i = 0; i < pdata->num_gpios; i++) {
141 device_remove_file(gpio_data[i].dev, &dev_attr_enable);
142 device_unregister(gpio_data[i].dev);
150 static struct platform_driver timed_gpio_driver = {
151 .probe = timed_gpio_probe,
152 .remove = timed_gpio_remove,
154 .name = "timed-gpio",
155 .owner = THIS_MODULE,
159 static int __init timed_gpio_init(void)
161 timed_gpio_class = class_create(THIS_MODULE, "timed_output");
162 if (IS_ERR(timed_gpio_class))
163 return PTR_ERR(timed_gpio_class);
164 return platform_driver_register(&timed_gpio_driver);
167 static void __exit timed_gpio_exit(void)
169 class_destroy(timed_gpio_class);
170 platform_driver_unregister(&timed_gpio_driver);
173 module_init(timed_gpio_init);
174 module_exit(timed_gpio_exit);
176 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
177 MODULE_DESCRIPTION("timed gpio driver");
178 MODULE_LICENSE("GPL");