Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / android / timed_gpio.c
CommitLineData
653d1290 1/* drivers/misc/timed_gpio.c
99f41131
ML
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>
2d0db6bf 21#include <linux/gpio.h>
99f41131 22
241e1287 23#include "timed_output.h"
653d1290 24#include "timed_gpio.h"
99f41131
ML
25
26
99f41131 27struct timed_gpio_data {
241e1287 28 struct timed_output_dev dev;
99f41131
ML
29 struct hrtimer timer;
30 spinlock_t lock;
31 unsigned gpio;
32 int max_timeout;
33 u8 active_low;
34};
35
36static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
37{
241e1287
ML
38 struct timed_gpio_data *data =
39 container_of(timer, struct timed_gpio_data, timer);
99f41131 40
241e1287 41 gpio_direction_output(data->gpio, data->active_low ? 1 : 0);
99f41131
ML
42 return HRTIMER_NORESTART;
43}
44
241e1287 45static int gpio_get_time(struct timed_output_dev *dev)
99f41131 46{
241e1287
ML
47 struct timed_gpio_data *data =
48 container_of(dev, struct timed_gpio_data, dev);
99f41131 49
241e1287
ML
50 if (hrtimer_active(&data->timer)) {
51 ktime_t r = hrtimer_get_remaining(&data->timer);
07960058 52 struct timeval t = ktime_to_timeval(r);
241e1287 53 return t.tv_sec * 1000 + t.tv_usec / 1000;
99f41131 54 } else
241e1287 55 return 0;
99f41131
ML
56}
57
241e1287 58static void gpio_enable(struct timed_output_dev *dev, int value)
99f41131 59{
241e1287
ML
60 struct timed_gpio_data *data =
61 container_of(dev, struct timed_gpio_data, dev);
99f41131
ML
62 unsigned long flags;
63
241e1287 64 spin_lock_irqsave(&data->lock, flags);
99f41131
ML
65
66 /* cancel previous timer and set GPIO according to value */
241e1287
ML
67 hrtimer_cancel(&data->timer);
68 gpio_direction_output(data->gpio, data->active_low ? !value : !!value);
99f41131
ML
69
70 if (value > 0) {
241e1287
ML
71 if (value > data->max_timeout)
72 value = data->max_timeout;
99f41131 73
241e1287
ML
74 hrtimer_start(&data->timer,
75 ktime_set(value / 1000, (value % 1000) * 1000000),
76 HRTIMER_MODE_REL);
99f41131
ML
77 }
78
241e1287 79 spin_unlock_irqrestore(&data->lock, flags);
99f41131
ML
80}
81
653d1290 82static int timed_gpio_probe(struct platform_device *pdev)
99f41131
ML
83{
84 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
85 struct timed_gpio *cur_gpio;
86 struct timed_gpio_data *gpio_data, *gpio_dat;
241e1287 87 int i, j, ret = 0;
99f41131
ML
88
89 if (!pdata)
90 return -EBUSY;
91
241e1287
ML
92 gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios,
93 GFP_KERNEL);
99f41131
ML
94 if (!gpio_data)
95 return -ENOMEM;
96
97 for (i = 0; i < pdata->num_gpios; i++) {
98 cur_gpio = &pdata->gpios[i];
99 gpio_dat = &gpio_data[i];
100
241e1287
ML
101 hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC,
102 HRTIMER_MODE_REL);
99f41131
ML
103 gpio_dat->timer.function = gpio_timer_func;
104 spin_lock_init(&gpio_dat->lock);
105
241e1287
ML
106 gpio_dat->dev.name = cur_gpio->name;
107 gpio_dat->dev.get_time = gpio_get_time;
108 gpio_dat->dev.enable = gpio_enable;
109 ret = timed_output_dev_register(&gpio_dat->dev);
110 if (ret < 0) {
111 for (j = 0; j < i; j++)
112 timed_output_dev_unregister(&gpio_data[i].dev);
113 kfree(gpio_data);
114 return ret;
115 }
116
99f41131
ML
117 gpio_dat->gpio = cur_gpio->gpio;
118 gpio_dat->max_timeout = cur_gpio->max_timeout;
119 gpio_dat->active_low = cur_gpio->active_low;
120 gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low);
99f41131
ML
121 }
122
123 platform_set_drvdata(pdev, gpio_data);
124
125 return 0;
126}
127
653d1290 128static int timed_gpio_remove(struct platform_device *pdev)
99f41131
ML
129{
130 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
131 struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev);
132 int i;
133
241e1287
ML
134 for (i = 0; i < pdata->num_gpios; i++)
135 timed_output_dev_unregister(&gpio_data[i].dev);
99f41131
ML
136
137 kfree(gpio_data);
138
139 return 0;
140}
141
653d1290
ML
142static struct platform_driver timed_gpio_driver = {
143 .probe = timed_gpio_probe,
144 .remove = timed_gpio_remove,
99f41131 145 .driver = {
241e1287 146 .name = TIMED_GPIO_NAME,
99f41131
ML
147 .owner = THIS_MODULE,
148 },
149};
150
653d1290 151static int __init timed_gpio_init(void)
99f41131 152{
653d1290 153 return platform_driver_register(&timed_gpio_driver);
99f41131
ML
154}
155
653d1290 156static void __exit timed_gpio_exit(void)
99f41131 157{
653d1290 158 platform_driver_unregister(&timed_gpio_driver);
99f41131
ML
159}
160
653d1290
ML
161module_init(timed_gpio_init);
162module_exit(timed_gpio_exit);
99f41131
ML
163
164MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
653d1290 165MODULE_DESCRIPTION("timed gpio driver");
99f41131 166MODULE_LICENSE("GPL");