pwm-vibra.c 6.4 KB

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