gpio-vibra.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * GPIO vibrator driver
  4. *
  5. * Copyright (C) 2019 Luca Weiss <luca@z3ntu.xyz>
  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 <dmitry.torokhov@gmail.com>
  12. *
  13. * Based on PWM beeper driver:
  14. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  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.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. if (IS_ERR(vibrator->vcc))
  96. return dev_err_probe(&pdev->dev, PTR_ERR(vibrator->vcc),
  97. "Failed to request regulator\n");
  98. vibrator->gpio = devm_gpiod_get(&pdev->dev, "enable", GPIOD_OUT_LOW);
  99. if (IS_ERR(vibrator->gpio))
  100. return dev_err_probe(&pdev->dev, PTR_ERR(vibrator->gpio),
  101. "Failed to request main gpio\n");
  102. INIT_WORK(&vibrator->play_work, gpio_vibrator_play_work);
  103. vibrator->input->name = "gpio-vibrator";
  104. vibrator->input->id.bustype = BUS_HOST;
  105. vibrator->input->close = gpio_vibrator_close;
  106. input_set_drvdata(vibrator->input, vibrator);
  107. input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
  108. err = input_ff_create_memless(vibrator->input, NULL,
  109. gpio_vibrator_play_effect);
  110. if (err) {
  111. dev_err(&pdev->dev, "Couldn't create FF dev: %d\n", err);
  112. return err;
  113. }
  114. err = input_register_device(vibrator->input);
  115. if (err) {
  116. dev_err(&pdev->dev, "Couldn't register input dev: %d\n", err);
  117. return err;
  118. }
  119. platform_set_drvdata(pdev, vibrator);
  120. return 0;
  121. }
  122. static int gpio_vibrator_suspend(struct device *dev)
  123. {
  124. struct platform_device *pdev = to_platform_device(dev);
  125. struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
  126. cancel_work_sync(&vibrator->play_work);
  127. if (vibrator->running)
  128. gpio_vibrator_stop(vibrator);
  129. return 0;
  130. }
  131. static int gpio_vibrator_resume(struct device *dev)
  132. {
  133. struct platform_device *pdev = to_platform_device(dev);
  134. struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
  135. if (vibrator->running)
  136. gpio_vibrator_start(vibrator);
  137. return 0;
  138. }
  139. static DEFINE_SIMPLE_DEV_PM_OPS(gpio_vibrator_pm_ops,
  140. gpio_vibrator_suspend, gpio_vibrator_resume);
  141. #ifdef CONFIG_OF
  142. static const struct of_device_id gpio_vibra_dt_match_table[] = {
  143. { .compatible = "gpio-vibrator" },
  144. {}
  145. };
  146. MODULE_DEVICE_TABLE(of, gpio_vibra_dt_match_table);
  147. #endif
  148. static struct platform_driver gpio_vibrator_driver = {
  149. .probe = gpio_vibrator_probe,
  150. .driver = {
  151. .name = "gpio-vibrator",
  152. .pm = pm_sleep_ptr(&gpio_vibrator_pm_ops),
  153. .of_match_table = of_match_ptr(gpio_vibra_dt_match_table),
  154. },
  155. };
  156. module_platform_driver(gpio_vibrator_driver);
  157. MODULE_AUTHOR("Luca Weiss <luca@z3ntu.xy>");
  158. MODULE_DESCRIPTION("GPIO vibrator driver");
  159. MODULE_LICENSE("GPL");
  160. MODULE_ALIAS("platform:gpio-vibrator");