1 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
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.
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.
13 #include <linux/errno.h>
14 #include <linux/input.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
23 #define VIB_MAX_LEVEL_mV (3100)
24 #define VIB_MIN_LEVEL_mV (1200)
25 #define VIB_MAX_LEVELS (VIB_MAX_LEVEL_mV - VIB_MIN_LEVEL_mV)
27 #define MAX_FF_SPEED 0xff
30 unsigned int enable_addr
;
31 unsigned int enable_mask
;
33 unsigned int drv_addr
;
34 unsigned int drv_mask
;
35 unsigned int drv_shift
;
36 unsigned int drv_en_manual_mask
;
39 static const struct pm8xxx_regs pm8058_regs
= {
43 .drv_en_manual_mask
= 0xfc,
46 static struct pm8xxx_regs pm8916_regs
= {
47 .enable_addr
= 0xc046,
48 .enable_mask
= BIT(7),
52 .drv_en_manual_mask
= 0,
56 * struct pm8xxx_vib - structure to hold vibrator data
57 * @vib_input_dev: input device supporting force feedback
58 * @work: work structure to set the vibration parameters
59 * @regmap: regmap for register read/write
60 * @regs: registers' info
61 * @speed: speed of vibration set from userland
62 * @active: state of vibrator
63 * @level: level of vibration to set in the chip
64 * @reg_vib_drv: regs->drv_addr register value
67 struct input_dev
*vib_input_dev
;
68 struct work_struct work
;
69 struct regmap
*regmap
;
70 const struct pm8xxx_regs
*regs
;
78 * pm8xxx_vib_set - handler to start/stop vibration
79 * @vib: pointer to vibrator structure
82 static int pm8xxx_vib_set(struct pm8xxx_vib
*vib
, bool on
)
85 unsigned int val
= vib
->reg_vib_drv
;
86 const struct pm8xxx_regs
*regs
= vib
->regs
;
89 val
|= (vib
->level
<< regs
->drv_shift
) & regs
->drv_mask
;
91 val
&= ~regs
->drv_mask
;
93 rc
= regmap_write(vib
->regmap
, regs
->drv_addr
, val
);
97 vib
->reg_vib_drv
= val
;
99 if (regs
->enable_mask
)
100 rc
= regmap_update_bits(vib
->regmap
, regs
->enable_addr
,
101 regs
->enable_mask
, on
? ~0 : 0);
107 * pm8xxx_work_handler - worker to set vibration level
108 * @work: pointer to work_struct
110 static void pm8xxx_work_handler(struct work_struct
*work
)
112 struct pm8xxx_vib
*vib
= container_of(work
, struct pm8xxx_vib
, work
);
113 const struct pm8xxx_regs
*regs
= vib
->regs
;
117 rc
= regmap_read(vib
->regmap
, regs
->drv_addr
, &val
);
122 * pmic vibrator supports voltage ranges from 1.2 to 3.1V, so
123 * scale the level to fit into these ranges.
127 vib
->level
= ((VIB_MAX_LEVELS
* vib
->speed
) / MAX_FF_SPEED
) +
132 vib
->level
= VIB_MIN_LEVEL_mV
/ 100;
135 pm8xxx_vib_set(vib
, vib
->active
);
139 * pm8xxx_vib_close - callback of input close callback
140 * @dev: input device pointer
142 * Turns off the vibrator.
144 static void pm8xxx_vib_close(struct input_dev
*dev
)
146 struct pm8xxx_vib
*vib
= input_get_drvdata(dev
);
148 cancel_work_sync(&vib
->work
);
150 pm8xxx_vib_set(vib
, false);
154 * pm8xxx_vib_play_effect - function to handle vib effects.
155 * @dev: input device pointer
156 * @data: data of effect
157 * @effect: effect to play
159 * Currently this driver supports only rumble effects.
161 static int pm8xxx_vib_play_effect(struct input_dev
*dev
, void *data
,
162 struct ff_effect
*effect
)
164 struct pm8xxx_vib
*vib
= input_get_drvdata(dev
);
166 vib
->speed
= effect
->u
.rumble
.strong_magnitude
>> 8;
168 vib
->speed
= effect
->u
.rumble
.weak_magnitude
>> 9;
170 schedule_work(&vib
->work
);
175 static int pm8xxx_vib_probe(struct platform_device
*pdev
)
177 struct pm8xxx_vib
*vib
;
178 struct input_dev
*input_dev
;
181 const struct pm8xxx_regs
*regs
;
183 vib
= devm_kzalloc(&pdev
->dev
, sizeof(*vib
), GFP_KERNEL
);
187 vib
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
191 input_dev
= devm_input_allocate_device(&pdev
->dev
);
195 INIT_WORK(&vib
->work
, pm8xxx_work_handler
);
196 vib
->vib_input_dev
= input_dev
;
198 regs
= of_device_get_match_data(&pdev
->dev
);
200 /* operate in manual mode */
201 error
= regmap_read(vib
->regmap
, regs
->drv_addr
, &val
);
205 val
&= regs
->drv_en_manual_mask
;
206 error
= regmap_write(vib
->regmap
, regs
->drv_addr
, val
);
211 vib
->reg_vib_drv
= val
;
213 input_dev
->name
= "pm8xxx_vib_ffmemless";
214 input_dev
->id
.version
= 1;
215 input_dev
->close
= pm8xxx_vib_close
;
216 input_set_drvdata(input_dev
, vib
);
217 input_set_capability(vib
->vib_input_dev
, EV_FF
, FF_RUMBLE
);
219 error
= input_ff_create_memless(input_dev
, NULL
,
220 pm8xxx_vib_play_effect
);
223 "couldn't register vibrator as FF device\n");
227 error
= input_register_device(input_dev
);
229 dev_err(&pdev
->dev
, "couldn't register input device\n");
233 platform_set_drvdata(pdev
, vib
);
237 static int __maybe_unused
pm8xxx_vib_suspend(struct device
*dev
)
239 struct pm8xxx_vib
*vib
= dev_get_drvdata(dev
);
241 /* Turn off the vibrator */
242 pm8xxx_vib_set(vib
, false);
247 static SIMPLE_DEV_PM_OPS(pm8xxx_vib_pm_ops
, pm8xxx_vib_suspend
, NULL
);
249 static const struct of_device_id pm8xxx_vib_id_table
[] = {
250 { .compatible
= "qcom,pm8058-vib", .data
= &pm8058_regs
},
251 { .compatible
= "qcom,pm8921-vib", .data
= &pm8058_regs
},
252 { .compatible
= "qcom,pm8916-vib", .data
= &pm8916_regs
},
255 MODULE_DEVICE_TABLE(of
, pm8xxx_vib_id_table
);
257 static struct platform_driver pm8xxx_vib_driver
= {
258 .probe
= pm8xxx_vib_probe
,
260 .name
= "pm8xxx-vib",
261 .pm
= &pm8xxx_vib_pm_ops
,
262 .of_match_table
= pm8xxx_vib_id_table
,
265 module_platform_driver(pm8xxx_vib_driver
);
267 MODULE_ALIAS("platform:pm8xxx_vib");
268 MODULE_DESCRIPTION("PMIC8xxx vibrator driver based on ff-memless framework");
269 MODULE_LICENSE("GPL v2");
270 MODULE_AUTHOR("Amy Maloche <amaloche@codeaurora.org>");