blob: e53801dbd5603f4fd1274cb608784383adfe9405 [file] [log] [blame]
Lars-Peter Clausene22739d2010-07-14 00:25:21 -07001/*
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>
David Lechner9e549242017-01-15 17:09:43 -080017#include <linux/regulator/consumer.h>
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070018#include <linux/module.h>
19#include <linux/kernel.h>
Sachin Kamat9dbe4c32013-10-06 00:56:09 -070020#include <linux/of.h>
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070021#include <linux/platform_device.h>
22#include <linux/pwm.h>
23#include <linux/slab.h>
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070024#include <linux/workqueue.h>
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070025
26struct pwm_beeper {
27 struct input_dev *input;
28 struct pwm_device *pwm;
David Lechner9e549242017-01-15 17:09:43 -080029 struct regulator *amplifier;
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070030 struct work_struct work;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070031 unsigned long period;
Dmitry Torokhove9728f02017-01-19 11:13:37 -080032 bool suspended;
David Lechner9e549242017-01-15 17:09:43 -080033 bool amplifier_on;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070034};
35
36#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
37
David Lechner9e549242017-01-15 17:09:43 -080038static int pwm_beeper_on(struct pwm_beeper *beeper, unsigned long period)
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070039{
Dmitry Torokhov2de8b412017-01-19 13:52:49 -080040 struct pwm_state state;
David Lechner9e549242017-01-15 17:09:43 -080041 int error;
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070042
Dmitry Torokhov2de8b412017-01-19 13:52:49 -080043 pwm_get_state(beeper->pwm, &state);
David Lechner9e549242017-01-15 17:09:43 -080044
Dmitry Torokhov2de8b412017-01-19 13:52:49 -080045 state.enabled = true;
46 state.period = period;
47 pwm_set_relative_duty_cycle(&state, 50, 100);
48
49 error = pwm_apply_state(beeper->pwm, &state);
David Lechner9e549242017-01-15 17:09:43 -080050 if (error)
51 return error;
52
53 if (!beeper->amplifier_on) {
54 error = regulator_enable(beeper->amplifier);
55 if (error) {
56 pwm_disable(beeper->pwm);
57 return error;
58 }
59
60 beeper->amplifier_on = true;
61 }
62
63 return 0;
64}
65
66static void pwm_beeper_off(struct pwm_beeper *beeper)
67{
68 if (beeper->amplifier_on) {
69 regulator_disable(beeper->amplifier);
70 beeper->amplifier_on = false;
71 }
72
73 pwm_disable(beeper->pwm);
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070074}
75
76static void pwm_beeper_work(struct work_struct *work)
77{
David Lechner9e549242017-01-15 17:09:43 -080078 struct pwm_beeper *beeper = container_of(work, struct pwm_beeper, work);
79 unsigned long period = READ_ONCE(beeper->period);
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070080
David Lechner9e549242017-01-15 17:09:43 -080081 if (period)
82 pwm_beeper_on(beeper, period);
83 else
84 pwm_beeper_off(beeper);
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070085}
86
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070087static int pwm_beeper_event(struct input_dev *input,
88 unsigned int type, unsigned int code, int value)
89{
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070090 struct pwm_beeper *beeper = input_get_drvdata(input);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070091
92 if (type != EV_SND || value < 0)
93 return -EINVAL;
94
95 switch (code) {
96 case SND_BELL:
97 value = value ? 1000 : 0;
98 break;
99 case SND_TONE:
100 break;
101 default:
102 return -EINVAL;
103 }
104
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -0700105 if (value == 0)
106 beeper->period = 0;
107 else
108 beeper->period = HZ_TO_NANOSECONDS(value);
109
Dmitry Torokhove9728f02017-01-19 11:13:37 -0800110 if (!beeper->suspended)
111 schedule_work(&beeper->work);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700112
113 return 0;
114}
115
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -0700116static void pwm_beeper_stop(struct pwm_beeper *beeper)
117{
118 cancel_work_sync(&beeper->work);
David Lechner9e549242017-01-15 17:09:43 -0800119 pwm_beeper_off(beeper);
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -0700120}
121
122static void pwm_beeper_close(struct input_dev *input)
123{
124 struct pwm_beeper *beeper = input_get_drvdata(input);
125
126 pwm_beeper_stop(beeper);
127}
128
Bill Pemberton5298cc42012-11-23 21:38:25 -0800129static int pwm_beeper_probe(struct platform_device *pdev)
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700130{
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800131 struct device *dev = &pdev->dev;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700132 struct pwm_beeper *beeper;
Dmitry Torokhov2de8b412017-01-19 13:52:49 -0800133 struct pwm_state state;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700134 int error;
135
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800136 beeper = devm_kzalloc(dev, sizeof(*beeper), GFP_KERNEL);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700137 if (!beeper)
138 return -ENOMEM;
139
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800140 beeper->pwm = devm_pwm_get(dev, NULL);
Sascha Hauerd56a2892012-11-03 12:05:52 -0700141 if (IS_ERR(beeper->pwm)) {
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700142 error = PTR_ERR(beeper->pwm);
David Lechner62481882017-01-06 10:35:16 -0800143 if (error != -EPROBE_DEFER)
144 dev_err(dev, "Failed to request PWM device: %d\n",
145 error);
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800146 return error;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700147 }
148
Dmitry Torokhov2de8b412017-01-19 13:52:49 -0800149 /* Sync up PWM state and ensure it is off. */
150 pwm_init_state(beeper->pwm, &state);
151 state.enabled = false;
152 error = pwm_apply_state(beeper->pwm, &state);
153 if (error) {
154 dev_err(dev, "failed to apply initial PWM state: %d\n",
155 error);
156 return error;
157 }
Boris Brezilloncfae56f2016-04-14 21:17:34 +0200158
David Lechner9e549242017-01-15 17:09:43 -0800159 beeper->amplifier = devm_regulator_get(dev, "amp");
160 if (IS_ERR(beeper->amplifier)) {
161 error = PTR_ERR(beeper->amplifier);
162 if (error != -EPROBE_DEFER)
163 dev_err(dev, "Failed to get 'amp' regulator: %d\n",
164 error);
165 return error;
166 }
167
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -0700168 INIT_WORK(&beeper->work, pwm_beeper_work);
169
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800170 beeper->input = devm_input_allocate_device(dev);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700171 if (!beeper->input) {
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800172 dev_err(dev, "Failed to allocate input device\n");
173 return -ENOMEM;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700174 }
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700175
176 beeper->input->name = "pwm-beeper";
177 beeper->input->phys = "pwm/input0";
178 beeper->input->id.bustype = BUS_HOST;
179 beeper->input->id.vendor = 0x001f;
180 beeper->input->id.product = 0x0001;
181 beeper->input->id.version = 0x0100;
182
Dmitry Torokhov48a55d72017-01-06 09:54:14 -0800183 input_set_capability(beeper->input, EV_SND, SND_TONE);
184 input_set_capability(beeper->input, EV_SND, SND_BELL);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700185
186 beeper->input->event = pwm_beeper_event;
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -0700187 beeper->input->close = pwm_beeper_close;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700188
189 input_set_drvdata(beeper->input, beeper);
190
191 error = input_register_device(beeper->input);
192 if (error) {
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800193 dev_err(dev, "Failed to register input device: %d\n", error);
194 return error;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700195 }
196
197 platform_set_drvdata(pdev, beeper);
198
199 return 0;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700200}
201
Jingoo Han97a652a2014-11-02 00:02:46 -0700202static int __maybe_unused pwm_beeper_suspend(struct device *dev)
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700203{
204 struct pwm_beeper *beeper = dev_get_drvdata(dev);
205
Dmitry Torokhove9728f02017-01-19 11:13:37 -0800206 /*
207 * Spinlock is taken here is not to protect write to
208 * beeper->suspended, but to ensure that pwm_beeper_event
209 * does not re-submit work once flag is set.
210 */
211 spin_lock_irq(&beeper->input->event_lock);
212 beeper->suspended = true;
213 spin_unlock_irq(&beeper->input->event_lock);
214
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -0700215 pwm_beeper_stop(beeper);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700216
217 return 0;
218}
219
Jingoo Han97a652a2014-11-02 00:02:46 -0700220static int __maybe_unused pwm_beeper_resume(struct device *dev)
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700221{
222 struct pwm_beeper *beeper = dev_get_drvdata(dev);
223
Dmitry Torokhove9728f02017-01-19 11:13:37 -0800224 spin_lock_irq(&beeper->input->event_lock);
225 beeper->suspended = false;
226 spin_unlock_irq(&beeper->input->event_lock);
227
228 /* Let worker figure out if we should resume beeping */
229 schedule_work(&beeper->work);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700230
231 return 0;
232}
233
234static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
235 pwm_beeper_suspend, pwm_beeper_resume);
236
Sascha Hauerd56a2892012-11-03 12:05:52 -0700237#ifdef CONFIG_OF
238static const struct of_device_id pwm_beeper_match[] = {
239 { .compatible = "pwm-beeper", },
240 { },
241};
Luis de Bethencourt544edf562015-09-03 10:49:46 -0700242MODULE_DEVICE_TABLE(of, pwm_beeper_match);
Sascha Hauerd56a2892012-11-03 12:05:52 -0700243#endif
244
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700245static struct platform_driver pwm_beeper_driver = {
246 .probe = pwm_beeper_probe,
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700247 .driver = {
248 .name = "pwm-beeper",
Dmitry Torokhovceb5b6c2015-02-19 16:22:31 -0800249 .pm = &pwm_beeper_pm_ops,
Sascha Hauerd56a2892012-11-03 12:05:52 -0700250 .of_match_table = of_match_ptr(pwm_beeper_match),
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700251 },
252};
JJ Ding840a7462011-11-29 11:08:40 -0800253module_platform_driver(pwm_beeper_driver);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700254
255MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
256MODULE_DESCRIPTION("PWM beeper driver");
257MODULE_LICENSE("GPL");
258MODULE_ALIAS("platform:pwm-beeper");