Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 33 | #include "../staging/android/timed_output.h" |
| 34 | |
| 35 | #define ANDROID_VIBRATOR_USE_WORKQUEUE |
| 36 | |
| 37 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 38 | static struct workqueue_struct *vibrator_workqueue; |
| 39 | #endif |
| 40 | |
| 41 | struct timed_vibrator_data { |
| 42 | struct timed_output_dev dev; |
| 43 | struct hrtimer timer; |
| 44 | spinlock_t lock; |
| 45 | atomic_t vib_status; /* on/off */ |
| 46 | |
| 47 | int max_timeout; |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 48 | 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 Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 52 | struct work_struct work_vibrator_off; |
| 53 | struct work_struct work_vibrator_on; |
| 54 | }; |
| 55 | |
| 56 | static int android_vibrator_force_set(struct timed_vibrator_data *vib, |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 57 | int intensity, int pwm) |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 58 | { |
| 59 | /* Check the Force value with Max and Min force value */ |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 60 | int vib_duration_ms = 0; |
| 61 | pr_debug("%s: intensity : %d\n", __func__, intensity); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 62 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 63 | if (intensity > 127) |
| 64 | intensity = 127; |
| 65 | if (intensity < -127) |
| 66 | intensity = -127; |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 67 | |
| 68 | /* TODO: control the gain of vibrator */ |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 69 | if (intensity == 0) { |
| 70 | vib->pdata->ic_enable_set(0); |
| 71 | vib->pdata->pwm_set(0, 0, pwm); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 72 | /* should be checked for vibrator response time */ |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 73 | vib->pdata->power_set(0); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 74 | |
| 75 | atomic_set(&vib->vib_status, false); |
| 76 | } else { |
| 77 | cancel_work_sync(&vib->work_vibrator_off); |
| 78 | hrtimer_cancel(&vib->timer); |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 79 | vib_duration_ms = atomic_read(&vib->ms_time); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 80 | /* should be checked for vibrator response time */ |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 81 | vib->pdata->power_set(1); |
| 82 | vib->pdata->pwm_set(1, intensity, pwm); |
| 83 | vib->pdata->ic_enable_set(1); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 84 | |
| 85 | atomic_set(&vib->vib_status, true); |
| 86 | hrtimer_start(&vib->timer, |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 87 | ns_to_ktime((u64)vib_duration_ms * NSEC_PER_MSEC), |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 88 | HRTIMER_MODE_REL); |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static void android_vibrator_on(struct work_struct *work) |
| 94 | { |
| 95 | struct timed_vibrator_data *vib = |
| 96 | container_of(work, struct timed_vibrator_data, |
| 97 | work_vibrator_on); |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 98 | int gain = atomic_read(&vib->gain); |
| 99 | int pwm = atomic_read(&vib->pwm); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 100 | /* suspend /resume logging test */ |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 101 | pr_debug("%s: gain = %d pwm = %d\n", __func__, |
| 102 | gain, pwm); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 103 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 104 | android_vibrator_force_set(vib, gain, pwm); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void android_vibrator_off(struct work_struct *work) |
| 108 | { |
| 109 | struct timed_vibrator_data *vib = |
| 110 | container_of(work, struct timed_vibrator_data, |
| 111 | work_vibrator_off); |
| 112 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 113 | pr_debug("%s\n", __func__); |
| 114 | android_vibrator_force_set(vib, 0, vib->pdata->vibe_n_value); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer) |
| 118 | { |
| 119 | struct timed_vibrator_data *vib = |
| 120 | container_of(timer, struct timed_vibrator_data, timer); |
| 121 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 122 | queue_work(vibrator_workqueue,&vib->work_vibrator_off); |
| 123 | #endif |
| 124 | return HRTIMER_NORESTART; |
| 125 | } |
| 126 | |
| 127 | static int vibrator_get_time(struct timed_output_dev *dev) |
| 128 | { |
| 129 | struct timed_vibrator_data *vib = |
| 130 | container_of(dev, struct timed_vibrator_data, dev); |
| 131 | |
| 132 | if (hrtimer_active(&vib->timer)) { |
| 133 | ktime_t r = hrtimer_get_remaining(&vib->timer); |
| 134 | return ktime_to_ms(r); |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 140 | static void vibrator_enable(struct timed_output_dev *dev, int ms_time) |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 141 | { |
| 142 | struct timed_vibrator_data *vib = |
| 143 | container_of(dev, struct timed_vibrator_data, dev); |
| 144 | unsigned long flags; |
| 145 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 146 | pr_debug("%s: ms_time %d \n", __func__, ms_time); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 147 | spin_lock_irqsave(&vib->lock, flags); |
| 148 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 149 | if (ms_time > 0) { |
| 150 | if (ms_time > vib->max_timeout) |
| 151 | ms_time = vib->max_timeout; |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 152 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 153 | atomic_set(&vib->ms_time, ms_time); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 154 | |
| 155 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 156 | queue_work(vibrator_workqueue,&vib->work_vibrator_on); |
| 157 | #endif |
| 158 | } else { |
| 159 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 160 | queue_work(vibrator_workqueue,&vib->work_vibrator_off); |
| 161 | #endif |
| 162 | } |
| 163 | spin_unlock_irqrestore(&vib->lock, flags); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static ssize_t vibrator_amp_show(struct device *dev, |
| 167 | struct device_attribute *attr, char *buf) |
| 168 | { |
| 169 | struct timed_output_dev *dev_ = |
| 170 | (struct timed_output_dev *)dev_get_drvdata(dev); |
| 171 | struct timed_vibrator_data *vib = |
| 172 | container_of(dev_, struct timed_vibrator_data, dev); |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 173 | int gain = atomic_read(&(vib->gain)); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 174 | |
| 175 | return sprintf(buf, "%d\n", gain); |
| 176 | } |
| 177 | |
| 178 | static ssize_t vibrator_amp_store(struct device *dev, |
| 179 | struct device_attribute *attr, const char *buf, size_t size) |
| 180 | { |
| 181 | struct timed_output_dev *dev_ = |
| 182 | (struct timed_output_dev *)dev_get_drvdata(dev); |
| 183 | struct timed_vibrator_data *vib = |
| 184 | container_of(dev_, struct timed_vibrator_data, dev); |
| 185 | |
| 186 | int gain; |
| 187 | sscanf(buf, "%d", &gain); |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 188 | atomic_set(&vib->gain, gain); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 189 | |
| 190 | return size; |
| 191 | } |
| 192 | |
| 193 | static ssize_t vibrator_pwm_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 Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 200 | int gain = atomic_read(&(vib->pwm)); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 201 | |
| 202 | gain = 4800/gain; |
| 203 | |
| 204 | return sprintf(buf, "%d\n", gain); |
| 205 | } |
| 206 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 207 | |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 208 | static ssize_t vibrator_pwm_store(struct device *dev, |
| 209 | struct device_attribute *attr, const char *buf, size_t size) |
| 210 | { |
| 211 | struct timed_output_dev *dev_ = |
| 212 | (struct timed_output_dev *)dev_get_drvdata(dev); |
| 213 | struct timed_vibrator_data *vib = |
| 214 | container_of(dev_, struct timed_vibrator_data, dev); |
| 215 | |
| 216 | int gain; |
| 217 | sscanf(buf, "%d", &gain); |
| 218 | |
| 219 | gain = 4800/gain; |
| 220 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 221 | atomic_set(&vib->pwm, gain); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 222 | |
| 223 | return size; |
| 224 | } |
| 225 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 226 | static struct device_attribute android_vibrator_device_attrs[] = { |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 227 | __ATTR(amp, S_IRUGO | S_IWUSR, vibrator_amp_show, vibrator_amp_store), |
| 228 | __ATTR(pwm, S_IRUGO | S_IWUSR, vibrator_pwm_show, vibrator_pwm_store), |
| 229 | }; |
| 230 | |
| 231 | struct timed_vibrator_data android_vibrator_data = { |
| 232 | .dev.name = "vibrator", |
| 233 | .dev.enable = vibrator_enable, |
| 234 | .dev.get_time = vibrator_get_time, |
| 235 | .max_timeout = 30000, /* max time for vibrator enable 30 sec. */ |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 236 | .pdata = NULL, |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | static int android_vibrator_probe(struct platform_device *pdev) |
| 240 | { |
| 241 | |
| 242 | int i, ret = 0; |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 243 | struct timed_vibrator_data *vib = &android_vibrator_data; |
| 244 | vib->pdata = pdev->dev.platform_data; |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 245 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 246 | if (!vib->pdata) { |
| 247 | pr_err("%s: no platform data\n", __func__); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 248 | return -ENODEV; |
| 249 | } |
| 250 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 251 | if (vib->pdata->vibrator_init) { |
| 252 | ret = vib->pdata->vibrator_init(); |
| 253 | if (ret < 0) { |
| 254 | pr_err("%s: failed to vibrator init\n", __func__); |
| 255 | return ret; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | platform_set_drvdata(pdev, &android_vibrator_data); |
| 260 | |
| 261 | atomic_set(&vib->gain, vib->pdata->amp); /* max value is 128 */ |
| 262 | atomic_set(&vib->pwm, vib->pdata->vibe_n_value); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 263 | atomic_set(&vib->vib_status, false); |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 264 | pr_info("android_vibrator: default amplitude %d \n", |
| 265 | vib->pdata->amp); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 266 | |
| 267 | INIT_WORK(&vib->work_vibrator_off, android_vibrator_off); |
| 268 | INIT_WORK(&vib->work_vibrator_on, android_vibrator_on); |
| 269 | hrtimer_init(&vib->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 270 | vib->timer.function = vibrator_timer_func; |
| 271 | spin_lock_init(&vib->lock); |
| 272 | |
| 273 | ret = timed_output_dev_register(&vib->dev); |
| 274 | if (ret < 0) { |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 275 | pr_err("%s: failed to register timed output device\n", |
| 276 | __func__); |
| 277 | return ret; |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 278 | } |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 279 | |
| 280 | for (i = 0; i < ARRAY_SIZE(android_vibrator_device_attrs); i++) { |
| 281 | ret = device_create_file(vib->dev.dev, |
| 282 | &android_vibrator_device_attrs[i]); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 283 | if (ret < 0) { |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 284 | pr_err("%s: failed to create sysfs\n", __func__); |
| 285 | goto err_sysfs; |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 289 | pr_info("android vibrator probed\n"); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 290 | |
| 291 | return 0; |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 292 | |
| 293 | err_sysfs: |
| 294 | for (; i >= 0; i--) { |
| 295 | device_remove_file(vib->dev.dev, |
| 296 | &android_vibrator_device_attrs[i]); |
| 297 | } |
| 298 | |
| 299 | timed_output_dev_unregister(&vib->dev); |
| 300 | return ret; |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static int android_vibrator_remove(struct platform_device *pdev) |
| 304 | { |
| 305 | struct timed_vibrator_data *vib = |
| 306 | (struct timed_vibrator_data *)platform_get_drvdata(pdev); |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 307 | int i; |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 308 | |
| 309 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 310 | queue_work(vibrator_workqueue, &vib->work_vibrator_off); |
| 311 | #endif |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 312 | for (i = ARRAY_SIZE(android_vibrator_device_attrs); i >= 0; i--) { |
| 313 | device_remove_file(vib->dev.dev, |
| 314 | &android_vibrator_device_attrs[i]); |
| 315 | } |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 316 | |
| 317 | timed_output_dev_unregister(&vib->dev); |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 322 | static struct platform_driver android_vibrator_driver = { |
| 323 | .probe = android_vibrator_probe, |
| 324 | .remove = android_vibrator_remove, |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 325 | .driver = { |
| 326 | .name = "android-vibrator", |
| 327 | }, |
| 328 | }; |
| 329 | |
| 330 | static int __init android_vibrator_init(void) |
| 331 | { |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 332 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 333 | vibrator_workqueue = create_workqueue("vibrator"); |
| 334 | |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 335 | if (!vibrator_workqueue) { |
| 336 | pr_err("%s: out of memory\n", __func__); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 337 | return -ENOMEM; |
Devin Kim | 795a9a6 | 2012-08-28 10:08:46 -0700 | [diff] [blame^] | 338 | } |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 339 | #endif |
| 340 | return platform_driver_register(&android_vibrator_driver); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static void __exit android_vibrator_exit(void) |
| 344 | { |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 345 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 346 | if (vibrator_workqueue) |
| 347 | destroy_workqueue(vibrator_workqueue); |
| 348 | |
| 349 | vibrator_workqueue = NULL; |
| 350 | #endif |
| 351 | platform_driver_unregister(&android_vibrator_driver); |
Devin Kim | 5546885 | 2012-06-19 12:01:32 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | late_initcall_sync(android_vibrator_init); /* to let init lately */ |
| 355 | module_exit(android_vibrator_exit); |
| 356 | |
| 357 | MODULE_AUTHOR("LG Electronics Inc."); |
| 358 | MODULE_DESCRIPTION("Android Vibrator Driver"); |
| 359 | MODULE_LICENSE("GPL"); |