pmic8058-vib-memless.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* Copyright (c) 2010, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pmic8058-vibrator.h>
  19. #include <linux/mfd/pmic8058.h>
  20. #include <linux/pm.h>
  21. #include <linux/input.h>
  22. #include <linux/slab.h>
  23. #define VIB_DRV 0x4A
  24. #define VIB_DRV_SEL_MASK 0xf8
  25. #define VIB_DRV_SEL_SHIFT 0x03
  26. #define VIB_DRV_EN_MANUAL_MASK 0xfc
  27. #define VIB_MAX_LEVEL_mV (3100)
  28. #define VIB_MIN_LEVEL_mV (1200)
  29. #define VIB_MAX_LEVELS (VIB_MAX_LEVEL_mV - VIB_MIN_LEVEL_mV)
  30. #define MAX_FF_SPEED 0xff
  31. struct pmic8058_vib {
  32. struct input_dev *info;
  33. spinlock_t lock;
  34. struct work_struct work;
  35. bool enabled;
  36. int speed;
  37. struct device *dev;
  38. struct pmic8058_vibrator_pdata *pdata;
  39. int state;
  40. int level;
  41. u8 reg_vib_drv;
  42. struct pm8058_chip *pm_chip;
  43. };
  44. /* REVISIT: just for debugging, will be removed in final working version */
  45. static void __dump_vib_regs(struct pmic8058_vib *vib, char *msg)
  46. {
  47. u8 temp;
  48. dev_dbg(vib->dev, "%s\n", msg);
  49. pm8058_read(vib->pm_chip, VIB_DRV, &temp, 1);
  50. dev_dbg(vib->dev, "VIB_DRV - %X\n", temp);
  51. }
  52. static int pmic8058_vib_read_u8(struct pmic8058_vib *vib,
  53. u8 *data, u16 reg)
  54. {
  55. int rc;
  56. rc = pm8058_read(vib->pm_chip, reg, data, 1);
  57. if (rc < 0)
  58. dev_warn(vib->dev, "Error reading pmic8058: %X - ret %X\n",
  59. reg, rc);
  60. return rc;
  61. }
  62. static int pmic8058_vib_write_u8(struct pmic8058_vib *vib,
  63. u8 data, u16 reg)
  64. {
  65. int rc;
  66. rc = pm8058_write(vib->pm_chip, reg, &data, 1);
  67. if (rc < 0)
  68. dev_warn(vib->dev, "Error writing pmic8058: %X - ret %X\n",
  69. reg, rc);
  70. return rc;
  71. }
  72. static int pmic8058_vib_set(struct pmic8058_vib *vib, int on)
  73. {
  74. int rc;
  75. u8 val;
  76. if (on) {
  77. val = vib->reg_vib_drv;
  78. val |= ((vib->level << VIB_DRV_SEL_SHIFT) & VIB_DRV_SEL_MASK);
  79. rc = pmic8058_vib_write_u8(vib, val, VIB_DRV);
  80. if (rc < 0)
  81. return rc;
  82. vib->reg_vib_drv = val;
  83. vib->enabled = 1;
  84. } else {
  85. val = vib->reg_vib_drv;
  86. val &= ~VIB_DRV_SEL_MASK;
  87. rc = pmic8058_vib_write_u8(vib, val, VIB_DRV);
  88. if (rc < 0)
  89. return rc;
  90. vib->reg_vib_drv = val;
  91. vib->enabled = 0;
  92. }
  93. __dump_vib_regs(vib, "vib_set_end");
  94. return rc;
  95. }
  96. static void pmic8058_work_handler(struct work_struct *work)
  97. {
  98. u8 val;
  99. int rc;
  100. struct pmic8058_vib *info;
  101. info = container_of(work, struct pmic8058_vib, work);
  102. rc = pmic8058_vib_read_u8(info, &val, VIB_DRV);
  103. if (rc < 0)
  104. return;
  105. /*
  106. * Vibrator support voltage ranges from 1.2 to 3.1V, so
  107. * scale the FF speed to these range.
  108. */
  109. if (info->speed) {
  110. info->state = 1;
  111. info->level = ((VIB_MAX_LEVELS * info->speed) / MAX_FF_SPEED) +
  112. VIB_MIN_LEVEL_mV;
  113. info->level /= 100;
  114. } else {
  115. info->state = 0;
  116. info->level = VIB_MIN_LEVEL_mV / 100;
  117. }
  118. pmic8058_vib_set(info, info->state);
  119. }
  120. static int pmic8058_vib_play_effect(struct input_dev *dev, void *data,
  121. struct ff_effect *effect)
  122. {
  123. struct pmic8058_vib *info = input_get_drvdata(dev);
  124. info->speed = effect->u.rumble.strong_magnitude >> 8;
  125. if (!info->speed)
  126. info->speed = effect->u.rumble.weak_magnitude >> 9;
  127. schedule_work(&info->work);
  128. return 0;
  129. }
  130. static int __devinit pmic8058_vib_probe(struct platform_device *pdev)
  131. {
  132. struct pmic8058_vibrator_pdata *pdata = pdev->dev.platform_data;
  133. struct pmic8058_vib *vib;
  134. u8 val;
  135. int rc;
  136. struct pm8058_chip *pm_chip;
  137. pm_chip = dev_get_drvdata(pdev->parent.dev);
  138. if (pm_chip == NULL) {
  139. dev_err(&pdev->dev, "no parent data passed in\n");
  140. return -EFAULT;
  141. }
  142. if (!pdata)
  143. return -EINVAL;
  144. if (pdata->level_mV < VIB_MIN_LEVEL_mV ||
  145. pdata->level_mV > VIB_MAX_LEVEL_mV)
  146. return -EINVAL;
  147. vib = kzalloc(sizeof(*vib), GFP_KERNEL);
  148. if (!vib)
  149. return -ENOMEM;
  150. vib->pm_chip = pm_chip;
  151. vib->enabled = 0;
  152. vib->pdata = pdata;
  153. vib->level = pdata->level_mV / 100;
  154. vib->dev = &pdev->dev;
  155. spin_lock_init(&vib->lock);
  156. INIT_WORK(&vib->work, pmic8058_work_handler);
  157. vib->info = input_allocate_device();
  158. if (vib->info == NULL) {
  159. dev_err(&pdev->dev, "couldn't allocate input device\n");
  160. return -ENOMEM;
  161. }
  162. input_set_drvdata(vib->info, vib);
  163. vib->info->name = "pmic8058:vibrator";
  164. vib->info->id.version = 1;
  165. vib->info->dev.parent = pdev->dev.parent;
  166. __set_bit(FF_RUMBLE, vib->info->ffbit);
  167. __dump_vib_regs(vib, "boot_vib_default");
  168. /* operate in manual mode */
  169. rc = pmic8058_vib_read_u8(vib, &val, VIB_DRV);
  170. if (rc < 0)
  171. goto err_read_vib;
  172. val &= ~VIB_DRV_EN_MANUAL_MASK;
  173. rc = pmic8058_vib_write_u8(vib, val, VIB_DRV);
  174. if (rc < 0)
  175. goto err_read_vib;
  176. vib->reg_vib_drv = val;
  177. rc = input_ff_create_memless(vib->info, NULL, pmic8058_vib_play_effect);
  178. if (rc < 0) {
  179. dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
  180. goto create_memless_err;
  181. }
  182. platform_set_drvdata(pdev, vib);
  183. rc = input_register_device(vib->info);
  184. if (rc < 0) {
  185. dev_dbg(&pdev->dev, "couldn't register input device\n");
  186. goto reg_err;
  187. }
  188. return 0;
  189. reg_err:
  190. input_ff_destroy(vib->info);
  191. create_memless_err:
  192. input_free_device(vib->info);
  193. err_read_vib:
  194. kfree(vib);
  195. return rc;
  196. }
  197. static int __devexit pmic8058_vib_remove(struct platform_device *pdev)
  198. {
  199. struct pmic8058_vib *vib = platform_get_drvdata(pdev);
  200. cancel_work_sync(&vib->work);
  201. if (vib->enabled)
  202. pmic8058_vib_set(vib, 0);
  203. input_unregister_device(vib->info);
  204. kfree(vib);
  205. return 0;
  206. }
  207. static struct platform_driver pmic8058_vib_driver = {
  208. .probe = pmic8058_vib_probe,
  209. .remove = __devexit_p(pmic8058_vib_remove),
  210. .driver = {
  211. .name = "pm8058-vib",
  212. .owner = THIS_MODULE,
  213. },
  214. };
  215. static int __init pmic8058_vib_init(void)
  216. {
  217. return platform_driver_register(&pmic8058_vib_driver);
  218. }
  219. module_init(pmic8058_vib_init);
  220. static void __exit pmic8058_vib_exit(void)
  221. {
  222. platform_driver_unregister(&pmic8058_vib_driver);
  223. }
  224. module_exit(pmic8058_vib_exit);
  225. MODULE_ALIAS("platform:pmic8058_vib");
  226. MODULE_DESCRIPTION("PMIC8058 vibrator driver memless framework");
  227. MODULE_LICENSE("GPL v2");