twl6040-vibra.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * twl6040-vibra.c - TWL6040 Vibrator driver
  4. *
  5. * Author: Jorge Eduardo Candelaria <[email protected]>
  6. * Author: Misael Lopez Cruz <[email protected]>
  7. *
  8. * Copyright: (C) 2011 Texas Instruments, Inc.
  9. *
  10. * Based on twl4030-vibra.c by Henrik Saari <[email protected]>
  11. * Felipe Balbi <[email protected]>
  12. * Jari Vanhala <[email protected]>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/input.h>
  19. #include <linux/mfd/twl6040.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/regulator/consumer.h>
  23. #define EFFECT_DIR_180_DEG 0x8000
  24. /* Recommended modulation index 85% */
  25. #define TWL6040_VIBRA_MOD 85
  26. #define TWL6040_NUM_SUPPLIES 2
  27. struct vibra_info {
  28. struct device *dev;
  29. struct input_dev *input_dev;
  30. struct work_struct play_work;
  31. int irq;
  32. bool enabled;
  33. int weak_speed;
  34. int strong_speed;
  35. int direction;
  36. unsigned int vibldrv_res;
  37. unsigned int vibrdrv_res;
  38. unsigned int viblmotor_res;
  39. unsigned int vibrmotor_res;
  40. struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES];
  41. struct twl6040 *twl6040;
  42. };
  43. static irqreturn_t twl6040_vib_irq_handler(int irq, void *data)
  44. {
  45. struct vibra_info *info = data;
  46. struct twl6040 *twl6040 = info->twl6040;
  47. u8 status;
  48. status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
  49. if (status & TWL6040_VIBLOCDET) {
  50. dev_warn(info->dev, "Left Vibrator overcurrent detected\n");
  51. twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL,
  52. TWL6040_VIBENA);
  53. }
  54. if (status & TWL6040_VIBROCDET) {
  55. dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
  56. twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
  57. TWL6040_VIBENA);
  58. }
  59. return IRQ_HANDLED;
  60. }
  61. static void twl6040_vibra_enable(struct vibra_info *info)
  62. {
  63. struct twl6040 *twl6040 = info->twl6040;
  64. int ret;
  65. ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies);
  66. if (ret) {
  67. dev_err(info->dev, "failed to enable regulators %d\n", ret);
  68. return;
  69. }
  70. twl6040_power(info->twl6040, 1);
  71. if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) {
  72. /*
  73. * ERRATA: Disable overcurrent protection for at least
  74. * 3ms when enabling vibrator drivers to avoid false
  75. * overcurrent detection
  76. */
  77. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
  78. TWL6040_VIBENA | TWL6040_VIBCTRL);
  79. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
  80. TWL6040_VIBENA | TWL6040_VIBCTRL);
  81. usleep_range(3000, 3500);
  82. }
  83. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
  84. TWL6040_VIBENA);
  85. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
  86. TWL6040_VIBENA);
  87. info->enabled = true;
  88. }
  89. static void twl6040_vibra_disable(struct vibra_info *info)
  90. {
  91. struct twl6040 *twl6040 = info->twl6040;
  92. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00);
  93. twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00);
  94. twl6040_power(info->twl6040, 0);
  95. regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies);
  96. info->enabled = false;
  97. }
  98. static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res,
  99. int speed, int direction)
  100. {
  101. int vpk, max_code;
  102. u8 vibdat;
  103. /* output swing */
  104. vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) /
  105. (100 * (vibdrv_res + motor_res));
  106. /* 50mV per VIBDAT code step */
  107. max_code = vpk / 50;
  108. if (max_code > TWL6040_VIBDAT_MAX)
  109. max_code = TWL6040_VIBDAT_MAX;
  110. /* scale speed to max allowed code */
  111. vibdat = (u8)((speed * max_code) / USHRT_MAX);
  112. /* 2's complement for direction > 180 degrees */
  113. vibdat *= direction;
  114. return vibdat;
  115. }
  116. static void twl6040_vibra_set_effect(struct vibra_info *info)
  117. {
  118. struct twl6040 *twl6040 = info->twl6040;
  119. u8 vibdatl, vibdatr;
  120. int volt;
  121. /* weak motor */
  122. volt = regulator_get_voltage(info->supplies[0].consumer) / 1000;
  123. vibdatl = twl6040_vibra_code(volt, info->vibldrv_res,
  124. info->viblmotor_res,
  125. info->weak_speed, info->direction);
  126. /* strong motor */
  127. volt = regulator_get_voltage(info->supplies[1].consumer) / 1000;
  128. vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res,
  129. info->vibrmotor_res,
  130. info->strong_speed, info->direction);
  131. twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl);
  132. twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr);
  133. }
  134. static void vibra_play_work(struct work_struct *work)
  135. {
  136. struct vibra_info *info = container_of(work,
  137. struct vibra_info, play_work);
  138. int ret;
  139. /* Do not allow effect, while the routing is set to use audio */
  140. ret = twl6040_get_vibralr_status(info->twl6040);
  141. if (ret & TWL6040_VIBSEL) {
  142. dev_info(info->dev, "Vibra is configured for audio\n");
  143. return;
  144. }
  145. if (info->weak_speed || info->strong_speed) {
  146. if (!info->enabled)
  147. twl6040_vibra_enable(info);
  148. twl6040_vibra_set_effect(info);
  149. } else if (info->enabled)
  150. twl6040_vibra_disable(info);
  151. }
  152. static int vibra_play(struct input_dev *input, void *data,
  153. struct ff_effect *effect)
  154. {
  155. struct vibra_info *info = input_get_drvdata(input);
  156. info->weak_speed = effect->u.rumble.weak_magnitude;
  157. info->strong_speed = effect->u.rumble.strong_magnitude;
  158. info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
  159. schedule_work(&info->play_work);
  160. return 0;
  161. }
  162. static void twl6040_vibra_close(struct input_dev *input)
  163. {
  164. struct vibra_info *info = input_get_drvdata(input);
  165. cancel_work_sync(&info->play_work);
  166. if (info->enabled)
  167. twl6040_vibra_disable(info);
  168. }
  169. static int __maybe_unused twl6040_vibra_suspend(struct device *dev)
  170. {
  171. struct platform_device *pdev = to_platform_device(dev);
  172. struct vibra_info *info = platform_get_drvdata(pdev);
  173. cancel_work_sync(&info->play_work);
  174. if (info->enabled)
  175. twl6040_vibra_disable(info);
  176. return 0;
  177. }
  178. static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
  179. static int twl6040_vibra_probe(struct platform_device *pdev)
  180. {
  181. struct device *twl6040_core_dev = pdev->dev.parent;
  182. struct device_node *twl6040_core_node;
  183. struct vibra_info *info;
  184. int vddvibl_uV = 0;
  185. int vddvibr_uV = 0;
  186. int error;
  187. twl6040_core_node = of_get_child_by_name(twl6040_core_dev->of_node,
  188. "vibra");
  189. if (!twl6040_core_node) {
  190. dev_err(&pdev->dev, "parent of node is missing?\n");
  191. return -EINVAL;
  192. }
  193. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  194. if (!info) {
  195. of_node_put(twl6040_core_node);
  196. dev_err(&pdev->dev, "couldn't allocate memory\n");
  197. return -ENOMEM;
  198. }
  199. info->dev = &pdev->dev;
  200. info->twl6040 = dev_get_drvdata(pdev->dev.parent);
  201. of_property_read_u32(twl6040_core_node, "ti,vibldrv-res",
  202. &info->vibldrv_res);
  203. of_property_read_u32(twl6040_core_node, "ti,vibrdrv-res",
  204. &info->vibrdrv_res);
  205. of_property_read_u32(twl6040_core_node, "ti,viblmotor-res",
  206. &info->viblmotor_res);
  207. of_property_read_u32(twl6040_core_node, "ti,vibrmotor-res",
  208. &info->vibrmotor_res);
  209. of_property_read_u32(twl6040_core_node, "ti,vddvibl-uV", &vddvibl_uV);
  210. of_property_read_u32(twl6040_core_node, "ti,vddvibr-uV", &vddvibr_uV);
  211. of_node_put(twl6040_core_node);
  212. if ((!info->vibldrv_res && !info->viblmotor_res) ||
  213. (!info->vibrdrv_res && !info->vibrmotor_res)) {
  214. dev_err(info->dev, "invalid vibra driver/motor resistance\n");
  215. return -EINVAL;
  216. }
  217. info->irq = platform_get_irq(pdev, 0);
  218. if (info->irq < 0)
  219. return -EINVAL;
  220. error = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
  221. twl6040_vib_irq_handler,
  222. IRQF_ONESHOT,
  223. "twl6040_irq_vib", info);
  224. if (error) {
  225. dev_err(info->dev, "VIB IRQ request failed: %d\n", error);
  226. return error;
  227. }
  228. info->supplies[0].supply = "vddvibl";
  229. info->supplies[1].supply = "vddvibr";
  230. /*
  231. * When booted with Device tree the regulators are attached to the
  232. * parent device (twl6040 MFD core)
  233. */
  234. error = devm_regulator_bulk_get(twl6040_core_dev,
  235. ARRAY_SIZE(info->supplies),
  236. info->supplies);
  237. if (error) {
  238. dev_err(info->dev, "couldn't get regulators %d\n", error);
  239. return error;
  240. }
  241. if (vddvibl_uV) {
  242. error = regulator_set_voltage(info->supplies[0].consumer,
  243. vddvibl_uV, vddvibl_uV);
  244. if (error) {
  245. dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
  246. error);
  247. return error;
  248. }
  249. }
  250. if (vddvibr_uV) {
  251. error = regulator_set_voltage(info->supplies[1].consumer,
  252. vddvibr_uV, vddvibr_uV);
  253. if (error) {
  254. dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
  255. error);
  256. return error;
  257. }
  258. }
  259. INIT_WORK(&info->play_work, vibra_play_work);
  260. info->input_dev = devm_input_allocate_device(&pdev->dev);
  261. if (!info->input_dev) {
  262. dev_err(info->dev, "couldn't allocate input device\n");
  263. return -ENOMEM;
  264. }
  265. input_set_drvdata(info->input_dev, info);
  266. info->input_dev->name = "twl6040:vibrator";
  267. info->input_dev->id.version = 1;
  268. info->input_dev->close = twl6040_vibra_close;
  269. __set_bit(FF_RUMBLE, info->input_dev->ffbit);
  270. error = input_ff_create_memless(info->input_dev, NULL, vibra_play);
  271. if (error) {
  272. dev_err(info->dev, "couldn't register vibrator to FF\n");
  273. return error;
  274. }
  275. error = input_register_device(info->input_dev);
  276. if (error) {
  277. dev_err(info->dev, "couldn't register input device\n");
  278. return error;
  279. }
  280. platform_set_drvdata(pdev, info);
  281. return 0;
  282. }
  283. static struct platform_driver twl6040_vibra_driver = {
  284. .probe = twl6040_vibra_probe,
  285. .driver = {
  286. .name = "twl6040-vibra",
  287. .pm = &twl6040_vibra_pm_ops,
  288. },
  289. };
  290. module_platform_driver(twl6040_vibra_driver);
  291. MODULE_ALIAS("platform:twl6040-vibra");
  292. MODULE_DESCRIPTION("TWL6040 Vibra driver");
  293. MODULE_LICENSE("GPL");
  294. MODULE_AUTHOR("Jorge Eduardo Candelaria <[email protected]>");
  295. MODULE_AUTHOR("Misael Lopez Cruz <[email protected]>");