pm8xxx-vibrator.c 5.6 KB

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