1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2017 Collabora Ltd.
7 * Based on previous work from:
8 * Copyright (C) 2012 Dmitry Torokhov <dmitry.torokhov@gmail.com>
10 * Based on PWM beeper driver:
11 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
14 #include <linux/input.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/pwm.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
25 struct input_dev
*input
;
26 struct pwm_device
*pwm
;
27 struct pwm_device
*pwm_dir
;
28 struct regulator
*vcc
;
30 struct work_struct play_work
;
32 u32 direction_duty_cycle
;
36 static int pwm_vibrator_start(struct pwm_vibrator
*vibrator
)
38 struct device
*pdev
= vibrator
->input
->dev
.parent
;
39 struct pwm_state state
;
42 if (!vibrator
->vcc_on
) {
43 err
= regulator_enable(vibrator
->vcc
);
45 dev_err(pdev
, "failed to enable regulator: %d", err
);
48 vibrator
->vcc_on
= true;
51 pwm_get_state(vibrator
->pwm
, &state
);
52 pwm_set_relative_duty_cycle(&state
, vibrator
->level
, 0xffff);
55 err
= pwm_apply_state(vibrator
->pwm
, &state
);
57 dev_err(pdev
, "failed to apply pwm state: %d", err
);
61 if (vibrator
->pwm_dir
) {
62 pwm_get_state(vibrator
->pwm_dir
, &state
);
63 state
.duty_cycle
= vibrator
->direction_duty_cycle
;
66 err
= pwm_apply_state(vibrator
->pwm_dir
, &state
);
68 dev_err(pdev
, "failed to apply dir-pwm state: %d", err
);
69 pwm_disable(vibrator
->pwm
);
77 static void pwm_vibrator_stop(struct pwm_vibrator
*vibrator
)
79 if (vibrator
->pwm_dir
)
80 pwm_disable(vibrator
->pwm_dir
);
81 pwm_disable(vibrator
->pwm
);
83 if (vibrator
->vcc_on
) {
84 regulator_disable(vibrator
->vcc
);
85 vibrator
->vcc_on
= false;
89 static void pwm_vibrator_play_work(struct work_struct
*work
)
91 struct pwm_vibrator
*vibrator
= container_of(work
,
92 struct pwm_vibrator
, play_work
);
95 pwm_vibrator_start(vibrator
);
97 pwm_vibrator_stop(vibrator
);
100 static int pwm_vibrator_play_effect(struct input_dev
*dev
, void *data
,
101 struct ff_effect
*effect
)
103 struct pwm_vibrator
*vibrator
= input_get_drvdata(dev
);
105 vibrator
->level
= effect
->u
.rumble
.strong_magnitude
;
106 if (!vibrator
->level
)
107 vibrator
->level
= effect
->u
.rumble
.weak_magnitude
;
109 schedule_work(&vibrator
->play_work
);
114 static void pwm_vibrator_close(struct input_dev
*input
)
116 struct pwm_vibrator
*vibrator
= input_get_drvdata(input
);
118 cancel_work_sync(&vibrator
->play_work
);
119 pwm_vibrator_stop(vibrator
);
122 static int pwm_vibrator_probe(struct platform_device
*pdev
)
124 struct pwm_vibrator
*vibrator
;
125 struct pwm_state state
;
128 vibrator
= devm_kzalloc(&pdev
->dev
, sizeof(*vibrator
), GFP_KERNEL
);
132 vibrator
->input
= devm_input_allocate_device(&pdev
->dev
);
133 if (!vibrator
->input
)
136 vibrator
->vcc
= devm_regulator_get(&pdev
->dev
, "vcc");
137 err
= PTR_ERR_OR_ZERO(vibrator
->vcc
);
139 if (err
!= -EPROBE_DEFER
)
140 dev_err(&pdev
->dev
, "Failed to request regulator: %d",
145 vibrator
->pwm
= devm_pwm_get(&pdev
->dev
, "enable");
146 err
= PTR_ERR_OR_ZERO(vibrator
->pwm
);
148 if (err
!= -EPROBE_DEFER
)
149 dev_err(&pdev
->dev
, "Failed to request main pwm: %d",
154 INIT_WORK(&vibrator
->play_work
, pwm_vibrator_play_work
);
156 /* Sync up PWM state and ensure it is off. */
157 pwm_init_state(vibrator
->pwm
, &state
);
158 state
.enabled
= false;
159 err
= pwm_apply_state(vibrator
->pwm
, &state
);
161 dev_err(&pdev
->dev
, "failed to apply initial PWM state: %d",
166 vibrator
->pwm_dir
= devm_pwm_get(&pdev
->dev
, "direction");
167 err
= PTR_ERR_OR_ZERO(vibrator
->pwm_dir
);
170 /* Sync up PWM state and ensure it is off. */
171 pwm_init_state(vibrator
->pwm_dir
, &state
);
172 state
.enabled
= false;
173 err
= pwm_apply_state(vibrator
->pwm_dir
, &state
);
175 dev_err(&pdev
->dev
, "failed to apply initial PWM state: %d",
180 vibrator
->direction_duty_cycle
=
181 pwm_get_period(vibrator
->pwm_dir
) / 2;
182 device_property_read_u32(&pdev
->dev
, "direction-duty-cycle-ns",
183 &vibrator
->direction_duty_cycle
);
187 /* Direction PWM is optional */
188 vibrator
->pwm_dir
= NULL
;
192 dev_err(&pdev
->dev
, "Failed to request direction pwm: %d", err
);
199 vibrator
->input
->name
= "pwm-vibrator";
200 vibrator
->input
->id
.bustype
= BUS_HOST
;
201 vibrator
->input
->dev
.parent
= &pdev
->dev
;
202 vibrator
->input
->close
= pwm_vibrator_close
;
204 input_set_drvdata(vibrator
->input
, vibrator
);
205 input_set_capability(vibrator
->input
, EV_FF
, FF_RUMBLE
);
207 err
= input_ff_create_memless(vibrator
->input
, NULL
,
208 pwm_vibrator_play_effect
);
210 dev_err(&pdev
->dev
, "Couldn't create FF dev: %d", err
);
214 err
= input_register_device(vibrator
->input
);
216 dev_err(&pdev
->dev
, "Couldn't register input dev: %d", err
);
220 platform_set_drvdata(pdev
, vibrator
);
225 static int __maybe_unused
pwm_vibrator_suspend(struct device
*dev
)
227 struct pwm_vibrator
*vibrator
= dev_get_drvdata(dev
);
229 cancel_work_sync(&vibrator
->play_work
);
231 pwm_vibrator_stop(vibrator
);
236 static int __maybe_unused
pwm_vibrator_resume(struct device
*dev
)
238 struct pwm_vibrator
*vibrator
= dev_get_drvdata(dev
);
241 pwm_vibrator_start(vibrator
);
246 static SIMPLE_DEV_PM_OPS(pwm_vibrator_pm_ops
,
247 pwm_vibrator_suspend
, pwm_vibrator_resume
);
250 static const struct of_device_id pwm_vibra_dt_match_table
[] = {
251 { .compatible
= "pwm-vibrator" },
254 MODULE_DEVICE_TABLE(of
, pwm_vibra_dt_match_table
);
257 static struct platform_driver pwm_vibrator_driver
= {
258 .probe
= pwm_vibrator_probe
,
260 .name
= "pwm-vibrator",
261 .pm
= &pwm_vibrator_pm_ops
,
262 .of_match_table
= of_match_ptr(pwm_vibra_dt_match_table
),
265 module_platform_driver(pwm_vibrator_driver
);
267 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
268 MODULE_DESCRIPTION("PWM vibrator driver");
269 MODULE_LICENSE("GPL");
270 MODULE_ALIAS("platform:pwm-vibrator");