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 v2 06/11] net: dsa: ksz9477: create regmap cdev for switch registers
Date: Wed, 11 Jan 2023 14:29:51 +0100	[thread overview]
Message-ID: <20230111132956.1153359-7-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230111132956.1153359-1-a.fatoum@pengutronix.de>

Now that we use regmap for the KSZ9477 driver, we can make the register
map available for introspection as a device file. As the KSZ driver has
a separate regmap for each of the three access sizes, we add a new
regmap_multi_register_cdev abstraction that multiplexes device file
access to the regmap with the best matching alignment.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/base/regmap/Makefile       |   1 +
 drivers/base/regmap/regmap-multi.c | 104 +++++++++++++++++++++++++++++
 drivers/net/ksz9477.c              |   7 +-
 include/regmap.h                   |  21 ++++++
 4 files changed, 132 insertions(+), 1 deletion(-)
 create mode 100644 drivers/base/regmap/regmap-multi.c

diff --git a/drivers/base/regmap/Makefile b/drivers/base/regmap/Makefile
index d99db4277149..fe5beaaaa382 100644
--- a/drivers/base/regmap/Makefile
+++ b/drivers/base/regmap/Makefile
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y	+= regmap.o
+obj-y	+= regmap-multi.o
 obj-y	+= regmap-mmio.o
 obj-$(CONFIG_REGMAP_FORMATTED)	+= regmap-fmt.o
 obj-$(CONFIG_I2C)	+= regmap-i2c.o
diff --git a/drivers/base/regmap/regmap-multi.c b/drivers/base/regmap/regmap-multi.c
new file mode 100644
index 000000000000..e3f5b9aec1e4
--- /dev/null
+++ b/drivers/base/regmap/regmap-multi.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright 2022 Ahmad Fatoum <a.fatoum@pengutronix.de>
+ */
+
+#include <common.h>
+#include <fcntl.h>
+#include <regmap.h>
+#include <linux/bitfield.h>
+#include <linux/export.h>
+
+#include "internal.h"
+
+enum { MULTI_MAP_8, MULTI_MAP_16, MULTI_MAP_32, MULTI_MAP_64, MULTI_MAP_COUNT };
+struct regmap_multi {
+	struct cdev cdev;
+	struct regmap *map[MULTI_MAP_COUNT];
+};
+
+static struct regmap *regmap_multi_cdev_get_map(struct cdev *cdev, unsigned rwsize)
+{
+	struct regmap_multi *multi = container_of(cdev, struct regmap_multi, cdev);
+
+	switch (rwsize) {
+		case 8:
+			return multi->map[MULTI_MAP_64];
+		case 4:
+			return multi->map[MULTI_MAP_32];
+		case 2:
+			return multi->map[MULTI_MAP_16];
+		case 1:
+			return multi->map[MULTI_MAP_8];
+	}
+
+	return NULL;
+}
+
+static ssize_t regmap_multi_cdev_read(struct cdev *cdev, void *buf, size_t count,
+				      loff_t offset, unsigned long flags)
+{
+	unsigned rwsize = FIELD_GET(O_RWSIZE_MASK, flags);
+	struct regmap *map;
+
+	map = regmap_multi_cdev_get_map(cdev, rwsize);
+	if (!map)
+		return -EINVAL;
+
+	count = ALIGN_DOWN(count, rwsize);
+	return regmap_bulk_read(map, offset, buf, count) ?: count;
+}
+
+static ssize_t regmap_multi_cdev_write(struct cdev *cdev, const void *buf, size_t count,
+				       loff_t offset, unsigned long flags)
+{
+	unsigned rwsize = FIELD_GET(O_RWSIZE_MASK, flags);
+	struct regmap *map;
+
+	map = regmap_multi_cdev_get_map(cdev, rwsize);
+	if (!map)
+		return -EINVAL;
+
+	count = ALIGN_DOWN(count, rwsize);
+	return regmap_bulk_write(map, offset, buf, count) ?: count;
+}
+
+static struct cdev_operations regmap_multi_fops = {
+	.read	= regmap_multi_cdev_read,
+	.write	= regmap_multi_cdev_write,
+};
+
+int regmap_multi_register_cdev(struct regmap *map8,
+			       struct regmap *map16,
+			       struct regmap *map32,
+			       struct regmap *map64)
+{
+	struct regmap *maps[MULTI_MAP_COUNT] = { map8, map16, map32, map64 };
+	struct regmap_multi *multi;
+	struct cdev *cdev;
+	int i;
+
+	multi = xzalloc(sizeof(*multi));
+	cdev = &multi->cdev;
+
+	cdev->ops = &regmap_multi_fops;
+	cdev->size = LLONG_MAX;
+
+	for (i = 0; i < MULTI_MAP_COUNT; i++) {
+		if (!maps[i])
+			continue;
+
+		multi->map[i] = maps[i];
+		cdev->size = min_t(loff_t, regmap_size_bytes(maps[i]), cdev->size);
+		cdev->dev = cdev->dev ?: maps[i]->dev;
+	}
+
+	if (!cdev->dev) {
+		free(multi);
+		return -EINVAL;
+	}
+
+	cdev->name = xstrdup(dev_name(cdev->dev));
+
+	return devfs_create(cdev);
+}
diff --git a/drivers/net/ksz9477.c b/drivers/net/ksz9477.c
index a980735e8e3a..92e08f45bf03 100644
--- a/drivers/net/ksz9477.c
+++ b/drivers/net/ksz9477.c
@@ -450,7 +450,12 @@ static int microchip_switch_probe(struct device *dev)
 
 	ksz_default_setup(priv);
 
-	return dsa_register_switch(ds);
+	ret = dsa_register_switch(ds);
+	if (ret)
+		return ret;
+
+	return regmap_multi_register_cdev(priv->regmap[0], priv->regmap[1],
+					  priv->regmap[2], NULL);
 }
 
 static const struct of_device_id microchip_switch_dt_ids[] = {
diff --git a/include/regmap.h b/include/regmap.h
index 986ed391ab28..98ac90523b07 100644
--- a/include/regmap.h
+++ b/include/regmap.h
@@ -175,6 +175,27 @@ struct device *regmap_get_device(struct regmap *map);
 
 int regmap_register_cdev(struct regmap *map, const char *name);
 
+/**
+ * regmap_multi_register_cdev() - Initialize cdev backed by multiple regmaps
+ *
+ * @map8:  regmap for  8-bit wide accesses. NULL if such access
+ *         should fail with -EINVAL
+ * @map16: regmap for 16-bit wide accesses. NULL if such access
+ *         should fail with -EINVAL
+ * @map32: regmap for 32-bit wide accesses. NULL if such access
+ *         should fail with -EINVAL
+ * @map64: regmap for 64-bit wide accesses. NULL if such access
+ *         should fail with -EINVAL
+ *
+ * Registers a cdev that demultiplexes cdev accesses to one
+ * of the underlying regmaps according to the access size
+ * (e.g. mw -b => map8, mw -l => map32)
+ */
+int regmap_multi_register_cdev(struct regmap *map8,
+			       struct regmap *map16,
+			       struct regmap *map32,
+			       struct regmap *map64);
+
 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
 
-- 
2.30.2




  parent reply	other threads:[~2023-01-11 13:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-11 13:29 [PATCH v2 00/11] net: dsa: ksz9477: use regmap to add I2C support next to SPI Ahmad Fatoum
2023-01-11 13:29 ` [PATCH v2 01/11] regmap: consolidate reg/val format into regmap_format Ahmad Fatoum
2023-01-11 13:29 ` [PATCH v2 02/11] regmap: support formatted read and write Ahmad Fatoum
2023-01-11 13:29 ` [PATCH v2 03/11] regmap: port regmap_init_spi Ahmad Fatoum
2023-01-11 13:29 ` [PATCH v2 04/11] regmap: factor out regmap cdev size calculation Ahmad Fatoum
2023-01-11 13:29 ` [PATCH v2 05/11] net: dsa: ksz9477: switch to regmap_init_spi Ahmad Fatoum
2023-01-11 13:29 ` Ahmad Fatoum [this message]
2023-01-11 13:29 ` [PATCH v2 07/11] drivers: base: regmap: introduce REGMAP_I2C Ahmad Fatoum
2023-01-11 13:29 ` [PATCH v2 08/11] dev: add dev_bus_is_spi/i2c helpers Ahmad Fatoum
2023-01-11 13:29 ` [PATCH v2 09/11] net: dsa: ksz9477: refactor to prepare i2c support Ahmad Fatoum
2023-01-11 13:29 ` [PATCH v2 10/11] regmap: i2c: use formatted I/O Ahmad Fatoum
2023-01-11 13:29 ` [PATCH v2 11/11] net: ksz9477: add I2C support Ahmad Fatoum
2023-01-12 14:58 ` [PATCH v2 00/11] net: dsa: ksz9477: use regmap to add I2C support next to SPI 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=20230111132956.1153359-7-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