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 10/11] regmap: i2c: use formatted I/O
Date: Wed, 11 Jan 2023 14:29:55 +0100	[thread overview]
Message-ID: <20230111132956.1153359-11-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230111132956.1153359-1-a.fatoum@pengutronix.de>

Switching to formatted I/O will enable consumers to customize access
sizes, which we'll need to support KSZ switch I2C communication.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/base/regmap/Kconfig      |  1 +
 drivers/base/regmap/regmap-i2c.c | 50 ++++++++++++++++++++++----------
 2 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/drivers/base/regmap/Kconfig b/drivers/base/regmap/Kconfig
index 78a00a085f05..c76908952a79 100644
--- a/drivers/base/regmap/Kconfig
+++ b/drivers/base/regmap/Kconfig
@@ -6,6 +6,7 @@ config REGMAP_FORMATTED
 config REGMAP_I2C
 	bool "I2C regmaps" if COMPILE_TEST
 	depends on I2C
+	select REGMAP_FORMATTED
 
 config REGMAP_SPI
 	bool "SPI regmaps" if COMPILE_TEST
diff --git a/drivers/base/regmap/regmap-i2c.c b/drivers/base/regmap/regmap-i2c.c
index 756bc224cc3b..583bfffc1bb0 100644
--- a/drivers/base/regmap/regmap-i2c.c
+++ b/drivers/base/regmap/regmap-i2c.c
@@ -7,34 +7,54 @@
 #include <regmap.h>
 
 
-static int regmap_i2c_reg_read(void *client, unsigned int reg, unsigned int *val)
+static int regmap_i2c_read(void *context,
+			   const void *reg, size_t reg_size,
+			   void *val, size_t val_size)
 {
-	u8 buf[1];
+	struct device_d *dev = context;
+	struct i2c_client *i2c = to_i2c_client(dev);
+	struct i2c_msg xfer[2];
 	int ret;
 
-	ret = i2c_read_reg(client, reg, buf, 1);
-	if (ret != 1)
-		return ret;
+	xfer[0].addr = i2c->addr;
+	xfer[0].flags = 0;
+	xfer[0].len = reg_size;
+	xfer[0].buf = (void *)reg;
 
-	*val = buf[0];
-	return 0;
+	xfer[1].addr = i2c->addr;
+	xfer[1].flags = I2C_M_RD;
+	xfer[1].len = val_size;
+	xfer[1].buf = val;
+
+	ret = i2c_transfer(i2c->adapter, xfer, 2);
+	if (ret == 2)
+		return 0;
+	else if (ret < 0)
+		return ret;
+	else
+		return -EIO;
 }
 
-static int regmap_i2c_reg_write(void *client, unsigned int reg, unsigned int val)
+static int regmap_i2c_write(void *context, const void *data, size_t count)
 {
-	u8 buf[] = { val & 0xff };
+	struct device_d *dev = context;
+	struct i2c_client *i2c = to_i2c_client(dev);
 	int ret;
 
-	ret = i2c_write_reg(client, reg, buf, 1);
-	if (ret != 1)
+	ret = i2c_master_send(i2c, data, count);
+	if (ret == count)
+		return 0;
+	else if (ret < 0)
 		return ret;
-
-	return 0;
+	else
+		return -EIO;
 }
 
 static const struct regmap_bus regmap_regmap_i2c_bus = {
-	.reg_write = regmap_i2c_reg_write,
-	.reg_read = regmap_i2c_reg_read,
+	.write = regmap_i2c_write,
+	.read = regmap_i2c_read,
+	.reg_format_endian_default = REGMAP_ENDIAN_BIG,
+	.val_format_endian_default = REGMAP_ENDIAN_BIG,
 };
 
 struct regmap *regmap_init_i2c(struct i2c_client *client,
-- 
2.30.2




  parent reply	other threads:[~2023-01-11 13:31 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 ` [PATCH v2 06/11] net: dsa: ksz9477: create regmap cdev for switch registers Ahmad Fatoum
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 ` Ahmad Fatoum [this message]
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-11-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