pwm-vibra.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <dmitry.torokhov@gmail.com>
  9. *
  10. * Based on PWM beeper driver:
  11. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  12. */
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/input.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/property.h>
  20. #include <linux/pwm.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/slab.h>
  23. struct pwm_vibrator {
  24. struct input_dev *input;
  25. struct gpio_desc *enable_gpio;
  26. struct pwm_device *pwm;
  27. struct pwm_device *pwm_dir;
  28. struct regulator *vcc;
  29. struct work_struct play_work;
  30. u16 level;
  31. u32 direction_duty_cycle;
  32. bool vcc_on;
  33. };
  34. static int pwm_vibrator_start(struct pwm_vibrator *vibrator)
  35. {
  36. struct device *pdev = vibrator->input->dev.parent;
  37. struct pwm_state state;
  38. int err;
  39. if (!vibrator->vcc_on) {
  40. err = regulator_enable(vibrator->vcc);
  41. if (err) {
  42. dev_err(pdev, "failed to enable regulator: %d\n", err);
  43. return err;
  44. }
  45. vibrator->vcc_on = true;
  46. }
  47. gpiod_set_value_cansleep(vibrator->enable_gpio, 1);
  48. pwm_get_state(vibrator->pwm, &state);
  49. pwm_set_relative_duty_cycle(&state, vibrator->level, 0xffff);
  50. state.enabled = true;
  51. err = pwm_apply_might_sleep(vibrator->pwm, &state);
  52. if (err) {
  53. dev_err(pdev, "failed to apply pwm state: %d\n", err);
  54. return err;
  55. }
  56. if (vibrator->pwm_dir) {
  57. pwm_get_state(vibrator->pwm_dir, &state);
  58. state.duty_cycle = vibrator->direction_duty_cycle;
  59. state.enabled = true;
  60. err = pwm_apply_might_sleep(vibrator->pwm_dir, &state);
  61. if (err) {
  62. dev_err(pdev, "failed to apply dir-pwm state: %d\n", err);
  63. pwm_disable(vibrator->pwm);
  64. return err;
  65. }
  66. }
  67. return 0;
  68. }
  69. static void pwm_vibrator_stop(struct pwm_vibrator *vibrator)
  70. {
  71. if (vibrator->pwm_dir)
  72. pwm_disable(vibrator->pwm_dir);
  73. pwm_disable(vibrator->pwm);
  74. gpiod_set_value_cansleep(vibrator->enable_gpio, 0);
  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. if (IS_ERR(vibrator->vcc))
  118. return dev_err_probe(&pdev->dev, PTR_ERR(vibrator->vcc),
  119. "Failed to request regulator\n");
  120. vibrator->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
  121. GPIOD_OUT_LOW);
  122. if (IS_ERR(vibrator->enable_gpio))
  123. return dev_err_probe(&pdev->dev, PTR_ERR(vibrator->enable_gpio),
  124. "Failed to request enable gpio\n");
  125. vibrator->pwm = devm_pwm_get(&pdev->dev, "enable");
  126. if (IS_ERR(vibrator->pwm))
  127. return dev_err_probe(&pdev->dev, PTR_ERR(vibrator->pwm),
  128. "Failed to request main pwm\n");
  129. INIT_WORK(&vibrator->play_work, pwm_vibrator_play_work);
  130. /* Sync up PWM state and ensure it is off. */
  131. pwm_init_state(vibrator->pwm, &state);
  132. state.enabled = false;
  133. err = pwm_apply_might_sleep(vibrator->pwm, &state);
  134. if (err) {
  135. dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n",
  136. err);
  137. return err;
  138. }
  139. vibrator->pwm_dir = devm_pwm_get(&pdev->dev, "direction");
  140. err = PTR_ERR_OR_ZERO(vibrator->pwm_dir);
  141. switch (err) {
  142. case 0:
  143. /* Sync up PWM state and ensure it is off. */
  144. pwm_init_state(vibrator->pwm_dir, &state);
  145. state.enabled = false;
  146. err = pwm_apply_might_sleep(vibrator->pwm_dir, &state);
  147. if (err) {
  148. dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n",
  149. err);
  150. return err;
  151. }
  152. vibrator->direction_duty_cycle =
  153. pwm_get_period(vibrator->pwm_dir) / 2;
  154. device_property_read_u32(&pdev->dev, "direction-duty-cycle-ns",
  155. &vibrator->direction_duty_cycle);
  156. break;
  157. case -ENODATA:
  158. /* Direction PWM is optional */
  159. vibrator->pwm_dir = NULL;
  160. break;
  161. default:
  162. dev_err(&pdev->dev, "Failed to request direction pwm: %d\n", err);
  163. fallthrough;
  164. case -EPROBE_DEFER:
  165. return err;
  166. }
  167. vibrator->input->name = "pwm-vibrator";
  168. vibrator->input->id.bustype = BUS_HOST;
  169. vibrator->input->dev.parent = &pdev->dev;
  170. vibrator->input->close = pwm_vibrator_close;
  171. input_set_drvdata(vibrator->input, vibrator);
  172. input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
  173. err = input_ff_create_memless(vibrator->input, NULL,
  174. pwm_vibrator_play_effect);
  175. if (err) {
  176. dev_err(&pdev->dev, "Couldn't create FF dev: %d\n", err);
  177. return err;
  178. }
  179. err = input_register_device(vibrator->input);
  180. if (err) {
  181. dev_err(&pdev->dev, "Couldn't register input dev: %d\n", err);
  182. return err;
  183. }
  184. platform_set_drvdata(pdev, vibrator);
  185. return 0;
  186. }
  187. static int pwm_vibrator_suspend(struct device *dev)
  188. {
  189. struct pwm_vibrator *vibrator = dev_get_drvdata(dev);
  190. cancel_work_sync(&vibrator->play_work);
  191. if (vibrator->level)
  192. pwm_vibrator_stop(vibrator);
  193. return 0;
  194. }
  195. static int pwm_vibrator_resume(struct device *dev)
  196. {
  197. struct pwm_vibrator *vibrator = dev_get_drvdata(dev);
  198. if (vibrator->level)
  199. pwm_vibrator_start(vibrator);
  200. return 0;
  201. }
  202. static DEFINE_SIMPLE_DEV_PM_OPS(pwm_vibrator_pm_ops,
  203. pwm_vibrator_suspend, pwm_vibrator_resume);
  204. #ifdef CONFIG_OF
  205. static const struct of_device_id pwm_vibra_dt_match_table[] = {
  206. { .compatible = "pwm-vibrator" },
  207. {},
  208. };
  209. MODULE_DEVICE_TABLE(of, pwm_vibra_dt_match_table);
  210. #endif
  211. static struct platform_driver pwm_vibrator_driver = {
  212. .probe = pwm_vibrator_probe,
  213. .driver = {
  214. .name = "pwm-vibrator",
  215. .pm = pm_sleep_ptr(&pwm_vibrator_pm_ops),
  216. .of_match_table = of_match_ptr(pwm_vibra_dt_match_table),
  217. },
  218. };
  219. module_platform_driver(pwm_vibrator_driver);
  220. MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
  221. MODULE_DESCRIPTION("PWM vibrator driver");
  222. MODULE_LICENSE("GPL");
  223. MODULE_ALIAS("platform:pwm-vibrator");