max8997_haptic.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MAX8997-haptic controller driver
  4. *
  5. * Copyright (C) 2012 Samsung Electronics
  6. * Donggeun Kim <[email protected]>
  7. *
  8. * This program is not provided / owned by Maxim Integrated Products.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/err.h>
  14. #include <linux/pwm.h>
  15. #include <linux/input.h>
  16. #include <linux/mfd/max8997-private.h>
  17. #include <linux/mfd/max8997.h>
  18. #include <linux/regulator/consumer.h>
  19. /* Haptic configuration 2 register */
  20. #define MAX8997_MOTOR_TYPE_SHIFT 7
  21. #define MAX8997_ENABLE_SHIFT 6
  22. #define MAX8997_MODE_SHIFT 5
  23. /* Haptic driver configuration register */
  24. #define MAX8997_CYCLE_SHIFT 6
  25. #define MAX8997_SIG_PERIOD_SHIFT 4
  26. #define MAX8997_SIG_DUTY_SHIFT 2
  27. #define MAX8997_PWM_DUTY_SHIFT 0
  28. struct max8997_haptic {
  29. struct device *dev;
  30. struct i2c_client *client;
  31. struct input_dev *input_dev;
  32. struct regulator *regulator;
  33. struct work_struct work;
  34. struct mutex mutex;
  35. bool enabled;
  36. unsigned int level;
  37. struct pwm_device *pwm;
  38. unsigned int pwm_period;
  39. enum max8997_haptic_pwm_divisor pwm_divisor;
  40. enum max8997_haptic_motor_type type;
  41. enum max8997_haptic_pulse_mode mode;
  42. unsigned int internal_mode_pattern;
  43. unsigned int pattern_cycle;
  44. unsigned int pattern_signal_period;
  45. };
  46. static int max8997_haptic_set_duty_cycle(struct max8997_haptic *chip)
  47. {
  48. int ret = 0;
  49. if (chip->mode == MAX8997_EXTERNAL_MODE) {
  50. unsigned int duty = chip->pwm_period * chip->level / 100;
  51. ret = pwm_config(chip->pwm, duty, chip->pwm_period);
  52. } else {
  53. u8 duty_index = 0;
  54. duty_index = DIV_ROUND_UP(chip->level * 64, 100);
  55. switch (chip->internal_mode_pattern) {
  56. case 0:
  57. max8997_write_reg(chip->client,
  58. MAX8997_HAPTIC_REG_SIGPWMDC1, duty_index);
  59. break;
  60. case 1:
  61. max8997_write_reg(chip->client,
  62. MAX8997_HAPTIC_REG_SIGPWMDC2, duty_index);
  63. break;
  64. case 2:
  65. max8997_write_reg(chip->client,
  66. MAX8997_HAPTIC_REG_SIGPWMDC3, duty_index);
  67. break;
  68. case 3:
  69. max8997_write_reg(chip->client,
  70. MAX8997_HAPTIC_REG_SIGPWMDC4, duty_index);
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. return ret;
  77. }
  78. static void max8997_haptic_configure(struct max8997_haptic *chip)
  79. {
  80. u8 value;
  81. value = chip->type << MAX8997_MOTOR_TYPE_SHIFT |
  82. chip->enabled << MAX8997_ENABLE_SHIFT |
  83. chip->mode << MAX8997_MODE_SHIFT | chip->pwm_divisor;
  84. max8997_write_reg(chip->client, MAX8997_HAPTIC_REG_CONF2, value);
  85. if (chip->mode == MAX8997_INTERNAL_MODE && chip->enabled) {
  86. value = chip->internal_mode_pattern << MAX8997_CYCLE_SHIFT |
  87. chip->internal_mode_pattern << MAX8997_SIG_PERIOD_SHIFT |
  88. chip->internal_mode_pattern << MAX8997_SIG_DUTY_SHIFT |
  89. chip->internal_mode_pattern << MAX8997_PWM_DUTY_SHIFT;
  90. max8997_write_reg(chip->client,
  91. MAX8997_HAPTIC_REG_DRVCONF, value);
  92. switch (chip->internal_mode_pattern) {
  93. case 0:
  94. value = chip->pattern_cycle << 4;
  95. max8997_write_reg(chip->client,
  96. MAX8997_HAPTIC_REG_CYCLECONF1, value);
  97. value = chip->pattern_signal_period;
  98. max8997_write_reg(chip->client,
  99. MAX8997_HAPTIC_REG_SIGCONF1, value);
  100. break;
  101. case 1:
  102. value = chip->pattern_cycle;
  103. max8997_write_reg(chip->client,
  104. MAX8997_HAPTIC_REG_CYCLECONF1, value);
  105. value = chip->pattern_signal_period;
  106. max8997_write_reg(chip->client,
  107. MAX8997_HAPTIC_REG_SIGCONF2, value);
  108. break;
  109. case 2:
  110. value = chip->pattern_cycle << 4;
  111. max8997_write_reg(chip->client,
  112. MAX8997_HAPTIC_REG_CYCLECONF2, value);
  113. value = chip->pattern_signal_period;
  114. max8997_write_reg(chip->client,
  115. MAX8997_HAPTIC_REG_SIGCONF3, value);
  116. break;
  117. case 3:
  118. value = chip->pattern_cycle;
  119. max8997_write_reg(chip->client,
  120. MAX8997_HAPTIC_REG_CYCLECONF2, value);
  121. value = chip->pattern_signal_period;
  122. max8997_write_reg(chip->client,
  123. MAX8997_HAPTIC_REG_SIGCONF4, value);
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. }
  130. static void max8997_haptic_enable(struct max8997_haptic *chip)
  131. {
  132. int error;
  133. mutex_lock(&chip->mutex);
  134. error = max8997_haptic_set_duty_cycle(chip);
  135. if (error) {
  136. dev_err(chip->dev, "set_pwm_cycle failed, error: %d\n", error);
  137. goto out;
  138. }
  139. if (!chip->enabled) {
  140. error = regulator_enable(chip->regulator);
  141. if (error) {
  142. dev_err(chip->dev, "Failed to enable regulator\n");
  143. goto out;
  144. }
  145. max8997_haptic_configure(chip);
  146. if (chip->mode == MAX8997_EXTERNAL_MODE) {
  147. error = pwm_enable(chip->pwm);
  148. if (error) {
  149. dev_err(chip->dev, "Failed to enable PWM\n");
  150. regulator_disable(chip->regulator);
  151. goto out;
  152. }
  153. }
  154. chip->enabled = true;
  155. }
  156. out:
  157. mutex_unlock(&chip->mutex);
  158. }
  159. static void max8997_haptic_disable(struct max8997_haptic *chip)
  160. {
  161. mutex_lock(&chip->mutex);
  162. if (chip->enabled) {
  163. chip->enabled = false;
  164. max8997_haptic_configure(chip);
  165. if (chip->mode == MAX8997_EXTERNAL_MODE)
  166. pwm_disable(chip->pwm);
  167. regulator_disable(chip->regulator);
  168. }
  169. mutex_unlock(&chip->mutex);
  170. }
  171. static void max8997_haptic_play_effect_work(struct work_struct *work)
  172. {
  173. struct max8997_haptic *chip =
  174. container_of(work, struct max8997_haptic, work);
  175. if (chip->level)
  176. max8997_haptic_enable(chip);
  177. else
  178. max8997_haptic_disable(chip);
  179. }
  180. static int max8997_haptic_play_effect(struct input_dev *dev, void *data,
  181. struct ff_effect *effect)
  182. {
  183. struct max8997_haptic *chip = input_get_drvdata(dev);
  184. chip->level = effect->u.rumble.strong_magnitude;
  185. if (!chip->level)
  186. chip->level = effect->u.rumble.weak_magnitude;
  187. schedule_work(&chip->work);
  188. return 0;
  189. }
  190. static void max8997_haptic_close(struct input_dev *dev)
  191. {
  192. struct max8997_haptic *chip = input_get_drvdata(dev);
  193. cancel_work_sync(&chip->work);
  194. max8997_haptic_disable(chip);
  195. }
  196. static int max8997_haptic_probe(struct platform_device *pdev)
  197. {
  198. struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  199. const struct max8997_platform_data *pdata =
  200. dev_get_platdata(iodev->dev);
  201. const struct max8997_haptic_platform_data *haptic_pdata = NULL;
  202. struct max8997_haptic *chip;
  203. struct input_dev *input_dev;
  204. int error;
  205. if (pdata)
  206. haptic_pdata = pdata->haptic_pdata;
  207. if (!haptic_pdata) {
  208. dev_err(&pdev->dev, "no haptic platform data\n");
  209. return -EINVAL;
  210. }
  211. chip = kzalloc(sizeof(struct max8997_haptic), GFP_KERNEL);
  212. input_dev = input_allocate_device();
  213. if (!chip || !input_dev) {
  214. dev_err(&pdev->dev, "unable to allocate memory\n");
  215. error = -ENOMEM;
  216. goto err_free_mem;
  217. }
  218. INIT_WORK(&chip->work, max8997_haptic_play_effect_work);
  219. mutex_init(&chip->mutex);
  220. chip->client = iodev->haptic;
  221. chip->dev = &pdev->dev;
  222. chip->input_dev = input_dev;
  223. chip->pwm_period = haptic_pdata->pwm_period;
  224. chip->type = haptic_pdata->type;
  225. chip->mode = haptic_pdata->mode;
  226. chip->pwm_divisor = haptic_pdata->pwm_divisor;
  227. switch (chip->mode) {
  228. case MAX8997_INTERNAL_MODE:
  229. chip->internal_mode_pattern =
  230. haptic_pdata->internal_mode_pattern;
  231. chip->pattern_cycle = haptic_pdata->pattern_cycle;
  232. chip->pattern_signal_period =
  233. haptic_pdata->pattern_signal_period;
  234. break;
  235. case MAX8997_EXTERNAL_MODE:
  236. chip->pwm = pwm_request(haptic_pdata->pwm_channel_id,
  237. "max8997-haptic");
  238. if (IS_ERR(chip->pwm)) {
  239. error = PTR_ERR(chip->pwm);
  240. dev_err(&pdev->dev,
  241. "unable to request PWM for haptic, error: %d\n",
  242. error);
  243. goto err_free_mem;
  244. }
  245. /*
  246. * FIXME: pwm_apply_args() should be removed when switching to
  247. * the atomic PWM API.
  248. */
  249. pwm_apply_args(chip->pwm);
  250. break;
  251. default:
  252. dev_err(&pdev->dev,
  253. "Invalid chip mode specified (%d)\n", chip->mode);
  254. error = -EINVAL;
  255. goto err_free_mem;
  256. }
  257. chip->regulator = regulator_get(&pdev->dev, "inmotor");
  258. if (IS_ERR(chip->regulator)) {
  259. error = PTR_ERR(chip->regulator);
  260. dev_err(&pdev->dev,
  261. "unable to get regulator, error: %d\n",
  262. error);
  263. goto err_free_pwm;
  264. }
  265. input_dev->name = "max8997-haptic";
  266. input_dev->id.version = 1;
  267. input_dev->dev.parent = &pdev->dev;
  268. input_dev->close = max8997_haptic_close;
  269. input_set_drvdata(input_dev, chip);
  270. input_set_capability(input_dev, EV_FF, FF_RUMBLE);
  271. error = input_ff_create_memless(input_dev, NULL,
  272. max8997_haptic_play_effect);
  273. if (error) {
  274. dev_err(&pdev->dev,
  275. "unable to create FF device, error: %d\n",
  276. error);
  277. goto err_put_regulator;
  278. }
  279. error = input_register_device(input_dev);
  280. if (error) {
  281. dev_err(&pdev->dev,
  282. "unable to register input device, error: %d\n",
  283. error);
  284. goto err_destroy_ff;
  285. }
  286. platform_set_drvdata(pdev, chip);
  287. return 0;
  288. err_destroy_ff:
  289. input_ff_destroy(input_dev);
  290. err_put_regulator:
  291. regulator_put(chip->regulator);
  292. err_free_pwm:
  293. if (chip->mode == MAX8997_EXTERNAL_MODE)
  294. pwm_free(chip->pwm);
  295. err_free_mem:
  296. input_free_device(input_dev);
  297. kfree(chip);
  298. return error;
  299. }
  300. static int max8997_haptic_remove(struct platform_device *pdev)
  301. {
  302. struct max8997_haptic *chip = platform_get_drvdata(pdev);
  303. input_unregister_device(chip->input_dev);
  304. regulator_put(chip->regulator);
  305. if (chip->mode == MAX8997_EXTERNAL_MODE)
  306. pwm_free(chip->pwm);
  307. kfree(chip);
  308. return 0;
  309. }
  310. static int __maybe_unused max8997_haptic_suspend(struct device *dev)
  311. {
  312. struct platform_device *pdev = to_platform_device(dev);
  313. struct max8997_haptic *chip = platform_get_drvdata(pdev);
  314. max8997_haptic_disable(chip);
  315. return 0;
  316. }
  317. static SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops, max8997_haptic_suspend, NULL);
  318. static const struct platform_device_id max8997_haptic_id[] = {
  319. { "max8997-haptic", 0 },
  320. { },
  321. };
  322. MODULE_DEVICE_TABLE(platform, max8997_haptic_id);
  323. static struct platform_driver max8997_haptic_driver = {
  324. .driver = {
  325. .name = "max8997-haptic",
  326. .pm = &max8997_haptic_pm_ops,
  327. },
  328. .probe = max8997_haptic_probe,
  329. .remove = max8997_haptic_remove,
  330. .id_table = max8997_haptic_id,
  331. };
  332. module_platform_driver(max8997_haptic_driver);
  333. MODULE_AUTHOR("Donggeun Kim <[email protected]>");
  334. MODULE_DESCRIPTION("max8997_haptic driver");
  335. MODULE_LICENSE("GPL");