Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / input / misc / pm8xxx-vibrator.c
blob381b064732791faa49cd90a3395b9f8a10a1f4c2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
3 */
5 #include <linux/errno.h>
6 #include <linux/input.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/platform_device.h>
11 #include <linux/regmap.h>
12 #include <linux/slab.h>
14 #define VIB_MAX_LEVEL_mV(vib) (vib->drv2_addr ? 3544 : 3100)
15 #define VIB_MIN_LEVEL_mV(vib) (vib->drv2_addr ? 1504 : 1200)
16 #define VIB_PER_STEP_mV(vib) (vib->drv2_addr ? 8 : 100)
17 #define VIB_MAX_LEVELS(vib) \
18 (VIB_MAX_LEVEL_mV(vib) - VIB_MIN_LEVEL_mV(vib) + VIB_PER_STEP_mV(vib))
20 #define MAX_FF_SPEED 0xff
22 struct pm8xxx_regs {
23 unsigned int enable_offset;
24 unsigned int enable_mask;
26 unsigned int drv_offset;
27 unsigned int drv_mask;
28 unsigned int drv_shift;
29 unsigned int drv2_offset;
30 unsigned int drv2_mask;
31 unsigned int drv2_shift;
32 unsigned int drv_en_manual_mask;
33 bool drv_in_step;
36 static const struct pm8xxx_regs pm8058_regs = {
37 .drv_offset = 0,
38 .drv_mask = GENMASK(7, 3),
39 .drv_shift = 3,
40 .drv_en_manual_mask = 0xfc,
41 .drv_in_step = true,
44 static struct pm8xxx_regs pm8916_regs = {
45 .enable_offset = 0x46,
46 .enable_mask = BIT(7),
47 .drv_offset = 0x41,
48 .drv_mask = GENMASK(4, 0),
49 .drv_shift = 0,
50 .drv_en_manual_mask = 0,
51 .drv_in_step = true,
54 static struct pm8xxx_regs pmi632_regs = {
55 .enable_offset = 0x46,
56 .enable_mask = BIT(7),
57 .drv_offset = 0x40,
58 .drv_mask = GENMASK(7, 0),
59 .drv_shift = 0,
60 .drv2_offset = 0x41,
61 .drv2_mask = GENMASK(3, 0),
62 .drv2_shift = 8,
63 .drv_en_manual_mask = 0,
64 .drv_in_step = false,
67 /**
68 * struct pm8xxx_vib - structure to hold vibrator data
69 * @vib_input_dev: input device supporting force feedback
70 * @work: work structure to set the vibration parameters
71 * @regmap: regmap for register read/write
72 * @regs: registers' info
73 * @enable_addr: vibrator enable register
74 * @drv_addr: vibrator drive strength register
75 * @drv2_addr: vibrator drive strength upper byte register
76 * @speed: speed of vibration set from userland
77 * @active: state of vibrator
78 * @level: level of vibration to set in the chip
79 * @reg_vib_drv: regs->drv_addr register value
81 struct pm8xxx_vib {
82 struct input_dev *vib_input_dev;
83 struct work_struct work;
84 struct regmap *regmap;
85 const struct pm8xxx_regs *regs;
86 unsigned int enable_addr;
87 unsigned int drv_addr;
88 unsigned int drv2_addr;
89 int speed;
90 int level;
91 bool active;
92 u8 reg_vib_drv;
95 /**
96 * pm8xxx_vib_set - handler to start/stop vibration
97 * @vib: pointer to vibrator structure
98 * @on: state to set
100 static int pm8xxx_vib_set(struct pm8xxx_vib *vib, bool on)
102 int rc;
103 unsigned int val = vib->reg_vib_drv;
104 const struct pm8xxx_regs *regs = vib->regs;
106 if (regs->drv_in_step)
107 vib->level /= VIB_PER_STEP_mV(vib);
109 if (on)
110 val |= (vib->level << regs->drv_shift) & regs->drv_mask;
111 else
112 val &= ~regs->drv_mask;
114 rc = regmap_write(vib->regmap, vib->drv_addr, val);
115 if (rc < 0)
116 return rc;
118 vib->reg_vib_drv = val;
120 if (regs->drv2_mask) {
121 val = vib->level << regs->drv2_shift;
122 rc = regmap_write_bits(vib->regmap, vib->drv2_addr,
123 regs->drv2_mask, on ? val : 0);
124 if (rc < 0)
125 return rc;
128 if (regs->enable_mask)
129 rc = regmap_update_bits(vib->regmap, vib->enable_addr,
130 regs->enable_mask, on ? regs->enable_mask : 0);
132 return rc;
136 * pm8xxx_work_handler - worker to set vibration level
137 * @work: pointer to work_struct
139 static void pm8xxx_work_handler(struct work_struct *work)
141 struct pm8xxx_vib *vib = container_of(work, struct pm8xxx_vib, work);
142 unsigned int val;
143 int rc;
145 rc = regmap_read(vib->regmap, vib->drv_addr, &val);
146 if (rc < 0)
147 return;
150 * pmic vibrator supports voltage ranges from MIN_LEVEL to MAX_LEVEL, so
151 * scale the level to fit into these ranges.
153 if (vib->speed) {
154 vib->active = true;
155 vib->level = VIB_MIN_LEVEL_mV(vib);
156 vib->level += mult_frac(VIB_MAX_LEVELS(vib), vib->speed, MAX_FF_SPEED);
157 } else {
158 vib->active = false;
159 vib->level = VIB_MIN_LEVEL_mV(vib);
162 pm8xxx_vib_set(vib, vib->active);
166 * pm8xxx_vib_close - callback of input close callback
167 * @dev: input device pointer
169 * Turns off the vibrator.
171 static void pm8xxx_vib_close(struct input_dev *dev)
173 struct pm8xxx_vib *vib = input_get_drvdata(dev);
175 cancel_work_sync(&vib->work);
176 if (vib->active)
177 pm8xxx_vib_set(vib, false);
181 * pm8xxx_vib_play_effect - function to handle vib effects.
182 * @dev: input device pointer
183 * @data: data of effect
184 * @effect: effect to play
186 * Currently this driver supports only rumble effects.
188 static int pm8xxx_vib_play_effect(struct input_dev *dev, void *data,
189 struct ff_effect *effect)
191 struct pm8xxx_vib *vib = input_get_drvdata(dev);
193 vib->speed = effect->u.rumble.strong_magnitude >> 8;
194 if (!vib->speed)
195 vib->speed = effect->u.rumble.weak_magnitude >> 9;
197 schedule_work(&vib->work);
199 return 0;
202 static int pm8xxx_vib_probe(struct platform_device *pdev)
204 struct pm8xxx_vib *vib;
205 struct input_dev *input_dev;
206 int error;
207 unsigned int val, reg_base = 0;
208 const struct pm8xxx_regs *regs;
210 vib = devm_kzalloc(&pdev->dev, sizeof(*vib), GFP_KERNEL);
211 if (!vib)
212 return -ENOMEM;
214 vib->regmap = dev_get_regmap(pdev->dev.parent, NULL);
215 if (!vib->regmap)
216 return -ENODEV;
218 input_dev = devm_input_allocate_device(&pdev->dev);
219 if (!input_dev)
220 return -ENOMEM;
222 INIT_WORK(&vib->work, pm8xxx_work_handler);
223 vib->vib_input_dev = input_dev;
225 error = fwnode_property_read_u32(pdev->dev.fwnode, "reg", &reg_base);
226 if (error < 0)
227 return dev_err_probe(&pdev->dev, error, "Failed to read reg address\n");
229 regs = of_device_get_match_data(&pdev->dev);
230 vib->enable_addr = reg_base + regs->enable_offset;
231 vib->drv_addr = reg_base + regs->drv_offset;
232 vib->drv2_addr = reg_base + regs->drv2_offset;
234 /* operate in manual mode */
235 error = regmap_read(vib->regmap, vib->drv_addr, &val);
236 if (error < 0)
237 return error;
239 val &= regs->drv_en_manual_mask;
240 error = regmap_write(vib->regmap, vib->drv_addr, val);
241 if (error < 0)
242 return error;
244 vib->regs = regs;
245 vib->reg_vib_drv = val;
247 input_dev->name = "pm8xxx_vib_ffmemless";
248 input_dev->id.version = 1;
249 input_dev->close = pm8xxx_vib_close;
250 input_set_drvdata(input_dev, vib);
251 input_set_capability(vib->vib_input_dev, EV_FF, FF_RUMBLE);
253 error = input_ff_create_memless(input_dev, NULL,
254 pm8xxx_vib_play_effect);
255 if (error) {
256 dev_err(&pdev->dev,
257 "couldn't register vibrator as FF device\n");
258 return error;
261 error = input_register_device(input_dev);
262 if (error) {
263 dev_err(&pdev->dev, "couldn't register input device\n");
264 return error;
267 platform_set_drvdata(pdev, vib);
268 return 0;
271 static int pm8xxx_vib_suspend(struct device *dev)
273 struct pm8xxx_vib *vib = dev_get_drvdata(dev);
275 /* Turn off the vibrator */
276 pm8xxx_vib_set(vib, false);
278 return 0;
281 static DEFINE_SIMPLE_DEV_PM_OPS(pm8xxx_vib_pm_ops, pm8xxx_vib_suspend, NULL);
283 static const struct of_device_id pm8xxx_vib_id_table[] = {
284 { .compatible = "qcom,pm8058-vib", .data = &pm8058_regs },
285 { .compatible = "qcom,pm8921-vib", .data = &pm8058_regs },
286 { .compatible = "qcom,pm8916-vib", .data = &pm8916_regs },
287 { .compatible = "qcom,pmi632-vib", .data = &pmi632_regs },
290 MODULE_DEVICE_TABLE(of, pm8xxx_vib_id_table);
292 static struct platform_driver pm8xxx_vib_driver = {
293 .probe = pm8xxx_vib_probe,
294 .driver = {
295 .name = "pm8xxx-vib",
296 .pm = pm_sleep_ptr(&pm8xxx_vib_pm_ops),
297 .of_match_table = pm8xxx_vib_id_table,
300 module_platform_driver(pm8xxx_vib_driver);
302 MODULE_ALIAS("platform:pm8xxx_vib");
303 MODULE_DESCRIPTION("PMIC8xxx vibrator driver based on ff-memless framework");
304 MODULE_LICENSE("GPL v2");
305 MODULE_AUTHOR("Amy Maloche <amaloche@codeaurora.org>");