2008-12-23 15:28:02 +00:00
|
|
|
/* drivers/misc/timed_gpio.c
|
2008-12-20 02:18:16 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Google, Inc.
|
|
|
|
* Author: Mike Lockwood <lockwood@android.com>
|
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the GNU General Public
|
|
|
|
* License version 2, as published by the Free Software Foundation, and
|
|
|
|
* may be copied, distributed, and modified under those terms.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/hrtimer.h>
|
|
|
|
#include <linux/err.h>
|
2009-01-09 00:48:46 +00:00
|
|
|
#include <linux/gpio.h>
|
2008-12-20 02:18:16 +00:00
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
#include "timed_output.h"
|
2008-12-23 15:28:02 +00:00
|
|
|
#include "timed_gpio.h"
|
2008-12-20 02:18:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct timed_gpio_data {
|
2009-05-04 22:48:00 +00:00
|
|
|
struct timed_output_dev dev;
|
2008-12-20 02:18:16 +00:00
|
|
|
struct hrtimer timer;
|
|
|
|
spinlock_t lock;
|
|
|
|
unsigned gpio;
|
|
|
|
int max_timeout;
|
|
|
|
u8 active_low;
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
|
|
|
|
{
|
2009-05-04 22:48:00 +00:00
|
|
|
struct timed_gpio_data *data =
|
|
|
|
container_of(timer, struct timed_gpio_data, timer);
|
2008-12-20 02:18:16 +00:00
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
gpio_direction_output(data->gpio, data->active_low ? 1 : 0);
|
2008-12-20 02:18:16 +00:00
|
|
|
return HRTIMER_NORESTART;
|
|
|
|
}
|
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
static int gpio_get_time(struct timed_output_dev *dev)
|
2008-12-20 02:18:16 +00:00
|
|
|
{
|
2009-05-04 22:48:00 +00:00
|
|
|
struct timed_gpio_data *data =
|
|
|
|
container_of(dev, struct timed_gpio_data, dev);
|
2008-12-20 02:18:16 +00:00
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
if (hrtimer_active(&data->timer)) {
|
|
|
|
ktime_t r = hrtimer_get_remaining(&data->timer);
|
2009-01-28 23:42:43 +00:00
|
|
|
struct timeval t = ktime_to_timeval(r);
|
2009-05-04 22:48:00 +00:00
|
|
|
return t.tv_sec * 1000 + t.tv_usec / 1000;
|
2008-12-20 02:18:16 +00:00
|
|
|
} else
|
2009-05-04 22:48:00 +00:00
|
|
|
return 0;
|
2008-12-20 02:18:16 +00:00
|
|
|
}
|
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
static void gpio_enable(struct timed_output_dev *dev, int value)
|
2008-12-20 02:18:16 +00:00
|
|
|
{
|
2009-05-04 22:48:00 +00:00
|
|
|
struct timed_gpio_data *data =
|
|
|
|
container_of(dev, struct timed_gpio_data, dev);
|
2008-12-20 02:18:16 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
spin_lock_irqsave(&data->lock, flags);
|
2008-12-20 02:18:16 +00:00
|
|
|
|
|
|
|
/* cancel previous timer and set GPIO according to value */
|
2009-05-04 22:48:00 +00:00
|
|
|
hrtimer_cancel(&data->timer);
|
|
|
|
gpio_direction_output(data->gpio, data->active_low ? !value : !!value);
|
2008-12-20 02:18:16 +00:00
|
|
|
|
|
|
|
if (value > 0) {
|
2009-05-04 22:48:00 +00:00
|
|
|
if (value > data->max_timeout)
|
|
|
|
value = data->max_timeout;
|
2008-12-20 02:18:16 +00:00
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
hrtimer_start(&data->timer,
|
|
|
|
ktime_set(value / 1000, (value % 1000) * 1000000),
|
|
|
|
HRTIMER_MODE_REL);
|
2008-12-20 02:18:16 +00:00
|
|
|
}
|
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
spin_unlock_irqrestore(&data->lock, flags);
|
2008-12-20 02:18:16 +00:00
|
|
|
}
|
|
|
|
|
2008-12-23 15:28:02 +00:00
|
|
|
static int timed_gpio_probe(struct platform_device *pdev)
|
2008-12-20 02:18:16 +00:00
|
|
|
{
|
|
|
|
struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
|
|
|
|
struct timed_gpio *cur_gpio;
|
|
|
|
struct timed_gpio_data *gpio_data, *gpio_dat;
|
2009-05-04 22:48:00 +00:00
|
|
|
int i, j, ret = 0;
|
2008-12-20 02:18:16 +00:00
|
|
|
|
|
|
|
if (!pdata)
|
|
|
|
return -EBUSY;
|
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios,
|
|
|
|
GFP_KERNEL);
|
2008-12-20 02:18:16 +00:00
|
|
|
if (!gpio_data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < pdata->num_gpios; i++) {
|
|
|
|
cur_gpio = &pdata->gpios[i];
|
|
|
|
gpio_dat = &gpio_data[i];
|
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC,
|
|
|
|
HRTIMER_MODE_REL);
|
2008-12-20 02:18:16 +00:00
|
|
|
gpio_dat->timer.function = gpio_timer_func;
|
|
|
|
spin_lock_init(&gpio_dat->lock);
|
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
gpio_dat->dev.name = cur_gpio->name;
|
|
|
|
gpio_dat->dev.get_time = gpio_get_time;
|
|
|
|
gpio_dat->dev.enable = gpio_enable;
|
|
|
|
ret = timed_output_dev_register(&gpio_dat->dev);
|
|
|
|
if (ret < 0) {
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
timed_output_dev_unregister(&gpio_data[i].dev);
|
|
|
|
kfree(gpio_data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-20 02:18:16 +00:00
|
|
|
gpio_dat->gpio = cur_gpio->gpio;
|
|
|
|
gpio_dat->max_timeout = cur_gpio->max_timeout;
|
|
|
|
gpio_dat->active_low = cur_gpio->active_low;
|
|
|
|
gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low);
|
|
|
|
}
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, gpio_data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-23 15:28:02 +00:00
|
|
|
static int timed_gpio_remove(struct platform_device *pdev)
|
2008-12-20 02:18:16 +00:00
|
|
|
{
|
|
|
|
struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
|
|
|
|
struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev);
|
|
|
|
int i;
|
|
|
|
|
2009-05-04 22:48:00 +00:00
|
|
|
for (i = 0; i < pdata->num_gpios; i++)
|
|
|
|
timed_output_dev_unregister(&gpio_data[i].dev);
|
2008-12-20 02:18:16 +00:00
|
|
|
|
|
|
|
kfree(gpio_data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-23 15:28:02 +00:00
|
|
|
static struct platform_driver timed_gpio_driver = {
|
|
|
|
.probe = timed_gpio_probe,
|
|
|
|
.remove = timed_gpio_remove,
|
2008-12-20 02:18:16 +00:00
|
|
|
.driver = {
|
2009-05-04 22:48:00 +00:00
|
|
|
.name = TIMED_GPIO_NAME,
|
2008-12-20 02:18:16 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2008-12-23 15:28:02 +00:00
|
|
|
static int __init timed_gpio_init(void)
|
2008-12-20 02:18:16 +00:00
|
|
|
{
|
2008-12-23 15:28:02 +00:00
|
|
|
return platform_driver_register(&timed_gpio_driver);
|
2008-12-20 02:18:16 +00:00
|
|
|
}
|
|
|
|
|
2008-12-23 15:28:02 +00:00
|
|
|
static void __exit timed_gpio_exit(void)
|
2008-12-20 02:18:16 +00:00
|
|
|
{
|
2008-12-23 15:28:02 +00:00
|
|
|
platform_driver_unregister(&timed_gpio_driver);
|
2008-12-20 02:18:16 +00:00
|
|
|
}
|
|
|
|
|
2008-12-23 15:28:02 +00:00
|
|
|
module_init(timed_gpio_init);
|
|
|
|
module_exit(timed_gpio_exit);
|
2008-12-20 02:18:16 +00:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
|
2008-12-23 15:28:02 +00:00
|
|
|
MODULE_DESCRIPTION("timed gpio driver");
|
2008-12-20 02:18:16 +00:00
|
|
|
MODULE_LICENSE("GPL");
|