2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
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/regulator/consumer.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
21 #include <linux/platform_device.h>
22 #include <linux/property.h>
23 #include <linux/pwm.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
28 struct input_dev
*input
;
29 struct pwm_device
*pwm
;
30 struct regulator
*amplifier
;
31 struct work_struct work
;
33 unsigned int bell_frequency
;
38 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
40 static int pwm_beeper_on(struct pwm_beeper
*beeper
, unsigned long period
)
42 struct pwm_state state
;
45 pwm_get_state(beeper
->pwm
, &state
);
48 state
.period
= period
;
49 pwm_set_relative_duty_cycle(&state
, 50, 100);
51 error
= pwm_apply_state(beeper
->pwm
, &state
);
55 if (!beeper
->amplifier_on
) {
56 error
= regulator_enable(beeper
->amplifier
);
58 pwm_disable(beeper
->pwm
);
62 beeper
->amplifier_on
= true;
68 static void pwm_beeper_off(struct pwm_beeper
*beeper
)
70 if (beeper
->amplifier_on
) {
71 regulator_disable(beeper
->amplifier
);
72 beeper
->amplifier_on
= false;
75 pwm_disable(beeper
->pwm
);
78 static void pwm_beeper_work(struct work_struct
*work
)
80 struct pwm_beeper
*beeper
= container_of(work
, struct pwm_beeper
, work
);
81 unsigned long period
= READ_ONCE(beeper
->period
);
84 pwm_beeper_on(beeper
, period
);
86 pwm_beeper_off(beeper
);
89 static int pwm_beeper_event(struct input_dev
*input
,
90 unsigned int type
, unsigned int code
, int value
)
92 struct pwm_beeper
*beeper
= input_get_drvdata(input
);
94 if (type
!= EV_SND
|| value
< 0)
99 value
= value
? beeper
->bell_frequency
: 0;
110 beeper
->period
= HZ_TO_NANOSECONDS(value
);
112 if (!beeper
->suspended
)
113 schedule_work(&beeper
->work
);
118 static void pwm_beeper_stop(struct pwm_beeper
*beeper
)
120 cancel_work_sync(&beeper
->work
);
121 pwm_beeper_off(beeper
);
124 static void pwm_beeper_close(struct input_dev
*input
)
126 struct pwm_beeper
*beeper
= input_get_drvdata(input
);
128 pwm_beeper_stop(beeper
);
131 static int pwm_beeper_probe(struct platform_device
*pdev
)
133 struct device
*dev
= &pdev
->dev
;
134 struct pwm_beeper
*beeper
;
135 struct pwm_state state
;
139 beeper
= devm_kzalloc(dev
, sizeof(*beeper
), GFP_KERNEL
);
143 beeper
->pwm
= devm_pwm_get(dev
, NULL
);
144 if (IS_ERR(beeper
->pwm
)) {
145 error
= PTR_ERR(beeper
->pwm
);
146 if (error
!= -EPROBE_DEFER
)
147 dev_err(dev
, "Failed to request PWM device: %d\n",
152 /* Sync up PWM state and ensure it is off. */
153 pwm_init_state(beeper
->pwm
, &state
);
154 state
.enabled
= false;
155 error
= pwm_apply_state(beeper
->pwm
, &state
);
157 dev_err(dev
, "failed to apply initial PWM state: %d\n",
162 beeper
->amplifier
= devm_regulator_get(dev
, "amp");
163 if (IS_ERR(beeper
->amplifier
)) {
164 error
= PTR_ERR(beeper
->amplifier
);
165 if (error
!= -EPROBE_DEFER
)
166 dev_err(dev
, "Failed to get 'amp' regulator: %d\n",
171 INIT_WORK(&beeper
->work
, pwm_beeper_work
);
173 error
= device_property_read_u32(dev
, "beeper-hz", &bell_frequency
);
175 bell_frequency
= 1000;
177 "failed to parse 'beeper-hz' property, using default: %uHz\n",
181 beeper
->bell_frequency
= bell_frequency
;
183 beeper
->input
= devm_input_allocate_device(dev
);
184 if (!beeper
->input
) {
185 dev_err(dev
, "Failed to allocate input device\n");
189 beeper
->input
->name
= "pwm-beeper";
190 beeper
->input
->phys
= "pwm/input0";
191 beeper
->input
->id
.bustype
= BUS_HOST
;
192 beeper
->input
->id
.vendor
= 0x001f;
193 beeper
->input
->id
.product
= 0x0001;
194 beeper
->input
->id
.version
= 0x0100;
196 input_set_capability(beeper
->input
, EV_SND
, SND_TONE
);
197 input_set_capability(beeper
->input
, EV_SND
, SND_BELL
);
199 beeper
->input
->event
= pwm_beeper_event
;
200 beeper
->input
->close
= pwm_beeper_close
;
202 input_set_drvdata(beeper
->input
, beeper
);
204 error
= input_register_device(beeper
->input
);
206 dev_err(dev
, "Failed to register input device: %d\n", error
);
210 platform_set_drvdata(pdev
, beeper
);
215 static int __maybe_unused
pwm_beeper_suspend(struct device
*dev
)
217 struct pwm_beeper
*beeper
= dev_get_drvdata(dev
);
220 * Spinlock is taken here is not to protect write to
221 * beeper->suspended, but to ensure that pwm_beeper_event
222 * does not re-submit work once flag is set.
224 spin_lock_irq(&beeper
->input
->event_lock
);
225 beeper
->suspended
= true;
226 spin_unlock_irq(&beeper
->input
->event_lock
);
228 pwm_beeper_stop(beeper
);
233 static int __maybe_unused
pwm_beeper_resume(struct device
*dev
)
235 struct pwm_beeper
*beeper
= dev_get_drvdata(dev
);
237 spin_lock_irq(&beeper
->input
->event_lock
);
238 beeper
->suspended
= false;
239 spin_unlock_irq(&beeper
->input
->event_lock
);
241 /* Let worker figure out if we should resume beeping */
242 schedule_work(&beeper
->work
);
247 static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops
,
248 pwm_beeper_suspend
, pwm_beeper_resume
);
251 static const struct of_device_id pwm_beeper_match
[] = {
252 { .compatible
= "pwm-beeper", },
255 MODULE_DEVICE_TABLE(of
, pwm_beeper_match
);
258 static struct platform_driver pwm_beeper_driver
= {
259 .probe
= pwm_beeper_probe
,
261 .name
= "pwm-beeper",
262 .pm
= &pwm_beeper_pm_ops
,
263 .of_match_table
= of_match_ptr(pwm_beeper_match
),
266 module_platform_driver(pwm_beeper_driver
);
268 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
269 MODULE_DESCRIPTION("PWM beeper driver");
270 MODULE_LICENSE("GPL");
271 MODULE_ALIAS("platform:pwm-beeper");