Skip to content

Commit 45615a9

Browse files
Xiaotong Ludtor
authored andcommitted
Input: add Spreadtrum vibrator driver
This patch adds the Spreadtrum vibrator driver, which embedded in the Spreadtrum SC27xx series PMICs. Signed-off-by: Xiaotong Lu <[email protected]> Signed-off-by: Baolin Wang <[email protected]> Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent 5ca4d1a commit 45615a9

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Spreadtrum SC27xx PMIC Vibrator
2+
3+
Required properties:
4+
- compatible: should be "sprd,sc2731-vibrator".
5+
- reg: address of vibrator control register.
6+
7+
Example :
8+
9+
sc2731_pmic: pmic@0 {
10+
compatible = "sprd,sc2731";
11+
reg = <0>;
12+
spi-max-frequency = <26000000>;
13+
interrupts = <GIC_SPI 31 IRQ_TYPE_LEVEL_HIGH>;
14+
interrupt-controller;
15+
#interrupt-cells = <2>;
16+
#address-cells = <1>;
17+
#size-cells = <0>;
18+
19+
vibrator@eb4 {
20+
compatible = "sprd,sc2731-vibrator";
21+
reg = <0xeb4>;
22+
};
23+
};

drivers/input/misc/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,4 +841,14 @@ config INPUT_RAVE_SP_PWRBUTTON
841841
To compile this driver as a module, choose M here: the
842842
module will be called rave-sp-pwrbutton.
843843

844+
config INPUT_SC27XX_VIBRA
845+
tristate "Spreadtrum sc27xx vibrator support"
846+
depends on MFD_SC27XX_PMIC || COMPILE_TEST
847+
select INPUT_FF_MEMLESS
848+
help
849+
This option enables support for Spreadtrum sc27xx vibrator driver.
850+
851+
To compile this driver as a module, choose M here. The module will
852+
be called sc27xx_vibra.
853+
844854
endif

drivers/input/misc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ obj-$(CONFIG_INPUT_RETU_PWRBUTTON) += retu-pwrbutton.o
6666
obj-$(CONFIG_INPUT_AXP20X_PEK) += axp20x-pek.o
6767
obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER) += rotary_encoder.o
6868
obj-$(CONFIG_INPUT_RK805_PWRKEY) += rk805-pwrkey.o
69+
obj-$(CONFIG_INPUT_SC27XX_VIBRA) += sc27xx-vibra.o
6970
obj-$(CONFIG_INPUT_SGI_BTNS) += sgi_btns.o
7071
obj-$(CONFIG_INPUT_SIRFSOC_ONKEY) += sirfsoc-onkey.o
7172
obj-$(CONFIG_INPUT_SOC_BUTTON_ARRAY) += soc_button_array.o

drivers/input/misc/sc27xx-vibra.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2018 Spreadtrum Communications Inc.
4+
*/
5+
6+
#include <linux/module.h>
7+
#include <linux/of_address.h>
8+
#include <linux/platform_device.h>
9+
#include <linux/regmap.h>
10+
#include <linux/input.h>
11+
#include <linux/workqueue.h>
12+
13+
#define CUR_DRV_CAL_SEL GENMASK(13, 12)
14+
#define SLP_LDOVIBR_PD_EN BIT(9)
15+
#define LDO_VIBR_PD BIT(8)
16+
17+
struct vibra_info {
18+
struct input_dev *input_dev;
19+
struct work_struct play_work;
20+
struct regmap *regmap;
21+
u32 base;
22+
u32 strength;
23+
bool enabled;
24+
};
25+
26+
static void sc27xx_vibra_set(struct vibra_info *info, bool on)
27+
{
28+
if (on) {
29+
regmap_update_bits(info->regmap, info->base, LDO_VIBR_PD, 0);
30+
regmap_update_bits(info->regmap, info->base,
31+
SLP_LDOVIBR_PD_EN, 0);
32+
info->enabled = true;
33+
} else {
34+
regmap_update_bits(info->regmap, info->base, LDO_VIBR_PD,
35+
LDO_VIBR_PD);
36+
regmap_update_bits(info->regmap, info->base,
37+
SLP_LDOVIBR_PD_EN, SLP_LDOVIBR_PD_EN);
38+
info->enabled = false;
39+
}
40+
}
41+
42+
static int sc27xx_vibra_hw_init(struct vibra_info *info)
43+
{
44+
return regmap_update_bits(info->regmap, info->base, CUR_DRV_CAL_SEL, 0);
45+
}
46+
47+
static void sc27xx_vibra_play_work(struct work_struct *work)
48+
{
49+
struct vibra_info *info = container_of(work, struct vibra_info,
50+
play_work);
51+
52+
if (info->strength && !info->enabled)
53+
sc27xx_vibra_set(info, true);
54+
else if (info->strength == 0 && info->enabled)
55+
sc27xx_vibra_set(info, false);
56+
}
57+
58+
static int sc27xx_vibra_play(struct input_dev *input, void *data,
59+
struct ff_effect *effect)
60+
{
61+
struct vibra_info *info = input_get_drvdata(input);
62+
63+
info->strength = effect->u.rumble.weak_magnitude;
64+
schedule_work(&info->play_work);
65+
66+
return 0;
67+
}
68+
69+
static void sc27xx_vibra_close(struct input_dev *input)
70+
{
71+
struct vibra_info *info = input_get_drvdata(input);
72+
73+
cancel_work_sync(&info->play_work);
74+
if (info->enabled)
75+
sc27xx_vibra_set(info, false);
76+
}
77+
78+
static int sc27xx_vibra_probe(struct platform_device *pdev)
79+
{
80+
struct vibra_info *info;
81+
int error;
82+
83+
info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
84+
if (!info)
85+
return -ENOMEM;
86+
87+
info->regmap = dev_get_regmap(pdev->dev.parent, NULL);
88+
if (!info->regmap) {
89+
dev_err(&pdev->dev, "failed to get vibrator regmap.\n");
90+
return -ENODEV;
91+
}
92+
93+
error = device_property_read_u32(&pdev->dev, "reg", &info->base);
94+
if (error) {
95+
dev_err(&pdev->dev, "failed to get vibrator base address.\n");
96+
return error;
97+
}
98+
99+
info->input_dev = devm_input_allocate_device(&pdev->dev);
100+
if (!info->input_dev) {
101+
dev_err(&pdev->dev, "failed to allocate input device.\n");
102+
return -ENOMEM;
103+
}
104+
105+
info->input_dev->name = "sc27xx:vibrator";
106+
info->input_dev->id.version = 0;
107+
info->input_dev->close = sc27xx_vibra_close;
108+
109+
input_set_drvdata(info->input_dev, info);
110+
input_set_capability(info->input_dev, EV_FF, FF_RUMBLE);
111+
INIT_WORK(&info->play_work, sc27xx_vibra_play_work);
112+
info->enabled = false;
113+
114+
error = sc27xx_vibra_hw_init(info);
115+
if (error) {
116+
dev_err(&pdev->dev, "failed to initialize the vibrator.\n");
117+
return error;
118+
}
119+
120+
error = input_ff_create_memless(info->input_dev, NULL,
121+
sc27xx_vibra_play);
122+
if (error) {
123+
dev_err(&pdev->dev, "failed to register vibrator to FF.\n");
124+
return error;
125+
}
126+
127+
error = input_register_device(info->input_dev);
128+
if (error) {
129+
dev_err(&pdev->dev, "failed to register input device.\n");
130+
return error;
131+
}
132+
133+
return 0;
134+
}
135+
136+
static const struct of_device_id sc27xx_vibra_of_match[] = {
137+
{ .compatible = "sprd,sc2731-vibrator", },
138+
{}
139+
};
140+
MODULE_DEVICE_TABLE(of, sc27xx_vibra_of_match);
141+
142+
static struct platform_driver sc27xx_vibra_driver = {
143+
.driver = {
144+
.name = "sc27xx-vibrator",
145+
.of_match_table = sc27xx_vibra_of_match,
146+
},
147+
.probe = sc27xx_vibra_probe,
148+
};
149+
150+
module_platform_driver(sc27xx_vibra_driver);
151+
152+
MODULE_DESCRIPTION("Spreadtrum SC27xx Vibrator Driver");
153+
MODULE_LICENSE("GPL v2");
154+
MODULE_AUTHOR("Xiaotong Lu <[email protected]>");

0 commit comments

Comments
 (0)