sc27xx-vibra.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Spreadtrum Communications Inc.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/input.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/property.h>
  11. #include <linux/regmap.h>
  12. #include <linux/workqueue.h>
  13. #define CUR_DRV_CAL_SEL GENMASK(13, 12)
  14. #define SLP_LDOVIBR_PD_EN BIT(9)
  15. #define LDO_VIBR_PD BIT(8)
  16. #define SC2730_CUR_DRV_CAL_SEL 0
  17. #define SC2730_SLP_LDOVIBR_PD_EN BIT(14)
  18. #define SC2730_LDO_VIBR_PD BIT(13)
  19. struct sc27xx_vibra_data {
  20. u32 cur_drv_cal_sel;
  21. u32 slp_pd_en;
  22. u32 ldo_pd;
  23. };
  24. struct vibra_info {
  25. struct input_dev *input_dev;
  26. struct work_struct play_work;
  27. struct regmap *regmap;
  28. const struct sc27xx_vibra_data *data;
  29. u32 base;
  30. u32 strength;
  31. bool enabled;
  32. };
  33. static const struct sc27xx_vibra_data sc2731_data = {
  34. .cur_drv_cal_sel = CUR_DRV_CAL_SEL,
  35. .slp_pd_en = SLP_LDOVIBR_PD_EN,
  36. .ldo_pd = LDO_VIBR_PD,
  37. };
  38. static const struct sc27xx_vibra_data sc2730_data = {
  39. .cur_drv_cal_sel = SC2730_CUR_DRV_CAL_SEL,
  40. .slp_pd_en = SC2730_SLP_LDOVIBR_PD_EN,
  41. .ldo_pd = SC2730_LDO_VIBR_PD,
  42. };
  43. static const struct sc27xx_vibra_data sc2721_data = {
  44. .cur_drv_cal_sel = CUR_DRV_CAL_SEL,
  45. .slp_pd_en = SLP_LDOVIBR_PD_EN,
  46. .ldo_pd = LDO_VIBR_PD,
  47. };
  48. static void sc27xx_vibra_set(struct vibra_info *info, bool on)
  49. {
  50. const struct sc27xx_vibra_data *data = info->data;
  51. if (on) {
  52. regmap_update_bits(info->regmap, info->base, data->ldo_pd, 0);
  53. regmap_update_bits(info->regmap, info->base,
  54. data->slp_pd_en, 0);
  55. info->enabled = true;
  56. } else {
  57. regmap_update_bits(info->regmap, info->base, data->ldo_pd,
  58. data->ldo_pd);
  59. regmap_update_bits(info->regmap, info->base,
  60. data->slp_pd_en, data->slp_pd_en);
  61. info->enabled = false;
  62. }
  63. }
  64. static int sc27xx_vibra_hw_init(struct vibra_info *info)
  65. {
  66. const struct sc27xx_vibra_data *data = info->data;
  67. if (!data->cur_drv_cal_sel)
  68. return 0;
  69. return regmap_update_bits(info->regmap, info->base,
  70. data->cur_drv_cal_sel, 0);
  71. }
  72. static void sc27xx_vibra_play_work(struct work_struct *work)
  73. {
  74. struct vibra_info *info = container_of(work, struct vibra_info,
  75. play_work);
  76. if (info->strength && !info->enabled)
  77. sc27xx_vibra_set(info, true);
  78. else if (info->strength == 0 && info->enabled)
  79. sc27xx_vibra_set(info, false);
  80. }
  81. static int sc27xx_vibra_play(struct input_dev *input, void *data,
  82. struct ff_effect *effect)
  83. {
  84. struct vibra_info *info = input_get_drvdata(input);
  85. info->strength = effect->u.rumble.weak_magnitude;
  86. schedule_work(&info->play_work);
  87. return 0;
  88. }
  89. static void sc27xx_vibra_close(struct input_dev *input)
  90. {
  91. struct vibra_info *info = input_get_drvdata(input);
  92. cancel_work_sync(&info->play_work);
  93. if (info->enabled)
  94. sc27xx_vibra_set(info, false);
  95. }
  96. static int sc27xx_vibra_probe(struct platform_device *pdev)
  97. {
  98. struct vibra_info *info;
  99. const struct sc27xx_vibra_data *data;
  100. int error;
  101. data = device_get_match_data(&pdev->dev);
  102. if (!data) {
  103. dev_err(&pdev->dev, "no matching driver data found\n");
  104. return -EINVAL;
  105. }
  106. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  107. if (!info)
  108. return -ENOMEM;
  109. info->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  110. if (!info->regmap) {
  111. dev_err(&pdev->dev, "failed to get vibrator regmap.\n");
  112. return -ENODEV;
  113. }
  114. error = device_property_read_u32(&pdev->dev, "reg", &info->base);
  115. if (error) {
  116. dev_err(&pdev->dev, "failed to get vibrator base address.\n");
  117. return error;
  118. }
  119. info->input_dev = devm_input_allocate_device(&pdev->dev);
  120. if (!info->input_dev) {
  121. dev_err(&pdev->dev, "failed to allocate input device.\n");
  122. return -ENOMEM;
  123. }
  124. info->input_dev->name = "sc27xx:vibrator";
  125. info->input_dev->id.version = 0;
  126. info->input_dev->close = sc27xx_vibra_close;
  127. info->data = data;
  128. input_set_drvdata(info->input_dev, info);
  129. input_set_capability(info->input_dev, EV_FF, FF_RUMBLE);
  130. INIT_WORK(&info->play_work, sc27xx_vibra_play_work);
  131. info->enabled = false;
  132. error = sc27xx_vibra_hw_init(info);
  133. if (error) {
  134. dev_err(&pdev->dev, "failed to initialize the vibrator.\n");
  135. return error;
  136. }
  137. error = input_ff_create_memless(info->input_dev, NULL,
  138. sc27xx_vibra_play);
  139. if (error) {
  140. dev_err(&pdev->dev, "failed to register vibrator to FF.\n");
  141. return error;
  142. }
  143. error = input_register_device(info->input_dev);
  144. if (error) {
  145. dev_err(&pdev->dev, "failed to register input device.\n");
  146. return error;
  147. }
  148. return 0;
  149. }
  150. static const struct of_device_id sc27xx_vibra_of_match[] = {
  151. { .compatible = "sprd,sc2721-vibrator", .data = &sc2721_data },
  152. { .compatible = "sprd,sc2730-vibrator", .data = &sc2730_data },
  153. { .compatible = "sprd,sc2731-vibrator", .data = &sc2731_data },
  154. {}
  155. };
  156. MODULE_DEVICE_TABLE(of, sc27xx_vibra_of_match);
  157. static struct platform_driver sc27xx_vibra_driver = {
  158. .driver = {
  159. .name = "sc27xx-vibrator",
  160. .of_match_table = sc27xx_vibra_of_match,
  161. },
  162. .probe = sc27xx_vibra_probe,
  163. };
  164. module_platform_driver(sc27xx_vibra_driver);
  165. MODULE_DESCRIPTION("Spreadtrum SC27xx Vibrator Driver");
  166. MODULE_LICENSE("GPL v2");
  167. MODULE_AUTHOR("Xiaotong Lu <[email protected]>");