max77843-haptic.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * MAXIM MAX77693 Haptic device driver
  3. *
  4. * Copyright (C) 2015 Samsung Electronics
  5. * Author: Jaewon Kim <jaewon02.kim@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/init.h>
  15. #include <linux/input.h>
  16. #include <linux/mfd/max77843-private.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pwm.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/slab.h>
  23. #include <linux/workqueue.h>
  24. #define MAX_MAGNITUDE_SHIFT 16
  25. enum max77843_haptic_motor_type {
  26. MAX77843_HAPTIC_ERM = 0,
  27. MAX77843_HAPTIC_LRA,
  28. };
  29. enum max77843_haptic_pwm_divisor {
  30. MAX77843_HAPTIC_PWM_DIVISOR_32 = 0,
  31. MAX77843_HAPTIC_PWM_DIVISOR_64,
  32. MAX77843_HAPTIC_PWM_DIVISOR_128,
  33. MAX77843_HAPTIC_PWM_DIVISOR_256,
  34. };
  35. struct max77843_haptic {
  36. struct regmap *regmap_haptic;
  37. struct device *dev;
  38. struct input_dev *input_dev;
  39. struct pwm_device *pwm_dev;
  40. struct regulator *motor_reg;
  41. struct work_struct work;
  42. struct mutex mutex;
  43. unsigned int magnitude;
  44. unsigned int pwm_duty;
  45. bool active;
  46. bool suspended;
  47. enum max77843_haptic_motor_type type;
  48. enum max77843_haptic_pwm_divisor pwm_divisor;
  49. };
  50. static int max77843_haptic_set_duty_cycle(struct max77843_haptic *haptic)
  51. {
  52. int delta = (haptic->pwm_dev->period + haptic->pwm_duty) / 2;
  53. int error;
  54. error = pwm_config(haptic->pwm_dev, delta, haptic->pwm_dev->period);
  55. if (error) {
  56. dev_err(haptic->dev, "failed to configure pwm: %d\n", error);
  57. return error;
  58. }
  59. return 0;
  60. }
  61. static int max77843_haptic_bias(struct max77843_haptic *haptic, bool on)
  62. {
  63. int error;
  64. error = regmap_update_bits(haptic->regmap_haptic,
  65. MAX77843_SYS_REG_MAINCTRL1,
  66. MAX77843_MAINCTRL1_BIASEN_MASK,
  67. on << MAINCTRL1_BIASEN_SHIFT);
  68. if (error) {
  69. dev_err(haptic->dev, "failed to %s bias: %d\n",
  70. on ? "enable" : "disable", error);
  71. return error;
  72. }
  73. return 0;
  74. }
  75. static int max77843_haptic_config(struct max77843_haptic *haptic, bool enable)
  76. {
  77. unsigned int value;
  78. int error;
  79. value = (haptic->type << MCONFIG_MODE_SHIFT) |
  80. (enable << MCONFIG_MEN_SHIFT) |
  81. (haptic->pwm_divisor << MCONFIG_PDIV_SHIFT);
  82. error = regmap_write(haptic->regmap_haptic,
  83. MAX77843_HAP_REG_MCONFIG, value);
  84. if (error) {
  85. dev_err(haptic->dev,
  86. "failed to update haptic config: %d\n", error);
  87. return error;
  88. }
  89. return 0;
  90. }
  91. static int max77843_haptic_enable(struct max77843_haptic *haptic)
  92. {
  93. int error;
  94. if (haptic->active)
  95. return 0;
  96. error = pwm_enable(haptic->pwm_dev);
  97. if (error) {
  98. dev_err(haptic->dev,
  99. "failed to enable pwm device: %d\n", error);
  100. return error;
  101. }
  102. error = max77843_haptic_config(haptic, true);
  103. if (error)
  104. goto err_config;
  105. haptic->active = true;
  106. return 0;
  107. err_config:
  108. pwm_disable(haptic->pwm_dev);
  109. return error;
  110. }
  111. static int max77843_haptic_disable(struct max77843_haptic *haptic)
  112. {
  113. int error;
  114. if (!haptic->active)
  115. return 0;
  116. error = max77843_haptic_config(haptic, false);
  117. if (error)
  118. return error;
  119. pwm_disable(haptic->pwm_dev);
  120. haptic->active = false;
  121. return 0;
  122. }
  123. static void max77843_haptic_play_work(struct work_struct *work)
  124. {
  125. struct max77843_haptic *haptic =
  126. container_of(work, struct max77843_haptic, work);
  127. int error;
  128. mutex_lock(&haptic->mutex);
  129. if (haptic->suspended)
  130. goto out_unlock;
  131. if (haptic->magnitude) {
  132. error = max77843_haptic_set_duty_cycle(haptic);
  133. if (error) {
  134. dev_err(haptic->dev,
  135. "failed to set duty cycle: %d\n", error);
  136. goto out_unlock;
  137. }
  138. error = max77843_haptic_enable(haptic);
  139. if (error)
  140. dev_err(haptic->dev,
  141. "cannot enable haptic: %d\n", error);
  142. } else {
  143. error = max77843_haptic_disable(haptic);
  144. if (error)
  145. dev_err(haptic->dev,
  146. "cannot disable haptic: %d\n", error);
  147. }
  148. out_unlock:
  149. mutex_unlock(&haptic->mutex);
  150. }
  151. static int max77843_haptic_play_effect(struct input_dev *dev, void *data,
  152. struct ff_effect *effect)
  153. {
  154. struct max77843_haptic *haptic = input_get_drvdata(dev);
  155. u64 period_mag_multi;
  156. haptic->magnitude = effect->u.rumble.strong_magnitude;
  157. if (!haptic->magnitude)
  158. haptic->magnitude = effect->u.rumble.weak_magnitude;
  159. period_mag_multi = (u64)haptic->pwm_dev->period * haptic->magnitude;
  160. haptic->pwm_duty = (unsigned int)(period_mag_multi >>
  161. MAX_MAGNITUDE_SHIFT);
  162. schedule_work(&haptic->work);
  163. return 0;
  164. }
  165. static int max77843_haptic_open(struct input_dev *dev)
  166. {
  167. struct max77843_haptic *haptic = input_get_drvdata(dev);
  168. int error;
  169. error = max77843_haptic_bias(haptic, true);
  170. if (error)
  171. return error;
  172. error = regulator_enable(haptic->motor_reg);
  173. if (error) {
  174. dev_err(haptic->dev,
  175. "failed to enable regulator: %d\n", error);
  176. return error;
  177. }
  178. return 0;
  179. }
  180. static void max77843_haptic_close(struct input_dev *dev)
  181. {
  182. struct max77843_haptic *haptic = input_get_drvdata(dev);
  183. int error;
  184. cancel_work_sync(&haptic->work);
  185. max77843_haptic_disable(haptic);
  186. error = regulator_disable(haptic->motor_reg);
  187. if (error)
  188. dev_err(haptic->dev,
  189. "failed to disable regulator: %d\n", error);
  190. max77843_haptic_bias(haptic, false);
  191. }
  192. static int max77843_haptic_probe(struct platform_device *pdev)
  193. {
  194. struct max77843 *max77843 = dev_get_drvdata(pdev->dev.parent);
  195. struct max77843_haptic *haptic;
  196. int error;
  197. haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL);
  198. if (!haptic)
  199. return -ENOMEM;
  200. haptic->regmap_haptic = max77843->regmap;
  201. haptic->dev = &pdev->dev;
  202. haptic->type = MAX77843_HAPTIC_LRA;
  203. haptic->pwm_divisor = MAX77843_HAPTIC_PWM_DIVISOR_128;
  204. INIT_WORK(&haptic->work, max77843_haptic_play_work);
  205. mutex_init(&haptic->mutex);
  206. haptic->pwm_dev = devm_pwm_get(&pdev->dev, NULL);
  207. if (IS_ERR(haptic->pwm_dev)) {
  208. dev_err(&pdev->dev, "failed to get pwm device\n");
  209. return PTR_ERR(haptic->pwm_dev);
  210. }
  211. haptic->motor_reg = devm_regulator_get_exclusive(&pdev->dev, "haptic");
  212. if (IS_ERR(haptic->motor_reg)) {
  213. dev_err(&pdev->dev, "failed to get regulator\n");
  214. return PTR_ERR(haptic->motor_reg);
  215. }
  216. haptic->input_dev = devm_input_allocate_device(&pdev->dev);
  217. if (!haptic->input_dev) {
  218. dev_err(&pdev->dev, "failed to allocate input device\n");
  219. return -ENOMEM;
  220. }
  221. haptic->input_dev->name = "max77843-haptic";
  222. haptic->input_dev->id.version = 1;
  223. haptic->input_dev->dev.parent = &pdev->dev;
  224. haptic->input_dev->open = max77843_haptic_open;
  225. haptic->input_dev->close = max77843_haptic_close;
  226. input_set_drvdata(haptic->input_dev, haptic);
  227. input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE);
  228. error = input_ff_create_memless(haptic->input_dev, NULL,
  229. max77843_haptic_play_effect);
  230. if (error) {
  231. dev_err(&pdev->dev, "failed to create force-feedback\n");
  232. return error;
  233. }
  234. error = input_register_device(haptic->input_dev);
  235. if (error) {
  236. dev_err(&pdev->dev, "failed to register input device\n");
  237. return error;
  238. }
  239. platform_set_drvdata(pdev, haptic);
  240. return 0;
  241. }
  242. static int __maybe_unused max77843_haptic_suspend(struct device *dev)
  243. {
  244. struct platform_device *pdev = to_platform_device(dev);
  245. struct max77843_haptic *haptic = platform_get_drvdata(pdev);
  246. int error;
  247. error = mutex_lock_interruptible(&haptic->mutex);
  248. if (error)
  249. return error;
  250. max77843_haptic_disable(haptic);
  251. haptic->suspended = true;
  252. mutex_unlock(&haptic->mutex);
  253. return 0;
  254. }
  255. static int __maybe_unused max77843_haptic_resume(struct device *dev)
  256. {
  257. struct platform_device *pdev = to_platform_device(dev);
  258. struct max77843_haptic *haptic = platform_get_drvdata(pdev);
  259. unsigned int magnitude;
  260. mutex_lock(&haptic->mutex);
  261. haptic->suspended = false;
  262. magnitude = ACCESS_ONCE(haptic->magnitude);
  263. if (magnitude)
  264. max77843_haptic_enable(haptic);
  265. mutex_unlock(&haptic->mutex);
  266. return 0;
  267. }
  268. static SIMPLE_DEV_PM_OPS(max77843_haptic_pm_ops,
  269. max77843_haptic_suspend, max77843_haptic_resume);
  270. static struct platform_driver max77843_haptic_driver = {
  271. .driver = {
  272. .name = "max77843-haptic",
  273. .pm = &max77843_haptic_pm_ops,
  274. },
  275. .probe = max77843_haptic_probe,
  276. };
  277. module_platform_driver(max77843_haptic_driver);
  278. MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
  279. MODULE_DESCRIPTION("MAXIM MAX77843 Haptic driver");
  280. MODULE_LICENSE("GPL");