max8997_haptic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 = NULL;
  222. struct max8997_haptic *chip;
  223. struct input_dev *input_dev;
  224. int error;
  225. if (pdata)
  226. haptic_pdata = pdata->haptic_pdata;
  227. if (!haptic_pdata) {
  228. dev_err(&pdev->dev, "no haptic platform data\n");
  229. return -EINVAL;
  230. }
  231. chip = kzalloc(sizeof(struct max8997_haptic), GFP_KERNEL);
  232. input_dev = input_allocate_device();
  233. if (!chip || !input_dev) {
  234. dev_err(&pdev->dev, "unable to allocate memory\n");
  235. error = -ENOMEM;
  236. goto err_free_mem;
  237. }
  238. INIT_WORK(&chip->work, max8997_haptic_play_effect_work);
  239. mutex_init(&chip->mutex);
  240. chip->client = iodev->haptic;
  241. chip->dev = &pdev->dev;
  242. chip->input_dev = input_dev;
  243. chip->pwm_period = haptic_pdata->pwm_period;
  244. chip->type = haptic_pdata->type;
  245. chip->mode = haptic_pdata->mode;
  246. chip->pwm_divisor = haptic_pdata->pwm_divisor;
  247. switch (chip->mode) {
  248. case MAX8997_INTERNAL_MODE:
  249. chip->internal_mode_pattern =
  250. haptic_pdata->internal_mode_pattern;
  251. chip->pattern_cycle = haptic_pdata->pattern_cycle;
  252. chip->pattern_signal_period =
  253. haptic_pdata->pattern_signal_period;
  254. break;
  255. case MAX8997_EXTERNAL_MODE:
  256. chip->pwm = pwm_request(haptic_pdata->pwm_channel_id,
  257. "max8997-haptic");
  258. if (IS_ERR(chip->pwm)) {
  259. error = PTR_ERR(chip->pwm);
  260. dev_err(&pdev->dev,
  261. "unable to request PWM for haptic, error: %d\n",
  262. error);
  263. goto err_free_mem;
  264. }
  265. break;
  266. default:
  267. dev_err(&pdev->dev,
  268. "Invalid chip mode specified (%d)\n", chip->mode);
  269. error = -EINVAL;
  270. goto err_free_mem;
  271. }
  272. chip->regulator = regulator_get(&pdev->dev, "inmotor");
  273. if (IS_ERR(chip->regulator)) {
  274. error = PTR_ERR(chip->regulator);
  275. dev_err(&pdev->dev,
  276. "unable to get regulator, error: %d\n",
  277. error);
  278. goto err_free_pwm;
  279. }
  280. input_dev->name = "max8997-haptic";
  281. input_dev->id.version = 1;
  282. input_dev->dev.parent = &pdev->dev;
  283. input_dev->close = max8997_haptic_close;
  284. input_set_drvdata(input_dev, chip);
  285. input_set_capability(input_dev, EV_FF, FF_RUMBLE);
  286. error = input_ff_create_memless(input_dev, NULL,
  287. max8997_haptic_play_effect);
  288. if (error) {
  289. dev_err(&pdev->dev,
  290. "unable to create FF device, error: %d\n",
  291. error);
  292. goto err_put_regulator;
  293. }
  294. error = input_register_device(input_dev);
  295. if (error) {
  296. dev_err(&pdev->dev,
  297. "unable to register input device, error: %d\n",
  298. error);
  299. goto err_destroy_ff;
  300. }
  301. platform_set_drvdata(pdev, chip);
  302. return 0;
  303. err_destroy_ff:
  304. input_ff_destroy(input_dev);
  305. err_put_regulator:
  306. regulator_put(chip->regulator);
  307. err_free_pwm:
  308. if (chip->mode == MAX8997_EXTERNAL_MODE)
  309. pwm_free(chip->pwm);
  310. err_free_mem:
  311. input_free_device(input_dev);
  312. kfree(chip);
  313. return error;
  314. }
  315. static int max8997_haptic_remove(struct platform_device *pdev)
  316. {
  317. struct max8997_haptic *chip = platform_get_drvdata(pdev);
  318. input_unregister_device(chip->input_dev);
  319. regulator_put(chip->regulator);
  320. if (chip->mode == MAX8997_EXTERNAL_MODE)
  321. pwm_free(chip->pwm);
  322. kfree(chip);
  323. return 0;
  324. }
  325. static int __maybe_unused max8997_haptic_suspend(struct device *dev)
  326. {
  327. struct platform_device *pdev = to_platform_device(dev);
  328. struct max8997_haptic *chip = platform_get_drvdata(pdev);
  329. max8997_haptic_disable(chip);
  330. return 0;
  331. }
  332. static SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops, max8997_haptic_suspend, NULL);
  333. static const struct platform_device_id max8997_haptic_id[] = {
  334. { "max8997-haptic", 0 },
  335. { },
  336. };
  337. MODULE_DEVICE_TABLE(platform, max8997_haptic_id);
  338. static struct platform_driver max8997_haptic_driver = {
  339. .driver = {
  340. .name = "max8997-haptic",
  341. .pm = &max8997_haptic_pm_ops,
  342. },
  343. .probe = max8997_haptic_probe,
  344. .remove = max8997_haptic_remove,
  345. .id_table = max8997_haptic_id,
  346. };
  347. module_platform_driver(max8997_haptic_driver);
  348. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  349. MODULE_DESCRIPTION("max8997_haptic driver");
  350. MODULE_LICENSE("GPL");