[PATCH 02/13] regmap: add A2B support

Alvin Šipraga
2024-05-17T12:58:48Z

From: Alvin Šipraga <alsi@bang-olufsen.dk>

Add regmap support for A2B drivers.

Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
---
drivers/base/regmap/Kconfig | 6 ++-
drivers/base/regmap/Makefile | 1 +
drivers/base/regmap/regmap-a2b.c | 82 ++++++++++++++++++++++++++++++++++++++++
include/linux/regmap.h | 38 +++++++++++++++++++
4 files changed, 126 insertions(+), 1 deletion(-)

diff --git a/drivers/base/regmap/Kconfig b/drivers/base/regmap/Kconfig
index b1affac70d5d..df9ad0c9a338 100644
--- a/drivers/base/regmap/Kconfig
+++ b/drivers/base/regmap/Kconfig
@@ -5,7 +5,7 @@

config REGMAP
bool
- default y if (REGMAP_I2C || REGMAP_SPI || REGMAP_SPMI || REGMAP_W1 || REGMAP_AC97 || REGMAP_MMIO || REGMAP_IRQ || REGMAP_SOUNDWIRE || REGMAP_SOUNDWIRE_MBQ || REGMAP_SCCB || REGMAP_I3C || REGMAP_SPI_AVMM || REGMAP_MDIO || REGMAP_FSI)
+ default y if (REGMAP_I2C || REGMAP_SPI || REGMAP_SPMI || REGMAP_W1 || REGMAP_AC97 || REGMAP_MMIO || REGMAP_IRQ || REGMAP_SOUNDWIRE || REGMAP_SOUNDWIRE_MBQ || REGMAP_SCCB || REGMAP_I3C || REGMAP_SPI_AVMM || REGMAP_MDIO || REGMAP_FSI || REGMAP_A2B)
select IRQ_DOMAIN if REGMAP_IRQ
select MDIO_BUS if REGMAP_MDIO
help
@@ -91,3 +91,7 @@ config REGMAP_SPI_AVMM
config REGMAP_FSI
tristate
depends on FSI
+
+config REGMAP_A2B
+ tristate
+ depends on A2B
diff --git a/drivers/base/regmap/Makefile b/drivers/base/regmap/Makefile
index 5fdd0845b45e..979e10419f8f 100644
--- a/drivers/base/regmap/Makefile
+++ b/drivers/base/regmap/Makefile
@@ -22,3 +22,4 @@ obj-$(CONFIG_REGMAP_I3C) += regmap-i3c.o
obj-$(CONFIG_REGMAP_SPI_AVMM) += regmap-spi-avmm.o
obj-$(CONFIG_REGMAP_MDIO) += regmap-mdio.o
obj-$(CONFIG_REGMAP_FSI) += regmap-fsi.o
+obj-$(CONFIG_REGMAP_A2B) += regmap-a2b.o
diff --git a/drivers/base/regmap/regmap-a2b.c b/drivers/base/regmap/regmap-a2b.c
new file mode 100644
index 000000000000..ba5fbc5ed6eb
--- /dev/null
+++ b/drivers/base/regmap/regmap-a2b.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+// Register map access API - A2B support
+//
+// Copyright (c) 2023-2024 Alvin Šipraga <alsi@bang-olufsen.dk>
+
+#include <linux/regmap.h>
+#include <linux/a2b/a2b.h>
+
+static int regmap_a2b_write(void *context, const void *data, size_t count)
+{
+ struct a2b_node *node = context;
+ struct a2b_bus *bus = node->bus;
+ const u8 *d = data;
+ u8 reg;
+ int ret;
+ int i;
+
+ reg = d[0];
+
+ for (i = 0; i < count - 1; i++) {
+ ret = bus->ops->write(bus, node, reg + i, d[i + 1]);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+static int regmap_a2b_read(void *context, const void *reg_buf, size_t reg_size,
+ void *val_buf, size_t val_size)
+{
+ struct a2b_node *node = context;
+ struct a2b_bus *bus = node->bus;
+ u8 reg = ((u8 *)reg_buf)[0];
+ u8 *v = val_buf;
+ int ret;
+ int i;
+
+ if (reg_size != 1)
+ return -EINVAL;
+
+ for (i = 0; i < val_size; i++) {
+ unsigned int tmp;
+
+ ret = bus->ops->read(bus, node, reg + i, &tmp);
+ if (ret)
+ return ret;
+
+ v[i] = tmp & 0xFF;
+ }
+
+ return 0;
+}
+
+static const struct regmap_bus regmap_a2b = {
+ .write = regmap_a2b_write,
+ .read = regmap_a2b_read,
+ .val_format_endian_default = REGMAP_ENDIAN_BIG,
+};
+
+struct regmap *__devm_regmap_init_a2b_node(struct a2b_node *node,
+ const struct regmap_config *config,
+ struct lock_class_key *lock_key,
+ const char *lock_name)
+{
+ return __devm_regmap_init(&node->dev, &regmap_a2b, node, config,
+ lock_key, lock_name);
+}
+EXPORT_SYMBOL_GPL(__devm_regmap_init_a2b_node);
+
+struct regmap *__devm_regmap_init_a2b_func(struct a2b_func *func,
+ const struct regmap_config *config,
+ struct lock_class_key *lock_key,
+ const char *lock_name)
+{
+ return __devm_regmap_init(&func->dev, &regmap_a2b, func->node, config,
+ lock_key, lock_name);
+}
+EXPORT_SYMBOL_GPL(__devm_regmap_init_a2b_func);
+
+MODULE_LICENSE("GPL");
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index a6bc2980a98b..742bcc110a95 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -37,6 +37,8 @@ struct regmap_range_cfg;
struct regmap_field;
struct snd_ac97;
struct sdw_slave;
+struct a2b_node;
+struct a2b_func;

/*
* regmap_mdio address encoding. IEEE 802.3ae clause 45 addresses consist of a
@@ -655,6 +657,14 @@ struct regmap *__regmap_init_fsi(struct fsi_device *fsi_dev,
const struct regmap_config *config,
struct lock_class_key *lock_key,
const char *lock_name);
+struct regmap *__devm_regmap_init_a2b_node(struct a2b_node *node,
+ const struct regmap_config *config,
+ struct lock_class_key *lock_key,
+ const char *lock_name);
+struct regmap *__devm_regmap_init_a2b_func(struct a2b_func *func,
+ const struct regmap_config *config,
+ struct lock_class_key *lock_key,
+ const char *lock_name);

struct regmap *__devm_regmap_init(struct device *dev,
const struct regmap_bus *bus,
@@ -1207,6 +1217,34 @@ bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
__regmap_lockdep_wrapper(__devm_regmap_init_fsi, #config, \
fsi_dev, config)

+/**
+ * devm_regmap_init_a2b_node() - Initialise managed register map for A2B node
+ *
+ * @node: Device that will be interacted with
+ * @config: Configuration for register map
+ *
+ * The return value will be an ERR_PTR() on error or a valid pointer
+ * to a struct regmap. The regmap will be automatically freed by the
+ * device management code.
+ */
+#define devm_regmap_init_a2b_node(node, config) \
+ __regmap_lockdep_wrapper(__devm_regmap_init_a2b_node, #config, node, \
+ config)
+
+/**
+ * devm_regmap_init_a2b_func() - Initialise managed register map for A2B func
+ *
+ * @func: Device that will be interacted with
+ * @config: Configuration for register map
+ *
+ * The return value will be an ERR_PTR() on error or a valid pointer
+ * to a struct regmap. The regmap will be automatically freed by the
+ * device management code.
+ */
+#define devm_regmap_init_a2b_func(func, config) \
+ __regmap_lockdep_wrapper(__devm_regmap_init_a2b_func, #config, func, \
+ config)
+
int regmap_mmio_attach_clk(struct regmap *map, struct clk *clk);
void regmap_mmio_detach_clk(struct regmap *map);
void regmap_exit(struct regmap *map);

--
2.44.0