max77693-haptic.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * MAXIM MAX77693 Haptic device driver
  3. *
  4. * Copyright (C) 2014 Samsung Electronics
  5. * Jaewon Kim <jaewon02.kim@samsung.com>
  6. *
  7. * This program is not provided / owned by Maxim Integrated Products.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <linux/input.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pwm.h>
  22. #include <linux/slab.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/mfd/max77693.h>
  26. #include <linux/mfd/max77693-private.h>
  27. #define MAX_MAGNITUDE_SHIFT 16
  28. enum max77693_haptic_motor_type {
  29. MAX77693_HAPTIC_ERM = 0,
  30. MAX77693_HAPTIC_LRA,
  31. };
  32. enum max77693_haptic_pulse_mode {
  33. MAX77693_HAPTIC_EXTERNAL_MODE = 0,
  34. MAX77693_HAPTIC_INTERNAL_MODE,
  35. };
  36. enum max77693_haptic_pwm_divisor {
  37. MAX77693_HAPTIC_PWM_DIVISOR_32 = 0,
  38. MAX77693_HAPTIC_PWM_DIVISOR_64,
  39. MAX77693_HAPTIC_PWM_DIVISOR_128,
  40. MAX77693_HAPTIC_PWM_DIVISOR_256,
  41. };
  42. struct max77693_haptic {
  43. struct regmap *regmap_pmic;
  44. struct regmap *regmap_haptic;
  45. struct device *dev;
  46. struct input_dev *input_dev;
  47. struct pwm_device *pwm_dev;
  48. struct regulator *motor_reg;
  49. bool enabled;
  50. bool suspend_state;
  51. unsigned int magnitude;
  52. unsigned int pwm_duty;
  53. enum max77693_haptic_motor_type type;
  54. enum max77693_haptic_pulse_mode mode;
  55. enum max77693_haptic_pwm_divisor pwm_divisor;
  56. struct work_struct work;
  57. };
  58. static int max77693_haptic_set_duty_cycle(struct max77693_haptic *haptic)
  59. {
  60. int delta = (haptic->pwm_dev->period + haptic->pwm_duty) / 2;
  61. int error;
  62. error = pwm_config(haptic->pwm_dev, delta, haptic->pwm_dev->period);
  63. if (error) {
  64. dev_err(haptic->dev, "failed to configure pwm: %d\n", error);
  65. return error;
  66. }
  67. return 0;
  68. }
  69. static int max77693_haptic_configure(struct max77693_haptic *haptic,
  70. bool enable)
  71. {
  72. unsigned int value;
  73. int error;
  74. value = ((haptic->type << MAX77693_CONFIG2_MODE) |
  75. (enable << MAX77693_CONFIG2_MEN) |
  76. (haptic->mode << MAX77693_CONFIG2_HTYP) |
  77. (haptic->pwm_divisor));
  78. error = regmap_write(haptic->regmap_haptic,
  79. MAX77693_HAPTIC_REG_CONFIG2, value);
  80. if (error) {
  81. dev_err(haptic->dev,
  82. "failed to update haptic config: %d\n", error);
  83. return error;
  84. }
  85. return 0;
  86. }
  87. static int max77693_haptic_lowsys(struct max77693_haptic *haptic, bool enable)
  88. {
  89. int error;
  90. error = regmap_update_bits(haptic->regmap_pmic,
  91. MAX77693_PMIC_REG_LSCNFG,
  92. MAX77693_PMIC_LOW_SYS_MASK,
  93. enable << MAX77693_PMIC_LOW_SYS_SHIFT);
  94. if (error) {
  95. dev_err(haptic->dev, "cannot update pmic regmap: %d\n", error);
  96. return error;
  97. }
  98. return 0;
  99. }
  100. static void max77693_haptic_enable(struct max77693_haptic *haptic)
  101. {
  102. int error;
  103. if (haptic->enabled)
  104. return;
  105. error = pwm_enable(haptic->pwm_dev);
  106. if (error) {
  107. dev_err(haptic->dev,
  108. "failed to enable haptic pwm device: %d\n", error);
  109. return;
  110. }
  111. error = max77693_haptic_lowsys(haptic, true);
  112. if (error)
  113. goto err_enable_lowsys;
  114. error = max77693_haptic_configure(haptic, true);
  115. if (error)
  116. goto err_enable_config;
  117. haptic->enabled = true;
  118. return;
  119. err_enable_config:
  120. max77693_haptic_lowsys(haptic, false);
  121. err_enable_lowsys:
  122. pwm_disable(haptic->pwm_dev);
  123. }
  124. static void max77693_haptic_disable(struct max77693_haptic *haptic)
  125. {
  126. int error;
  127. if (!haptic->enabled)
  128. return;
  129. error = max77693_haptic_configure(haptic, false);
  130. if (error)
  131. return;
  132. error = max77693_haptic_lowsys(haptic, false);
  133. if (error)
  134. goto err_disable_lowsys;
  135. pwm_disable(haptic->pwm_dev);
  136. haptic->enabled = false;
  137. return;
  138. err_disable_lowsys:
  139. max77693_haptic_configure(haptic, true);
  140. }
  141. static void max77693_haptic_play_work(struct work_struct *work)
  142. {
  143. struct max77693_haptic *haptic =
  144. container_of(work, struct max77693_haptic, work);
  145. int error;
  146. error = max77693_haptic_set_duty_cycle(haptic);
  147. if (error) {
  148. dev_err(haptic->dev, "failed to set duty cycle: %d\n", error);
  149. return;
  150. }
  151. if (haptic->magnitude)
  152. max77693_haptic_enable(haptic);
  153. else
  154. max77693_haptic_disable(haptic);
  155. }
  156. static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
  157. struct ff_effect *effect)
  158. {
  159. struct max77693_haptic *haptic = input_get_drvdata(dev);
  160. u64 period_mag_multi;
  161. haptic->magnitude = effect->u.rumble.strong_magnitude;
  162. if (!haptic->magnitude)
  163. haptic->magnitude = effect->u.rumble.weak_magnitude;
  164. /*
  165. * The magnitude comes from force-feedback interface.
  166. * The formula to convert magnitude to pwm_duty as follows:
  167. * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
  168. */
  169. period_mag_multi = (u64)haptic->pwm_dev->period * haptic->magnitude;
  170. haptic->pwm_duty = (unsigned int)(period_mag_multi >>
  171. MAX_MAGNITUDE_SHIFT);
  172. schedule_work(&haptic->work);
  173. return 0;
  174. }
  175. static int max77693_haptic_open(struct input_dev *dev)
  176. {
  177. struct max77693_haptic *haptic = input_get_drvdata(dev);
  178. int error;
  179. error = regulator_enable(haptic->motor_reg);
  180. if (error) {
  181. dev_err(haptic->dev,
  182. "failed to enable regulator: %d\n", error);
  183. return error;
  184. }
  185. return 0;
  186. }
  187. static void max77693_haptic_close(struct input_dev *dev)
  188. {
  189. struct max77693_haptic *haptic = input_get_drvdata(dev);
  190. int error;
  191. cancel_work_sync(&haptic->work);
  192. max77693_haptic_disable(haptic);
  193. error = regulator_disable(haptic->motor_reg);
  194. if (error)
  195. dev_err(haptic->dev,
  196. "failed to disable regulator: %d\n", error);
  197. }
  198. static int max77693_haptic_probe(struct platform_device *pdev)
  199. {
  200. struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
  201. struct max77693_haptic *haptic;
  202. int error;
  203. haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL);
  204. if (!haptic)
  205. return -ENOMEM;
  206. haptic->regmap_pmic = max77693->regmap;
  207. haptic->regmap_haptic = max77693->regmap_haptic;
  208. haptic->dev = &pdev->dev;
  209. haptic->type = MAX77693_HAPTIC_LRA;
  210. haptic->mode = MAX77693_HAPTIC_EXTERNAL_MODE;
  211. haptic->pwm_divisor = MAX77693_HAPTIC_PWM_DIVISOR_128;
  212. haptic->suspend_state = false;
  213. INIT_WORK(&haptic->work, max77693_haptic_play_work);
  214. /* Get pwm and regulatot for haptic device */
  215. haptic->pwm_dev = devm_pwm_get(&pdev->dev, NULL);
  216. if (IS_ERR(haptic->pwm_dev)) {
  217. dev_err(&pdev->dev, "failed to get pwm device\n");
  218. return PTR_ERR(haptic->pwm_dev);
  219. }
  220. haptic->motor_reg = devm_regulator_get(&pdev->dev, "haptic");
  221. if (IS_ERR(haptic->motor_reg)) {
  222. dev_err(&pdev->dev, "failed to get regulator\n");
  223. return PTR_ERR(haptic->motor_reg);
  224. }
  225. /* Initialize input device for haptic device */
  226. haptic->input_dev = devm_input_allocate_device(&pdev->dev);
  227. if (!haptic->input_dev) {
  228. dev_err(&pdev->dev, "failed to allocate input device\n");
  229. return -ENOMEM;
  230. }
  231. haptic->input_dev->name = "max77693-haptic";
  232. haptic->input_dev->id.version = 1;
  233. haptic->input_dev->dev.parent = &pdev->dev;
  234. haptic->input_dev->open = max77693_haptic_open;
  235. haptic->input_dev->close = max77693_haptic_close;
  236. input_set_drvdata(haptic->input_dev, haptic);
  237. input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE);
  238. error = input_ff_create_memless(haptic->input_dev, NULL,
  239. max77693_haptic_play_effect);
  240. if (error) {
  241. dev_err(&pdev->dev, "failed to create force-feedback\n");
  242. return error;
  243. }
  244. error = input_register_device(haptic->input_dev);
  245. if (error) {
  246. dev_err(&pdev->dev, "failed to register input device\n");
  247. return error;
  248. }
  249. platform_set_drvdata(pdev, haptic);
  250. return 0;
  251. }
  252. static int __maybe_unused max77693_haptic_suspend(struct device *dev)
  253. {
  254. struct platform_device *pdev = to_platform_device(dev);
  255. struct max77693_haptic *haptic = platform_get_drvdata(pdev);
  256. if (haptic->enabled) {
  257. max77693_haptic_disable(haptic);
  258. haptic->suspend_state = true;
  259. }
  260. return 0;
  261. }
  262. static int __maybe_unused max77693_haptic_resume(struct device *dev)
  263. {
  264. struct platform_device *pdev = to_platform_device(dev);
  265. struct max77693_haptic *haptic = platform_get_drvdata(pdev);
  266. if (haptic->suspend_state) {
  267. max77693_haptic_enable(haptic);
  268. haptic->suspend_state = false;
  269. }
  270. return 0;
  271. }
  272. static SIMPLE_DEV_PM_OPS(max77693_haptic_pm_ops,
  273. max77693_haptic_suspend, max77693_haptic_resume);
  274. static struct platform_driver max77693_haptic_driver = {
  275. .driver = {
  276. .name = "max77693-haptic",
  277. .pm = &max77693_haptic_pm_ops,
  278. },
  279. .probe = max77693_haptic_probe,
  280. };
  281. module_platform_driver(max77693_haptic_driver);
  282. MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
  283. MODULE_DESCRIPTION("MAXIM MAX77693 Haptic driver");
  284. MODULE_ALIAS("platform:max77693-haptic");
  285. MODULE_LICENSE("GPL");