mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 2/4] param: add support for setting parameters with percentages
Date: Mon, 27 Oct 2025 07:30:37 +0100	[thread overview]
Message-ID: <20251027063045.2396578-2-a.fatoum@barebox.org> (raw)
In-Reply-To: <20251027063045.2396578-1-a.fatoum@barebox.org>

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




  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 [PATCH 1/4] sound: pwm-beeper: make duty cycle configurable Ahmad Fatoum
2025-10-27  6:30 ` Ahmad Fatoum [this message]
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-2-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