twl4030-vibra.c 7.2 KB

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