regulator-haptic.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <jaewon02.kim@samsung.com>
  7. * Author: Hyunhee Kim <hyunhee.kim@samsung.com>
  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. #include <linux/string_choices.h>
  17. #define MAX_MAGNITUDE_SHIFT 16
  18. struct regulator_haptic {
  19. struct device *dev;
  20. struct input_dev *input_dev;
  21. struct regulator *regulator;
  22. struct work_struct work;
  23. struct mutex mutex;
  24. bool active;
  25. bool suspended;
  26. unsigned int max_volt;
  27. unsigned int min_volt;
  28. unsigned int magnitude;
  29. };
  30. static int regulator_haptic_toggle(struct regulator_haptic *haptic, bool on)
  31. {
  32. int error;
  33. if (haptic->active != on) {
  34. error = on ? regulator_enable(haptic->regulator) :
  35. regulator_disable(haptic->regulator);
  36. if (error) {
  37. dev_err(haptic->dev,
  38. "failed to switch regulator %s: %d\n",
  39. str_on_off(on), error);
  40. return error;
  41. }
  42. haptic->active = on;
  43. }
  44. return 0;
  45. }
  46. static int regulator_haptic_set_voltage(struct regulator_haptic *haptic,
  47. unsigned int magnitude)
  48. {
  49. u64 volt_mag_multi;
  50. unsigned int intensity;
  51. int error;
  52. volt_mag_multi = (u64)(haptic->max_volt - haptic->min_volt) * magnitude;
  53. intensity = (unsigned int)(volt_mag_multi >> MAX_MAGNITUDE_SHIFT);
  54. error = regulator_set_voltage(haptic->regulator,
  55. intensity + haptic->min_volt,
  56. haptic->max_volt);
  57. if (error) {
  58. dev_err(haptic->dev, "cannot set regulator voltage to %d: %d\n",
  59. intensity + haptic->min_volt, error);
  60. return error;
  61. }
  62. regulator_haptic_toggle(haptic, !!magnitude);
  63. return 0;
  64. }
  65. static void regulator_haptic_work(struct work_struct *work)
  66. {
  67. struct regulator_haptic *haptic = container_of(work,
  68. struct regulator_haptic, work);
  69. guard(mutex)(&haptic->mutex);
  70. if (!haptic->suspended)
  71. regulator_haptic_set_voltage(haptic, haptic->magnitude);
  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 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. scoped_guard(mutex_intr, &haptic->mutex) {
  167. regulator_haptic_set_voltage(haptic, 0);
  168. haptic->suspended = true;
  169. return 0;
  170. }
  171. return -EINTR;
  172. }
  173. static int regulator_haptic_resume(struct device *dev)
  174. {
  175. struct platform_device *pdev = to_platform_device(dev);
  176. struct regulator_haptic *haptic = platform_get_drvdata(pdev);
  177. unsigned int magnitude;
  178. guard(mutex)(&haptic->mutex);
  179. haptic->suspended = false;
  180. magnitude = READ_ONCE(haptic->magnitude);
  181. if (magnitude)
  182. regulator_haptic_set_voltage(haptic, magnitude);
  183. return 0;
  184. }
  185. static DEFINE_SIMPLE_DEV_PM_OPS(regulator_haptic_pm_ops,
  186. regulator_haptic_suspend, regulator_haptic_resume);
  187. static const struct of_device_id regulator_haptic_dt_match[] = {
  188. { .compatible = "regulator-haptic" },
  189. { /* sentinel */ },
  190. };
  191. MODULE_DEVICE_TABLE(of, regulator_haptic_dt_match);
  192. static struct platform_driver regulator_haptic_driver = {
  193. .probe = regulator_haptic_probe,
  194. .driver = {
  195. .name = "regulator-haptic",
  196. .of_match_table = regulator_haptic_dt_match,
  197. .pm = pm_sleep_ptr(&regulator_haptic_pm_ops),
  198. },
  199. };
  200. module_platform_driver(regulator_haptic_driver);
  201. MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
  202. MODULE_AUTHOR("Hyunhee Kim <hyunhee.kim@samsung.com>");
  203. MODULE_DESCRIPTION("Regulator haptic driver");
  204. MODULE_LICENSE("GPL");