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; |
| 48 | atomic_t vibe_gain; /* default max gain */ |
| 49 | atomic_t vibe_pwm; |
| 50 | atomic_t vib_timems; /* vibrator duration */ |
| 51 | struct android_vibrator_platform_data *vibe_data; |
| 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, |
| 57 | int nVibIntensity, int n_value) |
| 58 | { |
| 59 | /* Check the Force value with Max and Min force value */ |
| 60 | int vib_dutation_ms = 0; |
| 61 | INFO_MSG("nVibIntensity : %d\n", nVibIntensity); |
| 62 | |
| 63 | if (nVibIntensity > 127) |
| 64 | nVibIntensity = 127; |
| 65 | if (nVibIntensity < -127) |
| 66 | nVibIntensity = -127; |
| 67 | |
| 68 | /* TODO: control the gain of vibrator */ |
| 69 | if (nVibIntensity == 0) { |
| 70 | vib->vibe_data->ic_enable_set(0); |
| 71 | vib->vibe_data->pwm_set(0, 0, n_value); |
| 72 | /* should be checked for vibrator response time */ |
| 73 | vib->vibe_data->power_set(0); |
| 74 | |
| 75 | atomic_set(&vib->vib_status, false); |
| 76 | } else { |
| 77 | cancel_work_sync(&vib->work_vibrator_off); |
| 78 | hrtimer_cancel(&vib->timer); |
| 79 | vib_dutation_ms = atomic_read(&vib->vib_timems); |
| 80 | /* should be checked for vibrator response time */ |
| 81 | vib->vibe_data->power_set(1); |
| 82 | vib->vibe_data->pwm_set(1, nVibIntensity, n_value); |
| 83 | vib->vibe_data->ic_enable_set(1); |
| 84 | |
| 85 | atomic_set(&vib->vib_status, true); |
| 86 | hrtimer_start(&vib->timer, |
| 87 | ktime_set(vib_dutation_ms / 1000, |
| 88 | (vib_dutation_ms % 1000) * 1000000), |
| 89 | HRTIMER_MODE_REL); |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static void android_vibrator_on(struct work_struct *work) |
| 95 | { |
| 96 | struct timed_vibrator_data *vib = |
| 97 | container_of(work, struct timed_vibrator_data, |
| 98 | work_vibrator_on); |
| 99 | int gain = atomic_read(&vib->vibe_gain); |
| 100 | int n_value = atomic_read(&vib->vibe_pwm); |
| 101 | INFO_MSG("%s\n", __func__); |
| 102 | /* suspend /resume logging test */ |
| 103 | INFO_MSG("%s is stating ...gain = %d n_value = %d\n", __func__, |
| 104 | gain, n_value); |
| 105 | |
| 106 | android_vibrator_force_set(vib, gain, n_value); |
| 107 | |
| 108 | INFO_MSG("%s is exting ... \n", __func__); |
| 109 | } |
| 110 | |
| 111 | static void android_vibrator_off(struct work_struct *work) |
| 112 | { |
| 113 | struct timed_vibrator_data *vib = |
| 114 | container_of(work, struct timed_vibrator_data, |
| 115 | work_vibrator_off); |
| 116 | |
| 117 | INFO_MSG("%s is stating ... \n", __func__); |
| 118 | android_vibrator_force_set(vib, 0, vib->vibe_data->vibe_n_value); |
| 119 | INFO_MSG("%s is exting ... \n", __func__); |
| 120 | } |
| 121 | |
| 122 | static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer) |
| 123 | { |
| 124 | struct timed_vibrator_data *vib = |
| 125 | container_of(timer, struct timed_vibrator_data, timer); |
| 126 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 127 | queue_work(vibrator_workqueue,&vib->work_vibrator_off); |
| 128 | #endif |
| 129 | return HRTIMER_NORESTART; |
| 130 | } |
| 131 | |
| 132 | static int vibrator_get_time(struct timed_output_dev *dev) |
| 133 | { |
| 134 | struct timed_vibrator_data *vib = |
| 135 | container_of(dev, struct timed_vibrator_data, dev); |
| 136 | |
| 137 | if (hrtimer_active(&vib->timer)) { |
| 138 | ktime_t r = hrtimer_get_remaining(&vib->timer); |
| 139 | return ktime_to_ms(r); |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static void vibrator_enable(struct timed_output_dev *dev, int nDurationMs) |
| 146 | { |
| 147 | struct timed_vibrator_data *vib = |
| 148 | container_of(dev, struct timed_vibrator_data, dev); |
| 149 | unsigned long flags; |
| 150 | |
| 151 | INFO_MSG("%s is stating ... \n", __func__); |
| 152 | INFO_MSG("Android_Vibrator[%d] DurationMs = %d \n", |
| 153 | __LINE__, nDurationMs); |
| 154 | spin_lock_irqsave(&vib->lock, flags); |
| 155 | |
| 156 | if (nDurationMs > 0) { |
| 157 | if (nDurationMs > vib->max_timeout) |
| 158 | nDurationMs = vib->max_timeout; |
| 159 | |
| 160 | atomic_set(&vib->vib_timems, nDurationMs); |
| 161 | |
| 162 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 163 | queue_work(vibrator_workqueue,&vib->work_vibrator_on); |
| 164 | #endif |
| 165 | } else { |
| 166 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 167 | queue_work(vibrator_workqueue,&vib->work_vibrator_off); |
| 168 | #endif |
| 169 | } |
| 170 | spin_unlock_irqrestore(&vib->lock, flags); |
| 171 | |
| 172 | INFO_MSG("%s is exting ... \n", __func__); |
| 173 | } |
| 174 | |
| 175 | static ssize_t vibrator_amp_show(struct device *dev, |
| 176 | struct device_attribute *attr, char *buf) |
| 177 | { |
| 178 | struct timed_output_dev *dev_ = |
| 179 | (struct timed_output_dev *)dev_get_drvdata(dev); |
| 180 | struct timed_vibrator_data *vib = |
| 181 | container_of(dev_, struct timed_vibrator_data, dev); |
| 182 | int gain = atomic_read(&(vib->vibe_gain)); |
| 183 | |
| 184 | return sprintf(buf, "%d\n", gain); |
| 185 | } |
| 186 | |
| 187 | static ssize_t vibrator_amp_store(struct device *dev, |
| 188 | struct device_attribute *attr, const char *buf, size_t size) |
| 189 | { |
| 190 | struct timed_output_dev *dev_ = |
| 191 | (struct timed_output_dev *)dev_get_drvdata(dev); |
| 192 | struct timed_vibrator_data *vib = |
| 193 | container_of(dev_, struct timed_vibrator_data, dev); |
| 194 | |
| 195 | int gain; |
| 196 | sscanf(buf, "%d", &gain); |
| 197 | atomic_set(&vib->vibe_gain, gain); |
| 198 | |
| 199 | return size; |
| 200 | } |
| 201 | |
| 202 | static ssize_t vibrator_pwm_show(struct device *dev, |
| 203 | struct device_attribute *attr, char *buf) |
| 204 | { |
| 205 | struct timed_output_dev *dev_ = |
| 206 | (struct timed_output_dev *)dev_get_drvdata(dev); |
| 207 | struct timed_vibrator_data *vib = |
| 208 | container_of(dev_, struct timed_vibrator_data, dev); |
| 209 | int gain = atomic_read(&(vib->vibe_pwm)); |
| 210 | |
| 211 | gain = 4800/gain; |
| 212 | |
| 213 | return sprintf(buf, "%d\n", gain); |
| 214 | } |
| 215 | |
| 216 | static ssize_t vibrator_pwm_store(struct device *dev, |
| 217 | struct device_attribute *attr, const char *buf, size_t size) |
| 218 | { |
| 219 | struct timed_output_dev *dev_ = |
| 220 | (struct timed_output_dev *)dev_get_drvdata(dev); |
| 221 | struct timed_vibrator_data *vib = |
| 222 | container_of(dev_, struct timed_vibrator_data, dev); |
| 223 | |
| 224 | int gain; |
| 225 | sscanf(buf, "%d", &gain); |
| 226 | |
| 227 | gain = 4800/gain; |
| 228 | |
| 229 | atomic_set(&vib->vibe_pwm, gain); |
| 230 | |
| 231 | return size; |
| 232 | } |
| 233 | |
| 234 | static struct device_attribute sm100_device_attrs[] = { |
| 235 | __ATTR(amp, S_IRUGO | S_IWUSR, vibrator_amp_show, vibrator_amp_store), |
| 236 | __ATTR(pwm, S_IRUGO | S_IWUSR, vibrator_pwm_show, vibrator_pwm_store), |
| 237 | }; |
| 238 | |
| 239 | struct timed_vibrator_data android_vibrator_data = { |
| 240 | .dev.name = "vibrator", |
| 241 | .dev.enable = vibrator_enable, |
| 242 | .dev.get_time = vibrator_get_time, |
| 243 | .max_timeout = 30000, /* max time for vibrator enable 30 sec. */ |
| 244 | .vibe_data = NULL, |
| 245 | }; |
| 246 | |
| 247 | static int android_vibrator_probe(struct platform_device *pdev) |
| 248 | { |
| 249 | |
| 250 | int i, ret = 0; |
| 251 | struct timed_vibrator_data *vib; |
| 252 | |
| 253 | platform_set_drvdata(pdev, &android_vibrator_data); |
| 254 | vib = (struct timed_vibrator_data *)platform_get_drvdata(pdev); |
| 255 | vib->vibe_data = |
| 256 | (struct android_vibrator_platform_data *)pdev->dev.platform_data; |
| 257 | |
| 258 | if (vib->vibe_data->vibrator_init() < 0) { |
| 259 | ERR_MSG("Android Vreg, GPIO set failed\n"); |
| 260 | return -ENODEV; |
| 261 | } |
| 262 | |
| 263 | atomic_set(&vib->vibe_gain, vib->vibe_data->amp); /* max value is 128 */ |
| 264 | atomic_set(&vib->vibe_pwm, vib->vibe_data->vibe_n_value); |
| 265 | atomic_set(&vib->vib_status, false); |
| 266 | INFO_MSG("Android_Vibrator[%d] %s nVibIntensity = %d \n", |
| 267 | __LINE__, __func__,vib->vibe_data->amp); |
| 268 | |
| 269 | INIT_WORK(&vib->work_vibrator_off, android_vibrator_off); |
| 270 | INIT_WORK(&vib->work_vibrator_on, android_vibrator_on); |
| 271 | hrtimer_init(&vib->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 272 | vib->timer.function = vibrator_timer_func; |
| 273 | spin_lock_init(&vib->lock); |
| 274 | |
| 275 | ret = timed_output_dev_register(&vib->dev); |
| 276 | if (ret < 0) { |
| 277 | timed_output_dev_unregister(&vib->dev); |
| 278 | return -ENODEV; |
| 279 | } |
| 280 | for (i = 0; i < ARRAY_SIZE(sm100_device_attrs); i++) { |
| 281 | ret = device_create_file(vib->dev.dev, &sm100_device_attrs[i]); |
| 282 | if (ret < 0) { |
| 283 | timed_output_dev_unregister(&vib->dev); |
| 284 | device_remove_file(vib->dev.dev, &sm100_device_attrs[i]); |
| 285 | return -ENODEV; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | INFO_MSG("Android Vibrator Initialization was done\n"); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int android_vibrator_remove(struct platform_device *pdev) |
| 295 | { |
| 296 | struct timed_vibrator_data *vib = |
| 297 | (struct timed_vibrator_data *)platform_get_drvdata(pdev); |
| 298 | |
| 299 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 300 | queue_work(vibrator_workqueue, &vib->work_vibrator_off); |
| 301 | #endif |
| 302 | |
| 303 | timed_output_dev_unregister(&vib->dev); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | #ifdef CONFIG_PM |
| 309 | static int android_vibrator_suspend(struct platform_device *pdev, |
| 310 | pm_message_t state) |
| 311 | { |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static int android_vibrator_resume(struct platform_device *pdev) |
| 316 | { |
| 317 | return 0; |
| 318 | } |
| 319 | #endif |
| 320 | |
| 321 | static void android_vibrator_shutdown(struct platform_device *pdev) |
| 322 | { |
| 323 | } |
| 324 | |
| 325 | static struct platform_driver android_vibrator_driver = { |
| 326 | .probe = android_vibrator_probe, |
| 327 | .remove = android_vibrator_remove, |
| 328 | .shutdown = android_vibrator_shutdown, |
| 329 | #ifdef CONFIG_PM |
| 330 | .suspend = android_vibrator_suspend, |
| 331 | .resume = android_vibrator_resume, |
| 332 | #else |
| 333 | .suspend = NULL, |
| 334 | .resume = NULL, |
| 335 | #endif |
| 336 | .driver = { |
| 337 | .name = "android-vibrator", |
| 338 | }, |
| 339 | }; |
| 340 | |
| 341 | static int __init android_vibrator_init(void) |
| 342 | { |
| 343 | #if defined(CONFIG_ANDROID_VIBRATOR) |
| 344 | INFO_MSG("Android Vibrator Driver Init\n"); |
| 345 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 346 | vibrator_workqueue = create_workqueue("vibrator"); |
| 347 | |
| 348 | if(!vibrator_workqueue) |
| 349 | return -ENOMEM; |
| 350 | #endif |
| 351 | return platform_driver_register(&android_vibrator_driver); |
| 352 | #endif |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static void __exit android_vibrator_exit(void) |
| 357 | { |
| 358 | #if defined(CONFIG_ANDROID_VIBRATOR) |
| 359 | INFO_MSG("Android Vibrator Driver Exit\n"); |
| 360 | #ifdef ANDROID_VIBRATOR_USE_WORKQUEUE |
| 361 | if (vibrator_workqueue) |
| 362 | destroy_workqueue(vibrator_workqueue); |
| 363 | |
| 364 | vibrator_workqueue = NULL; |
| 365 | #endif |
| 366 | platform_driver_unregister(&android_vibrator_driver); |
| 367 | #endif |
| 368 | } |
| 369 | |
| 370 | late_initcall_sync(android_vibrator_init); /* to let init lately */ |
| 371 | module_exit(android_vibrator_exit); |
| 372 | |
| 373 | MODULE_AUTHOR("LG Electronics Inc."); |
| 374 | MODULE_DESCRIPTION("Android Vibrator Driver"); |
| 375 | MODULE_LICENSE("GPL"); |