msm_vibrator.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/err.h>
  15. #include <linux/hrtimer.h>
  16. #include <linux/module.h>
  17. #include <linux/gpio.h>
  18. #include <mach/pmic.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/of_gpio.h>
  22. #include "../staging/android/timed_output.h"
  23. /* default timeout */
  24. #define VIB_DEFAULT_TIMEOUT 10000
  25. struct msm_vib {
  26. struct hrtimer vib_timer;
  27. struct timed_output_dev timed_dev;
  28. struct work_struct work;
  29. struct workqueue_struct *queue;
  30. int state;
  31. int timeout;
  32. int motor_en;
  33. struct mutex lock;
  34. int pmic_gpio_enabled;
  35. };
  36. static void set_vibrator(int motor_en, int on)
  37. {
  38. pr_info("[VIB] %s, on=%d\n",__func__, on);
  39. gpio_set_value(motor_en, on);
  40. }
  41. static void vibrator_enable(struct timed_output_dev *dev, int value)
  42. {
  43. struct msm_vib *vib = container_of(dev, struct msm_vib,
  44. timed_dev);
  45. mutex_lock(&vib->lock);
  46. hrtimer_cancel(&vib->vib_timer);
  47. if (value == 0) {
  48. pr_info("[VIB] OFF\n");
  49. vib->state = 0;
  50. }
  51. else {
  52. pr_info("[VIB] ON, Duration : %d msec\n" , value);
  53. vib->state = 1;
  54. if (value == 0x7fffffff){
  55. pr_info("[VIB] No Use Timer %d \n", value);
  56. }
  57. else {
  58. value = (value > vib->timeout ?
  59. vib->timeout : value);
  60. hrtimer_start(&vib->vib_timer,
  61. ktime_set(value / 1000, (value % 1000) * 1000000),
  62. HRTIMER_MODE_REL);
  63. }
  64. }
  65. mutex_unlock(&vib->lock);
  66. queue_work(vib->queue, &vib->work);
  67. }
  68. static void msm_vibrator_update(struct work_struct *work)
  69. {
  70. struct msm_vib *vib = container_of(work, struct msm_vib,
  71. work);
  72. set_vibrator(vib->motor_en, vib->state);
  73. }
  74. static int vibrator_get_time(struct timed_output_dev *dev)
  75. {
  76. struct msm_vib *vib = container_of(dev, struct msm_vib,
  77. timed_dev);
  78. if (hrtimer_active(&vib->vib_timer)) {
  79. ktime_t r = hrtimer_get_remaining(&vib->vib_timer);
  80. return (int)ktime_to_us(r);
  81. }
  82. else
  83. return 0;
  84. }
  85. static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
  86. {
  87. struct msm_vib *vib = container_of(timer, struct msm_vib,
  88. vib_timer);
  89. vib->state = 0;
  90. queue_work(vib->queue, &vib->work);
  91. return HRTIMER_NORESTART;
  92. }
  93. #ifdef CONFIG_PM
  94. static int msm_vibrator_suspend(struct device *dev)
  95. {
  96. struct msm_vib *vib = dev_get_drvdata(dev);
  97. pr_info("[VIB] %s\n",__func__);
  98. hrtimer_cancel(&vib->vib_timer);
  99. cancel_work_sync(&vib->work);
  100. /* turn-off vibrator */
  101. set_vibrator(vib->motor_en, 0);
  102. return 0;
  103. }
  104. #endif
  105. static SIMPLE_DEV_PM_OPS(vibrator_pm_ops, msm_vibrator_suspend, NULL);
  106. extern int system_rev;
  107. extern int expander_gpio_config(unsigned config, unsigned disable);
  108. static int msm_vibrator_probe(struct platform_device *pdev)
  109. {
  110. struct msm_vib *vib;
  111. int rc = 0;
  112. pr_info("[VIB] %s\n",__func__);
  113. vib = devm_kzalloc(&pdev->dev, sizeof(*vib), GFP_KERNEL);
  114. if (!vib) {
  115. pr_err("%s : Failed to allocate memory\n", __func__);
  116. return -ENOMEM;
  117. }
  118. vib->motor_en = of_get_named_gpio(pdev->dev.of_node, "samsung,motor-en", 0);
  119. if (!gpio_is_valid(vib->motor_en)) {
  120. pr_err("%s:%d, reset gpio not specified\n",
  121. __func__, __LINE__);
  122. return -EINVAL;
  123. }
  124. vib->pmic_gpio_enabled = of_property_read_bool(pdev->dev.of_node, "samsung,is_pmic_vib_en");
  125. #ifdef CONFIG_SEC_AFYON_PROJECT
  126. vib->pmic_gpio_enabled = 0;
  127. #endif
  128. printk(KERN_ALERT " VIB PMIC GPIO ENABLED Flag is %d \n", vib->pmic_gpio_enabled);
  129. if (!(vib->pmic_gpio_enabled)){
  130. #if defined (CONFIG_GPIO_PCAL6416A)
  131. #ifdef CONFIG_MACH_ATLANTICLTE_ATT
  132. if(system_rev == 0) {
  133. rc = gpio_tlmm_config(GPIO_CFG(vib->motor_en, 0, GPIO_CFG_OUTPUT,
  134. GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  135. if (rc < 0) {
  136. pr_err("%s: gpio_tlmm_config is failed\n",__func__);
  137. gpio_free(vib->motor_en);
  138. return rc;
  139. }
  140. }
  141. else {
  142. rc = expander_gpio_config(GPIO_CFG(vib->motor_en, 0, GPIO_CFG_OUTPUT,
  143. GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  144. if (rc < 0) {
  145. pr_err("%s: expander_tlmm_config is failed\n",__func__);
  146. return rc;
  147. }
  148. }
  149. #else
  150. rc = expander_gpio_config(GPIO_CFG(vib->motor_en, 0, GPIO_CFG_OUTPUT,
  151. GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  152. if (rc < 0) {
  153. pr_err("%s: expander_tlmm_config is failed\n",__func__);
  154. return rc;
  155. }
  156. #endif
  157. #else
  158. rc = gpio_tlmm_config(GPIO_CFG(vib->motor_en, 0, GPIO_CFG_OUTPUT,
  159. GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  160. if (rc < 0) {
  161. pr_err("%s: gpio_tlmm_config is failed\n",__func__);
  162. gpio_free(vib->motor_en);
  163. return rc;
  164. }
  165. #endif
  166. }
  167. vib->timeout = VIB_DEFAULT_TIMEOUT;
  168. INIT_WORK(&vib->work, msm_vibrator_update);
  169. mutex_init(&vib->lock);
  170. vib->queue = create_singlethread_workqueue("msm_vibrator");
  171. if (!vib->queue) {
  172. pr_err("%s(): can't create workqueue\n", __func__);
  173. return -ENOMEM;
  174. }
  175. hrtimer_init(&vib->vib_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  176. vib->vib_timer.function = vibrator_timer_func;
  177. vib->timed_dev.name = "vibrator";
  178. vib->timed_dev.get_time = vibrator_get_time;
  179. vib->timed_dev.enable = vibrator_enable;
  180. dev_set_drvdata(&pdev->dev, vib);
  181. rc = timed_output_dev_register(&vib->timed_dev);
  182. if (rc < 0) {
  183. pr_err("[VIB] timed_output_dev_register fail (rc=%d)\n", rc);
  184. goto err_read_vib;
  185. }
  186. return 0;
  187. err_read_vib:
  188. destroy_workqueue(vib->queue);
  189. return rc;
  190. }
  191. static int msm_vibrator_remove(struct platform_device *pdev)
  192. {
  193. struct msm_vib *vib = dev_get_drvdata(&pdev->dev);
  194. destroy_workqueue(vib->queue);
  195. mutex_destroy(&vib->lock);
  196. return 0;
  197. }
  198. static const struct of_device_id vib_motor_match[] = {
  199. { .compatible = "vibrator",
  200. },
  201. {}
  202. };
  203. static struct platform_driver msm_vibrator_platdrv =
  204. {
  205. .driver =
  206. {
  207. .name = "msm_vibrator",
  208. .owner = THIS_MODULE,
  209. .of_match_table = vib_motor_match,
  210. .pm = &vibrator_pm_ops,
  211. },
  212. .probe = msm_vibrator_probe,
  213. .remove = __devexit_p(msm_vibrator_remove),
  214. };
  215. static int __init msm_timed_vibrator_init(void)
  216. {
  217. pr_info("[VIB] %s\n",__func__);
  218. return platform_driver_register(&msm_vibrator_platdrv);
  219. }
  220. void __exit msm_timed_vibrator_exit(void)
  221. {
  222. platform_driver_unregister(&msm_vibrator_platdrv);
  223. }
  224. module_init(msm_timed_vibrator_init);
  225. module_exit(msm_timed_vibrator_exit);
  226. MODULE_DESCRIPTION("timed output vibrator device");
  227. MODULE_LICENSE("GPL v2");