1 From d7894c598e2c1e5c1d173ca808694de523f1a06a Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Wed, 14 Jul 2010 00:25:21 -0700
4 Subject: [PATCH] Input: Add pwm beeper driver
6 This patch adds a simple driver which allows to use pwm based beepers (for
7 example piezo elements) as a pcspkr-like device.
9 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
10 Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
12 drivers/input/misc/Kconfig | 11 ++
13 drivers/input/misc/Makefile | 1 +
14 drivers/input/misc/pwm-beeper.c | 199 +++++++++++++++++++++++++++++++++++++++
15 3 files changed, 211 insertions(+), 0 deletions(-)
16 create mode 100644 drivers/input/misc/pwm-beeper.c
18 --- a/drivers/input/misc/Kconfig
19 +++ b/drivers/input/misc/Kconfig
20 @@ -327,6 +327,17 @@ config INPUT_PCF8574
21 To compile this driver as a module, choose M here: the
22 module will be called pcf8574_keypad.
24 +config INPUT_PWM_BEEPER
25 + tristate "PWM beeper support"
28 + Say Y here to get support for PWM based beeper devices.
32 + To compile this driver as a module, choose M here: the module will be
35 config INPUT_GPIO_ROTARY_ENCODER
36 tristate "Rotary encoders connected to GPIO pins"
37 depends on GPIOLIB && GENERIC_GPIO
38 --- a/drivers/input/misc/Makefile
39 +++ b/drivers/input/misc/Makefile
40 @@ -26,6 +26,7 @@ obj-$(CONFIG_INPUT_PCF50633_PMU) += pcf5
41 obj-$(CONFIG_INPUT_PCF8574) += pcf8574_keypad.o
42 obj-$(CONFIG_INPUT_PCSPKR) += pcspkr.o
43 obj-$(CONFIG_INPUT_POWERMATE) += powermate.o
44 +obj-$(CONFIG_INPUT_PWM_BEEPER) += pwm-beeper.o
45 obj-$(CONFIG_INPUT_RB532_BUTTON) += rb532_button.o
46 obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER) += rotary_encoder.o
47 obj-$(CONFIG_INPUT_SGI_BTNS) += sgi_btns.o
49 +++ b/drivers/input/misc/pwm-beeper.c
52 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
55 + * This program is free software; you can redistribute it and/or modify it
56 + * under the terms of the GNU General Public License as published by the
57 + * Free Software Foundation; either version 2 of the License, or (at your
58 + * option) any later version.
60 + * You should have received a copy of the GNU General Public License along
61 + * with this program; if not, write to the Free Software Foundation, Inc.,
62 + * 675 Mass Ave, Cambridge, MA 02139, USA.
66 +#include <linux/input.h>
67 +#include <linux/module.h>
68 +#include <linux/kernel.h>
69 +#include <linux/platform_device.h>
70 +#include <linux/pwm.h>
71 +#include <linux/slab.h>
74 + struct input_dev *input;
75 + struct pwm_device *pwm;
76 + unsigned long period;
79 +#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
81 +static int pwm_beeper_event(struct input_dev *input,
82 + unsigned int type, unsigned int code, int value)
85 + struct pwm_beeper *beeper = input_get_drvdata(input);
86 + unsigned long period;
88 + if (type != EV_SND || value < 0)
93 + value = value ? 1000 : 0;
102 + pwm_config(beeper->pwm, 0, 0);
103 + pwm_disable(beeper->pwm);
105 + period = HZ_TO_NANOSECONDS(value);
106 + ret = pwm_config(beeper->pwm, period / 2, period);
109 + ret = pwm_enable(beeper->pwm);
112 + beeper->period = period;
118 +static int __devinit pwm_beeper_probe(struct platform_device *pdev)
120 + unsigned long pwm_id = (unsigned long)pdev->dev.platform_data;
121 + struct pwm_beeper *beeper;
124 + beeper = kzalloc(sizeof(*beeper), GFP_KERNEL);
128 + beeper->pwm = pwm_request(pwm_id, "pwm beeper");
130 + if (IS_ERR(beeper->pwm)) {
131 + error = PTR_ERR(beeper->pwm);
132 + dev_err(&pdev->dev, "Failed to request pwm device: %d\n", error);
136 + beeper->input = input_allocate_device();
137 + if (!beeper->input) {
138 + dev_err(&pdev->dev, "Failed to allocate input device\n");
142 + beeper->input->dev.parent = &pdev->dev;
144 + beeper->input->name = "pwm-beeper";
145 + beeper->input->phys = "pwm/input0";
146 + beeper->input->id.bustype = BUS_HOST;
147 + beeper->input->id.vendor = 0x001f;
148 + beeper->input->id.product = 0x0001;
149 + beeper->input->id.version = 0x0100;
151 + beeper->input->evbit[0] = BIT(EV_SND);
152 + beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL);
154 + beeper->input->event = pwm_beeper_event;
156 + input_set_drvdata(beeper->input, beeper);
158 + error = input_register_device(beeper->input);
160 + dev_err(&pdev->dev, "Failed to register input device: %d\n", error);
161 + goto err_input_free;
164 + platform_set_drvdata(pdev, beeper);
169 + input_free_device(beeper->input);
171 + pwm_free(beeper->pwm);
178 +static int __devexit pwm_beeper_remove(struct platform_device *pdev)
180 + struct pwm_beeper *beeper = platform_get_drvdata(pdev);
182 + platform_set_drvdata(pdev, NULL);
183 + input_unregister_device(beeper->input);
185 + pwm_disable(beeper->pwm);
186 + pwm_free(beeper->pwm);
194 +static int pwm_beeper_suspend(struct device *dev)
196 + struct pwm_beeper *beeper = dev_get_drvdata(dev);
198 + if (beeper->period)
199 + pwm_disable(beeper->pwm);
204 +static int pwm_beeper_resume(struct device *dev)
206 + struct pwm_beeper *beeper = dev_get_drvdata(dev);
208 + if (beeper->period) {
209 + pwm_config(beeper->pwm, beeper->period / 2, beeper->period);
210 + pwm_enable(beeper->pwm);
216 +static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
217 + pwm_beeper_suspend, pwm_beeper_resume);
219 +#define PWM_BEEPER_PM_OPS (&pwm_beeper_pm_ops)
221 +#define PWM_BEEPER_PM_OPS NULL
224 +static struct platform_driver pwm_beeper_driver = {
225 + .probe = pwm_beeper_probe,
226 + .remove = __devexit_p(pwm_beeper_remove),
228 + .name = "pwm-beeper",
229 + .owner = THIS_MODULE,
230 + .pm = PWM_BEEPER_PM_OPS,
234 +static int __init pwm_beeper_init(void)
236 + return platform_driver_register(&pwm_beeper_driver);
238 +module_init(pwm_beeper_init);
240 +static void __exit pwm_beeper_exit(void)
242 + platform_driver_unregister(&pwm_beeper_driver);
244 +module_exit(pwm_beeper_exit);
246 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
247 +MODULE_DESCRIPTION("PWM beeper driver");
248 +MODULE_LICENSE("GPL");
249 +MODULE_ALIAS("platform:pwm-beeper");