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

* [PATCH 2/4] param: add support for setting parameters with percentages
  2025-10-27  6:30 [PATCH 1/4] sound: pwm-beeper: make duty cycle configurable Ahmad Fatoum
@ 2025-10-27  6:30 ` Ahmad Fatoum
  2025-10-27  6:30 ` [PATCH 3/4] video: backlight-pwm: switch to gpiod functions Ahmad Fatoum
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2025-10-27  6:30 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 include/param.h |  8 ++++++++
 lib/parameter.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/include/param.h b/include/param.h
index a15e064ddec2..59aa8a3a385f 100644
--- a/include/param.h
+++ b/include/param.h
@@ -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)
diff --git a/lib/parameter.c b/lib/parameter.c
index 274e6fcb8376..f36d77d119fa 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -354,6 +354,7 @@ struct param_int {
 	const char *format;
 	int (*set)(struct param_d *p, void *priv);
 	int (*get)(struct param_d *p, void *priv);
+	u64 scale_max;
 };
 
 static inline struct param_int *to_param_int(struct param_d *p)
@@ -403,6 +404,33 @@ static int param_int_set(struct bobject *bobj, struct param_d *p,
 	return ret;
 }
 
+static int param_int_set_scaled(struct bobject *bobj, struct param_d *p,
+				const char *val)
+{
+	char buf[sizeof("18446744073709551615")];
+	struct param_int *pi = to_param_int(p);
+	s64 scaled;
+	char *end;
+
+	if (!isempty(val) && val[strlen(val) - 1] == '%') {
+		u64 percent = div_u64(pi->scale_max, 100);
+
+		scaled = simple_strtofract(val, &end, percent);
+		if (val == end || *end != '%' ||
+		    scaled < 0 || scaled > percent * 100)
+			return -EINVAL;
+
+		/* saturate at 100% */
+		if (scaled == percent * 100)
+			scaled = pi->scale_max;
+
+		snprintf(buf, sizeof(buf), pi->format, scaled);
+		val = buf;
+	}
+
+	return param_int_set(bobj, p, val);
+}
+
 static const char *param_int_get(struct bobject *bobj, struct param_d *p)
 {
 	struct param_int *pi = to_param_int(p);
@@ -432,6 +460,24 @@ static const char *param_int_get(struct bobject *bobj, struct param_d *p)
 	return p->value;
 }
 
+static void param_int_max_info(struct param_d *p)
+{
+	struct param_int *pi = to_param_int(p);
+	printf(" (maximum: %llu)", pi->scale_max);
+}
+
+int param_int_set_scale(struct param_d *p, uint64_t max)
+{
+	if (IS_ERR(p))
+		return PTR_ERR(p);
+
+	p->info = param_int_max_info;
+	p->set = param_int_set_scaled;
+	to_param_int(p)->scale_max = max;
+
+	return 0;
+}
+
 int param_set_readonly(struct param_d *p, void *priv)
 {
 	return -EROFS;
-- 
2.47.3




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

* [PATCH 3/4] video: backlight-pwm: switch to gpiod functions
  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 ` 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
  3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2025-10-27  6:30 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/video/backlight-pwm.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

diff --git a/drivers/video/backlight-pwm.c b/drivers/video/backlight-pwm.c
index 87358ca778ec..d3c81114e006 100644
--- a/drivers/video/backlight-pwm.c
+++ b/drivers/video/backlight-pwm.c
@@ -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;
 }
-- 
2.47.3




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

* [PATCH 4/4] backlight: support setting brightness as percentage
  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 ` Ahmad Fatoum
  2025-10-28  8:19 ` [PATCH 1/4] sound: pwm-beeper: make duty cycle configurable Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2025-10-27  6:30 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

From: Ahmad Fatoum <ahmad@a3f.at>

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.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/video/backlight.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/video/backlight.c b/drivers/video/backlight.c
index 51d245a80fdb..6d8146ee5aa0 100644
--- a/drivers/video/backlight.c
+++ b/drivers/video/backlight.c
@@ -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);
 
-- 
2.47.3




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

* Re: [PATCH 1/4] sound: pwm-beeper: make duty cycle configurable
  2025-10-27  6:30 [PATCH 1/4] sound: pwm-beeper: make duty cycle configurable Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2025-10-27  6:30 ` [PATCH 4/4] backlight: support setting brightness as percentage Ahmad Fatoum
@ 2025-10-28  8:19 ` Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-10-28  8:19 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Mon, 27 Oct 2025 07:30:36 +0100, Ahmad Fatoum wrote:
> 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,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ 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