Partially fix commit r16660
[openwrt.git] / target / linux / goldfish / patches-2.6.30 / 0054-timed_gpio-Separate-timed_output-class-into-a-separ.patch
1 From e4c9c5d7d6d5deb124083678fe5d839d3133f60a Mon Sep 17 00:00:00 2001
2 From: Mike Lockwood <lockwood@android.com>
3 Date: Mon, 12 Jan 2009 13:25:05 -0500
4 Subject: [PATCH 054/134] timed_gpio: Separate timed_output class into a separate driver.
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=utf-8
7 Content-Transfer-Encoding: 8bit
8
9 Signed-off-by: Mike Lockwood <lockwood@android.com>
10 Signed-off-by: Arve Hjønnevåg <arve@android.com>
11 ---
12 drivers/staging/android/Kconfig | 6 ++-
13 drivers/staging/android/Makefile | 1 +
14 drivers/staging/android/timed_gpio.c | 98 +++++++++++--------------
15 drivers/staging/android/timed_gpio.h | 4 +-
16 drivers/staging/android/timed_output.c | 121 ++++++++++++++++++++++++++++++++
17 drivers/staging/android/timed_output.h | 37 ++++++++++
18 6 files changed, 210 insertions(+), 57 deletions(-)
19 create mode 100644 drivers/staging/android/timed_output.c
20 create mode 100644 drivers/staging/android/timed_output.h
21
22 diff --git a/drivers/staging/android/Kconfig b/drivers/staging/android/Kconfig
23 index 604bd1e..1784508 100644
24 --- a/drivers/staging/android/Kconfig
25 +++ b/drivers/staging/android/Kconfig
26 @@ -73,9 +73,13 @@ config ANDROID_RAM_CONSOLE_EARLY_SIZE
27 default 0
28 depends on ANDROID_RAM_CONSOLE_EARLY_INIT
29
30 +config ANDROID_TIMED_OUTPUT
31 + bool "Timed output class driver"
32 + default y
33 +
34 config ANDROID_TIMED_GPIO
35 tristate "Android timed gpio driver"
36 - depends on GENERIC_GPIO
37 + depends on GENERIC_GPIO && ANDROID_TIMED_OUTPUT
38 default n
39
40 config ANDROID_LOW_MEMORY_KILLER
41 diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
42 index 95209d6..8e057e6 100644
43 --- a/drivers/staging/android/Makefile
44 +++ b/drivers/staging/android/Makefile
45 @@ -1,5 +1,6 @@
46 obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o
47 obj-$(CONFIG_ANDROID_LOGGER) += logger.o
48 obj-$(CONFIG_ANDROID_RAM_CONSOLE) += ram_console.o
49 +obj-$(CONFIG_ANDROID_TIMED_OUTPUT) += timed_output.o
50 obj-$(CONFIG_ANDROID_TIMED_GPIO) += timed_gpio.o
51 obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER) += lowmemorykiller.o
52 diff --git a/drivers/staging/android/timed_gpio.c b/drivers/staging/android/timed_gpio.c
53 index 33daff0..be7cdaa 100644
54 --- a/drivers/staging/android/timed_gpio.c
55 +++ b/drivers/staging/android/timed_gpio.c
56 @@ -20,13 +20,12 @@
57 #include <linux/err.h>
58 #include <linux/gpio.h>
59
60 +#include "timed_output.h"
61 #include "timed_gpio.h"
62
63
64 -static struct class *timed_gpio_class;
65 -
66 struct timed_gpio_data {
67 - struct device *dev;
68 + struct timed_output_dev dev;
69 struct hrtimer timer;
70 spinlock_t lock;
71 unsigned gpio;
72 @@ -36,70 +35,62 @@ struct timed_gpio_data {
73
74 static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
75 {
76 - struct timed_gpio_data *gpio_data = container_of(timer, struct timed_gpio_data, timer);
77 + struct timed_gpio_data *data =
78 + container_of(timer, struct timed_gpio_data, timer);
79
80 - gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? 1 : 0);
81 + gpio_direction_output(data->gpio, data->active_low ? 1 : 0);
82 return HRTIMER_NORESTART;
83 }
84
85 -static ssize_t gpio_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
86 +static int gpio_get_time(struct timed_output_dev *dev)
87 {
88 - struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
89 - int remaining;
90 + struct timed_gpio_data *data =
91 + container_of(dev, struct timed_gpio_data, dev);
92
93 - if (hrtimer_active(&gpio_data->timer)) {
94 - ktime_t r = hrtimer_get_remaining(&gpio_data->timer);
95 + if (hrtimer_active(&data->timer)) {
96 + ktime_t r = hrtimer_get_remaining(&data->timer);
97 struct timeval t = ktime_to_timeval(r);
98 - remaining = t.tv_sec * 1000 + t.tv_usec / 1000;
99 + return t.tv_sec * 1000 + t.tv_usec / 1000;
100 } else
101 - remaining = 0;
102 -
103 - return sprintf(buf, "%d\n", remaining);
104 + return 0;
105 }
106
107 -static ssize_t gpio_enable_store(
108 - struct device *dev, struct device_attribute *attr,
109 - const char *buf, size_t size)
110 +static void gpio_enable(struct timed_output_dev *dev, int value)
111 {
112 - struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
113 - int value;
114 + struct timed_gpio_data *data =
115 + container_of(dev, struct timed_gpio_data, dev);
116 unsigned long flags;
117
118 - sscanf(buf, "%d", &value);
119 -
120 - spin_lock_irqsave(&gpio_data->lock, flags);
121 + spin_lock_irqsave(&data->lock, flags);
122
123 /* cancel previous timer and set GPIO according to value */
124 - hrtimer_cancel(&gpio_data->timer);
125 - gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? !value : !!value);
126 + hrtimer_cancel(&data->timer);
127 + gpio_direction_output(data->gpio, data->active_low ? !value : !!value);
128
129 if (value > 0) {
130 - if (value > gpio_data->max_timeout)
131 - value = gpio_data->max_timeout;
132 + if (value > data->max_timeout)
133 + value = data->max_timeout;
134
135 - hrtimer_start(&gpio_data->timer,
136 - ktime_set(value / 1000, (value % 1000) * 1000000),
137 - HRTIMER_MODE_REL);
138 + hrtimer_start(&data->timer,
139 + ktime_set(value / 1000, (value % 1000) * 1000000),
140 + HRTIMER_MODE_REL);
141 }
142
143 - spin_unlock_irqrestore(&gpio_data->lock, flags);
144 -
145 - return size;
146 + spin_unlock_irqrestore(&data->lock, flags);
147 }
148
149 -static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, gpio_enable_show, gpio_enable_store);
150 -
151 static int timed_gpio_probe(struct platform_device *pdev)
152 {
153 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
154 struct timed_gpio *cur_gpio;
155 struct timed_gpio_data *gpio_data, *gpio_dat;
156 - int i, ret = 0;
157 + int i, j, ret = 0;
158
159 if (!pdata)
160 return -EBUSY;
161
162 - gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios, GFP_KERNEL);
163 + gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios,
164 + GFP_KERNEL);
165 if (!gpio_data)
166 return -ENOMEM;
167
168 @@ -107,23 +98,26 @@ static int timed_gpio_probe(struct platform_device *pdev)
169 cur_gpio = &pdata->gpios[i];
170 gpio_dat = &gpio_data[i];
171
172 - hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
173 + hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC,
174 + HRTIMER_MODE_REL);
175 gpio_dat->timer.function = gpio_timer_func;
176 spin_lock_init(&gpio_dat->lock);
177
178 + gpio_dat->dev.name = cur_gpio->name;
179 + gpio_dat->dev.get_time = gpio_get_time;
180 + gpio_dat->dev.enable = gpio_enable;
181 + ret = timed_output_dev_register(&gpio_dat->dev);
182 + if (ret < 0) {
183 + for (j = 0; j < i; j++)
184 + timed_output_dev_unregister(&gpio_data[i].dev);
185 + kfree(gpio_data);
186 + return ret;
187 + }
188 +
189 gpio_dat->gpio = cur_gpio->gpio;
190 gpio_dat->max_timeout = cur_gpio->max_timeout;
191 gpio_dat->active_low = cur_gpio->active_low;
192 gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low);
193 -
194 - gpio_dat->dev = device_create(timed_gpio_class, &pdev->dev, 0, "%s", cur_gpio->name);
195 - if (unlikely(IS_ERR(gpio_dat->dev)))
196 - return PTR_ERR(gpio_dat->dev);
197 -
198 - dev_set_drvdata(gpio_dat->dev, gpio_dat);
199 - ret = device_create_file(gpio_dat->dev, &dev_attr_enable);
200 - if (ret)
201 - return ret;
202 }
203
204 platform_set_drvdata(pdev, gpio_data);
205 @@ -137,10 +131,8 @@ static int timed_gpio_remove(struct platform_device *pdev)
206 struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev);
207 int i;
208
209 - for (i = 0; i < pdata->num_gpios; i++) {
210 - device_remove_file(gpio_data[i].dev, &dev_attr_enable);
211 - device_unregister(gpio_data[i].dev);
212 - }
213 + for (i = 0; i < pdata->num_gpios; i++)
214 + timed_output_dev_unregister(&gpio_data[i].dev);
215
216 kfree(gpio_data);
217
218 @@ -151,22 +143,18 @@ static struct platform_driver timed_gpio_driver = {
219 .probe = timed_gpio_probe,
220 .remove = timed_gpio_remove,
221 .driver = {
222 - .name = "timed-gpio",
223 + .name = TIMED_GPIO_NAME,
224 .owner = THIS_MODULE,
225 },
226 };
227
228 static int __init timed_gpio_init(void)
229 {
230 - timed_gpio_class = class_create(THIS_MODULE, "timed_output");
231 - if (IS_ERR(timed_gpio_class))
232 - return PTR_ERR(timed_gpio_class);
233 return platform_driver_register(&timed_gpio_driver);
234 }
235
236 static void __exit timed_gpio_exit(void)
237 {
238 - class_destroy(timed_gpio_class);
239 platform_driver_unregister(&timed_gpio_driver);
240 }
241
242 diff --git a/drivers/staging/android/timed_gpio.h b/drivers/staging/android/timed_gpio.h
243 index 78449b2..a0e15f8 100644
244 --- a/drivers/staging/android/timed_gpio.h
245 +++ b/drivers/staging/android/timed_gpio.h
246 @@ -16,10 +16,12 @@
247 #ifndef _LINUX_TIMED_GPIO_H
248 #define _LINUX_TIMED_GPIO_H
249
250 +#define TIMED_GPIO_NAME "timed-gpio"
251 +
252 struct timed_gpio {
253 const char *name;
254 unsigned gpio;
255 - int max_timeout;
256 + int max_timeout;
257 u8 active_low;
258 };
259
260 diff --git a/drivers/staging/android/timed_output.c b/drivers/staging/android/timed_output.c
261 new file mode 100644
262 index 0000000..62e7918
263 --- /dev/null
264 +++ b/drivers/staging/android/timed_output.c
265 @@ -0,0 +1,121 @@
266 +/* drivers/misc/timed_output.c
267 + *
268 + * Copyright (C) 2009 Google, Inc.
269 + * Author: Mike Lockwood <lockwood@android.com>
270 + *
271 + * This software is licensed under the terms of the GNU General Public
272 + * License version 2, as published by the Free Software Foundation, and
273 + * may be copied, distributed, and modified under those terms.
274 + *
275 + * This program is distributed in the hope that it will be useful,
276 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
277 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
278 + * GNU General Public License for more details.
279 + *
280 + */
281 +
282 +#include <linux/module.h>
283 +#include <linux/types.h>
284 +#include <linux/device.h>
285 +#include <linux/fs.h>
286 +#include <linux/err.h>
287 +
288 +#include "timed_output.h"
289 +
290 +static struct class *timed_output_class;
291 +static atomic_t device_count;
292 +
293 +static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
294 + char *buf)
295 +{
296 + struct timed_output_dev *tdev = dev_get_drvdata(dev);
297 + int remaining = tdev->get_time(tdev);
298 +
299 + return sprintf(buf, "%d\n", remaining);
300 +}
301 +
302 +static ssize_t enable_store(
303 + struct device *dev, struct device_attribute *attr,
304 + const char *buf, size_t size)
305 +{
306 + struct timed_output_dev *tdev = dev_get_drvdata(dev);
307 + int value;
308 +
309 + sscanf(buf, "%d", &value);
310 + tdev->enable(tdev, value);
311 +
312 + return size;
313 +}
314 +
315 +static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
316 +
317 +static int create_timed_output_class(void)
318 +{
319 + if (!timed_output_class) {
320 + timed_output_class = class_create(THIS_MODULE, "timed_output");
321 + if (IS_ERR(timed_output_class))
322 + return PTR_ERR(timed_output_class);
323 + atomic_set(&device_count, 0);
324 + }
325 +
326 + return 0;
327 +}
328 +
329 +int timed_output_dev_register(struct timed_output_dev *tdev)
330 +{
331 + int ret;
332 +
333 + if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time)
334 + return -EINVAL;
335 +
336 + ret = create_timed_output_class();
337 + if (ret < 0)
338 + return ret;
339 +
340 + tdev->index = atomic_inc_return(&device_count);
341 + tdev->dev = device_create(timed_output_class, NULL,
342 + MKDEV(0, tdev->index), NULL, tdev->name);
343 + if (IS_ERR(tdev->dev))
344 + return PTR_ERR(tdev->dev);
345 +
346 + ret = device_create_file(tdev->dev, &dev_attr_enable);
347 + if (ret < 0)
348 + goto err_create_file;
349 +
350 + dev_set_drvdata(tdev->dev, tdev);
351 + tdev->state = 0;
352 + return 0;
353 +
354 +err_create_file:
355 + device_destroy(timed_output_class, MKDEV(0, tdev->index));
356 + printk(KERN_ERR "timed_output: Failed to register driver %s\n",
357 + tdev->name);
358 +
359 + return ret;
360 +}
361 +EXPORT_SYMBOL_GPL(timed_output_dev_register);
362 +
363 +void timed_output_dev_unregister(struct timed_output_dev *tdev)
364 +{
365 + device_remove_file(tdev->dev, &dev_attr_enable);
366 + device_destroy(timed_output_class, MKDEV(0, tdev->index));
367 + dev_set_drvdata(tdev->dev, NULL);
368 +}
369 +EXPORT_SYMBOL_GPL(timed_output_dev_unregister);
370 +
371 +static int __init timed_output_init(void)
372 +{
373 + return create_timed_output_class();
374 +}
375 +
376 +static void __exit timed_output_exit(void)
377 +{
378 + class_destroy(timed_output_class);
379 +}
380 +
381 +module_init(timed_output_init);
382 +module_exit(timed_output_exit);
383 +
384 +MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
385 +MODULE_DESCRIPTION("timed output class driver");
386 +MODULE_LICENSE("GPL");
387 diff --git a/drivers/staging/android/timed_output.h b/drivers/staging/android/timed_output.h
388 new file mode 100644
389 index 0000000..ec907ab
390 --- /dev/null
391 +++ b/drivers/staging/android/timed_output.h
392 @@ -0,0 +1,37 @@
393 +/* include/linux/timed_output.h
394 + *
395 + * Copyright (C) 2008 Google, Inc.
396 + *
397 + * This software is licensed under the terms of the GNU General Public
398 + * License version 2, as published by the Free Software Foundation, and
399 + * may be copied, distributed, and modified under those terms.
400 + *
401 + * This program is distributed in the hope that it will be useful,
402 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
403 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
404 + * GNU General Public License for more details.
405 + *
406 +*/
407 +
408 +#ifndef _LINUX_TIMED_OUTPUT_H
409 +#define _LINUX_TIMED_OUTPUT_H
410 +
411 +struct timed_output_dev {
412 + const char *name;
413 +
414 + /* enable the output and set the timer */
415 + void (*enable)(struct timed_output_dev *sdev, int timeout);
416 +
417 + /* returns the current number of milliseconds remaining on the timer */
418 + int (*get_time)(struct timed_output_dev *sdev);
419 +
420 + /* private data */
421 + struct device *dev;
422 + int index;
423 + int state;
424 +};
425 +
426 +extern int timed_output_dev_register(struct timed_output_dev *dev);
427 +extern void timed_output_dev_unregister(struct timed_output_dev *dev);
428 +
429 +#endif
430 --
431 1.6.2
432
This page took 0.064511 seconds and 5 git commands to generate.