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/of_device.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
13 #include <linux/slab.h>
15 #define VIB_MAX_LEVEL_mV (3100)
16 #define VIB_MIN_LEVEL_mV (1200)
17 #define VIB_MAX_LEVELS (VIB_MAX_LEVEL_mV - VIB_MIN_LEVEL_mV)
19 #define MAX_FF_SPEED 0xff
22 unsigned int enable_addr
;
23 unsigned int enable_mask
;
25 unsigned int drv_addr
;
26 unsigned int drv_mask
;
27 unsigned int drv_shift
;
28 unsigned int drv_en_manual_mask
;
31 static const struct pm8xxx_regs pm8058_regs
= {
35 .drv_en_manual_mask
= 0xfc,
38 static struct pm8xxx_regs pm8916_regs
= {
39 .enable_addr
= 0xc046,
40 .enable_mask
= BIT(7),
44 .drv_en_manual_mask
= 0,
48 * struct pm8xxx_vib - structure to hold vibrator data
49 * @vib_input_dev: input device supporting force feedback
50 * @work: work structure to set the vibration parameters
51 * @regmap: regmap for register read/write
52 * @regs: registers' info
53 * @speed: speed of vibration set from userland
54 * @active: state of vibrator
55 * @level: level of vibration to set in the chip
56 * @reg_vib_drv: regs->drv_addr register value
59 struct input_dev
*vib_input_dev
;
60 struct work_struct work
;
61 struct regmap
*regmap
;
62 const struct pm8xxx_regs
*regs
;
70 * pm8xxx_vib_set - handler to start/stop vibration
71 * @vib: pointer to vibrator structure
74 static int pm8xxx_vib_set(struct pm8xxx_vib
*vib
, bool on
)
77 unsigned int val
= vib
->reg_vib_drv
;
78 const struct pm8xxx_regs
*regs
= vib
->regs
;
81 val
|= (vib
->level
<< regs
->drv_shift
) & regs
->drv_mask
;
83 val
&= ~regs
->drv_mask
;
85 rc
= regmap_write(vib
->regmap
, regs
->drv_addr
, val
);
89 vib
->reg_vib_drv
= val
;
91 if (regs
->enable_mask
)
92 rc
= regmap_update_bits(vib
->regmap
, regs
->enable_addr
,
93 regs
->enable_mask
, on
? ~0 : 0);
99 * pm8xxx_work_handler - worker to set vibration level
100 * @work: pointer to work_struct
102 static void pm8xxx_work_handler(struct work_struct
*work
)
104 struct pm8xxx_vib
*vib
= container_of(work
, struct pm8xxx_vib
, work
);
105 const struct pm8xxx_regs
*regs
= vib
->regs
;
109 rc
= regmap_read(vib
->regmap
, regs
->drv_addr
, &val
);
114 * pmic vibrator supports voltage ranges from 1.2 to 3.1V, so
115 * scale the level to fit into these ranges.
119 vib
->level
= ((VIB_MAX_LEVELS
* vib
->speed
) / MAX_FF_SPEED
) +
124 vib
->level
= VIB_MIN_LEVEL_mV
/ 100;
127 pm8xxx_vib_set(vib
, vib
->active
);
131 * pm8xxx_vib_close - callback of input close callback
132 * @dev: input device pointer
134 * Turns off the vibrator.
136 static void pm8xxx_vib_close(struct input_dev
*dev
)
138 struct pm8xxx_vib
*vib
= input_get_drvdata(dev
);
140 cancel_work_sync(&vib
->work
);
142 pm8xxx_vib_set(vib
, false);
146 * pm8xxx_vib_play_effect - function to handle vib effects.
147 * @dev: input device pointer
148 * @data: data of effect
149 * @effect: effect to play
151 * Currently this driver supports only rumble effects.
153 static int pm8xxx_vib_play_effect(struct input_dev
*dev
, void *data
,
154 struct ff_effect
*effect
)
156 struct pm8xxx_vib
*vib
= input_get_drvdata(dev
);
158 vib
->speed
= effect
->u
.rumble
.strong_magnitude
>> 8;
160 vib
->speed
= effect
->u
.rumble
.weak_magnitude
>> 9;
162 schedule_work(&vib
->work
);
167 static int pm8xxx_vib_probe(struct platform_device
*pdev
)
169 struct pm8xxx_vib
*vib
;
170 struct input_dev
*input_dev
;
173 const struct pm8xxx_regs
*regs
;
175 vib
= devm_kzalloc(&pdev
->dev
, sizeof(*vib
), GFP_KERNEL
);
179 vib
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
183 input_dev
= devm_input_allocate_device(&pdev
->dev
);
187 INIT_WORK(&vib
->work
, pm8xxx_work_handler
);
188 vib
->vib_input_dev
= input_dev
;
190 regs
= of_device_get_match_data(&pdev
->dev
);
192 /* operate in manual mode */
193 error
= regmap_read(vib
->regmap
, regs
->drv_addr
, &val
);
197 val
&= regs
->drv_en_manual_mask
;
198 error
= regmap_write(vib
->regmap
, regs
->drv_addr
, val
);
203 vib
->reg_vib_drv
= val
;
205 input_dev
->name
= "pm8xxx_vib_ffmemless";
206 input_dev
->id
.version
= 1;
207 input_dev
->close
= pm8xxx_vib_close
;
208 input_set_drvdata(input_dev
, vib
);
209 input_set_capability(vib
->vib_input_dev
, EV_FF
, FF_RUMBLE
);
211 error
= input_ff_create_memless(input_dev
, NULL
,
212 pm8xxx_vib_play_effect
);
215 "couldn't register vibrator as FF device\n");
219 error
= input_register_device(input_dev
);
221 dev_err(&pdev
->dev
, "couldn't register input device\n");
225 platform_set_drvdata(pdev
, vib
);
229 static int __maybe_unused
pm8xxx_vib_suspend(struct device
*dev
)
231 struct pm8xxx_vib
*vib
= dev_get_drvdata(dev
);
233 /* Turn off the vibrator */
234 pm8xxx_vib_set(vib
, false);
239 static SIMPLE_DEV_PM_OPS(pm8xxx_vib_pm_ops
, pm8xxx_vib_suspend
, NULL
);
241 static const struct of_device_id pm8xxx_vib_id_table
[] = {
242 { .compatible
= "qcom,pm8058-vib", .data
= &pm8058_regs
},
243 { .compatible
= "qcom,pm8921-vib", .data
= &pm8058_regs
},
244 { .compatible
= "qcom,pm8916-vib", .data
= &pm8916_regs
},
247 MODULE_DEVICE_TABLE(of
, pm8xxx_vib_id_table
);
249 static struct platform_driver pm8xxx_vib_driver
= {
250 .probe
= pm8xxx_vib_probe
,
252 .name
= "pm8xxx-vib",
253 .pm
= &pm8xxx_vib_pm_ops
,
254 .of_match_table
= pm8xxx_vib_id_table
,
257 module_platform_driver(pm8xxx_vib_driver
);
259 MODULE_ALIAS("platform:pm8xxx_vib");
260 MODULE_DESCRIPTION("PMIC8xxx vibrator driver based on ff-memless framework");
261 MODULE_LICENSE("GPL v2");
262 MODULE_AUTHOR("Amy Maloche <amaloche@codeaurora.org>");