xref: /openbmc/linux/drivers/input/misc/pwm-vibra.c (revision 569820be)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  PWM vibrator driver
4  *
5  *  Copyright (C) 2017 Collabora Ltd.
6  *
7  *  Based on previous work from:
8  *  Copyright (C) 2012 Dmitry Torokhov <dmitry.torokhov@gmail.com>
9  *
10  *  Based on PWM beeper driver:
11  *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
12  */
13 
14 #include <linux/gpio/consumer.h>
15 #include <linux/input.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/property.h>
21 #include <linux/pwm.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 
25 struct pwm_vibrator {
26 	struct input_dev *input;
27 	struct gpio_desc *enable_gpio;
28 	struct pwm_device *pwm;
29 	struct pwm_device *pwm_dir;
30 	struct regulator *vcc;
31 
32 	struct work_struct play_work;
33 	u16 level;
34 	u32 direction_duty_cycle;
35 	bool vcc_on;
36 };
37 
38 static int pwm_vibrator_start(struct pwm_vibrator *vibrator)
39 {
40 	struct device *pdev = vibrator->input->dev.parent;
41 	struct pwm_state state;
42 	int err;
43 
44 	if (!vibrator->vcc_on) {
45 		err = regulator_enable(vibrator->vcc);
46 		if (err) {
47 			dev_err(pdev, "failed to enable regulator: %d\n", err);
48 			return err;
49 		}
50 		vibrator->vcc_on = true;
51 	}
52 
53 	gpiod_set_value_cansleep(vibrator->enable_gpio, 1);
54 
55 	pwm_get_state(vibrator->pwm, &state);
56 	pwm_set_relative_duty_cycle(&state, vibrator->level, 0xffff);
57 	state.enabled = true;
58 
59 	err = pwm_apply_state(vibrator->pwm, &state);
60 	if (err) {
61 		dev_err(pdev, "failed to apply pwm state: %d\n", err);
62 		return err;
63 	}
64 
65 	if (vibrator->pwm_dir) {
66 		pwm_get_state(vibrator->pwm_dir, &state);
67 		state.duty_cycle = vibrator->direction_duty_cycle;
68 		state.enabled = true;
69 
70 		err = pwm_apply_state(vibrator->pwm_dir, &state);
71 		if (err) {
72 			dev_err(pdev, "failed to apply dir-pwm state: %d\n", err);
73 			pwm_disable(vibrator->pwm);
74 			return err;
75 		}
76 	}
77 
78 	return 0;
79 }
80 
81 static void pwm_vibrator_stop(struct pwm_vibrator *vibrator)
82 {
83 	if (vibrator->pwm_dir)
84 		pwm_disable(vibrator->pwm_dir);
85 	pwm_disable(vibrator->pwm);
86 
87 	gpiod_set_value_cansleep(vibrator->enable_gpio, 0);
88 
89 	if (vibrator->vcc_on) {
90 		regulator_disable(vibrator->vcc);
91 		vibrator->vcc_on = false;
92 	}
93 }
94 
95 static void pwm_vibrator_play_work(struct work_struct *work)
96 {
97 	struct pwm_vibrator *vibrator = container_of(work,
98 					struct pwm_vibrator, play_work);
99 
100 	if (vibrator->level)
101 		pwm_vibrator_start(vibrator);
102 	else
103 		pwm_vibrator_stop(vibrator);
104 }
105 
106 static int pwm_vibrator_play_effect(struct input_dev *dev, void *data,
107 				    struct ff_effect *effect)
108 {
109 	struct pwm_vibrator *vibrator = input_get_drvdata(dev);
110 
111 	vibrator->level = effect->u.rumble.strong_magnitude;
112 	if (!vibrator->level)
113 		vibrator->level = effect->u.rumble.weak_magnitude;
114 
115 	schedule_work(&vibrator->play_work);
116 
117 	return 0;
118 }
119 
120 static void pwm_vibrator_close(struct input_dev *input)
121 {
122 	struct pwm_vibrator *vibrator = input_get_drvdata(input);
123 
124 	cancel_work_sync(&vibrator->play_work);
125 	pwm_vibrator_stop(vibrator);
126 }
127 
128 static int pwm_vibrator_probe(struct platform_device *pdev)
129 {
130 	struct pwm_vibrator *vibrator;
131 	struct pwm_state state;
132 	int err;
133 
134 	vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL);
135 	if (!vibrator)
136 		return -ENOMEM;
137 
138 	vibrator->input = devm_input_allocate_device(&pdev->dev);
139 	if (!vibrator->input)
140 		return -ENOMEM;
141 
142 	vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
143 	err = PTR_ERR_OR_ZERO(vibrator->vcc);
144 	if (err) {
145 		if (err != -EPROBE_DEFER)
146 			dev_err(&pdev->dev, "Failed to request regulator: %d\n",
147 				err);
148 		return err;
149 	}
150 
151 	vibrator->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
152 							GPIOD_OUT_LOW);
153 	err = PTR_ERR_OR_ZERO(vibrator->enable_gpio);
154 	if (err) {
155 		if (err != -EPROBE_DEFER)
156 			dev_err(&pdev->dev, "Failed to request enable gpio: %d\n",
157 				err);
158 		return err;
159 	}
160 
161 	vibrator->pwm = devm_pwm_get(&pdev->dev, "enable");
162 	err = PTR_ERR_OR_ZERO(vibrator->pwm);
163 	if (err) {
164 		if (err != -EPROBE_DEFER)
165 			dev_err(&pdev->dev, "Failed to request main pwm: %d\n",
166 				err);
167 		return err;
168 	}
169 
170 	INIT_WORK(&vibrator->play_work, pwm_vibrator_play_work);
171 
172 	/* Sync up PWM state and ensure it is off. */
173 	pwm_init_state(vibrator->pwm, &state);
174 	state.enabled = false;
175 	err = pwm_apply_state(vibrator->pwm, &state);
176 	if (err) {
177 		dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n",
178 			err);
179 		return err;
180 	}
181 
182 	vibrator->pwm_dir = devm_pwm_get(&pdev->dev, "direction");
183 	err = PTR_ERR_OR_ZERO(vibrator->pwm_dir);
184 	switch (err) {
185 	case 0:
186 		/* Sync up PWM state and ensure it is off. */
187 		pwm_init_state(vibrator->pwm_dir, &state);
188 		state.enabled = false;
189 		err = pwm_apply_state(vibrator->pwm_dir, &state);
190 		if (err) {
191 			dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n",
192 				err);
193 			return err;
194 		}
195 
196 		vibrator->direction_duty_cycle =
197 			pwm_get_period(vibrator->pwm_dir) / 2;
198 		device_property_read_u32(&pdev->dev, "direction-duty-cycle-ns",
199 					 &vibrator->direction_duty_cycle);
200 		break;
201 
202 	case -ENODATA:
203 		/* Direction PWM is optional */
204 		vibrator->pwm_dir = NULL;
205 		break;
206 
207 	default:
208 		dev_err(&pdev->dev, "Failed to request direction pwm: %d\n", err);
209 		fallthrough;
210 
211 	case -EPROBE_DEFER:
212 		return err;
213 	}
214 
215 	vibrator->input->name = "pwm-vibrator";
216 	vibrator->input->id.bustype = BUS_HOST;
217 	vibrator->input->dev.parent = &pdev->dev;
218 	vibrator->input->close = pwm_vibrator_close;
219 
220 	input_set_drvdata(vibrator->input, vibrator);
221 	input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
222 
223 	err = input_ff_create_memless(vibrator->input, NULL,
224 				      pwm_vibrator_play_effect);
225 	if (err) {
226 		dev_err(&pdev->dev, "Couldn't create FF dev: %d\n", err);
227 		return err;
228 	}
229 
230 	err = input_register_device(vibrator->input);
231 	if (err) {
232 		dev_err(&pdev->dev, "Couldn't register input dev: %d\n", err);
233 		return err;
234 	}
235 
236 	platform_set_drvdata(pdev, vibrator);
237 
238 	return 0;
239 }
240 
241 static int pwm_vibrator_suspend(struct device *dev)
242 {
243 	struct pwm_vibrator *vibrator = dev_get_drvdata(dev);
244 
245 	cancel_work_sync(&vibrator->play_work);
246 	if (vibrator->level)
247 		pwm_vibrator_stop(vibrator);
248 
249 	return 0;
250 }
251 
252 static int pwm_vibrator_resume(struct device *dev)
253 {
254 	struct pwm_vibrator *vibrator = dev_get_drvdata(dev);
255 
256 	if (vibrator->level)
257 		pwm_vibrator_start(vibrator);
258 
259 	return 0;
260 }
261 
262 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_vibrator_pm_ops,
263 				pwm_vibrator_suspend, pwm_vibrator_resume);
264 
265 #ifdef CONFIG_OF
266 static const struct of_device_id pwm_vibra_dt_match_table[] = {
267 	{ .compatible = "pwm-vibrator" },
268 	{},
269 };
270 MODULE_DEVICE_TABLE(of, pwm_vibra_dt_match_table);
271 #endif
272 
273 static struct platform_driver pwm_vibrator_driver = {
274 	.probe	= pwm_vibrator_probe,
275 	.driver	= {
276 		.name	= "pwm-vibrator",
277 		.pm	= pm_sleep_ptr(&pwm_vibrator_pm_ops),
278 		.of_match_table = of_match_ptr(pwm_vibra_dt_match_table),
279 	},
280 };
281 module_platform_driver(pwm_vibrator_driver);
282 
283 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
284 MODULE_DESCRIPTION("PWM vibrator driver");
285 MODULE_LICENSE("GPL");
286 MODULE_ALIAS("platform:pwm-vibrator");
287