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/module.h>
18 #include <linux/kernel.h>
19 #include <linux/platform_device.h>
20 #include <linux/pwm.h>
21 #include <linux/slab.h>
24 struct input_dev
*input
;
25 struct pwm_device
*pwm
;
29 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
31 static int pwm_beeper_event(struct input_dev
*input
,
32 unsigned int type
, unsigned int code
, int value
)
35 struct pwm_beeper
*beeper
= input_get_drvdata(input
);
38 if (type
!= EV_SND
|| value
< 0)
43 value
= value
? 1000 : 0;
52 pwm_config(beeper
->pwm
, 0, 0);
53 pwm_disable(beeper
->pwm
);
55 period
= HZ_TO_NANOSECONDS(value
);
56 ret
= pwm_config(beeper
->pwm
, period
/ 2, period
);
59 ret
= pwm_enable(beeper
->pwm
);
62 beeper
->period
= period
;
68 static int __devinit
pwm_beeper_probe(struct platform_device
*pdev
)
70 unsigned long pwm_id
= (unsigned long)pdev
->dev
.platform_data
;
71 struct pwm_beeper
*beeper
;
74 beeper
= kzalloc(sizeof(*beeper
), GFP_KERNEL
);
78 beeper
->pwm
= pwm_request(pwm_id
, "pwm beeper");
80 if (IS_ERR(beeper
->pwm
)) {
81 error
= PTR_ERR(beeper
->pwm
);
82 dev_err(&pdev
->dev
, "Failed to request pwm device: %d\n", error
);
86 beeper
->input
= input_allocate_device();
88 dev_err(&pdev
->dev
, "Failed to allocate input device\n");
92 beeper
->input
->dev
.parent
= &pdev
->dev
;
94 beeper
->input
->name
= "pwm-beeper";
95 beeper
->input
->phys
= "pwm/input0";
96 beeper
->input
->id
.bustype
= BUS_HOST
;
97 beeper
->input
->id
.vendor
= 0x001f;
98 beeper
->input
->id
.product
= 0x0001;
99 beeper
->input
->id
.version
= 0x0100;
101 beeper
->input
->evbit
[0] = BIT(EV_SND
);
102 beeper
->input
->sndbit
[0] = BIT(SND_TONE
) | BIT(SND_BELL
);
104 beeper
->input
->event
= pwm_beeper_event
;
106 input_set_drvdata(beeper
->input
, beeper
);
108 error
= input_register_device(beeper
->input
);
110 dev_err(&pdev
->dev
, "Failed to register input device: %d\n", error
);
114 platform_set_drvdata(pdev
, beeper
);
119 input_free_device(beeper
->input
);
121 pwm_free(beeper
->pwm
);
128 static int __devexit
pwm_beeper_remove(struct platform_device
*pdev
)
130 struct pwm_beeper
*beeper
= platform_get_drvdata(pdev
);
132 platform_set_drvdata(pdev
, NULL
);
133 input_unregister_device(beeper
->input
);
135 pwm_disable(beeper
->pwm
);
136 pwm_free(beeper
->pwm
);
144 static int pwm_beeper_suspend(struct device
*dev
)
146 struct pwm_beeper
*beeper
= dev_get_drvdata(dev
);
149 pwm_disable(beeper
->pwm
);
154 static int pwm_beeper_resume(struct device
*dev
)
156 struct pwm_beeper
*beeper
= dev_get_drvdata(dev
);
158 if (beeper
->period
) {
159 pwm_config(beeper
->pwm
, beeper
->period
/ 2, beeper
->period
);
160 pwm_enable(beeper
->pwm
);
166 static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops
,
167 pwm_beeper_suspend
, pwm_beeper_resume
);
169 #define PWM_BEEPER_PM_OPS (&pwm_beeper_pm_ops)
171 #define PWM_BEEPER_PM_OPS NULL
174 static struct platform_driver pwm_beeper_driver
= {
175 .probe
= pwm_beeper_probe
,
176 .remove
= __devexit_p(pwm_beeper_remove
),
178 .name
= "pwm-beeper",
179 .owner
= THIS_MODULE
,
180 .pm
= PWM_BEEPER_PM_OPS
,
183 module_platform_driver(pwm_beeper_driver
);
185 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
186 MODULE_DESCRIPTION("PWM beeper driver");
187 MODULE_LICENSE("GPL");
188 MODULE_ALIAS("platform:pwm-beeper");