Linux Audio

Check our new training course

Loading...
v4.6
  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
 21struct gpio_beeper {
 22	struct work_struct	work;
 23	struct gpio_desc	*desc;
 24	bool			beeping;
 25};
 26
 27static void gpio_beeper_toggle(struct gpio_beeper *beep, bool on)
 28{
 29	gpiod_set_value_cansleep(beep->desc, on);
 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	struct input_dev *input;
 69
 70	beep = devm_kzalloc(&pdev->dev, sizeof(*beep), GFP_KERNEL);
 71	if (!beep)
 72		return -ENOMEM;
 73
 74	beep->desc = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
 75	if (IS_ERR(beep->desc))
 76		return PTR_ERR(beep->desc);
 77
 78	input = devm_input_allocate_device(&pdev->dev);
 79	if (!input)
 80		return -ENOMEM;
 81
 82	INIT_WORK(&beep->work, gpio_beeper_work);
 83
 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;
 91
 92	input_set_capability(input, EV_SND, SND_BELL);
 93
 94	input_set_drvdata(input, beep);
 95
 96	return input_register_device(input);
 97}
 98
 99#ifdef CONFIG_OF
100static const struct of_device_id gpio_beeper_of_match[] = {
101	{ .compatible = BEEPER_MODNAME, },
102	{ }
103};
104MODULE_DEVICE_TABLE(of, gpio_beeper_of_match);
105#endif
106
107static struct platform_driver gpio_beeper_platform_driver = {
108	.driver	= {
109		.name		= BEEPER_MODNAME,
110		.of_match_table	= of_match_ptr(gpio_beeper_of_match),
111	},
112	.probe	= gpio_beeper_probe,
113};
114module_platform_driver(gpio_beeper_platform_driver);
115
116MODULE_LICENSE("GPL");
117MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
118MODULE_DESCRIPTION("Generic GPIO beeper driver");
v4.10.11
  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
 21struct gpio_beeper {
 22	struct work_struct	work;
 23	struct gpio_desc	*desc;
 24	bool			beeping;
 25};
 26
 27static void gpio_beeper_toggle(struct gpio_beeper *beep, bool on)
 28{
 29	gpiod_set_value_cansleep(beep->desc, on);
 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	struct input_dev *input;
 69
 70	beep = devm_kzalloc(&pdev->dev, sizeof(*beep), GFP_KERNEL);
 71	if (!beep)
 72		return -ENOMEM;
 73
 74	beep->desc = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
 75	if (IS_ERR(beep->desc))
 76		return PTR_ERR(beep->desc);
 77
 78	input = devm_input_allocate_device(&pdev->dev);
 79	if (!input)
 80		return -ENOMEM;
 81
 82	INIT_WORK(&beep->work, gpio_beeper_work);
 83
 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;
 91
 92	input_set_capability(input, EV_SND, SND_BELL);
 93
 94	input_set_drvdata(input, beep);
 95
 96	return input_register_device(input);
 97}
 98
 99#ifdef CONFIG_OF
100static const struct of_device_id gpio_beeper_of_match[] = {
101	{ .compatible = BEEPER_MODNAME, },
102	{ }
103};
104MODULE_DEVICE_TABLE(of, gpio_beeper_of_match);
105#endif
106
107static struct platform_driver gpio_beeper_platform_driver = {
108	.driver	= {
109		.name		= BEEPER_MODNAME,
110		.of_match_table	= of_match_ptr(gpio_beeper_of_match),
111	},
112	.probe	= gpio_beeper_probe,
113};
114module_platform_driver(gpio_beeper_platform_driver);
115
116MODULE_LICENSE("GPL");
117MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
118MODULE_DESCRIPTION("Generic GPIO beeper driver");