arizona-haptics.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <[email protected]>
  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. struct snd_soc_component *component =
  32. snd_soc_dapm_to_component(arizona->dapm);
  33. int ret;
  34. if (!haptics->arizona->dapm) {
  35. dev_err(arizona->dev, "No DAPM context\n");
  36. return;
  37. }
  38. if (haptics->intensity) {
  39. ret = regmap_update_bits(arizona->regmap,
  40. ARIZONA_HAPTICS_PHASE_2_INTENSITY,
  41. ARIZONA_PHASE2_INTENSITY_MASK,
  42. haptics->intensity);
  43. if (ret != 0) {
  44. dev_err(arizona->dev, "Failed to set intensity: %d\n",
  45. ret);
  46. return;
  47. }
  48. /* This enable sequence will be a noop if already enabled */
  49. ret = regmap_update_bits(arizona->regmap,
  50. ARIZONA_HAPTICS_CONTROL_1,
  51. ARIZONA_HAP_CTRL_MASK,
  52. 1 << ARIZONA_HAP_CTRL_SHIFT);
  53. if (ret != 0) {
  54. dev_err(arizona->dev, "Failed to start haptics: %d\n",
  55. ret);
  56. return;
  57. }
  58. ret = snd_soc_component_enable_pin(component, "HAPTICS");
  59. if (ret != 0) {
  60. dev_err(arizona->dev, "Failed to start HAPTICS: %d\n",
  61. ret);
  62. return;
  63. }
  64. ret = snd_soc_dapm_sync(arizona->dapm);
  65. if (ret != 0) {
  66. dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
  67. ret);
  68. return;
  69. }
  70. } else {
  71. /* This disable sequence will be a noop if already enabled */
  72. ret = snd_soc_component_disable_pin(component, "HAPTICS");
  73. if (ret != 0) {
  74. dev_err(arizona->dev, "Failed to disable HAPTICS: %d\n",
  75. ret);
  76. return;
  77. }
  78. ret = snd_soc_dapm_sync(arizona->dapm);
  79. if (ret != 0) {
  80. dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
  81. ret);
  82. return;
  83. }
  84. ret = regmap_update_bits(arizona->regmap,
  85. ARIZONA_HAPTICS_CONTROL_1,
  86. ARIZONA_HAP_CTRL_MASK, 0);
  87. if (ret != 0) {
  88. dev_err(arizona->dev, "Failed to stop haptics: %d\n",
  89. ret);
  90. return;
  91. }
  92. }
  93. }
  94. static int arizona_haptics_play(struct input_dev *input, void *data,
  95. struct ff_effect *effect)
  96. {
  97. struct arizona_haptics *haptics = input_get_drvdata(input);
  98. struct arizona *arizona = haptics->arizona;
  99. if (!arizona->dapm) {
  100. dev_err(arizona->dev, "No DAPM context\n");
  101. return -EBUSY;
  102. }
  103. if (effect->u.rumble.strong_magnitude) {
  104. /* Scale the magnitude into the range the device supports */
  105. if (arizona->pdata.hap_act) {
  106. haptics->intensity =
  107. effect->u.rumble.strong_magnitude >> 9;
  108. if (effect->direction < 0x8000)
  109. haptics->intensity += 0x7f;
  110. } else {
  111. haptics->intensity =
  112. effect->u.rumble.strong_magnitude >> 8;
  113. }
  114. } else {
  115. haptics->intensity = 0;
  116. }
  117. schedule_work(&haptics->work);
  118. return 0;
  119. }
  120. static void arizona_haptics_close(struct input_dev *input)
  121. {
  122. struct arizona_haptics *haptics = input_get_drvdata(input);
  123. struct snd_soc_component *component;
  124. cancel_work_sync(&haptics->work);
  125. if (haptics->arizona->dapm) {
  126. component = snd_soc_dapm_to_component(haptics->arizona->dapm);
  127. snd_soc_component_disable_pin(component, "HAPTICS");
  128. }
  129. }
  130. static int arizona_haptics_probe(struct platform_device *pdev)
  131. {
  132. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  133. struct arizona_haptics *haptics;
  134. int ret;
  135. haptics = devm_kzalloc(&pdev->dev, sizeof(*haptics), GFP_KERNEL);
  136. if (!haptics)
  137. return -ENOMEM;
  138. haptics->arizona = arizona;
  139. ret = regmap_update_bits(arizona->regmap, ARIZONA_HAPTICS_CONTROL_1,
  140. ARIZONA_HAP_ACT, arizona->pdata.hap_act);
  141. if (ret != 0) {
  142. dev_err(arizona->dev, "Failed to set haptics actuator: %d\n",
  143. ret);
  144. return ret;
  145. }
  146. INIT_WORK(&haptics->work, arizona_haptics_work);
  147. haptics->input_dev = devm_input_allocate_device(&pdev->dev);
  148. if (!haptics->input_dev) {
  149. dev_err(arizona->dev, "Failed to allocate input device\n");
  150. return -ENOMEM;
  151. }
  152. input_set_drvdata(haptics->input_dev, haptics);
  153. haptics->input_dev->name = "arizona:haptics";
  154. haptics->input_dev->close = arizona_haptics_close;
  155. __set_bit(FF_RUMBLE, haptics->input_dev->ffbit);
  156. ret = input_ff_create_memless(haptics->input_dev, NULL,
  157. arizona_haptics_play);
  158. if (ret < 0) {
  159. dev_err(arizona->dev, "input_ff_create_memless() failed: %d\n",
  160. ret);
  161. return ret;
  162. }
  163. ret = input_register_device(haptics->input_dev);
  164. if (ret < 0) {
  165. dev_err(arizona->dev, "couldn't register input device: %d\n",
  166. ret);
  167. return ret;
  168. }
  169. return 0;
  170. }
  171. static struct platform_driver arizona_haptics_driver = {
  172. .probe = arizona_haptics_probe,
  173. .driver = {
  174. .name = "arizona-haptics",
  175. },
  176. };
  177. module_platform_driver(arizona_haptics_driver);
  178. MODULE_ALIAS("platform:arizona-haptics");
  179. MODULE_DESCRIPTION("Arizona haptics driver");
  180. MODULE_LICENSE("GPL");
  181. MODULE_AUTHOR("Mark Brown <[email protected]>");