mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/5] nvmem: add nvmem_regmap_register helper
Date: Mon, 31 May 2021 09:24:05 +0200	[thread overview]
Message-ID: <20210531072406.5630-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20210531072406.5630-1-a.fatoum@pengutronix.de>

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@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


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2021-05-31  7:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-31  7:24 [PATCH 0/5] add regmap helpers for NVMEM and i2c Ahmad Fatoum
2021-05-31  7:24 ` [PATCH 1/5] regmap: implement regmap_init_i2c Ahmad Fatoum
2021-05-31  7:24 ` [PATCH 2/5] mfd: stpmic1: simplify using regmap_init_i2c Ahmad Fatoum
2021-05-31  7:24 ` [PATCH 3/5] nvmem: provider: align read/write callback prototype with upstream Ahmad Fatoum
2021-05-31  7:24 ` Ahmad Fatoum [this message]
2021-05-31  7:24 ` [PATCH 5/5] nvmem: stm32-bsec: simplify using new nvmem_regmap_register Ahmad Fatoum
2021-06-02  6:36 ` [PATCH 0/5] add regmap helpers for NVMEM and i2c 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=20210531072406.5630-5-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --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