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/gpio/consumer.h>
15 #include <linux/input.h>
16 #include <linux/kernel.h>
17 #include <linux/module.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>
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
;
32 struct work_struct play_work
;
34 u32 direction_duty_cycle
;
38 static int pwm_vibrator_start(struct pwm_vibrator
*vibrator
)
40 struct device
*pdev
= vibrator
->input
->dev
.parent
;
41 struct pwm_state state
;
44 if (!vibrator
->vcc_on
) {
45 err
= regulator_enable(vibrator
->vcc
);
47 dev_err(pdev
, "failed to enable regulator: %d\n", err
);
50 vibrator
->vcc_on
= true;
53 gpiod_set_value_cansleep(vibrator
->enable_gpio
, 1);
55 pwm_get_state(vibrator
->pwm
, &state
);
56 pwm_set_relative_duty_cycle(&state
, vibrator
->level
, 0xffff);
59 err
= pwm_apply_might_sleep(vibrator
->pwm
, &state
);
61 dev_err(pdev
, "failed to apply pwm state: %d\n", err
);
65 if (vibrator
->pwm_dir
) {
66 pwm_get_state(vibrator
->pwm_dir
, &state
);
67 state
.duty_cycle
= vibrator
->direction_duty_cycle
;
70 err
= pwm_apply_might_sleep(vibrator
->pwm_dir
, &state
);
72 dev_err(pdev
, "failed to apply dir-pwm state: %d\n", err
);
73 pwm_disable(vibrator
->pwm
);
81 static void pwm_vibrator_stop(struct pwm_vibrator
*vibrator
)
83 if (vibrator
->pwm_dir
)
84 pwm_disable(vibrator
->pwm_dir
);
85 pwm_disable(vibrator
->pwm
);
87 gpiod_set_value_cansleep(vibrator
->enable_gpio
, 0);
89 if (vibrator
->vcc_on
) {
90 regulator_disable(vibrator
->vcc
);
91 vibrator
->vcc_on
= false;
95 static void pwm_vibrator_play_work(struct work_struct
*work
)
97 struct pwm_vibrator
*vibrator
= container_of(work
,
98 struct pwm_vibrator
, play_work
);
101 pwm_vibrator_start(vibrator
);
103 pwm_vibrator_stop(vibrator
);
106 static int pwm_vibrator_play_effect(struct input_dev
*dev
, void *data
,
107 struct ff_effect
*effect
)
109 struct pwm_vibrator
*vibrator
= input_get_drvdata(dev
);
111 vibrator
->level
= effect
->u
.rumble
.strong_magnitude
;
112 if (!vibrator
->level
)
113 vibrator
->level
= effect
->u
.rumble
.weak_magnitude
;
115 schedule_work(&vibrator
->play_work
);
120 static void pwm_vibrator_close(struct input_dev
*input
)
122 struct pwm_vibrator
*vibrator
= input_get_drvdata(input
);
124 cancel_work_sync(&vibrator
->play_work
);
125 pwm_vibrator_stop(vibrator
);
128 static int pwm_vibrator_probe(struct platform_device
*pdev
)
130 struct pwm_vibrator
*vibrator
;
131 struct pwm_state state
;
134 vibrator
= devm_kzalloc(&pdev
->dev
, sizeof(*vibrator
), GFP_KERNEL
);
138 vibrator
->input
= devm_input_allocate_device(&pdev
->dev
);
139 if (!vibrator
->input
)
142 vibrator
->vcc
= devm_regulator_get(&pdev
->dev
, "vcc");
143 if (IS_ERR(vibrator
->vcc
))
144 return dev_err_probe(&pdev
->dev
, PTR_ERR(vibrator
->vcc
),
145 "Failed to request regulator\n");
147 vibrator
->enable_gpio
= devm_gpiod_get_optional(&pdev
->dev
, "enable",
149 if (IS_ERR(vibrator
->enable_gpio
))
150 return dev_err_probe(&pdev
->dev
, PTR_ERR(vibrator
->enable_gpio
),
151 "Failed to request enable gpio\n");
153 vibrator
->pwm
= devm_pwm_get(&pdev
->dev
, "enable");
154 if (IS_ERR(vibrator
->pwm
))
155 return dev_err_probe(&pdev
->dev
, PTR_ERR(vibrator
->pwm
),
156 "Failed to request main pwm\n");
158 INIT_WORK(&vibrator
->play_work
, pwm_vibrator_play_work
);
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_might_sleep(vibrator
->pwm
, &state
);
165 dev_err(&pdev
->dev
, "failed to apply initial PWM state: %d\n",
170 vibrator
->pwm_dir
= devm_pwm_get(&pdev
->dev
, "direction");
171 err
= PTR_ERR_OR_ZERO(vibrator
->pwm_dir
);
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_might_sleep(vibrator
->pwm_dir
, &state
);
179 dev_err(&pdev
->dev
, "failed to apply initial PWM state: %d\n",
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
);
191 /* Direction PWM is optional */
192 vibrator
->pwm_dir
= NULL
;
196 dev_err(&pdev
->dev
, "Failed to request direction pwm: %d\n", err
);
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
;
208 input_set_drvdata(vibrator
->input
, vibrator
);
209 input_set_capability(vibrator
->input
, EV_FF
, FF_RUMBLE
);
211 err
= input_ff_create_memless(vibrator
->input
, NULL
,
212 pwm_vibrator_play_effect
);
214 dev_err(&pdev
->dev
, "Couldn't create FF dev: %d\n", err
);
218 err
= input_register_device(vibrator
->input
);
220 dev_err(&pdev
->dev
, "Couldn't register input dev: %d\n", err
);
224 platform_set_drvdata(pdev
, vibrator
);
229 static int pwm_vibrator_suspend(struct device
*dev
)
231 struct pwm_vibrator
*vibrator
= dev_get_drvdata(dev
);
233 cancel_work_sync(&vibrator
->play_work
);
235 pwm_vibrator_stop(vibrator
);
240 static int pwm_vibrator_resume(struct device
*dev
)
242 struct pwm_vibrator
*vibrator
= dev_get_drvdata(dev
);
245 pwm_vibrator_start(vibrator
);
250 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_vibrator_pm_ops
,
251 pwm_vibrator_suspend
, pwm_vibrator_resume
);
254 static const struct of_device_id pwm_vibra_dt_match_table
[] = {
255 { .compatible
= "pwm-vibrator" },
258 MODULE_DEVICE_TABLE(of
, pwm_vibra_dt_match_table
);
261 static struct platform_driver pwm_vibrator_driver
= {
262 .probe
= pwm_vibrator_probe
,
264 .name
= "pwm-vibrator",
265 .pm
= pm_sleep_ptr(&pwm_vibrator_pm_ops
),
266 .of_match_table
= of_match_ptr(pwm_vibra_dt_match_table
),
269 module_platform_driver(pwm_vibrator_driver
);
271 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
272 MODULE_DESCRIPTION("PWM vibrator driver");
273 MODULE_LICENSE("GPL");
274 MODULE_ALIAS("platform:pwm-vibrator");