treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / misc / gpio-beeper.c
blobd2d2954e2f794638c0f3758c13924c9b2c8ffff7
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Generic GPIO beeper driver
5 * Copyright (C) 2013-2014 Alexander Shiyan <shc_work@mail.ru>
6 */
8 #include <linux/input.h>
9 #include <linux/module.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/of.h>
12 #include <linux/workqueue.h>
13 #include <linux/platform_device.h>
15 #define BEEPER_MODNAME "gpio-beeper"
17 struct gpio_beeper {
18 struct work_struct work;
19 struct gpio_desc *desc;
20 bool beeping;
23 static void gpio_beeper_toggle(struct gpio_beeper *beep, bool on)
25 gpiod_set_value_cansleep(beep->desc, on);
28 static void gpio_beeper_work(struct work_struct *work)
30 struct gpio_beeper *beep = container_of(work, struct gpio_beeper, work);
32 gpio_beeper_toggle(beep, beep->beeping);
35 static int gpio_beeper_event(struct input_dev *dev, unsigned int type,
36 unsigned int code, int value)
38 struct gpio_beeper *beep = input_get_drvdata(dev);
40 if (type != EV_SND || code != SND_BELL)
41 return -ENOTSUPP;
43 if (value < 0)
44 return -EINVAL;
46 beep->beeping = value;
47 /* Schedule work to actually turn the beeper on or off */
48 schedule_work(&beep->work);
50 return 0;
53 static void gpio_beeper_close(struct input_dev *input)
55 struct gpio_beeper *beep = input_get_drvdata(input);
57 cancel_work_sync(&beep->work);
58 gpio_beeper_toggle(beep, false);
61 static int gpio_beeper_probe(struct platform_device *pdev)
63 struct gpio_beeper *beep;
64 struct input_dev *input;
66 beep = devm_kzalloc(&pdev->dev, sizeof(*beep), GFP_KERNEL);
67 if (!beep)
68 return -ENOMEM;
70 beep->desc = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
71 if (IS_ERR(beep->desc))
72 return PTR_ERR(beep->desc);
74 input = devm_input_allocate_device(&pdev->dev);
75 if (!input)
76 return -ENOMEM;
78 INIT_WORK(&beep->work, gpio_beeper_work);
80 input->name = pdev->name;
81 input->id.bustype = BUS_HOST;
82 input->id.vendor = 0x0001;
83 input->id.product = 0x0001;
84 input->id.version = 0x0100;
85 input->close = gpio_beeper_close;
86 input->event = gpio_beeper_event;
88 input_set_capability(input, EV_SND, SND_BELL);
90 input_set_drvdata(input, beep);
92 return input_register_device(input);
95 #ifdef CONFIG_OF
96 static const struct of_device_id gpio_beeper_of_match[] = {
97 { .compatible = BEEPER_MODNAME, },
98 { }
100 MODULE_DEVICE_TABLE(of, gpio_beeper_of_match);
101 #endif
103 static struct platform_driver gpio_beeper_platform_driver = {
104 .driver = {
105 .name = BEEPER_MODNAME,
106 .of_match_table = of_match_ptr(gpio_beeper_of_match),
108 .probe = gpio_beeper_probe,
110 module_platform_driver(gpio_beeper_platform_driver);
112 MODULE_LICENSE("GPL");
113 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
114 MODULE_DESCRIPTION("Generic GPIO beeper driver");