2 * Generic GPIO beeper driver
4 * Copyright (C) 2013-2014 Alexander Shiyan <shc_work@mail.ru>
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.
12 #include <linux/input.h>
13 #include <linux/module.h>
14 #include <linux/gpio/consumer.h>
16 #include <linux/workqueue.h>
17 #include <linux/platform_device.h>
19 #define BEEPER_MODNAME "gpio-beeper"
22 struct work_struct work;
23 struct gpio_desc *desc;
27 static void gpio_beeper_toggle(struct gpio_beeper *beep, bool on)
29 gpiod_set_value_cansleep(beep->desc, on);
32 static void gpio_beeper_work(struct work_struct *work)
34 struct gpio_beeper *beep = container_of(work, struct gpio_beeper, work);
36 gpio_beeper_toggle(beep, beep->beeping);
39 static int gpio_beeper_event(struct input_dev *dev, unsigned int type,
40 unsigned int code, int value)
42 struct gpio_beeper *beep = input_get_drvdata(dev);
44 if (type != EV_SND || code != SND_BELL)
50 beep->beeping = value;
51 /* Schedule work to actually turn the beeper on or off */
52 schedule_work(&beep->work);
57 static void gpio_beeper_close(struct input_dev *input)
59 struct gpio_beeper *beep = input_get_drvdata(input);
61 cancel_work_sync(&beep->work);
62 gpio_beeper_toggle(beep, false);
65 static int gpio_beeper_probe(struct platform_device *pdev)
67 struct gpio_beeper *beep;
68 struct input_dev *input;
70 beep = devm_kzalloc(&pdev->dev, sizeof(*beep), GFP_KERNEL);
74 beep->desc = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
75 if (IS_ERR(beep->desc))
76 return PTR_ERR(beep->desc);
78 input = devm_input_allocate_device(&pdev->dev);
82 INIT_WORK(&beep->work, gpio_beeper_work);
84 input->name = pdev->name;
85 input->id.bustype = BUS_HOST;
86 input->id.vendor = 0x0001;
87 input->id.product = 0x0001;
88 input->id.version = 0x0100;
89 input->close = gpio_beeper_close;
90 input->event = gpio_beeper_event;
92 input_set_capability(input, EV_SND, SND_BELL);
94 input_set_drvdata(input, beep);
96 return input_register_device(input);
100 static const struct of_device_id gpio_beeper_of_match[] = {
101 { .compatible = BEEPER_MODNAME, },
104 MODULE_DEVICE_TABLE(of, gpio_beeper_of_match);
107 static struct platform_driver gpio_beeper_platform_driver = {
109 .name = BEEPER_MODNAME,
110 .of_match_table = of_match_ptr(gpio_beeper_of_match),
112 .probe = gpio_beeper_probe,
114 module_platform_driver(gpio_beeper_platform_driver);
116 MODULE_LICENSE("GPL");
117 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
118 MODULE_DESCRIPTION("Generic GPIO beeper driver");