Skip to contentSkip to conversation

Add sound and GRUB beep tune support

PATCH Applied Ahmad Fatoum 2 participants 9 messages Jan 31, 2021 – Feb 8, 2021 Framebuffer layer

lore
Share & tools
lore
#0 lore raw

cover Add sound and GRUB beep tune support

I've been meaning to upstream pwm-beeper support for a piezobuzzer, but it would be a real shame if the obnoxious buzzing would delay the boot. So let me present asynchronous beeping support for barebox.

If you don't have the suitable hardware lying around you can synth your own tunes on the barebox sandbox shell as well.

The Imperial March:
beep 480 440 4 440 4 440 4 349 3 523 1 440 4 349 3 523 1 440 8 659 4 659 4 659 4 698 3 523 1 415 4 349 3 523 1 440 8

Rebel's sabotage:
beep -c

Cheers,
Ahmad Fatoum (7):
sandbox: migrate to SDL 2.0
drivers: add sound card driver support
sound: add basic synthesizers for PCM beeper use
sound: add SDL 2.0 sound driver
commands: add beep command
sound: add PWM beeper support
sound: add gpio-beeper support

 arch/sandbox/Kconfig                          |   3 +
 arch/sandbox/Makefile                         |   6 +-
 arch/sandbox/configs/sandbox_defconfig        |   1 +
 arch/sandbox/dts/sandbox.dts                  |   4 +
 .../sandbox/mach-sandbox/include/mach/linux.h |  22 ++-
 arch/sandbox/os/Makefile                      |   4 +-
 arch/sandbox/os/sdl.c                         | 184 +++++++++++-------
 commands/Kconfig                              |   7 +
 commands/Makefile                             |   1 +
 commands/beep.c                               |  99 ++++++++++
 drivers/Kconfig                               |   1 +
 drivers/Makefile                              |   1 +
 drivers/sound/Kconfig                         |  38 ++++
 drivers/sound/Makefile                        |   5 +
 drivers/sound/core.c                          | 121 ++++++++++++
 drivers/sound/gpio-beeper.c                   |  77 ++++++++
 drivers/sound/pwm-beeper.c                    | 124 ++++++++++++
 drivers/sound/sdl.c                           |  87 +++++++++
 drivers/sound/synth.c                         |  43 ++++
 drivers/video/Kconfig                         |   1 +
 drivers/video/sdl.c                           |  37 ++--
 include/linux/fixp-arith.h                    | 144 ++++++++++++++
 include/poller.h                              |   4 +
 include/pwm.h                                 |  33 ++++
 include/sound.h                               |  77 ++++++++
 25 files changed, 1033 insertions(+), 91 deletions(-)
 create mode 100644 commands/beep.c
 create mode 100644 drivers/sound/Kconfig
 create mode 100644 drivers/sound/Makefile
 create mode 100644 drivers/sound/core.c
 create mode 100644 drivers/sound/gpio-beeper.c
 create mode 100644 drivers/sound/pwm-beeper.c
 create mode 100644 drivers/sound/sdl.c
 create mode 100644 drivers/sound/synth.c
 create mode 100644 include/linux/fixp-arith.h
 create mode 100644 include/sound.h
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 1/7 sandbox: migrate to SDL 2.0

SDL 2.0 has been released more than 7 years ago and should be available everywhere. Replace barebox's SDL 1.2 with SDL 2.0 instead:

- better maintained

- At least on Debian, you can't install x86_64 and i386 libsdl-dev at the same time, which makes a quick use of CONFIG_SANDBOX_LINUX_I386 harder as the user needs to reinstall libraries. With SDL 2.0, it works

- SDL 2.0 has easier audio API, which will be used in a later commit

- Wayland support for sandbox video driver

Port to SDL 2.0 and as we touch everything anyway, rename the sdl_ functions to start with sdl_video_ to differentiate from upcoming sdl_sound_.

---
 arch/sandbox/Kconfig                          |   3 +
 arch/sandbox/Makefile                         |   6 +-
 .../sandbox/mach-sandbox/include/mach/linux.h |  17 +-
 arch/sandbox/os/Makefile                      |   4 +-
 arch/sandbox/os/sdl.c                         | 146 ++++++++++--------
 drivers/video/Kconfig                         |   1 +
 drivers/video/sdl.c                           |  37 +++--
 7 files changed, 120 insertions(+), 94 deletions(-)
arch/sandbox/Kconfig +3 -0
index d9fc0c947b02..deff7f56a025 100644
@@ -43,3 +43,6 @@ config 64BIT
 
 config SANDBOX_LINUX_I386
 	bool "32-bit x86 barebox" if CC_HAS_LINUX_I386_SUPPORT
+
+config SDL
+	bool
arch/sandbox/Makefile +3 -3
index 17f9a298d773..ea594944e4eb 100644
@@ -37,8 +37,8 @@ archprepare: maketools
 
 PHONY += maketools
 
-ifeq ($(CONFIG_DRIVER_VIDEO_SDL),y)
-SDL_LIBS := $(shell pkg-config sdl --libs)
+ifeq ($(CONFIG_SDL),y)
+SDL_LIBS := $(shell pkg-config sdl2 --libs)
 endif
 
 ifeq ($(CONFIG_GPIO_LIBFTDI1),y)
@@ -64,7 +64,7 @@ endif
 BAREBOX_LDFLAGS += \
 	-Wl,-T,$(BAREBOX_LDS) \
 	-Wl,--whole-archive $(BAREBOX_OBJS) -Wl,--no-whole-archive \
-	-lrt -lpthread $(SDL_LIBS) $(FTDI1_LIBS) \
+	-lrt $(SDL_LIBS) $(FTDI1_LIBS) \
 	$(SANITIZER_LIBS)
 
 cmd_barebox__ = $(CC) -o $@ $(BAREBOX_LDFLAGS)
arch/sandbox/mach-sandbox/include/mach/linux.h +10 -7
index b26bfc24a291..c636a9af9cd2 100644
@@ -38,13 +38,16 @@ struct linux_console_data {
 
 extern int sdl_xres;
 extern int sdl_yres;
-void sdl_close(void);
-int sdl_open(int xres, int yres, int bpp, void* buf);
-void sdl_stop_timer(void);
-void sdl_start_timer(void);
-void sdl_get_bitfield_rgba(struct fb_bitfield *r, struct fb_bitfield *g,
-			    struct fb_bitfield *b, struct fb_bitfield *a);
-void sdl_setpixel(int x, int y, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
+struct sdl_fb_info {
+	void *screen_base;
+	int xres;
+	int yres;
+	int bpp;
+	int rmask, gmask, bmask, amask;
+};
+int sdl_video_open(const struct sdl_fb_info *);
+void sdl_video_pause(void);
+void sdl_video_close(void);
 
 struct ft2232_bitbang;
 struct ft2232_bitbang *barebox_libftdi1_open(int vendor_id, int device_id,
arch/sandbox/os/Makefile +2 -2
index 15d688bfdd1b..575b1a213050 100644
@@ -17,8 +17,8 @@ endif
 obj-y = common.o tap.o
 obj-$(CONFIG_MALLOC_LIBC) += libc_malloc.o
 
-CFLAGS_sdl.o = $(shell pkg-config sdl --cflags)
-obj-$(CONFIG_DRIVER_VIDEO_SDL) += sdl.o
+CFLAGS_sdl.o = $(shell pkg-config sdl2 --cflags)
+obj-$(CONFIG_SDL) += sdl.o
 
 CFLAGS_ftdi.o = $(shell pkg-config libftdi1 --cflags)
 obj-$(CONFIG_GPIO_LIBFTDI1) += ftdi.o
arch/sandbox/os/sdl.c +78 -68

181 lines not on this page · raw message

drivers/video/Kconfig +1 -0
index 9ec6ea4248c1..b6d468c63c03 100644
@@ -82,6 +82,7 @@ endif
 config DRIVER_VIDEO_SDL
 	bool "SDL framebuffer driver"
 	depends on SANDBOX
+	select SDL
 
 config DRIVER_VIDEO_PXA
 	bool "PXA27x framebuffer driver"
drivers/video/sdl.c +23 -14
index 9811b2cf128e..e9debc51b1e9 100644
@@ -13,23 +13,25 @@
 #include <errno.h>
 #include <gui/graphic_utils.h>
 
+#define to_mask(color) GENMASK(color.length - 1, color.offset)
+
 static void sdlfb_enable(struct fb_info *info)
 {
-	int ret;
-
-	ret = sdl_open(info->xres, info->yres, info->bits_per_pixel,
-		     info->screen_base);
-	if (ret)
-		return;
-	sdl_get_bitfield_rgba(&info->red, &info->green, &info->blue, &info->transp);
-
-	sdl_start_timer();
+	struct sdl_fb_info sdl_info = {
+		.screen_base = info->screen_base,
+		.xres = info->xres, .yres = info->yres, .bpp = info->bits_per_pixel,
+		.rmask = to_mask(info->red),
+		.gmask = to_mask(info->green),
+		.bmask = to_mask(info->blue),
+		.amask = to_mask(info->transp),
+	};
+
+	sdl_video_open(&sdl_info);
 }
 
 static void sdlfb_disable(struct fb_info *info)
 {
-	sdl_stop_timer();
-	sdl_close();
+	sdl_video_close();
 }
 
 static struct fb_ops sdlfb_ops = {
@@ -48,10 +50,19 @@ static int sdlfb_probe(struct device_d *dev)
 	fb = xzalloc(sizeof(*fb));
 	fb->modes.modes = fb->mode = dev->platform_data;
 	fb->modes.num_modes = 1;
-	fb->bits_per_pixel = 4 << 3;
 	fb->xres = fb->mode->xres;
 	fb->yres = fb->mode->yres;
 
+	fb->bits_per_pixel = 32;
+	fb->transp.length = 8;
+	fb->red.length = 8;
+	fb->green.length = 8;
+	fb->blue.length = 8;
+	fb->transp.offset = 24;
+	fb->red.offset = 16;
+	fb->green.offset = 8;
+	fb->blue.offset = 0;
+
 	fb->priv = fb;
 	fb->fbops = &sdlfb_ops;
 
@@ -68,7 +79,6 @@ static int sdlfb_probe(struct device_d *dev)
 
 	kfree(fb->screen_base);
 	kfree(fb);
-	sdl_close();
 	return ret;
 }
 
@@ -78,7 +88,6 @@ static void sdlfb_remove(struct device_d *dev)
 
 	kfree(fb->screen_base);
 	kfree(fb);
-	sdl_close();
 }
 
 static struct driver_d sdlfb_driver = {
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 2/7 drivers: add sound card driver support

In reply to Ahmad Fatoum

Add driver core boilerplate for sound support in barebox. Using the provided API in <sound.h>, consumers can play beeps for a fixed duration of time.

Playing beeps is not blocking and new beeps can be enqueued while one is already playing. They will be played in succession by a poller, which will also turn off the sound card when the beep tune is over.

API is also available for blocking until all beeps are played and for cancelling an underway beep tune.

The API could be later extended for arbitrary PCM audio, should the need arise.

---
 drivers/Kconfig        |   1 +
 drivers/Makefile       |   1 +
 drivers/sound/Kconfig  |   8 +++
 drivers/sound/Makefile |   2 +
 drivers/sound/core.c   | 121 +++++++++++++++++++++++++++++++++++++++++
 include/poller.h       |   4 ++
 include/sound.h        |  45 +++++++++++++++
 7 files changed, 182 insertions(+)
 create mode 100644 drivers/sound/Kconfig
 create mode 100644 drivers/sound/Makefile
 create mode 100644 drivers/sound/core.c
 create mode 100644 include/sound.h
drivers/Kconfig +1 -0
index dda240578067..0b87c2af2a83 100644
@@ -13,6 +13,7 @@ source "drivers/mtd/Kconfig"
 source "drivers/ata/Kconfig"
 source "drivers/usb/Kconfig"
 source "drivers/video/Kconfig"
+source "drivers/sound/Kconfig"
 source "drivers/mci/Kconfig"
 source "drivers/clk/Kconfig"
 source "drivers/clocksource/Kconfig"
drivers/Makefile +1 -0
index 5a03bdceab81..fab3790288f7 100644
@@ -43,3 +43,4 @@ obj-y	+= soc/imx/
 obj-y	+= nvme/
 obj-y	+= ddr/
 obj-y	+= power/
+obj-$(CONFIG_SOUND) += sound/
drivers/sound/Kconfig new file +8 -0
new file mode 100644
index 000000000000..cce5c760afdc
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+menuconfig SOUND
+	bool "Sound drivers"
+	select POLLER
+	help
+	  Say Y here for sound support. At the moment that's just beep tones.
+	  Tones are played asynchronously in a poller. Check the beep command
+	  for how to exercise the API.
drivers/sound/Makefile new file +2 -0
new file mode 100644
index 000000000000..e343e0fa72b0
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-y += core.o
drivers/sound/core.c new file +121 -0

121 lines not on this page · raw message

include/poller.h +4 -0
index 886557252b69..db773265b2f6 100644
@@ -34,6 +34,10 @@ int poller_async_unregister(struct poller_async *pa);
 int poller_call_async(struct poller_async *pa, uint64_t delay_ns,
 		void (*fn)(void *), void *ctx);
 int poller_async_cancel(struct poller_async *pa);
+static inline bool poller_async_active(struct poller_async *pa)
+{
+	return pa->active;
+}
 
 #ifdef CONFIG_POLLER
 void poller_call(void);
include/sound.h new file +45 -0

45 lines not on this page · raw message

Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH 3/7 sound: add basic synthesizers for PCM beeper use

In reply to Ahmad Fatoum

For beeping on PCM sound cards, barebox will need to synthesize samples. Add basic sine and square wave synthesizers to achieve this.

Client code can either call __synth_F to explicitly select synth F or synth_F, which depending on CONFIG_SYNTH_SQUARES may expand to either __synth_F or a gain-adjusted __synth_generate_square.

The latter is mainly useful for slow systems that can't synthesize enough sine samples in a poller without impacting boot performance.

---
 drivers/sound/Kconfig      |  13 ++++
 drivers/sound/Makefile     |   2 +-
 drivers/sound/synth.c      |  43 +++++++++++
 include/linux/fixp-arith.h | 144 +++++++++++++++++++++++++++++++++++++
 include/sound.h            |  32 +++++++++
 5 files changed, 233 insertions(+), 1 deletion(-)
 create mode 100644 drivers/sound/synth.c
 create mode 100644 include/linux/fixp-arith.h
drivers/sound/Kconfig +13 -0
index cce5c760afdc..d9f63a5f3c55 100644
@@ -6,3 +6,16 @@ menuconfig SOUND
 	  Say Y here for sound support. At the moment that's just beep tones.
 	  Tones are played asynchronously in a poller. Check the beep command
 	  for how to exercise the API.
+
+if SOUND
+
+config SYNTH_SQUARES
+	bool "Synthesize square waves only"
+	help
+	  For beeping on PCM sound cards, barebox needs to synthesize samples,
+	  which can take too much poller time for crisp playback and/or quick
+	  booting. If your playback stutters, say Y here. This will have all
+	  synthesizers output a gain-adjusted square wave instead, which is
+	  less time-consuming to compute.
+
+endif
drivers/sound/Makefile +1 -1
index e343e0fa72b0..69873faab985 100644
@@ -1,2 +1,2 @@
 # SPDX-License-Identifier: GPL-2.0-only
-obj-y += core.o
+obj-y += core.o synth.o
drivers/sound/synth.c new file +43 -0

43 lines not on this page · raw message

include/linux/fixp-arith.h new file +144 -0

144 lines not on this page · raw message

include/sound.h +32 -0
index f5124aebbda1..62fef106ee73 100644
@@ -42,4 +42,36 @@ static inline int beep_cancel(void)
 	return sound_card_beep_cancel(sound_card_get_default());
 }
 
+
+/*
+ * Synthesizers all have the same prototype, but their implementation
+ * is replaced with a gain-adjusted square wave if CONFIG_SYNTH_SQUARES=y.
+ * This is to support PCM beeping on systems, where sine generation may
+ * spend to much time in the poller.
+ */
+typedef void synth_t(unsigned freq, s16 amplitude, s16 *stream,
+		     unsigned sample_rate, unsigned nsamples);
+
+#ifdef CONFIG_SYNTH_SQUARES
+#define SYNTH(fn, volume_percent)                                \
+	static inline void fn(unsigned f, s16 amplitude, s16 *s, \
+			      unsigned r, unsigned n) {          \
+		synth_t __synth_square;                          \
+		amplitude = amplitude * volume_percent / 100;    \
+		__synth_square(f, amplitude, s, r, n);           \
+	}                                                        \
+	synth_t __##fn
+#else
+#define SYNTH(fn, volume_percent)                                \
+	static inline void fn(unsigned f, s16 a, s16 *s,         \
+			      unsigned r, unsigned n) {          \
+		synth_t __##fn;                                  \
+		__##fn(f, a, s, r, n);                           \
+	}                                                        \
+        synth_t __##fn
+#endif
+
+SYNTH(synth_square, 100);	/* square wave always has full amplitude */
+SYNTH(synth_sin, 64);		/* ∫₀¹ sin(πx) dx ≈ 64% */
+
 #endif
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

PATCH 4/7 sound: add SDL 2.0 sound driver

In reply to Ahmad Fatoum

SDL 2.0.4 features SDL_QueueAudio, which we could use to just queue buffers for audio playback. Use it to implement the new sound API for the sandbox architecture.

---
 arch/sandbox/configs/sandbox_defconfig        |  1 +
 arch/sandbox/dts/sandbox.dts                  |  4 +
 .../sandbox/mach-sandbox/include/mach/linux.h |  5 ++
 arch/sandbox/os/sdl.c                         | 44 ++++++++++
 drivers/sound/Kconfig                         |  5 ++
 drivers/sound/Makefile                        |  1 +
 drivers/sound/sdl.c                           | 87 +++++++++++++++++++
 7 files changed, 147 insertions(+)
 create mode 100644 drivers/sound/sdl.c
arch/sandbox/configs/sandbox_defconfig +1 -0
index ca24d81acac7..66fcfbdbd0a3 100644
@@ -107,6 +107,7 @@ CONFIG_MTD=y
 CONFIG_MTD_M25P80=y
 CONFIG_VIDEO=y
 CONFIG_FRAMEBUFFER_CONSOLE=y
+CONFIG_SOUND=y
 CONFIG_MFD_SYSCON=y
 CONFIG_STATE_DRV=y
 CONFIG_UBOOTVAR=y
arch/sandbox/dts/sandbox.dts +4 -0
index afe48154c488..ef1fa7b8661f 100644
@@ -94,4 +94,8 @@ barebox,sandbox-power
 		compatible = "barebox,sandbox-watchdog";
 		barebox,reset-source = <&stickypage 0>;
 	};
+
+	sound {
+		compatible = "barebox,sandbox-sound";
+	};
 };
arch/sandbox/mach-sandbox/include/mach/linux.h +5 -0
index c636a9af9cd2..6e10fdbe6d80 100644
@@ -49,6 +49,11 @@ int sdl_video_open(const struct sdl_fb_info *);
 void sdl_video_pause(void);
 void sdl_video_close(void);
 
+int sdl_sound_init(unsigned sample_rate);
+int sdl_sound_play(const void *data, unsigned nsamples);
+void sdl_sound_stop(void);
+void sdl_sound_close(void);
+
 struct ft2232_bitbang;
 struct ft2232_bitbang *barebox_libftdi1_open(int vendor_id, int device_id,
 						const char *serial);
arch/sandbox/os/sdl.c +44 -0

47 lines not on this page · raw message

drivers/sound/Kconfig +5 -0
index d9f63a5f3c55..889657305b0b 100644
@@ -9,6 +9,11 @@ menuconfig SOUND
 
 if SOUND
 
+config SOUND_SDL
+	bool "SDL sound driver for sandbox"
+	depends on SANDBOX && OFDEVICE
+	select SDL
+
 config SYNTH_SQUARES
 	bool "Synthesize square waves only"
 	help
drivers/sound/Makefile +1 -0
index 69873faab985..692105fd6b59 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y += core.o synth.o
+obj-$(CONFIG_SOUND_SDL)		+= sdl.o
drivers/sound/sdl.c new file +87 -0

87 lines not on this page · raw message

Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw

PATCH 5/7 commands: add beep command

In reply to Ahmad Fatoum

Add a beep command that's compatible with GRUB's

play "tempo pitch1 duration1..."

Unlike the GRUB command, playing a tune is not a blocking operating. For this reason barebox, additionally supports:
* -w: wait until tune is over
* -c: cancel playing tune

Additionally, beep tempo can be used to ring the bell at a frequency chosen by the sound card.

Examples:

# 1-up

  beep 1750 523 1 392 1 523 1 659 1 784 1 1047 1 784 1 415 1 523 \
	1 622 1 831 1 622 1 831 1 1046 1 1244 1 1661 1

# 1-second beep
beep 60

---
 commands/Kconfig  |  7 ++++
 commands/Makefile |  1 +
 commands/beep.c   | 99 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+)
 create mode 100644 commands/beep.c
commands/Kconfig +7 -0
index 03ddfc887074..b672f0c16a85 100644
@@ -1474,6 +1474,13 @@ config CMD_FBTEST
 	  Framebuffer test command that allows to produce a number of
 	  test patterns on a screen.
 
+config CMD_BEEP
+	def_bool y
+	depends on SOUND
+	prompt "Beep"
+	help
+	  Play beeps. Accepts same format as GRUB play
+
 config CMD_READLINE
 	tristate
 	prompt "readline"
commands/Makefile +1 -0
index dc285cd00e1b..034c0e6383d3 100644
@@ -58,6 +58,7 @@ obj-$(CONFIG_CMD_LSMOD)		+= lsmod.o
 obj-$(CONFIG_CMD_INSMOD)	+= insmod.o
 obj-$(CONFIG_CMD_SPLASH)	+= splash.o
 obj-$(CONFIG_CMD_FBTEST)	+= fbtest.o
+obj-$(CONFIG_CMD_BEEP)		+= beep.o
 obj-$(CONFIG_USB_GADGET_DFU)	+= dfu.o
 obj-$(CONFIG_USB_GADGET_SERIAL)	+= usbserial.o
 obj-$(CONFIG_CMD_GPIO)		+= gpio.o
commands/beep.c new file +99 -0

99 lines not on this page · raw message

Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw

PATCH 6/7 sound: add PWM beeper support

In reply to Ahmad Fatoum

This driver can be used to drive a piezo-buzzer attached to a PWM.

---
 drivers/sound/Kconfig      |   6 ++
 drivers/sound/Makefile     |   1 +
 drivers/sound/pwm-beeper.c | 124 +++++++++++++++++++++++++++++++++++++
 include/pwm.h              |  33 ++++++++++
 4 files changed, 164 insertions(+)
 create mode 100644 drivers/sound/pwm-beeper.c
drivers/sound/Kconfig +6 -0
index 889657305b0b..9b7bbd7e7a33 100644
@@ -14,6 +14,12 @@ config SOUND_SDL
 	depends on SANDBOX && OFDEVICE
 	select SDL
 
+config PWM_BEEPER
+	bool "PWM beeper support"
+	depends on PWM && OFDEVICE
+	help
+	  Say Y here to get support for PWM based beeper devices.
+
 config SYNTH_SQUARES
 	bool "Synthesize square waves only"
 	help
drivers/sound/Makefile +1 -0
index 692105fd6b59..468e5bee838d 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y += core.o synth.o
 obj-$(CONFIG_SOUND_SDL)		+= sdl.o
+obj-$(CONFIG_PWM_BEEPER) 	+= pwm-beeper.o
drivers/sound/pwm-beeper.c new file +124 -0

124 lines not on this page · raw message

include/pwm.h +33 -0
index b67ab13d2e2d..2bd59fb8d3b6 100644
@@ -3,6 +3,7 @@
 #define __PWM_H
 
 #include <dt-bindings/pwm/pwm.h>
+#include <errno.h>
 
 struct pwm_device;
 struct device_d;
@@ -63,6 +64,38 @@ void pwm_disable(struct pwm_device *pwm);
 
 unsigned int pwm_get_period(struct pwm_device *pwm);
 
+/**
+ * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
+ * @state: PWM state to fill
+ * @duty_cycle: relative duty cycle value
+ * @scale: scale in which @duty_cycle is expressed
+ *
+ * This functions converts a relative into an absolute duty cycle (expressed
+ * in nanoseconds), and puts the result in state->duty_cycle.
+ *
+ * For example if you want to configure a 50% duty cycle, call:
+ *
+ * pwm_init_state(pwm, &state);
+ * pwm_set_relative_duty_cycle(&state, 50, 100);
+ * pwm_apply_state(pwm, &state);
+ *
+ * This functions returns -EINVAL if @duty_cycle and/or @scale are
+ * inconsistent (@scale == 0 or @duty_cycle > @scale).
+ */
+static inline int
+pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
+			    unsigned int scale)
+{
+	if (!scale || duty_cycle > scale)
+		return -EINVAL;
+
+	state->duty_ns = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
+					       state->period_ns,
+					       scale);
+
+	return 0;
+}
+
 struct pwm_chip;
 
 /**
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#7 lore raw

PATCH 7/7 sound: add gpio-beeper support

In reply to Ahmad Fatoum

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.

---
 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
drivers/sound/Kconfig +6 -0
index 9b7bbd7e7a33..bf6f715200e0 100644
@@ -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
drivers/sound/Makefile +1 -0
index 468e5bee838d..57d9cbd332f7 100644
@@ -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
drivers/sound/gpio-beeper.c new file +77 -0

77 lines not on this page · raw message

Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#8 lore raw
In reply to Ahmad Fatoum
Quoted 13 lines · Ahmad Fatoum

I've been meaning to upstream pwm-beeper support for a piezobuzzer, but it would be a real shame if the obnoxious buzzing would delay the boot. So let me present asynchronous beeping support for barebox.

If you don't have the suitable hardware lying around you can synth your own tunes on the barebox sandbox shell as well.

The Imperial March:
beep 480 440 4 440 4 440 4 349 3 523 1 440 4 349 3 523 1 440 8 659 4 659 4 659 4 698 3 523 1 415 4 349 3 523 1 440 8

Rebel's sabotage:
beep -c

Quoted 9 lines

Cheers,
Ahmad Fatoum (7):
sandbox: migrate to SDL 2.0
drivers: add sound card driver support
sound: add basic synthesizers for PCM beeper use
sound: add SDL 2.0 sound driver
commands: add beep command
sound: add PWM beeper support
sound: add gpio-beeper support

Finally! I've been waiting for that for so long ;)

Applied, thanks

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox