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
|
/* arch/arm/mach-omap2/board-tuna-vibrator.c
*
* Copyright (C) 2011 Samsung Electronics Co. Ltd. All Rights Reserved.
* Author: Rom Lemarchand <rlemarchand@sta.samsung.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/hrtimer.h>
#include <linux/gpio.h>
#include <linux/wakelock.h>
#include <linux/mutex.h>
#include <asm/mach-types.h>
#include <plat/dmtimer.h>
#include <../../../drivers/staging/android/timed_output.h>
#include "mux.h"
#include "board-tuna.h"
/* Vibrator enable pin is changed on Rev 05 to block not intended vibration. */
#define GPIO_MOTOR_EN 162
#define GPIO_MOTOR_EN_REV05 54
#define VIB_GPTIMER_NUM 10
#define PWM_DUTY_MAX 1463
#define MAX_TIMEOUT 10000 /* 10s */
static unsigned long pwmval = 127;
static unsigned long oldpwmval;
static struct vibrator {
struct wake_lock wklock;
struct hrtimer timer;
struct mutex lock;
struct omap_dm_timer *gptimer;
bool enabled;
unsigned gpio_en;
} vibdata;
static ssize_t pwmvalue_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int count;
count = sprintf(buf, "%lu\n", pwmval);
pr_info("vibrator: pwmval: %lu\n", pwmval);
return count;
}
ssize_t pwmvalue_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
if (kstrtoul(buf, 0, &pwmval))
pr_err("vibrator: error in storing pwm value\n");
pr_info("vibrator: pwmval: %lu\n", pwmval);
return size;
}
static DEVICE_ATTR(pwmvalue, S_IRUGO | S_IWUGO,
pwmvalue_show, pwmvalue_store);
static int tuna_create_vibrator_sysfs(void)
{
int ret;
struct kobject *vibrator_kobj;
vibrator_kobj = kobject_create_and_add("vibrator", NULL);
if (unlikely(!vibrator_kobj))
return -ENOMEM;
ret = sysfs_create_file(vibrator_kobj,
&dev_attr_pwmvalue.attr);
if (unlikely(ret < 0)) {
pr_err("vibrator: sysfs_create_file failed: %d\n", ret);
return ret;
}
return 0;
}
static void vibrator_off(void)
{
if (!vibdata.enabled)
return;
omap_dm_timer_stop(vibdata.gptimer);
gpio_set_value(vibdata.gpio_en, 0);
vibdata.enabled = false;
wake_unlock(&vibdata.wklock);
}
static int vibrator_get_time(struct timed_output_dev *dev)
{
if (hrtimer_active(&vibdata.timer)) {
ktime_t r = hrtimer_get_remaining(&vibdata.timer);
return ktime_to_ms(r);
}
return 0;
}
static int vibrator_timer_init(void)
{
int ret;
int pwm_duty;
/*
* Formula for matching the user space force (-127 to +127)
* to Duty cycle.
* Duty cycle will vary from 0 to 45('0' means 0% duty cycle,
* '45' means 100% duty cycle.
* Also if user space force equals to -127 then duty
* cycle will be 0 (0%), if force equals to 0 duty cycle
* will be 22.5(50%), if +127 then duty cycle will
* be 45(100%)
*/
pwm_duty = ((pwmval + 128) * (PWM_DUTY_MAX >> 1)/128);
ret = omap_dm_timer_set_source(vibdata.gptimer,
OMAP_TIMER_SRC_SYS_CLK);
if (ret < 0)
return ret;
omap_dm_timer_set_load(vibdata.gptimer, 1, -PWM_DUTY_MAX);
omap_dm_timer_set_match(vibdata.gptimer, 1, -pwm_duty);
omap_dm_timer_set_pwm(vibdata.gptimer, 0, 1,
OMAP_TIMER_TRIGGER_OVERFLOW_AND_COMPARE);
omap_dm_timer_enable(vibdata.gptimer);
omap_dm_timer_write_counter(vibdata.gptimer, -2);
omap_dm_timer_disable(vibdata.gptimer);
return 0;
}
static void vibrator_enable(struct timed_output_dev *dev, int value)
{
mutex_lock(&vibdata.lock);
/* make sure pwmval is between 0 and 127 */
if( pwmval > 127 ) {
pwmval = 127;
} else if ( pwmval < 0 ) {
pwmval = 0;
}
/* set the current pwmval */
if (pwmval != oldpwmval) {
vibrator_timer_init();
oldpwmval = pwmval;
}
/* cancel previous timer and set GPIO according to value */
hrtimer_cancel(&vibdata.timer);
if (value) {
pr_info("vibrator: value=%d, pwmval=%lu\n", value, pwmval);
wake_lock(&vibdata.wklock);
vibrator_timer_init();
gpio_set_value(vibdata.gpio_en, 1);
omap_dm_timer_start(vibdata.gptimer);
vibdata.enabled = true;
if (value > 0) {
if (value > MAX_TIMEOUT)
value = MAX_TIMEOUT;
hrtimer_start(&vibdata.timer,
ns_to_ktime((u64)value * NSEC_PER_MSEC),
HRTIMER_MODE_REL);
}
} else {
vibrator_off();
}
mutex_unlock(&vibdata.lock);
}
static struct timed_output_dev to_dev = {
.name = "vibrator",
.get_time = vibrator_get_time,
.enable = vibrator_enable,
};
static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
{
vibrator_off();
return HRTIMER_NORESTART;
}
static int __init vibrator_init(void)
{
int ret;
vibdata.enabled = false;
hrtimer_init(&vibdata.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
vibdata.timer.function = vibrator_timer_func;
vibdata.gptimer = omap_dm_timer_request_specific(VIB_GPTIMER_NUM);
if (vibdata.gptimer == NULL)
return -1;
omap_dm_timer_dump_regs(vibdata.gptimer);
ret = vibrator_timer_init();
if (ret < 0)
goto err_dm_timer_init;
omap_dm_timer_dump_regs(vibdata.gptimer);
wake_lock_init(&vibdata.wklock, WAKE_LOCK_SUSPEND, "vibrator");
mutex_init(&vibdata.lock);
tuna_create_vibrator_sysfs();
ret = timed_output_dev_register(&to_dev);
if (ret < 0)
goto err_to_dev_reg;
return 0;
err_to_dev_reg:
mutex_destroy(&vibdata.lock);
wake_lock_destroy(&vibdata.wklock);
err_dm_timer_init:
omap_dm_timer_free(vibdata.gptimer);
vibdata.gptimer = NULL;
return -1;
}
static int __init omap4_tuna_vibrator_init(void)
{
int ret;
if (!machine_is_tuna())
return 0;
vibdata.gpio_en = (omap4_tuna_get_revision() >= 5) ?
GPIO_MOTOR_EN_REV05 : GPIO_MOTOR_EN;
omap_mux_init_gpio(vibdata.gpio_en, OMAP_PIN_OUTPUT |
OMAP_PIN_OFF_OUTPUT_LOW);
omap_mux_init_signal("dpm_emu18.dmtimer10_pwm_evt", OMAP_PIN_OUTPUT);
ret = gpio_request(vibdata.gpio_en, "vibrator-en");
if (ret)
return ret;
gpio_direction_output(vibdata.gpio_en, 0);
ret = vibrator_init();
if (ret < 0)
gpio_free(vibdata.gpio_en);
return ret;
}
/*
* This is needed because the vibrator is dependent on omap_dm_timers which get
* initialized at device_init time
*/
late_initcall(omap4_tuna_vibrator_init);
|