regulator-haptic.c 6.5 KB

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