pwm-vibra.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PWM vibrator driver
  4. *
  5. * Copyright (C) 2017 Collabora Ltd.
  6. *
  7. * Based on previous work from:
  8. * Copyright (C) 2012 Dmitry Torokhov <[email protected]>
  9. *
  10. * Based on PWM beeper driver:
  11. * Copyright (C) 2010, Lars-Peter Clausen <[email protected]>
  12. */
  13. #include <linux/input.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/property.h>
  19. #include <linux/pwm.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/slab.h>
  22. struct pwm_vibrator {
  23. struct input_dev *input;
  24. struct pwm_device *pwm;
  25. struct pwm_device *pwm_dir;
  26. struct regulator *vcc;
  27. struct work_struct play_work;
  28. u16 level;
  29. u32 direction_duty_cycle;
  30. bool vcc_on;
  31. };
  32. static int pwm_vibrator_start(struct pwm_vibrator *vibrator)
  33. {
  34. struct device *pdev = vibrator->input->dev.parent;
  35. struct pwm_state state;
  36. int err;
  37. if (!vibrator->vcc_on) {
  38. err = regulator_enable(vibrator->vcc);
  39. if (err) {
  40. dev_err(pdev, "failed to enable regulator: %d", err);
  41. return err;
  42. }
  43. vibrator->vcc_on = true;
  44. }
  45. pwm_get_state(vibrator->pwm, &state);
  46. pwm_set_relative_duty_cycle(&state, vibrator->level, 0xffff);
  47. state.enabled = true;
  48. err = pwm_apply_state(vibrator->pwm, &state);
  49. if (err) {
  50. dev_err(pdev, "failed to apply pwm state: %d", err);
  51. return err;
  52. }
  53. if (vibrator->pwm_dir) {
  54. pwm_get_state(vibrator->pwm_dir, &state);
  55. state.duty_cycle = vibrator->direction_duty_cycle;
  56. state.enabled = true;
  57. err = pwm_apply_state(vibrator->pwm_dir, &state);
  58. if (err) {
  59. dev_err(pdev, "failed to apply dir-pwm state: %d", err);
  60. pwm_disable(vibrator->pwm);
  61. return err;
  62. }
  63. }
  64. return 0;
  65. }
  66. static void pwm_vibrator_stop(struct pwm_vibrator *vibrator)
  67. {
  68. if (vibrator->pwm_dir)
  69. pwm_disable(vibrator->pwm_dir);
  70. pwm_disable(vibrator->pwm);
  71. if (vibrator->vcc_on) {
  72. regulator_disable(vibrator->vcc);
  73. vibrator->vcc_on = false;
  74. }
  75. }
  76. static void pwm_vibrator_play_work(struct work_struct *work)
  77. {
  78. struct pwm_vibrator *vibrator = container_of(work,
  79. struct pwm_vibrator, play_work);
  80. if (vibrator->level)
  81. pwm_vibrator_start(vibrator);
  82. else
  83. pwm_vibrator_stop(vibrator);
  84. }
  85. static int pwm_vibrator_play_effect(struct input_dev *dev, void *data,
  86. struct ff_effect *effect)
  87. {
  88. struct pwm_vibrator *vibrator = input_get_drvdata(dev);
  89. vibrator->level = effect->u.rumble.strong_magnitude;
  90. if (!vibrator->level)
  91. vibrator->level = effect->u.rumble.weak_magnitude;
  92. schedule_work(&vibrator->play_work);
  93. return 0;
  94. }
  95. static void pwm_vibrator_close(struct input_dev *input)
  96. {
  97. struct pwm_vibrator *vibrator = input_get_drvdata(input);
  98. cancel_work_sync(&vibrator->play_work);
  99. pwm_vibrator_stop(vibrator);
  100. }
  101. static int pwm_vibrator_probe(struct platform_device *pdev)
  102. {
  103. struct pwm_vibrator *vibrator;
  104. struct pwm_state state;
  105. int err;
  106. vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL);
  107. if (!vibrator)
  108. return -ENOMEM;
  109. vibrator->input = devm_input_allocate_device(&pdev->dev);
  110. if (!vibrator->input)
  111. return -ENOMEM;
  112. vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
  113. err = PTR_ERR_OR_ZERO(vibrator->vcc);
  114. if (err) {
  115. if (err != -EPROBE_DEFER)
  116. dev_err(&pdev->dev, "Failed to request regulator: %d",
  117. err);
  118. return err;
  119. }
  120. vibrator->pwm = devm_pwm_get(&pdev->dev, "enable");
  121. err = PTR_ERR_OR_ZERO(vibrator->pwm);
  122. if (err) {
  123. if (err != -EPROBE_DEFER)
  124. dev_err(&pdev->dev, "Failed to request main pwm: %d",
  125. err);
  126. return err;
  127. }
  128. INIT_WORK(&vibrator->play_work, pwm_vibrator_play_work);
  129. /* Sync up PWM state and ensure it is off. */
  130. pwm_init_state(vibrator->pwm, &state);
  131. state.enabled = false;
  132. err = pwm_apply_state(vibrator->pwm, &state);
  133. if (err) {
  134. dev_err(&pdev->dev, "failed to apply initial PWM state: %d",
  135. err);
  136. return err;
  137. }
  138. vibrator->pwm_dir = devm_pwm_get(&pdev->dev, "direction");
  139. err = PTR_ERR_OR_ZERO(vibrator->pwm_dir);
  140. switch (err) {
  141. case 0:
  142. /* Sync up PWM state and ensure it is off. */
  143. pwm_init_state(vibrator->pwm_dir, &state);
  144. state.enabled = false;
  145. err = pwm_apply_state(vibrator->pwm_dir, &state);
  146. if (err) {
  147. dev_err(&pdev->dev, "failed to apply initial PWM state: %d",
  148. err);
  149. return err;
  150. }
  151. vibrator->direction_duty_cycle =
  152. pwm_get_period(vibrator->pwm_dir) / 2;
  153. device_property_read_u32(&pdev->dev, "direction-duty-cycle-ns",
  154. &vibrator->direction_duty_cycle);
  155. break;
  156. case -ENODATA:
  157. /* Direction PWM is optional */
  158. vibrator->pwm_dir = NULL;
  159. break;
  160. default:
  161. dev_err(&pdev->dev, "Failed to request direction pwm: %d", err);
  162. fallthrough;
  163. case -EPROBE_DEFER:
  164. return err;
  165. }
  166. vibrator->input->name = "pwm-vibrator";
  167. vibrator->input->id.bustype = BUS_HOST;
  168. vibrator->input->dev.parent = &pdev->dev;
  169. vibrator->input->close = pwm_vibrator_close;
  170. input_set_drvdata(vibrator->input, vibrator);
  171. input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
  172. err = input_ff_create_memless(vibrator->input, NULL,
  173. pwm_vibrator_play_effect);
  174. if (err) {
  175. dev_err(&pdev->dev, "Couldn't create FF dev: %d", err);
  176. return err;
  177. }
  178. err = input_register_device(vibrator->input);
  179. if (err) {
  180. dev_err(&pdev->dev, "Couldn't register input dev: %d", err);
  181. return err;
  182. }
  183. platform_set_drvdata(pdev, vibrator);
  184. return 0;
  185. }
  186. static int __maybe_unused pwm_vibrator_suspend(struct device *dev)
  187. {
  188. struct pwm_vibrator *vibrator = dev_get_drvdata(dev);
  189. cancel_work_sync(&vibrator->play_work);
  190. if (vibrator->level)
  191. pwm_vibrator_stop(vibrator);
  192. return 0;
  193. }
  194. static int __maybe_unused pwm_vibrator_resume(struct device *dev)
  195. {
  196. struct pwm_vibrator *vibrator = dev_get_drvdata(dev);
  197. if (vibrator->level)
  198. pwm_vibrator_start(vibrator);
  199. return 0;
  200. }
  201. static SIMPLE_DEV_PM_OPS(pwm_vibrator_pm_ops,
  202. pwm_vibrator_suspend, pwm_vibrator_resume);
  203. #ifdef CONFIG_OF
  204. static const struct of_device_id pwm_vibra_dt_match_table[] = {
  205. { .compatible = "pwm-vibrator" },
  206. {},
  207. };
  208. MODULE_DEVICE_TABLE(of, pwm_vibra_dt_match_table);
  209. #endif
  210. static struct platform_driver pwm_vibrator_driver = {
  211. .probe = pwm_vibrator_probe,
  212. .driver = {
  213. .name = "pwm-vibrator",
  214. .pm = &pwm_vibrator_pm_ops,
  215. .of_match_table = of_match_ptr(pwm_vibra_dt_match_table),
  216. },
  217. };
  218. module_platform_driver(pwm_vibrator_driver);
  219. MODULE_AUTHOR("Sebastian Reichel <[email protected]>");
  220. MODULE_DESCRIPTION("PWM vibrator driver");
  221. MODULE_LICENSE("GPL");
  222. MODULE_ALIAS("platform:pwm-vibrator");