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 1/5] regmap: implement regmap_init_i2c
Date: Mon, 31 May 2021 09:24:02 +0200	[thread overview]
Message-ID: <20210531072406.5630-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20210531072406.5630-1-a.fatoum@pengutronix.de>

Linux offers a helper for creating regmaps over i2c. Add a similar
helper for barebox to ease driver porting.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/base/regmap/Makefile     |  1 +
 drivers/base/regmap/regmap-i2c.c | 44 ++++++++++++++++++++++++++++++++
 include/regmap.h                 | 13 ++++++++++
 3 files changed, 58 insertions(+)
 create mode 100644 drivers/base/regmap/regmap-i2c.c

diff --git a/drivers/base/regmap/Makefile b/drivers/base/regmap/Makefile
index ab2387037d37..b12b69531156 100644
--- a/drivers/base/regmap/Makefile
+++ b/drivers/base/regmap/Makefile
@@ -1,2 +1,3 @@
 obj-y	+= regmap.o
 obj-y	+= regmap-mmio.o
+obj-$(CONFIG_I2C)	+= regmap-i2c.o
diff --git a/drivers/base/regmap/regmap-i2c.c b/drivers/base/regmap/regmap-i2c.c
new file mode 100644
index 000000000000..88b24ae6a825
--- /dev/null
+++ b/drivers/base/regmap/regmap-i2c.c
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021, Ahmad Fatoum, Pengutronix
+ */
+
+#include <i2c/i2c.h>
+#include <regmap.h>
+
+
+static int regmap_i2c_reg_read(void *client, unsigned int reg, unsigned int *val)
+{
+	u8 buf[1];
+	int ret;
+
+	ret = i2c_read_reg(client, reg, buf, 1);
+	if (ret != 1)
+		return ret;
+
+	*val = buf[0];
+	return 0;
+}
+
+static int regmap_i2c_reg_write(void *client, unsigned int reg, unsigned int val)
+{
+	u8 buf[] = { val & 0xff };
+	int ret;
+
+	ret = i2c_write_reg(client, reg, buf, 1);
+	if (ret != 1)
+		return ret;
+
+	return 0;
+}
+
+static const struct regmap_bus regmap_regmap_i2c_bus = {
+	.reg_write = regmap_i2c_reg_write,
+	.reg_read = regmap_i2c_reg_read,
+};
+
+struct regmap *regmap_init_i2c(struct i2c_client *client,
+			       const struct regmap_config *config)
+{
+	return  regmap_init(&client->dev, &regmap_regmap_i2c_bus, client, config);
+}
diff --git a/include/regmap.h b/include/regmap.h
index b43cd936fa61..c5cf954ad6d9 100644
--- a/include/regmap.h
+++ b/include/regmap.h
@@ -76,6 +76,19 @@ struct regmap *regmap_init_mmio_clk(struct device_d *dev, const char *clk_id,
 				    void __iomem *regs,
 				    const struct regmap_config *config);
 
+/**
+ * regmap_init_i2c() - Initialise i2c register map
+ *
+ * @i2c: 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.
+ */
+struct i2c_client;
+struct regmap *regmap_init_i2c(struct i2c_client *i2c,
+			       const struct regmap_config *config);
+
 /**
  * regmap_init_mmio() - Initialise register map
  *
-- 
2.29.2


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


  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 ` Ahmad Fatoum [this message]
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 ` [PATCH 4/5] nvmem: add nvmem_regmap_register helper Ahmad Fatoum
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-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --subject='Re: [PATCH 1/5] regmap: implement regmap_init_i2c' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox