twl4030-vibra.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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-audio.h>
  31. #include <linux/input.h>
  32. #include <linux/slab.h>
  33. /* MODULE ID2 */
  34. #define LEDEN 0x00
  35. /* ForceFeedback */
  36. #define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */
  37. struct vibra_info {
  38. struct device *dev;
  39. struct input_dev *input_dev;
  40. struct workqueue_struct *workqueue;
  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. queue_work(info->workqueue, &info->play_work);
  119. return 0;
  120. }
  121. static int twl4030_vibra_open(struct input_dev *input)
  122. {
  123. struct vibra_info *info = input_get_drvdata(input);
  124. info->workqueue = create_singlethread_workqueue("vibra");
  125. if (info->workqueue == NULL) {
  126. dev_err(&input->dev, "couldn't create workqueue\n");
  127. return -ENOMEM;
  128. }
  129. return 0;
  130. }
  131. static void twl4030_vibra_close(struct input_dev *input)
  132. {
  133. struct vibra_info *info = input_get_drvdata(input);
  134. cancel_work_sync(&info->play_work);
  135. INIT_WORK(&info->play_work, vibra_play_work); /* cleanup */
  136. destroy_workqueue(info->workqueue);
  137. info->workqueue = NULL;
  138. if (info->enabled)
  139. vibra_disable(info);
  140. }
  141. /*** Module ***/
  142. #ifdef CONFIG_PM_SLEEP
  143. static int twl4030_vibra_suspend(struct device *dev)
  144. {
  145. struct platform_device *pdev = to_platform_device(dev);
  146. struct vibra_info *info = platform_get_drvdata(pdev);
  147. if (info->enabled)
  148. vibra_disable(info);
  149. return 0;
  150. }
  151. static int twl4030_vibra_resume(struct device *dev)
  152. {
  153. vibra_disable_leds();
  154. return 0;
  155. }
  156. #endif
  157. static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
  158. twl4030_vibra_suspend, twl4030_vibra_resume);
  159. static int __devinit twl4030_vibra_probe(struct platform_device *pdev)
  160. {
  161. struct twl4030_vibra_data *pdata = pdev->dev.platform_data;
  162. struct vibra_info *info;
  163. int ret;
  164. if (!pdata) {
  165. dev_dbg(&pdev->dev, "platform_data not available\n");
  166. return -EINVAL;
  167. }
  168. info = kzalloc(sizeof(*info), GFP_KERNEL);
  169. if (!info)
  170. return -ENOMEM;
  171. info->dev = &pdev->dev;
  172. info->coexist = pdata->coexist;
  173. INIT_WORK(&info->play_work, vibra_play_work);
  174. info->input_dev = input_allocate_device();
  175. if (info->input_dev == NULL) {
  176. dev_err(&pdev->dev, "couldn't allocate input device\n");
  177. ret = -ENOMEM;
  178. goto err_kzalloc;
  179. }
  180. input_set_drvdata(info->input_dev, info);
  181. info->input_dev->name = "twl4030:vibrator";
  182. info->input_dev->id.version = 1;
  183. info->input_dev->dev.parent = pdev->dev.parent;
  184. info->input_dev->open = twl4030_vibra_open;
  185. info->input_dev->close = twl4030_vibra_close;
  186. __set_bit(FF_RUMBLE, info->input_dev->ffbit);
  187. ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
  188. if (ret < 0) {
  189. dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
  190. goto err_ialloc;
  191. }
  192. ret = input_register_device(info->input_dev);
  193. if (ret < 0) {
  194. dev_dbg(&pdev->dev, "couldn't register input device\n");
  195. goto err_iff;
  196. }
  197. vibra_disable_leds();
  198. platform_set_drvdata(pdev, info);
  199. return 0;
  200. err_iff:
  201. input_ff_destroy(info->input_dev);
  202. err_ialloc:
  203. input_free_device(info->input_dev);
  204. err_kzalloc:
  205. kfree(info);
  206. return ret;
  207. }
  208. static int __devexit twl4030_vibra_remove(struct platform_device *pdev)
  209. {
  210. struct vibra_info *info = platform_get_drvdata(pdev);
  211. /* this also free ff-memless and calls close if needed */
  212. input_unregister_device(info->input_dev);
  213. kfree(info);
  214. platform_set_drvdata(pdev, NULL);
  215. return 0;
  216. }
  217. static struct platform_driver twl4030_vibra_driver = {
  218. .probe = twl4030_vibra_probe,
  219. .remove = __devexit_p(twl4030_vibra_remove),
  220. .driver = {
  221. .name = "twl4030-vibra",
  222. .owner = THIS_MODULE,
  223. .pm = &twl4030_vibra_pm_ops,
  224. },
  225. };
  226. module_platform_driver(twl4030_vibra_driver);
  227. MODULE_ALIAS("platform:twl4030-vibra");
  228. MODULE_DESCRIPTION("TWL4030 Vibra driver");
  229. MODULE_LICENSE("GPL");
  230. MODULE_AUTHOR("Nokia Corporation");