qpnp-vibrator.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* Copyright (c) 2013, 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/slab.h>
  17. #include <linux/hrtimer.h>
  18. #include <linux/of_device.h>
  19. #include <linux/spmi.h>
  20. #include <linux/qpnp/vibrator.h>
  21. #include "../../staging/android/timed_output.h"
  22. #define QPNP_VIB_VTG_CTL(base) (base + 0x41)
  23. #define QPNP_VIB_EN_CTL(base) (base + 0x46)
  24. #define QPNP_VIB_MAX_LEVEL 31
  25. #define QPNP_VIB_MIN_LEVEL 12
  26. #define QPNP_VIB_DEFAULT_TIMEOUT 15000
  27. #define QPNP_VIB_DEFAULT_VTG_LVL 3100
  28. #define QPNP_VIB_EN BIT(7)
  29. #define QPNP_VIB_VTG_SET_MASK 0x1F
  30. #define QPNP_VIB_LOGIC_SHIFT 4
  31. struct qpnp_vib {
  32. struct spmi_device *spmi;
  33. struct hrtimer vib_timer;
  34. struct timed_output_dev timed_dev;
  35. struct work_struct work;
  36. u8 reg_vtg_ctl;
  37. u8 reg_en_ctl;
  38. u16 base;
  39. int state;
  40. int vtg_level;
  41. int timeout;
  42. struct mutex lock;
  43. };
  44. static struct qpnp_vib *vib_dev;
  45. static int qpnp_vib_read_u8(struct qpnp_vib *vib, u8 *data, u16 reg)
  46. {
  47. int rc;
  48. rc = spmi_ext_register_readl(vib->spmi->ctrl, vib->spmi->sid,
  49. reg, data, 1);
  50. if (rc < 0)
  51. dev_err(&vib->spmi->dev,
  52. "Error reading address: %X - ret %X\n", reg, rc);
  53. return rc;
  54. }
  55. static int qpnp_vib_write_u8(struct qpnp_vib *vib, u8 *data, u16 reg)
  56. {
  57. int rc;
  58. rc = spmi_ext_register_writel(vib->spmi->ctrl, vib->spmi->sid,
  59. reg, data, 1);
  60. if (rc < 0)
  61. dev_err(&vib->spmi->dev,
  62. "Error writing address: %X - ret %X\n", reg, rc);
  63. return rc;
  64. }
  65. int qpnp_vibrator_config(struct qpnp_vib_config *vib_cfg)
  66. {
  67. u8 reg = 0;
  68. int rc = -EINVAL, level;
  69. if (vib_dev == NULL) {
  70. pr_err("%s: vib_dev is NULL\n", __func__);
  71. return -ENODEV;
  72. }
  73. level = vib_cfg->drive_mV / 100;
  74. if (level) {
  75. if ((level < QPNP_VIB_MIN_LEVEL) ||
  76. (level > QPNP_VIB_MAX_LEVEL)) {
  77. dev_err(&vib_dev->spmi->dev, "Invalid voltage level\n");
  78. return -EINVAL;
  79. }
  80. } else {
  81. dev_err(&vib_dev->spmi->dev, "Voltage level not specified\n");
  82. return -EINVAL;
  83. }
  84. /* Configure the VTG CTL regiser */
  85. reg = vib_dev->reg_vtg_ctl;
  86. reg &= ~QPNP_VIB_VTG_SET_MASK;
  87. reg |= (level & QPNP_VIB_VTG_SET_MASK);
  88. rc = qpnp_vib_write_u8(vib_dev, &reg, QPNP_VIB_VTG_CTL(vib_dev->base));
  89. if (rc)
  90. return rc;
  91. vib_dev->reg_vtg_ctl = reg;
  92. /* Configure the VIB ENABLE regiser */
  93. reg = vib_dev->reg_en_ctl;
  94. reg |= (!!vib_cfg->active_low) << QPNP_VIB_LOGIC_SHIFT;
  95. if (vib_cfg->enable_mode == QPNP_VIB_MANUAL)
  96. reg |= QPNP_VIB_EN;
  97. else
  98. reg |= BIT(vib_cfg->enable_mode - 1);
  99. rc = qpnp_vib_write_u8(vib_dev, &reg, QPNP_VIB_EN_CTL(vib_dev->base));
  100. if (rc < 0)
  101. return rc;
  102. vib_dev->reg_en_ctl = reg;
  103. return rc;
  104. }
  105. EXPORT_SYMBOL(qpnp_vibrator_config);
  106. static int qpnp_vib_set(struct qpnp_vib *vib, int on)
  107. {
  108. int rc;
  109. u8 val;
  110. if (on) {
  111. val = vib->reg_vtg_ctl;
  112. val &= ~QPNP_VIB_VTG_SET_MASK;
  113. val |= (vib->vtg_level & QPNP_VIB_VTG_SET_MASK);
  114. rc = qpnp_vib_write_u8(vib, &val, QPNP_VIB_VTG_CTL(vib->base));
  115. if (rc < 0)
  116. return rc;
  117. vib->reg_vtg_ctl = val;
  118. val = vib->reg_en_ctl;
  119. val |= QPNP_VIB_EN;
  120. rc = qpnp_vib_write_u8(vib, &val, QPNP_VIB_EN_CTL(vib->base));
  121. if (rc < 0)
  122. return rc;
  123. vib->reg_en_ctl = val;
  124. } else {
  125. val = vib->reg_en_ctl;
  126. val &= ~QPNP_VIB_EN;
  127. rc = qpnp_vib_write_u8(vib, &val, QPNP_VIB_EN_CTL(vib->base));
  128. if (rc < 0)
  129. return rc;
  130. vib->reg_en_ctl = val;
  131. }
  132. return rc;
  133. }
  134. static void qpnp_vib_enable(struct timed_output_dev *dev, int value)
  135. {
  136. struct qpnp_vib *vib = container_of(dev, struct qpnp_vib,
  137. timed_dev);
  138. mutex_lock(&vib->lock);
  139. hrtimer_cancel(&vib->vib_timer);
  140. if (value == 0)
  141. vib->state = 0;
  142. else {
  143. value = (value > vib->timeout ?
  144. vib->timeout : value);
  145. vib->state = 1;
  146. hrtimer_start(&vib->vib_timer,
  147. ktime_set(value / 1000, (value % 1000) * 1000000),
  148. HRTIMER_MODE_REL);
  149. }
  150. mutex_unlock(&vib->lock);
  151. schedule_work(&vib->work);
  152. }
  153. static void qpnp_vib_update(struct work_struct *work)
  154. {
  155. struct qpnp_vib *vib = container_of(work, struct qpnp_vib,
  156. work);
  157. qpnp_vib_set(vib, vib->state);
  158. }
  159. static int qpnp_vib_get_time(struct timed_output_dev *dev)
  160. {
  161. struct qpnp_vib *vib = container_of(dev, struct qpnp_vib,
  162. timed_dev);
  163. if (hrtimer_active(&vib->vib_timer)) {
  164. ktime_t r = hrtimer_get_remaining(&vib->vib_timer);
  165. return (int)ktime_to_us(r);
  166. } else
  167. return 0;
  168. }
  169. static enum hrtimer_restart qpnp_vib_timer_func(struct hrtimer *timer)
  170. {
  171. struct qpnp_vib *vib = container_of(timer, struct qpnp_vib,
  172. vib_timer);
  173. vib->state = 0;
  174. schedule_work(&vib->work);
  175. return HRTIMER_NORESTART;
  176. }
  177. #ifdef CONFIG_PM
  178. static int qpnp_vibrator_suspend(struct device *dev)
  179. {
  180. struct qpnp_vib *vib = dev_get_drvdata(dev);
  181. hrtimer_cancel(&vib->vib_timer);
  182. cancel_work_sync(&vib->work);
  183. /* turn-off vibrator */
  184. qpnp_vib_set(vib, 0);
  185. return 0;
  186. }
  187. #endif
  188. static SIMPLE_DEV_PM_OPS(qpnp_vibrator_pm_ops, qpnp_vibrator_suspend, NULL);
  189. static int __devinit qpnp_vibrator_probe(struct spmi_device *spmi)
  190. {
  191. struct qpnp_vib *vib;
  192. struct resource *vib_resource;
  193. int rc;
  194. u8 val;
  195. u32 temp_val;
  196. vib = devm_kzalloc(&spmi->dev, sizeof(*vib), GFP_KERNEL);
  197. if (!vib)
  198. return -ENOMEM;
  199. vib->spmi = spmi;
  200. vib->timeout = QPNP_VIB_DEFAULT_TIMEOUT;
  201. rc = of_property_read_u32(spmi->dev.of_node,
  202. "qcom,vib-timeout-ms", &temp_val);
  203. if (!rc) {
  204. vib->timeout = temp_val;
  205. } else if (rc != EINVAL) {
  206. dev_err(&spmi->dev, "Unable to read vib timeout\n");
  207. return rc;
  208. }
  209. vib->vtg_level = QPNP_VIB_DEFAULT_VTG_LVL;
  210. rc = of_property_read_u32(spmi->dev.of_node,
  211. "qcom,vib-vtg-level-mV", &temp_val);
  212. if (!rc) {
  213. vib->vtg_level = temp_val;
  214. } else if (rc != -EINVAL) {
  215. dev_err(&spmi->dev, "Unable to read vtg level\n");
  216. return rc;
  217. }
  218. vib->vtg_level /= 100;
  219. vib_resource = spmi_get_resource(spmi, 0, IORESOURCE_MEM, 0);
  220. if (!vib_resource) {
  221. dev_err(&spmi->dev, "Unable to get vibrator base address\n");
  222. return -EINVAL;
  223. }
  224. vib->base = vib_resource->start;
  225. /* save the control registers values */
  226. rc = qpnp_vib_read_u8(vib, &val, QPNP_VIB_VTG_CTL(vib->base));
  227. if (rc < 0)
  228. return rc;
  229. vib->reg_vtg_ctl = val;
  230. rc = qpnp_vib_read_u8(vib, &val, QPNP_VIB_EN_CTL(vib->base));
  231. if (rc < 0)
  232. return rc;
  233. vib->reg_en_ctl = val;
  234. mutex_init(&vib->lock);
  235. INIT_WORK(&vib->work, qpnp_vib_update);
  236. hrtimer_init(&vib->vib_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  237. vib->vib_timer.function = qpnp_vib_timer_func;
  238. vib->timed_dev.name = "vibrator";
  239. vib->timed_dev.get_time = qpnp_vib_get_time;
  240. vib->timed_dev.enable = qpnp_vib_enable;
  241. dev_set_drvdata(&spmi->dev, vib);
  242. rc = timed_output_dev_register(&vib->timed_dev);
  243. if (rc < 0)
  244. return rc;
  245. vib_dev = vib;
  246. return rc;
  247. }
  248. static int __devexit qpnp_vibrator_remove(struct spmi_device *spmi)
  249. {
  250. struct qpnp_vib *vib = dev_get_drvdata(&spmi->dev);
  251. cancel_work_sync(&vib->work);
  252. hrtimer_cancel(&vib->vib_timer);
  253. timed_output_dev_unregister(&vib->timed_dev);
  254. mutex_destroy(&vib->lock);
  255. return 0;
  256. }
  257. static struct of_device_id spmi_match_table[] = {
  258. { .compatible = "qcom,qpnp-vibrator",
  259. },
  260. {}
  261. };
  262. static struct spmi_driver qpnp_vibrator_driver = {
  263. .driver = {
  264. .name = "qcom,qpnp-vibrator",
  265. .of_match_table = spmi_match_table,
  266. .pm = &qpnp_vibrator_pm_ops,
  267. },
  268. .probe = qpnp_vibrator_probe,
  269. .remove = __devexit_p(qpnp_vibrator_remove),
  270. };
  271. static int __init qpnp_vibrator_init(void)
  272. {
  273. return spmi_driver_register(&qpnp_vibrator_driver);
  274. }
  275. module_init(qpnp_vibrator_init);
  276. static void __exit qpnp_vibrator_exit(void)
  277. {
  278. return spmi_driver_unregister(&qpnp_vibrator_driver);
  279. }
  280. module_exit(qpnp_vibrator_exit);
  281. MODULE_DESCRIPTION("qpnp vibrator driver");
  282. MODULE_LICENSE("GPL v2");