vibrator_drv.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /******************************************************************************
  2. * mt6575_vibrator.c - MT6575 Android Linux Vibrator Device Driver
  3. *
  4. * Copyright 2009-2010 MediaTek Co.,Ltd.
  5. *
  6. * DESCRIPTION:
  7. * This file provid the other drivers vibrator relative functions
  8. *
  9. ******************************************************************************/
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/device.h>
  15. #include <linux/workqueue.h>
  16. #include "timed_output.h"
  17. #include <linux/hrtimer.h>
  18. #include <linux/err.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/timer.h>
  23. /* #include <mach/mt6577_pm_ldo.h> */
  24. #include <vibrator.h>
  25. #include <vibrator_hal.h>
  26. #define VERSION "v 0.1"
  27. #define VIB_DEVICE "mtk_vibrator"
  28. static int debug_enable_vib_hal = 1;
  29. /* #define pr_fmt(fmt) "[vibrator]"fmt */
  30. #define VIB_DEBUG(format, args...) do { \
  31. if (debug_enable_vib_hal) {\
  32. pr_debug(format, ##args);\
  33. } \
  34. } while (0)
  35. /******************************************************************************
  36. Error Code No.
  37. ******************************************************************************/
  38. #define RSUCCESS 0
  39. /******************************************************************************
  40. Debug Message Settings
  41. ******************************************************************************/
  42. /* Debug message event */
  43. #define DBG_EVT_NONE 0x00000000 /* No event */
  44. #define DBG_EVT_INT 0x00000001 /* Interrupt related event */
  45. #define DBG_EVT_TASKLET 0x00000002 /* Tasklet related event */
  46. #define DBG_EVT_ALL 0xffffffff
  47. #define DBG_EVT_MASK (DBG_EVT_TASKLET)
  48. #if 1
  49. #define MSG(evt, fmt, args...) \
  50. do { \
  51. if ((DBG_EVT_##evt) & DBG_EVT_MASK) { \
  52. VIB_DEBUG(fmt, ##args); \
  53. } \
  54. } while (0)
  55. #define MSG_FUNC_ENTRY(f) MSG(FUC, "<FUN_ENT>: %s\n", __func__)
  56. #else
  57. #define MSG(evt, fmt, args...) do {} while (0)
  58. #define MSG_FUNC_ENTRY(f) do {} while (0)
  59. #endif
  60. /******************************************************************************
  61. Global Definations
  62. ******************************************************************************/
  63. static struct workqueue_struct *vibrator_queue;
  64. static struct work_struct vibrator_work;
  65. static struct hrtimer vibe_timer;
  66. static spinlock_t vibe_lock;
  67. static int vibe_state;
  68. static int ldo_state;
  69. static int shutdown_flag;
  70. static int vibr_Enable(void)
  71. {
  72. if (!ldo_state) {
  73. vibr_Enable_HW();
  74. ldo_state = 1;
  75. }
  76. return 0;
  77. }
  78. static int vibr_Disable(void)
  79. {
  80. if (ldo_state) {
  81. vibr_Disable_HW();
  82. ldo_state = 0;
  83. }
  84. return 0;
  85. }
  86. static void update_vibrator(struct work_struct *work)
  87. {
  88. if (!vibe_state)
  89. vibr_Disable();
  90. else
  91. vibr_Enable();
  92. }
  93. static int vibrator_get_time(struct timed_output_dev *dev)
  94. {
  95. if (hrtimer_active(&vibe_timer)) {
  96. ktime_t r = hrtimer_get_remaining(&vibe_timer);
  97. return ktime_to_ms(r);
  98. } else
  99. return 0;
  100. }
  101. static void vibrator_enable(struct timed_output_dev *dev, int value)
  102. {
  103. unsigned long flags;
  104. #if 1
  105. struct vibrator_hw *hw = mt_get_cust_vibrator_hw();
  106. #endif
  107. VIB_DEBUG("vibrator_enable: vibrator first in value = %d\n", value);
  108. spin_lock_irqsave(&vibe_lock, flags);
  109. while (hrtimer_cancel(&vibe_timer))
  110. VIB_DEBUG("vibrator_enable: try to cancel hrtimer\n");
  111. if (value == 0 || shutdown_flag == 1) {
  112. VIB_DEBUG("vibrator_enable: shutdown_flag = %d\n",
  113. shutdown_flag);
  114. vibe_state = 0;
  115. } else {
  116. #if 1
  117. VIB_DEBUG("vibrator_enable: vibrator cust timer: %d\n",
  118. hw->vib_timer);
  119. #ifdef CUST_VIBR_LIMIT
  120. if (value > hw->vib_limit && value < hw->vib_timer)
  121. #else
  122. if (value >= 10 && value < hw->vib_timer)
  123. #endif
  124. value = hw->vib_timer;
  125. #endif
  126. value = (value > 15000 ? 15000 : value);
  127. vibe_state = 1;
  128. hrtimer_start(&vibe_timer,
  129. ktime_set(value / 1000, (value % 1000) * 1000000),
  130. HRTIMER_MODE_REL);
  131. }
  132. spin_unlock_irqrestore(&vibe_lock, flags);
  133. VIB_DEBUG("vibrator_enable: vibrator start: %d\n", value);
  134. queue_work(vibrator_queue, &vibrator_work);
  135. }
  136. static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
  137. {
  138. vibe_state = 0;
  139. VIB_DEBUG("vibrator_timer_func: vibrator will disable\n");
  140. queue_work(vibrator_queue, &vibrator_work);
  141. return HRTIMER_NORESTART;
  142. }
  143. static struct timed_output_dev mtk_vibrator = {
  144. .name = "vibrator",
  145. .get_time = vibrator_get_time,
  146. .enable = vibrator_enable,
  147. };
  148. static int vib_probe(struct platform_device *pdev)
  149. {
  150. return 0;
  151. }
  152. static int vib_remove(struct platform_device *pdev)
  153. {
  154. return 0;
  155. }
  156. static void vib_shutdown(struct platform_device *pdev)
  157. {
  158. unsigned long flags;
  159. VIB_DEBUG("vib_shutdown: enter!\n");
  160. spin_lock_irqsave(&vibe_lock, flags);
  161. shutdown_flag = 1;
  162. if (vibe_state) {
  163. VIB_DEBUG("vib_shutdown: vibrator will disable\n");
  164. vibe_state = 0;
  165. spin_unlock_irqrestore(&vibe_lock, flags);
  166. vibr_Disable();
  167. } else {
  168. spin_unlock_irqrestore(&vibe_lock, flags);
  169. }
  170. }
  171. /******************************************************************************
  172. Device driver structure
  173. *****************************************************************************/
  174. static struct platform_driver vibrator_driver = {
  175. .probe = vib_probe,
  176. .remove = vib_remove,
  177. .shutdown = vib_shutdown,
  178. .driver = {
  179. .name = VIB_DEVICE,
  180. .owner = THIS_MODULE,
  181. },
  182. };
  183. static struct platform_device vibrator_device = {
  184. .name = "mtk_vibrator",
  185. .id = -1,
  186. };
  187. static ssize_t store_vibr_on(struct device *dev, struct device_attribute *attr,
  188. const char *buf, size_t size)
  189. {
  190. if (buf != NULL && size != 0) {
  191. /* VIB_DEBUG("buf is %s and size is %d\n", buf, size); */
  192. if (buf[0] == '0')
  193. vibr_Disable();
  194. else
  195. vibr_Enable();
  196. }
  197. return size;
  198. }
  199. static DEVICE_ATTR(vibr_on, 0220, NULL, store_vibr_on);
  200. /******************************************************************************
  201. * vib_mod_init
  202. *
  203. * DESCRIPTION:
  204. * Register the vibrator device driver !
  205. *
  206. * PARAMETERS:
  207. * None
  208. *
  209. * RETURNS:
  210. * None
  211. *
  212. * NOTES:
  213. * RSUCCESS : Success
  214. *
  215. ******************************************************************************/
  216. static int vib_mod_init(void)
  217. {
  218. s32 ret;
  219. VIB_DEBUG("MediaTek MTK vibrator driver register, version %s\n",
  220. VERSION);
  221. /* set vibr voltage if needs. Before MT6320 vibr default voltage=2.8v,
  222. but in MT6323 vibr default voltage=1.2v */
  223. vibr_power_set();
  224. ret = platform_device_register(&vibrator_device);
  225. if (ret != 0) {
  226. VIB_DEBUG("Unable to register vibrator device (%d)\n", ret);
  227. return ret;
  228. }
  229. vibrator_queue = create_singlethread_workqueue(VIB_DEVICE);
  230. if (!vibrator_queue) {
  231. VIB_DEBUG("Unable to create workqueue\n");
  232. return -ENODATA;
  233. }
  234. INIT_WORK(&vibrator_work, update_vibrator);
  235. spin_lock_init(&vibe_lock);
  236. shutdown_flag = 0;
  237. vibe_state = 0;
  238. hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  239. vibe_timer.function = vibrator_timer_func;
  240. timed_output_dev_register(&mtk_vibrator);
  241. ret = platform_driver_register(&vibrator_driver);
  242. if (ret) {
  243. VIB_DEBUG("Unable to register vibrator driver (%d)\n", ret);
  244. return ret;
  245. }
  246. ret = device_create_file(mtk_vibrator.dev, &dev_attr_vibr_on);
  247. if (ret)
  248. VIB_DEBUG("device_create_file vibr_on fail!\n");
  249. VIB_DEBUG("vib_mod_init Done\n");
  250. return RSUCCESS;
  251. }
  252. /******************************************************************************
  253. * vib_mod_exit
  254. *
  255. * DESCRIPTION:
  256. * Free the device driver !
  257. *
  258. * PARAMETERS:
  259. * None
  260. *
  261. * RETURNS:
  262. * None
  263. *
  264. * NOTES:
  265. * None
  266. *
  267. ******************************************************************************/
  268. static void vib_mod_exit(void)
  269. {
  270. VIB_DEBUG("MediaTek MTK vibrator driver unregister, version %s\n",
  271. VERSION);
  272. if (vibrator_queue)
  273. destroy_workqueue(vibrator_queue);
  274. VIB_DEBUG("vib_mod_exit Done\n");
  275. }
  276. module_init(vib_mod_init);
  277. module_exit(vib_mod_exit);
  278. MODULE_AUTHOR("MediaTek Inc.");
  279. MODULE_DESCRIPTION("MTK Vibrator Driver (VIB)");
  280. MODULE_LICENSE("GPL");