twl4030-vibra.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/workqueue.h>
  29. #include <linux/i2c/twl.h>
  30. #include <linux/mfd/twl4030-codec.h>
  31. #include <linux/mfd/core.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 workqueue_struct *workqueue;
  42. struct work_struct play_work;
  43. bool enabled;
  44. int speed;
  45. int direction;
  46. bool coexist;
  47. };
  48. static void vibra_disable_leds(void)
  49. {
  50. u8 reg;
  51. /* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
  52. twl_i2c_read_u8(TWL4030_MODULE_LED, &reg, LEDEN);
  53. reg &= ~0x03;
  54. twl_i2c_write_u8(TWL4030_MODULE_LED, LEDEN, reg);
  55. }
  56. /* Powers H-Bridge and enables audio clk */
  57. static void vibra_enable(struct vibra_info *info)
  58. {
  59. u8 reg;
  60. twl4030_codec_enable_resource(TWL4030_CODEC_RES_POWER);
  61. /* turn H-Bridge on */
  62. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  63. &reg, TWL4030_REG_VIBRA_CTL);
  64. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  65. (reg | TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
  66. twl4030_codec_enable_resource(TWL4030_CODEC_RES_APLL);
  67. info->enabled = true;
  68. }
  69. static void vibra_disable(struct vibra_info *info)
  70. {
  71. u8 reg;
  72. /* Power down H-Bridge */
  73. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  74. &reg, TWL4030_REG_VIBRA_CTL);
  75. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  76. (reg & ~TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
  77. twl4030_codec_disable_resource(TWL4030_CODEC_RES_APLL);
  78. twl4030_codec_disable_resource(TWL4030_CODEC_RES_POWER);
  79. info->enabled = false;
  80. }
  81. static void vibra_play_work(struct work_struct *work)
  82. {
  83. struct vibra_info *info = container_of(work,
  84. struct vibra_info, play_work);
  85. int dir;
  86. int pwm;
  87. u8 reg;
  88. dir = info->direction;
  89. pwm = info->speed;
  90. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  91. &reg, TWL4030_REG_VIBRA_CTL);
  92. if (pwm && (!info->coexist || !(reg & TWL4030_VIBRA_SEL))) {
  93. if (!info->enabled)
  94. vibra_enable(info);
  95. /* set vibra rotation direction */
  96. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
  97. &reg, TWL4030_REG_VIBRA_CTL);
  98. reg = (dir) ? (reg | TWL4030_VIBRA_DIR) :
  99. (reg & ~TWL4030_VIBRA_DIR);
  100. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  101. reg, TWL4030_REG_VIBRA_CTL);
  102. /* set PWM, 1 = max, 255 = min */
  103. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  104. 256 - pwm, TWL4030_REG_VIBRA_SET);
  105. } else {
  106. if (info->enabled)
  107. vibra_disable(info);
  108. }
  109. }
  110. /*** Input/ForceFeedback ***/
  111. static int vibra_play(struct input_dev *input, void *data,
  112. struct ff_effect *effect)
  113. {
  114. struct vibra_info *info = input_get_drvdata(input);
  115. info->speed = effect->u.rumble.strong_magnitude >> 8;
  116. if (!info->speed)
  117. info->speed = effect->u.rumble.weak_magnitude >> 9;
  118. info->direction = effect->direction < EFFECT_DIR_180_DEG ? 0 : 1;
  119. queue_work(info->workqueue, &info->play_work);
  120. return 0;
  121. }
  122. static int twl4030_vibra_open(struct input_dev *input)
  123. {
  124. struct vibra_info *info = input_get_drvdata(input);
  125. info->workqueue = create_singlethread_workqueue("vibra");
  126. if (info->workqueue == NULL) {
  127. dev_err(&input->dev, "couldn't create workqueue\n");
  128. return -ENOMEM;
  129. }
  130. return 0;
  131. }
  132. static void twl4030_vibra_close(struct input_dev *input)
  133. {
  134. struct vibra_info *info = input_get_drvdata(input);
  135. cancel_work_sync(&info->play_work);
  136. INIT_WORK(&info->play_work, vibra_play_work); /* cleanup */
  137. destroy_workqueue(info->workqueue);
  138. info->workqueue = NULL;
  139. if (info->enabled)
  140. vibra_disable(info);
  141. }
  142. /*** Module ***/
  143. #if CONFIG_PM
  144. static int twl4030_vibra_suspend(struct device *dev)
  145. {
  146. struct platform_device *pdev = to_platform_device(dev);
  147. struct vibra_info *info = platform_get_drvdata(pdev);
  148. if (info->enabled)
  149. vibra_disable(info);
  150. return 0;
  151. }
  152. static int twl4030_vibra_resume(struct device *dev)
  153. {
  154. vibra_disable_leds();
  155. return 0;
  156. }
  157. static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
  158. twl4030_vibra_suspend, twl4030_vibra_resume);
  159. #endif
  160. static int __devinit twl4030_vibra_probe(struct platform_device *pdev)
  161. {
  162. struct twl4030_codec_vibra_data *pdata = mfd_get_data(pdev);
  163. struct vibra_info *info;
  164. int ret;
  165. if (!pdata) {
  166. dev_dbg(&pdev->dev, "platform_data not available\n");
  167. return -EINVAL;
  168. }
  169. info = kzalloc(sizeof(*info), GFP_KERNEL);
  170. if (!info)
  171. return -ENOMEM;
  172. info->dev = &pdev->dev;
  173. info->coexist = pdata->coexist;
  174. INIT_WORK(&info->play_work, vibra_play_work);
  175. info->input_dev = input_allocate_device();
  176. if (info->input_dev == NULL) {
  177. dev_err(&pdev->dev, "couldn't allocate input device\n");
  178. ret = -ENOMEM;
  179. goto err_kzalloc;
  180. }
  181. input_set_drvdata(info->input_dev, info);
  182. info->input_dev->name = "twl4030:vibrator";
  183. info->input_dev->id.version = 1;
  184. info->input_dev->dev.parent = pdev->dev.parent;
  185. info->input_dev->open = twl4030_vibra_open;
  186. info->input_dev->close = twl4030_vibra_close;
  187. __set_bit(FF_RUMBLE, info->input_dev->ffbit);
  188. ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
  189. if (ret < 0) {
  190. dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
  191. goto err_ialloc;
  192. }
  193. ret = input_register_device(info->input_dev);
  194. if (ret < 0) {
  195. dev_dbg(&pdev->dev, "couldn't register input device\n");
  196. goto err_iff;
  197. }
  198. vibra_disable_leds();
  199. platform_set_drvdata(pdev, info);
  200. return 0;
  201. err_iff:
  202. input_ff_destroy(info->input_dev);
  203. err_ialloc:
  204. input_free_device(info->input_dev);
  205. err_kzalloc:
  206. kfree(info);
  207. return ret;
  208. }
  209. static int __devexit twl4030_vibra_remove(struct platform_device *pdev)
  210. {
  211. struct vibra_info *info = platform_get_drvdata(pdev);
  212. /* this also free ff-memless and calls close if needed */
  213. input_unregister_device(info->input_dev);
  214. kfree(info);
  215. platform_set_drvdata(pdev, NULL);
  216. return 0;
  217. }
  218. static struct platform_driver twl4030_vibra_driver = {
  219. .probe = twl4030_vibra_probe,
  220. .remove = __devexit_p(twl4030_vibra_remove),
  221. .driver = {
  222. .name = "twl4030-vibra",
  223. .owner = THIS_MODULE,
  224. #ifdef CONFIG_PM
  225. .pm = &twl4030_vibra_pm_ops,
  226. #endif
  227. },
  228. };
  229. static int __init twl4030_vibra_init(void)
  230. {
  231. return platform_driver_register(&twl4030_vibra_driver);
  232. }
  233. module_init(twl4030_vibra_init);
  234. static void __exit twl4030_vibra_exit(void)
  235. {
  236. platform_driver_unregister(&twl4030_vibra_driver);
  237. }
  238. module_exit(twl4030_vibra_exit);
  239. MODULE_ALIAS("platform:twl4030-vibra");
  240. MODULE_DESCRIPTION("TWL4030 Vibra driver");
  241. MODULE_LICENSE("GPL");
  242. MODULE_AUTHOR("Nokia Corporation");