arizona-haptics.c 5.3 KB

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