twl4030-vibra.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * twl4030-vibra.c - TWL4030 Vibrator driver
  3. *
  4. * Copyright (C) 2008-2010 Nokia Corporation
  5. *
  6. * Written by Henrik Saari <henrik.saari@nokia.com>
  7. * Updates by Felipe Balbi <felipe.balbi@nokia.com>
  8. * Input by Jari Vanhala <ext-jari.vanhala@nokia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/of.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/i2c/twl.h>
  31. #include <linux/mfd/twl4030-audio.h>
  32. #include <linux/input.h>
  33. #include <linux/slab.h>
  34. /* MODULE ID2 */
  35. #define LEDEN 0x00
  36. /* ForceFeedback */
  37. #define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */
  38. struct vibra_info {
  39. struct device *dev;
  40. struct input_dev *input_dev;
  41. struct work_struct play_work;
  42. bool enabled;
  43. int speed;
  44. int direction;
  45. bool coexist;
  46. };
  47. static void vibra_disable_leds(void)
  48. {
  49. u8 reg;
  50. /* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
  51. twl_i2c_read_u8(TWL4030_MODULE_LED, &reg, LEDEN);
  52. reg &= ~0x03;
  53. twl_i2c_write_u8(TWL4030_MODULE_LED, LEDEN, reg);
  54. }
  55. /* Powers H-Bridge and enables audio clk */
  56. static void vibra_enable(struct vibra_info *info)
  57. {
  58. u8 reg;
  59. twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
  60. /* turn H-Bridge on */
  61. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  62. &reg, TWL4030_REG_VIBRA_CTL);
  63. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  64. (reg | TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
  65. twl4030_audio_enable_resource(TWL4030_AUDIO_RES_APLL);
  66. info->enabled = true;
  67. }
  68. static void vibra_disable(struct vibra_info *info)
  69. {
  70. u8 reg;
  71. /* Power down H-Bridge */
  72. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  73. &reg, TWL4030_REG_VIBRA_CTL);
  74. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  75. (reg & ~TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
  76. twl4030_audio_disable_resource(TWL4030_AUDIO_RES_APLL);
  77. twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER);
  78. info->enabled = false;
  79. }
  80. static void vibra_play_work(struct work_struct *work)
  81. {
  82. struct vibra_info *info = container_of(work,
  83. struct vibra_info, play_work);
  84. int dir;
  85. int pwm;
  86. u8 reg;
  87. dir = info->direction;
  88. pwm = info->speed;
  89. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  90. &reg, TWL4030_REG_VIBRA_CTL);
  91. if (pwm && (!info->coexist || !(reg & TWL4030_VIBRA_SEL))) {
  92. if (!info->enabled)
  93. vibra_enable(info);
  94. /* set vibra rotation direction */
  95. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  96. &reg, TWL4030_REG_VIBRA_CTL);
  97. reg = (dir) ? (reg | TWL4030_VIBRA_DIR) :
  98. (reg & ~TWL4030_VIBRA_DIR);
  99. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  100. reg, TWL4030_REG_VIBRA_CTL);
  101. /* set PWM, 1 = max, 255 = min */
  102. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  103. 256 - pwm, TWL4030_REG_VIBRA_SET);
  104. } else {
  105. if (info->enabled)
  106. vibra_disable(info);
  107. }
  108. }
  109. /*** Input/ForceFeedback ***/
  110. static int vibra_play(struct input_dev *input, void *data,
  111. struct ff_effect *effect)
  112. {
  113. struct vibra_info *info = input_get_drvdata(input);
  114. info->speed = effect->u.rumble.strong_magnitude >> 8;
  115. if (!info->speed)
  116. info->speed = effect->u.rumble.weak_magnitude >> 9;
  117. info->direction = effect->direction < EFFECT_DIR_180_DEG ? 0 : 1;
  118. schedule_work(&info->play_work);
  119. return 0;
  120. }
  121. static void twl4030_vibra_close(struct input_dev *input)
  122. {
  123. struct vibra_info *info = input_get_drvdata(input);
  124. cancel_work_sync(&info->play_work);
  125. if (info->enabled)
  126. vibra_disable(info);
  127. }
  128. /*** Module ***/
  129. static int __maybe_unused twl4030_vibra_suspend(struct device *dev)
  130. {
  131. struct platform_device *pdev = to_platform_device(dev);
  132. struct vibra_info *info = platform_get_drvdata(pdev);
  133. if (info->enabled)
  134. vibra_disable(info);
  135. return 0;
  136. }
  137. static int __maybe_unused twl4030_vibra_resume(struct device *dev)
  138. {
  139. vibra_disable_leds();
  140. return 0;
  141. }
  142. static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
  143. twl4030_vibra_suspend, twl4030_vibra_resume);
  144. static bool twl4030_vibra_check_coexist(struct twl4030_vibra_data *pdata,
  145. struct device_node *parent)
  146. {
  147. struct device_node *node;
  148. if (pdata && pdata->coexist)
  149. return true;
  150. node = of_get_child_by_name(parent, "codec");
  151. if (node) {
  152. of_node_put(node);
  153. return true;
  154. }
  155. return false;
  156. }
  157. static int twl4030_vibra_probe(struct platform_device *pdev)
  158. {
  159. struct twl4030_vibra_data *pdata = dev_get_platdata(&pdev->dev);
  160. struct device_node *twl4030_core_node = pdev->dev.parent->of_node;
  161. struct vibra_info *info;
  162. int ret;
  163. if (!pdata && !twl4030_core_node) {
  164. dev_dbg(&pdev->dev, "platform_data not available\n");
  165. return -EINVAL;
  166. }
  167. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  168. if (!info)
  169. return -ENOMEM;
  170. info->dev = &pdev->dev;
  171. info->coexist = twl4030_vibra_check_coexist(pdata, twl4030_core_node);
  172. INIT_WORK(&info->play_work, vibra_play_work);
  173. info->input_dev = devm_input_allocate_device(&pdev->dev);
  174. if (info->input_dev == NULL) {
  175. dev_err(&pdev->dev, "couldn't allocate input device\n");
  176. return -ENOMEM;
  177. }
  178. input_set_drvdata(info->input_dev, info);
  179. info->input_dev->name = "twl4030:vibrator";
  180. info->input_dev->id.version = 1;
  181. info->input_dev->dev.parent = pdev->dev.parent;
  182. info->input_dev->close = twl4030_vibra_close;
  183. __set_bit(FF_RUMBLE, info->input_dev->ffbit);
  184. ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
  185. if (ret < 0) {
  186. dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
  187. return ret;
  188. }
  189. ret = input_register_device(info->input_dev);
  190. if (ret < 0) {
  191. dev_dbg(&pdev->dev, "couldn't register input device\n");
  192. goto err_iff;
  193. }
  194. vibra_disable_leds();
  195. platform_set_drvdata(pdev, info);
  196. return 0;
  197. err_iff:
  198. input_ff_destroy(info->input_dev);
  199. return ret;
  200. }
  201. static struct platform_driver twl4030_vibra_driver = {
  202. .probe = twl4030_vibra_probe,
  203. .driver = {
  204. .name = "twl4030-vibra",
  205. .pm = &twl4030_vibra_pm_ops,
  206. },
  207. };
  208. module_platform_driver(twl4030_vibra_driver);
  209. MODULE_ALIAS("platform:twl4030-vibra");
  210. MODULE_DESCRIPTION("TWL4030 Vibra driver");
  211. MODULE_LICENSE("GPL");
  212. MODULE_AUTHOR("Nokia Corporation");