max77693-haptic.c 10 KB

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