max8997_haptic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. chip->enabled = true;
  161. regulator_enable(chip->regulator);
  162. max8997_haptic_configure(chip);
  163. if (chip->mode == MAX8997_EXTERNAL_MODE)
  164. pwm_enable(chip->pwm);
  165. }
  166. out:
  167. mutex_unlock(&chip->mutex);
  168. }
  169. static void max8997_haptic_disable(struct max8997_haptic *chip)
  170. {
  171. mutex_lock(&chip->mutex);
  172. if (chip->enabled) {
  173. chip->enabled = false;
  174. max8997_haptic_configure(chip);
  175. if (chip->mode == MAX8997_EXTERNAL_MODE)
  176. pwm_disable(chip->pwm);
  177. regulator_disable(chip->regulator);
  178. }
  179. mutex_unlock(&chip->mutex);
  180. }
  181. static void max8997_haptic_play_effect_work(struct work_struct *work)
  182. {
  183. struct max8997_haptic *chip =
  184. container_of(work, struct max8997_haptic, work);
  185. if (chip->level)
  186. max8997_haptic_enable(chip);
  187. else
  188. max8997_haptic_disable(chip);
  189. }
  190. static int max8997_haptic_play_effect(struct input_dev *dev, void *data,
  191. struct ff_effect *effect)
  192. {
  193. struct max8997_haptic *chip = input_get_drvdata(dev);
  194. chip->level = effect->u.rumble.strong_magnitude;
  195. if (!chip->level)
  196. chip->level = effect->u.rumble.weak_magnitude;
  197. schedule_work(&chip->work);
  198. return 0;
  199. }
  200. static void max8997_haptic_close(struct input_dev *dev)
  201. {
  202. struct max8997_haptic *chip = input_get_drvdata(dev);
  203. cancel_work_sync(&chip->work);
  204. max8997_haptic_disable(chip);
  205. }
  206. static int max8997_haptic_probe(struct platform_device *pdev)
  207. {
  208. struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  209. const struct max8997_platform_data *pdata =
  210. dev_get_platdata(iodev->dev);
  211. const struct max8997_haptic_platform_data *haptic_pdata =
  212. pdata->haptic_pdata;
  213. struct max8997_haptic *chip;
  214. struct input_dev *input_dev;
  215. int error;
  216. if (!haptic_pdata) {
  217. dev_err(&pdev->dev, "no haptic platform data\n");
  218. return -EINVAL;
  219. }
  220. chip = kzalloc(sizeof(struct max8997_haptic), GFP_KERNEL);
  221. input_dev = input_allocate_device();
  222. if (!chip || !input_dev) {
  223. dev_err(&pdev->dev, "unable to allocate memory\n");
  224. error = -ENOMEM;
  225. goto err_free_mem;
  226. }
  227. INIT_WORK(&chip->work, max8997_haptic_play_effect_work);
  228. mutex_init(&chip->mutex);
  229. chip->client = iodev->haptic;
  230. chip->dev = &pdev->dev;
  231. chip->input_dev = input_dev;
  232. chip->pwm_period = haptic_pdata->pwm_period;
  233. chip->type = haptic_pdata->type;
  234. chip->mode = haptic_pdata->mode;
  235. chip->pwm_divisor = haptic_pdata->pwm_divisor;
  236. switch (chip->mode) {
  237. case MAX8997_INTERNAL_MODE:
  238. chip->internal_mode_pattern =
  239. haptic_pdata->internal_mode_pattern;
  240. chip->pattern_cycle = haptic_pdata->pattern_cycle;
  241. chip->pattern_signal_period =
  242. haptic_pdata->pattern_signal_period;
  243. break;
  244. case MAX8997_EXTERNAL_MODE:
  245. chip->pwm = pwm_request(haptic_pdata->pwm_channel_id,
  246. "max8997-haptic");
  247. if (IS_ERR(chip->pwm)) {
  248. error = PTR_ERR(chip->pwm);
  249. dev_err(&pdev->dev,
  250. "unable to request PWM for haptic, error: %d\n",
  251. error);
  252. goto err_free_mem;
  253. }
  254. break;
  255. default:
  256. dev_err(&pdev->dev,
  257. "Invalid chip mode specified (%d)\n", chip->mode);
  258. error = -EINVAL;
  259. goto err_free_mem;
  260. }
  261. chip->regulator = regulator_get(&pdev->dev, "inmotor");
  262. if (IS_ERR(chip->regulator)) {
  263. error = PTR_ERR(chip->regulator);
  264. dev_err(&pdev->dev,
  265. "unable to get regulator, error: %d\n",
  266. error);
  267. goto err_free_pwm;
  268. }
  269. input_dev->name = "max8997-haptic";
  270. input_dev->id.version = 1;
  271. input_dev->dev.parent = &pdev->dev;
  272. input_dev->close = max8997_haptic_close;
  273. input_set_drvdata(input_dev, chip);
  274. input_set_capability(input_dev, EV_FF, FF_RUMBLE);
  275. error = input_ff_create_memless(input_dev, NULL,
  276. max8997_haptic_play_effect);
  277. if (error) {
  278. dev_err(&pdev->dev,
  279. "unable to create FF device, error: %d\n",
  280. error);
  281. goto err_put_regulator;
  282. }
  283. error = input_register_device(input_dev);
  284. if (error) {
  285. dev_err(&pdev->dev,
  286. "unable to register input device, error: %d\n",
  287. error);
  288. goto err_destroy_ff;
  289. }
  290. platform_set_drvdata(pdev, chip);
  291. return 0;
  292. err_destroy_ff:
  293. input_ff_destroy(input_dev);
  294. err_put_regulator:
  295. regulator_put(chip->regulator);
  296. err_free_pwm:
  297. if (chip->mode == MAX8997_EXTERNAL_MODE)
  298. pwm_free(chip->pwm);
  299. err_free_mem:
  300. input_free_device(input_dev);
  301. kfree(chip);
  302. return error;
  303. }
  304. static int max8997_haptic_remove(struct platform_device *pdev)
  305. {
  306. struct max8997_haptic *chip = platform_get_drvdata(pdev);
  307. input_unregister_device(chip->input_dev);
  308. regulator_put(chip->regulator);
  309. if (chip->mode == MAX8997_EXTERNAL_MODE)
  310. pwm_free(chip->pwm);
  311. kfree(chip);
  312. return 0;
  313. }
  314. #ifdef CONFIG_PM_SLEEP
  315. static int max8997_haptic_suspend(struct device *dev)
  316. {
  317. struct platform_device *pdev = to_platform_device(dev);
  318. struct max8997_haptic *chip = platform_get_drvdata(pdev);
  319. max8997_haptic_disable(chip);
  320. return 0;
  321. }
  322. #endif
  323. static SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops, max8997_haptic_suspend, NULL);
  324. static const struct platform_device_id max8997_haptic_id[] = {
  325. { "max8997-haptic", 0 },
  326. { },
  327. };
  328. MODULE_DEVICE_TABLE(i2c, max8997_haptic_id);
  329. static struct platform_driver max8997_haptic_driver = {
  330. .driver = {
  331. .name = "max8997-haptic",
  332. .owner = THIS_MODULE,
  333. .pm = &max8997_haptic_pm_ops,
  334. },
  335. .probe = max8997_haptic_probe,
  336. .remove = max8997_haptic_remove,
  337. .id_table = max8997_haptic_id,
  338. };
  339. module_platform_driver(max8997_haptic_driver);
  340. MODULE_ALIAS("platform:max8997-haptic");
  341. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  342. MODULE_DESCRIPTION("max8997_haptic driver");
  343. MODULE_LICENSE("GPL");