max8997_haptic.c 10 KB

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