x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / input / misc / pwm-vibra.c
blob55da191ae550743edf6505a01cc3af53404910a9
1 /*
2 * PWM vibrator driver
4 * Copyright (C) 2017 Collabora Ltd.
6 * Based on previous work from:
7 * Copyright (C) 2012 Dmitry Torokhov <dmitry.torokhov@gmail.com>
9 * Based on PWM beeper driver:
10 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
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.
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>
28 struct pwm_vibrator {
29 struct input_dev *input;
30 struct pwm_device *pwm;
31 struct pwm_device *pwm_dir;
32 struct regulator *vcc;
34 struct work_struct play_work;
35 u16 level;
36 u32 direction_duty_cycle;
39 static int pwm_vibrator_start(struct pwm_vibrator *vibrator)
41 struct device *pdev = vibrator->input->dev.parent;
42 struct pwm_state state;
43 int err;
45 err = regulator_enable(vibrator->vcc);
46 if (err) {
47 dev_err(pdev, "failed to enable regulator: %d", err);
48 return err;
51 pwm_get_state(vibrator->pwm, &state);
52 pwm_set_relative_duty_cycle(&state, vibrator->level, 0xffff);
53 state.enabled = true;
55 err = pwm_apply_state(vibrator->pwm, &state);
56 if (err) {
57 dev_err(pdev, "failed to apply pwm state: %d", err);
58 return err;
61 if (vibrator->pwm_dir) {
62 pwm_get_state(vibrator->pwm_dir, &state);
63 state.duty_cycle = vibrator->direction_duty_cycle;
64 state.enabled = true;
66 err = pwm_apply_state(vibrator->pwm_dir, &state);
67 if (err) {
68 dev_err(pdev, "failed to apply dir-pwm state: %d", err);
69 pwm_disable(vibrator->pwm);
70 return err;
74 return 0;
77 static void pwm_vibrator_stop(struct pwm_vibrator *vibrator)
79 regulator_disable(vibrator->vcc);
81 if (vibrator->pwm_dir)
82 pwm_disable(vibrator->pwm_dir);
83 pwm_disable(vibrator->pwm);
86 static void pwm_vibrator_play_work(struct work_struct *work)
88 struct pwm_vibrator *vibrator = container_of(work,
89 struct pwm_vibrator, play_work);
91 if (vibrator->level)
92 pwm_vibrator_start(vibrator);
93 else
94 pwm_vibrator_stop(vibrator);
97 static int pwm_vibrator_play_effect(struct input_dev *dev, void *data,
98 struct ff_effect *effect)
100 struct pwm_vibrator *vibrator = input_get_drvdata(dev);
102 vibrator->level = effect->u.rumble.strong_magnitude;
103 if (!vibrator->level)
104 vibrator->level = effect->u.rumble.weak_magnitude;
106 schedule_work(&vibrator->play_work);
108 return 0;
111 static void pwm_vibrator_close(struct input_dev *input)
113 struct pwm_vibrator *vibrator = input_get_drvdata(input);
115 cancel_work_sync(&vibrator->play_work);
116 pwm_vibrator_stop(vibrator);
119 static int pwm_vibrator_probe(struct platform_device *pdev)
121 struct pwm_vibrator *vibrator;
122 struct pwm_state state;
123 int err;
125 vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL);
126 if (!vibrator)
127 return -ENOMEM;
129 vibrator->input = devm_input_allocate_device(&pdev->dev);
130 if (!vibrator->input)
131 return -ENOMEM;
133 vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
134 err = PTR_ERR_OR_ZERO(vibrator->vcc);
135 if (err) {
136 if (err != -EPROBE_DEFER)
137 dev_err(&pdev->dev, "Failed to request regulator: %d",
138 err);
139 return err;
142 vibrator->pwm = devm_pwm_get(&pdev->dev, "enable");
143 err = PTR_ERR_OR_ZERO(vibrator->pwm);
144 if (err) {
145 if (err != -EPROBE_DEFER)
146 dev_err(&pdev->dev, "Failed to request main pwm: %d",
147 err);
148 return err;
151 INIT_WORK(&vibrator->play_work, pwm_vibrator_play_work);
153 /* Sync up PWM state and ensure it is off. */
154 pwm_init_state(vibrator->pwm, &state);
155 state.enabled = false;
156 err = pwm_apply_state(vibrator->pwm, &state);
157 if (err) {
158 dev_err(&pdev->dev, "failed to apply initial PWM state: %d",
159 err);
160 return err;
163 vibrator->pwm_dir = devm_pwm_get(&pdev->dev, "direction");
164 err = PTR_ERR_OR_ZERO(vibrator->pwm_dir);
165 switch (err) {
166 case 0:
167 /* Sync up PWM state and ensure it is off. */
168 pwm_init_state(vibrator->pwm_dir, &state);
169 state.enabled = false;
170 err = pwm_apply_state(vibrator->pwm_dir, &state);
171 if (err) {
172 dev_err(&pdev->dev, "failed to apply initial PWM state: %d",
173 err);
174 return err;
177 vibrator->direction_duty_cycle =
178 pwm_get_period(vibrator->pwm_dir) / 2;
179 device_property_read_u32(&pdev->dev, "direction-duty-cycle-ns",
180 &vibrator->direction_duty_cycle);
181 break;
183 case -ENODATA:
184 /* Direction PWM is optional */
185 vibrator->pwm_dir = NULL;
186 break;
188 default:
189 dev_err(&pdev->dev, "Failed to request direction pwm: %d", err);
190 /* Fall through */
192 case -EPROBE_DEFER:
193 return err;
196 vibrator->input->name = "pwm-vibrator";
197 vibrator->input->id.bustype = BUS_HOST;
198 vibrator->input->dev.parent = &pdev->dev;
199 vibrator->input->close = pwm_vibrator_close;
201 input_set_drvdata(vibrator->input, vibrator);
202 input_set_capability(vibrator->input, EV_FF, FF_RUMBLE);
204 err = input_ff_create_memless(vibrator->input, NULL,
205 pwm_vibrator_play_effect);
206 if (err) {
207 dev_err(&pdev->dev, "Couldn't create FF dev: %d", err);
208 return err;
211 err = input_register_device(vibrator->input);
212 if (err) {
213 dev_err(&pdev->dev, "Couldn't register input dev: %d", err);
214 return err;
217 platform_set_drvdata(pdev, vibrator);
219 return 0;
222 static int __maybe_unused pwm_vibrator_suspend(struct device *dev)
224 struct pwm_vibrator *vibrator = dev_get_drvdata(dev);
226 cancel_work_sync(&vibrator->play_work);
227 if (vibrator->level)
228 pwm_vibrator_stop(vibrator);
230 return 0;
233 static int __maybe_unused pwm_vibrator_resume(struct device *dev)
235 struct pwm_vibrator *vibrator = dev_get_drvdata(dev);
237 if (vibrator->level)
238 pwm_vibrator_start(vibrator);
240 return 0;
243 static SIMPLE_DEV_PM_OPS(pwm_vibrator_pm_ops,
244 pwm_vibrator_suspend, pwm_vibrator_resume);
246 #ifdef CONFIG_OF
247 static const struct of_device_id pwm_vibra_dt_match_table[] = {
248 { .compatible = "pwm-vibrator" },
251 MODULE_DEVICE_TABLE(of, pwm_vibra_dt_match_table);
252 #endif
254 static struct platform_driver pwm_vibrator_driver = {
255 .probe = pwm_vibrator_probe,
256 .driver = {
257 .name = "pwm-vibrator",
258 .pm = &pwm_vibrator_pm_ops,
259 .of_match_table = of_match_ptr(pwm_vibra_dt_match_table),
262 module_platform_driver(pwm_vibrator_driver);
264 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
265 MODULE_DESCRIPTION("PWM vibrator driver");
266 MODULE_LICENSE("GPL");
267 MODULE_ALIAS("platform:pwm-vibrator");