sc27xx-vibra.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Spreadtrum Communications Inc.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/of_address.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/regmap.h>
  9. #include <linux/input.h>
  10. #include <linux/workqueue.h>
  11. #define CUR_DRV_CAL_SEL GENMASK(13, 12)
  12. #define SLP_LDOVIBR_PD_EN BIT(9)
  13. #define LDO_VIBR_PD BIT(8)
  14. struct vibra_info {
  15. struct input_dev *input_dev;
  16. struct work_struct play_work;
  17. struct regmap *regmap;
  18. u32 base;
  19. u32 strength;
  20. bool enabled;
  21. };
  22. static void sc27xx_vibra_set(struct vibra_info *info, bool on)
  23. {
  24. if (on) {
  25. regmap_update_bits(info->regmap, info->base, LDO_VIBR_PD, 0);
  26. regmap_update_bits(info->regmap, info->base,
  27. SLP_LDOVIBR_PD_EN, 0);
  28. info->enabled = true;
  29. } else {
  30. regmap_update_bits(info->regmap, info->base, LDO_VIBR_PD,
  31. LDO_VIBR_PD);
  32. regmap_update_bits(info->regmap, info->base,
  33. SLP_LDOVIBR_PD_EN, SLP_LDOVIBR_PD_EN);
  34. info->enabled = false;
  35. }
  36. }
  37. static int sc27xx_vibra_hw_init(struct vibra_info *info)
  38. {
  39. return regmap_update_bits(info->regmap, info->base, CUR_DRV_CAL_SEL, 0);
  40. }
  41. static void sc27xx_vibra_play_work(struct work_struct *work)
  42. {
  43. struct vibra_info *info = container_of(work, struct vibra_info,
  44. play_work);
  45. if (info->strength && !info->enabled)
  46. sc27xx_vibra_set(info, true);
  47. else if (info->strength == 0 && info->enabled)
  48. sc27xx_vibra_set(info, false);
  49. }
  50. static int sc27xx_vibra_play(struct input_dev *input, void *data,
  51. struct ff_effect *effect)
  52. {
  53. struct vibra_info *info = input_get_drvdata(input);
  54. info->strength = effect->u.rumble.weak_magnitude;
  55. schedule_work(&info->play_work);
  56. return 0;
  57. }
  58. static void sc27xx_vibra_close(struct input_dev *input)
  59. {
  60. struct vibra_info *info = input_get_drvdata(input);
  61. cancel_work_sync(&info->play_work);
  62. if (info->enabled)
  63. sc27xx_vibra_set(info, false);
  64. }
  65. static int sc27xx_vibra_probe(struct platform_device *pdev)
  66. {
  67. struct vibra_info *info;
  68. int error;
  69. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  70. if (!info)
  71. return -ENOMEM;
  72. info->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  73. if (!info->regmap) {
  74. dev_err(&pdev->dev, "failed to get vibrator regmap.\n");
  75. return -ENODEV;
  76. }
  77. error = device_property_read_u32(&pdev->dev, "reg", &info->base);
  78. if (error) {
  79. dev_err(&pdev->dev, "failed to get vibrator base address.\n");
  80. return error;
  81. }
  82. info->input_dev = devm_input_allocate_device(&pdev->dev);
  83. if (!info->input_dev) {
  84. dev_err(&pdev->dev, "failed to allocate input device.\n");
  85. return -ENOMEM;
  86. }
  87. info->input_dev->name = "sc27xx:vibrator";
  88. info->input_dev->id.version = 0;
  89. info->input_dev->close = sc27xx_vibra_close;
  90. input_set_drvdata(info->input_dev, info);
  91. input_set_capability(info->input_dev, EV_FF, FF_RUMBLE);
  92. INIT_WORK(&info->play_work, sc27xx_vibra_play_work);
  93. info->enabled = false;
  94. error = sc27xx_vibra_hw_init(info);
  95. if (error) {
  96. dev_err(&pdev->dev, "failed to initialize the vibrator.\n");
  97. return error;
  98. }
  99. error = input_ff_create_memless(info->input_dev, NULL,
  100. sc27xx_vibra_play);
  101. if (error) {
  102. dev_err(&pdev->dev, "failed to register vibrator to FF.\n");
  103. return error;
  104. }
  105. error = input_register_device(info->input_dev);
  106. if (error) {
  107. dev_err(&pdev->dev, "failed to register input device.\n");
  108. return error;
  109. }
  110. return 0;
  111. }
  112. static const struct of_device_id sc27xx_vibra_of_match[] = {
  113. { .compatible = "sprd,sc2731-vibrator", },
  114. {}
  115. };
  116. MODULE_DEVICE_TABLE(of, sc27xx_vibra_of_match);
  117. static struct platform_driver sc27xx_vibra_driver = {
  118. .driver = {
  119. .name = "sc27xx-vibrator",
  120. .of_match_table = sc27xx_vibra_of_match,
  121. },
  122. .probe = sc27xx_vibra_probe,
  123. };
  124. module_platform_driver(sc27xx_vibra_driver);
  125. MODULE_DESCRIPTION("Spreadtrum SC27xx Vibrator Driver");
  126. MODULE_LICENSE("GPL v2");
  127. MODULE_AUTHOR("Xiaotong Lu <xiaotong.lu@spreadtrum.com>");