gpio-vibra.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * GPIO vibrator driver
  4. *
  5. * Copyright (C) 2019 Luca Weiss <[email protected]>
  6. *
  7. * Based on PWM vibrator driver:
  8. * Copyright (C) 2017 Collabora Ltd.
  9. *
  10. * Based on previous work from:
  11. * Copyright (C) 2012 Dmitry Torokhov <[email protected]>
  12. *
  13. * Based on PWM beeper driver:
  14. * Copyright (C) 2010, Lars-Peter Clausen <[email protected]>
  15. */
  16. #include <linux/gpio/consumer.h>
  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/regulator/consumer.h>
  24. #include <linux/slab.h>
  25. struct gpio_vibrator {
  26. struct input_dev *input;
  27. struct gpio_desc *gpio;
  28. struct regulator *vcc;
  29. struct work_struct play_work;
  30. bool running;
  31. bool vcc_on;
  32. };
  33. static int gpio_vibrator_start(struct gpio_vibrator *vibrator)
  34. {
  35. struct device *pdev = vibrator->input->dev.parent;
  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\n", err);
  41. return err;
  42. }
  43. vibrator->vcc_on = true;
  44. }
  45. gpiod_set_value_cansleep(vibrator->gpio, 1);
  46. return 0;
  47. }
  48. static void gpio_vibrator_stop(struct gpio_vibrator *vibrator)
  49. {
  50. gpiod_set_value_cansleep(vibrator->gpio, 0);
  51. if (vibrator->vcc_on) {
  52. regulator_disable(vibrator->vcc);
  53. vibrator->vcc_on = false;
  54. }
  55. }
  56. static void gpio_vibrator_play_work(struct work_struct *work)
  57. {
  58. struct gpio_vibrator *vibrator =
  59. container_of(work, struct gpio_vibrator, play_work);
  60. if (vibrator->running)
  61. gpio_vibrator_start(vibrator);
  62. else
  63. gpio_vibrator_stop(vibrator);
  64. }
  65. static int gpio_vibrator_play_effect(struct input_dev *dev, void *data,
  66. struct ff_effect *effect)
  67. {
  68. struct gpio_vibrator *vibrator = input_get_drvdata(dev);
  69. int level;
  70. level = effect->u.rumble.strong_magnitude;
  71. if (!level)
  72. level = effect->u.rumble.weak_magnitude;
  73. vibrator->running = level;
  74. schedule_work(&vibrator->play_work);
  75. return 0;
  76. }
  77. static void gpio_vibrator_close(struct input_dev *input)
  78. {
  79. struct gpio_vibrator *vibrator = input_get_drvdata(input);
  80. cancel_work_sync(&vibrator->play_work);
  81. gpio_vibrator_stop(vibrator);
  82. vibrator->running = false;
  83. }
  84. static int gpio_vibrator_probe(struct platform_device *pdev)
  85. {
  86. struct gpio_vibrator *vibrator;
  87. int err;
  88. vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL);
  89. if (!vibrator)
  90. return -ENOMEM;
  91. vibrator->input = devm_input_allocate_device(&pdev->dev);
  92. if (!vibrator->input)
  93. return -ENOMEM;
  94. vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
  95. err = PTR_ERR_OR_ZERO(vibrator->vcc);
  96. if (err) {
  97. if (err != -EPROBE_DEFER)
  98. dev_err(&pdev->dev, "Failed to request regulator: %d\n",
  99. err);
  100. return err;
  101. }
  102. vibrator->gpio = devm_gpiod_get(&pdev->dev, "enable", GPIOD_OUT_LOW);
  103. err = PTR_ERR_OR_ZERO(vibrator->gpio);
  104. if (err) {
  105. if (err != -EPROBE_DEFER)
  106. dev_err(&pdev->dev, "Failed to request main gpio: %d\n",
  107. err);
  108. return err;
  109. }
  110. INIT_WORK(&vibrator->play_work, gpio_vibrator_play_work);
  111. vibrator->input->name = "gpio-vibrator";
  112. vibrator->input->id.bustype = BUS_HOST;
  113. vibrator->input->close = gpio_vibrator_close;
  114. input_set_drvdata(vibrator->input, vibrator);
  115. input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
  116. err = input_ff_create_memless(vibrator->input, NULL,
  117. gpio_vibrator_play_effect);
  118. if (err) {
  119. dev_err(&pdev->dev, "Couldn't create FF dev: %d\n", err);
  120. return err;
  121. }
  122. err = input_register_device(vibrator->input);
  123. if (err) {
  124. dev_err(&pdev->dev, "Couldn't register input dev: %d\n", err);
  125. return err;
  126. }
  127. platform_set_drvdata(pdev, vibrator);
  128. return 0;
  129. }
  130. static int __maybe_unused gpio_vibrator_suspend(struct device *dev)
  131. {
  132. struct platform_device *pdev = to_platform_device(dev);
  133. struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
  134. cancel_work_sync(&vibrator->play_work);
  135. if (vibrator->running)
  136. gpio_vibrator_stop(vibrator);
  137. return 0;
  138. }
  139. static int __maybe_unused gpio_vibrator_resume(struct device *dev)
  140. {
  141. struct platform_device *pdev = to_platform_device(dev);
  142. struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
  143. if (vibrator->running)
  144. gpio_vibrator_start(vibrator);
  145. return 0;
  146. }
  147. static SIMPLE_DEV_PM_OPS(gpio_vibrator_pm_ops,
  148. gpio_vibrator_suspend, gpio_vibrator_resume);
  149. #ifdef CONFIG_OF
  150. static const struct of_device_id gpio_vibra_dt_match_table[] = {
  151. { .compatible = "gpio-vibrator" },
  152. {}
  153. };
  154. MODULE_DEVICE_TABLE(of, gpio_vibra_dt_match_table);
  155. #endif
  156. static struct platform_driver gpio_vibrator_driver = {
  157. .probe = gpio_vibrator_probe,
  158. .driver = {
  159. .name = "gpio-vibrator",
  160. .pm = &gpio_vibrator_pm_ops,
  161. .of_match_table = of_match_ptr(gpio_vibra_dt_match_table),
  162. },
  163. };
  164. module_platform_driver(gpio_vibrator_driver);
  165. MODULE_AUTHOR("Luca Weiss <[email protected]>");
  166. MODULE_DESCRIPTION("GPIO vibrator driver");
  167. MODULE_LICENSE("GPL");
  168. MODULE_ALIAS("platform:gpio-vibrator");