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.
6 Content-Type: text/plain; charset=utf-8
7 Content-Transfer-Encoding: 8bit
9 Signed-off-by: Mike Lockwood <lockwood@android.com>
10 Signed-off-by: Arve Hjønnevåg <arve@android.com>
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
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
28 depends on ANDROID_RAM_CONSOLE_EARLY_INIT
30 +config ANDROID_TIMED_OUTPUT
31 + bool "Timed output class driver"
34 config ANDROID_TIMED_GPIO
35 tristate "Android timed gpio driver"
36 - depends on GENERIC_GPIO
37 + depends on GENERIC_GPIO && ANDROID_TIMED_OUTPUT
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
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
57 #include <linux/err.h>
58 #include <linux/gpio.h>
60 +#include "timed_output.h"
61 #include "timed_gpio.h"
64 -static struct class *timed_gpio_class;
66 struct timed_gpio_data {
68 + struct timed_output_dev dev;
72 @@ -36,70 +35,62 @@ struct timed_gpio_data {
74 static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
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);
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;
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)
88 - struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
90 + struct timed_gpio_data *data =
91 + container_of(dev, struct timed_gpio_data, dev);
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;
103 - return sprintf(buf, "%d\n", remaining);
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)
112 - struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
114 + struct timed_gpio_data *data =
115 + container_of(dev, struct timed_gpio_data, dev);
118 - sscanf(buf, "%d", &value);
120 - spin_lock_irqsave(&gpio_data->lock, flags);
121 + spin_lock_irqsave(&data->lock, flags);
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);
130 - if (value > gpio_data->max_timeout)
131 - value = gpio_data->max_timeout;
132 + if (value > data->max_timeout)
133 + value = data->max_timeout;
135 - hrtimer_start(&gpio_data->timer,
136 - ktime_set(value / 1000, (value % 1000) * 1000000),
138 + hrtimer_start(&data->timer,
139 + ktime_set(value / 1000, (value % 1000) * 1000000),
143 - spin_unlock_irqrestore(&gpio_data->lock, flags);
146 + spin_unlock_irqrestore(&data->lock, flags);
149 -static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, gpio_enable_show, gpio_enable_store);
151 static int timed_gpio_probe(struct platform_device *pdev)
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;
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,
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];
172 - hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
173 + hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC,
175 gpio_dat->timer.function = gpio_timer_func;
176 spin_lock_init(&gpio_dat->lock);
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);
183 + for (j = 0; j < i; j++)
184 + timed_output_dev_unregister(&gpio_data[i].dev);
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);
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);
198 - dev_set_drvdata(gpio_dat->dev, gpio_dat);
199 - ret = device_create_file(gpio_dat->dev, &dev_attr_enable);
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);
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);
213 + for (i = 0; i < pdata->num_gpios; i++)
214 + timed_output_dev_unregister(&gpio_data[i].dev);
218 @@ -151,22 +143,18 @@ static struct platform_driver timed_gpio_driver = {
219 .probe = timed_gpio_probe,
220 .remove = timed_gpio_remove,
222 - .name = "timed-gpio",
223 + .name = TIMED_GPIO_NAME,
224 .owner = THIS_MODULE,
228 static int __init timed_gpio_init(void)
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);
236 static void __exit timed_gpio_exit(void)
238 - class_destroy(timed_gpio_class);
239 platform_driver_unregister(&timed_gpio_driver);
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
247 #ifndef _LINUX_TIMED_GPIO_H
248 #define _LINUX_TIMED_GPIO_H
250 +#define TIMED_GPIO_NAME "timed-gpio"
260 diff --git a/drivers/staging/android/timed_output.c b/drivers/staging/android/timed_output.c
262 index 0000000..62e7918
264 +++ b/drivers/staging/android/timed_output.c
266 +/* drivers/misc/timed_output.c
268 + * Copyright (C) 2009 Google, Inc.
269 + * Author: Mike Lockwood <lockwood@android.com>
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.
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.
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>
288 +#include "timed_output.h"
290 +static struct class *timed_output_class;
291 +static atomic_t device_count;
293 +static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
296 + struct timed_output_dev *tdev = dev_get_drvdata(dev);
297 + int remaining = tdev->get_time(tdev);
299 + return sprintf(buf, "%d\n", remaining);
302 +static ssize_t enable_store(
303 + struct device *dev, struct device_attribute *attr,
304 + const char *buf, size_t size)
306 + struct timed_output_dev *tdev = dev_get_drvdata(dev);
309 + sscanf(buf, "%d", &value);
310 + tdev->enable(tdev, value);
315 +static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
317 +static int create_timed_output_class(void)
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);
329 +int timed_output_dev_register(struct timed_output_dev *tdev)
333 + if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time)
336 + ret = create_timed_output_class();
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);
346 + ret = device_create_file(tdev->dev, &dev_attr_enable);
348 + goto err_create_file;
350 + dev_set_drvdata(tdev->dev, tdev);
355 + device_destroy(timed_output_class, MKDEV(0, tdev->index));
356 + printk(KERN_ERR "timed_output: Failed to register driver %s\n",
361 +EXPORT_SYMBOL_GPL(timed_output_dev_register);
363 +void timed_output_dev_unregister(struct timed_output_dev *tdev)
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);
369 +EXPORT_SYMBOL_GPL(timed_output_dev_unregister);
371 +static int __init timed_output_init(void)
373 + return create_timed_output_class();
376 +static void __exit timed_output_exit(void)
378 + class_destroy(timed_output_class);
381 +module_init(timed_output_init);
382 +module_exit(timed_output_exit);
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
389 index 0000000..ec907ab
391 +++ b/drivers/staging/android/timed_output.h
393 +/* include/linux/timed_output.h
395 + * Copyright (C) 2008 Google, Inc.
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.
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.
408 +#ifndef _LINUX_TIMED_OUTPUT_H
409 +#define _LINUX_TIMED_OUTPUT_H
411 +struct timed_output_dev {
414 + /* enable the output and set the timer */
415 + void (*enable)(struct timed_output_dev *sdev, int timeout);
417 + /* returns the current number of milliseconds remaining on the timer */
418 + int (*get_time)(struct timed_output_dev *sdev);
421 + struct device *dev;
426 +extern int timed_output_dev_register(struct timed_output_dev *dev);
427 +extern void timed_output_dev_unregister(struct timed_output_dev *dev);