1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
5 #include <linux/errno.h>
6 #include <linux/input.h>
7 #include <linux/kernel.h>
8 #include <linux/module.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
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
;
36 static const struct pm8xxx_regs pm8058_regs
= {
38 .drv_mask
= GENMASK(7, 3),
40 .drv_en_manual_mask
= 0xfc,
44 static struct pm8xxx_regs pm8916_regs
= {
45 .enable_offset
= 0x46,
46 .enable_mask
= BIT(7),
48 .drv_mask
= GENMASK(4, 0),
50 .drv_en_manual_mask
= 0,
54 static struct pm8xxx_regs pmi632_regs
= {
55 .enable_offset
= 0x46,
56 .enable_mask
= BIT(7),
58 .drv_mask
= GENMASK(7, 0),
61 .drv2_mask
= GENMASK(3, 0),
63 .drv_en_manual_mask
= 0,
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
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
;
96 * pm8xxx_vib_set - handler to start/stop vibration
97 * @vib: pointer to vibrator structure
100 static int pm8xxx_vib_set(struct pm8xxx_vib
*vib
, bool on
)
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
);
110 val
|= (vib
->level
<< regs
->drv_shift
) & regs
->drv_mask
;
112 val
&= ~regs
->drv_mask
;
114 rc
= regmap_write(vib
->regmap
, vib
->drv_addr
, val
);
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);
128 if (regs
->enable_mask
)
129 rc
= regmap_update_bits(vib
->regmap
, vib
->enable_addr
,
130 regs
->enable_mask
, on
? regs
->enable_mask
: 0);
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
);
145 rc
= regmap_read(vib
->regmap
, vib
->drv_addr
, &val
);
150 * pmic vibrator supports voltage ranges from MIN_LEVEL to MAX_LEVEL, so
151 * scale the level to fit into these ranges.
155 vib
->level
= VIB_MIN_LEVEL_mV(vib
);
156 vib
->level
+= mult_frac(VIB_MAX_LEVELS(vib
), vib
->speed
, MAX_FF_SPEED
);
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
);
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;
195 vib
->speed
= effect
->u
.rumble
.weak_magnitude
>> 9;
197 schedule_work(&vib
->work
);
202 static int pm8xxx_vib_probe(struct platform_device
*pdev
)
204 struct pm8xxx_vib
*vib
;
205 struct input_dev
*input_dev
;
207 unsigned int val
, reg_base
= 0;
208 const struct pm8xxx_regs
*regs
;
210 vib
= devm_kzalloc(&pdev
->dev
, sizeof(*vib
), GFP_KERNEL
);
214 vib
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
218 input_dev
= devm_input_allocate_device(&pdev
->dev
);
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", ®_base
);
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
);
239 val
&= regs
->drv_en_manual_mask
;
240 error
= regmap_write(vib
->regmap
, vib
->drv_addr
, val
);
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
);
257 "couldn't register vibrator as FF device\n");
261 error
= input_register_device(input_dev
);
263 dev_err(&pdev
->dev
, "couldn't register input device\n");
267 platform_set_drvdata(pdev
, vib
);
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);
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
,
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>");