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