mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/4] sound: pwm-beeper: make duty cycle configurable
@ 2025-10-27  6:30 Ahmad Fatoum
  2025-10-27  6:30 ` [PATCH 2/4] param: add support for setting parameters with percentages Ahmad Fatoum
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2025-10-27  6:30 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

A lower duty cycle may be less jarring to hear, so give users the
possibility to control it via device parameter attached to the PWM
beeper device.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/sound/pwm-beeper.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/drivers/sound/pwm-beeper.c b/drivers/sound/pwm-beeper.c
index 94b27359c1c3..5e29e953ddc8 100644
--- a/drivers/sound/pwm-beeper.c
+++ b/drivers/sound/pwm-beeper.c
@@ -14,6 +14,7 @@ struct pwm_beeper {
 	struct pwm_device *pwm;
 	struct regulator *amplifier;
 	struct sound_card card;
+	u32 duty_cycle;
 };
 
 #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
@@ -33,7 +34,7 @@ static int pwm_beeper_beep(struct sound_card *card, unsigned freq, unsigned dura
 
 	state.enabled = true;
 	state.period = HZ_TO_NANOSECONDS(freq);
-	pwm_set_relative_duty_cycle(&state, 50, 100);
+	pwm_set_relative_duty_cycle(&state, beeper->duty_cycle, 100);
 
 	error = pwm_apply_state(beeper->pwm, &state);
 	if (error)
@@ -49,6 +50,22 @@ static int pwm_beeper_beep(struct sound_card *card, unsigned freq, unsigned dura
 	return error;
 }
 
+static int pwm_beeper_apply_params(struct param_d *p, void *priv)
+{
+	struct pwm_beeper *beeper = priv;
+	struct pwm_state state;
+
+	if (beeper->duty_cycle > 100)
+	    return -EINVAL;
+
+	pwm_get_state(beeper->pwm, &state);
+	if (!state.enabled)
+		return 0;
+
+	pwm_set_relative_duty_cycle(&state, beeper->duty_cycle, 100);
+	return pwm_apply_state(beeper->pwm, &state);
+}
+
 static int pwm_beeper_probe(struct device *dev)
 {
 	struct pwm_beeper *beeper;
@@ -74,6 +91,7 @@ static int pwm_beeper_probe(struct device *dev)
 		return error;
 	}
 
+	beeper->duty_cycle = 50;
 	beeper->amplifier = regulator_get(dev, "amp");
 	if (IS_ERR(beeper->amplifier))
 		return dev_errp_probe(dev, beeper->amplifier, "getting 'amp' regulator\n");
@@ -91,6 +109,9 @@ static int pwm_beeper_probe(struct device *dev)
 	card->bell_frequency = bell_frequency;
 	card->beep = pwm_beeper_beep;
 
+	dev_add_param_uint32(dev, "duty_cycle", pwm_beeper_apply_params,
+			NULL, &beeper->duty_cycle, "%u%", beeper);
+
 	return sound_card_register(card);
 }
 
-- 
2.47.3




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-10-28  8:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-27  6:30 [PATCH 1/4] sound: pwm-beeper: make duty cycle configurable Ahmad Fatoum
2025-10-27  6:30 ` [PATCH 2/4] param: add support for setting parameters with percentages Ahmad Fatoum
2025-10-27  6:30 ` [PATCH 3/4] video: backlight-pwm: switch to gpiod functions Ahmad Fatoum
2025-10-27  6:30 ` [PATCH 4/4] backlight: support setting brightness as percentage Ahmad Fatoum
2025-10-28  8:19 ` [PATCH 1/4] sound: pwm-beeper: make duty cycle configurable Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox