1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
7 #include <linux/input.h>
8 #include <linux/regulator/consumer.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.h>
14 #include <linux/pwm.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
19 struct input_dev
*input
;
20 struct pwm_device
*pwm
;
21 struct regulator
*amplifier
;
22 struct work_struct work
;
24 unsigned int bell_frequency
;
29 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
31 static int pwm_beeper_on(struct pwm_beeper
*beeper
, unsigned long period
)
33 struct pwm_state state
;
36 pwm_get_state(beeper
->pwm
, &state
);
39 state
.period
= period
;
40 pwm_set_relative_duty_cycle(&state
, 50, 100);
42 error
= pwm_apply_might_sleep(beeper
->pwm
, &state
);
46 if (!beeper
->amplifier_on
) {
47 error
= regulator_enable(beeper
->amplifier
);
49 pwm_disable(beeper
->pwm
);
53 beeper
->amplifier_on
= true;
59 static void pwm_beeper_off(struct pwm_beeper
*beeper
)
61 if (beeper
->amplifier_on
) {
62 regulator_disable(beeper
->amplifier
);
63 beeper
->amplifier_on
= false;
66 pwm_disable(beeper
->pwm
);
69 static void pwm_beeper_work(struct work_struct
*work
)
71 struct pwm_beeper
*beeper
= container_of(work
, struct pwm_beeper
, work
);
72 unsigned long period
= READ_ONCE(beeper
->period
);
75 pwm_beeper_on(beeper
, period
);
77 pwm_beeper_off(beeper
);
80 static int pwm_beeper_event(struct input_dev
*input
,
81 unsigned int type
, unsigned int code
, int value
)
83 struct pwm_beeper
*beeper
= input_get_drvdata(input
);
85 if (type
!= EV_SND
|| value
< 0)
90 value
= value
? beeper
->bell_frequency
: 0;
101 beeper
->period
= HZ_TO_NANOSECONDS(value
);
103 if (!beeper
->suspended
)
104 schedule_work(&beeper
->work
);
109 static void pwm_beeper_stop(struct pwm_beeper
*beeper
)
111 cancel_work_sync(&beeper
->work
);
112 pwm_beeper_off(beeper
);
115 static void pwm_beeper_close(struct input_dev
*input
)
117 struct pwm_beeper
*beeper
= input_get_drvdata(input
);
119 pwm_beeper_stop(beeper
);
122 static int pwm_beeper_probe(struct platform_device
*pdev
)
124 struct device
*dev
= &pdev
->dev
;
125 struct pwm_beeper
*beeper
;
126 struct pwm_state state
;
130 beeper
= devm_kzalloc(dev
, sizeof(*beeper
), GFP_KERNEL
);
134 beeper
->pwm
= devm_pwm_get(dev
, NULL
);
135 if (IS_ERR(beeper
->pwm
))
136 return dev_err_probe(dev
, PTR_ERR(beeper
->pwm
), "Failed to request PWM device\n");
138 /* Sync up PWM state and ensure it is off. */
139 pwm_init_state(beeper
->pwm
, &state
);
140 state
.enabled
= false;
141 error
= pwm_apply_might_sleep(beeper
->pwm
, &state
);
143 dev_err(dev
, "failed to apply initial PWM state: %d\n",
148 beeper
->amplifier
= devm_regulator_get(dev
, "amp");
149 if (IS_ERR(beeper
->amplifier
))
150 return dev_err_probe(dev
, PTR_ERR(beeper
->amplifier
),
151 "Failed to get 'amp' regulator\n");
153 INIT_WORK(&beeper
->work
, pwm_beeper_work
);
155 error
= device_property_read_u32(dev
, "beeper-hz", &bell_frequency
);
157 bell_frequency
= 1000;
159 "failed to parse 'beeper-hz' property, using default: %uHz\n",
163 beeper
->bell_frequency
= bell_frequency
;
165 beeper
->input
= devm_input_allocate_device(dev
);
166 if (!beeper
->input
) {
167 dev_err(dev
, "Failed to allocate input device\n");
171 beeper
->input
->name
= "pwm-beeper";
172 beeper
->input
->phys
= "pwm/input0";
173 beeper
->input
->id
.bustype
= BUS_HOST
;
174 beeper
->input
->id
.vendor
= 0x001f;
175 beeper
->input
->id
.product
= 0x0001;
176 beeper
->input
->id
.version
= 0x0100;
178 input_set_capability(beeper
->input
, EV_SND
, SND_TONE
);
179 input_set_capability(beeper
->input
, EV_SND
, SND_BELL
);
181 beeper
->input
->event
= pwm_beeper_event
;
182 beeper
->input
->close
= pwm_beeper_close
;
184 input_set_drvdata(beeper
->input
, beeper
);
186 error
= input_register_device(beeper
->input
);
188 dev_err(dev
, "Failed to register input device: %d\n", error
);
192 platform_set_drvdata(pdev
, beeper
);
197 static int pwm_beeper_suspend(struct device
*dev
)
199 struct pwm_beeper
*beeper
= dev_get_drvdata(dev
);
202 * Spinlock is taken here is not to protect write to
203 * beeper->suspended, but to ensure that pwm_beeper_event
204 * does not re-submit work once flag is set.
206 scoped_guard(spinlock_irq
, &beeper
->input
->event_lock
) {
207 beeper
->suspended
= true;
210 pwm_beeper_stop(beeper
);
215 static int pwm_beeper_resume(struct device
*dev
)
217 struct pwm_beeper
*beeper
= dev_get_drvdata(dev
);
219 scoped_guard(spinlock_irq
, &beeper
->input
->event_lock
) {
220 beeper
->suspended
= false;
223 /* Let worker figure out if we should resume beeping */
224 schedule_work(&beeper
->work
);
229 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops
,
230 pwm_beeper_suspend
, pwm_beeper_resume
);
233 static const struct of_device_id pwm_beeper_match
[] = {
234 { .compatible
= "pwm-beeper", },
237 MODULE_DEVICE_TABLE(of
, pwm_beeper_match
);
240 static struct platform_driver pwm_beeper_driver
= {
241 .probe
= pwm_beeper_probe
,
243 .name
= "pwm-beeper",
244 .pm
= pm_sleep_ptr(&pwm_beeper_pm_ops
),
245 .of_match_table
= of_match_ptr(pwm_beeper_match
),
248 module_platform_driver(pwm_beeper_driver
);
250 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
251 MODULE_DESCRIPTION("PWM beeper driver");
252 MODULE_LICENSE("GPL");
253 MODULE_ALIAS("platform:pwm-beeper");