blob: c428bfb386b95f1f7d0ac0b34eed0925d3cd72ed [file] [log] [blame]
Kavya Nunnad622c742019-01-30 15:51:13 +05301/* Copyright (c) 2013-2015, 2018-2019, The Linux Foundation. All rights reserved.
Anirudh Ghayal85430b92018-04-16 18:58:35 +05302 *
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#include <linux/module.h>
14#include <linux/regmap.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/slab.h>
19#include <linux/hrtimer.h>
20#include <linux/of_device.h>
21#include <linux/of_irq.h>
22#include <linux/platform_device.h>
23#include <linux/err.h>
24#include <linux/leds.h>
25#include <linux/device.h>
26#include <linux/qpnp/pwm.h>
27
28#define QPNP_VIB_VTG_CTL(base) (base + 0x41)
29#define QPNP_VIB_EN_CTL(base) (base + 0x46)
30
31#define QPNP_VIB_MAX_LEVEL 31
32#define QPNP_VIB_MIN_LEVEL 12
33
34#define QPNP_VIB_DEFAULT_TIMEOUT 15000
35#define QPNP_VIB_DEFAULT_VTG_LVL 3100
36
37#define QPNP_VIB_EN BIT(7)
38#define QPNP_VIB_VTG_SET_MASK 0x1F
39#define QPNP_VIB_LOGIC_SHIFT 4
40
41enum qpnp_vib_mode {
42 QPNP_VIB_MANUAL,
43 QPNP_VIB_DTEST1,
44 QPNP_VIB_DTEST2,
45 QPNP_VIB_DTEST3,
46};
47
48struct qpnp_pwm_info {
49 struct pwm_device *pwm_dev;
50 u32 pwm_channel;
51 u32 duty_us;
52 u32 period_us;
53};
54
55struct qpnp_vib {
56 struct platform_device *pdev;
57 struct regmap *regmap;
58 struct hrtimer vib_timer;
59 struct led_classdev cdev;
60 struct work_struct work;
61 struct qpnp_pwm_info pwm_info;
62 enum qpnp_vib_mode mode;
63
64 u8 reg_vtg_ctl;
65 u8 reg_en_ctl;
66 u8 active_low;
67 u16 base;
68 int state;
69 int vtg_level;
70 int timeout;
71 u32 vib_play_ms;
72 struct mutex lock;
73};
74
75static int qpnp_vib_read_u8(struct qpnp_vib *vib, u8 *data, u16 reg)
76{
77 int rc;
78
79 rc = regmap_read(vib->regmap, reg, (unsigned int *)data);
80 if (rc < 0)
81 dev_err(&vib->pdev->dev,
82 "Error reading address: %X - ret %X\n", reg, rc);
83
84 return rc;
85}
86
87static int qpnp_vib_write_u8(struct qpnp_vib *vib, u8 *data, u16 reg)
88{
89 int rc;
90
91 rc = regmap_write(vib->regmap, reg, (unsigned int)*data);
92 if (rc < 0)
93 dev_err(&vib->pdev->dev,
94 "Error writing address: %X - ret %X\n", reg, rc);
95
96 return rc;
97}
98
99static int qpnp_vibrator_config(struct qpnp_vib *vib)
100{
101 u8 reg = 0;
102 int rc;
103
104 /* Configure the VTG CTL regiser */
105 rc = qpnp_vib_read_u8(vib, &reg, QPNP_VIB_VTG_CTL(vib->base));
106 if (rc < 0)
107 return rc;
108 reg &= ~QPNP_VIB_VTG_SET_MASK;
109 reg |= (vib->vtg_level & QPNP_VIB_VTG_SET_MASK);
110 rc = qpnp_vib_write_u8(vib, &reg, QPNP_VIB_VTG_CTL(vib->base));
111 if (rc)
112 return rc;
113 vib->reg_vtg_ctl = reg;
114
115 /* Configure the VIB ENABLE regiser */
116 rc = qpnp_vib_read_u8(vib, &reg, QPNP_VIB_EN_CTL(vib->base));
117 if (rc < 0)
118 return rc;
119 reg |= (!!vib->active_low) << QPNP_VIB_LOGIC_SHIFT;
120 if (vib->mode != QPNP_VIB_MANUAL) {
121 vib->pwm_info.pwm_dev = pwm_request(vib->pwm_info.pwm_channel,
122 "qpnp-vib");
123 if (IS_ERR_OR_NULL(vib->pwm_info.pwm_dev)) {
124 dev_err(&vib->pdev->dev, "vib pwm request failed\n");
125 return -ENODEV;
126 }
127
128 rc = pwm_config(vib->pwm_info.pwm_dev, vib->pwm_info.duty_us,
129 vib->pwm_info.period_us);
130 if (rc < 0) {
131 dev_err(&vib->pdev->dev, "vib pwm config failed\n");
132 pwm_free(vib->pwm_info.pwm_dev);
133 return -ENODEV;
134 }
135
136 reg |= BIT(vib->mode - 1);
137 }
138
139 rc = qpnp_vib_write_u8(vib, &reg, QPNP_VIB_EN_CTL(vib->base));
140 if (rc < 0)
141 return rc;
142 vib->reg_en_ctl = reg;
143
144 return rc;
145}
146
147static int qpnp_vib_set(struct qpnp_vib *vib, int on)
148{
149 int rc;
150 u8 val;
151
152 if (on) {
153 if (vib->mode != QPNP_VIB_MANUAL) {
154 pwm_enable(vib->pwm_info.pwm_dev);
155 } else {
156 val = vib->reg_en_ctl;
157 val |= QPNP_VIB_EN;
158 rc = qpnp_vib_write_u8(vib, &val,
159 QPNP_VIB_EN_CTL(vib->base));
160 if (rc < 0)
161 return rc;
162 vib->reg_en_ctl = val;
163 }
164 } else {
165 if (vib->mode != QPNP_VIB_MANUAL) {
166 pwm_disable(vib->pwm_info.pwm_dev);
167 } else {
168 val = vib->reg_en_ctl;
169 val &= ~QPNP_VIB_EN;
170 rc = qpnp_vib_write_u8(vib, &val,
171 QPNP_VIB_EN_CTL(vib->base));
172 if (rc < 0)
173 return rc;
174 vib->reg_en_ctl = val;
175 }
176 }
177
178 return 0;
179}
180
181static void qpnp_vib_update(struct work_struct *work)
182{
183 struct qpnp_vib *vib = container_of(work, struct qpnp_vib,
184 work);
185 qpnp_vib_set(vib, vib->state);
186}
187
188static enum hrtimer_restart qpnp_vib_timer_func(struct hrtimer *timer)
189{
190 struct qpnp_vib *vib = container_of(timer, struct qpnp_vib,
191 vib_timer);
192
193 vib->state = 0;
194 schedule_work(&vib->work);
195
196 return HRTIMER_NORESTART;
197}
198
199#ifdef CONFIG_PM
200static int qpnp_vibrator_suspend(struct device *dev)
201{
202 struct qpnp_vib *vib = dev_get_drvdata(dev);
203
204 hrtimer_cancel(&vib->vib_timer);
205 cancel_work_sync(&vib->work);
206 /* turn-off vibrator */
207 qpnp_vib_set(vib, 0);
208
209 return 0;
210}
211#endif
212
213static SIMPLE_DEV_PM_OPS(qpnp_vibrator_pm_ops, qpnp_vibrator_suspend, NULL);
214
215static int qpnp_vib_parse_dt(struct qpnp_vib *vib)
216{
217 struct platform_device *pdev = vib->pdev;
218 struct device_node *node = pdev->dev.of_node;
219 const char *mode;
220 u32 temp_val;
221 int rc;
222
223 vib->timeout = QPNP_VIB_DEFAULT_TIMEOUT;
224
225 rc = of_property_read_u32(node, "reg", &temp_val);
226 if (rc < 0) {
227 dev_err(&pdev->dev,
228 "Couldn't find reg in node = %s rc = %d\n",
229 node->full_name, rc);
230 return rc;
231 }
232 vib->base = temp_val;
233
234 rc = of_property_read_u32(node,
235 "qcom,vib-timeout-ms", &temp_val);
236 if (!rc) {
237 vib->timeout = temp_val;
238 } else if (rc != -EINVAL) {
239 dev_err(&pdev->dev, "Unable to read vib timeout\n");
240 return rc;
241 }
242
243 vib->vtg_level = QPNP_VIB_DEFAULT_VTG_LVL;
244 rc = of_property_read_u32(node,
245 "qcom,vib-vtg-level-mV", &temp_val);
246 if (!rc) {
247 vib->vtg_level = temp_val;
248 } else if (rc != -EINVAL) {
249 dev_err(&pdev->dev, "Unable to read vtg level\n");
250 return rc;
251 }
252
253 vib->vtg_level /= 100;
254 if (vib->vtg_level < QPNP_VIB_MIN_LEVEL)
255 vib->vtg_level = QPNP_VIB_MIN_LEVEL;
256 else if (vib->vtg_level > QPNP_VIB_MAX_LEVEL)
257 vib->vtg_level = QPNP_VIB_MAX_LEVEL;
258
259 vib->mode = QPNP_VIB_MANUAL;
260 rc = of_property_read_string(node, "qcom,mode", &mode);
261 if (!rc) {
262 if (strcmp(mode, "manual") == 0) {
263 vib->mode = QPNP_VIB_MANUAL;
264 } else if (strcmp(mode, "dtest1") == 0) {
265 vib->mode = QPNP_VIB_DTEST1;
266 } else if (strcmp(mode, "dtest2") == 0) {
267 vib->mode = QPNP_VIB_DTEST2;
268 } else if (strcmp(mode, "dtest3") == 0) {
269 vib->mode = QPNP_VIB_DTEST3;
270 } else {
271 dev_err(&pdev->dev, "Invalid mode\n");
272 return -EINVAL;
273 }
274 } else if (rc != -EINVAL) {
275 dev_err(&pdev->dev, "Unable to read mode\n");
276 return rc;
277 }
278
279 if (vib->mode != QPNP_VIB_MANUAL) {
280 rc = of_property_read_u32(node,
281 "qcom,pwm-channel", &temp_val);
282 if (!rc)
283 vib->pwm_info.pwm_channel = temp_val;
284 else
285 return rc;
286
287 rc = of_property_read_u32(node,
288 "qcom,period-us", &temp_val);
289 if (!rc)
290 vib->pwm_info.period_us = temp_val;
291 else
292 return rc;
293
294 rc = of_property_read_u32(node,
295 "qcom,duty-us", &temp_val);
296 if (!rc)
297 vib->pwm_info.duty_us = temp_val;
298 else
299 return rc;
300 }
301
302 vib->active_low = of_property_read_bool(node,
303 "qcom,active-low");
304
305 return 0;
306}
307
308static ssize_t qpnp_vib_get_state(struct device *dev,
309 struct device_attribute *attr, char *buf)
310{
311 struct led_classdev *cdev = dev_get_drvdata(dev);
312 struct qpnp_vib *chip = container_of(cdev, struct qpnp_vib, cdev);
313
314 return snprintf(buf, PAGE_SIZE, "%d\n", !!chip->reg_en_ctl);
315}
316
Kavya Nunnad622c742019-01-30 15:51:13 +0530317static ssize_t qpnp_vib_set_state(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t count)
319{
320 /* At present, nothing to do with setting state */
321 return count;
322}
323
Anirudh Ghayal85430b92018-04-16 18:58:35 +0530324static ssize_t qpnp_vib_get_duration(struct device *dev,
325 struct device_attribute *attr, char *buf)
326{
327 struct led_classdev *cdev = dev_get_drvdata(dev);
328 struct qpnp_vib *vib = container_of(cdev, struct qpnp_vib, cdev);
329 ktime_t time_rem;
330 s64 time_us = 0;
331
332 if (hrtimer_active(&vib->vib_timer)) {
333 time_rem = hrtimer_get_remaining(&vib->vib_timer);
334 time_us = ktime_to_us(time_rem);
335 }
336
337 return snprintf(buf, PAGE_SIZE, "%lld\n", div_s64(time_us, 1000));
338}
339
340
341static ssize_t qpnp_vib_set_duration(struct device *dev,
342 struct device_attribute *attr, const char *buf, size_t count)
343{
344 struct led_classdev *cdev = dev_get_drvdata(dev);
345 struct qpnp_vib *vib = container_of(cdev, struct qpnp_vib, cdev);
346 u32 value;
347 int rc;
348
349 rc = kstrtouint(buf, 0, &value);
350 if (rc < 0)
351 return rc;
352
353 /* setting 0 on duration is NOP for now */
354 if (value <= 0)
355 return count;
356
357 if (value > vib->timeout)
358 value = vib->timeout;
359
360 mutex_lock(&vib->lock);
361 vib->vib_play_ms = value;
362 mutex_unlock(&vib->lock);
363 return count;
364}
365
366static ssize_t qpnp_vib_get_activate(struct device *dev,
367 struct device_attribute *attr, char *buf)
368{
369 struct led_classdev *cdev = dev_get_drvdata(dev);
370 struct qpnp_vib *vib = container_of(cdev, struct qpnp_vib, cdev);
371
372 return snprintf(buf, PAGE_SIZE, "%d\n", vib->state);
373}
374
375static ssize_t qpnp_vib_set_activate(struct device *dev,
376 struct device_attribute *attr, const char *buf, size_t count)
377{
378 struct led_classdev *cdev = dev_get_drvdata(dev);
379 struct qpnp_vib *vib = container_of(cdev, struct qpnp_vib, cdev);
380 u32 value;
381 int rc;
382
383 rc = kstrtouint(buf, 0, &value);
384 if (rc < 0)
385 return rc;
386
387 if (value != 0 && value != 1)
388 return count;
389
390 vib->state = value;
391 pr_debug("state = %d, time = %ums\n", vib->state,
392 vib->vib_play_ms);
393 mutex_lock(&vib->lock);
394 if (vib->state) {
395 hrtimer_cancel(&vib->vib_timer);
396 hrtimer_start(&vib->vib_timer,
397 ktime_set(vib->vib_play_ms / 1000,
398 (vib->vib_play_ms % 1000) * 1000000),
399 HRTIMER_MODE_REL);
400 }
Anirudh Ghayal85430b92018-04-16 18:58:35 +0530401 mutex_unlock(&vib->lock);
402 schedule_work(&vib->work);
403
404 return count;
405}
406
407static struct device_attribute qpnp_vib_attrs[] = {
Kavya Nunnad622c742019-01-30 15:51:13 +0530408 __ATTR(state, 0664, qpnp_vib_get_state, qpnp_vib_set_state),
Anirudh Ghayal85430b92018-04-16 18:58:35 +0530409 __ATTR(duration, 0664, qpnp_vib_get_duration, qpnp_vib_set_duration),
410 __ATTR(activate, 0664, qpnp_vib_get_activate, qpnp_vib_set_activate),
411};
412
413/* Dummy functions for brightness */
414static
415enum led_brightness qpnp_brightness_get(struct led_classdev *cdev)
416{
417 return 0;
418}
419
420static void qpnp_brightness_set(struct led_classdev *cdev,
421 enum led_brightness level)
422{
423
424}
425
426static int qpnp_vibrator_probe(struct platform_device *pdev)
427{
428 struct qpnp_vib *vib;
429 int rc;
430 int i;
431
432 vib = devm_kzalloc(&pdev->dev, sizeof(*vib), GFP_KERNEL);
433 if (!vib)
434 return -ENOMEM;
435
436 vib->regmap = dev_get_regmap(pdev->dev.parent, NULL);
437 if (!vib->regmap) {
438 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
439 return -EINVAL;
440 }
441
442 vib->pdev = pdev;
443
444 rc = qpnp_vib_parse_dt(vib);
445 if (rc) {
446 dev_err(&pdev->dev, "DT parsing failed\n");
447 return rc;
448 }
449
450 rc = qpnp_vibrator_config(vib);
451 if (rc) {
452 dev_err(&pdev->dev, "vib config failed\n");
453 return rc;
454 }
455
456 mutex_init(&vib->lock);
457 INIT_WORK(&vib->work, qpnp_vib_update);
458
459 hrtimer_init(&vib->vib_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
460 vib->vib_timer.function = qpnp_vib_timer_func;
461
462 vib->cdev.name = "vibrator";
463 vib->cdev.brightness_get = qpnp_brightness_get;
464 vib->cdev.brightness_set = qpnp_brightness_set;
465 vib->cdev.max_brightness = 100;
466 rc = devm_led_classdev_register(&pdev->dev, &vib->cdev);
467 if (rc < 0) {
468 dev_err(&pdev->dev, "Error in registering led class device, rc=%d\n",
469 rc);
470 goto led_reg_fail;
471 }
472
473 /* Enabling sysfs entries */
474 for (i = 0; i < ARRAY_SIZE(qpnp_vib_attrs); i++) {
475 rc = sysfs_create_file(&vib->cdev.dev->kobj,
476 &qpnp_vib_attrs[i].attr);
477 if (rc < 0) {
478 dev_err(&pdev->dev, "Error in creating sysfs file, rc=%d\n",
479 rc);
480 goto sysfs_fail;
481 }
482 }
483
484 dev_set_drvdata(&pdev->dev, vib);
485 return rc;
486
487sysfs_fail:
488 dev_set_drvdata(&pdev->dev, NULL);
489led_reg_fail:
490 hrtimer_cancel(&vib->vib_timer);
491 cancel_work_sync(&vib->work);
492 mutex_destroy(&vib->lock);
493 return -EINVAL;
494}
495
496static int qpnp_vibrator_remove(struct platform_device *pdev)
497{
498 struct qpnp_vib *vib = dev_get_drvdata(&pdev->dev);
499 int i;
500
501/* Removing sysfs entries */
502 for (i = 0; i < ARRAY_SIZE(qpnp_vib_attrs); i++)
503 sysfs_remove_file(&vib->cdev.dev->kobj,
504 &qpnp_vib_attrs[i].attr);
505
506 dev_set_drvdata(&pdev->dev, NULL);
507 hrtimer_cancel(&vib->vib_timer);
508 cancel_work_sync(&vib->work);
509 mutex_destroy(&vib->lock);
510 return 0;
511}
512
513static const struct of_device_id spmi_match_table[] = {
514 { .compatible = "qcom,qpnp-vibrator",
515 },
516 {}
517};
518
519static struct platform_driver qpnp_vibrator_driver = {
520 .driver = {
521 .name = "qcom,qpnp-vibrator",
522 .of_match_table = spmi_match_table,
523 .pm = &qpnp_vibrator_pm_ops,
524 },
525 .probe = qpnp_vibrator_probe,
526 .remove = qpnp_vibrator_remove,
527};
528
529module_platform_driver(qpnp_vibrator_driver);
530
531MODULE_DESCRIPTION("qpnp vibrator driver");
532MODULE_LICENSE("GPL v2");