blob: fa57a0eeee37f144cae4fdbd36036e8e18664a69 [file] [log] [blame]
Devin Kim55468852012-06-19 12:01:32 -07001/*
2 * android vibrator driver
3 *
4 * Copyright (C) 2009-2012 LGE, Inc.
5 *
6 * Author: Jinkyu Choi <jinkyu@lge.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/platform_device.h>
28#include <linux/gpio.h>
29#include <linux/delay.h>
30#include <linux/timer.h>
31#include <linux/err.h>
32#include <linux/android_vibrator.h>
Devin Kima66ff762012-09-13 14:44:22 -070033#include <linux/mutex.h>
Devin Kim55468852012-06-19 12:01:32 -070034#include "../staging/android/timed_output.h"
35
36#define ANDROID_VIBRATOR_USE_WORKQUEUE
37
38#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
39static struct workqueue_struct *vibrator_workqueue;
40#endif
41
42struct timed_vibrator_data {
43 struct timed_output_dev dev;
44 struct hrtimer timer;
45 spinlock_t lock;
Devin Kim55468852012-06-19 12:01:32 -070046 int max_timeout;
Devin Kima66ff762012-09-13 14:44:22 -070047 atomic_t vib_status; /* 1:on,0:off */
Devin Kim795a9a62012-08-28 10:08:46 -070048 atomic_t gain; /* default max gain */
49 atomic_t pwm;
50 atomic_t ms_time; /* vibrator duration */
51 struct android_vibrator_platform_data *pdata;
Devin Kim55468852012-06-19 12:01:32 -070052 struct work_struct work_vibrator_off;
53 struct work_struct work_vibrator_on;
54};
55
Devin Kima66ff762012-09-13 14:44:22 -070056#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
57static inline void vibrator_work(struct work_struct *work)
58{
59 if (!work_pending(work))
60 queue_work(vibrator_workqueue, work);
61}
62
63#else
64static inline void vibrator_work(struct work_struct *work)
65{
66 if (!work_pending(work))
67 schedule_work(work);
68}
69#endif
70
71static DEFINE_MUTEX(vib_lock);
72
Devin Kim55468852012-06-19 12:01:32 -070073static int android_vibrator_force_set(struct timed_vibrator_data *vib,
Devin Kim795a9a62012-08-28 10:08:46 -070074 int intensity, int pwm)
Devin Kim55468852012-06-19 12:01:32 -070075{
76 /* Check the Force value with Max and Min force value */
Devin Kim795a9a62012-08-28 10:08:46 -070077 int vib_duration_ms = 0;
78 pr_debug("%s: intensity : %d\n", __func__, intensity);
Devin Kim55468852012-06-19 12:01:32 -070079
Devin Kim795a9a62012-08-28 10:08:46 -070080 if (intensity > 127)
81 intensity = 127;
82 if (intensity < -127)
83 intensity = -127;
Devin Kim55468852012-06-19 12:01:32 -070084
Devin Kima66ff762012-09-13 14:44:22 -070085 mutex_lock(&vib_lock);
86
Devin Kim55468852012-06-19 12:01:32 -070087 /* TODO: control the gain of vibrator */
Devin Kim795a9a62012-08-28 10:08:46 -070088 if (intensity == 0) {
89 vib->pdata->ic_enable_set(0);
90 vib->pdata->pwm_set(0, 0, pwm);
Devin Kim55468852012-06-19 12:01:32 -070091 /* should be checked for vibrator response time */
Devin Kim795a9a62012-08-28 10:08:46 -070092 vib->pdata->power_set(0);
Devin Kima66ff762012-09-13 14:44:22 -070093 atomic_set(&vib->vib_status, 0);
Devin Kim55468852012-06-19 12:01:32 -070094 } else {
Devin Kima66ff762012-09-13 14:44:22 -070095 if (work_pending(&vib->work_vibrator_off))
96 cancel_work_sync(&vib->work_vibrator_off);
Devin Kim55468852012-06-19 12:01:32 -070097 hrtimer_cancel(&vib->timer);
Devin Kima66ff762012-09-13 14:44:22 -070098
Devin Kim795a9a62012-08-28 10:08:46 -070099 vib_duration_ms = atomic_read(&vib->ms_time);
Devin Kim55468852012-06-19 12:01:32 -0700100 /* should be checked for vibrator response time */
Devin Kim795a9a62012-08-28 10:08:46 -0700101 vib->pdata->power_set(1);
102 vib->pdata->pwm_set(1, intensity, pwm);
103 vib->pdata->ic_enable_set(1);
Devin Kima66ff762012-09-13 14:44:22 -0700104 atomic_set(&vib->vib_status, 1);
Devin Kim55468852012-06-19 12:01:32 -0700105
Devin Kim55468852012-06-19 12:01:32 -0700106 hrtimer_start(&vib->timer,
Devin Kim795a9a62012-08-28 10:08:46 -0700107 ns_to_ktime((u64)vib_duration_ms * NSEC_PER_MSEC),
Devin Kim55468852012-06-19 12:01:32 -0700108 HRTIMER_MODE_REL);
109 }
Devin Kima66ff762012-09-13 14:44:22 -0700110
111 mutex_unlock(&vib_lock);
112
Devin Kim55468852012-06-19 12:01:32 -0700113 return 0;
114}
115
116static void android_vibrator_on(struct work_struct *work)
117{
118 struct timed_vibrator_data *vib =
119 container_of(work, struct timed_vibrator_data,
120 work_vibrator_on);
Devin Kim795a9a62012-08-28 10:08:46 -0700121 int gain = atomic_read(&vib->gain);
122 int pwm = atomic_read(&vib->pwm);
Devin Kim55468852012-06-19 12:01:32 -0700123 /* suspend /resume logging test */
Devin Kim795a9a62012-08-28 10:08:46 -0700124 pr_debug("%s: gain = %d pwm = %d\n", __func__,
125 gain, pwm);
Devin Kim55468852012-06-19 12:01:32 -0700126
Devin Kim795a9a62012-08-28 10:08:46 -0700127 android_vibrator_force_set(vib, gain, pwm);
Devin Kim55468852012-06-19 12:01:32 -0700128}
129
130static void android_vibrator_off(struct work_struct *work)
131{
132 struct timed_vibrator_data *vib =
133 container_of(work, struct timed_vibrator_data,
134 work_vibrator_off);
135
Devin Kim795a9a62012-08-28 10:08:46 -0700136 pr_debug("%s\n", __func__);
137 android_vibrator_force_set(vib, 0, vib->pdata->vibe_n_value);
Devin Kim55468852012-06-19 12:01:32 -0700138}
139
140static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
141{
142 struct timed_vibrator_data *vib =
143 container_of(timer, struct timed_vibrator_data, timer);
Devin Kima66ff762012-09-13 14:44:22 -0700144 vibrator_work(&vib->work_vibrator_off);
Devin Kim55468852012-06-19 12:01:32 -0700145 return HRTIMER_NORESTART;
146}
147
148static int vibrator_get_time(struct timed_output_dev *dev)
149{
150 struct timed_vibrator_data *vib =
151 container_of(dev, struct timed_vibrator_data, dev);
152
153 if (hrtimer_active(&vib->timer)) {
154 ktime_t r = hrtimer_get_remaining(&vib->timer);
155 return ktime_to_ms(r);
156 }
157
158 return 0;
159}
160
Devin Kim795a9a62012-08-28 10:08:46 -0700161static void vibrator_enable(struct timed_output_dev *dev, int ms_time)
Devin Kim55468852012-06-19 12:01:32 -0700162{
163 struct timed_vibrator_data *vib =
164 container_of(dev, struct timed_vibrator_data, dev);
165 unsigned long flags;
166
Devin Kim795a9a62012-08-28 10:08:46 -0700167 pr_debug("%s: ms_time %d \n", __func__, ms_time);
Devin Kim55468852012-06-19 12:01:32 -0700168 spin_lock_irqsave(&vib->lock, flags);
169
Devin Kim795a9a62012-08-28 10:08:46 -0700170 if (ms_time > 0) {
171 if (ms_time > vib->max_timeout)
172 ms_time = vib->max_timeout;
Devin Kim55468852012-06-19 12:01:32 -0700173
Devin Kim795a9a62012-08-28 10:08:46 -0700174 atomic_set(&vib->ms_time, ms_time);
Devin Kim55468852012-06-19 12:01:32 -0700175
Devin Kima66ff762012-09-13 14:44:22 -0700176 vibrator_work(&vib->work_vibrator_on);
Devin Kim55468852012-06-19 12:01:32 -0700177 } else {
Devin Kima66ff762012-09-13 14:44:22 -0700178 vibrator_work(&vib->work_vibrator_off);
Devin Kim55468852012-06-19 12:01:32 -0700179 }
180 spin_unlock_irqrestore(&vib->lock, flags);
Devin Kim55468852012-06-19 12:01:32 -0700181}
182
183static ssize_t vibrator_amp_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
186 struct timed_output_dev *dev_ =
187 (struct timed_output_dev *)dev_get_drvdata(dev);
188 struct timed_vibrator_data *vib =
189 container_of(dev_, struct timed_vibrator_data, dev);
Devin Kim795a9a62012-08-28 10:08:46 -0700190 int gain = atomic_read(&(vib->gain));
Devin Kim55468852012-06-19 12:01:32 -0700191
192 return sprintf(buf, "%d\n", gain);
193}
194
195static ssize_t vibrator_amp_store(struct device *dev,
196 struct device_attribute *attr, const char *buf, size_t size)
197{
198 struct timed_output_dev *dev_ =
199 (struct timed_output_dev *)dev_get_drvdata(dev);
200 struct timed_vibrator_data *vib =
201 container_of(dev_, struct timed_vibrator_data, dev);
202
203 int gain;
204 sscanf(buf, "%d", &gain);
Devin Kim795a9a62012-08-28 10:08:46 -0700205 atomic_set(&vib->gain, gain);
Devin Kim55468852012-06-19 12:01:32 -0700206
207 return size;
208}
209
210static ssize_t vibrator_pwm_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
212{
213 struct timed_output_dev *dev_ =
214 (struct timed_output_dev *)dev_get_drvdata(dev);
215 struct timed_vibrator_data *vib =
216 container_of(dev_, struct timed_vibrator_data, dev);
Devin Kim795a9a62012-08-28 10:08:46 -0700217 int gain = atomic_read(&(vib->pwm));
Devin Kim55468852012-06-19 12:01:32 -0700218
219 gain = 4800/gain;
220
221 return sprintf(buf, "%d\n", gain);
222}
223
Devin Kim795a9a62012-08-28 10:08:46 -0700224
Devin Kim55468852012-06-19 12:01:32 -0700225static ssize_t vibrator_pwm_store(struct device *dev,
226 struct device_attribute *attr, const char *buf, size_t size)
227{
228 struct timed_output_dev *dev_ =
229 (struct timed_output_dev *)dev_get_drvdata(dev);
230 struct timed_vibrator_data *vib =
231 container_of(dev_, struct timed_vibrator_data, dev);
232
233 int gain;
234 sscanf(buf, "%d", &gain);
235
236 gain = 4800/gain;
237
Devin Kim795a9a62012-08-28 10:08:46 -0700238 atomic_set(&vib->pwm, gain);
Devin Kim55468852012-06-19 12:01:32 -0700239
240 return size;
241}
242
Devin Kim795a9a62012-08-28 10:08:46 -0700243static struct device_attribute android_vibrator_device_attrs[] = {
Devin Kim55468852012-06-19 12:01:32 -0700244 __ATTR(amp, S_IRUGO | S_IWUSR, vibrator_amp_show, vibrator_amp_store),
245 __ATTR(pwm, S_IRUGO | S_IWUSR, vibrator_pwm_show, vibrator_pwm_store),
246};
247
248struct timed_vibrator_data android_vibrator_data = {
249 .dev.name = "vibrator",
250 .dev.enable = vibrator_enable,
251 .dev.get_time = vibrator_get_time,
252 .max_timeout = 30000, /* max time for vibrator enable 30 sec. */
Devin Kim55468852012-06-19 12:01:32 -0700253};
254
255static int android_vibrator_probe(struct platform_device *pdev)
256{
257
258 int i, ret = 0;
Devin Kim795a9a62012-08-28 10:08:46 -0700259 struct timed_vibrator_data *vib = &android_vibrator_data;
260 vib->pdata = pdev->dev.platform_data;
Devin Kim55468852012-06-19 12:01:32 -0700261
Devin Kim795a9a62012-08-28 10:08:46 -0700262 if (!vib->pdata) {
263 pr_err("%s: no platform data\n", __func__);
Devin Kim55468852012-06-19 12:01:32 -0700264 return -ENODEV;
265 }
266
Devin Kim795a9a62012-08-28 10:08:46 -0700267 if (vib->pdata->vibrator_init) {
268 ret = vib->pdata->vibrator_init();
269 if (ret < 0) {
270 pr_err("%s: failed to vibrator init\n", __func__);
271 return ret;
272 }
273 }
274
275 platform_set_drvdata(pdev, &android_vibrator_data);
276
277 atomic_set(&vib->gain, vib->pdata->amp); /* max value is 128 */
278 atomic_set(&vib->pwm, vib->pdata->vibe_n_value);
Devin Kima66ff762012-09-13 14:44:22 -0700279 atomic_set(&vib->vib_status, 0);
Devin Kim795a9a62012-08-28 10:08:46 -0700280 pr_info("android_vibrator: default amplitude %d \n",
281 vib->pdata->amp);
Devin Kim55468852012-06-19 12:01:32 -0700282
283 INIT_WORK(&vib->work_vibrator_off, android_vibrator_off);
284 INIT_WORK(&vib->work_vibrator_on, android_vibrator_on);
285 hrtimer_init(&vib->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
286 vib->timer.function = vibrator_timer_func;
287 spin_lock_init(&vib->lock);
288
289 ret = timed_output_dev_register(&vib->dev);
290 if (ret < 0) {
Devin Kim795a9a62012-08-28 10:08:46 -0700291 pr_err("%s: failed to register timed output device\n",
292 __func__);
293 return ret;
Devin Kim55468852012-06-19 12:01:32 -0700294 }
Devin Kim795a9a62012-08-28 10:08:46 -0700295
296 for (i = 0; i < ARRAY_SIZE(android_vibrator_device_attrs); i++) {
297 ret = device_create_file(vib->dev.dev,
298 &android_vibrator_device_attrs[i]);
Devin Kim55468852012-06-19 12:01:32 -0700299 if (ret < 0) {
Devin Kim795a9a62012-08-28 10:08:46 -0700300 pr_err("%s: failed to create sysfs\n", __func__);
301 goto err_sysfs;
Devin Kim55468852012-06-19 12:01:32 -0700302 }
303 }
304
Devin Kim795a9a62012-08-28 10:08:46 -0700305 pr_info("android vibrator probed\n");
Devin Kim55468852012-06-19 12:01:32 -0700306
307 return 0;
Devin Kim795a9a62012-08-28 10:08:46 -0700308
309err_sysfs:
310 for (; i >= 0; i--) {
311 device_remove_file(vib->dev.dev,
312 &android_vibrator_device_attrs[i]);
313 }
314
315 timed_output_dev_unregister(&vib->dev);
316 return ret;
Devin Kim55468852012-06-19 12:01:32 -0700317}
318
319static int android_vibrator_remove(struct platform_device *pdev)
320{
321 struct timed_vibrator_data *vib =
322 (struct timed_vibrator_data *)platform_get_drvdata(pdev);
Devin Kim795a9a62012-08-28 10:08:46 -0700323 int i;
Devin Kim55468852012-06-19 12:01:32 -0700324
Devin Kima66ff762012-09-13 14:44:22 -0700325 vibrator_work(&vib->work_vibrator_off);
Devin Kim795a9a62012-08-28 10:08:46 -0700326 for (i = ARRAY_SIZE(android_vibrator_device_attrs); i >= 0; i--) {
327 device_remove_file(vib->dev.dev,
328 &android_vibrator_device_attrs[i]);
329 }
Devin Kim55468852012-06-19 12:01:32 -0700330
331 timed_output_dev_unregister(&vib->dev);
332
333 return 0;
334}
335
Devin Kim55468852012-06-19 12:01:32 -0700336static struct platform_driver android_vibrator_driver = {
337 .probe = android_vibrator_probe,
338 .remove = android_vibrator_remove,
Devin Kim55468852012-06-19 12:01:32 -0700339 .driver = {
340 .name = "android-vibrator",
341 },
342};
343
344static int __init android_vibrator_init(void)
345{
Devin Kim55468852012-06-19 12:01:32 -0700346#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
347 vibrator_workqueue = create_workqueue("vibrator");
Devin Kim795a9a62012-08-28 10:08:46 -0700348 if (!vibrator_workqueue) {
349 pr_err("%s: out of memory\n", __func__);
Devin Kim55468852012-06-19 12:01:32 -0700350 return -ENOMEM;
Devin Kim795a9a62012-08-28 10:08:46 -0700351 }
Devin Kim55468852012-06-19 12:01:32 -0700352#endif
353 return platform_driver_register(&android_vibrator_driver);
Devin Kim55468852012-06-19 12:01:32 -0700354}
355
356static void __exit android_vibrator_exit(void)
357{
Devin Kim55468852012-06-19 12:01:32 -0700358#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
359 if (vibrator_workqueue)
360 destroy_workqueue(vibrator_workqueue);
Devin Kim55468852012-06-19 12:01:32 -0700361 vibrator_workqueue = NULL;
362#endif
363 platform_driver_unregister(&android_vibrator_driver);
Devin Kim55468852012-06-19 12:01:32 -0700364}
365
366late_initcall_sync(android_vibrator_init); /* to let init lately */
367module_exit(android_vibrator_exit);
368
369MODULE_AUTHOR("LG Electronics Inc.");
370MODULE_DESCRIPTION("Android Vibrator Driver");
371MODULE_LICENSE("GPL");