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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
* android vibrator driver
*
* Copyright (C) 2009-2012 LGE, Inc.
*
* Author: Jinkyu Choi <jinkyu@lge.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/err.h>
#include <linux/android_vibrator.h>
#include <linux/mutex.h>
#include "../staging/android/timed_output.h"
#define ANDROID_VIBRATOR_USE_WORKQUEUE
#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
static struct workqueue_struct *vibrator_workqueue;
#endif
struct timed_vibrator_data {
struct timed_output_dev dev;
struct hrtimer timer;
spinlock_t lock;
int max_timeout;
atomic_t vib_status; /* 1:on,0:off */
atomic_t gain; /* default max gain */
atomic_t pwm;
atomic_t ms_time; /* vibrator duration */
struct android_vibrator_platform_data *pdata;
struct work_struct work_vibrator_off;
struct work_struct work_vibrator_on;
};
#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
static inline void vibrator_work_on(struct work_struct *work)
{
queue_work(vibrator_workqueue, work);
}
static inline void vibrator_work_off(struct work_struct *work)
{
if (!work_pending(work))
queue_work(vibrator_workqueue, work);
}
#else
static inline void vibrator_work_on(struct work_struct *work)
{
schedule_work(work);
}
static inline void vibrator_work_off(struct work_struct *work)
{
if (!work_pending(work))
schedule_work(work);
}
#endif
static int android_vibrator_force_set(struct timed_vibrator_data *vib,
int intensity, int pwm)
{
struct android_vibrator_platform_data *pdata = vib->pdata;
int vib_duration_ms = 0;
pr_debug("%s: intensity : %d\n", __func__, intensity);
if (pdata->vibe_warmup_delay > 0) {
if (atomic_read(&vib->vib_status))
msleep(pdata->vibe_warmup_delay);
}
/* TODO: control the gain of vibrator */
if (intensity == 0) {
pdata->ic_enable_set(0);
pdata->pwm_set(0, 0, pwm);
/* should be checked for vibrator response time */
pdata->power_set(0);
atomic_set(&vib->vib_status, 0);
} else {
if (work_pending(&vib->work_vibrator_off))
cancel_work_sync(&vib->work_vibrator_off);
hrtimer_cancel(&vib->timer);
vib_duration_ms = atomic_read(&vib->ms_time);
/* should be checked for vibrator response time */
pdata->power_set(1);
pdata->pwm_set(1, intensity, pwm);
pdata->ic_enable_set(1);
atomic_set(&vib->vib_status, 1);
hrtimer_start(&vib->timer,
ns_to_ktime((u64)vib_duration_ms * NSEC_PER_MSEC),
HRTIMER_MODE_REL);
}
return 0;
}
static void android_vibrator_on(struct work_struct *work)
{
struct timed_vibrator_data *vib =
container_of(work, struct timed_vibrator_data,
work_vibrator_on);
int gain = atomic_read(&vib->gain);
int pwm = atomic_read(&vib->pwm);
/* suspend /resume logging test */
pr_debug("%s: gain = %d pwm = %d\n", __func__,
gain, pwm);
android_vibrator_force_set(vib, gain, pwm);
}
static void android_vibrator_off(struct work_struct *work)
{
struct timed_vibrator_data *vib =
container_of(work, struct timed_vibrator_data,
work_vibrator_off);
pr_debug("%s\n", __func__);
android_vibrator_force_set(vib, 0, vib->pdata->vibe_n_value);
}
static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
{
struct timed_vibrator_data *vib =
container_of(timer, struct timed_vibrator_data, timer);
vibrator_work_off(&vib->work_vibrator_off);
return HRTIMER_NORESTART;
}
static int vibrator_get_time(struct timed_output_dev *dev)
{
struct timed_vibrator_data *vib =
container_of(dev, struct timed_vibrator_data, dev);
if (hrtimer_active(&vib->timer)) {
ktime_t r = hrtimer_get_remaining(&vib->timer);
return ktime_to_ms(r);
}
return 0;
}
static void vibrator_enable(struct timed_output_dev *dev, int ms_time)
{
struct timed_vibrator_data *vib =
container_of(dev, struct timed_vibrator_data, dev);
unsigned long flags;
pr_debug("%s: ms_time %d \n", __func__, ms_time);
spin_lock_irqsave(&vib->lock, flags);
if (ms_time > 0) {
if (ms_time > vib->max_timeout)
ms_time = vib->max_timeout;
atomic_set(&vib->ms_time, ms_time);
vibrator_work_on(&vib->work_vibrator_on);
} else {
vibrator_work_off(&vib->work_vibrator_off);
}
spin_unlock_irqrestore(&vib->lock, flags);
}
static ssize_t vibrator_amp_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct timed_output_dev *dev_ =
(struct timed_output_dev *)dev_get_drvdata(dev);
struct timed_vibrator_data *vib =
container_of(dev_, struct timed_vibrator_data, dev);
int gain = atomic_read(&(vib->gain));
return sprintf(buf, "%d\n", gain);
}
static ssize_t vibrator_amp_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct timed_output_dev *dev_ =
(struct timed_output_dev *)dev_get_drvdata(dev);
struct timed_vibrator_data *vib =
container_of(dev_, struct timed_vibrator_data, dev);
int gain;
sscanf(buf, "%d", &gain);
if (gain > 100)
gain = 100;
else if (gain < -100)
gain = -100;
atomic_set(&vib->gain, gain);
return size;
}
static ssize_t vibrator_pwm_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct timed_output_dev *dev_ =
(struct timed_output_dev *)dev_get_drvdata(dev);
struct timed_vibrator_data *vib =
container_of(dev_, struct timed_vibrator_data, dev);
int gain = atomic_read(&(vib->pwm));
gain = 4800/gain;
return sprintf(buf, "%d\n", gain);
}
static ssize_t vibrator_pwm_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct timed_output_dev *dev_ =
(struct timed_output_dev *)dev_get_drvdata(dev);
struct timed_vibrator_data *vib =
container_of(dev_, struct timed_vibrator_data, dev);
int gain;
sscanf(buf, "%d", &gain);
gain = 4800/gain;
atomic_set(&vib->pwm, gain);
return size;
}
static struct device_attribute android_vibrator_device_attrs[] = {
__ATTR(amp, S_IRUGO | S_IWUSR, vibrator_amp_show, vibrator_amp_store),
__ATTR(pwm, S_IRUGO | S_IWUSR, vibrator_pwm_show, vibrator_pwm_store),
};
struct timed_vibrator_data android_vibrator_data = {
.dev.name = "vibrator",
.dev.enable = vibrator_enable,
.dev.get_time = vibrator_get_time,
.max_timeout = 30000, /* max time for vibrator enable 30 sec. */
};
static int android_vibrator_probe(struct platform_device *pdev)
{
int i, ret = 0;
struct timed_vibrator_data *vib = &android_vibrator_data;
vib->pdata = pdev->dev.platform_data;
if (!vib->pdata) {
pr_err("%s: no platform data\n", __func__);
return -ENODEV;
}
if (vib->pdata->vibrator_init) {
ret = vib->pdata->vibrator_init();
if (ret < 0) {
pr_err("%s: failed to vibrator init\n", __func__);
return ret;
}
}
platform_set_drvdata(pdev, &android_vibrator_data);
if (vib->pdata->amp > 100)
vib->pdata->amp = 100;
else if (vib->pdata->amp < -100)
vib->pdata->amp = -100;
atomic_set(&vib->gain, vib->pdata->amp); /* max value is 100 */
atomic_set(&vib->pwm, vib->pdata->vibe_n_value);
atomic_set(&vib->vib_status, 0);
pr_info("android_vibrator: default amplitude %d \n",
vib->pdata->amp);
INIT_WORK(&vib->work_vibrator_off, android_vibrator_off);
INIT_WORK(&vib->work_vibrator_on, android_vibrator_on);
hrtimer_init(&vib->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
vib->timer.function = vibrator_timer_func;
spin_lock_init(&vib->lock);
ret = timed_output_dev_register(&vib->dev);
if (ret < 0) {
pr_err("%s: failed to register timed output device\n",
__func__);
return ret;
}
for (i = 0; i < ARRAY_SIZE(android_vibrator_device_attrs); i++) {
ret = device_create_file(vib->dev.dev,
&android_vibrator_device_attrs[i]);
if (ret < 0) {
pr_err("%s: failed to create sysfs\n", __func__);
goto err_sysfs;
}
}
pr_info("android vibrator probed\n");
return 0;
err_sysfs:
for (; i >= 0; i--) {
device_remove_file(vib->dev.dev,
&android_vibrator_device_attrs[i]);
}
timed_output_dev_unregister(&vib->dev);
return ret;
}
static int android_vibrator_remove(struct platform_device *pdev)
{
struct timed_vibrator_data *vib =
(struct timed_vibrator_data *)platform_get_drvdata(pdev);
int i;
vibrator_work_off(&vib->work_vibrator_off);
for (i = ARRAY_SIZE(android_vibrator_device_attrs); i >= 0; i--) {
device_remove_file(vib->dev.dev,
&android_vibrator_device_attrs[i]);
}
timed_output_dev_unregister(&vib->dev);
return 0;
}
static struct platform_driver android_vibrator_driver = {
.probe = android_vibrator_probe,
.remove = android_vibrator_remove,
.driver = {
.name = "android-vibrator",
},
};
static int __init android_vibrator_init(void)
{
#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
vibrator_workqueue = create_workqueue("vibrator");
if (!vibrator_workqueue) {
pr_err("%s: out of memory\n", __func__);
return -ENOMEM;
}
#endif
return platform_driver_register(&android_vibrator_driver);
}
static void __exit android_vibrator_exit(void)
{
#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
if (vibrator_workqueue)
destroy_workqueue(vibrator_workqueue);
vibrator_workqueue = NULL;
#endif
platform_driver_unregister(&android_vibrator_driver);
}
late_initcall_sync(android_vibrator_init); /* to let init lately */
module_exit(android_vibrator_exit);
MODULE_AUTHOR("LG Electronics Inc.");
MODULE_DESCRIPTION("Android Vibrator Driver");
MODULE_LICENSE("GPL");
|