blob: ce3f7c39c342c5115963bb95903b612996357161 [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
Devin Kimb017c812012-09-17 09:50:23 -070057static inline void vibrator_work_on(struct work_struct *work)
58{
59 queue_work(vibrator_workqueue, work);
60}
61
62static inline void vibrator_work_off(struct work_struct *work)
Devin Kima66ff762012-09-13 14:44:22 -070063{
64 if (!work_pending(work))
65 queue_work(vibrator_workqueue, work);
66}
Devin Kima66ff762012-09-13 14:44:22 -070067#else
Devin Kimb017c812012-09-17 09:50:23 -070068static inline void vibrator_work_on(struct work_struct *work)
69{
70 schedule_work(work);
71}
72
73static inline void vibrator_work_off(struct work_struct *work)
Devin Kima66ff762012-09-13 14:44:22 -070074{
75 if (!work_pending(work))
76 schedule_work(work);
77}
78#endif
79
Devin Kim55468852012-06-19 12:01:32 -070080static int android_vibrator_force_set(struct timed_vibrator_data *vib,
Devin Kim795a9a62012-08-28 10:08:46 -070081 int intensity, int pwm)
Devin Kim55468852012-06-19 12:01:32 -070082{
Devin Kim7eb84ca2012-09-14 16:16:13 -070083 struct android_vibrator_platform_data *pdata = vib->pdata;
Devin Kim795a9a62012-08-28 10:08:46 -070084 int vib_duration_ms = 0;
Devin Kim7eb84ca2012-09-14 16:16:13 -070085
Devin Kim795a9a62012-08-28 10:08:46 -070086 pr_debug("%s: intensity : %d\n", __func__, intensity);
Devin Kim55468852012-06-19 12:01:32 -070087
Devin Kim7eb84ca2012-09-14 16:16:13 -070088 /* Check the Force value with Max and Min force value */
Devin Kim795a9a62012-08-28 10:08:46 -070089 if (intensity > 127)
90 intensity = 127;
91 if (intensity < -127)
92 intensity = -127;
Devin Kim55468852012-06-19 12:01:32 -070093
Devin Kim7eb84ca2012-09-14 16:16:13 -070094 if (pdata->vibe_warmup_delay > 0) {
95 if (atomic_read(&vib->vib_status))
96 msleep(pdata->vibe_warmup_delay);
97 }
98
Devin Kim55468852012-06-19 12:01:32 -070099 /* TODO: control the gain of vibrator */
Devin Kim795a9a62012-08-28 10:08:46 -0700100 if (intensity == 0) {
Devin Kim7eb84ca2012-09-14 16:16:13 -0700101 pdata->ic_enable_set(0);
102 pdata->pwm_set(0, 0, pwm);
Devin Kim55468852012-06-19 12:01:32 -0700103 /* should be checked for vibrator response time */
Devin Kim7eb84ca2012-09-14 16:16:13 -0700104 pdata->power_set(0);
Devin Kima66ff762012-09-13 14:44:22 -0700105 atomic_set(&vib->vib_status, 0);
Devin Kim55468852012-06-19 12:01:32 -0700106 } else {
Devin Kima66ff762012-09-13 14:44:22 -0700107 if (work_pending(&vib->work_vibrator_off))
108 cancel_work_sync(&vib->work_vibrator_off);
Devin Kim55468852012-06-19 12:01:32 -0700109 hrtimer_cancel(&vib->timer);
Devin Kima66ff762012-09-13 14:44:22 -0700110
Devin Kim795a9a62012-08-28 10:08:46 -0700111 vib_duration_ms = atomic_read(&vib->ms_time);
Devin Kim55468852012-06-19 12:01:32 -0700112 /* should be checked for vibrator response time */
Devin Kim7eb84ca2012-09-14 16:16:13 -0700113 pdata->power_set(1);
114 pdata->pwm_set(1, intensity, pwm);
115 pdata->ic_enable_set(1);
Devin Kima66ff762012-09-13 14:44:22 -0700116 atomic_set(&vib->vib_status, 1);
Devin Kim55468852012-06-19 12:01:32 -0700117
Devin Kim55468852012-06-19 12:01:32 -0700118 hrtimer_start(&vib->timer,
Devin Kim795a9a62012-08-28 10:08:46 -0700119 ns_to_ktime((u64)vib_duration_ms * NSEC_PER_MSEC),
Devin Kim55468852012-06-19 12:01:32 -0700120 HRTIMER_MODE_REL);
121 }
Devin Kima66ff762012-09-13 14:44:22 -0700122
Devin Kim55468852012-06-19 12:01:32 -0700123 return 0;
124}
125
126static void android_vibrator_on(struct work_struct *work)
127{
128 struct timed_vibrator_data *vib =
129 container_of(work, struct timed_vibrator_data,
130 work_vibrator_on);
Devin Kim795a9a62012-08-28 10:08:46 -0700131 int gain = atomic_read(&vib->gain);
132 int pwm = atomic_read(&vib->pwm);
Devin Kim55468852012-06-19 12:01:32 -0700133 /* suspend /resume logging test */
Devin Kim795a9a62012-08-28 10:08:46 -0700134 pr_debug("%s: gain = %d pwm = %d\n", __func__,
135 gain, pwm);
Devin Kim55468852012-06-19 12:01:32 -0700136
Devin Kim795a9a62012-08-28 10:08:46 -0700137 android_vibrator_force_set(vib, gain, pwm);
Devin Kim55468852012-06-19 12:01:32 -0700138}
139
140static void android_vibrator_off(struct work_struct *work)
141{
142 struct timed_vibrator_data *vib =
143 container_of(work, struct timed_vibrator_data,
144 work_vibrator_off);
145
Devin Kim795a9a62012-08-28 10:08:46 -0700146 pr_debug("%s\n", __func__);
147 android_vibrator_force_set(vib, 0, vib->pdata->vibe_n_value);
Devin Kim55468852012-06-19 12:01:32 -0700148}
149
150static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
151{
152 struct timed_vibrator_data *vib =
153 container_of(timer, struct timed_vibrator_data, timer);
Devin Kimb017c812012-09-17 09:50:23 -0700154 vibrator_work_off(&vib->work_vibrator_off);
Devin Kim55468852012-06-19 12:01:32 -0700155 return HRTIMER_NORESTART;
156}
157
158static int vibrator_get_time(struct timed_output_dev *dev)
159{
160 struct timed_vibrator_data *vib =
161 container_of(dev, struct timed_vibrator_data, dev);
162
163 if (hrtimer_active(&vib->timer)) {
164 ktime_t r = hrtimer_get_remaining(&vib->timer);
165 return ktime_to_ms(r);
166 }
167
168 return 0;
169}
170
Devin Kim795a9a62012-08-28 10:08:46 -0700171static void vibrator_enable(struct timed_output_dev *dev, int ms_time)
Devin Kim55468852012-06-19 12:01:32 -0700172{
173 struct timed_vibrator_data *vib =
174 container_of(dev, struct timed_vibrator_data, dev);
175 unsigned long flags;
176
Devin Kim795a9a62012-08-28 10:08:46 -0700177 pr_debug("%s: ms_time %d \n", __func__, ms_time);
Devin Kim55468852012-06-19 12:01:32 -0700178 spin_lock_irqsave(&vib->lock, flags);
179
Devin Kim795a9a62012-08-28 10:08:46 -0700180 if (ms_time > 0) {
181 if (ms_time > vib->max_timeout)
182 ms_time = vib->max_timeout;
Devin Kim55468852012-06-19 12:01:32 -0700183
Devin Kim795a9a62012-08-28 10:08:46 -0700184 atomic_set(&vib->ms_time, ms_time);
Devin Kim55468852012-06-19 12:01:32 -0700185
Devin Kimb017c812012-09-17 09:50:23 -0700186 vibrator_work_on(&vib->work_vibrator_on);
Devin Kim55468852012-06-19 12:01:32 -0700187 } else {
Devin Kimb017c812012-09-17 09:50:23 -0700188 vibrator_work_off(&vib->work_vibrator_off);
Devin Kim55468852012-06-19 12:01:32 -0700189 }
190 spin_unlock_irqrestore(&vib->lock, flags);
Devin Kim55468852012-06-19 12:01:32 -0700191}
192
193static ssize_t vibrator_amp_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195{
196 struct timed_output_dev *dev_ =
197 (struct timed_output_dev *)dev_get_drvdata(dev);
198 struct timed_vibrator_data *vib =
199 container_of(dev_, struct timed_vibrator_data, dev);
Devin Kim795a9a62012-08-28 10:08:46 -0700200 int gain = atomic_read(&(vib->gain));
Devin Kim55468852012-06-19 12:01:32 -0700201
202 return sprintf(buf, "%d\n", gain);
203}
204
205static ssize_t vibrator_amp_store(struct device *dev,
206 struct device_attribute *attr, const char *buf, size_t size)
207{
208 struct timed_output_dev *dev_ =
209 (struct timed_output_dev *)dev_get_drvdata(dev);
210 struct timed_vibrator_data *vib =
211 container_of(dev_, struct timed_vibrator_data, dev);
212
213 int gain;
214 sscanf(buf, "%d", &gain);
Devin Kim795a9a62012-08-28 10:08:46 -0700215 atomic_set(&vib->gain, gain);
Devin Kim55468852012-06-19 12:01:32 -0700216
217 return size;
218}
219
220static ssize_t vibrator_pwm_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
222{
223 struct timed_output_dev *dev_ =
224 (struct timed_output_dev *)dev_get_drvdata(dev);
225 struct timed_vibrator_data *vib =
226 container_of(dev_, struct timed_vibrator_data, dev);
Devin Kim795a9a62012-08-28 10:08:46 -0700227 int gain = atomic_read(&(vib->pwm));
Devin Kim55468852012-06-19 12:01:32 -0700228
229 gain = 4800/gain;
230
231 return sprintf(buf, "%d\n", gain);
232}
233
Devin Kim795a9a62012-08-28 10:08:46 -0700234
Devin Kim55468852012-06-19 12:01:32 -0700235static ssize_t vibrator_pwm_store(struct device *dev,
236 struct device_attribute *attr, const char *buf, size_t size)
237{
238 struct timed_output_dev *dev_ =
239 (struct timed_output_dev *)dev_get_drvdata(dev);
240 struct timed_vibrator_data *vib =
241 container_of(dev_, struct timed_vibrator_data, dev);
242
243 int gain;
244 sscanf(buf, "%d", &gain);
245
246 gain = 4800/gain;
247
Devin Kim795a9a62012-08-28 10:08:46 -0700248 atomic_set(&vib->pwm, gain);
Devin Kim55468852012-06-19 12:01:32 -0700249
250 return size;
251}
252
Devin Kim795a9a62012-08-28 10:08:46 -0700253static struct device_attribute android_vibrator_device_attrs[] = {
Devin Kim55468852012-06-19 12:01:32 -0700254 __ATTR(amp, S_IRUGO | S_IWUSR, vibrator_amp_show, vibrator_amp_store),
255 __ATTR(pwm, S_IRUGO | S_IWUSR, vibrator_pwm_show, vibrator_pwm_store),
256};
257
258struct timed_vibrator_data android_vibrator_data = {
259 .dev.name = "vibrator",
260 .dev.enable = vibrator_enable,
261 .dev.get_time = vibrator_get_time,
262 .max_timeout = 30000, /* max time for vibrator enable 30 sec. */
Devin Kim55468852012-06-19 12:01:32 -0700263};
264
265static int android_vibrator_probe(struct platform_device *pdev)
266{
267
268 int i, ret = 0;
Devin Kim795a9a62012-08-28 10:08:46 -0700269 struct timed_vibrator_data *vib = &android_vibrator_data;
270 vib->pdata = pdev->dev.platform_data;
Devin Kim55468852012-06-19 12:01:32 -0700271
Devin Kim795a9a62012-08-28 10:08:46 -0700272 if (!vib->pdata) {
273 pr_err("%s: no platform data\n", __func__);
Devin Kim55468852012-06-19 12:01:32 -0700274 return -ENODEV;
275 }
276
Devin Kim795a9a62012-08-28 10:08:46 -0700277 if (vib->pdata->vibrator_init) {
278 ret = vib->pdata->vibrator_init();
279 if (ret < 0) {
280 pr_err("%s: failed to vibrator init\n", __func__);
281 return ret;
282 }
283 }
284
285 platform_set_drvdata(pdev, &android_vibrator_data);
286
287 atomic_set(&vib->gain, vib->pdata->amp); /* max value is 128 */
288 atomic_set(&vib->pwm, vib->pdata->vibe_n_value);
Devin Kima66ff762012-09-13 14:44:22 -0700289 atomic_set(&vib->vib_status, 0);
Devin Kim795a9a62012-08-28 10:08:46 -0700290 pr_info("android_vibrator: default amplitude %d \n",
291 vib->pdata->amp);
Devin Kim55468852012-06-19 12:01:32 -0700292
293 INIT_WORK(&vib->work_vibrator_off, android_vibrator_off);
294 INIT_WORK(&vib->work_vibrator_on, android_vibrator_on);
295 hrtimer_init(&vib->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
296 vib->timer.function = vibrator_timer_func;
297 spin_lock_init(&vib->lock);
298
299 ret = timed_output_dev_register(&vib->dev);
300 if (ret < 0) {
Devin Kim795a9a62012-08-28 10:08:46 -0700301 pr_err("%s: failed to register timed output device\n",
302 __func__);
303 return ret;
Devin Kim55468852012-06-19 12:01:32 -0700304 }
Devin Kim795a9a62012-08-28 10:08:46 -0700305
306 for (i = 0; i < ARRAY_SIZE(android_vibrator_device_attrs); i++) {
307 ret = device_create_file(vib->dev.dev,
308 &android_vibrator_device_attrs[i]);
Devin Kim55468852012-06-19 12:01:32 -0700309 if (ret < 0) {
Devin Kim795a9a62012-08-28 10:08:46 -0700310 pr_err("%s: failed to create sysfs\n", __func__);
311 goto err_sysfs;
Devin Kim55468852012-06-19 12:01:32 -0700312 }
313 }
314
Devin Kim795a9a62012-08-28 10:08:46 -0700315 pr_info("android vibrator probed\n");
Devin Kim55468852012-06-19 12:01:32 -0700316
317 return 0;
Devin Kim795a9a62012-08-28 10:08:46 -0700318
319err_sysfs:
320 for (; i >= 0; i--) {
321 device_remove_file(vib->dev.dev,
322 &android_vibrator_device_attrs[i]);
323 }
324
325 timed_output_dev_unregister(&vib->dev);
326 return ret;
Devin Kim55468852012-06-19 12:01:32 -0700327}
328
329static int android_vibrator_remove(struct platform_device *pdev)
330{
331 struct timed_vibrator_data *vib =
332 (struct timed_vibrator_data *)platform_get_drvdata(pdev);
Devin Kim795a9a62012-08-28 10:08:46 -0700333 int i;
Devin Kim55468852012-06-19 12:01:32 -0700334
Devin Kimb017c812012-09-17 09:50:23 -0700335 vibrator_work_off(&vib->work_vibrator_off);
Devin Kim795a9a62012-08-28 10:08:46 -0700336 for (i = ARRAY_SIZE(android_vibrator_device_attrs); i >= 0; i--) {
337 device_remove_file(vib->dev.dev,
338 &android_vibrator_device_attrs[i]);
339 }
Devin Kim55468852012-06-19 12:01:32 -0700340
341 timed_output_dev_unregister(&vib->dev);
342
343 return 0;
344}
345
Devin Kim55468852012-06-19 12:01:32 -0700346static struct platform_driver android_vibrator_driver = {
347 .probe = android_vibrator_probe,
348 .remove = android_vibrator_remove,
Devin Kim55468852012-06-19 12:01:32 -0700349 .driver = {
350 .name = "android-vibrator",
351 },
352};
353
354static int __init android_vibrator_init(void)
355{
Devin Kim55468852012-06-19 12:01:32 -0700356#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
357 vibrator_workqueue = create_workqueue("vibrator");
Devin Kim795a9a62012-08-28 10:08:46 -0700358 if (!vibrator_workqueue) {
359 pr_err("%s: out of memory\n", __func__);
Devin Kim55468852012-06-19 12:01:32 -0700360 return -ENOMEM;
Devin Kim795a9a62012-08-28 10:08:46 -0700361 }
Devin Kim55468852012-06-19 12:01:32 -0700362#endif
363 return platform_driver_register(&android_vibrator_driver);
Devin Kim55468852012-06-19 12:01:32 -0700364}
365
366static void __exit android_vibrator_exit(void)
367{
Devin Kim55468852012-06-19 12:01:32 -0700368#ifdef ANDROID_VIBRATOR_USE_WORKQUEUE
369 if (vibrator_workqueue)
370 destroy_workqueue(vibrator_workqueue);
Devin Kim55468852012-06-19 12:01:32 -0700371 vibrator_workqueue = NULL;
372#endif
373 platform_driver_unregister(&android_vibrator_driver);
Devin Kim55468852012-06-19 12:01:32 -0700374}
375
376late_initcall_sync(android_vibrator_init); /* to let init lately */
377module_exit(android_vibrator_exit);
378
379MODULE_AUTHOR("LG Electronics Inc.");
380MODULE_DESCRIPTION("Android Vibrator Driver");
381MODULE_LICENSE("GPL");