[ar71xx] tune rtl8366s default config, make vlan 1 cpu port tagged and use eth0.1...
[openwrt.git] / target / linux / xburst / patches-2.6.35 / 107-pwm-beeper.patch
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
5
6 This patch adds a simple driver which allows to use pwm based beepers (for
7 example piezo elements) as a pcspkr-like device.
8
9 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
10 Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
11 ---
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
17
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.
23
24 +config INPUT_PWM_BEEPER
25 + tristate "PWM beeper support"
26 + depends on HAVE_PWM
27 + help
28 + Say Y here to get support for PWM based beeper devices.
29 +
30 + If unsure, say N.
31 +
32 + To compile this driver as a module, choose M here: the module will be
33 + called pwm-beeper.
34 +
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
48 --- /dev/null
49 +++ b/drivers/input/misc/pwm-beeper.c
50 @@ -0,0 +1,199 @@
51 +/*
52 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
53 + * PWM beeper driver
54 + *
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.
59 + *
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.
63 + *
64 + */
65 +
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>
72 +
73 +struct pwm_beeper {
74 + struct input_dev *input;
75 + struct pwm_device *pwm;
76 + unsigned long period;
77 +};
78 +
79 +#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
80 +
81 +static int pwm_beeper_event(struct input_dev *input,
82 + unsigned int type, unsigned int code, int value)
83 +{
84 + int ret = 0;
85 + struct pwm_beeper *beeper = input_get_drvdata(input);
86 + unsigned long period;
87 +
88 + if (type != EV_SND || value < 0)
89 + return -EINVAL;
90 +
91 + switch (code) {
92 + case SND_BELL:
93 + value = value ? 1000 : 0;
94 + break;
95 + case SND_TONE:
96 + break;
97 + default:
98 + return -EINVAL;
99 + }
100 +
101 + if (value == 0) {
102 + pwm_config(beeper->pwm, 0, 0);
103 + pwm_disable(beeper->pwm);
104 + } else {
105 + period = HZ_TO_NANOSECONDS(value);
106 + ret = pwm_config(beeper->pwm, period / 2, period);
107 + if (ret)
108 + return ret;
109 + ret = pwm_enable(beeper->pwm);
110 + if (ret)
111 + return ret;
112 + beeper->period = period;
113 + }
114 +
115 + return 0;
116 +}
117 +
118 +static int __devinit pwm_beeper_probe(struct platform_device *pdev)
119 +{
120 + unsigned long pwm_id = (unsigned long)pdev->dev.platform_data;
121 + struct pwm_beeper *beeper;
122 + int error;
123 +
124 + beeper = kzalloc(sizeof(*beeper), GFP_KERNEL);
125 + if (!beeper)
126 + return -ENOMEM;
127 +
128 + beeper->pwm = pwm_request(pwm_id, "pwm beeper");
129 +
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);
133 + goto err_free;
134 + }
135 +
136 + beeper->input = input_allocate_device();
137 + if (!beeper->input) {
138 + dev_err(&pdev->dev, "Failed to allocate input device\n");
139 + error = -ENOMEM;
140 + goto err_pwm_free;
141 + }
142 + beeper->input->dev.parent = &pdev->dev;
143 +
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;
150 +
151 + beeper->input->evbit[0] = BIT(EV_SND);
152 + beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL);
153 +
154 + beeper->input->event = pwm_beeper_event;
155 +
156 + input_set_drvdata(beeper->input, beeper);
157 +
158 + error = input_register_device(beeper->input);
159 + if (error) {
160 + dev_err(&pdev->dev, "Failed to register input device: %d\n", error);
161 + goto err_input_free;
162 + }
163 +
164 + platform_set_drvdata(pdev, beeper);
165 +
166 + return 0;
167 +
168 +err_input_free:
169 + input_free_device(beeper->input);
170 +err_pwm_free:
171 + pwm_free(beeper->pwm);
172 +err_free:
173 + kfree(beeper);
174 +
175 + return error;
176 +}
177 +
178 +static int __devexit pwm_beeper_remove(struct platform_device *pdev)
179 +{
180 + struct pwm_beeper *beeper = platform_get_drvdata(pdev);
181 +
182 + platform_set_drvdata(pdev, NULL);
183 + input_unregister_device(beeper->input);
184 +
185 + pwm_disable(beeper->pwm);
186 + pwm_free(beeper->pwm);
187 +
188 + kfree(beeper);
189 +
190 + return 0;
191 +}
192 +
193 +#ifdef CONFIG_PM
194 +static int pwm_beeper_suspend(struct device *dev)
195 +{
196 + struct pwm_beeper *beeper = dev_get_drvdata(dev);
197 +
198 + if (beeper->period)
199 + pwm_disable(beeper->pwm);
200 +
201 + return 0;
202 +}
203 +
204 +static int pwm_beeper_resume(struct device *dev)
205 +{
206 + struct pwm_beeper *beeper = dev_get_drvdata(dev);
207 +
208 + if (beeper->period) {
209 + pwm_config(beeper->pwm, beeper->period / 2, beeper->period);
210 + pwm_enable(beeper->pwm);
211 + }
212 +
213 + return 0;
214 +}
215 +
216 +static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
217 + pwm_beeper_suspend, pwm_beeper_resume);
218 +
219 +#define PWM_BEEPER_PM_OPS (&pwm_beeper_pm_ops)
220 +#else
221 +#define PWM_BEEPER_PM_OPS NULL
222 +#endif
223 +
224 +static struct platform_driver pwm_beeper_driver = {
225 + .probe = pwm_beeper_probe,
226 + .remove = __devexit_p(pwm_beeper_remove),
227 + .driver = {
228 + .name = "pwm-beeper",
229 + .owner = THIS_MODULE,
230 + .pm = PWM_BEEPER_PM_OPS,
231 + },
232 +};
233 +
234 +static int __init pwm_beeper_init(void)
235 +{
236 + return platform_driver_register(&pwm_beeper_driver);
237 +}
238 +module_init(pwm_beeper_init);
239 +
240 +static void __exit pwm_beeper_exit(void)
241 +{
242 + platform_driver_unregister(&pwm_beeper_driver);
243 +}
244 +module_exit(pwm_beeper_exit);
245 +
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");
This page took 0.06057 seconds and 5 git commands to generate.