Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Generic GPIO beeper driver
  4 *
  5 * Copyright (C) 2013-2014 Alexander Shiyan <shc_work@mail.ru>
 
 
 
 
 
  6 */
  7
  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>
 14
 15#define BEEPER_MODNAME		"gpio-beeper"
 16
 17struct gpio_beeper {
 18	struct work_struct	work;
 19	struct gpio_desc	*desc;
 
 20	bool			beeping;
 21};
 22
 23static void gpio_beeper_toggle(struct gpio_beeper *beep, bool on)
 24{
 25	gpiod_set_value_cansleep(beep->desc, on);
 26}
 27
 28static void gpio_beeper_work(struct work_struct *work)
 29{
 30	struct gpio_beeper *beep = container_of(work, struct gpio_beeper, work);
 31
 32	gpio_beeper_toggle(beep, beep->beeping);
 33}
 34
 35static int gpio_beeper_event(struct input_dev *dev, unsigned int type,
 36			     unsigned int code, int value)
 37{
 38	struct gpio_beeper *beep = input_get_drvdata(dev);
 39
 40	if (type != EV_SND || code != SND_BELL)
 41		return -ENOTSUPP;
 42
 43	if (value < 0)
 44		return -EINVAL;
 45
 46	beep->beeping = value;
 47	/* Schedule work to actually turn the beeper on or off */
 48	schedule_work(&beep->work);
 49
 50	return 0;
 51}
 52
 53static void gpio_beeper_close(struct input_dev *input)
 54{
 55	struct gpio_beeper *beep = input_get_drvdata(input);
 56
 57	cancel_work_sync(&beep->work);
 58	gpio_beeper_toggle(beep, false);
 59}
 60
 61static int gpio_beeper_probe(struct platform_device *pdev)
 62{
 63	struct gpio_beeper *beep;
 
 64	struct input_dev *input;
 
 
 65
 66	beep = devm_kzalloc(&pdev->dev, sizeof(*beep), GFP_KERNEL);
 67	if (!beep)
 68		return -ENOMEM;
 69
 70	beep->desc = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
 71	if (IS_ERR(beep->desc))
 72		return PTR_ERR(beep->desc);
 73
 74	input = devm_input_allocate_device(&pdev->dev);
 75	if (!input)
 76		return -ENOMEM;
 77
 78	INIT_WORK(&beep->work, gpio_beeper_work);
 79
 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;
 87
 88	input_set_capability(input, EV_SND, SND_BELL);
 89
 
 
 
 
 
 
 
 90	input_set_drvdata(input, beep);
 91
 92	return input_register_device(input);
 93}
 94
 95#ifdef CONFIG_OF
 96static const struct of_device_id gpio_beeper_of_match[] = {
 97	{ .compatible = BEEPER_MODNAME, },
 98	{ }
 99};
100MODULE_DEVICE_TABLE(of, gpio_beeper_of_match);
101#endif
102
103static struct platform_driver gpio_beeper_platform_driver = {
104	.driver	= {
105		.name		= BEEPER_MODNAME,
106		.of_match_table	= of_match_ptr(gpio_beeper_of_match),
 
107	},
108	.probe	= gpio_beeper_probe,
109};
110module_platform_driver(gpio_beeper_platform_driver);
111
112MODULE_LICENSE("GPL");
113MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
114MODULE_DESCRIPTION("Generic GPIO beeper driver");
v3.15
 
  1/*
  2 * Generic GPIO beeper driver
  3 *
  4 * Copyright (C) 2013 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/of_gpio.h>
 
 15#include <linux/workqueue.h>
 16#include <linux/platform_device.h>
 17
 18#define BEEPER_MODNAME		"gpio-beeper"
 19
 20struct gpio_beeper {
 21	struct work_struct	work;
 22	int			gpio;
 23	bool			active_low;
 24	bool			beeping;
 25};
 26
 27static void gpio_beeper_toggle(struct gpio_beeper *beep, bool on)
 28{
 29	gpio_set_value_cansleep(beep->gpio, on ^ beep->active_low);
 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	enum of_gpio_flags flags;
 69	struct input_dev *input;
 70	unsigned long gflags;
 71	int err;
 72
 73	beep = devm_kzalloc(&pdev->dev, sizeof(*beep), GFP_KERNEL);
 74	if (!beep)
 75		return -ENOMEM;
 76
 77	beep->gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
 78	if (!gpio_is_valid(beep->gpio))
 79		return beep->gpio;
 80
 81	input = devm_input_allocate_device(&pdev->dev);
 82	if (!input)
 83		return -ENOMEM;
 84
 85	INIT_WORK(&beep->work, gpio_beeper_work);
 86
 87	input->name		= pdev->name;
 88	input->id.bustype	= BUS_HOST;
 89	input->id.vendor	= 0x0001;
 90	input->id.product	= 0x0001;
 91	input->id.version	= 0x0100;
 92	input->close		= gpio_beeper_close;
 93	input->event		= gpio_beeper_event;
 94
 95	input_set_capability(input, EV_SND, SND_BELL);
 96
 97	beep->active_low = flags & OF_GPIO_ACTIVE_LOW;
 98	gflags = beep->active_low ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
 99
100	err = devm_gpio_request_one(&pdev->dev, beep->gpio, gflags, pdev->name);
101	if (err)
102		return err;
103
104	input_set_drvdata(input, beep);
105
106	return input_register_device(input);
107}
108
109static struct of_device_id gpio_beeper_of_match[] = {
 
110	{ .compatible = BEEPER_MODNAME, },
111	{ }
112};
113MODULE_DEVICE_TABLE(of, gpio_beeper_of_match);
 
114
115static struct platform_driver gpio_beeper_platform_driver = {
116	.driver	= {
117		.name		= BEEPER_MODNAME,
118		.owner		= THIS_MODULE,
119		.of_match_table	= gpio_beeper_of_match,
120	},
121	.probe	= gpio_beeper_probe,
122};
123module_platform_driver(gpio_beeper_platform_driver);
124
125MODULE_LICENSE("GPL");
126MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
127MODULE_DESCRIPTION("Generic GPIO beeper driver");