1 /* drivers/misc/timed_gpio.c
3 * Copyright (C) 2008 Google, Inc.
4 * Author: Mike Lockwood <lockwood@android.com>
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.
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.
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/hrtimer.h>
20 #include <linux/err.h>
21 #include <asm/arch/gpio.h>
23 #include "timed_gpio.h"
26 static struct class *timed_gpio_class
;
28 struct timed_gpio_data
{
37 static enum hrtimer_restart
gpio_timer_func(struct hrtimer
*timer
)
39 struct timed_gpio_data
*gpio_data
= container_of(timer
, struct timed_gpio_data
, timer
);
41 gpio_direction_output(gpio_data
->gpio
, gpio_data
->active_low
? 1 : 0);
42 return HRTIMER_NORESTART
;
45 static ssize_t
gpio_enable_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
47 struct timed_gpio_data
*gpio_data
= dev_get_drvdata(dev
);
50 if (hrtimer_active(&gpio_data
->timer
)) {
51 ktime_t r
= hrtimer_get_remaining(&gpio_data
->timer
);
52 remaining
= r
.tv
.sec
* 1000 + r
.tv
.nsec
/ 1000000;
56 return sprintf(buf
, "%d\n", remaining
);
59 static ssize_t
gpio_enable_store(
60 struct device
*dev
, struct device_attribute
*attr
,
61 const char *buf
, size_t size
)
63 struct timed_gpio_data
*gpio_data
= dev_get_drvdata(dev
);
67 sscanf(buf
, "%d", &value
);
69 spin_lock_irqsave(&gpio_data
->lock
, flags
);
71 /* cancel previous timer and set GPIO according to value */
72 hrtimer_cancel(&gpio_data
->timer
);
73 gpio_direction_output(gpio_data
->gpio
, gpio_data
->active_low
? !value
: !!value
);
76 if (value
> gpio_data
->max_timeout
)
77 value
= gpio_data
->max_timeout
;
79 hrtimer_start(&gpio_data
->timer
,
80 ktime_set(value
/ 1000, (value
% 1000) * 1000000),
84 spin_unlock_irqrestore(&gpio_data
->lock
, flags
);
89 static DEVICE_ATTR(enable
, S_IRUGO
| S_IWUSR
, gpio_enable_show
, gpio_enable_store
);
91 static int timed_gpio_probe(struct platform_device
*pdev
)
93 struct timed_gpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
94 struct timed_gpio
*cur_gpio
;
95 struct timed_gpio_data
*gpio_data
, *gpio_dat
;
101 gpio_data
= kzalloc(sizeof(struct timed_gpio_data
) * pdata
->num_gpios
, GFP_KERNEL
);
105 for (i
= 0; i
< pdata
->num_gpios
; i
++) {
106 cur_gpio
= &pdata
->gpios
[i
];
107 gpio_dat
= &gpio_data
[i
];
109 hrtimer_init(&gpio_dat
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
110 gpio_dat
->timer
.function
= gpio_timer_func
;
111 spin_lock_init(&gpio_dat
->lock
);
113 gpio_dat
->gpio
= cur_gpio
->gpio
;
114 gpio_dat
->max_timeout
= cur_gpio
->max_timeout
;
115 gpio_dat
->active_low
= cur_gpio
->active_low
;
116 gpio_direction_output(gpio_dat
->gpio
, gpio_dat
->active_low
);
118 gpio_dat
->dev
= device_create(timed_gpio_class
, &pdev
->dev
, 0, "%s", cur_gpio
->name
);
119 if (unlikely(IS_ERR(gpio_dat
->dev
)))
120 return PTR_ERR(gpio_dat
->dev
);
122 dev_set_drvdata(gpio_dat
->dev
, gpio_dat
);
123 ret
= device_create_file(gpio_dat
->dev
, &dev_attr_enable
);
128 platform_set_drvdata(pdev
, gpio_data
);
133 static int timed_gpio_remove(struct platform_device
*pdev
)
135 struct timed_gpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
136 struct timed_gpio_data
*gpio_data
= platform_get_drvdata(pdev
);
139 for (i
= 0; i
< pdata
->num_gpios
; i
++) {
140 device_remove_file(gpio_data
[i
].dev
, &dev_attr_enable
);
141 device_unregister(gpio_data
[i
].dev
);
149 static struct platform_driver timed_gpio_driver
= {
150 .probe
= timed_gpio_probe
,
151 .remove
= timed_gpio_remove
,
153 .name
= "timed-gpio",
154 .owner
= THIS_MODULE
,
158 static int __init
timed_gpio_init(void)
160 timed_gpio_class
= class_create(THIS_MODULE
, "timed_output");
161 if (IS_ERR(timed_gpio_class
))
162 return PTR_ERR(timed_gpio_class
);
163 return platform_driver_register(&timed_gpio_driver
);
166 static void __exit
timed_gpio_exit(void)
168 class_destroy(timed_gpio_class
);
169 platform_driver_unregister(&timed_gpio_driver
);
172 module_init(timed_gpio_init
);
173 module_exit(timed_gpio_exit
);
175 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
176 MODULE_DESCRIPTION("timed gpio driver");
177 MODULE_LICENSE("GPL");