max77693-haptic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MAXIM MAX77693/MAX77843 Haptic device driver
  4. *
  5. * Copyright (C) 2014,2015 Samsung Electronics
  6. * Jaewon Kim <jaewon02.kim@samsung.com>
  7. * Krzysztof Kozlowski <krzk@kernel.org>
  8. *
  9. * This program is not provided / owned by Maxim Integrated Products.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/i2c.h>
  14. #include <linux/regmap.h>
  15. #include <linux/input.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  20. #include <linux/string_choices.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/mfd/max77693.h>
  24. #include <linux/mfd/max77693-common.h>
  25. #include <linux/mfd/max77693-private.h>
  26. #include <linux/mfd/max77705-private.h>
  27. #include <linux/mfd/max77843-private.h>
  28. #define MAX_MAGNITUDE_SHIFT 16
  29. enum max77693_haptic_motor_type {
  30. MAX77693_HAPTIC_ERM = 0,
  31. MAX77693_HAPTIC_LRA,
  32. };
  33. enum max77693_haptic_pulse_mode {
  34. MAX77693_HAPTIC_EXTERNAL_MODE = 0,
  35. MAX77693_HAPTIC_INTERNAL_MODE,
  36. };
  37. enum max77693_haptic_pwm_divisor {
  38. MAX77693_HAPTIC_PWM_DIVISOR_32 = 0,
  39. MAX77693_HAPTIC_PWM_DIVISOR_64,
  40. MAX77693_HAPTIC_PWM_DIVISOR_128,
  41. MAX77693_HAPTIC_PWM_DIVISOR_256,
  42. };
  43. struct max77693_haptic {
  44. enum max77693_types dev_type;
  45. struct regmap *regmap_pmic;
  46. struct regmap *regmap_haptic;
  47. struct device *dev;
  48. struct input_dev *input_dev;
  49. struct pwm_device *pwm_dev;
  50. struct regulator *motor_reg;
  51. bool enabled;
  52. bool suspend_state;
  53. unsigned int magnitude;
  54. unsigned int pwm_duty;
  55. enum max77693_haptic_motor_type type;
  56. enum max77693_haptic_pulse_mode mode;
  57. struct work_struct work;
  58. };
  59. static int max77693_haptic_set_duty_cycle(struct max77693_haptic *haptic)
  60. {
  61. struct pwm_state state;
  62. int error;
  63. pwm_init_state(haptic->pwm_dev, &state);
  64. state.duty_cycle = (state.period + haptic->pwm_duty) / 2;
  65. error = pwm_apply_might_sleep(haptic->pwm_dev, &state);
  66. if (error) {
  67. dev_err(haptic->dev,
  68. "failed to set pwm duty cycle: %d\n", error);
  69. return error;
  70. }
  71. return 0;
  72. }
  73. static int max77843_haptic_bias(struct max77693_haptic *haptic, bool on)
  74. {
  75. int error;
  76. if (haptic->dev_type != TYPE_MAX77843)
  77. return 0;
  78. error = regmap_update_bits(haptic->regmap_haptic,
  79. MAX77843_SYS_REG_MAINCTRL1,
  80. MAX77843_MAINCTRL1_BIASEN_MASK,
  81. on << MAINCTRL1_BIASEN_SHIFT);
  82. if (error) {
  83. dev_err(haptic->dev, "failed to %s bias: %d\n",
  84. str_enable_disable(on), error);
  85. return error;
  86. }
  87. return 0;
  88. }
  89. static int max77693_haptic_configure(struct max77693_haptic *haptic,
  90. bool enable)
  91. {
  92. unsigned int value, config_reg;
  93. int error;
  94. switch (haptic->dev_type) {
  95. case TYPE_MAX77693:
  96. value = ((haptic->type << MAX77693_CONFIG2_MODE) |
  97. (enable << MAX77693_CONFIG2_MEN) |
  98. (haptic->mode << MAX77693_CONFIG2_HTYP) |
  99. MAX77693_HAPTIC_PWM_DIVISOR_128);
  100. config_reg = MAX77693_HAPTIC_REG_CONFIG2;
  101. break;
  102. case TYPE_MAX77705:
  103. value = ((haptic->type << MAX77693_CONFIG2_MODE) |
  104. (enable << MAX77693_CONFIG2_MEN) |
  105. (haptic->mode << MAX77693_CONFIG2_HTYP) |
  106. MAX77693_HAPTIC_PWM_DIVISOR_128);
  107. config_reg = MAX77705_PMIC_REG_MCONFIG;
  108. break;
  109. case TYPE_MAX77843:
  110. value = (haptic->type << MCONFIG_MODE_SHIFT) |
  111. (enable << MCONFIG_MEN_SHIFT) |
  112. MAX77693_HAPTIC_PWM_DIVISOR_128;
  113. config_reg = MAX77843_HAP_REG_MCONFIG;
  114. break;
  115. default:
  116. return -EINVAL;
  117. }
  118. error = regmap_write(haptic->regmap_haptic,
  119. config_reg, value);
  120. if (error) {
  121. dev_err(haptic->dev,
  122. "failed to update haptic config: %d\n", error);
  123. return error;
  124. }
  125. return 0;
  126. }
  127. static int max77693_haptic_lowsys(struct max77693_haptic *haptic, bool enable)
  128. {
  129. int error;
  130. if (haptic->dev_type != TYPE_MAX77693)
  131. return 0;
  132. error = regmap_update_bits(haptic->regmap_pmic,
  133. MAX77693_PMIC_REG_LSCNFG,
  134. MAX77693_PMIC_LOW_SYS_MASK,
  135. enable << MAX77693_PMIC_LOW_SYS_SHIFT);
  136. if (error) {
  137. dev_err(haptic->dev, "cannot update pmic regmap: %d\n", error);
  138. return error;
  139. }
  140. return 0;
  141. }
  142. static void max77693_haptic_enable(struct max77693_haptic *haptic)
  143. {
  144. struct pwm_state state;
  145. int error;
  146. if (haptic->enabled)
  147. return;
  148. pwm_init_state(haptic->pwm_dev, &state);
  149. state.duty_cycle = (state.period + haptic->pwm_duty) / 2;
  150. state.enabled = true;
  151. error = pwm_apply_might_sleep(haptic->pwm_dev, &state);
  152. if (error) {
  153. dev_err(haptic->dev,
  154. "failed to enable haptic pwm device: %d\n", error);
  155. return;
  156. }
  157. error = max77693_haptic_lowsys(haptic, true);
  158. if (error)
  159. goto err_enable_lowsys;
  160. error = max77693_haptic_configure(haptic, true);
  161. if (error)
  162. goto err_enable_config;
  163. haptic->enabled = true;
  164. return;
  165. err_enable_config:
  166. max77693_haptic_lowsys(haptic, false);
  167. err_enable_lowsys:
  168. pwm_disable(haptic->pwm_dev);
  169. }
  170. static void max77693_haptic_disable(struct max77693_haptic *haptic)
  171. {
  172. int error;
  173. if (!haptic->enabled)
  174. return;
  175. error = max77693_haptic_configure(haptic, false);
  176. if (error)
  177. return;
  178. error = max77693_haptic_lowsys(haptic, false);
  179. if (error)
  180. goto err_disable_lowsys;
  181. pwm_disable(haptic->pwm_dev);
  182. haptic->enabled = false;
  183. return;
  184. err_disable_lowsys:
  185. max77693_haptic_configure(haptic, true);
  186. }
  187. static void max77693_haptic_play_work(struct work_struct *work)
  188. {
  189. struct max77693_haptic *haptic =
  190. container_of(work, struct max77693_haptic, work);
  191. if (!haptic->magnitude)
  192. max77693_haptic_disable(haptic);
  193. else if (haptic->enabled)
  194. max77693_haptic_set_duty_cycle(haptic);
  195. else
  196. max77693_haptic_enable(haptic);
  197. }
  198. static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
  199. struct ff_effect *effect)
  200. {
  201. struct max77693_haptic *haptic = input_get_drvdata(dev);
  202. struct pwm_args pargs;
  203. u64 period_mag_multi;
  204. haptic->magnitude = effect->u.rumble.strong_magnitude;
  205. if (!haptic->magnitude)
  206. haptic->magnitude = effect->u.rumble.weak_magnitude;
  207. /*
  208. * The magnitude comes from force-feedback interface.
  209. * The formula to convert magnitude to pwm_duty as follows:
  210. * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
  211. */
  212. pwm_get_args(haptic->pwm_dev, &pargs);
  213. period_mag_multi = (u64)pargs.period * haptic->magnitude;
  214. haptic->pwm_duty = (unsigned int)(period_mag_multi >>
  215. MAX_MAGNITUDE_SHIFT);
  216. schedule_work(&haptic->work);
  217. return 0;
  218. }
  219. static int max77693_haptic_open(struct input_dev *dev)
  220. {
  221. struct max77693_haptic *haptic = input_get_drvdata(dev);
  222. int error;
  223. error = max77843_haptic_bias(haptic, true);
  224. if (error)
  225. return error;
  226. error = regulator_enable(haptic->motor_reg);
  227. if (error) {
  228. dev_err(haptic->dev,
  229. "failed to enable regulator: %d\n", error);
  230. return error;
  231. }
  232. return 0;
  233. }
  234. static void max77693_haptic_close(struct input_dev *dev)
  235. {
  236. struct max77693_haptic *haptic = input_get_drvdata(dev);
  237. int error;
  238. cancel_work_sync(&haptic->work);
  239. max77693_haptic_disable(haptic);
  240. error = regulator_disable(haptic->motor_reg);
  241. if (error)
  242. dev_err(haptic->dev,
  243. "failed to disable regulator: %d\n", error);
  244. max77843_haptic_bias(haptic, false);
  245. }
  246. static int max77693_haptic_probe(struct platform_device *pdev)
  247. {
  248. struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
  249. struct max77693_haptic *haptic;
  250. int error;
  251. haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL);
  252. if (!haptic)
  253. return -ENOMEM;
  254. haptic->regmap_pmic = max77693->regmap;
  255. haptic->dev = &pdev->dev;
  256. haptic->type = MAX77693_HAPTIC_LRA;
  257. haptic->mode = MAX77693_HAPTIC_EXTERNAL_MODE;
  258. haptic->suspend_state = false;
  259. /* Variant-specific init */
  260. haptic->dev_type = max77693->type;
  261. switch (haptic->dev_type) {
  262. case TYPE_MAX77693:
  263. haptic->regmap_haptic = max77693->regmap_haptic;
  264. break;
  265. case TYPE_MAX77705:
  266. case TYPE_MAX77843:
  267. haptic->regmap_haptic = max77693->regmap;
  268. break;
  269. default:
  270. dev_err(&pdev->dev, "unsupported device type: %u\n",
  271. haptic->dev_type);
  272. return -EINVAL;
  273. }
  274. INIT_WORK(&haptic->work, max77693_haptic_play_work);
  275. /* Get pwm and regulatot for haptic device */
  276. haptic->pwm_dev = devm_pwm_get(&pdev->dev, NULL);
  277. if (IS_ERR(haptic->pwm_dev)) {
  278. dev_err(&pdev->dev, "failed to get pwm device\n");
  279. return PTR_ERR(haptic->pwm_dev);
  280. }
  281. haptic->motor_reg = devm_regulator_get(&pdev->dev, "haptic");
  282. if (IS_ERR(haptic->motor_reg)) {
  283. dev_err(&pdev->dev, "failed to get regulator\n");
  284. return PTR_ERR(haptic->motor_reg);
  285. }
  286. /* Initialize input device for haptic device */
  287. haptic->input_dev = devm_input_allocate_device(&pdev->dev);
  288. if (!haptic->input_dev) {
  289. dev_err(&pdev->dev, "failed to allocate input device\n");
  290. return -ENOMEM;
  291. }
  292. haptic->input_dev->name = "max77693-haptic";
  293. haptic->input_dev->id.version = 1;
  294. haptic->input_dev->dev.parent = &pdev->dev;
  295. haptic->input_dev->open = max77693_haptic_open;
  296. haptic->input_dev->close = max77693_haptic_close;
  297. input_set_drvdata(haptic->input_dev, haptic);
  298. input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE);
  299. error = input_ff_create_memless(haptic->input_dev, NULL,
  300. max77693_haptic_play_effect);
  301. if (error) {
  302. dev_err(&pdev->dev, "failed to create force-feedback\n");
  303. return error;
  304. }
  305. error = input_register_device(haptic->input_dev);
  306. if (error) {
  307. dev_err(&pdev->dev, "failed to register input device\n");
  308. return error;
  309. }
  310. platform_set_drvdata(pdev, haptic);
  311. return 0;
  312. }
  313. static int max77693_haptic_suspend(struct device *dev)
  314. {
  315. struct platform_device *pdev = to_platform_device(dev);
  316. struct max77693_haptic *haptic = platform_get_drvdata(pdev);
  317. if (haptic->enabled) {
  318. max77693_haptic_disable(haptic);
  319. haptic->suspend_state = true;
  320. }
  321. return 0;
  322. }
  323. static int max77693_haptic_resume(struct device *dev)
  324. {
  325. struct platform_device *pdev = to_platform_device(dev);
  326. struct max77693_haptic *haptic = platform_get_drvdata(pdev);
  327. if (haptic->suspend_state) {
  328. max77693_haptic_enable(haptic);
  329. haptic->suspend_state = false;
  330. }
  331. return 0;
  332. }
  333. static DEFINE_SIMPLE_DEV_PM_OPS(max77693_haptic_pm_ops,
  334. max77693_haptic_suspend,
  335. max77693_haptic_resume);
  336. static const struct platform_device_id max77693_haptic_id[] = {
  337. { "max77693-haptic", },
  338. { "max77705-haptic", },
  339. { "max77843-haptic", },
  340. {},
  341. };
  342. MODULE_DEVICE_TABLE(platform, max77693_haptic_id);
  343. static const struct of_device_id of_max77693_haptic_dt_match[] = {
  344. { .compatible = "maxim,max77693-haptic", },
  345. { .compatible = "maxim,max77705-haptic", },
  346. { .compatible = "maxim,max77843-haptic", },
  347. { /* sentinel */ },
  348. };
  349. MODULE_DEVICE_TABLE(of, of_max77693_haptic_dt_match);
  350. static struct platform_driver max77693_haptic_driver = {
  351. .driver = {
  352. .name = "max77693-haptic",
  353. .pm = pm_sleep_ptr(&max77693_haptic_pm_ops),
  354. .of_match_table = of_max77693_haptic_dt_match,
  355. },
  356. .probe = max77693_haptic_probe,
  357. .id_table = max77693_haptic_id,
  358. };
  359. module_platform_driver(max77693_haptic_driver);
  360. MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
  361. MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
  362. MODULE_DESCRIPTION("MAXIM 77693/77705/77843 Haptic driver");
  363. MODULE_LICENSE("GPL");