summaryrefslogtreecommitdiff
path: root/drivers/misc/vib-gpio.c
blob: c893bd544e48ec214e95323fb292ddb17676f2be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* drivers/misc/vib-gpio.c
 *
 * Copyright (C) 2013 Motorola, Inc.
 * Copyright (C) 2008 Google, Inc.
 * Author: Mike Lockwood <lockwood@android.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/hrtimer.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/workqueue.h>
#include <linux/slab.h>
#include <linux/regulator/consumer.h>
#include <linux/of.h>
#include <linux/wakelock.h>

/* TODO: replace with correct header */
#include "../staging/android/timed_output.h"

struct vib_gpio_data {
	struct timed_output_dev dev;
	struct work_struct vib_work;
	struct hrtimer timer;
	spinlock_t lock;
	struct mutex io_mutex; /* protect GPIO & regulator operations */
	struct wake_lock wake_lock;

	struct regulator *reg;
	int gpio;
	int max_timeout;
	bool active_low;
	int initial_vibrate;

	int vib_power_state;
	int vib_state;
};

static int power_on(struct vib_gpio_data *vib_data)
{
	if (vib_data->reg) {
		dev_dbg(vib_data->dev.dev, "enable regulator\n");
		return regulator_enable(vib_data->reg);
	}
	return 0;
}

static int power_off(struct vib_gpio_data *vib_data)
{
	if (vib_data->reg) {
		dev_dbg(vib_data->dev.dev, "disable regulator\n");
		return regulator_disable(vib_data->reg);
	}
	return 0;
}

static void vib_gpio_set(struct vib_gpio_data *vib_data, int on)
{
	dev_dbg(vib_data->dev.dev, "%s(%d)\n", __func__, on);

	mutex_lock(&(vib_data->io_mutex));

	if (on) {
		if (!wake_lock_active(&vib_data->wake_lock))
			wake_lock(&vib_data->wake_lock);

		if (!vib_data->vib_power_state) {
			power_on(vib_data);
			vib_data->vib_power_state = 1;
		}
		if (vib_data->gpio >= 0)
			gpio_direction_output(vib_data->gpio,
					      vib_data->active_low ?  0 : 1);
	} else {
		if (vib_data->gpio >= 0)
			gpio_direction_output(vib_data->gpio,
					      vib_data->active_low ?  1 : 0);

		if (vib_data->vib_power_state) {
			power_off(vib_data);
			vib_data->vib_power_state = 0;
		}

		wake_unlock(&vib_data->wake_lock);
	}

	mutex_unlock(&(vib_data->io_mutex));
}

static void vib_gpio_update(struct work_struct *work)
{
	struct vib_gpio_data *vib_data;

	vib_data = container_of(work, struct vib_gpio_data, vib_work);
	if (vib_data)
		vib_gpio_set(vib_data, vib_data->vib_state);
}

static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer)
{
	struct vib_gpio_data *vib_data =
	    container_of(timer, struct vib_gpio_data, timer);
	dev_dbg(vib_data->dev.dev, "Timer expired: disabling vibrator\n");
	vib_data->vib_state = 0;
	schedule_work(&vib_data->vib_work);
	return HRTIMER_NORESTART;
}

static int vib_gpio_get_time(struct timed_output_dev *dev)
{
	struct vib_gpio_data *vib_data =
	    container_of(dev, struct vib_gpio_data, dev);

	if (hrtimer_active(&vib_data->timer)) {
		ktime_t r = hrtimer_get_remaining(&vib_data->timer);
		struct timeval t = ktime_to_timeval(r);
		return t.tv_sec * 1000 + t.tv_usec / 1000;
	} else
		return 0;
}

static void vib_gpio_enable(struct timed_output_dev *dev, int value)
{
	struct vib_gpio_data *vib_data =
	    container_of(dev, struct vib_gpio_data, dev);
	unsigned long flags;

	dev_dbg(dev->dev, "Enable vibrator for %dms\n", value);

	spin_lock_irqsave(&vib_data->lock, flags);
	hrtimer_cancel(&vib_data->timer);

	if (value == 0)
		vib_data->vib_state = 0;
	else {
		value = (value > vib_data->max_timeout ?
				 vib_data->max_timeout : value);
		vib_data->vib_state = 1;
		hrtimer_start(&vib_data->timer,
			      ktime_set(value / 1000, (value % 1000) * 1000000),
			      HRTIMER_MODE_REL);
	}

	spin_unlock_irqrestore(&vib_data->lock, flags);

	schedule_work(&vib_data->vib_work);
}

static int vib_gpio_probe(struct platform_device *pdev)
{
	struct vib_gpio_data *vib_data;
	struct device_node *np;
	unsigned int prop;
	int ret = 0;

	vib_data = kzalloc(sizeof(struct vib_gpio_data), GFP_KERNEL);
	if (!vib_data) {
		ret = -ENOMEM;
		goto err;
	}

	mutex_init(&(vib_data->io_mutex));

	wake_lock_init(&vib_data->wake_lock, WAKE_LOCK_SUSPEND, "vibrator");

	platform_set_drvdata(pdev, vib_data);

	vib_data->gpio = -1;
	vib_data->active_low = 0;
	vib_data->initial_vibrate = 0;
	vib_data->max_timeout = 1500;
	vib_data->reg = NULL;
#ifdef CONFIG_OF
	np = pdev->dev.of_node;
	if (!np) {
		dev_err(&pdev->dev, "required device_tree entry not found\n");
		goto destroy_wakelock;
	}

	if (!of_property_read_u32(np, "gpio", &prop))
		vib_data->gpio = prop;

	if (!of_property_read_u32(np, "max-timeout", &prop))
		vib_data->max_timeout = prop;

	if (!of_property_read_u32(np, "active-low", &prop))
		vib_data->active_low = prop;

	if (!of_property_read_u32(np, "initial-vibrate", &prop))
		vib_data->initial_vibrate = prop;

#endif
	vib_data->reg = regulator_get(&pdev->dev, "vib-gpio");
	if (IS_ERR(vib_data->reg)) {
		ret = PTR_ERR(vib_data->reg);
		goto destroy_wakelock;
	}

	INIT_WORK(&vib_data->vib_work, vib_gpio_update);

	hrtimer_init(&vib_data->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);

	vib_data->timer.function = gpio_timer_func;
	spin_lock_init(&vib_data->lock);

	vib_data->dev.name = "vibrator";
	vib_data->dev.get_time = vib_gpio_get_time;
	vib_data->dev.enable = vib_gpio_enable;
	ret = timed_output_dev_register(&vib_data->dev);
	if (ret < 0)
		goto reg_put;

	if (vib_data->gpio >= 0)
		gpio_direction_output(vib_data->gpio,
				      vib_data->active_low);

	vib_gpio_enable(&vib_data->dev, vib_data->initial_vibrate);

	pr_info("vib gpio probe done\n");
	return 0;

reg_put:
	regulator_put(vib_data->reg);
destroy_wakelock:
	wake_lock_destroy(&vib_data->wake_lock);
	mutex_destroy(&(vib_data->io_mutex));
	kfree(vib_data);
err:
	return ret;
}

static int vib_gpio_remove(struct platform_device *pdev)
{
	struct vib_gpio_data *vib_data = platform_get_drvdata(pdev);

	timed_output_dev_unregister(&vib_data->dev);
	regulator_put(vib_data->reg);
	if (wake_lock_active(&vib_data->wake_lock))
		wake_unlock(&vib_data->wake_lock);
	wake_lock_destroy(&vib_data->wake_lock);
	mutex_destroy(&(vib_data->io_mutex));
	kfree(vib_data);

	return 0;
}

#ifdef CONFIG_OF
static struct of_device_id vib_gpio_of_match[] = {
	{ .compatible = "mot,vib-gpio" },
	{ }, };
MODULE_DEVICE_TABLE(of, vib_gpio_of_match);
#endif

static struct platform_driver vib_gpio_driver = {
	.probe = vib_gpio_probe,
	.remove = vib_gpio_remove,
	.driver = {
		   .name = "vib-gpio",
		   .owner = THIS_MODULE,
#ifdef CONFIG_OF
		   .of_match_table = of_match_ptr(vib_gpio_of_match),
#endif
		   },
};

static int __init vib_gpio_init(void)
{
	return platform_driver_register(&vib_gpio_driver);
}

static void __exit vib_gpio_exit(void)
{
	platform_driver_unregister(&vib_gpio_driver);
}

late_initcall(vib_gpio_init);
module_exit(vib_gpio_exit);

MODULE_AUTHOR("Motorola");
MODULE_DESCRIPTION("vib gpio driver");
MODULE_LICENSE("GPL");