leds-qpnp-vibrator-ldo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* Copyright (c) 2017-2018, 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. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/errno.h>
  14. #include <linux/hrtimer.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/leds.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/of_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/workqueue.h>
  23. /* Vibrator-LDO register definitions */
  24. #define QPNP_VIB_LDO_REG_STATUS1 0x08
  25. #define QPNP_VIB_LDO_VREG_READY BIT(7)
  26. #define QPNP_VIB_LDO_REG_VSET_LB 0x40
  27. #define QPNP_VIB_LDO_REG_EN_CTL 0x46
  28. #define QPNP_VIB_LDO_EN BIT(7)
  29. /* Vibrator-LDO voltage settings */
  30. #define QPNP_VIB_LDO_VMIN_UV 1504000
  31. #define QPNP_VIB_LDO_VMAX_UV 3544000
  32. #define QPNP_VIB_LDO_VOLT_STEP_UV 8000
  33. /*
  34. * Define vibration periods: default(5sec), min(50ms), max(15sec) and
  35. * overdrive(30ms).
  36. */
  37. #define QPNP_VIB_MIN_PLAY_MS 50
  38. #define QPNP_VIB_PLAY_MS 5000
  39. #define QPNP_VIB_MAX_PLAY_MS 15000
  40. #define QPNP_VIB_OVERDRIVE_PLAY_MS 30
  41. struct vib_ldo_chip {
  42. struct led_classdev cdev;
  43. struct regmap *regmap;
  44. struct mutex lock;
  45. struct hrtimer stop_timer;
  46. struct hrtimer overdrive_timer;
  47. struct work_struct vib_work;
  48. struct work_struct overdrive_work;
  49. u16 base;
  50. int vmax_uV;
  51. int overdrive_volt_uV;
  52. int ldo_uV;
  53. int state;
  54. u64 vib_play_ms;
  55. bool vib_enabled;
  56. bool disable_overdrive;
  57. };
  58. static int qpnp_vib_ldo_set_voltage(struct vib_ldo_chip *chip, int new_uV)
  59. {
  60. unsigned int val;
  61. u32 vlevel;
  62. u8 reg[2];
  63. int ret;
  64. if (chip->ldo_uV == new_uV)
  65. return 0;
  66. vlevel = roundup(new_uV, QPNP_VIB_LDO_VOLT_STEP_UV) / 1000;
  67. reg[0] = vlevel & 0xff;
  68. reg[1] = (vlevel & 0xff00) >> 8;
  69. ret = regmap_bulk_write(chip->regmap,
  70. chip->base + QPNP_VIB_LDO_REG_VSET_LB, reg, 2);
  71. if (ret < 0) {
  72. pr_err("regmap write failed, ret=%d\n", ret);
  73. return ret;
  74. }
  75. if (chip->vib_enabled) {
  76. ret = regmap_read_poll_timeout(chip->regmap,
  77. chip->base + QPNP_VIB_LDO_REG_STATUS1,
  78. val, val & QPNP_VIB_LDO_VREG_READY,
  79. 100, 1000);
  80. if (ret < 0) {
  81. pr_err("Vibrator LDO vreg_ready timeout, status=0x%02x, ret=%d\n",
  82. val, ret);
  83. return ret;
  84. }
  85. }
  86. chip->ldo_uV = new_uV;
  87. return ret;
  88. }
  89. static inline int qpnp_vib_ldo_enable(struct vib_ldo_chip *chip, bool enable)
  90. {
  91. unsigned int val;
  92. int ret;
  93. if (chip->vib_enabled == enable)
  94. return 0;
  95. ret = regmap_update_bits(chip->regmap,
  96. chip->base + QPNP_VIB_LDO_REG_EN_CTL,
  97. QPNP_VIB_LDO_EN,
  98. enable ? QPNP_VIB_LDO_EN : 0);
  99. if (ret < 0) {
  100. pr_err("Program Vibrator LDO %s is failed, ret=%d\n",
  101. enable ? "enable" : "disable", ret);
  102. return ret;
  103. }
  104. if (enable) {
  105. ret = regmap_read_poll_timeout(chip->regmap,
  106. chip->base + QPNP_VIB_LDO_REG_STATUS1,
  107. val, val & QPNP_VIB_LDO_VREG_READY,
  108. 100, 1000);
  109. if (ret < 0) {
  110. pr_err("Vibrator LDO vreg_ready timeout, status=0x%02x, ret=%d\n",
  111. val, ret);
  112. return ret;
  113. }
  114. }
  115. chip->vib_enabled = enable;
  116. return ret;
  117. }
  118. static int qpnp_vibrator_play_on(struct vib_ldo_chip *chip)
  119. {
  120. int volt_uV;
  121. int ret;
  122. volt_uV = chip->vmax_uV;
  123. if (!chip->disable_overdrive)
  124. volt_uV = chip->overdrive_volt_uV ? chip->overdrive_volt_uV
  125. : min(chip->vmax_uV * 2, QPNP_VIB_LDO_VMAX_UV);
  126. ret = qpnp_vib_ldo_set_voltage(chip, volt_uV);
  127. if (ret < 0) {
  128. pr_err("set voltage = %duV failed, ret=%d\n", volt_uV, ret);
  129. return ret;
  130. }
  131. pr_debug("voltage set to %d uV\n", volt_uV);
  132. ret = qpnp_vib_ldo_enable(chip, true);
  133. if (ret < 0) {
  134. pr_err("vibration enable failed, ret=%d\n", ret);
  135. return ret;
  136. }
  137. if (!chip->disable_overdrive)
  138. hrtimer_start(&chip->overdrive_timer,
  139. ms_to_ktime(QPNP_VIB_OVERDRIVE_PLAY_MS),
  140. HRTIMER_MODE_REL);
  141. return ret;
  142. }
  143. static void qpnp_vib_work(struct work_struct *work)
  144. {
  145. struct vib_ldo_chip *chip = container_of(work, struct vib_ldo_chip,
  146. vib_work);
  147. int ret = 0;
  148. if (chip->state) {
  149. if (!chip->vib_enabled)
  150. ret = qpnp_vibrator_play_on(chip);
  151. if (ret == 0)
  152. hrtimer_start(&chip->stop_timer,
  153. ms_to_ktime(chip->vib_play_ms),
  154. HRTIMER_MODE_REL);
  155. } else {
  156. if (!chip->disable_overdrive) {
  157. hrtimer_cancel(&chip->overdrive_timer);
  158. cancel_work_sync(&chip->overdrive_work);
  159. }
  160. qpnp_vib_ldo_enable(chip, false);
  161. }
  162. }
  163. static enum hrtimer_restart vib_stop_timer(struct hrtimer *timer)
  164. {
  165. struct vib_ldo_chip *chip = container_of(timer, struct vib_ldo_chip,
  166. stop_timer);
  167. chip->state = 0;
  168. schedule_work(&chip->vib_work);
  169. return HRTIMER_NORESTART;
  170. }
  171. static void qpnp_vib_overdrive_work(struct work_struct *work)
  172. {
  173. struct vib_ldo_chip *chip = container_of(work, struct vib_ldo_chip,
  174. overdrive_work);
  175. int ret;
  176. mutex_lock(&chip->lock);
  177. /* LDO voltage update not required if Vibration disabled */
  178. if (!chip->vib_enabled)
  179. goto unlock;
  180. ret = qpnp_vib_ldo_set_voltage(chip, chip->vmax_uV);
  181. if (ret < 0) {
  182. pr_err("set vibration voltage = %duV failed, ret=%d\n",
  183. chip->vmax_uV, ret);
  184. qpnp_vib_ldo_enable(chip, false);
  185. goto unlock;
  186. }
  187. pr_debug("voltage set to %d\n", chip->vmax_uV);
  188. unlock:
  189. mutex_unlock(&chip->lock);
  190. }
  191. static enum hrtimer_restart vib_overdrive_timer(struct hrtimer *timer)
  192. {
  193. struct vib_ldo_chip *chip = container_of(timer, struct vib_ldo_chip,
  194. overdrive_timer);
  195. schedule_work(&chip->overdrive_work);
  196. pr_debug("overdrive timer expired\n");
  197. return HRTIMER_NORESTART;
  198. }
  199. static ssize_t qpnp_vib_show_state(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct led_classdev *cdev = dev_get_drvdata(dev);
  203. struct vib_ldo_chip *chip = container_of(cdev, struct vib_ldo_chip,
  204. cdev);
  205. return snprintf(buf, PAGE_SIZE, "%d\n", chip->vib_enabled);
  206. }
  207. static ssize_t qpnp_vib_store_state(struct device *dev,
  208. struct device_attribute *attr, const char *buf, size_t count)
  209. {
  210. /* At present, nothing to do with setting state */
  211. return count;
  212. }
  213. static ssize_t qpnp_vib_show_duration(struct device *dev,
  214. struct device_attribute *attr, char *buf)
  215. {
  216. struct led_classdev *cdev = dev_get_drvdata(dev);
  217. struct vib_ldo_chip *chip = container_of(cdev, struct vib_ldo_chip,
  218. cdev);
  219. ktime_t time_rem;
  220. s64 time_ms = 0;
  221. if (hrtimer_active(&chip->stop_timer)) {
  222. time_rem = hrtimer_get_remaining(&chip->stop_timer);
  223. time_ms = ktime_to_ms(time_rem);
  224. }
  225. return snprintf(buf, PAGE_SIZE, "%lld\n", time_ms);
  226. }
  227. static ssize_t qpnp_vib_store_duration(struct device *dev,
  228. struct device_attribute *attr, const char *buf, size_t count)
  229. {
  230. struct led_classdev *cdev = dev_get_drvdata(dev);
  231. struct vib_ldo_chip *chip = container_of(cdev, struct vib_ldo_chip,
  232. cdev);
  233. u32 val;
  234. int ret;
  235. ret = kstrtouint(buf, 0, &val);
  236. if (ret < 0)
  237. return ret;
  238. /* setting 0 on duration is NOP for now */
  239. if (val <= 0)
  240. return count;
  241. if (val < QPNP_VIB_MIN_PLAY_MS)
  242. val = QPNP_VIB_MIN_PLAY_MS;
  243. if (val > QPNP_VIB_MAX_PLAY_MS)
  244. val = QPNP_VIB_MAX_PLAY_MS;
  245. mutex_lock(&chip->lock);
  246. chip->vib_play_ms = val;
  247. mutex_unlock(&chip->lock);
  248. return count;
  249. }
  250. static ssize_t qpnp_vib_show_activate(struct device *dev,
  251. struct device_attribute *attr, char *buf)
  252. {
  253. /* For now nothing to show */
  254. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  255. }
  256. static ssize_t qpnp_vib_store_activate(struct device *dev,
  257. struct device_attribute *attr, const char *buf, size_t count)
  258. {
  259. struct led_classdev *cdev = dev_get_drvdata(dev);
  260. struct vib_ldo_chip *chip = container_of(cdev, struct vib_ldo_chip,
  261. cdev);
  262. u32 val;
  263. int ret;
  264. ret = kstrtouint(buf, 0, &val);
  265. if (ret < 0)
  266. return ret;
  267. if (val != 0 && val != 1)
  268. return count;
  269. mutex_lock(&chip->lock);
  270. hrtimer_cancel(&chip->stop_timer);
  271. chip->state = val;
  272. pr_debug("state = %d, time = %llums\n", chip->state, chip->vib_play_ms);
  273. mutex_unlock(&chip->lock);
  274. schedule_work(&chip->vib_work);
  275. return count;
  276. }
  277. static ssize_t qpnp_vib_show_vmax(struct device *dev,
  278. struct device_attribute *attr, char *buf)
  279. {
  280. struct led_classdev *cdev = dev_get_drvdata(dev);
  281. struct vib_ldo_chip *chip = container_of(cdev, struct vib_ldo_chip,
  282. cdev);
  283. return snprintf(buf, PAGE_SIZE, "%d\n", chip->vmax_uV / 1000);
  284. }
  285. static ssize_t qpnp_vib_store_vmax(struct device *dev,
  286. struct device_attribute *attr, const char *buf, size_t count)
  287. {
  288. struct led_classdev *cdev = dev_get_drvdata(dev);
  289. struct vib_ldo_chip *chip = container_of(cdev, struct vib_ldo_chip,
  290. cdev);
  291. int data, ret;
  292. ret = kstrtoint(buf, 10, &data);
  293. if (ret < 0)
  294. return ret;
  295. data = data * 1000; /* Convert to microvolts */
  296. /* check against vibrator ldo min/max voltage limits */
  297. data = min(data, QPNP_VIB_LDO_VMAX_UV);
  298. data = max(data, QPNP_VIB_LDO_VMIN_UV);
  299. mutex_lock(&chip->lock);
  300. chip->vmax_uV = data;
  301. mutex_unlock(&chip->lock);
  302. return ret;
  303. }
  304. static struct device_attribute qpnp_vib_attrs[] = {
  305. __ATTR(state, 0664, qpnp_vib_show_state, qpnp_vib_store_state),
  306. __ATTR(duration, 0664, qpnp_vib_show_duration, qpnp_vib_store_duration),
  307. __ATTR(activate, 0664, qpnp_vib_show_activate, qpnp_vib_store_activate),
  308. __ATTR(vmax_mv, 0664, qpnp_vib_show_vmax, qpnp_vib_store_vmax),
  309. };
  310. static int qpnp_vib_parse_dt(struct device *dev, struct vib_ldo_chip *chip)
  311. {
  312. int ret;
  313. ret = of_property_read_u32(dev->of_node, "qcom,vib-ldo-volt-uv",
  314. &chip->vmax_uV);
  315. if (ret < 0) {
  316. pr_err("qcom,vib-ldo-volt-uv property read failed, ret=%d\n",
  317. ret);
  318. return ret;
  319. }
  320. chip->disable_overdrive = of_property_read_bool(dev->of_node,
  321. "qcom,disable-overdrive");
  322. if (of_find_property(dev->of_node, "qcom,vib-overdrive-volt-uv",
  323. NULL)) {
  324. ret = of_property_read_u32(dev->of_node,
  325. "qcom,vib-overdrive-volt-uv",
  326. &chip->overdrive_volt_uV);
  327. if (ret < 0) {
  328. pr_err("qcom,vib-overdrive-volt-uv property read failed, ret=%d\n",
  329. ret);
  330. return ret;
  331. }
  332. /* check against vibrator ldo min/max voltage limits */
  333. chip->overdrive_volt_uV = min(chip->overdrive_volt_uV,
  334. QPNP_VIB_LDO_VMAX_UV);
  335. chip->overdrive_volt_uV = max(chip->overdrive_volt_uV,
  336. QPNP_VIB_LDO_VMIN_UV);
  337. }
  338. return ret;
  339. }
  340. /* Dummy functions for brightness */
  341. static enum led_brightness qpnp_vib_brightness_get(struct led_classdev *cdev)
  342. {
  343. return 0;
  344. }
  345. static void qpnp_vib_brightness_set(struct led_classdev *cdev,
  346. enum led_brightness level)
  347. {
  348. }
  349. static int qpnp_vibrator_ldo_suspend(struct device *dev)
  350. {
  351. struct vib_ldo_chip *chip = dev_get_drvdata(dev);
  352. mutex_lock(&chip->lock);
  353. if (!chip->disable_overdrive) {
  354. hrtimer_cancel(&chip->overdrive_timer);
  355. cancel_work_sync(&chip->overdrive_work);
  356. }
  357. hrtimer_cancel(&chip->stop_timer);
  358. cancel_work_sync(&chip->vib_work);
  359. mutex_unlock(&chip->lock);
  360. return 0;
  361. }
  362. static SIMPLE_DEV_PM_OPS(qpnp_vibrator_ldo_pm_ops, qpnp_vibrator_ldo_suspend,
  363. NULL);
  364. static int qpnp_vibrator_ldo_probe(struct platform_device *pdev)
  365. {
  366. struct device_node *of_node = pdev->dev.of_node;
  367. struct vib_ldo_chip *chip;
  368. int i, ret;
  369. u32 base;
  370. ret = of_property_read_u32(of_node, "reg", &base);
  371. if (ret < 0) {
  372. pr_err("reg property reading failed, ret=%d\n", ret);
  373. return ret;
  374. }
  375. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  376. if (!chip)
  377. return -ENOMEM;
  378. chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  379. if (!chip->regmap) {
  380. pr_err("couldn't get parent's regmap\n");
  381. return -EINVAL;
  382. }
  383. ret = qpnp_vib_parse_dt(&pdev->dev, chip);
  384. if (ret < 0) {
  385. pr_err("couldn't parse device tree, ret=%d\n", ret);
  386. return ret;
  387. }
  388. chip->base = (uint16_t)base;
  389. chip->vib_play_ms = QPNP_VIB_PLAY_MS;
  390. mutex_init(&chip->lock);
  391. INIT_WORK(&chip->vib_work, qpnp_vib_work);
  392. INIT_WORK(&chip->overdrive_work, qpnp_vib_overdrive_work);
  393. hrtimer_init(&chip->stop_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  394. chip->stop_timer.function = vib_stop_timer;
  395. hrtimer_init(&chip->overdrive_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  396. chip->overdrive_timer.function = vib_overdrive_timer;
  397. dev_set_drvdata(&pdev->dev, chip);
  398. chip->cdev.name = "vibrator";
  399. chip->cdev.brightness_get = qpnp_vib_brightness_get;
  400. chip->cdev.brightness_set = qpnp_vib_brightness_set;
  401. chip->cdev.max_brightness = 100;
  402. ret = devm_led_classdev_register(&pdev->dev, &chip->cdev);
  403. if (ret < 0) {
  404. pr_err("Error in registering led class device, ret=%d\n", ret);
  405. goto fail;
  406. }
  407. for (i = 0; i < ARRAY_SIZE(qpnp_vib_attrs); i++) {
  408. ret = sysfs_create_file(&chip->cdev.dev->kobj,
  409. &qpnp_vib_attrs[i].attr);
  410. if (ret < 0) {
  411. dev_err(&pdev->dev, "Error in creating sysfs file, ret=%d\n",
  412. ret);
  413. goto sysfs_fail;
  414. }
  415. }
  416. pr_info("Vibrator LDO successfully registered: uV = %d, overdrive = %s\n",
  417. chip->vmax_uV,
  418. chip->disable_overdrive ? "disabled" : "enabled");
  419. return 0;
  420. sysfs_fail:
  421. for (--i; i >= 0; i--)
  422. sysfs_remove_file(&chip->cdev.dev->kobj,
  423. &qpnp_vib_attrs[i].attr);
  424. fail:
  425. mutex_destroy(&chip->lock);
  426. dev_set_drvdata(&pdev->dev, NULL);
  427. return ret;
  428. }
  429. static int qpnp_vibrator_ldo_remove(struct platform_device *pdev)
  430. {
  431. struct vib_ldo_chip *chip = dev_get_drvdata(&pdev->dev);
  432. if (!chip->disable_overdrive) {
  433. hrtimer_cancel(&chip->overdrive_timer);
  434. cancel_work_sync(&chip->overdrive_work);
  435. }
  436. hrtimer_cancel(&chip->stop_timer);
  437. cancel_work_sync(&chip->vib_work);
  438. mutex_destroy(&chip->lock);
  439. dev_set_drvdata(&pdev->dev, NULL);
  440. return 0;
  441. }
  442. static const struct of_device_id vibrator_ldo_match_table[] = {
  443. { .compatible = "qcom,qpnp-vibrator-ldo" },
  444. { /* sentinel */ },
  445. };
  446. MODULE_DEVICE_TABLE(of, vibrator_ldo_match_table);
  447. static struct platform_driver qpnp_vibrator_ldo_driver = {
  448. .driver = {
  449. .name = "qcom,qpnp-vibrator-ldo",
  450. .of_match_table = vibrator_ldo_match_table,
  451. .pm = &qpnp_vibrator_ldo_pm_ops,
  452. },
  453. .probe = qpnp_vibrator_ldo_probe,
  454. .remove = qpnp_vibrator_ldo_remove,
  455. };
  456. module_platform_driver(qpnp_vibrator_ldo_driver);
  457. MODULE_DESCRIPTION("QCOM QPNP Vibrator-LDO driver");
  458. MODULE_LICENSE("GPL v2");