xref: /linux/drivers/input/misc/pm8xxx-vibrator.c (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
3  */
4 
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>
13 
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))
19 
20 #define MAX_FF_SPEED		0xff
21 
22 struct pm8xxx_regs {
23 	unsigned int enable_offset;
24 	unsigned int enable_mask;
25 
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;
34 };
35 
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,
42 };
43 
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,
52 };
53 
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,
65 };
66 
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
80  */
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;
93 };
94 
95 /**
96  * pm8xxx_vib_set - handler to start/stop vibration
97  * @vib: pointer to vibrator structure
98  * @on: state to set
99  */
100 static int pm8xxx_vib_set(struct pm8xxx_vib *vib, bool on)
101 {
102 	int rc;
103 	unsigned int val = vib->reg_vib_drv;
104 	const struct pm8xxx_regs *regs = vib->regs;
105 
106 	if (regs->drv_in_step)
107 		vib->level /= VIB_PER_STEP_mV(vib);
108 
109 	if (on)
110 		val |= (vib->level << regs->drv_shift) & regs->drv_mask;
111 	else
112 		val &= ~regs->drv_mask;
113 
114 	rc = regmap_write(vib->regmap, vib->drv_addr, val);
115 	if (rc < 0)
116 		return rc;
117 
118 	vib->reg_vib_drv = val;
119 
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;
126 	}
127 
128 	if (regs->enable_mask)
129 		rc = regmap_update_bits(vib->regmap, vib->enable_addr,
130 					regs->enable_mask, on ? regs->enable_mask : 0);
131 
132 	return rc;
133 }
134 
135 /**
136  * pm8xxx_work_handler - worker to set vibration level
137  * @work: pointer to work_struct
138  */
139 static void pm8xxx_work_handler(struct work_struct *work)
140 {
141 	struct pm8xxx_vib *vib = container_of(work, struct pm8xxx_vib, work);
142 	unsigned int val;
143 	int rc;
144 
145 	rc = regmap_read(vib->regmap, vib->drv_addr, &val);
146 	if (rc < 0)
147 		return;
148 
149 	/*
150 	 * pmic vibrator supports voltage ranges from MIN_LEVEL to MAX_LEVEL, so
151 	 * scale the level to fit into these ranges.
152 	 */
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);
160 	}
161 
162 	pm8xxx_vib_set(vib, vib->active);
163 }
164 
165 /**
166  * pm8xxx_vib_close - callback of input close callback
167  * @dev: input device pointer
168  *
169  * Turns off the vibrator.
170  */
171 static void pm8xxx_vib_close(struct input_dev *dev)
172 {
173 	struct pm8xxx_vib *vib = input_get_drvdata(dev);
174 
175 	cancel_work_sync(&vib->work);
176 	if (vib->active)
177 		pm8xxx_vib_set(vib, false);
178 }
179 
180 /**
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
185  *
186  * Currently this driver supports only rumble effects.
187  */
188 static int pm8xxx_vib_play_effect(struct input_dev *dev, void *data,
189 				  struct ff_effect *effect)
190 {
191 	struct pm8xxx_vib *vib = input_get_drvdata(dev);
192 
193 	vib->speed = effect->u.rumble.strong_magnitude >> 8;
194 	if (!vib->speed)
195 		vib->speed = effect->u.rumble.weak_magnitude >> 9;
196 
197 	schedule_work(&vib->work);
198 
199 	return 0;
200 }
201 
202 static int pm8xxx_vib_probe(struct platform_device *pdev)
203 {
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;
209 
210 	vib = devm_kzalloc(&pdev->dev, sizeof(*vib), GFP_KERNEL);
211 	if (!vib)
212 		return -ENOMEM;
213 
214 	vib->regmap = dev_get_regmap(pdev->dev.parent, NULL);
215 	if (!vib->regmap)
216 		return -ENODEV;
217 
218 	input_dev = devm_input_allocate_device(&pdev->dev);
219 	if (!input_dev)
220 		return -ENOMEM;
221 
222 	INIT_WORK(&vib->work, pm8xxx_work_handler);
223 	vib->vib_input_dev = input_dev;
224 
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");
228 
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;
233 
234 	/* operate in manual mode */
235 	error = regmap_read(vib->regmap, vib->drv_addr, &val);
236 	if (error < 0)
237 		return error;
238 
239 	val &= regs->drv_en_manual_mask;
240 	error = regmap_write(vib->regmap, vib->drv_addr, val);
241 	if (error < 0)
242 		return error;
243 
244 	vib->regs = regs;
245 	vib->reg_vib_drv = val;
246 
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);
252 
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;
259 	}
260 
261 	error = input_register_device(input_dev);
262 	if (error) {
263 		dev_err(&pdev->dev, "couldn't register input device\n");
264 		return error;
265 	}
266 
267 	platform_set_drvdata(pdev, vib);
268 	return 0;
269 }
270 
271 static int pm8xxx_vib_suspend(struct device *dev)
272 {
273 	struct pm8xxx_vib *vib = dev_get_drvdata(dev);
274 
275 	/* Turn off the vibrator */
276 	pm8xxx_vib_set(vib, false);
277 
278 	return 0;
279 }
280 
281 static DEFINE_SIMPLE_DEV_PM_OPS(pm8xxx_vib_pm_ops, pm8xxx_vib_suspend, NULL);
282 
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 },
288 	{ }
289 };
290 MODULE_DEVICE_TABLE(of, pm8xxx_vib_id_table);
291 
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,
298 	},
299 };
300 module_platform_driver(pm8xxx_vib_driver);
301 
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>");
306