mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <ahmad@a3f.at>
To: barebox@lists.infradead.org
Subject: [PATCH 7/7] sound: add gpio-beeper support
Date: Sun, 31 Jan 2021 21:18:46 +0100	[thread overview]
Message-ID: <20210131201846.3440477-8-ahmad@a3f.at> (raw)
In-Reply-To: <20210131201846.3440477-1-ahmad@a3f.at>

Add support for simple gpio-beepers.

Note that unlike with PWM buzzers, GPIO buzzers can't be controlled
in frequency and thus it doesn't make much sense to use the multiple
argument version of beep with them.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 drivers/sound/Kconfig       |  6 +++
 drivers/sound/Makefile      |  1 +
 drivers/sound/gpio-beeper.c | 77 +++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+)
 create mode 100644 drivers/sound/gpio-beeper.c

diff --git a/drivers/sound/Kconfig b/drivers/sound/Kconfig
index 9b7bbd7e7a33..bf6f715200e0 100644
--- a/drivers/sound/Kconfig
+++ b/drivers/sound/Kconfig
@@ -20,6 +20,12 @@ config PWM_BEEPER
 	help
 	  Say Y here to get support for PWM based beeper devices.
 
+config GPIO_BEEPER
+	bool "GPIO beeper support"
+	depends on GPIOLIB && OFDEVICE
+	help
+	  Say Y here to get support for GPIO based beeper devices.
+
 config SYNTH_SQUARES
 	bool "Synthesize square waves only"
 	help
diff --git a/drivers/sound/Makefile b/drivers/sound/Makefile
index 468e5bee838d..57d9cbd332f7 100644
--- a/drivers/sound/Makefile
+++ b/drivers/sound/Makefile
@@ -2,3 +2,4 @@
 obj-y += core.o synth.o
 obj-$(CONFIG_SOUND_SDL)		+= sdl.o
 obj-$(CONFIG_PWM_BEEPER) 	+= pwm-beeper.o
+obj-$(CONFIG_GPIO_BEEPER)	+= gpio-beeper.o
diff --git a/drivers/sound/gpio-beeper.c b/drivers/sound/gpio-beeper.c
new file mode 100644
index 000000000000..86fd4a4ee67c
--- /dev/null
+++ b/drivers/sound/gpio-beeper.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *  Copyright (C) 2021, Ahmad Fatoum
+ */
+
+#include <common.h>
+#include <regulator.h>
+#include <sound.h>
+#include <of.h>
+#include <gpio.h>
+#include <of_gpio.h>
+
+struct gpio_beeper {
+	int gpio;
+	struct sound_card card;
+};
+
+static int gpio_beeper_beep(struct sound_card *card, unsigned freq, unsigned duration)
+{
+	struct gpio_beeper *beeper = container_of(card, struct gpio_beeper, card);
+
+	gpio_set_active(beeper->gpio, freq);
+	return 0;
+}
+
+static int gpio_beeper_probe(struct device_d *dev)
+{
+	struct device_node *np = dev->device_node;
+	struct gpio_beeper *beeper;
+	struct sound_card *card;
+	enum of_gpio_flags of_flags;
+	unsigned long gpio_flags = GPIOF_OUT_INIT_ACTIVE;
+	int ret, gpio;
+
+	gpio = of_get_named_gpio_flags(np, "gpios", 0, &of_flags);
+	if (!gpio_is_valid(gpio))
+		return gpio;
+
+	if (of_flags & OF_GPIO_ACTIVE_LOW)
+		gpio_flags |= GPIOF_ACTIVE_LOW;
+
+	ret = gpio_request_one(gpio, gpio_flags, "gpio-beeper");
+	if (ret) {
+		dev_err(dev, "failed to request gpio %d: %d\n", gpio, ret);
+		return ret;
+	}
+
+	beeper = xzalloc(sizeof(*beeper));
+	beeper->gpio = gpio;
+	dev->priv = beeper;
+
+	card = &beeper->card;
+	card->name = np->full_name;
+	card->beep = gpio_beeper_beep;
+
+	return sound_card_register(card);
+}
+
+static void gpio_beeper_suspend(struct device_d *dev)
+{
+	struct gpio_beeper *beeper = dev->priv;
+
+	gpio_beeper_beep(&beeper->card, 0, 0);
+}
+
+static const struct of_device_id gpio_beeper_match[] = {
+	{ .compatible = "gpio-beeper", },
+	{ },
+};
+
+static struct driver_d gpio_beeper_driver = {
+	.name		= "gpio-beeper",
+	.probe		= gpio_beeper_probe,
+	.remove		= gpio_beeper_suspend,
+	.of_compatible	= gpio_beeper_match,
+};
+device_platform_driver(gpio_beeper_driver);
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2021-01-31 20:18 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-31 20:18 [PATCH 0/7] Add sound and GRUB beep tune support Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 1/7] sandbox: migrate to SDL 2.0 Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 2/7] drivers: add sound card driver support Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 3/7] sound: add basic synthesizers for PCM beeper use Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 4/7] sound: add SDL 2.0 sound driver Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 5/7] commands: add beep command Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 6/7] sound: add PWM beeper support Ahmad Fatoum
2021-01-31 20:18 ` Ahmad Fatoum [this message]
2021-02-08 10:42 ` [PATCH 0/7] Add sound and GRUB beep tune support Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210131201846.3440477-8-ahmad@a3f.at \
    --to=ahmad@a3f.at \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox