blob: 2ff4d1c78ab8ff91c4338c6e40d6ff91fac349c3 [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>
19#include <linux/platform_device.h>
20#include <linux/pwm.h>
21#include <linux/slab.h>
22
23struct pwm_beeper {
24 struct input_dev *input;
25 struct pwm_device *pwm;
26 unsigned long period;
27};
28
29#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
30
31static int pwm_beeper_event(struct input_dev *input,
32 unsigned int type, unsigned int code, int value)
33{
34 int ret = 0;
35 struct pwm_beeper *beeper = input_get_drvdata(input);
36 unsigned long period;
37
38 if (type != EV_SND || value < 0)
39 return -EINVAL;
40
41 switch (code) {
42 case SND_BELL:
43 value = value ? 1000 : 0;
44 break;
45 case SND_TONE:
46 break;
47 default:
48 return -EINVAL;
49 }
50
51 if (value == 0) {
52 pwm_config(beeper->pwm, 0, 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;
63 }
64
65 return 0;
66}
67
Bill Pemberton5298cc42012-11-23 21:38:25 -080068static int pwm_beeper_probe(struct platform_device *pdev)
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070069{
70 unsigned long pwm_id = (unsigned long)pdev->dev.platform_data;
71 struct pwm_beeper *beeper;
72 int error;
73
74 beeper = kzalloc(sizeof(*beeper), GFP_KERNEL);
75 if (!beeper)
76 return -ENOMEM;
77
Sascha Hauerd56a2892012-11-03 12:05:52 -070078 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");
82 }
Lars-Peter Clausene22739d2010-07-14 00:25:21 -070083
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;
88 }
89
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;
95 }
96 beeper->input->dev.parent = &pdev->dev;
97
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;
104
105 beeper->input->evbit[0] = BIT(EV_SND);
106 beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL);
107
108 beeper->input->event = pwm_beeper_event;
109
110 input_set_drvdata(beeper->input, beeper);
111
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;
116 }
117
118 platform_set_drvdata(pdev, beeper);
119
120 return 0;
121
122err_input_free:
123 input_free_device(beeper->input);
124err_pwm_free:
125 pwm_free(beeper->pwm);
126err_free:
127 kfree(beeper);
128
129 return error;
130}
131
Bill Pembertone2619cf2012-11-23 21:50:47 -0800132static int pwm_beeper_remove(struct platform_device *pdev)
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700133{
134 struct pwm_beeper *beeper = platform_get_drvdata(pdev);
135
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700136 input_unregister_device(beeper->input);
137
138 pwm_disable(beeper->pwm);
139 pwm_free(beeper->pwm);
140
141 kfree(beeper);
142
143 return 0;
144}
145
Jingoo Han7f262822013-08-05 09:06:42 -0700146#ifdef CONFIG_PM_SLEEP
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700147static int pwm_beeper_suspend(struct device *dev)
148{
149 struct pwm_beeper *beeper = dev_get_drvdata(dev);
150
151 if (beeper->period)
152 pwm_disable(beeper->pwm);
153
154 return 0;
155}
156
157static int pwm_beeper_resume(struct device *dev)
158{
159 struct pwm_beeper *beeper = dev_get_drvdata(dev);
160
161 if (beeper->period) {
162 pwm_config(beeper->pwm, beeper->period / 2, beeper->period);
163 pwm_enable(beeper->pwm);
164 }
165
166 return 0;
167}
168
169static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
170 pwm_beeper_suspend, pwm_beeper_resume);
171
172#define PWM_BEEPER_PM_OPS (&pwm_beeper_pm_ops)
173#else
174#define PWM_BEEPER_PM_OPS NULL
175#endif
176
Sascha Hauerd56a2892012-11-03 12:05:52 -0700177#ifdef CONFIG_OF
178static const struct of_device_id pwm_beeper_match[] = {
179 { .compatible = "pwm-beeper", },
180 { },
181};
182#endif
183
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700184static struct platform_driver pwm_beeper_driver = {
185 .probe = pwm_beeper_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800186 .remove = pwm_beeper_remove,
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700187 .driver = {
188 .name = "pwm-beeper",
189 .owner = THIS_MODULE,
190 .pm = PWM_BEEPER_PM_OPS,
Sascha Hauerd56a2892012-11-03 12:05:52 -0700191 .of_match_table = of_match_ptr(pwm_beeper_match),
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700192 },
193};
JJ Ding840a7462011-11-29 11:08:40 -0800194module_platform_driver(pwm_beeper_driver);
Lars-Peter Clausene22739d2010-07-14 00:25:21 -0700195
196MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
197MODULE_DESCRIPTION("PWM beeper driver");
198MODULE_LICENSE("GPL");
199MODULE_ALIAS("platform:pwm-beeper");