twl6040-vibra.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/of.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/input.h>
  33. #include <linux/mfd/twl6040.h>
  34. #include <linux/slab.h>
  35. #include <linux/delay.h>
  36. #include <linux/regulator/consumer.h>
  37. #define EFFECT_DIR_180_DEG 0x8000
  38. /* Recommended modulation index 85% */
  39. #define TWL6040_VIBRA_MOD 85
  40. #define TWL6040_NUM_SUPPLIES 2
  41. struct vibra_info {
  42. struct device *dev;
  43. struct input_dev *input_dev;
  44. struct workqueue_struct *workqueue;
  45. struct work_struct play_work;
  46. struct mutex mutex;
  47. int irq;
  48. bool enabled;
  49. int weak_speed;
  50. int strong_speed;
  51. int direction;
  52. unsigned int vibldrv_res;
  53. unsigned int vibrdrv_res;
  54. unsigned int viblmotor_res;
  55. unsigned int vibrmotor_res;
  56. struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES];
  57. struct twl6040 *twl6040;
  58. };
  59. static irqreturn_t twl6040_vib_irq_handler(int irq, void *data)
  60. {
  61. struct vibra_info *info = data;
  62. struct twl6040 *twl6040 = info->twl6040;
  63. u8 status;
  64. status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
  65. if (status & TWL6040_VIBLOCDET) {
  66. dev_warn(info->dev, "Left Vibrator overcurrent detected\n");
  67. twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL,
  68. TWL6040_VIBENA);
  69. }
  70. if (status & TWL6040_VIBROCDET) {
  71. dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
  72. twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
  73. TWL6040_VIBENA);
  74. }
  75. return IRQ_HANDLED;
  76. }
  77. static void twl6040_vibra_enable(struct vibra_info *info)
  78. {
  79. struct twl6040 *twl6040 = info->twl6040;
  80. int ret;
  81. ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies);
  82. if (ret) {
  83. dev_err(info->dev, "failed to enable regulators %d\n", ret);
  84. return;
  85. }
  86. twl6040_power(info->twl6040, 1);
  87. if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) {
  88. /*
  89. * ERRATA: Disable overcurrent protection for at least
  90. * 3ms when enabling vibrator drivers to avoid false
  91. * overcurrent detection
  92. */
  93. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
  94. TWL6040_VIBENA | TWL6040_VIBCTRL);
  95. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
  96. TWL6040_VIBENA | TWL6040_VIBCTRL);
  97. usleep_range(3000, 3500);
  98. }
  99. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
  100. TWL6040_VIBENA);
  101. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
  102. TWL6040_VIBENA);
  103. info->enabled = true;
  104. }
  105. static void twl6040_vibra_disable(struct vibra_info *info)
  106. {
  107. struct twl6040 *twl6040 = info->twl6040;
  108. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00);
  109. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00);
  110. twl6040_power(info->twl6040, 0);
  111. regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies);
  112. info->enabled = false;
  113. }
  114. static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res,
  115. int speed, int direction)
  116. {
  117. int vpk, max_code;
  118. u8 vibdat;
  119. /* output swing */
  120. vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) /
  121. (100 * (vibdrv_res + motor_res));
  122. /* 50mV per VIBDAT code step */
  123. max_code = vpk / 50;
  124. if (max_code > TWL6040_VIBDAT_MAX)
  125. max_code = TWL6040_VIBDAT_MAX;
  126. /* scale speed to max allowed code */
  127. vibdat = (u8)((speed * max_code) / USHRT_MAX);
  128. /* 2's complement for direction > 180 degrees */
  129. vibdat *= direction;
  130. return vibdat;
  131. }
  132. static void twl6040_vibra_set_effect(struct vibra_info *info)
  133. {
  134. struct twl6040 *twl6040 = info->twl6040;
  135. u8 vibdatl, vibdatr;
  136. int volt;
  137. /* weak motor */
  138. volt = regulator_get_voltage(info->supplies[0].consumer) / 1000;
  139. vibdatl = twl6040_vibra_code(volt, info->vibldrv_res,
  140. info->viblmotor_res,
  141. info->weak_speed, info->direction);
  142. /* strong motor */
  143. volt = regulator_get_voltage(info->supplies[1].consumer) / 1000;
  144. vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res,
  145. info->vibrmotor_res,
  146. info->strong_speed, info->direction);
  147. twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl);
  148. twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr);
  149. }
  150. static void vibra_play_work(struct work_struct *work)
  151. {
  152. struct vibra_info *info = container_of(work,
  153. struct vibra_info, play_work);
  154. mutex_lock(&info->mutex);
  155. if (info->weak_speed || info->strong_speed) {
  156. if (!info->enabled)
  157. twl6040_vibra_enable(info);
  158. twl6040_vibra_set_effect(info);
  159. } else if (info->enabled)
  160. twl6040_vibra_disable(info);
  161. mutex_unlock(&info->mutex);
  162. }
  163. static int vibra_play(struct input_dev *input, void *data,
  164. struct ff_effect *effect)
  165. {
  166. struct vibra_info *info = input_get_drvdata(input);
  167. int ret;
  168. /* Do not allow effect, while the routing is set to use audio */
  169. ret = twl6040_get_vibralr_status(info->twl6040);
  170. if (ret & TWL6040_VIBSEL) {
  171. dev_info(&input->dev, "Vibra is configured for audio\n");
  172. return -EBUSY;
  173. }
  174. info->weak_speed = effect->u.rumble.weak_magnitude;
  175. info->strong_speed = effect->u.rumble.strong_magnitude;
  176. info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
  177. ret = queue_work(info->workqueue, &info->play_work);
  178. if (!ret) {
  179. dev_info(&input->dev, "work is already on queue\n");
  180. return ret;
  181. }
  182. return 0;
  183. }
  184. static void twl6040_vibra_close(struct input_dev *input)
  185. {
  186. struct vibra_info *info = input_get_drvdata(input);
  187. cancel_work_sync(&info->play_work);
  188. mutex_lock(&info->mutex);
  189. if (info->enabled)
  190. twl6040_vibra_disable(info);
  191. mutex_unlock(&info->mutex);
  192. }
  193. #ifdef CONFIG_PM_SLEEP
  194. static int twl6040_vibra_suspend(struct device *dev)
  195. {
  196. struct platform_device *pdev = to_platform_device(dev);
  197. struct vibra_info *info = platform_get_drvdata(pdev);
  198. mutex_lock(&info->mutex);
  199. if (info->enabled)
  200. twl6040_vibra_disable(info);
  201. mutex_unlock(&info->mutex);
  202. return 0;
  203. }
  204. #endif
  205. static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
  206. static int __devinit twl6040_vibra_probe(struct platform_device *pdev)
  207. {
  208. struct twl6040_vibra_data *pdata = pdev->dev.platform_data;
  209. struct device_node *node = pdev->dev.of_node;
  210. struct vibra_info *info;
  211. int vddvibl_uV = 0;
  212. int vddvibr_uV = 0;
  213. int ret;
  214. if (!pdata && !node) {
  215. dev_err(&pdev->dev, "platform_data not available\n");
  216. return -EINVAL;
  217. }
  218. info = kzalloc(sizeof(*info), GFP_KERNEL);
  219. if (!info) {
  220. dev_err(&pdev->dev, "couldn't allocate memory\n");
  221. return -ENOMEM;
  222. }
  223. info->dev = &pdev->dev;
  224. info->twl6040 = dev_get_drvdata(pdev->dev.parent);
  225. if (pdata) {
  226. info->vibldrv_res = pdata->vibldrv_res;
  227. info->vibrdrv_res = pdata->vibrdrv_res;
  228. info->viblmotor_res = pdata->viblmotor_res;
  229. info->vibrmotor_res = pdata->vibrmotor_res;
  230. vddvibl_uV = pdata->vddvibl_uV;
  231. vddvibr_uV = pdata->vddvibr_uV;
  232. } else {
  233. of_property_read_u32(node, "vibldrv_res", &info->vibldrv_res);
  234. of_property_read_u32(node, "vibrdrv_res", &info->vibrdrv_res);
  235. of_property_read_u32(node, "viblmotor_res",
  236. &info->viblmotor_res);
  237. of_property_read_u32(node, "vibrmotor_res",
  238. &info->vibrmotor_res);
  239. of_property_read_u32(node, "vddvibl_uV", &vddvibl_uV);
  240. of_property_read_u32(node, "vddvibr_uV", &vddvibr_uV);
  241. }
  242. if ((!info->vibldrv_res && !info->viblmotor_res) ||
  243. (!info->vibrdrv_res && !info->vibrmotor_res)) {
  244. dev_err(info->dev, "invalid vibra driver/motor resistance\n");
  245. ret = -EINVAL;
  246. goto err_kzalloc;
  247. }
  248. info->irq = platform_get_irq(pdev, 0);
  249. if (info->irq < 0) {
  250. dev_err(info->dev, "invalid irq\n");
  251. ret = -EINVAL;
  252. goto err_kzalloc;
  253. }
  254. mutex_init(&info->mutex);
  255. info->input_dev = input_allocate_device();
  256. if (info->input_dev == NULL) {
  257. dev_err(info->dev, "couldn't allocate input device\n");
  258. ret = -ENOMEM;
  259. goto err_kzalloc;
  260. }
  261. input_set_drvdata(info->input_dev, info);
  262. info->input_dev->name = "twl6040:vibrator";
  263. info->input_dev->id.version = 1;
  264. info->input_dev->dev.parent = pdev->dev.parent;
  265. info->input_dev->close = twl6040_vibra_close;
  266. __set_bit(FF_RUMBLE, info->input_dev->ffbit);
  267. ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
  268. if (ret < 0) {
  269. dev_err(info->dev, "couldn't register vibrator to FF\n");
  270. goto err_ialloc;
  271. }
  272. ret = input_register_device(info->input_dev);
  273. if (ret < 0) {
  274. dev_err(info->dev, "couldn't register input device\n");
  275. goto err_iff;
  276. }
  277. platform_set_drvdata(pdev, info);
  278. ret = request_threaded_irq(info->irq, NULL, twl6040_vib_irq_handler, 0,
  279. "twl6040_irq_vib", info);
  280. if (ret) {
  281. dev_err(info->dev, "VIB IRQ request failed: %d\n", ret);
  282. goto err_irq;
  283. }
  284. info->supplies[0].supply = "vddvibl";
  285. info->supplies[1].supply = "vddvibr";
  286. ret = regulator_bulk_get(info->dev, ARRAY_SIZE(info->supplies),
  287. info->supplies);
  288. if (ret) {
  289. dev_err(info->dev, "couldn't get regulators %d\n", ret);
  290. goto err_regulator;
  291. }
  292. if (vddvibl_uV) {
  293. ret = regulator_set_voltage(info->supplies[0].consumer,
  294. vddvibl_uV, vddvibl_uV);
  295. if (ret) {
  296. dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
  297. ret);
  298. goto err_voltage;
  299. }
  300. }
  301. if (vddvibr_uV) {
  302. ret = regulator_set_voltage(info->supplies[1].consumer,
  303. vddvibr_uV, vddvibr_uV);
  304. if (ret) {
  305. dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
  306. ret);
  307. goto err_voltage;
  308. }
  309. }
  310. info->workqueue = alloc_workqueue("twl6040-vibra", 0, 0);
  311. if (info->workqueue == NULL) {
  312. dev_err(info->dev, "couldn't create workqueue\n");
  313. ret = -ENOMEM;
  314. goto err_voltage;
  315. }
  316. INIT_WORK(&info->play_work, vibra_play_work);
  317. return 0;
  318. err_voltage:
  319. regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
  320. err_regulator:
  321. free_irq(info->irq, info);
  322. err_irq:
  323. input_unregister_device(info->input_dev);
  324. info->input_dev = NULL;
  325. err_iff:
  326. if (info->input_dev)
  327. input_ff_destroy(info->input_dev);
  328. err_ialloc:
  329. input_free_device(info->input_dev);
  330. err_kzalloc:
  331. kfree(info);
  332. return ret;
  333. }
  334. static int __devexit twl6040_vibra_remove(struct platform_device *pdev)
  335. {
  336. struct vibra_info *info = platform_get_drvdata(pdev);
  337. input_unregister_device(info->input_dev);
  338. free_irq(info->irq, info);
  339. regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
  340. destroy_workqueue(info->workqueue);
  341. kfree(info);
  342. return 0;
  343. }
  344. static const struct of_device_id twl6040_vibra_of_match[] = {
  345. {.compatible = "ti,twl6040-vibra", },
  346. { },
  347. };
  348. MODULE_DEVICE_TABLE(of, twl6040_vibra_of_match);
  349. static struct platform_driver twl6040_vibra_driver = {
  350. .probe = twl6040_vibra_probe,
  351. .remove = __devexit_p(twl6040_vibra_remove),
  352. .driver = {
  353. .name = "twl6040-vibra",
  354. .owner = THIS_MODULE,
  355. .pm = &twl6040_vibra_pm_ops,
  356. .of_match_table = twl6040_vibra_of_match,
  357. },
  358. };
  359. module_platform_driver(twl6040_vibra_driver);
  360. MODULE_ALIAS("platform:twl6040-vibra");
  361. MODULE_DESCRIPTION("TWL6040 Vibra driver");
  362. MODULE_LICENSE("GPL");
  363. MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
  364. MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");