arizona-haptics.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Arizona haptics driver
  4. *
  5. * Copyright 2012 Wolfson Microelectronics plc
  6. *
  7. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/input.h>
  12. #include <linux/slab.h>
  13. #include <sound/soc.h>
  14. #include <sound/soc-dapm.h>
  15. #include <linux/mfd/arizona/core.h>
  16. #include <linux/mfd/arizona/pdata.h>
  17. #include <linux/mfd/arizona/registers.h>
  18. struct arizona_haptics {
  19. struct arizona *arizona;
  20. struct input_dev *input_dev;
  21. struct work_struct work;
  22. struct mutex mutex;
  23. u8 intensity;
  24. };
  25. static void arizona_haptics_work(struct work_struct *work)
  26. {
  27. struct arizona_haptics *haptics = container_of(work,
  28. struct arizona_haptics,
  29. work);
  30. struct arizona *arizona = haptics->arizona;
  31. int ret;
  32. if (!haptics->arizona->dapm) {
  33. dev_err(arizona->dev, "No DAPM context\n");
  34. return;
  35. }
  36. if (haptics->intensity) {
  37. ret = regmap_update_bits(arizona->regmap,
  38. ARIZONA_HAPTICS_PHASE_2_INTENSITY,
  39. ARIZONA_PHASE2_INTENSITY_MASK,
  40. haptics->intensity);
  41. if (ret != 0) {
  42. dev_err(arizona->dev, "Failed to set intensity: %d\n",
  43. ret);
  44. return;
  45. }
  46. /* This enable sequence will be a noop if already enabled */
  47. ret = regmap_update_bits(arizona->regmap,
  48. ARIZONA_HAPTICS_CONTROL_1,
  49. ARIZONA_HAP_CTRL_MASK,
  50. 1 << ARIZONA_HAP_CTRL_SHIFT);
  51. if (ret != 0) {
  52. dev_err(arizona->dev, "Failed to start haptics: %d\n",
  53. ret);
  54. return;
  55. }
  56. ret = snd_soc_dapm_enable_pin(arizona->dapm, "HAPTICS");
  57. if (ret != 0) {
  58. dev_err(arizona->dev, "Failed to start HAPTICS: %d\n",
  59. ret);
  60. return;
  61. }
  62. ret = snd_soc_dapm_sync(arizona->dapm);
  63. if (ret != 0) {
  64. dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
  65. ret);
  66. return;
  67. }
  68. } else {
  69. /* This disable sequence will be a noop if already enabled */
  70. ret = snd_soc_dapm_disable_pin(arizona->dapm, "HAPTICS");
  71. if (ret != 0) {
  72. dev_err(arizona->dev, "Failed to disable HAPTICS: %d\n",
  73. ret);
  74. return;
  75. }
  76. ret = snd_soc_dapm_sync(arizona->dapm);
  77. if (ret != 0) {
  78. dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
  79. ret);
  80. return;
  81. }
  82. ret = regmap_update_bits(arizona->regmap,
  83. ARIZONA_HAPTICS_CONTROL_1,
  84. ARIZONA_HAP_CTRL_MASK, 0);
  85. if (ret != 0) {
  86. dev_err(arizona->dev, "Failed to stop haptics: %d\n",
  87. ret);
  88. return;
  89. }
  90. }
  91. }
  92. static int arizona_haptics_play(struct input_dev *input, void *data,
  93. struct ff_effect *effect)
  94. {
  95. struct arizona_haptics *haptics = input_get_drvdata(input);
  96. struct arizona *arizona = haptics->arizona;
  97. if (!arizona->dapm) {
  98. dev_err(arizona->dev, "No DAPM context\n");
  99. return -EBUSY;
  100. }
  101. if (effect->u.rumble.strong_magnitude) {
  102. /* Scale the magnitude into the range the device supports */
  103. if (arizona->pdata.hap_act) {
  104. haptics->intensity =
  105. effect->u.rumble.strong_magnitude >> 9;
  106. if (effect->direction < 0x8000)
  107. haptics->intensity += 0x7f;
  108. } else {
  109. haptics->intensity =
  110. effect->u.rumble.strong_magnitude >> 8;
  111. }
  112. } else {
  113. haptics->intensity = 0;
  114. }
  115. schedule_work(&haptics->work);
  116. return 0;
  117. }
  118. static void arizona_haptics_close(struct input_dev *input)
  119. {
  120. struct arizona_haptics *haptics = input_get_drvdata(input);
  121. struct snd_soc_dapm_context *dapm = haptics->arizona->dapm;
  122. cancel_work_sync(&haptics->work);
  123. if (dapm)
  124. snd_soc_dapm_disable_pin(dapm, "HAPTICS");
  125. }
  126. static int arizona_haptics_probe(struct platform_device *pdev)
  127. {
  128. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  129. struct arizona_haptics *haptics;
  130. int ret;
  131. haptics = devm_kzalloc(&pdev->dev, sizeof(*haptics), GFP_KERNEL);
  132. if (!haptics)
  133. return -ENOMEM;
  134. haptics->arizona = arizona;
  135. ret = regmap_update_bits(arizona->regmap, ARIZONA_HAPTICS_CONTROL_1,
  136. ARIZONA_HAP_ACT, arizona->pdata.hap_act);
  137. if (ret != 0) {
  138. dev_err(arizona->dev, "Failed to set haptics actuator: %d\n",
  139. ret);
  140. return ret;
  141. }
  142. INIT_WORK(&haptics->work, arizona_haptics_work);
  143. haptics->input_dev = devm_input_allocate_device(&pdev->dev);
  144. if (!haptics->input_dev) {
  145. dev_err(arizona->dev, "Failed to allocate input device\n");
  146. return -ENOMEM;
  147. }
  148. input_set_drvdata(haptics->input_dev, haptics);
  149. haptics->input_dev->name = "arizona:haptics";
  150. haptics->input_dev->close = arizona_haptics_close;
  151. __set_bit(FF_RUMBLE, haptics->input_dev->ffbit);
  152. ret = input_ff_create_memless(haptics->input_dev, NULL,
  153. arizona_haptics_play);
  154. if (ret < 0) {
  155. dev_err(arizona->dev, "input_ff_create_memless() failed: %d\n",
  156. ret);
  157. return ret;
  158. }
  159. ret = input_register_device(haptics->input_dev);
  160. if (ret < 0) {
  161. dev_err(arizona->dev, "couldn't register input device: %d\n",
  162. ret);
  163. return ret;
  164. }
  165. return 0;
  166. }
  167. static struct platform_driver arizona_haptics_driver = {
  168. .probe = arizona_haptics_probe,
  169. .driver = {
  170. .name = "arizona-haptics",
  171. },
  172. };
  173. module_platform_driver(arizona_haptics_driver);
  174. MODULE_ALIAS("platform:arizona-haptics");
  175. MODULE_DESCRIPTION("Arizona haptics driver");
  176. MODULE_LICENSE("GPL");
  177. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");