arizona-haptics.c 6.1 KB

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