max77693-haptic.c 10 KB

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