[PATCH 4/5] nvmem: add nvmem_regmap_register helper

Ahmad Fatoum a.fatoum at pengutronix.de
Mon May 31 00:24:05 PDT 2021


Registering a nvmem device for a regmap involves some boilerplate that
doesn't need to change from driver to driver:

  - Reads are made aligned, to support normal use with md
  - Writes are passed along as is
  - nvmem parameters can be extracted from regmap

Instead of having to replicate this driver by driver, add one helper to
make this adaptation easier.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 drivers/base/regmap/regmap.c   |  5 +++
 drivers/nvmem/Makefile         |  2 +-
 drivers/nvmem/regmap.c         | 78 ++++++++++++++++++++++++++++++++++
 include/linux/nvmem-provider.h |  8 ++++
 include/regmap.h               |  1 +
 5 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 drivers/nvmem/regmap.c

diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 6613670263f6..1af0c15a7baf 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -132,6 +132,11 @@ struct regmap *dev_get_regmap(struct device_d *dev, const char *name)
 	return ERR_PTR(-ENOENT);
 }
 
+struct device_d *regmap_get_device(struct regmap *map)
+{
+	return map->dev;
+}
+
 /*
  * regmap_write - write a register in a map
  *
diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index 7101c5aca44c..617e3725a726 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -3,7 +3,7 @@
 #
 
 obj-$(CONFIG_NVMEM)		+= nvmem_core.o
-nvmem_core-y			:= core.o
+nvmem_core-y			:= core.o regmap.o
 
 # Devices
 obj-$(CONFIG_NVMEM_SNVS_LPGPR)	+= nvmem_snvs_lpgpr.o
diff --git a/drivers/nvmem/regmap.c b/drivers/nvmem/regmap.c
new file mode 100644
index 000000000000..346d2516f3c3
--- /dev/null
+++ b/drivers/nvmem/regmap.c
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <common.h>
+#include <driver.h>
+#include <malloc.h>
+#include <xfuncs.h>
+#include <errno.h>
+#include <init.h>
+#include <io.h>
+#include <regmap.h>
+#include <linux/nvmem-provider.h>
+
+static int nvmem_regmap_write(void *ctx, unsigned offset, const void *val, size_t bytes)
+{
+	return regmap_bulk_write(ctx, offset, val, bytes);
+}
+
+static int nvmem_regmap_read(void *ctx, unsigned offset, void *buf, size_t bytes)
+{
+	struct regmap *map = ctx;
+	size_t rbytes, stride, skip_bytes;
+	u32 roffset, val;
+	u8 *buf8 = buf, *val8 = (u8 *)&val;
+	int i, j = 0, ret, size;
+
+	stride = regmap_get_reg_stride(map);
+
+	roffset = rounddown(offset, stride);
+	skip_bytes = offset & (stride - 1);
+	rbytes = roundup(bytes + skip_bytes, stride);
+
+	if (roffset + rbytes > stride * regmap_get_max_register(map))
+		return -EINVAL;
+
+	for (i = roffset; i < roffset + rbytes; i += stride) {
+		ret = regmap_bulk_read(map, i, &val, stride);
+		if (ret) {
+			dev_err(regmap_get_device(map), "Can't read data%d (%d)\n", i, ret);
+			return ret;
+		}
+
+		/* skip first bytes in case of unaligned read */
+		if (skip_bytes)
+			size = min(bytes, stride - skip_bytes);
+		else
+			size = min(bytes, stride);
+
+		memcpy(&buf8[j], &val8[skip_bytes], size);
+		bytes -= size;
+		j += size;
+		skip_bytes = 0;
+	}
+
+	return 0;
+}
+
+static struct nvmem_bus nvmem_regmap_bus = {
+	.read = nvmem_regmap_read,
+	.write = nvmem_regmap_write,
+};
+
+struct nvmem_device *nvmem_regmap_register(struct regmap *map, const char *name)
+{
+	struct nvmem_config config;
+
+	/* Can be retrofitted if needed */
+	if (regmap_get_reg_stride(map) != regmap_get_val_bytes(map))
+		return ERR_PTR(-EINVAL);
+
+	config.name = name;
+	config.dev = regmap_get_device(map);
+	config.priv = map;
+	config.stride = 1;
+	config.word_size = 1;
+	config.size = regmap_get_max_register(map) * regmap_get_reg_stride(map);
+	config.bus = &nvmem_regmap_bus;
+
+	return nvmem_register(&config);
+}
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index ac9ad21711aa..2d738983736e 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -33,9 +33,12 @@ struct nvmem_config {
 	void			*priv;
 };
 
+struct regmap;
+
 #if IS_ENABLED(CONFIG_NVMEM)
 
 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
+struct nvmem_device *nvmem_regmap_register(struct regmap *regmap, const char *name);
 
 #else
 
@@ -44,5 +47,10 @@ static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
 	return ERR_PTR(-ENOSYS);
 }
 
+static inline struct nvmem_device *nvmem_regmap_register(struct regmap *regmap, const char *name)
+{
+	return ERR_PTR(-ENOSYS);
+}
+
 #endif /* CONFIG_NVMEM */
 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */
diff --git a/include/regmap.h b/include/regmap.h
index c5cf954ad6d9..f04319f20482 100644
--- a/include/regmap.h
+++ b/include/regmap.h
@@ -109,6 +109,7 @@ void regmap_mmio_detach_clk(struct regmap *map);
 void regmap_exit(struct regmap *map);
 
 struct regmap *dev_get_regmap(struct device_d *dev, const char *name);
+struct device_d *regmap_get_device(struct regmap *map);
 
 int regmap_register_cdev(struct regmap *map, const char *name);
 
-- 
2.29.2




More information about the barebox mailing list