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/slab.h>
20 #include <linux/hrtimer.h>
21 #include <linux/err.h>
22 #include <linux/gpio.h>
24 #include "timed_output.h"
25 #include "timed_gpio.h"
28 struct timed_gpio_data {
29 struct timed_output_dev dev;
37 static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
39 struct timed_gpio_data *data =
40 container_of(timer, struct timed_gpio_data, timer);
42 gpio_direction_output(data->gpio, data->active_low ? 1 : 0);
43 return HRTIMER_NORESTART;
46 static int gpio_get_time(struct timed_output_dev *dev)
48 struct timed_gpio_data *data;
51 data = container_of(dev, struct timed_gpio_data, dev);
53 if (!hrtimer_active(&data->timer))
56 t = ktime_to_timeval(hrtimer_get_remaining(&data->timer));
58 return t.tv_sec * 1000 + t.tv_usec / 1000;
61 static void gpio_enable(struct timed_output_dev *dev, int value)
63 struct timed_gpio_data *data =
64 container_of(dev, struct timed_gpio_data, dev);
67 spin_lock_irqsave(&data->lock, flags);
69 /* cancel previous timer and set GPIO according to value */
70 hrtimer_cancel(&data->timer);
71 gpio_direction_output(data->gpio, data->active_low ? !value : !!value);
74 if (value > data->max_timeout)
75 value = data->max_timeout;
77 hrtimer_start(&data->timer,
78 ktime_set(value / 1000, (value % 1000) * 1000000),
82 spin_unlock_irqrestore(&data->lock, flags);
85 static int timed_gpio_probe(struct platform_device *pdev)
87 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
88 struct timed_gpio *cur_gpio;
89 struct timed_gpio_data *gpio_data, *gpio_dat;
95 gpio_data = devm_kzalloc(&pdev->dev,
96 sizeof(struct timed_gpio_data) * pdata->num_gpios,
101 for (i = 0; i < pdata->num_gpios; i++) {
102 cur_gpio = &pdata->gpios[i];
103 gpio_dat = &gpio_data[i];
105 hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC,
107 gpio_dat->timer.function = gpio_timer_func;
108 spin_lock_init(&gpio_dat->lock);
110 gpio_dat->dev.name = cur_gpio->name;
111 gpio_dat->dev.get_time = gpio_get_time;
112 gpio_dat->dev.enable = gpio_enable;
113 ret = gpio_request(cur_gpio->gpio, cur_gpio->name);
116 ret = timed_output_dev_register(&gpio_dat->dev);
118 gpio_free(cur_gpio->gpio);
122 gpio_dat->gpio = cur_gpio->gpio;
123 gpio_dat->max_timeout = cur_gpio->max_timeout;
124 gpio_dat->active_low = cur_gpio->active_low;
125 gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low);
128 platform_set_drvdata(pdev, gpio_data);
134 timed_output_dev_unregister(&gpio_data[i].dev);
135 gpio_free(gpio_data[i].gpio);
141 static int timed_gpio_remove(struct platform_device *pdev)
143 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
144 struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev);
147 for (i = 0; i < pdata->num_gpios; i++) {
148 timed_output_dev_unregister(&gpio_data[i].dev);
149 gpio_free(gpio_data[i].gpio);
155 static struct platform_driver timed_gpio_driver = {
156 .probe = timed_gpio_probe,
157 .remove = timed_gpio_remove,
159 .name = TIMED_GPIO_NAME,
163 module_platform_driver(timed_gpio_driver);
165 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
166 MODULE_DESCRIPTION("timed gpio driver");
167 MODULE_LICENSE("GPL");