twl4030-vibra.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * twl4030-vibra.c - TWL4030 Vibrator driver
  4. *
  5. * Copyright (C) 2008-2010 Nokia Corporation
  6. *
  7. * Written by Henrik Saari <[email protected]>
  8. * Updates by Felipe Balbi <[email protected]>
  9. * Input by Jari Vanhala <[email protected]>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/of.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/mfd/twl.h>
  17. #include <linux/mfd/twl4030-audio.h>
  18. #include <linux/input.h>
  19. #include <linux/slab.h>
  20. /* MODULE ID2 */
  21. #define LEDEN 0x00
  22. /* ForceFeedback */
  23. #define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */
  24. struct vibra_info {
  25. struct device *dev;
  26. struct input_dev *input_dev;
  27. struct work_struct play_work;
  28. bool enabled;
  29. int speed;
  30. int direction;
  31. bool coexist;
  32. };
  33. static void vibra_disable_leds(void)
  34. {
  35. u8 reg;
  36. /* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
  37. twl_i2c_read_u8(TWL4030_MODULE_LED, &reg, LEDEN);
  38. reg &= ~0x03;
  39. twl_i2c_write_u8(TWL4030_MODULE_LED, LEDEN, reg);
  40. }
  41. /* Powers H-Bridge and enables audio clk */
  42. static void vibra_enable(struct vibra_info *info)
  43. {
  44. u8 reg;
  45. twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
  46. /* turn H-Bridge on */
  47. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  48. &reg, TWL4030_REG_VIBRA_CTL);
  49. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  50. (reg | TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
  51. twl4030_audio_enable_resource(TWL4030_AUDIO_RES_APLL);
  52. info->enabled = true;
  53. }
  54. static void vibra_disable(struct vibra_info *info)
  55. {
  56. u8 reg;
  57. /* Power down H-Bridge */
  58. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  59. &reg, TWL4030_REG_VIBRA_CTL);
  60. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  61. (reg & ~TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
  62. twl4030_audio_disable_resource(TWL4030_AUDIO_RES_APLL);
  63. twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER);
  64. info->enabled = false;
  65. }
  66. static void vibra_play_work(struct work_struct *work)
  67. {
  68. struct vibra_info *info = container_of(work,
  69. struct vibra_info, play_work);
  70. int dir;
  71. int pwm;
  72. u8 reg;
  73. dir = info->direction;
  74. pwm = info->speed;
  75. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  76. &reg, TWL4030_REG_VIBRA_CTL);
  77. if (pwm && (!info->coexist || !(reg & TWL4030_VIBRA_SEL))) {
  78. if (!info->enabled)
  79. vibra_enable(info);
  80. /* set vibra rotation direction */
  81. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  82. &reg, TWL4030_REG_VIBRA_CTL);
  83. reg = (dir) ? (reg | TWL4030_VIBRA_DIR) :
  84. (reg & ~TWL4030_VIBRA_DIR);
  85. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  86. reg, TWL4030_REG_VIBRA_CTL);
  87. /* set PWM, 1 = max, 255 = min */
  88. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  89. 256 - pwm, TWL4030_REG_VIBRA_SET);
  90. } else {
  91. if (info->enabled)
  92. vibra_disable(info);
  93. }
  94. }
  95. /*** Input/ForceFeedback ***/
  96. static int vibra_play(struct input_dev *input, void *data,
  97. struct ff_effect *effect)
  98. {
  99. struct vibra_info *info = input_get_drvdata(input);
  100. info->speed = effect->u.rumble.strong_magnitude >> 8;
  101. if (!info->speed)
  102. info->speed = effect->u.rumble.weak_magnitude >> 9;
  103. info->direction = effect->direction < EFFECT_DIR_180_DEG ? 0 : 1;
  104. schedule_work(&info->play_work);
  105. return 0;
  106. }
  107. static void twl4030_vibra_close(struct input_dev *input)
  108. {
  109. struct vibra_info *info = input_get_drvdata(input);
  110. cancel_work_sync(&info->play_work);
  111. if (info->enabled)
  112. vibra_disable(info);
  113. }
  114. /*** Module ***/
  115. static int __maybe_unused twl4030_vibra_suspend(struct device *dev)
  116. {
  117. struct platform_device *pdev = to_platform_device(dev);
  118. struct vibra_info *info = platform_get_drvdata(pdev);
  119. if (info->enabled)
  120. vibra_disable(info);
  121. return 0;
  122. }
  123. static int __maybe_unused twl4030_vibra_resume(struct device *dev)
  124. {
  125. vibra_disable_leds();
  126. return 0;
  127. }
  128. static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
  129. twl4030_vibra_suspend, twl4030_vibra_resume);
  130. static bool twl4030_vibra_check_coexist(struct device_node *parent)
  131. {
  132. struct device_node *node;
  133. node = of_get_child_by_name(parent, "codec");
  134. if (node) {
  135. of_node_put(node);
  136. return true;
  137. }
  138. return false;
  139. }
  140. static int twl4030_vibra_probe(struct platform_device *pdev)
  141. {
  142. struct device_node *twl4030_core_node = pdev->dev.parent->of_node;
  143. struct vibra_info *info;
  144. int ret;
  145. if (!twl4030_core_node) {
  146. dev_dbg(&pdev->dev, "twl4030 OF node is missing\n");
  147. return -EINVAL;
  148. }
  149. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  150. if (!info)
  151. return -ENOMEM;
  152. info->dev = &pdev->dev;
  153. info->coexist = twl4030_vibra_check_coexist(twl4030_core_node);
  154. INIT_WORK(&info->play_work, vibra_play_work);
  155. info->input_dev = devm_input_allocate_device(&pdev->dev);
  156. if (info->input_dev == NULL) {
  157. dev_err(&pdev->dev, "couldn't allocate input device\n");
  158. return -ENOMEM;
  159. }
  160. input_set_drvdata(info->input_dev, info);
  161. info->input_dev->name = "twl4030:vibrator";
  162. info->input_dev->id.version = 1;
  163. info->input_dev->close = twl4030_vibra_close;
  164. __set_bit(FF_RUMBLE, info->input_dev->ffbit);
  165. ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
  166. if (ret < 0) {
  167. dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
  168. return ret;
  169. }
  170. ret = input_register_device(info->input_dev);
  171. if (ret < 0) {
  172. dev_dbg(&pdev->dev, "couldn't register input device\n");
  173. goto err_iff;
  174. }
  175. vibra_disable_leds();
  176. platform_set_drvdata(pdev, info);
  177. return 0;
  178. err_iff:
  179. input_ff_destroy(info->input_dev);
  180. return ret;
  181. }
  182. static struct platform_driver twl4030_vibra_driver = {
  183. .probe = twl4030_vibra_probe,
  184. .driver = {
  185. .name = "twl4030-vibra",
  186. .pm = &twl4030_vibra_pm_ops,
  187. },
  188. };
  189. module_platform_driver(twl4030_vibra_driver);
  190. MODULE_ALIAS("platform:twl4030-vibra");
  191. MODULE_DESCRIPTION("TWL4030 Vibra driver");
  192. MODULE_LICENSE("GPL");
  193. MODULE_AUTHOR("Nokia Corporation");