xref: /linux/drivers/input/misc/gpio-vibra.c (revision 13bda2d07791d0093728aed7698e76d2977e7639)
1*13bda2d0SLuca Weiss // SPDX-License-Identifier: GPL-2.0+
2*13bda2d0SLuca Weiss /*
3*13bda2d0SLuca Weiss  *  GPIO vibrator driver
4*13bda2d0SLuca Weiss  *
5*13bda2d0SLuca Weiss  *  Copyright (C) 2019 Luca Weiss <luca@z3ntu.xyz>
6*13bda2d0SLuca Weiss  *
7*13bda2d0SLuca Weiss  *  Based on PWM vibrator driver:
8*13bda2d0SLuca Weiss  *  Copyright (C) 2017 Collabora Ltd.
9*13bda2d0SLuca Weiss  *
10*13bda2d0SLuca Weiss  *  Based on previous work from:
11*13bda2d0SLuca Weiss  *  Copyright (C) 2012 Dmitry Torokhov <dmitry.torokhov@gmail.com>
12*13bda2d0SLuca Weiss  *
13*13bda2d0SLuca Weiss  *  Based on PWM beeper driver:
14*13bda2d0SLuca Weiss  *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
15*13bda2d0SLuca Weiss  */
16*13bda2d0SLuca Weiss 
17*13bda2d0SLuca Weiss #include <linux/gpio/consumer.h>
18*13bda2d0SLuca Weiss #include <linux/input.h>
19*13bda2d0SLuca Weiss #include <linux/kernel.h>
20*13bda2d0SLuca Weiss #include <linux/module.h>
21*13bda2d0SLuca Weiss #include <linux/of_device.h>
22*13bda2d0SLuca Weiss #include <linux/platform_device.h>
23*13bda2d0SLuca Weiss #include <linux/property.h>
24*13bda2d0SLuca Weiss #include <linux/regulator/consumer.h>
25*13bda2d0SLuca Weiss #include <linux/slab.h>
26*13bda2d0SLuca Weiss 
27*13bda2d0SLuca Weiss struct gpio_vibrator {
28*13bda2d0SLuca Weiss 	struct input_dev *input;
29*13bda2d0SLuca Weiss 	struct gpio_desc *gpio;
30*13bda2d0SLuca Weiss 	struct regulator *vcc;
31*13bda2d0SLuca Weiss 
32*13bda2d0SLuca Weiss 	struct work_struct play_work;
33*13bda2d0SLuca Weiss 	bool running;
34*13bda2d0SLuca Weiss 	bool vcc_on;
35*13bda2d0SLuca Weiss };
36*13bda2d0SLuca Weiss 
37*13bda2d0SLuca Weiss static int gpio_vibrator_start(struct gpio_vibrator *vibrator)
38*13bda2d0SLuca Weiss {
39*13bda2d0SLuca Weiss 	struct device *pdev = vibrator->input->dev.parent;
40*13bda2d0SLuca Weiss 	int err;
41*13bda2d0SLuca Weiss 
42*13bda2d0SLuca Weiss 	if (!vibrator->vcc_on) {
43*13bda2d0SLuca Weiss 		err = regulator_enable(vibrator->vcc);
44*13bda2d0SLuca Weiss 		if (err) {
45*13bda2d0SLuca Weiss 			dev_err(pdev, "failed to enable regulator: %d\n", err);
46*13bda2d0SLuca Weiss 			return err;
47*13bda2d0SLuca Weiss 		}
48*13bda2d0SLuca Weiss 		vibrator->vcc_on = true;
49*13bda2d0SLuca Weiss 	}
50*13bda2d0SLuca Weiss 
51*13bda2d0SLuca Weiss 	gpiod_set_value_cansleep(vibrator->gpio, 1);
52*13bda2d0SLuca Weiss 
53*13bda2d0SLuca Weiss 	return 0;
54*13bda2d0SLuca Weiss }
55*13bda2d0SLuca Weiss 
56*13bda2d0SLuca Weiss static void gpio_vibrator_stop(struct gpio_vibrator *vibrator)
57*13bda2d0SLuca Weiss {
58*13bda2d0SLuca Weiss 	gpiod_set_value_cansleep(vibrator->gpio, 0);
59*13bda2d0SLuca Weiss 
60*13bda2d0SLuca Weiss 	if (vibrator->vcc_on) {
61*13bda2d0SLuca Weiss 		regulator_disable(vibrator->vcc);
62*13bda2d0SLuca Weiss 		vibrator->vcc_on = false;
63*13bda2d0SLuca Weiss 	}
64*13bda2d0SLuca Weiss }
65*13bda2d0SLuca Weiss 
66*13bda2d0SLuca Weiss static void gpio_vibrator_play_work(struct work_struct *work)
67*13bda2d0SLuca Weiss {
68*13bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator =
69*13bda2d0SLuca Weiss 		container_of(work, struct gpio_vibrator, play_work);
70*13bda2d0SLuca Weiss 
71*13bda2d0SLuca Weiss 	if (vibrator->running)
72*13bda2d0SLuca Weiss 		gpio_vibrator_start(vibrator);
73*13bda2d0SLuca Weiss 	else
74*13bda2d0SLuca Weiss 		gpio_vibrator_stop(vibrator);
75*13bda2d0SLuca Weiss }
76*13bda2d0SLuca Weiss 
77*13bda2d0SLuca Weiss static int gpio_vibrator_play_effect(struct input_dev *dev, void *data,
78*13bda2d0SLuca Weiss 				     struct ff_effect *effect)
79*13bda2d0SLuca Weiss {
80*13bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator = input_get_drvdata(dev);
81*13bda2d0SLuca Weiss 	int level;
82*13bda2d0SLuca Weiss 
83*13bda2d0SLuca Weiss 	level = effect->u.rumble.strong_magnitude;
84*13bda2d0SLuca Weiss 	if (!level)
85*13bda2d0SLuca Weiss 		level = effect->u.rumble.weak_magnitude;
86*13bda2d0SLuca Weiss 
87*13bda2d0SLuca Weiss 	vibrator->running = level;
88*13bda2d0SLuca Weiss 	schedule_work(&vibrator->play_work);
89*13bda2d0SLuca Weiss 
90*13bda2d0SLuca Weiss 	return 0;
91*13bda2d0SLuca Weiss }
92*13bda2d0SLuca Weiss 
93*13bda2d0SLuca Weiss static void gpio_vibrator_close(struct input_dev *input)
94*13bda2d0SLuca Weiss {
95*13bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator = input_get_drvdata(input);
96*13bda2d0SLuca Weiss 
97*13bda2d0SLuca Weiss 	cancel_work_sync(&vibrator->play_work);
98*13bda2d0SLuca Weiss 	gpio_vibrator_stop(vibrator);
99*13bda2d0SLuca Weiss 	vibrator->running = false;
100*13bda2d0SLuca Weiss }
101*13bda2d0SLuca Weiss 
102*13bda2d0SLuca Weiss static int gpio_vibrator_probe(struct platform_device *pdev)
103*13bda2d0SLuca Weiss {
104*13bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator;
105*13bda2d0SLuca Weiss 	int err;
106*13bda2d0SLuca Weiss 
107*13bda2d0SLuca Weiss 	vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL);
108*13bda2d0SLuca Weiss 	if (!vibrator)
109*13bda2d0SLuca Weiss 		return -ENOMEM;
110*13bda2d0SLuca Weiss 
111*13bda2d0SLuca Weiss 	vibrator->input = devm_input_allocate_device(&pdev->dev);
112*13bda2d0SLuca Weiss 	if (!vibrator->input)
113*13bda2d0SLuca Weiss 		return -ENOMEM;
114*13bda2d0SLuca Weiss 
115*13bda2d0SLuca Weiss 	vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
116*13bda2d0SLuca Weiss 	err = PTR_ERR_OR_ZERO(vibrator->vcc);
117*13bda2d0SLuca Weiss 	if (err) {
118*13bda2d0SLuca Weiss 		if (err != -EPROBE_DEFER)
119*13bda2d0SLuca Weiss 			dev_err(&pdev->dev, "Failed to request regulator: %d\n",
120*13bda2d0SLuca Weiss 				err);
121*13bda2d0SLuca Weiss 		return err;
122*13bda2d0SLuca Weiss 	}
123*13bda2d0SLuca Weiss 
124*13bda2d0SLuca Weiss 	vibrator->gpio = devm_gpiod_get(&pdev->dev, "enable", GPIOD_OUT_LOW);
125*13bda2d0SLuca Weiss 	err = PTR_ERR_OR_ZERO(vibrator->gpio);
126*13bda2d0SLuca Weiss 	if (err) {
127*13bda2d0SLuca Weiss 		if (err != -EPROBE_DEFER)
128*13bda2d0SLuca Weiss 			dev_err(&pdev->dev, "Failed to request main gpio: %d\n",
129*13bda2d0SLuca Weiss 				err);
130*13bda2d0SLuca Weiss 		return err;
131*13bda2d0SLuca Weiss 	}
132*13bda2d0SLuca Weiss 
133*13bda2d0SLuca Weiss 	INIT_WORK(&vibrator->play_work, gpio_vibrator_play_work);
134*13bda2d0SLuca Weiss 
135*13bda2d0SLuca Weiss 	vibrator->input->name = "gpio-vibrator";
136*13bda2d0SLuca Weiss 	vibrator->input->id.bustype = BUS_HOST;
137*13bda2d0SLuca Weiss 	vibrator->input->close = gpio_vibrator_close;
138*13bda2d0SLuca Weiss 
139*13bda2d0SLuca Weiss 	input_set_drvdata(vibrator->input, vibrator);
140*13bda2d0SLuca Weiss 	input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
141*13bda2d0SLuca Weiss 
142*13bda2d0SLuca Weiss 	err = input_ff_create_memless(vibrator->input, NULL,
143*13bda2d0SLuca Weiss 				      gpio_vibrator_play_effect);
144*13bda2d0SLuca Weiss 	if (err) {
145*13bda2d0SLuca Weiss 		dev_err(&pdev->dev, "Couldn't create FF dev: %d\n", err);
146*13bda2d0SLuca Weiss 		return err;
147*13bda2d0SLuca Weiss 	}
148*13bda2d0SLuca Weiss 
149*13bda2d0SLuca Weiss 	err = input_register_device(vibrator->input);
150*13bda2d0SLuca Weiss 	if (err) {
151*13bda2d0SLuca Weiss 		dev_err(&pdev->dev, "Couldn't register input dev: %d\n", err);
152*13bda2d0SLuca Weiss 		return err;
153*13bda2d0SLuca Weiss 	}
154*13bda2d0SLuca Weiss 
155*13bda2d0SLuca Weiss 	platform_set_drvdata(pdev, vibrator);
156*13bda2d0SLuca Weiss 
157*13bda2d0SLuca Weiss 	return 0;
158*13bda2d0SLuca Weiss }
159*13bda2d0SLuca Weiss 
160*13bda2d0SLuca Weiss static int __maybe_unused gpio_vibrator_suspend(struct device *dev)
161*13bda2d0SLuca Weiss {
162*13bda2d0SLuca Weiss 	struct platform_device *pdev = to_platform_device(dev);
163*13bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
164*13bda2d0SLuca Weiss 
165*13bda2d0SLuca Weiss 	cancel_work_sync(&vibrator->play_work);
166*13bda2d0SLuca Weiss 	if (vibrator->running)
167*13bda2d0SLuca Weiss 		gpio_vibrator_stop(vibrator);
168*13bda2d0SLuca Weiss 
169*13bda2d0SLuca Weiss 	return 0;
170*13bda2d0SLuca Weiss }
171*13bda2d0SLuca Weiss 
172*13bda2d0SLuca Weiss static int __maybe_unused gpio_vibrator_resume(struct device *dev)
173*13bda2d0SLuca Weiss {
174*13bda2d0SLuca Weiss 	struct platform_device *pdev = to_platform_device(dev);
175*13bda2d0SLuca Weiss 	struct gpio_vibrator *vibrator = platform_get_drvdata(pdev);
176*13bda2d0SLuca Weiss 
177*13bda2d0SLuca Weiss 	if (vibrator->running)
178*13bda2d0SLuca Weiss 		gpio_vibrator_start(vibrator);
179*13bda2d0SLuca Weiss 
180*13bda2d0SLuca Weiss 	return 0;
181*13bda2d0SLuca Weiss }
182*13bda2d0SLuca Weiss 
183*13bda2d0SLuca Weiss static SIMPLE_DEV_PM_OPS(gpio_vibrator_pm_ops,
184*13bda2d0SLuca Weiss 			 gpio_vibrator_suspend, gpio_vibrator_resume);
185*13bda2d0SLuca Weiss 
186*13bda2d0SLuca Weiss #ifdef CONFIG_OF
187*13bda2d0SLuca Weiss static const struct of_device_id gpio_vibra_dt_match_table[] = {
188*13bda2d0SLuca Weiss 	{ .compatible = "gpio-vibrator" },
189*13bda2d0SLuca Weiss 	{}
190*13bda2d0SLuca Weiss };
191*13bda2d0SLuca Weiss MODULE_DEVICE_TABLE(of, gpio_vibra_dt_match_table);
192*13bda2d0SLuca Weiss #endif
193*13bda2d0SLuca Weiss 
194*13bda2d0SLuca Weiss static struct platform_driver gpio_vibrator_driver = {
195*13bda2d0SLuca Weiss 	.probe	= gpio_vibrator_probe,
196*13bda2d0SLuca Weiss 	.driver	= {
197*13bda2d0SLuca Weiss 		.name	= "gpio-vibrator",
198*13bda2d0SLuca Weiss 		.pm	= &gpio_vibrator_pm_ops,
199*13bda2d0SLuca Weiss 		.of_match_table = of_match_ptr(gpio_vibra_dt_match_table),
200*13bda2d0SLuca Weiss 	},
201*13bda2d0SLuca Weiss };
202*13bda2d0SLuca Weiss module_platform_driver(gpio_vibrator_driver);
203*13bda2d0SLuca Weiss 
204*13bda2d0SLuca Weiss MODULE_AUTHOR("Luca Weiss <luca@z3ntu.xy>");
205*13bda2d0SLuca Weiss MODULE_DESCRIPTION("GPIO vibrator driver");
206*13bda2d0SLuca Weiss MODULE_LICENSE("GPL");
207*13bda2d0SLuca Weiss MODULE_ALIAS("platform:gpio-vibrator");
208