twl4030-vibra.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 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_audio_enable_resource(TWL4030_AUDIO_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_audio_enable_resource(TWL4030_AUDIO_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_audio_disable_resource(TWL4030_AUDIO_RES_APLL);
  78. twl4030_audio_disable_resource(TWL4030_AUDIO_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. #ifdef CONFIG_PM_SLEEP
  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. #endif
  158. static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
  159. twl4030_vibra_suspend, twl4030_vibra_resume);
  160. static bool twl4030_vibra_check_coexist(struct twl4030_vibra_data *pdata,
  161. struct device_node *node)
  162. {
  163. if (pdata && pdata->coexist)
  164. return true;
  165. if (of_find_node_by_name(node, "codec"))
  166. return true;
  167. return false;
  168. }
  169. static int twl4030_vibra_probe(struct platform_device *pdev)
  170. {
  171. struct twl4030_vibra_data *pdata = pdev->dev.platform_data;
  172. struct device_node *twl4030_core_node = pdev->dev.parent->of_node;
  173. struct vibra_info *info;
  174. int ret;
  175. if (!pdata && !twl4030_core_node) {
  176. dev_dbg(&pdev->dev, "platform_data not available\n");
  177. return -EINVAL;
  178. }
  179. info = kzalloc(sizeof(*info), GFP_KERNEL);
  180. if (!info)
  181. return -ENOMEM;
  182. info->dev = &pdev->dev;
  183. info->coexist = twl4030_vibra_check_coexist(pdata, twl4030_core_node);
  184. INIT_WORK(&info->play_work, vibra_play_work);
  185. info->input_dev = input_allocate_device();
  186. if (info->input_dev == NULL) {
  187. dev_err(&pdev->dev, "couldn't allocate input device\n");
  188. ret = -ENOMEM;
  189. goto err_kzalloc;
  190. }
  191. input_set_drvdata(info->input_dev, info);
  192. info->input_dev->name = "twl4030:vibrator";
  193. info->input_dev->id.version = 1;
  194. info->input_dev->dev.parent = pdev->dev.parent;
  195. info->input_dev->open = twl4030_vibra_open;
  196. info->input_dev->close = twl4030_vibra_close;
  197. __set_bit(FF_RUMBLE, info->input_dev->ffbit);
  198. ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
  199. if (ret < 0) {
  200. dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
  201. goto err_ialloc;
  202. }
  203. ret = input_register_device(info->input_dev);
  204. if (ret < 0) {
  205. dev_dbg(&pdev->dev, "couldn't register input device\n");
  206. goto err_iff;
  207. }
  208. vibra_disable_leds();
  209. platform_set_drvdata(pdev, info);
  210. return 0;
  211. err_iff:
  212. input_ff_destroy(info->input_dev);
  213. err_ialloc:
  214. input_free_device(info->input_dev);
  215. err_kzalloc:
  216. kfree(info);
  217. return ret;
  218. }
  219. static int twl4030_vibra_remove(struct platform_device *pdev)
  220. {
  221. struct vibra_info *info = platform_get_drvdata(pdev);
  222. /* this also free ff-memless and calls close if needed */
  223. input_unregister_device(info->input_dev);
  224. kfree(info);
  225. platform_set_drvdata(pdev, NULL);
  226. return 0;
  227. }
  228. static struct platform_driver twl4030_vibra_driver = {
  229. .probe = twl4030_vibra_probe,
  230. .remove = twl4030_vibra_remove,
  231. .driver = {
  232. .name = "twl4030-vibra",
  233. .owner = THIS_MODULE,
  234. .pm = &twl4030_vibra_pm_ops,
  235. },
  236. };
  237. module_platform_driver(twl4030_vibra_driver);
  238. MODULE_ALIAS("platform:twl4030-vibra");
  239. MODULE_DESCRIPTION("TWL4030 Vibra driver");
  240. MODULE_LICENSE("GPL");
  241. MODULE_AUTHOR("Nokia Corporation");