2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 #include <linux/input.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
21 #include <linux/platform_device.h>
22 #include <linux/pwm.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
27 struct input_dev *input;
28 struct pwm_device *pwm;
29 struct regulator *amplifier;
30 struct work_struct work;
36 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
38 static int pwm_beeper_on(struct pwm_beeper *beeper, unsigned long period)
40 struct pwm_state state;
43 pwm_get_state(beeper->pwm, &state);
46 state.period = period;
47 pwm_set_relative_duty_cycle(&state, 50, 100);
49 error = pwm_apply_state(beeper->pwm, &state);
53 if (!beeper->amplifier_on) {
54 error = regulator_enable(beeper->amplifier);
56 pwm_disable(beeper->pwm);
60 beeper->amplifier_on = true;
66 static void pwm_beeper_off(struct pwm_beeper *beeper)
68 if (beeper->amplifier_on) {
69 regulator_disable(beeper->amplifier);
70 beeper->amplifier_on = false;
73 pwm_disable(beeper->pwm);
76 static void pwm_beeper_work(struct work_struct *work)
78 struct pwm_beeper *beeper = container_of(work, struct pwm_beeper, work);
79 unsigned long period = READ_ONCE(beeper->period);
82 pwm_beeper_on(beeper, period);
84 pwm_beeper_off(beeper);
87 static int pwm_beeper_event(struct input_dev *input,
88 unsigned int type, unsigned int code, int value)
90 struct pwm_beeper *beeper = input_get_drvdata(input);
92 if (type != EV_SND || value < 0)
97 value = value ? 1000 : 0;
108 beeper->period = HZ_TO_NANOSECONDS(value);
110 if (!beeper->suspended)
111 schedule_work(&beeper->work);
116 static void pwm_beeper_stop(struct pwm_beeper *beeper)
118 cancel_work_sync(&beeper->work);
119 pwm_beeper_off(beeper);
122 static void pwm_beeper_close(struct input_dev *input)
124 struct pwm_beeper *beeper = input_get_drvdata(input);
126 pwm_beeper_stop(beeper);
129 static int pwm_beeper_probe(struct platform_device *pdev)
131 struct device *dev = &pdev->dev;
132 struct pwm_beeper *beeper;
133 struct pwm_state state;
136 beeper = devm_kzalloc(dev, sizeof(*beeper), GFP_KERNEL);
140 beeper->pwm = devm_pwm_get(dev, NULL);
141 if (IS_ERR(beeper->pwm)) {
142 error = PTR_ERR(beeper->pwm);
143 if (error != -EPROBE_DEFER)
144 dev_err(dev, "Failed to request PWM device: %d\n",
149 /* Sync up PWM state and ensure it is off. */
150 pwm_init_state(beeper->pwm, &state);
151 state.enabled = false;
152 error = pwm_apply_state(beeper->pwm, &state);
154 dev_err(dev, "failed to apply initial PWM state: %d\n",
159 beeper->amplifier = devm_regulator_get(dev, "amp");
160 if (IS_ERR(beeper->amplifier)) {
161 error = PTR_ERR(beeper->amplifier);
162 if (error != -EPROBE_DEFER)
163 dev_err(dev, "Failed to get 'amp' regulator: %d\n",
168 INIT_WORK(&beeper->work, pwm_beeper_work);
170 beeper->input = devm_input_allocate_device(dev);
171 if (!beeper->input) {
172 dev_err(dev, "Failed to allocate input device\n");
176 beeper->input->name = "pwm-beeper";
177 beeper->input->phys = "pwm/input0";
178 beeper->input->id.bustype = BUS_HOST;
179 beeper->input->id.vendor = 0x001f;
180 beeper->input->id.product = 0x0001;
181 beeper->input->id.version = 0x0100;
183 input_set_capability(beeper->input, EV_SND, SND_TONE);
184 input_set_capability(beeper->input, EV_SND, SND_BELL);
186 beeper->input->event = pwm_beeper_event;
187 beeper->input->close = pwm_beeper_close;
189 input_set_drvdata(beeper->input, beeper);
191 error = input_register_device(beeper->input);
193 dev_err(dev, "Failed to register input device: %d\n", error);
197 platform_set_drvdata(pdev, beeper);
202 static int __maybe_unused pwm_beeper_suspend(struct device *dev)
204 struct pwm_beeper *beeper = dev_get_drvdata(dev);
207 * Spinlock is taken here is not to protect write to
208 * beeper->suspended, but to ensure that pwm_beeper_event
209 * does not re-submit work once flag is set.
211 spin_lock_irq(&beeper->input->event_lock);
212 beeper->suspended = true;
213 spin_unlock_irq(&beeper->input->event_lock);
215 pwm_beeper_stop(beeper);
220 static int __maybe_unused pwm_beeper_resume(struct device *dev)
222 struct pwm_beeper *beeper = dev_get_drvdata(dev);
224 spin_lock_irq(&beeper->input->event_lock);
225 beeper->suspended = false;
226 spin_unlock_irq(&beeper->input->event_lock);
228 /* Let worker figure out if we should resume beeping */
229 schedule_work(&beeper->work);
234 static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
235 pwm_beeper_suspend, pwm_beeper_resume);
238 static const struct of_device_id pwm_beeper_match[] = {
239 { .compatible = "pwm-beeper", },
242 MODULE_DEVICE_TABLE(of, pwm_beeper_match);
245 static struct platform_driver pwm_beeper_driver = {
246 .probe = pwm_beeper_probe,
248 .name = "pwm-beeper",
249 .pm = &pwm_beeper_pm_ops,
250 .of_match_table = of_match_ptr(pwm_beeper_match),
253 module_platform_driver(pwm_beeper_driver);
255 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
256 MODULE_DESCRIPTION("PWM beeper driver");
257 MODULE_LICENSE("GPL");
258 MODULE_ALIAS("platform:pwm-beeper");