mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] nvmem: core: Allow sub-register width nvmem_regmap_write()
@ 2026-05-25 11:45 Jonas Rebmann
  2026-05-26  5:39 ` Ahmad Fatoum
  2026-05-26  6:26 ` Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Jonas Rebmann @ 2026-05-25 11:45 UTC (permalink / raw)
  To: Sascha Hauer, open list:BAREBOX; +Cc: Ahmad Fatoum, Jonas Rebmann

From: Ahmad Fatoum <a.fatoum@pengutronix.de>

The previous implementation of nvmem_regmap_write() required the
supplied value to be a multiple of the register size defined by the
driver ("expect users to observe alignment").

This is however not respected by nvmem cell functions
nvmem_cell_prepare_write_buffer()/__nvmem_cell_entry_write() which
prepare the value buffer based on the "bits" supplied in the devicetree,
resulting in EINVAL errors if an nvmem cell with "bits" spanning less
bytes than val_bytes = DIV_ROUND_UP(config->val_bits, 8) of the nvmem
driver.

To resolve this, accept buffers shorter than val_bytes, and place them
correctly with regard to endianness.

Not-Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
Ahmad and me pair-programmed this on friday. Ahmad, can we have your
SoB?
---
 drivers/nvmem/regmap.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/drivers/nvmem/regmap.c b/drivers/nvmem/regmap.c
index 98e57909eb..22e667af17 100644
--- a/drivers/nvmem/regmap.c
+++ b/drivers/nvmem/regmap.c
@@ -12,16 +12,31 @@
 static int nvmem_regmap_write(void *ctx, unsigned offset, const void *val, size_t bytes)
 {
 	struct regmap *map = ctx;
+	unsigned int tmp;
 
-	/*
-	 * eFuse writes going through this function may be irreversible,
-	 * so expect users to observe alignment.
-	 */
-	if (bytes % regmap_get_val_bytes(map))
+	if (bytes > regmap_get_val_bytes(map)) {
+		if (bytes % regmap_get_val_bytes(map))
+			return -EINVAL;
+
+		return regmap_bulk_write(map, offset, val,
+					 bytes / regmap_get_val_bytes(map));
+	}
+
+	switch (bytes) {
+	case 1:
+		tmp = *(u8 *)val;
+		break;
+	case 2:
+		tmp = *(u16 *)val;
+		break;
+	case 4:
+		tmp = *(u32 *)val;
+		break;
+	default:
 		return -EINVAL;
+	}
 
-	return regmap_bulk_write(map, offset, val,
-				 bytes / regmap_get_val_bytes(map));
+	return regmap_write(map, offset, tmp);
 }
 
 static int nvmem_regmap_read(void *ctx, unsigned offset, void *buf, size_t bytes)

---
base-commit: f5956c772dc00837bad36fc66df8a53aae86558d
change-id: 20260525-short_nvmem_write-86a2114f6774

Best regards,
--  
Jonas Rebmann <jre@pengutronix.de>




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

* Re: [PATCH] nvmem: core: Allow sub-register width nvmem_regmap_write()
  2026-05-25 11:45 [PATCH] nvmem: core: Allow sub-register width nvmem_regmap_write() Jonas Rebmann
@ 2026-05-26  5:39 ` Ahmad Fatoum
  2026-05-26  6:26 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2026-05-26  5:39 UTC (permalink / raw)
  To: Jonas Rebmann, Sascha Hauer, open list:BAREBOX

On 5/25/26 13:45, Jonas Rebmann wrote:
> From: Ahmad Fatoum <a.fatoum@pengutronix.de>
> 
> The previous implementation of nvmem_regmap_write() required the
> supplied value to be a multiple of the register size defined by the
> driver ("expect users to observe alignment").
> 
> This is however not respected by nvmem cell functions
> nvmem_cell_prepare_write_buffer()/__nvmem_cell_entry_write() which
> prepare the value buffer based on the "bits" supplied in the devicetree,
> resulting in EINVAL errors if an nvmem cell with "bits" spanning less
> bytes than val_bytes = DIV_ROUND_UP(config->val_bits, 8) of the nvmem
> driver.
> 
> To resolve this, accept buffers shorter than val_bytes, and place them
> correctly with regard to endianness.
> 
> Not-Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
> ---
> Ahmad and me pair-programmed this on friday. Ahmad, can we have your
> SoB?

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

> ---
>  drivers/nvmem/regmap.c | 29 ++++++++++++++++++++++-------
>  1 file changed, 22 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/nvmem/regmap.c b/drivers/nvmem/regmap.c
> index 98e57909eb..22e667af17 100644
> --- a/drivers/nvmem/regmap.c
> +++ b/drivers/nvmem/regmap.c
> @@ -12,16 +12,31 @@
>  static int nvmem_regmap_write(void *ctx, unsigned offset, const void *val, size_t bytes)
>  {
>  	struct regmap *map = ctx;
> +	unsigned int tmp;
>  
> -	/*
> -	 * eFuse writes going through this function may be irreversible,
> -	 * so expect users to observe alignment.
> -	 */
> -	if (bytes % regmap_get_val_bytes(map))
> +	if (bytes > regmap_get_val_bytes(map)) {
> +		if (bytes % regmap_get_val_bytes(map))
> +			return -EINVAL;
> +
> +		return regmap_bulk_write(map, offset, val,
> +					 bytes / regmap_get_val_bytes(map));
> +	}
> +
> +	switch (bytes) {
> +	case 1:
> +		tmp = *(u8 *)val;
> +		break;
> +	case 2:
> +		tmp = *(u16 *)val;
> +		break;
> +	case 4:
> +		tmp = *(u32 *)val;
> +		break;
> +	default:
>  		return -EINVAL;
> +	}
>  
> -	return regmap_bulk_write(map, offset, val,
> -				 bytes / regmap_get_val_bytes(map));
> +	return regmap_write(map, offset, tmp);
>  }
>  
>  static int nvmem_regmap_read(void *ctx, unsigned offset, void *buf, size_t bytes)
> 
> ---
> base-commit: f5956c772dc00837bad36fc66df8a53aae86558d
> change-id: 20260525-short_nvmem_write-86a2114f6774
> 
> Best regards,
> --  
> Jonas Rebmann <jre@pengutronix.de>
> 
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



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

* Re: [PATCH] nvmem: core: Allow sub-register width nvmem_regmap_write()
  2026-05-25 11:45 [PATCH] nvmem: core: Allow sub-register width nvmem_regmap_write() Jonas Rebmann
  2026-05-26  5:39 ` Ahmad Fatoum
@ 2026-05-26  6:26 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2026-05-26  6:26 UTC (permalink / raw)
  To: open list:BAREBOX, Jonas Rebmann; +Cc: Ahmad Fatoum


On Mon, 25 May 2026 13:45:58 +0200, Jonas Rebmann wrote:
> The previous implementation of nvmem_regmap_write() required the
> supplied value to be a multiple of the register size defined by the
> driver ("expect users to observe alignment").
> 
> This is however not respected by nvmem cell functions
> nvmem_cell_prepare_write_buffer()/__nvmem_cell_entry_write() which
> prepare the value buffer based on the "bits" supplied in the devicetree,
> resulting in EINVAL errors if an nvmem cell with "bits" spanning less
> bytes than val_bytes = DIV_ROUND_UP(config->val_bits, 8) of the nvmem
> driver.
> 
> [...]

Applied, thanks!

[1/1] nvmem: core: Allow sub-register width nvmem_regmap_write()
      https://git.pengutronix.de/cgit/barebox/commit/?id=1d6e3066cf7f (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




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

end of thread, other threads:[~2026-05-26  6:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-25 11:45 [PATCH] nvmem: core: Allow sub-register width nvmem_regmap_write() Jonas Rebmann
2026-05-26  5:39 ` Ahmad Fatoum
2026-05-26  6:26 ` Sascha Hauer

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