pwm-beeper.c 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
 *  PWM beeper driver
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under  the terms of the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the License, or (at your
 *  option) any later version.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/input.h>
#include <linux/module.h>
#include <linux/kernel.h>
19
#include <linux/of.h>
20 21 22
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/slab.h>
23
#include <linux/workqueue.h>
24 25 26 27

struct pwm_beeper {
	struct input_dev *input;
	struct pwm_device *pwm;
28
	struct work_struct work;
29 30 31 32 33
	unsigned long period;
};

#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
static void __pwm_beeper_set(struct pwm_beeper *beeper)
{
	unsigned long period = beeper->period;

	if (period) {
		pwm_config(beeper->pwm, period / 2, period);
		pwm_enable(beeper->pwm);
	} else
		pwm_disable(beeper->pwm);
}

static void pwm_beeper_work(struct work_struct *work)
{
	struct pwm_beeper *beeper =
		container_of(work, struct pwm_beeper, work);

	__pwm_beeper_set(beeper);
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static int pwm_beeper_event(struct input_dev *input,
			    unsigned int type, unsigned int code, int value)
{
	struct pwm_beeper *beeper = input_get_drvdata(input);

	if (type != EV_SND || value < 0)
		return -EINVAL;

	switch (code) {
	case SND_BELL:
		value = value ? 1000 : 0;
		break;
	case SND_TONE:
		break;
	default:
		return -EINVAL;
	}

71 72 73 74 75 76
	if (value == 0)
		beeper->period = 0;
	else
		beeper->period = HZ_TO_NANOSECONDS(value);

	schedule_work(&beeper->work);
77 78 79 80

	return 0;
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static void pwm_beeper_stop(struct pwm_beeper *beeper)
{
	cancel_work_sync(&beeper->work);

	if (beeper->period)
		pwm_disable(beeper->pwm);
}

static void pwm_beeper_close(struct input_dev *input)
{
	struct pwm_beeper *beeper = input_get_drvdata(input);

	pwm_beeper_stop(beeper);
}

B
Bill Pemberton 已提交
96
static int pwm_beeper_probe(struct platform_device *pdev)
97
{
98
	struct device *dev = &pdev->dev;
99 100 101
	struct pwm_beeper *beeper;
	int error;

102
	beeper = devm_kzalloc(dev, sizeof(*beeper), GFP_KERNEL);
103 104 105
	if (!beeper)
		return -ENOMEM;

106
	beeper->pwm = devm_pwm_get(dev, NULL);
107 108
	if (IS_ERR(beeper->pwm)) {
		error = PTR_ERR(beeper->pwm);
109 110
		dev_err(dev, "Failed to request PWM device: %d\n", error);
		return error;
111 112
	}

113 114 115 116 117 118
	/*
	 * FIXME: pwm_apply_args() should be removed when switching to
	 * the atomic PWM API.
	 */
	pwm_apply_args(beeper->pwm);

119 120
	INIT_WORK(&beeper->work, pwm_beeper_work);

121
	beeper->input = devm_input_allocate_device(dev);
122
	if (!beeper->input) {
123 124
		dev_err(dev, "Failed to allocate input device\n");
		return -ENOMEM;
125 126 127 128 129 130 131 132 133 134 135 136 137
	}

	beeper->input->name = "pwm-beeper";
	beeper->input->phys = "pwm/input0";
	beeper->input->id.bustype = BUS_HOST;
	beeper->input->id.vendor = 0x001f;
	beeper->input->id.product = 0x0001;
	beeper->input->id.version = 0x0100;

	beeper->input->evbit[0] = BIT(EV_SND);
	beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL);

	beeper->input->event = pwm_beeper_event;
138
	beeper->input->close = pwm_beeper_close;
139 140 141 142 143

	input_set_drvdata(beeper->input, beeper);

	error = input_register_device(beeper->input);
	if (error) {
144 145
		dev_err(dev, "Failed to register input device: %d\n", error);
		return error;
146 147 148 149 150 151 152
	}

	platform_set_drvdata(pdev, beeper);

	return 0;
}

153
static int __maybe_unused pwm_beeper_suspend(struct device *dev)
154 155 156
{
	struct pwm_beeper *beeper = dev_get_drvdata(dev);

157
	pwm_beeper_stop(beeper);
158 159 160 161

	return 0;
}

162
static int __maybe_unused pwm_beeper_resume(struct device *dev)
163 164 165
{
	struct pwm_beeper *beeper = dev_get_drvdata(dev);

166 167
	if (beeper->period)
		__pwm_beeper_set(beeper);
168 169 170 171 172 173 174

	return 0;
}

static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
			 pwm_beeper_suspend, pwm_beeper_resume);

175 176 177 178 179
#ifdef CONFIG_OF
static const struct of_device_id pwm_beeper_match[] = {
	{ .compatible = "pwm-beeper", },
	{ },
};
180
MODULE_DEVICE_TABLE(of, pwm_beeper_match);
181 182
#endif

183 184 185 186
static struct platform_driver pwm_beeper_driver = {
	.probe	= pwm_beeper_probe,
	.driver = {
		.name	= "pwm-beeper",
187
		.pm	= &pwm_beeper_pm_ops,
188
		.of_match_table = of_match_ptr(pwm_beeper_match),
189 190
	},
};
191
module_platform_driver(pwm_beeper_driver);
192 193 194 195 196

MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
MODULE_DESCRIPTION("PWM beeper driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:pwm-beeper");