[RFC 2/7] regmap: check for alignment on translated register addresses

From: Maxime Chevallier
Date: Fri Mar 24 2023 - 05:37:19 EST


With regmap->reg_base and regmap->reg_downshift, the actual register
address that is going to be used for the next operation might not be the
same as the one we have as an input. Addresses can be offset with
reg_base and shifted, which will affect alignment.

Check for alignment on the real register address.

Signed-off-by: Maxime Chevallier <maxime.chevallier@xxxxxxxxxxx>
---
drivers/base/regmap/regmap.c | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index a4e4367648bf..726f59612fd6 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -2016,7 +2016,7 @@ int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
{
int ret;

- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;

map->lock(map->lock_arg);
@@ -2043,7 +2043,7 @@ int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
{
int ret;

- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;

map->lock(map->lock_arg);
@@ -2258,7 +2258,7 @@ int regmap_noinc_write(struct regmap *map, unsigned int reg,
return -EINVAL;
if (val_len % map->format.val_bytes)
return -EINVAL;
- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;
if (val_len == 0)
return -EINVAL;
@@ -2399,7 +2399,7 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
int ret = 0, i;
size_t val_bytes = map->format.val_bytes;

- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;

/*
@@ -2638,7 +2638,7 @@ static int _regmap_multi_reg_write(struct regmap *map,
int reg = regs[i].reg;
if (!map->writeable_reg(map->dev, reg))
return -EINVAL;
- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;
}

@@ -2789,7 +2789,7 @@ int regmap_raw_write_async(struct regmap *map, unsigned int reg,

if (val_len % map->format.val_bytes)
return -EINVAL;
- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;

map->lock(map->lock_arg);
@@ -2911,7 +2911,7 @@ int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
{
int ret;

- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;

map->lock(map->lock_arg);
@@ -2945,7 +2945,7 @@ int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,

if (val_len % map->format.val_bytes)
return -EINVAL;
- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;
if (val_count == 0)
return -EINVAL;
@@ -3040,7 +3040,7 @@ int regmap_noinc_read(struct regmap *map, unsigned int reg,

if (val_len % map->format.val_bytes)
return -EINVAL;
- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;
if (val_len == 0)
return -EINVAL;
@@ -3162,7 +3162,7 @@ int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
size_t val_bytes = map->format.val_bytes;
bool vol = regmap_volatile_range(map, reg, val_count);

- if (!IS_ALIGNED(reg, map->reg_stride))
+ if (!IS_ALIGNED(regmap_reg_addr(map, reg), map->reg_stride))
return -EINVAL;
if (val_count == 0)
return -EINVAL;
--
2.39.2