Loading...
1/*
2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
3 * PWM beeper driver
4 *
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.
9 *
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.
13 *
14 */
15
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
24struct pwm_beeper {
25 struct input_dev *input;
26 struct pwm_device *pwm;
27 unsigned long period;
28};
29
30#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
31
32static int pwm_beeper_event(struct input_dev *input,
33 unsigned int type, unsigned int code, int value)
34{
35 int ret = 0;
36 struct pwm_beeper *beeper = input_get_drvdata(input);
37 unsigned long period;
38
39 if (type != EV_SND || value < 0)
40 return -EINVAL;
41
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;
50 }
51
52 if (value == 0) {
53 pwm_config(beeper->pwm, 0, 0);
54 pwm_disable(beeper->pwm);
55 } else {
56 period = HZ_TO_NANOSECONDS(value);
57 ret = pwm_config(beeper->pwm, period / 2, period);
58 if (ret)
59 return ret;
60 ret = pwm_enable(beeper->pwm);
61 if (ret)
62 return ret;
63 beeper->period = period;
64 }
65
66 return 0;
67}
68
69static int pwm_beeper_probe(struct platform_device *pdev)
70{
71 unsigned long pwm_id = (unsigned long)dev_get_platdata(&pdev->dev);
72 struct pwm_beeper *beeper;
73 int error;
74
75 beeper = kzalloc(sizeof(*beeper), GFP_KERNEL);
76 if (!beeper)
77 return -ENOMEM;
78
79 beeper->pwm = pwm_get(&pdev->dev, NULL);
80 if (IS_ERR(beeper->pwm)) {
81 dev_dbg(&pdev->dev, "unable to request PWM, trying legacy API\n");
82 beeper->pwm = pwm_request(pwm_id, "pwm beeper");
83 }
84
85 if (IS_ERR(beeper->pwm)) {
86 error = PTR_ERR(beeper->pwm);
87 dev_err(&pdev->dev, "Failed to request pwm device: %d\n", error);
88 goto err_free;
89 }
90
91 beeper->input = input_allocate_device();
92 if (!beeper->input) {
93 dev_err(&pdev->dev, "Failed to allocate input device\n");
94 error = -ENOMEM;
95 goto err_pwm_free;
96 }
97 beeper->input->dev.parent = &pdev->dev;
98
99 beeper->input->name = "pwm-beeper";
100 beeper->input->phys = "pwm/input0";
101 beeper->input->id.bustype = BUS_HOST;
102 beeper->input->id.vendor = 0x001f;
103 beeper->input->id.product = 0x0001;
104 beeper->input->id.version = 0x0100;
105
106 beeper->input->evbit[0] = BIT(EV_SND);
107 beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL);
108
109 beeper->input->event = pwm_beeper_event;
110
111 input_set_drvdata(beeper->input, beeper);
112
113 error = input_register_device(beeper->input);
114 if (error) {
115 dev_err(&pdev->dev, "Failed to register input device: %d\n", error);
116 goto err_input_free;
117 }
118
119 platform_set_drvdata(pdev, beeper);
120
121 return 0;
122
123err_input_free:
124 input_free_device(beeper->input);
125err_pwm_free:
126 pwm_free(beeper->pwm);
127err_free:
128 kfree(beeper);
129
130 return error;
131}
132
133static int pwm_beeper_remove(struct platform_device *pdev)
134{
135 struct pwm_beeper *beeper = platform_get_drvdata(pdev);
136
137 input_unregister_device(beeper->input);
138
139 pwm_disable(beeper->pwm);
140 pwm_free(beeper->pwm);
141
142 kfree(beeper);
143
144 return 0;
145}
146
147#ifdef CONFIG_PM_SLEEP
148static int pwm_beeper_suspend(struct device *dev)
149{
150 struct pwm_beeper *beeper = dev_get_drvdata(dev);
151
152 if (beeper->period)
153 pwm_disable(beeper->pwm);
154
155 return 0;
156}
157
158static int pwm_beeper_resume(struct device *dev)
159{
160 struct pwm_beeper *beeper = dev_get_drvdata(dev);
161
162 if (beeper->period) {
163 pwm_config(beeper->pwm, beeper->period / 2, beeper->period);
164 pwm_enable(beeper->pwm);
165 }
166
167 return 0;
168}
169
170static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
171 pwm_beeper_suspend, pwm_beeper_resume);
172
173#define PWM_BEEPER_PM_OPS (&pwm_beeper_pm_ops)
174#else
175#define PWM_BEEPER_PM_OPS NULL
176#endif
177
178#ifdef CONFIG_OF
179static const struct of_device_id pwm_beeper_match[] = {
180 { .compatible = "pwm-beeper", },
181 { },
182};
183#endif
184
185static struct platform_driver pwm_beeper_driver = {
186 .probe = pwm_beeper_probe,
187 .remove = pwm_beeper_remove,
188 .driver = {
189 .name = "pwm-beeper",
190 .owner = THIS_MODULE,
191 .pm = PWM_BEEPER_PM_OPS,
192 .of_match_table = of_match_ptr(pwm_beeper_match),
193 },
194};
195module_platform_driver(pwm_beeper_driver);
196
197MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
198MODULE_DESCRIPTION("PWM beeper driver");
199MODULE_LICENSE("GPL");
200MODULE_ALIAS("platform:pwm-beeper");
1/*
2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
3 * PWM beeper driver
4 *
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.
9 *
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.
13 *
14 */
15
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>
24
25struct pwm_beeper {
26 struct input_dev *input;
27 struct pwm_device *pwm;
28 struct work_struct work;
29 unsigned long period;
30};
31
32#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
33
34static void __pwm_beeper_set(struct pwm_beeper *beeper)
35{
36 unsigned long period = beeper->period;
37
38 if (period) {
39 pwm_config(beeper->pwm, period / 2, period);
40 pwm_enable(beeper->pwm);
41 } else
42 pwm_disable(beeper->pwm);
43}
44
45static void pwm_beeper_work(struct work_struct *work)
46{
47 struct pwm_beeper *beeper =
48 container_of(work, struct pwm_beeper, work);
49
50 __pwm_beeper_set(beeper);
51}
52
53static int pwm_beeper_event(struct input_dev *input,
54 unsigned int type, unsigned int code, int value)
55{
56 struct pwm_beeper *beeper = input_get_drvdata(input);
57
58 if (type != EV_SND || value < 0)
59 return -EINVAL;
60
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;
69 }
70
71 if (value == 0)
72 beeper->period = 0;
73 else
74 beeper->period = HZ_TO_NANOSECONDS(value);
75
76 schedule_work(&beeper->work);
77
78 return 0;
79}
80
81static void pwm_beeper_stop(struct pwm_beeper *beeper)
82{
83 cancel_work_sync(&beeper->work);
84
85 if (beeper->period)
86 pwm_disable(beeper->pwm);
87}
88
89static void pwm_beeper_close(struct input_dev *input)
90{
91 struct pwm_beeper *beeper = input_get_drvdata(input);
92
93 pwm_beeper_stop(beeper);
94}
95
96static int pwm_beeper_probe(struct platform_device *pdev)
97{
98 unsigned long pwm_id = (unsigned long)dev_get_platdata(&pdev->dev);
99 struct pwm_beeper *beeper;
100 int error;
101
102 beeper = kzalloc(sizeof(*beeper), GFP_KERNEL);
103 if (!beeper)
104 return -ENOMEM;
105
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");
110 }
111
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;
116 }
117
118 /*
119 * FIXME: pwm_apply_args() should be removed when switching to
120 * the atomic PWM API.
121 */
122 pwm_apply_args(beeper->pwm);
123
124 INIT_WORK(&beeper->work, pwm_beeper_work);
125
126 beeper->input = input_allocate_device();
127 if (!beeper->input) {
128 dev_err(&pdev->dev, "Failed to allocate input device\n");
129 error = -ENOMEM;
130 goto err_pwm_free;
131 }
132 beeper->input->dev.parent = &pdev->dev;
133
134 beeper->input->name = "pwm-beeper";
135 beeper->input->phys = "pwm/input0";
136 beeper->input->id.bustype = BUS_HOST;
137 beeper->input->id.vendor = 0x001f;
138 beeper->input->id.product = 0x0001;
139 beeper->input->id.version = 0x0100;
140
141 beeper->input->evbit[0] = BIT(EV_SND);
142 beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL);
143
144 beeper->input->event = pwm_beeper_event;
145 beeper->input->close = pwm_beeper_close;
146
147 input_set_drvdata(beeper->input, beeper);
148
149 error = input_register_device(beeper->input);
150 if (error) {
151 dev_err(&pdev->dev, "Failed to register input device: %d\n", error);
152 goto err_input_free;
153 }
154
155 platform_set_drvdata(pdev, beeper);
156
157 return 0;
158
159err_input_free:
160 input_free_device(beeper->input);
161err_pwm_free:
162 pwm_free(beeper->pwm);
163err_free:
164 kfree(beeper);
165
166 return error;
167}
168
169static int pwm_beeper_remove(struct platform_device *pdev)
170{
171 struct pwm_beeper *beeper = platform_get_drvdata(pdev);
172
173 input_unregister_device(beeper->input);
174
175 pwm_free(beeper->pwm);
176
177 kfree(beeper);
178
179 return 0;
180}
181
182static int __maybe_unused pwm_beeper_suspend(struct device *dev)
183{
184 struct pwm_beeper *beeper = dev_get_drvdata(dev);
185
186 pwm_beeper_stop(beeper);
187
188 return 0;
189}
190
191static int __maybe_unused pwm_beeper_resume(struct device *dev)
192{
193 struct pwm_beeper *beeper = dev_get_drvdata(dev);
194
195 if (beeper->period)
196 __pwm_beeper_set(beeper);
197
198 return 0;
199}
200
201static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
202 pwm_beeper_suspend, pwm_beeper_resume);
203
204#ifdef CONFIG_OF
205static const struct of_device_id pwm_beeper_match[] = {
206 { .compatible = "pwm-beeper", },
207 { },
208};
209MODULE_DEVICE_TABLE(of, pwm_beeper_match);
210#endif
211
212static struct platform_driver pwm_beeper_driver = {
213 .probe = pwm_beeper_probe,
214 .remove = pwm_beeper_remove,
215 .driver = {
216 .name = "pwm-beeper",
217 .pm = &pwm_beeper_pm_ops,
218 .of_match_table = of_match_ptr(pwm_beeper_match),
219 },
220};
221module_platform_driver(pwm_beeper_driver);
222
223MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
224MODULE_DESCRIPTION("PWM beeper driver");
225MODULE_LICENSE("GPL");
226MODULE_ALIAS("platform:pwm-beeper");