1/*
2 * Generic GPIO beeper driver
3 *
4 * Copyright (C) 2013-2014 Alexander Shiyan <shc_work@mail.ru>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/input.h>
13#include <linux/module.h>
14#include <linux/gpio/consumer.h>
15#include <linux/of.h>
16#include <linux/workqueue.h>
17#include <linux/platform_device.h>
18
19#define BEEPER_MODNAME		"gpio-beeper"
20
21struct gpio_beeper {
22	struct work_struct	work;
23	struct gpio_desc	*desc;
24	bool			beeping;
25};
26
27static void gpio_beeper_toggle(struct gpio_beeper *beep, bool on)
28{
29	gpiod_set_value_cansleep(beep->desc, on);
30}
31
32static void gpio_beeper_work(struct work_struct *work)
33{
34	struct gpio_beeper *beep = container_of(work, struct gpio_beeper, work);
35
36	gpio_beeper_toggle(beep, beep->beeping);
37}
38
39static int gpio_beeper_event(struct input_dev *dev, unsigned int type,
40			     unsigned int code, int value)
41{
42	struct gpio_beeper *beep = input_get_drvdata(dev);
43
44	if (type != EV_SND || code != SND_BELL)
45		return -ENOTSUPP;
46
47	if (value < 0)
48		return -EINVAL;
49
50	beep->beeping = value;
51	/* Schedule work to actually turn the beeper on or off */
52	schedule_work(&beep->work);
53
54	return 0;
55}
56
57static void gpio_beeper_close(struct input_dev *input)
58{
59	struct gpio_beeper *beep = input_get_drvdata(input);
60
61	cancel_work_sync(&beep->work);
62	gpio_beeper_toggle(beep, false);
63}
64
65static int gpio_beeper_probe(struct platform_device *pdev)
66{
67	struct gpio_beeper *beep;
68	struct input_dev *input;
69	int err;
70
71	beep = devm_kzalloc(&pdev->dev, sizeof(*beep), GFP_KERNEL);
72	if (!beep)
73		return -ENOMEM;
74
75	beep->desc = devm_gpiod_get(&pdev->dev, NULL);
76	if (IS_ERR(beep->desc))
77		return PTR_ERR(beep->desc);
78
79	input = devm_input_allocate_device(&pdev->dev);
80	if (!input)
81		return -ENOMEM;
82
83	INIT_WORK(&beep->work, gpio_beeper_work);
84
85	input->name		= pdev->name;
86	input->id.bustype	= BUS_HOST;
87	input->id.vendor	= 0x0001;
88	input->id.product	= 0x0001;
89	input->id.version	= 0x0100;
90	input->close		= gpio_beeper_close;
91	input->event		= gpio_beeper_event;
92
93	input_set_capability(input, EV_SND, SND_BELL);
94
95	err = gpiod_direction_output(beep->desc, 0);
96	if (err)
97		return err;
98
99	input_set_drvdata(input, beep);
100
101	return input_register_device(input);
102}
103
104#ifdef CONFIG_OF
105static const struct of_device_id gpio_beeper_of_match[] = {
106	{ .compatible = BEEPER_MODNAME, },
107	{ }
108};
109MODULE_DEVICE_TABLE(of, gpio_beeper_of_match);
110#endif
111
112static struct platform_driver gpio_beeper_platform_driver = {
113	.driver	= {
114		.name		= BEEPER_MODNAME,
115		.of_match_table	= of_match_ptr(gpio_beeper_of_match),
116	},
117	.probe	= gpio_beeper_probe,
118};
119module_platform_driver(gpio_beeper_platform_driver);
120
121MODULE_LICENSE("GPL");
122MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
123MODULE_DESCRIPTION("Generic GPIO beeper driver");
124