diff mbox

[V3,2/2] regmap: add regmap_bulk_write() for register write

Message ID 1328889628-13151-2-git-send-email-ldewangan@nvidia.com
State Not Applicable, archived
Headers show

Commit Message

Laxman Dewangan Feb. 10, 2012, 4 p.m. UTC
The bulk_write() supports the data transfer to multi
register which takes the data into cpu_endianness format
and does formatting of data to device format before
sending to device.
The transfer can be completed in single transfer or multiple
transfer based on data formatting.

Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
---
Changes from V1: 
- Change commit message.
- Byte swapping in input data packet.
Changes from V2:
- Remove formatting if val_byte is 1.
- Allocating proper size of memory for val_byte != 1
 drivers/base/regmap/regmap.c |   61 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/regmap.h       |    2 +
 2 files changed, 63 insertions(+), 0 deletions(-)

Comments

Mark Brown Feb. 11, 2012, 9:47 p.m. UTC | #1
On Fri, Feb 10, 2012 at 09:30:28PM +0530, Laxman Dewangan wrote:

> +		/* No formatting is require if val_byte is 1 */
> +		if (val_bytes == 1)
> +			wval = (void *)val;
> +		else {

Please don't combine branches with and without braces.

> +			wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
> +			if (!wval) {
> +				mutex_unlock(&map->lock);
> +				dev_err(map->dev,
> +					"Error in memory allocation\n");
> +				return -ENOMEM;

As a style thing a goto error pattern is clearer for stuff like this -
there's so many branches that it's complex to keep track of the
unwinding.
diff mbox

Patch

diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index ba92217..a1656a7 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -482,6 +482,67 @@  int regmap_raw_write(struct regmap *map, unsigned int reg,
 }
 EXPORT_SYMBOL_GPL(regmap_raw_write);
 
+/*
+ * regmap_bulk_write(): Write multiple registers to the device
+ *
+ * @map: Register map to write to
+ * @reg: First register to be write from
+ * @val: Block of data to be written, in native register size for device
+ * @val_count: Number of registers to write
+ *
+ * This function is intended to be used for writing a large block of
+ * data to be device either in single transfer or multiple transfer.
+ *
+ * A value of zero will be returned on success, a negative errno will
+ * be returned in error cases.
+ */
+int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
+		     size_t val_count)
+{
+	int ret = 0, i;
+	unsigned int ival;
+	size_t val_bytes = map->format.val_bytes;
+
+	mutex_lock(&map->lock);
+
+	if (map->format.parse_val) {
+		void *wval;
+		/* No formatting is require if val_byte is 1 */
+		if (val_bytes == 1)
+			wval = (void *)val;
+		else {
+			wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
+			if (!wval) {
+				mutex_unlock(&map->lock);
+				dev_err(map->dev,
+					"Error in memory allocation\n");
+				return -ENOMEM;
+			}
+			for (i = 0; i < val_count * val_bytes; i += val_bytes)
+				map->format.parse_val(wval + i);
+		}
+		ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
+
+		if (val_bytes != 1)
+			kfree(wval);
+	} else {
+		for (i = 0; i < val_count; i++) {
+			memcpy(&ival, val + (i * val_bytes), val_bytes);
+			ret = _regmap_write(map, reg + i, ival);
+			if (ret) {
+				dev_err(map->dev,
+				    "Error in register %u write, ret %d\n",
+						reg + i, ret);
+				break;
+			}
+		}
+	}
+
+	mutex_unlock(&map->lock);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(regmap_bulk_write);
+
 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
 			    unsigned int val_len)
 {
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index a6ed6e6..0925e24 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -135,6 +135,8 @@  int regmap_reinit_cache(struct regmap *map,
 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
 int regmap_raw_write(struct regmap *map, unsigned int reg,
 		     const void *val, size_t val_len);
+int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
+			size_t val_count);
 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
 int regmap_raw_read(struct regmap *map, unsigned int reg,
 		    void *val, size_t val_len);