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 --- a/drivers/staging/android/Kconfig
23 +++ b/drivers/staging/android/Kconfig
24 @@ -73,9 +73,13 @@ config ANDROID_RAM_CONSOLE_EARLY_SIZE
26 depends on ANDROID_RAM_CONSOLE_EARLY_INIT
28 +config ANDROID_TIMED_OUTPUT
29 + bool "Timed output class driver"
32 config ANDROID_TIMED_GPIO
33 tristate "Android timed gpio driver"
34 - depends on GENERIC_GPIO
35 + depends on GENERIC_GPIO && ANDROID_TIMED_OUTPUT
38 config ANDROID_LOW_MEMORY_KILLER
39 --- a/drivers/staging/android/Makefile
40 +++ b/drivers/staging/android/Makefile
42 obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o
43 obj-$(CONFIG_ANDROID_LOGGER) += logger.o
44 obj-$(CONFIG_ANDROID_RAM_CONSOLE) += ram_console.o
45 +obj-$(CONFIG_ANDROID_TIMED_OUTPUT) += timed_output.o
46 obj-$(CONFIG_ANDROID_TIMED_GPIO) += timed_gpio.o
47 obj-$(CONFIG_ANDROID_LOW_MEMORY_KILLER) += lowmemorykiller.o
48 --- a/drivers/staging/android/timed_gpio.c
49 +++ b/drivers/staging/android/timed_gpio.c
51 #include <linux/err.h>
52 #include <linux/gpio.h>
54 +#include "timed_output.h"
55 #include "timed_gpio.h"
58 -static struct class *timed_gpio_class;
60 struct timed_gpio_data {
62 + struct timed_output_dev dev;
66 @@ -36,70 +35,62 @@ struct timed_gpio_data {
68 static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
70 - struct timed_gpio_data *gpio_data = container_of(timer, struct timed_gpio_data, timer);
71 + struct timed_gpio_data *data =
72 + container_of(timer, struct timed_gpio_data, timer);
74 - gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? 1 : 0);
75 + gpio_direction_output(data->gpio, data->active_low ? 1 : 0);
76 return HRTIMER_NORESTART;
79 -static ssize_t gpio_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
80 +static int gpio_get_time(struct timed_output_dev *dev)
82 - struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
84 + struct timed_gpio_data *data =
85 + container_of(dev, struct timed_gpio_data, dev);
87 - if (hrtimer_active(&gpio_data->timer)) {
88 - ktime_t r = hrtimer_get_remaining(&gpio_data->timer);
89 + if (hrtimer_active(&data->timer)) {
90 + ktime_t r = hrtimer_get_remaining(&data->timer);
91 struct timeval t = ktime_to_timeval(r);
92 - remaining = t.tv_sec * 1000 + t.tv_usec / 1000;
93 + return t.tv_sec * 1000 + t.tv_usec / 1000;
97 - return sprintf(buf, "%d\n", remaining);
101 -static ssize_t gpio_enable_store(
102 - struct device *dev, struct device_attribute *attr,
103 - const char *buf, size_t size)
104 +static void gpio_enable(struct timed_output_dev *dev, int value)
106 - struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
108 + struct timed_gpio_data *data =
109 + container_of(dev, struct timed_gpio_data, dev);
112 - sscanf(buf, "%d", &value);
114 - spin_lock_irqsave(&gpio_data->lock, flags);
115 + spin_lock_irqsave(&data->lock, flags);
117 /* cancel previous timer and set GPIO according to value */
118 - hrtimer_cancel(&gpio_data->timer);
119 - gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? !value : !!value);
120 + hrtimer_cancel(&data->timer);
121 + gpio_direction_output(data->gpio, data->active_low ? !value : !!value);
124 - if (value > gpio_data->max_timeout)
125 - value = gpio_data->max_timeout;
126 + if (value > data->max_timeout)
127 + value = data->max_timeout;
129 - hrtimer_start(&gpio_data->timer,
130 - ktime_set(value / 1000, (value % 1000) * 1000000),
132 + hrtimer_start(&data->timer,
133 + ktime_set(value / 1000, (value % 1000) * 1000000),
137 - spin_unlock_irqrestore(&gpio_data->lock, flags);
140 + spin_unlock_irqrestore(&data->lock, flags);
143 -static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, gpio_enable_show, gpio_enable_store);
145 static int timed_gpio_probe(struct platform_device *pdev)
147 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
148 struct timed_gpio *cur_gpio;
149 struct timed_gpio_data *gpio_data, *gpio_dat;
156 - gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios, GFP_KERNEL);
157 + gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios,
162 @@ -107,23 +98,26 @@ static int timed_gpio_probe(struct platf
163 cur_gpio = &pdata->gpios[i];
164 gpio_dat = &gpio_data[i];
166 - hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
167 + hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC,
169 gpio_dat->timer.function = gpio_timer_func;
170 spin_lock_init(&gpio_dat->lock);
172 + gpio_dat->dev.name = cur_gpio->name;
173 + gpio_dat->dev.get_time = gpio_get_time;
174 + gpio_dat->dev.enable = gpio_enable;
175 + ret = timed_output_dev_register(&gpio_dat->dev);
177 + for (j = 0; j < i; j++)
178 + timed_output_dev_unregister(&gpio_data[i].dev);
183 gpio_dat->gpio = cur_gpio->gpio;
184 gpio_dat->max_timeout = cur_gpio->max_timeout;
185 gpio_dat->active_low = cur_gpio->active_low;
186 gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low);
188 - gpio_dat->dev = device_create(timed_gpio_class, &pdev->dev, 0, "%s", cur_gpio->name);
189 - if (unlikely(IS_ERR(gpio_dat->dev)))
190 - return PTR_ERR(gpio_dat->dev);
192 - dev_set_drvdata(gpio_dat->dev, gpio_dat);
193 - ret = device_create_file(gpio_dat->dev, &dev_attr_enable);
198 platform_set_drvdata(pdev, gpio_data);
199 @@ -137,10 +131,8 @@ static int timed_gpio_remove(struct plat
200 struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev);
203 - for (i = 0; i < pdata->num_gpios; i++) {
204 - device_remove_file(gpio_data[i].dev, &dev_attr_enable);
205 - device_unregister(gpio_data[i].dev);
207 + for (i = 0; i < pdata->num_gpios; i++)
208 + timed_output_dev_unregister(&gpio_data[i].dev);
212 @@ -151,22 +143,18 @@ static struct platform_driver timed_gpio
213 .probe = timed_gpio_probe,
214 .remove = timed_gpio_remove,
216 - .name = "timed-gpio",
217 + .name = TIMED_GPIO_NAME,
218 .owner = THIS_MODULE,
222 static int __init timed_gpio_init(void)
224 - timed_gpio_class = class_create(THIS_MODULE, "timed_output");
225 - if (IS_ERR(timed_gpio_class))
226 - return PTR_ERR(timed_gpio_class);
227 return platform_driver_register(&timed_gpio_driver);
230 static void __exit timed_gpio_exit(void)
232 - class_destroy(timed_gpio_class);
233 platform_driver_unregister(&timed_gpio_driver);
236 --- a/drivers/staging/android/timed_gpio.h
237 +++ b/drivers/staging/android/timed_gpio.h
239 #ifndef _LINUX_TIMED_GPIO_H
240 #define _LINUX_TIMED_GPIO_H
242 +#define TIMED_GPIO_NAME "timed-gpio"
253 +++ b/drivers/staging/android/timed_output.c
255 +/* drivers/misc/timed_output.c
257 + * Copyright (C) 2009 Google, Inc.
258 + * Author: Mike Lockwood <lockwood@android.com>
260 + * This software is licensed under the terms of the GNU General Public
261 + * License version 2, as published by the Free Software Foundation, and
262 + * may be copied, distributed, and modified under those terms.
264 + * This program is distributed in the hope that it will be useful,
265 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
266 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
267 + * GNU General Public License for more details.
271 +#include <linux/module.h>
272 +#include <linux/types.h>
273 +#include <linux/device.h>
274 +#include <linux/fs.h>
275 +#include <linux/err.h>
277 +#include "timed_output.h"
279 +static struct class *timed_output_class;
280 +static atomic_t device_count;
282 +static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
285 + struct timed_output_dev *tdev = dev_get_drvdata(dev);
286 + int remaining = tdev->get_time(tdev);
288 + return sprintf(buf, "%d\n", remaining);
291 +static ssize_t enable_store(
292 + struct device *dev, struct device_attribute *attr,
293 + const char *buf, size_t size)
295 + struct timed_output_dev *tdev = dev_get_drvdata(dev);
298 + sscanf(buf, "%d", &value);
299 + tdev->enable(tdev, value);
304 +static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
306 +static int create_timed_output_class(void)
308 + if (!timed_output_class) {
309 + timed_output_class = class_create(THIS_MODULE, "timed_output");
310 + if (IS_ERR(timed_output_class))
311 + return PTR_ERR(timed_output_class);
312 + atomic_set(&device_count, 0);
318 +int timed_output_dev_register(struct timed_output_dev *tdev)
322 + if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time)
325 + ret = create_timed_output_class();
329 + tdev->index = atomic_inc_return(&device_count);
330 + tdev->dev = device_create(timed_output_class, NULL,
331 + MKDEV(0, tdev->index), NULL, tdev->name);
332 + if (IS_ERR(tdev->dev))
333 + return PTR_ERR(tdev->dev);
335 + ret = device_create_file(tdev->dev, &dev_attr_enable);
337 + goto err_create_file;
339 + dev_set_drvdata(tdev->dev, tdev);
344 + device_destroy(timed_output_class, MKDEV(0, tdev->index));
345 + printk(KERN_ERR "timed_output: Failed to register driver %s\n",
350 +EXPORT_SYMBOL_GPL(timed_output_dev_register);
352 +void timed_output_dev_unregister(struct timed_output_dev *tdev)
354 + device_remove_file(tdev->dev, &dev_attr_enable);
355 + device_destroy(timed_output_class, MKDEV(0, tdev->index));
356 + dev_set_drvdata(tdev->dev, NULL);
358 +EXPORT_SYMBOL_GPL(timed_output_dev_unregister);
360 +static int __init timed_output_init(void)
362 + return create_timed_output_class();
365 +static void __exit timed_output_exit(void)
367 + class_destroy(timed_output_class);
370 +module_init(timed_output_init);
371 +module_exit(timed_output_exit);
373 +MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
374 +MODULE_DESCRIPTION("timed output class driver");
375 +MODULE_LICENSE("GPL");
377 +++ b/drivers/staging/android/timed_output.h
379 +/* include/linux/timed_output.h
381 + * Copyright (C) 2008 Google, Inc.
383 + * This software is licensed under the terms of the GNU General Public
384 + * License version 2, as published by the Free Software Foundation, and
385 + * may be copied, distributed, and modified under those terms.
387 + * This program is distributed in the hope that it will be useful,
388 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
389 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
390 + * GNU General Public License for more details.
394 +#ifndef _LINUX_TIMED_OUTPUT_H
395 +#define _LINUX_TIMED_OUTPUT_H
397 +struct timed_output_dev {
400 + /* enable the output and set the timer */
401 + void (*enable)(struct timed_output_dev *sdev, int timeout);
403 + /* returns the current number of milliseconds remaining on the timer */
404 + int (*get_time)(struct timed_output_dev *sdev);
407 + struct device *dev;
412 +extern int timed_output_dev_register(struct timed_output_dev *dev);
413 +extern void timed_output_dev_unregister(struct timed_output_dev *dev);