regulator-haptic.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Regulator haptic driver
  4. *
  5. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  6. * Author: Jaewon Kim <[email protected]>
  7. * Author: Hyunhee Kim <[email protected]>
  8. */
  9. #include <linux/input.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_data/regulator-haptic.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/slab.h>
  16. #define MAX_MAGNITUDE_SHIFT 16
  17. struct regulator_haptic {
  18. struct device *dev;
  19. struct input_dev *input_dev;
  20. struct regulator *regulator;
  21. struct work_struct work;
  22. struct mutex mutex;
  23. bool active;
  24. bool suspended;
  25. unsigned int max_volt;
  26. unsigned int min_volt;
  27. unsigned int magnitude;
  28. };
  29. static int regulator_haptic_toggle(struct regulator_haptic *haptic, bool on)
  30. {
  31. int error;
  32. if (haptic->active != on) {
  33. error = on ? regulator_enable(haptic->regulator) :
  34. regulator_disable(haptic->regulator);
  35. if (error) {
  36. dev_err(haptic->dev,
  37. "failed to switch regulator %s: %d\n",
  38. on ? "on" : "off", error);
  39. return error;
  40. }
  41. haptic->active = on;
  42. }
  43. return 0;
  44. }
  45. static int regulator_haptic_set_voltage(struct regulator_haptic *haptic,
  46. unsigned int magnitude)
  47. {
  48. u64 volt_mag_multi;
  49. unsigned int intensity;
  50. int error;
  51. volt_mag_multi = (u64)(haptic->max_volt - haptic->min_volt) * magnitude;
  52. intensity = (unsigned int)(volt_mag_multi >> MAX_MAGNITUDE_SHIFT);
  53. error = regulator_set_voltage(haptic->regulator,
  54. intensity + haptic->min_volt,
  55. haptic->max_volt);
  56. if (error) {
  57. dev_err(haptic->dev, "cannot set regulator voltage to %d: %d\n",
  58. intensity + haptic->min_volt, error);
  59. return error;
  60. }
  61. regulator_haptic_toggle(haptic, !!magnitude);
  62. return 0;
  63. }
  64. static void regulator_haptic_work(struct work_struct *work)
  65. {
  66. struct regulator_haptic *haptic = container_of(work,
  67. struct regulator_haptic, work);
  68. mutex_lock(&haptic->mutex);
  69. if (!haptic->suspended)
  70. regulator_haptic_set_voltage(haptic, haptic->magnitude);
  71. mutex_unlock(&haptic->mutex);
  72. }
  73. static int regulator_haptic_play_effect(struct input_dev *input, void *data,
  74. struct ff_effect *effect)
  75. {
  76. struct regulator_haptic *haptic = input_get_drvdata(input);
  77. haptic->magnitude = effect->u.rumble.strong_magnitude;
  78. if (!haptic->magnitude)
  79. haptic->magnitude = effect->u.rumble.weak_magnitude;
  80. schedule_work(&haptic->work);
  81. return 0;
  82. }
  83. static void regulator_haptic_close(struct input_dev *input)
  84. {
  85. struct regulator_haptic *haptic = input_get_drvdata(input);
  86. cancel_work_sync(&haptic->work);
  87. regulator_haptic_set_voltage(haptic, 0);
  88. }
  89. static int __maybe_unused
  90. regulator_haptic_parse_dt(struct device *dev, struct regulator_haptic *haptic)
  91. {
  92. struct device_node *node;
  93. int error;
  94. node = dev->of_node;
  95. if(!node) {
  96. dev_err(dev, "Missing device tree data\n");
  97. return -EINVAL;
  98. }
  99. error = of_property_read_u32(node, "max-microvolt", &haptic->max_volt);
  100. if (error) {
  101. dev_err(dev, "cannot parse max-microvolt\n");
  102. return error;
  103. }
  104. error = of_property_read_u32(node, "min-microvolt", &haptic->min_volt);
  105. if (error) {
  106. dev_err(dev, "cannot parse min-microvolt\n");
  107. return error;
  108. }
  109. return 0;
  110. }
  111. static int regulator_haptic_probe(struct platform_device *pdev)
  112. {
  113. const struct regulator_haptic_data *pdata = dev_get_platdata(&pdev->dev);
  114. struct regulator_haptic *haptic;
  115. struct input_dev *input_dev;
  116. int error;
  117. haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL);
  118. if (!haptic)
  119. return -ENOMEM;
  120. platform_set_drvdata(pdev, haptic);
  121. haptic->dev = &pdev->dev;
  122. mutex_init(&haptic->mutex);
  123. INIT_WORK(&haptic->work, regulator_haptic_work);
  124. if (pdata) {
  125. haptic->max_volt = pdata->max_volt;
  126. haptic->min_volt = pdata->min_volt;
  127. } else if (IS_ENABLED(CONFIG_OF)) {
  128. error = regulator_haptic_parse_dt(&pdev->dev, haptic);
  129. if (error)
  130. return error;
  131. } else {
  132. dev_err(&pdev->dev, "Missing platform data\n");
  133. return -EINVAL;
  134. }
  135. haptic->regulator = devm_regulator_get_exclusive(&pdev->dev, "haptic");
  136. if (IS_ERR(haptic->regulator)) {
  137. dev_err(&pdev->dev, "failed to get regulator\n");
  138. return PTR_ERR(haptic->regulator);
  139. }
  140. input_dev = devm_input_allocate_device(&pdev->dev);
  141. if (!input_dev)
  142. return -ENOMEM;
  143. haptic->input_dev = input_dev;
  144. haptic->input_dev->name = "regulator-haptic";
  145. haptic->input_dev->dev.parent = &pdev->dev;
  146. haptic->input_dev->close = regulator_haptic_close;
  147. input_set_drvdata(haptic->input_dev, haptic);
  148. input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE);
  149. error = input_ff_create_memless(input_dev, NULL,
  150. regulator_haptic_play_effect);
  151. if (error) {
  152. dev_err(&pdev->dev, "failed to create force-feedback\n");
  153. return error;
  154. }
  155. error = input_register_device(haptic->input_dev);
  156. if (error) {
  157. dev_err(&pdev->dev, "failed to register input device\n");
  158. return error;
  159. }
  160. return 0;
  161. }
  162. static int __maybe_unused regulator_haptic_suspend(struct device *dev)
  163. {
  164. struct platform_device *pdev = to_platform_device(dev);
  165. struct regulator_haptic *haptic = platform_get_drvdata(pdev);
  166. int error;
  167. error = mutex_lock_interruptible(&haptic->mutex);
  168. if (error)
  169. return error;
  170. regulator_haptic_set_voltage(haptic, 0);
  171. haptic->suspended = true;
  172. mutex_unlock(&haptic->mutex);
  173. return 0;
  174. }
  175. static int __maybe_unused regulator_haptic_resume(struct device *dev)
  176. {
  177. struct platform_device *pdev = to_platform_device(dev);
  178. struct regulator_haptic *haptic = platform_get_drvdata(pdev);
  179. unsigned int magnitude;
  180. mutex_lock(&haptic->mutex);
  181. haptic->suspended = false;
  182. magnitude = READ_ONCE(haptic->magnitude);
  183. if (magnitude)
  184. regulator_haptic_set_voltage(haptic, magnitude);
  185. mutex_unlock(&haptic->mutex);
  186. return 0;
  187. }
  188. static SIMPLE_DEV_PM_OPS(regulator_haptic_pm_ops,
  189. regulator_haptic_suspend, regulator_haptic_resume);
  190. static const struct of_device_id regulator_haptic_dt_match[] = {
  191. { .compatible = "regulator-haptic" },
  192. { /* sentinel */ },
  193. };
  194. MODULE_DEVICE_TABLE(of, regulator_haptic_dt_match);
  195. static struct platform_driver regulator_haptic_driver = {
  196. .probe = regulator_haptic_probe,
  197. .driver = {
  198. .name = "regulator-haptic",
  199. .of_match_table = regulator_haptic_dt_match,
  200. .pm = &regulator_haptic_pm_ops,
  201. },
  202. };
  203. module_platform_driver(regulator_haptic_driver);
  204. MODULE_AUTHOR("Jaewon Kim <[email protected]>");
  205. MODULE_AUTHOR("Hyunhee Kim <[email protected]>");
  206. MODULE_DESCRIPTION("Regulator haptic driver");
  207. MODULE_LICENSE("GPL");