max8997_haptic.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <dg77.kim@samsung.com>
  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 void max8997_haptic_set_internal_duty_cycle(struct max8997_haptic *chip)
  47. {
  48. u8 duty_index = DIV_ROUND_UP(chip->level * 64, 100);
  49. switch (chip->internal_mode_pattern) {
  50. case 0:
  51. max8997_write_reg(chip->client,
  52. MAX8997_HAPTIC_REG_SIGPWMDC1, duty_index);
  53. break;
  54. case 1:
  55. max8997_write_reg(chip->client,
  56. MAX8997_HAPTIC_REG_SIGPWMDC2, duty_index);
  57. break;
  58. case 2:
  59. max8997_write_reg(chip->client,
  60. MAX8997_HAPTIC_REG_SIGPWMDC3, duty_index);
  61. break;
  62. case 3:
  63. max8997_write_reg(chip->client,
  64. MAX8997_HAPTIC_REG_SIGPWMDC4, duty_index);
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. static void max8997_haptic_configure(struct max8997_haptic *chip)
  71. {
  72. u8 value;
  73. value = chip->type << MAX8997_MOTOR_TYPE_SHIFT |
  74. chip->enabled << MAX8997_ENABLE_SHIFT |
  75. chip->mode << MAX8997_MODE_SHIFT | chip->pwm_divisor;
  76. max8997_write_reg(chip->client, MAX8997_HAPTIC_REG_CONF2, value);
  77. if (chip->mode == MAX8997_INTERNAL_MODE && chip->enabled) {
  78. value = chip->internal_mode_pattern << MAX8997_CYCLE_SHIFT |
  79. chip->internal_mode_pattern << MAX8997_SIG_PERIOD_SHIFT |
  80. chip->internal_mode_pattern << MAX8997_SIG_DUTY_SHIFT |
  81. chip->internal_mode_pattern << MAX8997_PWM_DUTY_SHIFT;
  82. max8997_write_reg(chip->client,
  83. MAX8997_HAPTIC_REG_DRVCONF, value);
  84. switch (chip->internal_mode_pattern) {
  85. case 0:
  86. value = chip->pattern_cycle << 4;
  87. max8997_write_reg(chip->client,
  88. MAX8997_HAPTIC_REG_CYCLECONF1, value);
  89. value = chip->pattern_signal_period;
  90. max8997_write_reg(chip->client,
  91. MAX8997_HAPTIC_REG_SIGCONF1, value);
  92. break;
  93. case 1:
  94. value = chip->pattern_cycle;
  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_SIGCONF2, value);
  100. break;
  101. case 2:
  102. value = chip->pattern_cycle << 4;
  103. max8997_write_reg(chip->client,
  104. MAX8997_HAPTIC_REG_CYCLECONF2, value);
  105. value = chip->pattern_signal_period;
  106. max8997_write_reg(chip->client,
  107. MAX8997_HAPTIC_REG_SIGCONF3, value);
  108. break;
  109. case 3:
  110. value = chip->pattern_cycle;
  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_SIGCONF4, value);
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. }
  122. static void max8997_haptic_enable(struct max8997_haptic *chip)
  123. {
  124. int error;
  125. guard(mutex)(&chip->mutex);
  126. if (chip->mode != MAX8997_EXTERNAL_MODE)
  127. max8997_haptic_set_internal_duty_cycle(chip);
  128. if (!chip->enabled) {
  129. error = regulator_enable(chip->regulator);
  130. if (error) {
  131. dev_err(chip->dev, "Failed to enable regulator\n");
  132. return;
  133. }
  134. max8997_haptic_configure(chip);
  135. }
  136. /*
  137. * It would be more straight forward to configure the external PWM
  138. * earlier i.e. when the internal duty_cycle is setup in internal mode.
  139. * But historically this is done only after the regulator was enabled
  140. * and max8997_haptic_configure() set the enable bit in
  141. * MAX8997_HAPTIC_REG_CONF2. So better keep it this way.
  142. */
  143. if (chip->mode == MAX8997_EXTERNAL_MODE) {
  144. struct pwm_state state;
  145. pwm_init_state(chip->pwm, &state);
  146. state.period = chip->pwm_period;
  147. state.duty_cycle = chip->pwm_period * chip->level / 100;
  148. state.enabled = true;
  149. error = pwm_apply_might_sleep(chip->pwm, &state);
  150. if (error) {
  151. dev_err(chip->dev, "Failed to enable PWM\n");
  152. regulator_disable(chip->regulator);
  153. return;
  154. }
  155. }
  156. chip->enabled = true;
  157. }
  158. static void max8997_haptic_disable(struct max8997_haptic *chip)
  159. {
  160. guard(mutex)(&chip->mutex);
  161. if (chip->enabled) {
  162. chip->enabled = false;
  163. max8997_haptic_configure(chip);
  164. if (chip->mode == MAX8997_EXTERNAL_MODE)
  165. pwm_disable(chip->pwm);
  166. regulator_disable(chip->regulator);
  167. }
  168. }
  169. static void max8997_haptic_play_effect_work(struct work_struct *work)
  170. {
  171. struct max8997_haptic *chip =
  172. container_of(work, struct max8997_haptic, work);
  173. if (chip->level)
  174. max8997_haptic_enable(chip);
  175. else
  176. max8997_haptic_disable(chip);
  177. }
  178. static int max8997_haptic_play_effect(struct input_dev *dev, void *data,
  179. struct ff_effect *effect)
  180. {
  181. struct max8997_haptic *chip = input_get_drvdata(dev);
  182. chip->level = effect->u.rumble.strong_magnitude;
  183. if (!chip->level)
  184. chip->level = effect->u.rumble.weak_magnitude;
  185. schedule_work(&chip->work);
  186. return 0;
  187. }
  188. static void max8997_haptic_close(struct input_dev *dev)
  189. {
  190. struct max8997_haptic *chip = input_get_drvdata(dev);
  191. cancel_work_sync(&chip->work);
  192. max8997_haptic_disable(chip);
  193. }
  194. static int max8997_haptic_probe(struct platform_device *pdev)
  195. {
  196. struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  197. const struct max8997_platform_data *pdata =
  198. dev_get_platdata(iodev->dev);
  199. const struct max8997_haptic_platform_data *haptic_pdata = NULL;
  200. struct max8997_haptic *chip;
  201. struct input_dev *input_dev;
  202. int error;
  203. if (pdata)
  204. haptic_pdata = pdata->haptic_pdata;
  205. if (!haptic_pdata) {
  206. dev_err(&pdev->dev, "no haptic platform data\n");
  207. return -EINVAL;
  208. }
  209. chip = kzalloc_obj(*chip);
  210. input_dev = input_allocate_device();
  211. if (!chip || !input_dev) {
  212. dev_err(&pdev->dev, "unable to allocate memory\n");
  213. error = -ENOMEM;
  214. goto err_free_mem;
  215. }
  216. INIT_WORK(&chip->work, max8997_haptic_play_effect_work);
  217. mutex_init(&chip->mutex);
  218. chip->client = iodev->haptic;
  219. chip->dev = &pdev->dev;
  220. chip->input_dev = input_dev;
  221. chip->pwm_period = haptic_pdata->pwm_period;
  222. chip->type = haptic_pdata->type;
  223. chip->mode = haptic_pdata->mode;
  224. chip->pwm_divisor = haptic_pdata->pwm_divisor;
  225. switch (chip->mode) {
  226. case MAX8997_INTERNAL_MODE:
  227. chip->internal_mode_pattern =
  228. haptic_pdata->internal_mode_pattern;
  229. chip->pattern_cycle = haptic_pdata->pattern_cycle;
  230. chip->pattern_signal_period =
  231. haptic_pdata->pattern_signal_period;
  232. break;
  233. case MAX8997_EXTERNAL_MODE:
  234. chip->pwm = pwm_get(&pdev->dev, NULL);
  235. if (IS_ERR(chip->pwm)) {
  236. error = PTR_ERR(chip->pwm);
  237. dev_err(&pdev->dev,
  238. "unable to request PWM for haptic, error: %d\n",
  239. error);
  240. goto err_free_mem;
  241. }
  242. break;
  243. default:
  244. dev_err(&pdev->dev,
  245. "Invalid chip mode specified (%d)\n", chip->mode);
  246. error = -EINVAL;
  247. goto err_free_mem;
  248. }
  249. chip->regulator = regulator_get(&pdev->dev, "inmotor");
  250. if (IS_ERR(chip->regulator)) {
  251. error = PTR_ERR(chip->regulator);
  252. dev_err(&pdev->dev,
  253. "unable to get regulator, error: %d\n",
  254. error);
  255. goto err_free_pwm;
  256. }
  257. input_dev->name = "max8997-haptic";
  258. input_dev->id.version = 1;
  259. input_dev->dev.parent = &pdev->dev;
  260. input_dev->close = max8997_haptic_close;
  261. input_set_drvdata(input_dev, chip);
  262. input_set_capability(input_dev, EV_FF, FF_RUMBLE);
  263. error = input_ff_create_memless(input_dev, NULL,
  264. max8997_haptic_play_effect);
  265. if (error) {
  266. dev_err(&pdev->dev,
  267. "unable to create FF device, error: %d\n",
  268. error);
  269. goto err_put_regulator;
  270. }
  271. error = input_register_device(input_dev);
  272. if (error) {
  273. dev_err(&pdev->dev,
  274. "unable to register input device, error: %d\n",
  275. error);
  276. goto err_destroy_ff;
  277. }
  278. platform_set_drvdata(pdev, chip);
  279. return 0;
  280. err_destroy_ff:
  281. input_ff_destroy(input_dev);
  282. err_put_regulator:
  283. regulator_put(chip->regulator);
  284. err_free_pwm:
  285. if (chip->mode == MAX8997_EXTERNAL_MODE)
  286. pwm_put(chip->pwm);
  287. err_free_mem:
  288. input_free_device(input_dev);
  289. kfree(chip);
  290. return error;
  291. }
  292. static void max8997_haptic_remove(struct platform_device *pdev)
  293. {
  294. struct max8997_haptic *chip = platform_get_drvdata(pdev);
  295. input_unregister_device(chip->input_dev);
  296. regulator_put(chip->regulator);
  297. if (chip->mode == MAX8997_EXTERNAL_MODE)
  298. pwm_put(chip->pwm);
  299. kfree(chip);
  300. }
  301. static int max8997_haptic_suspend(struct device *dev)
  302. {
  303. struct platform_device *pdev = to_platform_device(dev);
  304. struct max8997_haptic *chip = platform_get_drvdata(pdev);
  305. max8997_haptic_disable(chip);
  306. return 0;
  307. }
  308. static DEFINE_SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops,
  309. max8997_haptic_suspend, NULL);
  310. static const struct platform_device_id max8997_haptic_id[] = {
  311. { "max8997-haptic", 0 },
  312. { },
  313. };
  314. MODULE_DEVICE_TABLE(platform, max8997_haptic_id);
  315. static struct platform_driver max8997_haptic_driver = {
  316. .driver = {
  317. .name = "max8997-haptic",
  318. .pm = pm_sleep_ptr(&max8997_haptic_pm_ops),
  319. },
  320. .probe = max8997_haptic_probe,
  321. .remove = max8997_haptic_remove,
  322. .id_table = max8997_haptic_id,
  323. };
  324. module_platform_driver(max8997_haptic_driver);
  325. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  326. MODULE_DESCRIPTION("max8997_haptic driver");
  327. MODULE_LICENSE("GPL");