blob: 9d7987d9ba1b0a9b09e0f98b8bc2e401d7890511 [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>
17#include <linux/module.h>
18#include <linux/kernel.h>
Sachin Kamat9dbe4c32013-10-06 00:56:09 -070019#include <linux/of.h>
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070020#include <linux/platform_device.h>
21#include <linux/pwm.h>
22#include <linux/slab.h>
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070023#include <linux/workqueue.h>
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070024
25struct pwm_beeper {
26 struct input_dev *input;
27 struct pwm_device *pwm;
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070028 struct work_struct work;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070029 unsigned long period;
Dmitry Torokhove9728f02017-01-19 11:13:37 -080030 bool suspended;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070031};
32
33#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
34
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070035static void __pwm_beeper_set(struct pwm_beeper *beeper)
36{
37 unsigned long period = beeper->period;
38
39 if (period) {
40 pwm_config(beeper->pwm, period / 2, period);
41 pwm_enable(beeper->pwm);
42 } else
43 pwm_disable(beeper->pwm);
44}
45
46static void pwm_beeper_work(struct work_struct *work)
47{
48 struct pwm_beeper *beeper =
49 container_of(work, struct pwm_beeper, work);
50
51 __pwm_beeper_set(beeper);
52}
53
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070054static int pwm_beeper_event(struct input_dev *input,
55 unsigned int type, unsigned int code, int value)
56{
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070057 struct pwm_beeper *beeper = input_get_drvdata(input);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070058
59 if (type != EV_SND || value < 0)
60 return -EINVAL;
61
62 switch (code) {
63 case SND_BELL:
64 value = value ? 1000 : 0;
65 break;
66 case SND_TONE:
67 break;
68 default:
69 return -EINVAL;
70 }
71
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070072 if (value == 0)
73 beeper->period = 0;
74 else
75 beeper->period = HZ_TO_NANOSECONDS(value);
76
Dmitry Torokhove9728f02017-01-19 11:13:37 -080077 if (!beeper->suspended)
78 schedule_work(&beeper->work);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070079
80 return 0;
81}
82
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -070083static void pwm_beeper_stop(struct pwm_beeper *beeper)
84{
85 cancel_work_sync(&beeper->work);
86
87 if (beeper->period)
88 pwm_disable(beeper->pwm);
89}
90
91static void pwm_beeper_close(struct input_dev *input)
92{
93 struct pwm_beeper *beeper = input_get_drvdata(input);
94
95 pwm_beeper_stop(beeper);
96}
97
Bill Pemberton5298cc42012-11-23 21:38:25 -080098static int pwm_beeper_probe(struct platform_device *pdev)
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070099{
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800100 struct device *dev = &pdev->dev;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700101 struct pwm_beeper *beeper;
102 int error;
103
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800104 beeper = devm_kzalloc(dev, sizeof(*beeper), GFP_KERNEL);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700105 if (!beeper)
106 return -ENOMEM;
107
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800108 beeper->pwm = devm_pwm_get(dev, NULL);
Sascha Hauerd56a2892012-11-03 12:05:52 -0700109 if (IS_ERR(beeper->pwm)) {
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700110 error = PTR_ERR(beeper->pwm);
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800111 dev_err(dev, "Failed to request PWM device: %d\n", error);
112 return error;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700113 }
114
Boris Brezilloncfae56f2016-04-14 21:17:34 +0200115 /*
116 * FIXME: pwm_apply_args() should be removed when switching to
117 * the atomic PWM API.
118 */
119 pwm_apply_args(beeper->pwm);
120
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -0700121 INIT_WORK(&beeper->work, pwm_beeper_work);
122
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800123 beeper->input = devm_input_allocate_device(dev);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700124 if (!beeper->input) {
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800125 dev_err(dev, "Failed to allocate input device\n");
126 return -ENOMEM;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700127 }
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700128
129 beeper->input->name = "pwm-beeper";
130 beeper->input->phys = "pwm/input0";
131 beeper->input->id.bustype = BUS_HOST;
132 beeper->input->id.vendor = 0x001f;
133 beeper->input->id.product = 0x0001;
134 beeper->input->id.version = 0x0100;
135
Dmitry Torokhov48a55d72017-01-06 09:54:14 -0800136 input_set_capability(beeper->input, EV_SND, SND_TONE);
137 input_set_capability(beeper->input, EV_SND, SND_BELL);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700138
139 beeper->input->event = pwm_beeper_event;
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -0700140 beeper->input->close = pwm_beeper_close;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700141
142 input_set_drvdata(beeper->input, beeper);
143
144 error = input_register_device(beeper->input);
145 if (error) {
Dmitry Torokhovbcf4b042017-01-06 09:50:14 -0800146 dev_err(dev, "Failed to register input device: %d\n", error);
147 return error;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700148 }
149
150 platform_set_drvdata(pdev, beeper);
151
152 return 0;
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700153}
154
Jingoo Han97a652a2014-11-02 00:02:46 -0700155static int __maybe_unused pwm_beeper_suspend(struct device *dev)
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700156{
157 struct pwm_beeper *beeper = dev_get_drvdata(dev);
158
Dmitry Torokhove9728f02017-01-19 11:13:37 -0800159 /*
160 * Spinlock is taken here is not to protect write to
161 * beeper->suspended, but to ensure that pwm_beeper_event
162 * does not re-submit work once flag is set.
163 */
164 spin_lock_irq(&beeper->input->event_lock);
165 beeper->suspended = true;
166 spin_unlock_irq(&beeper->input->event_lock);
167
Manfred Schlaeglf49cf3b2016-05-27 16:36:36 -0700168 pwm_beeper_stop(beeper);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700169
170 return 0;
171}
172
Jingoo Han97a652a2014-11-02 00:02:46 -0700173static int __maybe_unused pwm_beeper_resume(struct device *dev)
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700174{
175 struct pwm_beeper *beeper = dev_get_drvdata(dev);
176
Dmitry Torokhove9728f02017-01-19 11:13:37 -0800177 spin_lock_irq(&beeper->input->event_lock);
178 beeper->suspended = false;
179 spin_unlock_irq(&beeper->input->event_lock);
180
181 /* Let worker figure out if we should resume beeping */
182 schedule_work(&beeper->work);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700183
184 return 0;
185}
186
187static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
188 pwm_beeper_suspend, pwm_beeper_resume);
189
Sascha Hauerd56a2892012-11-03 12:05:52 -0700190#ifdef CONFIG_OF
191static const struct of_device_id pwm_beeper_match[] = {
192 { .compatible = "pwm-beeper", },
193 { },
194};
Luis de Bethencourt544edf562015-09-03 10:49:46 -0700195MODULE_DEVICE_TABLE(of, pwm_beeper_match);
Sascha Hauerd56a2892012-11-03 12:05:52 -0700196#endif
197
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700198static struct platform_driver pwm_beeper_driver = {
199 .probe = pwm_beeper_probe,
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700200 .driver = {
201 .name = "pwm-beeper",
Dmitry Torokhovceb5b6c2015-02-19 16:22:31 -0800202 .pm = &pwm_beeper_pm_ops,
Sascha Hauerd56a2892012-11-03 12:05:52 -0700203 .of_match_table = of_match_ptr(pwm_beeper_match),
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700204 },
205};
JJ Ding840a7462011-11-29 11:08:40 -0800206module_platform_driver(pwm_beeper_driver);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700207
208MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
209MODULE_DESCRIPTION("PWM beeper driver");
210MODULE_LICENSE("GPL");
211MODULE_ALIAS("platform:pwm-beeper");