Skip to contentSkip to conversation

sound: pwm-beeper: make duty cycle configurable

PATCH Applied Ahmad Fatoum 2 participants 5 messages Oct 27, 2025 – Oct 28, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 sound: pwm-beeper: make duty cycle configurable

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.

---
 drivers/sound/pwm-beeper.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/sound/pwm-beeper.c +22 -1
index 94b27359c1c3..5e29e953ddc8 100644
@@ -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);
 }
 
Signature · 1 line
2.47.3
#1 lore raw

PATCH 2/4 param: add support for setting parameters with percentages

Some parameters have a maximum that's below the maximum value allowed by the type, which would be convenient to set as percentage, e.g. volume of a sound card or brightness level of a backlight.

With the new simple_strtofract, we can easily accomplish this, so add support for that.

---
 include/param.h |  8 ++++++++
 lib/parameter.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
include/param.h +8 -0
index a15e064ddec2..59aa8a3a385f 100644
@@ -116,6 +116,8 @@ void param_remove(struct param_d *p);
 int bobject_param_set_generic(bobject_t bobj, struct param_d *p,
 			  const char *val);
 
+int param_int_set_scale(struct param_d *p, uint64_t max);
+
 #else
 static inline const char *bobject_get_param(bobject_t bobj, const char *name)
 {
@@ -251,6 +253,12 @@ static inline int bobject_param_set_generic(bobject_t bobj, struct param_d *p,
 {
 	return 0;
 }
+
+static inline int param_int_set_scale(struct param_d *p, uint64_t max)
+{
+	return 0;
+}
+
 #endif
 
 static inline const char *get_param_value(struct param_d *param)
lib/parameter.c +46 -0

64 lines not on this page · raw message

Signature · 1 line
2.47.3
#2 lore raw

PATCH 3/4 video: backlight-pwm: switch to gpiod functions

In reply to Ahmad Fatoum

Using GPIO descriptors aligns us more with the Linux customs nowadays and takes care of potential active low handling.

---
 drivers/video/backlight-pwm.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
drivers/video/backlight-pwm.c +8 -19
index 87358ca778ec..d3c81114e006 100644
@@ -12,8 +12,7 @@
 #include <linux/err.h>
 #include <of.h>
 #include <regulator.h>
-#include <gpio.h>
-#include <of_gpio.h>
+#include <linux/gpio/consumer.h>
 #include <linux/math64.h>
 
 struct pwm_backlight {
@@ -22,8 +21,7 @@ struct pwm_backlight {
 	struct regulator *power;
 	uint32_t period;
 	unsigned int *levels;
-	int enable_gpio;
-	int enable_active_high;
+	struct gpio_desc *enable_gpio;
 	int enabled;
 	unsigned int scale;
 };
@@ -41,10 +39,7 @@ static int backlight_pwm_enable(struct pwm_backlight *pwm_backlight)
 
 	regulator_enable(pwm_backlight->power);
 
-	if (gpio_is_valid(pwm_backlight->enable_gpio)) {
-		gpio_direction_output(pwm_backlight->enable_gpio,
-				pwm_backlight->enable_active_high);
-	}
+	gpiod_direction_output(pwm_backlight->enable_gpio, true);
 
 	pwm_backlight->enabled = 1;
 
@@ -53,13 +48,13 @@ static int backlight_pwm_enable(struct pwm_backlight *pwm_backlight)
 
 static int backlight_pwm_disable(struct pwm_backlight *pwm_backlight)
 {
+	int ret;
+
 	if (!pwm_backlight->enabled)
 		return 0;
 
-	if (gpio_is_valid(pwm_backlight->enable_gpio)) {
-		gpio_direction_output(pwm_backlight->enable_gpio,
-				!pwm_backlight->enable_active_high);
-
+	ret = gpiod_direction_output(pwm_backlight->enable_gpio, false);
+	if (!ret) {
 		regulator_disable(pwm_backlight->power);
 
 		/*
@@ -109,7 +104,6 @@ static int pwm_backlight_parse_dt(struct device *dev,
 	int length;
 	u32 value;
 	int ret, i;
-	enum of_gpio_flags flags;
 
 	if (!node)
 		return -ENODEV;
@@ -154,12 +148,7 @@ static int pwm_backlight_parse_dt(struct device *dev,
 		pwm_backlight->backlight.brightness_max = pwm_backlight->scale;
 	}
 
-	pwm_backlight->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0, &flags);
-
-	if (gpio_is_valid(pwm_backlight->enable_gpio)) {
-		if (!(flags & OF_GPIO_ACTIVE_LOW))
-			pwm_backlight->enable_active_high = 1;
-	}
+	pwm_backlight->enable_gpio = gpiod_get_optional(dev, "enable-gpios", 0);
 
 	return 0;
 }
Signature · 1 line
2.47.3
#3 lore raw

PATCH 4/4 backlight: support setting brightness as percentage

In reply to Ahmad Fatoum

From: Ahmad Fatoum <[email protected]>

Unlike the PWN duty cycle, we already have a parameter for the brightness, so we can't retroactively redefine it to be in terms of percentages.

By setting the scale, we allow users to enter percentages between 0% and 100% over the same parameter as well.

---
 drivers/video/backlight.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/video/backlight.c +5 -2
index 51d245a80fdb..6d8146ee5aa0 100644
@@ -69,6 +69,7 @@ static int backlight_brightness_set(struct param_d *p, void *priv)
 
 int backlight_register(struct backlight_device *bl)
 {
+	struct param_d *param;
 	int ret;
 
 	dev_set_name(&bl->dev, "backlight");
@@ -78,8 +79,10 @@ int backlight_register(struct backlight_device *bl)
 	if (ret)
 		return ret;
 
-	dev_add_param_uint32(&bl->dev, "brightness", backlight_brightness_set,
-			NULL, &bl->brightness, "%u", bl);
+	param = dev_add_param_uint32(&bl->dev, "brightness", backlight_brightness_set,
+				     NULL, &bl->brightness, "%u", bl);
+	param_int_set_scale(param, bl->brightness_max);
+
 	dev_add_param_uint32(&bl->dev, "slew_time_ms", NULL, NULL,
 			     &bl->slew_time_ms, "%u", NULL);
 
Signature · 1 line
2.47.3
Applied
#4 lore raw
In reply to Ahmad Fatoum
Quoted 5 lines · 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.

Applied, thanks!

[1/4] sound: pwm-beeper: make duty cycle configurable
https://git.pengutronix.de/cgit/barebox/commit/?id=a1e1f274c4e3 (link may not be stable)
[2/4] param: add support for setting parameters with percentages
https://git.pengutronix.de/cgit/barebox/commit/?id=4c837b2e1417 (link may not be stable)
[3/4] video: backlight-pwm: switch to gpiod functions
https://git.pengutronix.de/cgit/barebox/commit/?id=4c7238df6866 (link may not be stable)
[4/4] backlight: support setting brightness as percentage
https://git.pengutronix.de/cgit/barebox/commit/?id=e6cc85dc1de1 (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>