From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 1/4] sound: pwm-beeper: make duty cycle configurable
Date: Mon, 27 Oct 2025 07:30:36 +0100 [thread overview]
Message-ID: <20251027063045.2396578-1-a.fatoum@barebox.org> (raw)
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
next reply other threads:[~2025-10-27 6:31 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-27 6:30 Ahmad Fatoum [this message]
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
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=20251027063045.2396578-1-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--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