x86: unify PM-Timer messages
[firefly-linux-kernel-4.4.55.git] / drivers / staging / android / timed_gpio.c
1 /* drivers/misc/timed_gpio.c
2  *
3  * Copyright (C) 2008 Google, Inc.
4  * Author: Mike Lockwood <lockwood@android.com>
5  *
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.
9  *
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.
14  *
15  */
16
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>
22
23 #include "timed_gpio.h"
24
25
26 static struct class *timed_gpio_class;
27
28 struct timed_gpio_data {
29         struct device *dev;
30         struct hrtimer timer;
31         spinlock_t lock;
32         unsigned        gpio;
33         int             max_timeout;
34         u8              active_low;
35 };
36
37 static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
38 {
39         struct timed_gpio_data *gpio_data = container_of(timer, struct timed_gpio_data, timer);
40
41         gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? 1 : 0);
42         return HRTIMER_NORESTART;
43 }
44
45 static ssize_t gpio_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
46 {
47         struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
48         int remaining;
49
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;
53         } else
54                 remaining = 0;
55
56         return sprintf(buf, "%d\n", remaining);
57 }
58
59 static ssize_t gpio_enable_store(
60                 struct device *dev, struct device_attribute *attr,
61                 const char *buf, size_t size)
62 {
63         struct timed_gpio_data *gpio_data = dev_get_drvdata(dev);
64         int value;
65         unsigned long   flags;
66
67         sscanf(buf, "%d", &value);
68
69         spin_lock_irqsave(&gpio_data->lock, flags);
70
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);
74
75         if (value > 0) {
76                 if (value > gpio_data->max_timeout)
77                         value = gpio_data->max_timeout;
78
79                 hrtimer_start(&gpio_data->timer,
80                                                 ktime_set(value / 1000, (value % 1000) * 1000000),
81                                                 HRTIMER_MODE_REL);
82         }
83
84         spin_unlock_irqrestore(&gpio_data->lock, flags);
85
86         return size;
87 }
88
89 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, gpio_enable_show, gpio_enable_store);
90
91 static int timed_gpio_probe(struct platform_device *pdev)
92 {
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;
96         int i, ret = 0;
97
98         if (!pdata)
99                 return -EBUSY;
100
101         gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios, GFP_KERNEL);
102         if (!gpio_data)
103                 return -ENOMEM;
104
105         for (i = 0; i < pdata->num_gpios; i++) {
106                 cur_gpio = &pdata->gpios[i];
107                 gpio_dat = &gpio_data[i];
108
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);
112
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);
117
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);
121
122                 dev_set_drvdata(gpio_dat->dev, gpio_dat);
123                 ret = device_create_file(gpio_dat->dev, &dev_attr_enable);
124                 if (ret)
125                         return ret;
126         }
127
128         platform_set_drvdata(pdev, gpio_data);
129
130         return 0;
131 }
132
133 static int timed_gpio_remove(struct platform_device *pdev)
134 {
135         struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
136         struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev);
137         int i;
138
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);
142         }
143
144         kfree(gpio_data);
145
146         return 0;
147 }
148
149 static struct platform_driver timed_gpio_driver = {
150         .probe          = timed_gpio_probe,
151         .remove         = timed_gpio_remove,
152         .driver         = {
153                 .name           = "timed-gpio",
154                 .owner          = THIS_MODULE,
155         },
156 };
157
158 static int __init timed_gpio_init(void)
159 {
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);
164 }
165
166 static void __exit timed_gpio_exit(void)
167 {
168         class_destroy(timed_gpio_class);
169         platform_driver_unregister(&timed_gpio_driver);
170 }
171
172 module_init(timed_gpio_init);
173 module_exit(timed_gpio_exit);
174
175 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
176 MODULE_DESCRIPTION("timed gpio driver");
177 MODULE_LICENSE("GPL");