pm8xxx-vibrator.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* Copyright (c) 2010-2011, Code Aurora Forum. 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/platform_device.h>
  17. #include <linux/input.h>
  18. #include <linux/slab.h>
  19. #include <linux/mfd/pm8xxx/core.h>
  20. #define VIB_DRV 0x4A
  21. #define VIB_DRV_SEL_MASK 0xf8
  22. #define VIB_DRV_SEL_SHIFT 0x03
  23. #define VIB_DRV_EN_MANUAL_MASK 0xfc
  24. #define VIB_MAX_LEVEL_mV (3100)
  25. #define VIB_MIN_LEVEL_mV (1200)
  26. #define VIB_MAX_LEVELS (VIB_MAX_LEVEL_mV - VIB_MIN_LEVEL_mV)
  27. #define MAX_FF_SPEED 0xff
  28. /**
  29. * struct pm8xxx_vib - structure to hold vibrator data
  30. * @vib_input_dev: input device supporting force feedback
  31. * @work: work structure to set the vibration parameters
  32. * @dev: device supporting force feedback
  33. * @speed: speed of vibration set from userland
  34. * @active: state of vibrator
  35. * @level: level of vibration to set in the chip
  36. * @reg_vib_drv: VIB_DRV register value
  37. */
  38. struct pm8xxx_vib {
  39. struct input_dev *vib_input_dev;
  40. struct work_struct work;
  41. struct device *dev;
  42. int speed;
  43. int level;
  44. bool active;
  45. u8 reg_vib_drv;
  46. };
  47. /**
  48. * pm8xxx_vib_read_u8 - helper to read a byte from pmic chip
  49. * @vib: pointer to vibrator structure
  50. * @data: placeholder for data to be read
  51. * @reg: register address
  52. */
  53. static int pm8xxx_vib_read_u8(struct pm8xxx_vib *vib,
  54. u8 *data, u16 reg)
  55. {
  56. int rc;
  57. rc = pm8xxx_readb(vib->dev->parent, reg, data);
  58. if (rc < 0)
  59. dev_warn(vib->dev, "Error reading pm8xxx reg 0x%x(0x%x)\n",
  60. reg, rc);
  61. return rc;
  62. }
  63. /**
  64. * pm8xxx_vib_write_u8 - helper to write a byte to pmic chip
  65. * @vib: pointer to vibrator structure
  66. * @data: data to write
  67. * @reg: register address
  68. */
  69. static int pm8xxx_vib_write_u8(struct pm8xxx_vib *vib,
  70. u8 data, u16 reg)
  71. {
  72. int rc;
  73. rc = pm8xxx_writeb(vib->dev->parent, reg, data);
  74. if (rc < 0)
  75. dev_warn(vib->dev, "Error writing pm8xxx reg 0x%x(0x%x)\n",
  76. reg, rc);
  77. return rc;
  78. }
  79. /**
  80. * pm8xxx_vib_set - handler to start/stop vibration
  81. * @vib: pointer to vibrator structure
  82. * @on: state to set
  83. */
  84. static int pm8xxx_vib_set(struct pm8xxx_vib *vib, bool on)
  85. {
  86. int rc;
  87. u8 val = vib->reg_vib_drv;
  88. if (on)
  89. val |= ((vib->level << VIB_DRV_SEL_SHIFT) & VIB_DRV_SEL_MASK);
  90. else
  91. val &= ~VIB_DRV_SEL_MASK;
  92. rc = pm8xxx_vib_write_u8(vib, val, VIB_DRV);
  93. if (rc < 0)
  94. return rc;
  95. vib->reg_vib_drv = val;
  96. return 0;
  97. }
  98. /**
  99. * pm8xxx_work_handler - worker to set vibration level
  100. * @work: pointer to work_struct
  101. */
  102. static void pm8xxx_work_handler(struct work_struct *work)
  103. {
  104. struct pm8xxx_vib *vib = container_of(work, struct pm8xxx_vib, work);
  105. int rc;
  106. u8 val;
  107. rc = pm8xxx_vib_read_u8(vib, &val, VIB_DRV);
  108. if (rc < 0)
  109. return;
  110. /*
  111. * pmic vibrator supports voltage ranges from 1.2 to 3.1V, so
  112. * scale the level to fit into these ranges.
  113. */
  114. if (vib->speed) {
  115. vib->active = true;
  116. vib->level = ((VIB_MAX_LEVELS * vib->speed) / MAX_FF_SPEED) +
  117. VIB_MIN_LEVEL_mV;
  118. vib->level /= 100;
  119. } else {
  120. vib->active = false;
  121. vib->level = VIB_MIN_LEVEL_mV / 100;
  122. }
  123. pm8xxx_vib_set(vib, vib->active);
  124. }
  125. /**
  126. * pm8xxx_vib_close - callback of input close callback
  127. * @dev: input device pointer
  128. *
  129. * Turns off the vibrator.
  130. */
  131. static void pm8xxx_vib_close(struct input_dev *dev)
  132. {
  133. struct pm8xxx_vib *vib = input_get_drvdata(dev);
  134. cancel_work_sync(&vib->work);
  135. if (vib->active)
  136. pm8xxx_vib_set(vib, false);
  137. }
  138. /**
  139. * pm8xxx_vib_play_effect - function to handle vib effects.
  140. * @dev: input device pointer
  141. * @data: data of effect
  142. * @effect: effect to play
  143. *
  144. * Currently this driver supports only rumble effects.
  145. */
  146. static int pm8xxx_vib_play_effect(struct input_dev *dev, void *data,
  147. struct ff_effect *effect)
  148. {
  149. struct pm8xxx_vib *vib = input_get_drvdata(dev);
  150. vib->speed = effect->u.rumble.strong_magnitude >> 8;
  151. if (!vib->speed)
  152. vib->speed = effect->u.rumble.weak_magnitude >> 9;
  153. schedule_work(&vib->work);
  154. return 0;
  155. }
  156. static int __devinit pm8xxx_vib_probe(struct platform_device *pdev)
  157. {
  158. struct pm8xxx_vib *vib;
  159. struct input_dev *input_dev;
  160. int error;
  161. u8 val;
  162. vib = kzalloc(sizeof(*vib), GFP_KERNEL);
  163. input_dev = input_allocate_device();
  164. if (!vib || !input_dev) {
  165. dev_err(&pdev->dev, "couldn't allocate memory\n");
  166. error = -ENOMEM;
  167. goto err_free_mem;
  168. }
  169. INIT_WORK(&vib->work, pm8xxx_work_handler);
  170. vib->dev = &pdev->dev;
  171. vib->vib_input_dev = input_dev;
  172. /* operate in manual mode */
  173. error = pm8xxx_vib_read_u8(vib, &val, VIB_DRV);
  174. if (error < 0)
  175. goto err_free_mem;
  176. val &= ~VIB_DRV_EN_MANUAL_MASK;
  177. error = pm8xxx_vib_write_u8(vib, val, VIB_DRV);
  178. if (error < 0)
  179. goto err_free_mem;
  180. vib->reg_vib_drv = val;
  181. input_dev->name = "pm8xxx_vib_ffmemless";
  182. input_dev->id.version = 1;
  183. input_dev->dev.parent = &pdev->dev;
  184. input_dev->close = pm8xxx_vib_close;
  185. input_set_drvdata(input_dev, vib);
  186. input_set_capability(vib->vib_input_dev, EV_FF, FF_RUMBLE);
  187. error = input_ff_create_memless(input_dev, NULL,
  188. pm8xxx_vib_play_effect);
  189. if (error) {
  190. dev_err(&pdev->dev,
  191. "couldn't register vibrator as FF device\n");
  192. goto err_free_mem;
  193. }
  194. error = input_register_device(input_dev);
  195. if (error) {
  196. dev_err(&pdev->dev, "couldn't register input device\n");
  197. goto err_destroy_memless;
  198. }
  199. platform_set_drvdata(pdev, vib);
  200. return 0;
  201. err_destroy_memless:
  202. input_ff_destroy(input_dev);
  203. err_free_mem:
  204. input_free_device(input_dev);
  205. kfree(vib);
  206. return error;
  207. }
  208. static int __devexit pm8xxx_vib_remove(struct platform_device *pdev)
  209. {
  210. struct pm8xxx_vib *vib = platform_get_drvdata(pdev);
  211. input_unregister_device(vib->vib_input_dev);
  212. kfree(vib);
  213. platform_set_drvdata(pdev, NULL);
  214. return 0;
  215. }
  216. #ifdef CONFIG_PM_SLEEP
  217. static int pm8xxx_vib_suspend(struct device *dev)
  218. {
  219. struct pm8xxx_vib *vib = dev_get_drvdata(dev);
  220. /* Turn off the vibrator */
  221. pm8xxx_vib_set(vib, false);
  222. return 0;
  223. }
  224. #endif
  225. static SIMPLE_DEV_PM_OPS(pm8xxx_vib_pm_ops, pm8xxx_vib_suspend, NULL);
  226. static struct platform_driver pm8xxx_vib_driver = {
  227. .probe = pm8xxx_vib_probe,
  228. .remove = __devexit_p(pm8xxx_vib_remove),
  229. .driver = {
  230. .name = "pm8xxx-vib",
  231. .owner = THIS_MODULE,
  232. .pm = &pm8xxx_vib_pm_ops,
  233. },
  234. };
  235. static int __init pm8xxx_vib_init(void)
  236. {
  237. return platform_driver_register(&pm8xxx_vib_driver);
  238. }
  239. module_init(pm8xxx_vib_init);
  240. static void __exit pm8xxx_vib_exit(void)
  241. {
  242. platform_driver_unregister(&pm8xxx_vib_driver);
  243. }
  244. module_exit(pm8xxx_vib_exit);
  245. MODULE_ALIAS("platform:pm8xxx_vib");
  246. MODULE_DESCRIPTION("PMIC8xxx vibrator driver based on ff-memless framework");
  247. MODULE_LICENSE("GPL v2");
  248. MODULE_AUTHOR("Amy Maloche <amaloche@codeaurora.org>");