Skip to contentSkip to conversation

regmap: spi: Set regmap max raw r/w from max_transfer_size

PATCH Superseded Lucas Tanure 2 participants 6 messages Sep 15, 2021

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 regmap: spi: Set regmap max raw r/w from max_transfer_size

Set regmap raw read/write from spi max_transfer_size so regmap_raw_read/write can split the access into chunks

---

Changes v2:
New series

Changes v3:
None

 drivers/base/regmap/regmap-spi.c | 36 ++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)
drivers/base/regmap/regmap-spi.c +32 -4
index c1894e93c378..0e6552e57ecf 100644
@@ -109,13 +109,37 @@ static const struct regmap_bus regmap_spi = {
 	.val_format_endian_default = REGMAP_ENDIAN_BIG,
 };
 
+static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
+						   const struct regmap_config *config)
+{
+	struct spi_master *master = spi->master;
+	struct regmap_bus *bus = NULL;
+	size_t max_size = spi_max_transfer_size(spi);
+
+	if (max_size != SIZE_MAX) {
+		bus = kmemdup(&regmap_spi, sizeof(*bus), GFP_KERNEL);
+		if (!bus)
+			return ERR_PTR(-ENOMEM);
+		bus->free_on_exit = true;
+		bus->max_raw_read = max_size;
+		bus->max_raw_write = max_size;
+		return bus;
+	}
+
+	return &regmap_spi;
+}
+
 struct regmap *__regmap_init_spi(struct spi_device *spi,
 				 const struct regmap_config *config,
 				 struct lock_class_key *lock_key,
 				 const char *lock_name)
 {
-	return __regmap_init(&spi->dev, &regmap_spi, &spi->dev, config,
-			     lock_key, lock_name);
+	const struct regmap_bus *bus = regmap_get_spi_bus(spi, config);
+
+	if (IS_ERR(bus))
+		return ERR_CAST(bus);
+
+	return __regmap_init(&spi->dev, bus, &spi->dev, config, lock_key, lock_name);
 }
 EXPORT_SYMBOL_GPL(__regmap_init_spi);
 
@@ -124,8 +148,12 @@ struct regmap *__devm_regmap_init_spi(struct spi_device *spi,
 				      struct lock_class_key *lock_key,
 				      const char *lock_name)
 {
-	return __devm_regmap_init(&spi->dev, &regmap_spi, &spi->dev, config,
-				  lock_key, lock_name);
+	const struct regmap_bus *bus = regmap_get_spi_bus(spi, config);
+
+	if (IS_ERR(bus))
+		return ERR_CAST(bus);
+
+	return __devm_regmap_init(&spi->dev, bus, &spi->dev, config, lock_key, lock_name);
 }
 EXPORT_SYMBOL_GPL(__devm_regmap_init_spi);
 
Signature · 1 line
2.33.0
#1 lore raw

PATCH v3 2/2 regmap: spi: Check raw_[read|write] against max message size

regmap-spi will split data and address between two transfers in the same message, so max_[read|write] must include space for the address and padding

---

Changes v3:
New series

 drivers/base/regmap/regmap-spi.c |  4 ++++
 drivers/base/regmap/regmap.c     | 15 +++++++++++++++
 include/linux/regmap.h           |  3 +++
 3 files changed, 22 insertions(+)
drivers/base/regmap/regmap-spi.c +4 -0
index 0e6552e57ecf..1434c502e340 100644
@@ -123,6 +123,10 @@ static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
 		bus->free_on_exit = true;
 		bus->max_raw_read = max_size;
 		bus->max_raw_write = max_size;
+
+		if (spi_max_message_size(spi) != SIZE_MAX)
+			bus->max_combined_rw = spi_max_message_size(spi);
+
 		return bus;
 	}
 
drivers/base/regmap/regmap.c +15 -0
index 21a0c2562ec0..a99152f010f8 100644
@@ -735,6 +735,7 @@ struct regmap *__regmap_init(struct device *dev,
 	struct regmap *map;
 	int ret = -EINVAL;
 	enum regmap_endian reg_endian, val_endian;
+	size_t reg_pad_size;
 	int i, j;
 
 	if (!config)
@@ -840,6 +841,20 @@ struct regmap *__regmap_init(struct device *dev,
 	if (bus) {
 		map->max_raw_read = bus->max_raw_read;
 		map->max_raw_write = bus->max_raw_write;
+		if (bus->max_combined_rw) {
+			reg_pad_size = map->format.reg_bytes + map->format.pad_bytes;
+
+			if (map->max_raw_read + reg_pad_size > bus->max_combined_rw)
+				map->max_raw_read -= reg_pad_size;
+			if (map->max_raw_write + reg_pad_size > bus->max_combined_rw)
+				map->max_raw_write -= reg_pad_size;
+
+			if (map->max_raw_read  < map->format.buf_size ||
+			    map->max_raw_write < map->format.buf_size) {
+				ret = -EINVAL;
+				goto err_hwlock;
+			}
+		}
 	}
 	map->dev = dev;
 	map->bus = bus;
include/linux/regmap.h +3 -0
index e3c9a25a853a..a720f578b8e6 100644
@@ -506,6 +506,8 @@ typedef void (*regmap_hw_free_context)(void *context);
  * @max_raw_read: Max raw read size that can be used on the bus.
  * @max_raw_write: Max raw write size that can be used on the bus.
  * @free_on_exit: kfree this on exit of regmap
+ * @max_combined_rw: Max size for raw_read + raw_write, when they are issued
+ *                   together as part of the same message
  */
 struct regmap_bus {
 	bool fast_io;
@@ -523,6 +525,7 @@ struct regmap_bus {
 	enum regmap_endian val_format_endian_default;
 	size_t max_raw_read;
 	size_t max_raw_write;
+	size_t max_combined_rw;
 	bool free_on_exit;
 };
 
Signature · 1 line
2.33.0
maintainer
#2 lore raw

Re: [PATCH v3 2/2] regmap: spi: Check raw_[read|write] against max message size

Quoted 9 lines · Lucas Tanure

regmap-spi will split data and address between two transfers in the same message, so max_[read|write] must include space for the address and padding

---

Changes v3:
New series

The changelog in patch 1 claimed that v2 was the first version of a new series. Which is it? If you're going to include an inter-version changelog it should be meaningful, just writing something for the sake of providing a changelog tends to only cause confusion.

drivers/base/regmap/regmap-spi.c static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi, · line 129

Quoted 3 lines
drivers/base/regmap/regmap-spi.c +3 -0
+		if (spi_max_message_size(spi) != SIZE_MAX)
+			bus->max_combined_rw = spi_max_message_size(spi);
+

Why is this conditional? The whole point in returning SIZE_MAX is that users don't need to care if there's a limit or not, any client logic checking sizes will always work.

drivers/base/regmap/regmap.c struct regmap *__regmap_init(struct device *dev, · line 857

Quoted 6 lines
drivers/base/regmap/regmap.c +6 -0
+			if (map->max_raw_read  < map->format.buf_size ||
+			    map->max_raw_write < map->format.buf_size) {
+				ret = -EINVAL;
+				goto err_hwlock;
+			}
+		}

This is at best an assert that the logic in _init() is self-consistent so just silently returning an error doesn't seem like the right thing to do, it's going to be super obscure for users.

maintainer
#3 lore raw
In reply to Lucas Tanure
Quoted 11 lines · Lucas Tanure

Set regmap raw read/write from spi max_transfer_size so regmap_raw_read/write can split the access into chunks

---

Changes v2:
New series

Changes v3:
None

How does this relate to the previous free standing copy of this patch you sent only two days ago on Monday? You've dropped Charles' Reviewed-by so I guess there must be some change but this changelog claims there's nothing (and appears to claim that v2 was the first version...).

#4 lore raw
Quoted 10 lines · Mark Brown
Quoted 9 lines · Lucas Tanure

Set regmap raw read/write from spi max_transfer_size so regmap_raw_read/write can split the access into chunks

---

Changes v2:
New series

Quoted 9 lines
Quoted 2 lines

Changes v3:
None

How does this relate to the previous free standing copy of this patch you sent only two days ago on Monday? You've dropped Charles' Reviewed-by so I guess there must be some change but this changelog claims there's nothing (and appears to claim that v2 was the first version...).

Hi,

Yes, its exactly the same, I forgot to add the second patch the previous one. I will resend with Charles review-by.

Thanks

maintainer
#5 lore raw
Quoted 5 lines · Lucas tanure

On 9/15/21 13:34, Mark Brown wrote:

Yes, its exactly the same, I forgot to add the second patch the previous one.
I will resend with Charles review-by.

Please allow a bit more time in case people are reviewing your whole series.