pm8xxx-vibrator.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/errno.h>
  13. #include <linux/input.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/slab.h>
  21. #define VIB_MAX_LEVEL_mV (3100)
  22. #define VIB_MIN_LEVEL_mV (1200)
  23. #define VIB_MAX_LEVELS (VIB_MAX_LEVEL_mV - VIB_MIN_LEVEL_mV)
  24. #define MAX_FF_SPEED 0xff
  25. struct pm8xxx_regs {
  26. unsigned int enable_addr;
  27. unsigned int enable_mask;
  28. unsigned int drv_addr;
  29. unsigned int drv_mask;
  30. unsigned int drv_shift;
  31. unsigned int drv_en_manual_mask;
  32. };
  33. static const struct pm8xxx_regs pm8058_regs = {
  34. .drv_addr = 0x4A,
  35. .drv_mask = 0xf8,
  36. .drv_shift = 3,
  37. .drv_en_manual_mask = 0xfc,
  38. };
  39. static struct pm8xxx_regs pm8916_regs = {
  40. .enable_addr = 0xc046,
  41. .enable_mask = BIT(7),
  42. .drv_addr = 0xc041,
  43. .drv_mask = 0x1F,
  44. .drv_shift = 0,
  45. .drv_en_manual_mask = 0,
  46. };
  47. /**
  48. * struct pm8xxx_vib - structure to hold vibrator data
  49. * @vib_input_dev: input device supporting force feedback
  50. * @work: work structure to set the vibration parameters
  51. * @regmap: regmap for register read/write
  52. * @regs: registers' info
  53. * @speed: speed of vibration set from userland
  54. * @active: state of vibrator
  55. * @level: level of vibration to set in the chip
  56. * @reg_vib_drv: regs->drv_addr register value
  57. */
  58. struct pm8xxx_vib {
  59. struct input_dev *vib_input_dev;
  60. struct work_struct work;
  61. struct regmap *regmap;
  62. const struct pm8xxx_regs *regs;
  63. int speed;
  64. int level;
  65. bool active;
  66. u8 reg_vib_drv;
  67. };
  68. /**
  69. * pm8xxx_vib_set - handler to start/stop vibration
  70. * @vib: pointer to vibrator structure
  71. * @on: state to set
  72. */
  73. static int pm8xxx_vib_set(struct pm8xxx_vib *vib, bool on)
  74. {
  75. int rc;
  76. unsigned int val = vib->reg_vib_drv;
  77. const struct pm8xxx_regs *regs = vib->regs;
  78. if (on)
  79. val |= (vib->level << regs->drv_shift) & regs->drv_mask;
  80. else
  81. val &= ~regs->drv_mask;
  82. rc = regmap_write(vib->regmap, regs->drv_addr, val);
  83. if (rc < 0)
  84. return rc;
  85. vib->reg_vib_drv = val;
  86. if (regs->enable_mask)
  87. rc = regmap_update_bits(vib->regmap, regs->enable_addr,
  88. regs->enable_mask, on ? ~0 : 0);
  89. return rc;
  90. }
  91. /**
  92. * pm8xxx_work_handler - worker to set vibration level
  93. * @work: pointer to work_struct
  94. */
  95. static void pm8xxx_work_handler(struct work_struct *work)
  96. {
  97. struct pm8xxx_vib *vib = container_of(work, struct pm8xxx_vib, work);
  98. const struct pm8xxx_regs *regs = vib->regs;
  99. int rc;
  100. unsigned int val;
  101. rc = regmap_read(vib->regmap, regs->drv_addr, &val);
  102. if (rc < 0)
  103. return;
  104. /*
  105. * pmic vibrator supports voltage ranges from 1.2 to 3.1V, so
  106. * scale the level to fit into these ranges.
  107. */
  108. if (vib->speed) {
  109. vib->active = true;
  110. vib->level = ((VIB_MAX_LEVELS * vib->speed) / MAX_FF_SPEED) +
  111. VIB_MIN_LEVEL_mV;
  112. vib->level /= 100;
  113. } else {
  114. vib->active = false;
  115. vib->level = VIB_MIN_LEVEL_mV / 100;
  116. }
  117. pm8xxx_vib_set(vib, vib->active);
  118. }
  119. /**
  120. * pm8xxx_vib_close - callback of input close callback
  121. * @dev: input device pointer
  122. *
  123. * Turns off the vibrator.
  124. */
  125. static void pm8xxx_vib_close(struct input_dev *dev)
  126. {
  127. struct pm8xxx_vib *vib = input_get_drvdata(dev);
  128. cancel_work_sync(&vib->work);
  129. if (vib->active)
  130. pm8xxx_vib_set(vib, false);
  131. }
  132. /**
  133. * pm8xxx_vib_play_effect - function to handle vib effects.
  134. * @dev: input device pointer
  135. * @data: data of effect
  136. * @effect: effect to play
  137. *
  138. * Currently this driver supports only rumble effects.
  139. */
  140. static int pm8xxx_vib_play_effect(struct input_dev *dev, void *data,
  141. struct ff_effect *effect)
  142. {
  143. struct pm8xxx_vib *vib = input_get_drvdata(dev);
  144. vib->speed = effect->u.rumble.strong_magnitude >> 8;
  145. if (!vib->speed)
  146. vib->speed = effect->u.rumble.weak_magnitude >> 9;
  147. schedule_work(&vib->work);
  148. return 0;
  149. }
  150. static int pm8xxx_vib_probe(struct platform_device *pdev)
  151. {
  152. struct pm8xxx_vib *vib;
  153. struct input_dev *input_dev;
  154. int error;
  155. unsigned int val;
  156. const struct pm8xxx_regs *regs;
  157. vib = devm_kzalloc(&pdev->dev, sizeof(*vib), GFP_KERNEL);
  158. if (!vib)
  159. return -ENOMEM;
  160. vib->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  161. if (!vib->regmap)
  162. return -ENODEV;
  163. input_dev = devm_input_allocate_device(&pdev->dev);
  164. if (!input_dev)
  165. return -ENOMEM;
  166. INIT_WORK(&vib->work, pm8xxx_work_handler);
  167. vib->vib_input_dev = input_dev;
  168. regs = of_device_get_match_data(&pdev->dev);
  169. /* operate in manual mode */
  170. error = regmap_read(vib->regmap, regs->drv_addr, &val);
  171. if (error < 0)
  172. return error;
  173. val &= regs->drv_en_manual_mask;
  174. error = regmap_write(vib->regmap, regs->drv_addr, val);
  175. if (error < 0)
  176. return error;
  177. vib->regs = regs;
  178. vib->reg_vib_drv = val;
  179. input_dev->name = "pm8xxx_vib_ffmemless";
  180. input_dev->id.version = 1;
  181. input_dev->close = pm8xxx_vib_close;
  182. input_set_drvdata(input_dev, vib);
  183. input_set_capability(vib->vib_input_dev, EV_FF, FF_RUMBLE);
  184. error = input_ff_create_memless(input_dev, NULL,
  185. pm8xxx_vib_play_effect);
  186. if (error) {
  187. dev_err(&pdev->dev,
  188. "couldn't register vibrator as FF device\n");
  189. return error;
  190. }
  191. error = input_register_device(input_dev);
  192. if (error) {
  193. dev_err(&pdev->dev, "couldn't register input device\n");
  194. return error;
  195. }
  196. platform_set_drvdata(pdev, vib);
  197. return 0;
  198. }
  199. static int __maybe_unused pm8xxx_vib_suspend(struct device *dev)
  200. {
  201. struct pm8xxx_vib *vib = dev_get_drvdata(dev);
  202. /* Turn off the vibrator */
  203. pm8xxx_vib_set(vib, false);
  204. return 0;
  205. }
  206. static SIMPLE_DEV_PM_OPS(pm8xxx_vib_pm_ops, pm8xxx_vib_suspend, NULL);
  207. static const struct of_device_id pm8xxx_vib_id_table[] = {
  208. { .compatible = "qcom,pm8058-vib", .data = &pm8058_regs },
  209. { .compatible = "qcom,pm8921-vib", .data = &pm8058_regs },
  210. { .compatible = "qcom,pm8916-vib", .data = &pm8916_regs },
  211. { }
  212. };
  213. MODULE_DEVICE_TABLE(of, pm8xxx_vib_id_table);
  214. static struct platform_driver pm8xxx_vib_driver = {
  215. .probe = pm8xxx_vib_probe,
  216. .driver = {
  217. .name = "pm8xxx-vib",
  218. .pm = &pm8xxx_vib_pm_ops,
  219. .of_match_table = pm8xxx_vib_id_table,
  220. },
  221. };
  222. module_platform_driver(pm8xxx_vib_driver);
  223. MODULE_ALIAS("platform:pm8xxx_vib");
  224. MODULE_DESCRIPTION("PMIC8xxx vibrator driver based on ff-memless framework");
  225. MODULE_LICENSE("GPL v2");
  226. MODULE_AUTHOR("Amy Maloche <amaloche@codeaurora.org>");