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 <linux/gpio.h>
23 #include "timed_output.h"
24 #include "timed_gpio.h"
27 struct timed_gpio_data
{
28 struct timed_output_dev dev
;
36 static enum hrtimer_restart
gpio_timer_func(struct hrtimer
*timer
)
38 struct timed_gpio_data
*data
=
39 container_of(timer
, struct timed_gpio_data
, timer
);
41 gpio_direction_output(data
->gpio
, data
->active_low
? 1 : 0);
42 return HRTIMER_NORESTART
;
45 static int gpio_get_time(struct timed_output_dev
*dev
)
47 struct timed_gpio_data
*data
=
48 container_of(dev
, struct timed_gpio_data
, dev
);
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;
58 static void gpio_enable(struct timed_output_dev
*dev
, int value
)
60 struct timed_gpio_data
*data
=
61 container_of(dev
, struct timed_gpio_data
, dev
);
64 spin_lock_irqsave(&data
->lock
, flags
);
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
);
71 if (value
> data
->max_timeout
)
72 value
= data
->max_timeout
;
74 hrtimer_start(&data
->timer
,
75 ktime_set(value
/ 1000, (value
% 1000) * 1000000),
79 spin_unlock_irqrestore(&data
->lock
, flags
);
82 static int timed_gpio_probe(struct platform_device
*pdev
)
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
;
92 gpio_data
= kzalloc(sizeof(struct timed_gpio_data
) * pdata
->num_gpios
,
97 for (i
= 0; i
< pdata
->num_gpios
; i
++) {
98 cur_gpio
= &pdata
->gpios
[i
];
99 gpio_dat
= &gpio_data
[i
];
101 hrtimer_init(&gpio_dat
->timer
, CLOCK_MONOTONIC
,
103 gpio_dat
->timer
.function
= gpio_timer_func
;
104 spin_lock_init(&gpio_dat
->lock
);
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
);
111 for (j
= 0; j
< i
; j
++)
112 timed_output_dev_unregister(&gpio_data
[i
].dev
);
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
);
123 platform_set_drvdata(pdev
, gpio_data
);
128 static int timed_gpio_remove(struct platform_device
*pdev
)
130 struct timed_gpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
131 struct timed_gpio_data
*gpio_data
= platform_get_drvdata(pdev
);
134 for (i
= 0; i
< pdata
->num_gpios
; i
++)
135 timed_output_dev_unregister(&gpio_data
[i
].dev
);
142 static struct platform_driver timed_gpio_driver
= {
143 .probe
= timed_gpio_probe
,
144 .remove
= timed_gpio_remove
,
146 .name
= TIMED_GPIO_NAME
,
147 .owner
= THIS_MODULE
,
151 static int __init
timed_gpio_init(void)
153 return platform_driver_register(&timed_gpio_driver
);
156 static void __exit
timed_gpio_exit(void)
158 platform_driver_unregister(&timed_gpio_driver
);
161 module_init(timed_gpio_init
);
162 module_exit(timed_gpio_exit
);
164 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
165 MODULE_DESCRIPTION("timed gpio driver");
166 MODULE_LICENSE("GPL");