MAINTAINERS: add myself as co-maintainer of the iwlwifi driver
[linux/fpc-iii.git] / drivers / input / misc / pwm-beeper.c
blobf2261ab5470126f0935c3ca9d3830b55699dc378
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>
24 struct pwm_beeper {
25 struct input_dev *input;
26 struct pwm_device *pwm;
27 unsigned long period;
30 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
32 static int pwm_beeper_event(struct input_dev *input,
33 unsigned int type, unsigned int code, int value)
35 int ret = 0;
36 struct pwm_beeper *beeper = input_get_drvdata(input);
37 unsigned long period;
39 if (type != EV_SND || value < 0)
40 return -EINVAL;
42 switch (code) {
43 case SND_BELL:
44 value = value ? 1000 : 0;
45 break;
46 case SND_TONE:
47 break;
48 default:
49 return -EINVAL;
52 if (value == 0) {
53 pwm_disable(beeper->pwm);
54 } else {
55 period = HZ_TO_NANOSECONDS(value);
56 ret = pwm_config(beeper->pwm, period / 2, period);
57 if (ret)
58 return ret;
59 ret = pwm_enable(beeper->pwm);
60 if (ret)
61 return ret;
62 beeper->period = period;
65 return 0;
68 static int pwm_beeper_probe(struct platform_device *pdev)
70 unsigned long pwm_id = (unsigned long)dev_get_platdata(&pdev->dev);
71 struct pwm_beeper *beeper;
72 int error;
74 beeper = kzalloc(sizeof(*beeper), GFP_KERNEL);
75 if (!beeper)
76 return -ENOMEM;
78 beeper->pwm = pwm_get(&pdev->dev, NULL);
79 if (IS_ERR(beeper->pwm)) {
80 dev_dbg(&pdev->dev, "unable to request PWM, trying legacy API\n");
81 beeper->pwm = pwm_request(pwm_id, "pwm beeper");
84 if (IS_ERR(beeper->pwm)) {
85 error = PTR_ERR(beeper->pwm);
86 dev_err(&pdev->dev, "Failed to request pwm device: %d\n", error);
87 goto err_free;
90 beeper->input = input_allocate_device();
91 if (!beeper->input) {
92 dev_err(&pdev->dev, "Failed to allocate input device\n");
93 error = -ENOMEM;
94 goto err_pwm_free;
96 beeper->input->dev.parent = &pdev->dev;
98 beeper->input->name = "pwm-beeper";
99 beeper->input->phys = "pwm/input0";
100 beeper->input->id.bustype = BUS_HOST;
101 beeper->input->id.vendor = 0x001f;
102 beeper->input->id.product = 0x0001;
103 beeper->input->id.version = 0x0100;
105 beeper->input->evbit[0] = BIT(EV_SND);
106 beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL);
108 beeper->input->event = pwm_beeper_event;
110 input_set_drvdata(beeper->input, beeper);
112 error = input_register_device(beeper->input);
113 if (error) {
114 dev_err(&pdev->dev, "Failed to register input device: %d\n", error);
115 goto err_input_free;
118 platform_set_drvdata(pdev, beeper);
120 return 0;
122 err_input_free:
123 input_free_device(beeper->input);
124 err_pwm_free:
125 pwm_free(beeper->pwm);
126 err_free:
127 kfree(beeper);
129 return error;
132 static int pwm_beeper_remove(struct platform_device *pdev)
134 struct pwm_beeper *beeper = platform_get_drvdata(pdev);
136 input_unregister_device(beeper->input);
138 pwm_disable(beeper->pwm);
139 pwm_free(beeper->pwm);
141 kfree(beeper);
143 return 0;
146 static int __maybe_unused pwm_beeper_suspend(struct device *dev)
148 struct pwm_beeper *beeper = dev_get_drvdata(dev);
150 if (beeper->period)
151 pwm_disable(beeper->pwm);
153 return 0;
156 static int __maybe_unused pwm_beeper_resume(struct device *dev)
158 struct pwm_beeper *beeper = dev_get_drvdata(dev);
160 if (beeper->period) {
161 pwm_config(beeper->pwm, beeper->period / 2, beeper->period);
162 pwm_enable(beeper->pwm);
165 return 0;
168 static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
169 pwm_beeper_suspend, pwm_beeper_resume);
171 #ifdef CONFIG_OF
172 static const struct of_device_id pwm_beeper_match[] = {
173 { .compatible = "pwm-beeper", },
174 { },
176 MODULE_DEVICE_TABLE(of, pwm_beeper_match);
177 #endif
179 static struct platform_driver pwm_beeper_driver = {
180 .probe = pwm_beeper_probe,
181 .remove = pwm_beeper_remove,
182 .driver = {
183 .name = "pwm-beeper",
184 .pm = &pwm_beeper_pm_ops,
185 .of_match_table = of_match_ptr(pwm_beeper_match),
188 module_platform_driver(pwm_beeper_driver);
190 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
191 MODULE_DESCRIPTION("PWM beeper driver");
192 MODULE_LICENSE("GPL");
193 MODULE_ALIAS("platform:pwm-beeper");