max77693-haptic.c 10 KB

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