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/module.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/platform_device.h>
17 #include <linux/input.h>
18 #include <linux/slab.h>
19 #include <linux/regmap.h>
23 #define VIB_DRV_SEL_MASK 0xf8
24 #define VIB_DRV_SEL_SHIFT 0x03
25 #define VIB_DRV_EN_MANUAL_MASK 0xfc
27 #define VIB_MAX_LEVEL_mV (3100)
28 #define VIB_MIN_LEVEL_mV (1200)
29 #define VIB_MAX_LEVELS (VIB_MAX_LEVEL_mV - VIB_MIN_LEVEL_mV)
31 #define MAX_FF_SPEED 0xff
34 * struct pm8xxx_vib - structure to hold vibrator data
35 * @vib_input_dev: input device supporting force feedback
36 * @work: work structure to set the vibration parameters
37 * @regmap: regmap for register read/write
38 * @speed: speed of vibration set from userland
39 * @active: state of vibrator
40 * @level: level of vibration to set in the chip
41 * @reg_vib_drv: VIB_DRV register value
44 struct input_dev
*vib_input_dev
;
45 struct work_struct work
;
46 struct regmap
*regmap
;
54 * pm8xxx_vib_set - handler to start/stop vibration
55 * @vib: pointer to vibrator structure
58 static int pm8xxx_vib_set(struct pm8xxx_vib
*vib
, bool on
)
61 unsigned int val
= vib
->reg_vib_drv
;
64 val
|= ((vib
->level
<< VIB_DRV_SEL_SHIFT
) & VIB_DRV_SEL_MASK
);
66 val
&= ~VIB_DRV_SEL_MASK
;
68 rc
= regmap_write(vib
->regmap
, VIB_DRV
, val
);
72 vib
->reg_vib_drv
= val
;
77 * pm8xxx_work_handler - worker to set vibration level
78 * @work: pointer to work_struct
80 static void pm8xxx_work_handler(struct work_struct
*work
)
82 struct pm8xxx_vib
*vib
= container_of(work
, struct pm8xxx_vib
, work
);
86 rc
= regmap_read(vib
->regmap
, VIB_DRV
, &val
);
91 * pmic vibrator supports voltage ranges from 1.2 to 3.1V, so
92 * scale the level to fit into these ranges.
96 vib
->level
= ((VIB_MAX_LEVELS
* vib
->speed
) / MAX_FF_SPEED
) +
101 vib
->level
= VIB_MIN_LEVEL_mV
/ 100;
104 pm8xxx_vib_set(vib
, vib
->active
);
108 * pm8xxx_vib_close - callback of input close callback
109 * @dev: input device pointer
111 * Turns off the vibrator.
113 static void pm8xxx_vib_close(struct input_dev
*dev
)
115 struct pm8xxx_vib
*vib
= input_get_drvdata(dev
);
117 cancel_work_sync(&vib
->work
);
119 pm8xxx_vib_set(vib
, false);
123 * pm8xxx_vib_play_effect - function to handle vib effects.
124 * @dev: input device pointer
125 * @data: data of effect
126 * @effect: effect to play
128 * Currently this driver supports only rumble effects.
130 static int pm8xxx_vib_play_effect(struct input_dev
*dev
, void *data
,
131 struct ff_effect
*effect
)
133 struct pm8xxx_vib
*vib
= input_get_drvdata(dev
);
135 vib
->speed
= effect
->u
.rumble
.strong_magnitude
>> 8;
137 vib
->speed
= effect
->u
.rumble
.weak_magnitude
>> 9;
139 schedule_work(&vib
->work
);
144 static int pm8xxx_vib_probe(struct platform_device
*pdev
)
147 struct pm8xxx_vib
*vib
;
148 struct input_dev
*input_dev
;
152 vib
= devm_kzalloc(&pdev
->dev
, sizeof(*vib
), GFP_KERNEL
);
156 vib
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
160 input_dev
= devm_input_allocate_device(&pdev
->dev
);
164 INIT_WORK(&vib
->work
, pm8xxx_work_handler
);
165 vib
->vib_input_dev
= input_dev
;
167 /* operate in manual mode */
168 error
= regmap_read(vib
->regmap
, VIB_DRV
, &val
);
172 val
&= ~VIB_DRV_EN_MANUAL_MASK
;
173 error
= regmap_write(vib
->regmap
, VIB_DRV
, val
);
177 vib
->reg_vib_drv
= val
;
179 input_dev
->name
= "pm8xxx_vib_ffmemless";
180 input_dev
->id
.version
= 1;
181 input_dev
->close
= pm8xxx_vib_close
;
182 input_set_drvdata(input_dev
, vib
);
183 input_set_capability(vib
->vib_input_dev
, EV_FF
, FF_RUMBLE
);
185 error
= input_ff_create_memless(input_dev
, NULL
,
186 pm8xxx_vib_play_effect
);
189 "couldn't register vibrator as FF device\n");
193 error
= input_register_device(input_dev
);
195 dev_err(&pdev
->dev
, "couldn't register input device\n");
199 platform_set_drvdata(pdev
, vib
);
203 #ifdef CONFIG_PM_SLEEP
204 static int pm8xxx_vib_suspend(struct device
*dev
)
206 struct pm8xxx_vib
*vib
= dev_get_drvdata(dev
);
208 /* Turn off the vibrator */
209 pm8xxx_vib_set(vib
, false);
215 static SIMPLE_DEV_PM_OPS(pm8xxx_vib_pm_ops
, pm8xxx_vib_suspend
, NULL
);
217 static struct platform_driver pm8xxx_vib_driver
= {
218 .probe
= pm8xxx_vib_probe
,
220 .name
= "pm8xxx-vib",
221 .owner
= THIS_MODULE
,
222 .pm
= &pm8xxx_vib_pm_ops
,
225 module_platform_driver(pm8xxx_vib_driver
);
227 MODULE_ALIAS("platform:pm8xxx_vib");
228 MODULE_DESCRIPTION("PMIC8xxx vibrator driver based on ff-memless framework");
229 MODULE_LICENSE("GPL v2");
230 MODULE_AUTHOR("Amy Maloche <amaloche@codeaurora.org>");