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