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