max77826_vibrator.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 max77826_vib {
  26. struct hrtimer vib_timer;
  27. struct timed_output_dev timed_dev;
  28. struct work_struct work;
  29. struct workqueue_struct *queue;
  30. struct regulator *vib_power;
  31. const char *power_name;
  32. int power_volt;
  33. int state;
  34. int timeout;
  35. struct mutex lock;
  36. };
  37. static void set_vibrator(struct regulator *reg_vib, int on)
  38. {
  39. int ret = 0;
  40. pr_info("[VIB] %s, on=%d\n",__func__, on);
  41. if (on) {
  42. if (regulator_is_enabled(reg_vib))
  43. pr_err("%s: power regulator(3.0V) is enabled\n", __func__);
  44. else
  45. ret = regulator_enable(reg_vib);
  46. if (ret) {
  47. pr_err("%s: power regulator enable failed, rc=%d\n",
  48. __func__, ret);
  49. return;
  50. }
  51. pr_info("%s: max77826 VIB 3.0V ON\n", __func__);
  52. } else {
  53. if (regulator_is_enabled(reg_vib))
  54. ret = regulator_disable(reg_vib);
  55. else
  56. pr_err("%s: power regulator(3.0V) is disabled\n", __func__);
  57. if (ret) {
  58. pr_err("%s: disable power regulator failed, rc=%d\n",
  59. __func__, ret);
  60. return;
  61. }
  62. pr_info("%s: max77826 VIB 3.0V OFF\n", __func__);
  63. }
  64. }
  65. static void vibrator_enable(struct timed_output_dev *dev, int value)
  66. {
  67. struct max77826_vib *vib = container_of(dev, struct max77826_vib,
  68. timed_dev);
  69. mutex_lock(&vib->lock);
  70. hrtimer_cancel(&vib->vib_timer);
  71. if (value == 0) {
  72. pr_info("[VIB] OFF\n");
  73. vib->state = 0;
  74. }
  75. else {
  76. pr_info("[VIB] ON, Duration : %d msec\n" , value);
  77. vib->state = 1;
  78. if (value == 0x7fffffff){
  79. pr_info("[VIB] No Use Timer %d \n", value);
  80. }
  81. else {
  82. value = (value > vib->timeout ?
  83. vib->timeout : value);
  84. hrtimer_start(&vib->vib_timer,
  85. ktime_set(value / 1000, (value % 1000) * 1000000),
  86. HRTIMER_MODE_REL);
  87. }
  88. }
  89. mutex_unlock(&vib->lock);
  90. queue_work(vib->queue, &vib->work);
  91. }
  92. static void max77826_vibrator_update(struct work_struct *work)
  93. {
  94. struct max77826_vib *vib = container_of(work, struct max77826_vib,
  95. work);
  96. set_vibrator(vib->vib_power, vib->state);
  97. }
  98. static int vibrator_get_time(struct timed_output_dev *dev)
  99. {
  100. struct max77826_vib *vib = container_of(dev, struct max77826_vib,
  101. timed_dev);
  102. if (hrtimer_active(&vib->vib_timer)) {
  103. ktime_t r = hrtimer_get_remaining(&vib->vib_timer);
  104. return (int)ktime_to_us(r);
  105. }
  106. else
  107. return 0;
  108. }
  109. static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
  110. {
  111. struct max77826_vib *vib = container_of(timer, struct max77826_vib,
  112. vib_timer);
  113. vib->state = 0;
  114. queue_work(vib->queue, &vib->work);
  115. return HRTIMER_NORESTART;
  116. }
  117. #ifdef CONFIG_PM
  118. static int max77826_vibrator_suspend(struct device *dev)
  119. {
  120. struct max77826_vib *vib = dev_get_drvdata(dev);
  121. pr_info("[VIB] %s\n",__func__);
  122. hrtimer_cancel(&vib->vib_timer);
  123. cancel_work_sync(&vib->work);
  124. /* turn-off vibrator */
  125. set_vibrator(vib->vib_power, 0);
  126. return 0;
  127. }
  128. #endif
  129. static SIMPLE_DEV_PM_OPS(vibrator_pm_ops, max77826_vibrator_suspend, NULL);
  130. static int max77826_vibrator_probe(struct platform_device *pdev)
  131. {
  132. struct max77826_vib *vib;
  133. int rc = 0;
  134. pr_info("[VIB] %s\n",__func__);
  135. vib = devm_kzalloc(&pdev->dev, sizeof(*vib), GFP_KERNEL);
  136. if (!vib) {
  137. pr_err("%s : Failed to allocate memory\n", __func__);
  138. return -ENOMEM;
  139. }
  140. rc = of_property_read_string(pdev->dev.of_node,
  141. "vibrator,ldo_name", &vib->power_name);
  142. if (rc < 0) {
  143. pr_err("[%s]: Unable to read ldo_name\n", __func__);
  144. }
  145. rc = of_property_read_u32(pdev->dev.of_node,
  146. "vibrator,ldo_volt", &vib->power_volt);
  147. if (rc < 0) {
  148. pr_err("[%s] Unable to read ldo_volt\n", __func__);
  149. }
  150. vib->vib_power = devm_regulator_get(&pdev->dev, vib->power_name);
  151. if (IS_ERR(vib->vib_power)) {
  152. pr_err("%s: could not get vib_power, rc=%ld\n",
  153. __func__, PTR_ERR(vib->vib_power));
  154. return PTR_ERR(vib->vib_power);
  155. } else {
  156. pr_info("%s: vib_power init.. \n", __func__);
  157. }
  158. pr_info("[%s] ldo_name[%s] ldo_volt[%d]\n", __func__,
  159. vib->power_name, vib->power_volt);
  160. regulator_set_voltage(vib->vib_power, vib->power_volt, vib->power_volt);
  161. vib->timeout = VIB_DEFAULT_TIMEOUT;
  162. INIT_WORK(&vib->work, max77826_vibrator_update);
  163. mutex_init(&vib->lock);
  164. vib->queue = create_singlethread_workqueue("max77826_vibrator");
  165. if (!vib->queue) {
  166. pr_err("%s(): can't create workqueue\n", __func__);
  167. return -ENOMEM;
  168. }
  169. hrtimer_init(&vib->vib_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  170. vib->vib_timer.function = vibrator_timer_func;
  171. vib->timed_dev.name = "vibrator";
  172. vib->timed_dev.get_time = vibrator_get_time;
  173. vib->timed_dev.enable = vibrator_enable;
  174. dev_set_drvdata(&pdev->dev, vib);
  175. rc = timed_output_dev_register(&vib->timed_dev);
  176. if (rc < 0) {
  177. pr_err("[VIB] timed_output_dev_register fail (rc=%d)\n", rc);
  178. goto err_read_vib;
  179. }
  180. return 0;
  181. err_read_vib:
  182. destroy_workqueue(vib->queue);
  183. return rc;
  184. }
  185. static int max77826_vibrator_remove(struct platform_device *pdev)
  186. {
  187. struct max77826_vib *vib = dev_get_drvdata(&pdev->dev);
  188. destroy_workqueue(vib->queue);
  189. mutex_destroy(&vib->lock);
  190. return 0;
  191. }
  192. static const struct of_device_id vib_motor_match[] = {
  193. {.compatible = "max77826,vibrator"},
  194. {}
  195. };
  196. static struct platform_driver max77826_vibrator_drv =
  197. {
  198. .driver = {
  199. .name = "max77826_vibrator",
  200. .owner = THIS_MODULE,
  201. .of_match_table = vib_motor_match,
  202. .pm = &vibrator_pm_ops,
  203. },
  204. .probe = max77826_vibrator_probe,
  205. .remove = __devexit_p(max77826_vibrator_remove),
  206. };
  207. static int __init max77826_vibrator_init(void)
  208. {
  209. pr_info("[VIB] %s\n",__func__);
  210. return platform_driver_register(&max77826_vibrator_drv);
  211. }
  212. void __exit max77826_vibrator_exit(void)
  213. {
  214. platform_driver_unregister(&max77826_vibrator_drv);
  215. }
  216. module_init(max77826_vibrator_init);
  217. module_exit(max77826_vibrator_exit);
  218. MODULE_DESCRIPTION("timed output vibrator device");
  219. MODULE_LICENSE("GPL v2");