Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Generic GPIO beeper driver
0004  *
0005  * Copyright (C) 2013-2014 Alexander Shiyan <shc_work@mail.ru>
0006  */
0007 
0008 #include <linux/input.h>
0009 #include <linux/module.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/of.h>
0012 #include <linux/workqueue.h>
0013 #include <linux/platform_device.h>
0014 
0015 #define BEEPER_MODNAME      "gpio-beeper"
0016 
0017 struct gpio_beeper {
0018     struct work_struct  work;
0019     struct gpio_desc    *desc;
0020     bool            beeping;
0021 };
0022 
0023 static void gpio_beeper_toggle(struct gpio_beeper *beep, bool on)
0024 {
0025     gpiod_set_value_cansleep(beep->desc, on);
0026 }
0027 
0028 static void gpio_beeper_work(struct work_struct *work)
0029 {
0030     struct gpio_beeper *beep = container_of(work, struct gpio_beeper, work);
0031 
0032     gpio_beeper_toggle(beep, beep->beeping);
0033 }
0034 
0035 static int gpio_beeper_event(struct input_dev *dev, unsigned int type,
0036                  unsigned int code, int value)
0037 {
0038     struct gpio_beeper *beep = input_get_drvdata(dev);
0039 
0040     if (type != EV_SND || code != SND_BELL)
0041         return -ENOTSUPP;
0042 
0043     if (value < 0)
0044         return -EINVAL;
0045 
0046     beep->beeping = value;
0047     /* Schedule work to actually turn the beeper on or off */
0048     schedule_work(&beep->work);
0049 
0050     return 0;
0051 }
0052 
0053 static void gpio_beeper_close(struct input_dev *input)
0054 {
0055     struct gpio_beeper *beep = input_get_drvdata(input);
0056 
0057     cancel_work_sync(&beep->work);
0058     gpio_beeper_toggle(beep, false);
0059 }
0060 
0061 static int gpio_beeper_probe(struct platform_device *pdev)
0062 {
0063     struct gpio_beeper *beep;
0064     struct input_dev *input;
0065 
0066     beep = devm_kzalloc(&pdev->dev, sizeof(*beep), GFP_KERNEL);
0067     if (!beep)
0068         return -ENOMEM;
0069 
0070     beep->desc = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
0071     if (IS_ERR(beep->desc))
0072         return PTR_ERR(beep->desc);
0073 
0074     input = devm_input_allocate_device(&pdev->dev);
0075     if (!input)
0076         return -ENOMEM;
0077 
0078     INIT_WORK(&beep->work, gpio_beeper_work);
0079 
0080     input->name     = pdev->name;
0081     input->id.bustype   = BUS_HOST;
0082     input->id.vendor    = 0x0001;
0083     input->id.product   = 0x0001;
0084     input->id.version   = 0x0100;
0085     input->close        = gpio_beeper_close;
0086     input->event        = gpio_beeper_event;
0087 
0088     input_set_capability(input, EV_SND, SND_BELL);
0089 
0090     input_set_drvdata(input, beep);
0091 
0092     return input_register_device(input);
0093 }
0094 
0095 #ifdef CONFIG_OF
0096 static const struct of_device_id gpio_beeper_of_match[] = {
0097     { .compatible = BEEPER_MODNAME, },
0098     { }
0099 };
0100 MODULE_DEVICE_TABLE(of, gpio_beeper_of_match);
0101 #endif
0102 
0103 static struct platform_driver gpio_beeper_platform_driver = {
0104     .driver = {
0105         .name       = BEEPER_MODNAME,
0106         .of_match_table = of_match_ptr(gpio_beeper_of_match),
0107     },
0108     .probe  = gpio_beeper_probe,
0109 };
0110 module_platform_driver(gpio_beeper_platform_driver);
0111 
0112 MODULE_LICENSE("GPL");
0113 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
0114 MODULE_DESCRIPTION("Generic GPIO beeper driver");