max8997_haptic.c 10 KB

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