twl6040-vibra.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * twl6040-vibra.c - TWL6040 Vibrator driver
  3. *
  4. * Author: Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
  5. * Author: Misael Lopez Cruz <misael.lopez@ti.com>
  6. *
  7. * Copyright: (C) 2011 Texas Instruments, Inc.
  8. *
  9. * Based on twl4030-vibra.c by Henrik Saari <henrik.saari@nokia.com>
  10. * Felipe Balbi <felipe.balbi@nokia.com>
  11. * Jari Vanhala <ext-javi.vanhala@nokia.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  25. * 02110-1301 USA
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/i2c/twl.h>
  32. #include <linux/mfd/twl6040.h>
  33. #include <linux/slab.h>
  34. #include <linux/delay.h>
  35. #include <linux/regulator/consumer.h>
  36. #define EFFECT_DIR_180_DEG 0x8000
  37. /* Recommended modulation index 85% */
  38. #define TWL6040_VIBRA_MOD 85
  39. #define TWL6040_NUM_SUPPLIES 2
  40. struct vibra_info {
  41. struct device *dev;
  42. struct input_dev *input_dev;
  43. struct workqueue_struct *workqueue;
  44. struct work_struct play_work;
  45. struct mutex mutex;
  46. int irq;
  47. bool enabled;
  48. int weak_speed;
  49. int strong_speed;
  50. int direction;
  51. unsigned int vibldrv_res;
  52. unsigned int vibrdrv_res;
  53. unsigned int viblmotor_res;
  54. unsigned int vibrmotor_res;
  55. struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES];
  56. struct twl6040 *twl6040;
  57. };
  58. static irqreturn_t twl6040_vib_irq_handler(int irq, void *data)
  59. {
  60. struct vibra_info *info = data;
  61. struct twl6040 *twl6040 = info->twl6040;
  62. u8 status;
  63. status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
  64. if (status & TWL6040_VIBLOCDET) {
  65. dev_warn(info->dev, "Left Vibrator overcurrent detected\n");
  66. twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL,
  67. TWL6040_VIBENAL);
  68. }
  69. if (status & TWL6040_VIBROCDET) {
  70. dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
  71. twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
  72. TWL6040_VIBENAR);
  73. }
  74. return IRQ_HANDLED;
  75. }
  76. static void twl6040_vibra_enable(struct vibra_info *info)
  77. {
  78. struct twl6040 *twl6040 = info->twl6040;
  79. int ret;
  80. ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies);
  81. if (ret) {
  82. dev_err(info->dev, "failed to enable regulators %d\n", ret);
  83. return;
  84. }
  85. twl6040_power(info->twl6040, 1);
  86. if (twl6040->rev <= TWL6040_REV_ES1_1) {
  87. /*
  88. * ERRATA: Disable overcurrent protection for at least
  89. * 3ms when enabling vibrator drivers to avoid false
  90. * overcurrent detection
  91. */
  92. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
  93. TWL6040_VIBENAL | TWL6040_VIBCTRLL);
  94. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
  95. TWL6040_VIBENAR | TWL6040_VIBCTRLR);
  96. usleep_range(3000, 3500);
  97. }
  98. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
  99. TWL6040_VIBENAL);
  100. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
  101. TWL6040_VIBENAR);
  102. info->enabled = true;
  103. }
  104. static void twl6040_vibra_disable(struct vibra_info *info)
  105. {
  106. struct twl6040 *twl6040 = info->twl6040;
  107. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00);
  108. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00);
  109. twl6040_power(info->twl6040, 0);
  110. regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies);
  111. info->enabled = false;
  112. }
  113. static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res,
  114. int speed, int direction)
  115. {
  116. int vpk, max_code;
  117. u8 vibdat;
  118. /* output swing */
  119. vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) /
  120. (100 * (vibdrv_res + motor_res));
  121. /* 50mV per VIBDAT code step */
  122. max_code = vpk / 50;
  123. if (max_code > TWL6040_VIBDAT_MAX)
  124. max_code = TWL6040_VIBDAT_MAX;
  125. /* scale speed to max allowed code */
  126. vibdat = (u8)((speed * max_code) / USHRT_MAX);
  127. /* 2's complement for direction > 180 degrees */
  128. vibdat *= direction;
  129. return vibdat;
  130. }
  131. static void twl6040_vibra_set_effect(struct vibra_info *info)
  132. {
  133. struct twl6040 *twl6040 = info->twl6040;
  134. u8 vibdatl, vibdatr;
  135. int volt;
  136. /* weak motor */
  137. volt = regulator_get_voltage(info->supplies[0].consumer) / 1000;
  138. vibdatl = twl6040_vibra_code(volt, info->vibldrv_res,
  139. info->viblmotor_res,
  140. info->weak_speed, info->direction);
  141. /* strong motor */
  142. volt = regulator_get_voltage(info->supplies[1].consumer) / 1000;
  143. vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res,
  144. info->vibrmotor_res,
  145. info->strong_speed, info->direction);
  146. twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl);
  147. twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr);
  148. }
  149. static void vibra_play_work(struct work_struct *work)
  150. {
  151. struct vibra_info *info = container_of(work,
  152. struct vibra_info, play_work);
  153. mutex_lock(&info->mutex);
  154. if (info->weak_speed || info->strong_speed) {
  155. if (!info->enabled)
  156. twl6040_vibra_enable(info);
  157. twl6040_vibra_set_effect(info);
  158. } else if (info->enabled)
  159. twl6040_vibra_disable(info);
  160. mutex_unlock(&info->mutex);
  161. }
  162. static int vibra_play(struct input_dev *input, void *data,
  163. struct ff_effect *effect)
  164. {
  165. struct vibra_info *info = input_get_drvdata(input);
  166. int ret;
  167. info->weak_speed = effect->u.rumble.weak_magnitude;
  168. info->strong_speed = effect->u.rumble.strong_magnitude;
  169. info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
  170. ret = queue_work(info->workqueue, &info->play_work);
  171. if (!ret) {
  172. dev_info(&input->dev, "work is already on queue\n");
  173. return ret;
  174. }
  175. return 0;
  176. }
  177. static void twl6040_vibra_close(struct input_dev *input)
  178. {
  179. struct vibra_info *info = input_get_drvdata(input);
  180. cancel_work_sync(&info->play_work);
  181. mutex_lock(&info->mutex);
  182. if (info->enabled)
  183. twl6040_vibra_disable(info);
  184. mutex_unlock(&info->mutex);
  185. }
  186. #if CONFIG_PM_SLEEP
  187. static int twl6040_vibra_suspend(struct device *dev)
  188. {
  189. struct platform_device *pdev = to_platform_device(dev);
  190. struct vibra_info *info = platform_get_drvdata(pdev);
  191. mutex_lock(&info->mutex);
  192. if (info->enabled)
  193. twl6040_vibra_disable(info);
  194. mutex_unlock(&info->mutex);
  195. return 0;
  196. }
  197. #endif
  198. static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
  199. static int __devinit twl6040_vibra_probe(struct platform_device *pdev)
  200. {
  201. struct twl4030_vibra_data *pdata = pdev->dev.platform_data;
  202. struct vibra_info *info;
  203. int ret;
  204. if (!pdata) {
  205. dev_err(&pdev->dev, "platform_data not available\n");
  206. return -EINVAL;
  207. }
  208. info = kzalloc(sizeof(*info), GFP_KERNEL);
  209. if (!info) {
  210. dev_err(&pdev->dev, "couldn't allocate memory\n");
  211. return -ENOMEM;
  212. }
  213. info->dev = &pdev->dev;
  214. info->twl6040 = dev_get_drvdata(pdev->dev.parent);
  215. info->vibldrv_res = pdata->vibldrv_res;
  216. info->vibrdrv_res = pdata->vibrdrv_res;
  217. info->viblmotor_res = pdata->viblmotor_res;
  218. info->vibrmotor_res = pdata->vibrmotor_res;
  219. if ((!info->vibldrv_res && !info->viblmotor_res) ||
  220. (!info->vibrdrv_res && !info->vibrmotor_res)) {
  221. dev_err(info->dev, "invalid vibra driver/motor resistance\n");
  222. ret = -EINVAL;
  223. goto err_kzalloc;
  224. }
  225. info->irq = platform_get_irq(pdev, 0);
  226. if (info->irq < 0) {
  227. dev_err(info->dev, "invalid irq\n");
  228. ret = -EINVAL;
  229. goto err_kzalloc;
  230. }
  231. mutex_init(&info->mutex);
  232. info->input_dev = input_allocate_device();
  233. if (info->input_dev == NULL) {
  234. dev_err(info->dev, "couldn't allocate input device\n");
  235. ret = -ENOMEM;
  236. goto err_kzalloc;
  237. }
  238. input_set_drvdata(info->input_dev, info);
  239. info->input_dev->name = "twl6040:vibrator";
  240. info->input_dev->id.version = 1;
  241. info->input_dev->dev.parent = pdev->dev.parent;
  242. info->input_dev->close = twl6040_vibra_close;
  243. __set_bit(FF_RUMBLE, info->input_dev->ffbit);
  244. ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
  245. if (ret < 0) {
  246. dev_err(info->dev, "couldn't register vibrator to FF\n");
  247. goto err_ialloc;
  248. }
  249. ret = input_register_device(info->input_dev);
  250. if (ret < 0) {
  251. dev_err(info->dev, "couldn't register input device\n");
  252. goto err_iff;
  253. }
  254. platform_set_drvdata(pdev, info);
  255. ret = request_threaded_irq(info->irq, NULL, twl6040_vib_irq_handler, 0,
  256. "twl6040_irq_vib", info);
  257. if (ret) {
  258. dev_err(info->dev, "VIB IRQ request failed: %d\n", ret);
  259. goto err_irq;
  260. }
  261. info->supplies[0].supply = "vddvibl";
  262. info->supplies[1].supply = "vddvibr";
  263. ret = regulator_bulk_get(info->dev, ARRAY_SIZE(info->supplies),
  264. info->supplies);
  265. if (ret) {
  266. dev_err(info->dev, "couldn't get regulators %d\n", ret);
  267. goto err_regulator;
  268. }
  269. if (pdata->vddvibl_uV) {
  270. ret = regulator_set_voltage(info->supplies[0].consumer,
  271. pdata->vddvibl_uV,
  272. pdata->vddvibl_uV);
  273. if (ret) {
  274. dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
  275. ret);
  276. goto err_voltage;
  277. }
  278. }
  279. if (pdata->vddvibr_uV) {
  280. ret = regulator_set_voltage(info->supplies[1].consumer,
  281. pdata->vddvibr_uV,
  282. pdata->vddvibr_uV);
  283. if (ret) {
  284. dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
  285. ret);
  286. goto err_voltage;
  287. }
  288. }
  289. info->workqueue = alloc_workqueue("twl6040-vibra", 0, 0);
  290. if (info->workqueue == NULL) {
  291. dev_err(info->dev, "couldn't create workqueue\n");
  292. ret = -ENOMEM;
  293. goto err_voltage;
  294. }
  295. INIT_WORK(&info->play_work, vibra_play_work);
  296. return 0;
  297. err_voltage:
  298. regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
  299. err_regulator:
  300. free_irq(info->irq, info);
  301. err_irq:
  302. input_unregister_device(info->input_dev);
  303. info->input_dev = NULL;
  304. err_iff:
  305. if (info->input_dev)
  306. input_ff_destroy(info->input_dev);
  307. err_ialloc:
  308. input_free_device(info->input_dev);
  309. err_kzalloc:
  310. kfree(info);
  311. return ret;
  312. }
  313. static int __devexit twl6040_vibra_remove(struct platform_device *pdev)
  314. {
  315. struct vibra_info *info = platform_get_drvdata(pdev);
  316. input_unregister_device(info->input_dev);
  317. free_irq(info->irq, info);
  318. regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
  319. destroy_workqueue(info->workqueue);
  320. kfree(info);
  321. return 0;
  322. }
  323. static struct platform_driver twl6040_vibra_driver = {
  324. .probe = twl6040_vibra_probe,
  325. .remove = __devexit_p(twl6040_vibra_remove),
  326. .driver = {
  327. .name = "twl6040-vibra",
  328. .owner = THIS_MODULE,
  329. .pm = &twl6040_vibra_pm_ops,
  330. },
  331. };
  332. static int __init twl6040_vibra_init(void)
  333. {
  334. return platform_driver_register(&twl6040_vibra_driver);
  335. }
  336. module_init(twl6040_vibra_init);
  337. static void __exit twl6040_vibra_exit(void)
  338. {
  339. platform_driver_unregister(&twl6040_vibra_driver);
  340. }
  341. module_exit(twl6040_vibra_exit);
  342. MODULE_ALIAS("platform:twl6040-vibra");
  343. MODULE_DESCRIPTION("TWL6040 Vibra driver");
  344. MODULE_LICENSE("GPL");
  345. MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
  346. MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");