pm8xxx-vibrator.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Copyright (c) 2010-2011, 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/module.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/mfd/pm8xxx/core.h>
  20. #include <linux/mfd/pm8xxx/vibrator.h>
  21. #include "../staging/android/timed_output.h"
  22. #define VIB_DRV 0x4A
  23. #define VIB_DRV_SEL_MASK 0xf8
  24. #define VIB_DRV_SEL_SHIFT 0x03
  25. #define VIB_DRV_EN_MANUAL_MASK 0xfc
  26. #define VIB_DRV_LOGIC_SHIFT 0x2
  27. #define VIB_MAX_LEVEL_mV 3100
  28. #define VIB_MIN_LEVEL_mV 1200
  29. struct pm8xxx_vib {
  30. struct hrtimer vib_timer;
  31. struct timed_output_dev timed_dev;
  32. spinlock_t lock;
  33. struct work_struct work;
  34. struct device *dev;
  35. const struct pm8xxx_vibrator_platform_data *pdata;
  36. int state;
  37. int level;
  38. u8 reg_vib_drv;
  39. };
  40. static struct pm8xxx_vib *vib_dev;
  41. int pm8xxx_vibrator_config(struct pm8xxx_vib_config *vib_config)
  42. {
  43. u8 reg = 0;
  44. int rc;
  45. if (vib_dev == NULL) {
  46. pr_err("%s: vib_dev is NULL\n", __func__);
  47. return -EINVAL;
  48. }
  49. if (vib_config->drive_mV) {
  50. if ((vib_config->drive_mV < VIB_MIN_LEVEL_mV) ||
  51. (vib_config->drive_mV > VIB_MAX_LEVEL_mV)) {
  52. pr_err("Invalid vibrator drive strength\n");
  53. return -EINVAL;
  54. }
  55. }
  56. reg = (vib_config->drive_mV / 100) << VIB_DRV_SEL_SHIFT;
  57. reg |= (!!vib_config->active_low) << VIB_DRV_LOGIC_SHIFT;
  58. reg |= vib_config->enable_mode;
  59. rc = pm8xxx_writeb(vib_dev->dev->parent, VIB_DRV, reg);
  60. if (rc)
  61. pr_err("%s: pm8xxx write failed: rc=%d\n", __func__, rc);
  62. return rc;
  63. }
  64. EXPORT_SYMBOL(pm8xxx_vibrator_config);
  65. /* REVISIT: just for debugging, will be removed in final working version */
  66. static void __dump_vib_regs(struct pm8xxx_vib *vib, char *msg)
  67. {
  68. u8 temp;
  69. dev_dbg(vib->dev, "%s\n", msg);
  70. pm8xxx_readb(vib->dev->parent, VIB_DRV, &temp);
  71. dev_dbg(vib->dev, "VIB_DRV - %X\n", temp);
  72. }
  73. static int pm8xxx_vib_read_u8(struct pm8xxx_vib *vib,
  74. u8 *data, u16 reg)
  75. {
  76. int rc;
  77. rc = pm8xxx_readb(vib->dev->parent, reg, data);
  78. if (rc < 0)
  79. dev_warn(vib->dev, "Error reading pm8xxx: %X - ret %X\n",
  80. reg, rc);
  81. return rc;
  82. }
  83. static int pm8xxx_vib_write_u8(struct pm8xxx_vib *vib,
  84. u8 data, u16 reg)
  85. {
  86. int rc;
  87. rc = pm8xxx_writeb(vib->dev->parent, reg, data);
  88. if (rc < 0)
  89. dev_warn(vib->dev, "Error writing pm8xxx: %X - ret %X\n",
  90. reg, rc);
  91. return rc;
  92. }
  93. static int pm8xxx_vib_set(struct pm8xxx_vib *vib, int on)
  94. {
  95. int rc;
  96. u8 val;
  97. if (on) {
  98. val = vib->reg_vib_drv;
  99. val |= ((vib->level << VIB_DRV_SEL_SHIFT) & VIB_DRV_SEL_MASK);
  100. rc = pm8xxx_vib_write_u8(vib, val, VIB_DRV);
  101. if (rc < 0)
  102. return rc;
  103. vib->reg_vib_drv = val;
  104. } else {
  105. val = vib->reg_vib_drv;
  106. val &= ~VIB_DRV_SEL_MASK;
  107. rc = pm8xxx_vib_write_u8(vib, val, VIB_DRV);
  108. if (rc < 0)
  109. return rc;
  110. vib->reg_vib_drv = val;
  111. }
  112. __dump_vib_regs(vib, "vib_set_end");
  113. return rc;
  114. }
  115. static void pm8xxx_vib_enable(struct timed_output_dev *dev, int value)
  116. {
  117. struct pm8xxx_vib *vib = container_of(dev, struct pm8xxx_vib,
  118. timed_dev);
  119. unsigned long flags;
  120. retry:
  121. spin_lock_irqsave(&vib->lock, flags);
  122. if (hrtimer_try_to_cancel(&vib->vib_timer) < 0) {
  123. spin_unlock_irqrestore(&vib->lock, flags);
  124. cpu_relax();
  125. goto retry;
  126. }
  127. if (value == 0)
  128. vib->state = 0;
  129. else {
  130. value = (value > vib->pdata->max_timeout_ms ?
  131. vib->pdata->max_timeout_ms : value);
  132. vib->state = 1;
  133. hrtimer_start(&vib->vib_timer,
  134. ktime_set(value / 1000, (value % 1000) * 1000000),
  135. HRTIMER_MODE_REL);
  136. }
  137. spin_unlock_irqrestore(&vib->lock, flags);
  138. schedule_work(&vib->work);
  139. }
  140. static void pm8xxx_vib_update(struct work_struct *work)
  141. {
  142. struct pm8xxx_vib *vib = container_of(work, struct pm8xxx_vib,
  143. work);
  144. pm8xxx_vib_set(vib, vib->state);
  145. }
  146. static int pm8xxx_vib_get_time(struct timed_output_dev *dev)
  147. {
  148. struct pm8xxx_vib *vib = container_of(dev, struct pm8xxx_vib,
  149. timed_dev);
  150. if (hrtimer_active(&vib->vib_timer)) {
  151. ktime_t r = hrtimer_get_remaining(&vib->vib_timer);
  152. return (int)ktime_to_us(r);
  153. } else
  154. return 0;
  155. }
  156. static enum hrtimer_restart pm8xxx_vib_timer_func(struct hrtimer *timer)
  157. {
  158. struct pm8xxx_vib *vib = container_of(timer, struct pm8xxx_vib,
  159. vib_timer);
  160. vib->state = 0;
  161. schedule_work(&vib->work);
  162. return HRTIMER_NORESTART;
  163. }
  164. #ifdef CONFIG_PM
  165. static int pm8xxx_vib_suspend(struct device *dev)
  166. {
  167. struct pm8xxx_vib *vib = dev_get_drvdata(dev);
  168. hrtimer_cancel(&vib->vib_timer);
  169. cancel_work_sync(&vib->work);
  170. /* turn-off vibrator */
  171. pm8xxx_vib_set(vib, 0);
  172. return 0;
  173. }
  174. static const struct dev_pm_ops pm8xxx_vib_pm_ops = {
  175. .suspend = pm8xxx_vib_suspend,
  176. };
  177. #endif
  178. static int __devinit pm8xxx_vib_probe(struct platform_device *pdev)
  179. {
  180. const struct pm8xxx_vibrator_platform_data *pdata =
  181. pdev->dev.platform_data;
  182. struct pm8xxx_vib *vib;
  183. u8 val;
  184. int rc;
  185. if (!pdata)
  186. return -EINVAL;
  187. if (pdata->level_mV < VIB_MIN_LEVEL_mV ||
  188. pdata->level_mV > VIB_MAX_LEVEL_mV)
  189. return -EINVAL;
  190. vib = kzalloc(sizeof(*vib), GFP_KERNEL);
  191. if (!vib)
  192. return -ENOMEM;
  193. vib->pdata = pdata;
  194. vib->level = pdata->level_mV / 100;
  195. vib->dev = &pdev->dev;
  196. spin_lock_init(&vib->lock);
  197. INIT_WORK(&vib->work, pm8xxx_vib_update);
  198. hrtimer_init(&vib->vib_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  199. vib->vib_timer.function = pm8xxx_vib_timer_func;
  200. vib->timed_dev.name = "vibrator";
  201. vib->timed_dev.get_time = pm8xxx_vib_get_time;
  202. vib->timed_dev.enable = pm8xxx_vib_enable;
  203. __dump_vib_regs(vib, "boot_vib_default");
  204. /*
  205. * Configure the vibrator, it operates in manual mode
  206. * for timed_output framework.
  207. */
  208. rc = pm8xxx_vib_read_u8(vib, &val, VIB_DRV);
  209. if (rc < 0)
  210. goto err_read_vib;
  211. val &= ~VIB_DRV_EN_MANUAL_MASK;
  212. rc = pm8xxx_vib_write_u8(vib, val, VIB_DRV);
  213. if (rc < 0)
  214. goto err_read_vib;
  215. vib->reg_vib_drv = val;
  216. rc = timed_output_dev_register(&vib->timed_dev);
  217. if (rc < 0)
  218. goto err_read_vib;
  219. pm8xxx_vib_enable(&vib->timed_dev, pdata->initial_vibrate_ms);
  220. platform_set_drvdata(pdev, vib);
  221. vib_dev = vib;
  222. return 0;
  223. err_read_vib:
  224. kfree(vib);
  225. return rc;
  226. }
  227. static int __devexit pm8xxx_vib_remove(struct platform_device *pdev)
  228. {
  229. struct pm8xxx_vib *vib = platform_get_drvdata(pdev);
  230. cancel_work_sync(&vib->work);
  231. hrtimer_cancel(&vib->vib_timer);
  232. timed_output_dev_unregister(&vib->timed_dev);
  233. platform_set_drvdata(pdev, NULL);
  234. kfree(vib);
  235. return 0;
  236. }
  237. static struct platform_driver pm8xxx_vib_driver = {
  238. .probe = pm8xxx_vib_probe,
  239. .remove = __devexit_p(pm8xxx_vib_remove),
  240. .driver = {
  241. .name = PM8XXX_VIBRATOR_DEV_NAME,
  242. .owner = THIS_MODULE,
  243. #ifdef CONFIG_PM
  244. .pm = &pm8xxx_vib_pm_ops,
  245. #endif
  246. },
  247. };
  248. static int __init pm8xxx_vib_init(void)
  249. {
  250. return platform_driver_register(&pm8xxx_vib_driver);
  251. }
  252. module_init(pm8xxx_vib_init);
  253. static void __exit pm8xxx_vib_exit(void)
  254. {
  255. platform_driver_unregister(&pm8xxx_vib_driver);
  256. }
  257. module_exit(pm8xxx_vib_exit);
  258. MODULE_ALIAS("platform:" PM8XXX_VIBRATOR_DEV_NAME);
  259. MODULE_DESCRIPTION("pm8xxx vibrator driver");
  260. MODULE_LICENSE("GPL v2");