dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / input / misc / pwm-beeper.c
blob18663d4edae5be089b112f5a0c3f830ae3ca432c
1 /*
2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
3 * PWM beeper driver
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/module.h>
18 #include <linux/kernel.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/pwm.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
25 struct pwm_beeper {
26 struct input_dev *input;
27 struct pwm_device *pwm;
28 struct work_struct work;
29 unsigned long period;
32 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
34 static void __pwm_beeper_set(struct pwm_beeper *beeper)
36 unsigned long period = beeper->period;
38 if (period) {
39 pwm_config(beeper->pwm, period / 2, period);
40 pwm_enable(beeper->pwm);
41 } else
42 pwm_disable(beeper->pwm);
45 static void pwm_beeper_work(struct work_struct *work)
47 struct pwm_beeper *beeper =
48 container_of(work, struct pwm_beeper, work);
50 __pwm_beeper_set(beeper);
53 static int pwm_beeper_event(struct input_dev *input,
54 unsigned int type, unsigned int code, int value)
56 struct pwm_beeper *beeper = input_get_drvdata(input);
58 if (type != EV_SND || value < 0)
59 return -EINVAL;
61 switch (code) {
62 case SND_BELL:
63 value = value ? 1000 : 0;
64 break;
65 case SND_TONE:
66 break;
67 default:
68 return -EINVAL;
71 if (value == 0)
72 beeper->period = 0;
73 else
74 beeper->period = HZ_TO_NANOSECONDS(value);
76 schedule_work(&beeper->work);
78 return 0;
81 static void pwm_beeper_stop(struct pwm_beeper *beeper)
83 cancel_work_sync(&beeper->work);
85 if (beeper->period)
86 pwm_disable(beeper->pwm);
89 static void pwm_beeper_close(struct input_dev *input)
91 struct pwm_beeper *beeper = input_get_drvdata(input);
93 pwm_beeper_stop(beeper);
96 static int pwm_beeper_probe(struct platform_device *pdev)
98 unsigned long pwm_id = (unsigned long)dev_get_platdata(&pdev->dev);
99 struct pwm_beeper *beeper;
100 int error;
102 beeper = kzalloc(sizeof(*beeper), GFP_KERNEL);
103 if (!beeper)
104 return -ENOMEM;
106 beeper->pwm = pwm_get(&pdev->dev, NULL);
107 if (IS_ERR(beeper->pwm)) {
108 dev_dbg(&pdev->dev, "unable to request PWM, trying legacy API\n");
109 beeper->pwm = pwm_request(pwm_id, "pwm beeper");
112 if (IS_ERR(beeper->pwm)) {
113 error = PTR_ERR(beeper->pwm);
114 dev_err(&pdev->dev, "Failed to request pwm device: %d\n", error);
115 goto err_free;
118 INIT_WORK(&beeper->work, pwm_beeper_work);
120 beeper->input = input_allocate_device();
121 if (!beeper->input) {
122 dev_err(&pdev->dev, "Failed to allocate input device\n");
123 error = -ENOMEM;
124 goto err_pwm_free;
126 beeper->input->dev.parent = &pdev->dev;
128 beeper->input->name = "pwm-beeper";
129 beeper->input->phys = "pwm/input0";
130 beeper->input->id.bustype = BUS_HOST;
131 beeper->input->id.vendor = 0x001f;
132 beeper->input->id.product = 0x0001;
133 beeper->input->id.version = 0x0100;
135 beeper->input->evbit[0] = BIT(EV_SND);
136 beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL);
138 beeper->input->event = pwm_beeper_event;
139 beeper->input->close = pwm_beeper_close;
141 input_set_drvdata(beeper->input, beeper);
143 error = input_register_device(beeper->input);
144 if (error) {
145 dev_err(&pdev->dev, "Failed to register input device: %d\n", error);
146 goto err_input_free;
149 platform_set_drvdata(pdev, beeper);
151 return 0;
153 err_input_free:
154 input_free_device(beeper->input);
155 err_pwm_free:
156 pwm_free(beeper->pwm);
157 err_free:
158 kfree(beeper);
160 return error;
163 static int pwm_beeper_remove(struct platform_device *pdev)
165 struct pwm_beeper *beeper = platform_get_drvdata(pdev);
167 input_unregister_device(beeper->input);
169 pwm_free(beeper->pwm);
171 kfree(beeper);
173 return 0;
176 static int __maybe_unused pwm_beeper_suspend(struct device *dev)
178 struct pwm_beeper *beeper = dev_get_drvdata(dev);
180 pwm_beeper_stop(beeper);
182 return 0;
185 static int __maybe_unused pwm_beeper_resume(struct device *dev)
187 struct pwm_beeper *beeper = dev_get_drvdata(dev);
189 if (beeper->period)
190 __pwm_beeper_set(beeper);
192 return 0;
195 static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
196 pwm_beeper_suspend, pwm_beeper_resume);
198 #ifdef CONFIG_OF
199 static const struct of_device_id pwm_beeper_match[] = {
200 { .compatible = "pwm-beeper", },
201 { },
203 MODULE_DEVICE_TABLE(of, pwm_beeper_match);
204 #endif
206 static struct platform_driver pwm_beeper_driver = {
207 .probe = pwm_beeper_probe,
208 .remove = pwm_beeper_remove,
209 .driver = {
210 .name = "pwm-beeper",
211 .pm = &pwm_beeper_pm_ops,
212 .of_match_table = of_match_ptr(pwm_beeper_match),
215 module_platform_driver(pwm_beeper_driver);
217 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
218 MODULE_DESCRIPTION("PWM beeper driver");
219 MODULE_LICENSE("GPL");
220 MODULE_ALIAS("platform:pwm-beeper");