pwm-vibra.c 6.5 KB

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