1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2019 Luca Weiss <luca@z3ntu.xyz>
7 * Based on PWM vibrator driver:
8 * Copyright (C) 2017 Collabora Ltd.
10 * Based on previous work from:
11 * Copyright (C) 2012 Dmitry Torokhov <dmitry.torokhov@gmail.com>
13 * Based on PWM beeper driver:
14 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
17 #include <linux/gpio/consumer.h>
18 #include <linux/input.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/property.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
27 struct gpio_vibrator
{
28 struct input_dev
*input
;
29 struct gpio_desc
*gpio
;
30 struct regulator
*vcc
;
32 struct work_struct play_work
;
37 static int gpio_vibrator_start(struct gpio_vibrator
*vibrator
)
39 struct device
*pdev
= vibrator
->input
->dev
.parent
;
42 if (!vibrator
->vcc_on
) {
43 err
= regulator_enable(vibrator
->vcc
);
45 dev_err(pdev
, "failed to enable regulator: %d\n", err
);
48 vibrator
->vcc_on
= true;
51 gpiod_set_value_cansleep(vibrator
->gpio
, 1);
56 static void gpio_vibrator_stop(struct gpio_vibrator
*vibrator
)
58 gpiod_set_value_cansleep(vibrator
->gpio
, 0);
60 if (vibrator
->vcc_on
) {
61 regulator_disable(vibrator
->vcc
);
62 vibrator
->vcc_on
= false;
66 static void gpio_vibrator_play_work(struct work_struct
*work
)
68 struct gpio_vibrator
*vibrator
=
69 container_of(work
, struct gpio_vibrator
, play_work
);
71 if (vibrator
->running
)
72 gpio_vibrator_start(vibrator
);
74 gpio_vibrator_stop(vibrator
);
77 static int gpio_vibrator_play_effect(struct input_dev
*dev
, void *data
,
78 struct ff_effect
*effect
)
80 struct gpio_vibrator
*vibrator
= input_get_drvdata(dev
);
83 level
= effect
->u
.rumble
.strong_magnitude
;
85 level
= effect
->u
.rumble
.weak_magnitude
;
87 vibrator
->running
= level
;
88 schedule_work(&vibrator
->play_work
);
93 static void gpio_vibrator_close(struct input_dev
*input
)
95 struct gpio_vibrator
*vibrator
= input_get_drvdata(input
);
97 cancel_work_sync(&vibrator
->play_work
);
98 gpio_vibrator_stop(vibrator
);
99 vibrator
->running
= false;
102 static int gpio_vibrator_probe(struct platform_device
*pdev
)
104 struct gpio_vibrator
*vibrator
;
107 vibrator
= devm_kzalloc(&pdev
->dev
, sizeof(*vibrator
), GFP_KERNEL
);
111 vibrator
->input
= devm_input_allocate_device(&pdev
->dev
);
112 if (!vibrator
->input
)
115 vibrator
->vcc
= devm_regulator_get(&pdev
->dev
, "vcc");
116 if (IS_ERR(vibrator
->vcc
))
117 return dev_err_probe(&pdev
->dev
, PTR_ERR(vibrator
->vcc
),
118 "Failed to request regulator\n");
120 vibrator
->gpio
= devm_gpiod_get(&pdev
->dev
, "enable", GPIOD_OUT_LOW
);
121 if (IS_ERR(vibrator
->gpio
))
122 return dev_err_probe(&pdev
->dev
, PTR_ERR(vibrator
->gpio
),
123 "Failed to request main gpio\n");
125 INIT_WORK(&vibrator
->play_work
, gpio_vibrator_play_work
);
127 vibrator
->input
->name
= "gpio-vibrator";
128 vibrator
->input
->id
.bustype
= BUS_HOST
;
129 vibrator
->input
->close
= gpio_vibrator_close
;
131 input_set_drvdata(vibrator
->input
, vibrator
);
132 input_set_capability(vibrator
->input
, EV_FF
, FF_RUMBLE
);
134 err
= input_ff_create_memless(vibrator
->input
, NULL
,
135 gpio_vibrator_play_effect
);
137 dev_err(&pdev
->dev
, "Couldn't create FF dev: %d\n", err
);
141 err
= input_register_device(vibrator
->input
);
143 dev_err(&pdev
->dev
, "Couldn't register input dev: %d\n", err
);
147 platform_set_drvdata(pdev
, vibrator
);
152 static int gpio_vibrator_suspend(struct device
*dev
)
154 struct platform_device
*pdev
= to_platform_device(dev
);
155 struct gpio_vibrator
*vibrator
= platform_get_drvdata(pdev
);
157 cancel_work_sync(&vibrator
->play_work
);
158 if (vibrator
->running
)
159 gpio_vibrator_stop(vibrator
);
164 static int gpio_vibrator_resume(struct device
*dev
)
166 struct platform_device
*pdev
= to_platform_device(dev
);
167 struct gpio_vibrator
*vibrator
= platform_get_drvdata(pdev
);
169 if (vibrator
->running
)
170 gpio_vibrator_start(vibrator
);
175 static DEFINE_SIMPLE_DEV_PM_OPS(gpio_vibrator_pm_ops
,
176 gpio_vibrator_suspend
, gpio_vibrator_resume
);
179 static const struct of_device_id gpio_vibra_dt_match_table
[] = {
180 { .compatible
= "gpio-vibrator" },
183 MODULE_DEVICE_TABLE(of
, gpio_vibra_dt_match_table
);
186 static struct platform_driver gpio_vibrator_driver
= {
187 .probe
= gpio_vibrator_probe
,
189 .name
= "gpio-vibrator",
190 .pm
= pm_sleep_ptr(&gpio_vibrator_pm_ops
),
191 .of_match_table
= of_match_ptr(gpio_vibra_dt_match_table
),
194 module_platform_driver(gpio_vibrator_driver
);
196 MODULE_AUTHOR("Luca Weiss <luca@z3ntu.xy>");
197 MODULE_DESCRIPTION("GPIO vibrator driver");
198 MODULE_LICENSE("GPL");
199 MODULE_ALIAS("platform:gpio-vibrator");