Version:  2.0.40 2.2.26 2.4.37 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18

Linux/drivers/input/misc/gpio-beeper.c

  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 
 21 struct gpio_beeper {
 22         struct work_struct      work;
 23         struct gpio_desc        *desc;
 24         bool                    beeping;
 25 };
 26 
 27 static void gpio_beeper_toggle(struct gpio_beeper *beep, bool on)
 28 {
 29         gpiod_set_value_cansleep(beep->desc, on);
 30 }
 31 
 32 static 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 
 39 static 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 
 57 static 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 
 65 static 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
105 static const struct of_device_id gpio_beeper_of_match[] = {
106         { .compatible = BEEPER_MODNAME, },
107         { }
108 };
109 MODULE_DEVICE_TABLE(of, gpio_beeper_of_match);
110 #endif
111 
112 static struct platform_driver gpio_beeper_platform_driver = {
113         .driver = {
114                 .name           = BEEPER_MODNAME,
115                 .owner          = THIS_MODULE,
116                 .of_match_table = of_match_ptr(gpio_beeper_of_match),
117         },
118         .probe  = gpio_beeper_probe,
119 };
120 module_platform_driver(gpio_beeper_platform_driver);
121 
122 MODULE_LICENSE("GPL");
123 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
124 MODULE_DESCRIPTION("Generic GPIO beeper driver");
125 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us