Skip to contentSkip to conversation

regmap: Add asynchronous I/O support

PATCH In review Mark Brown 2 participants 7 messages Jan 27, 2013 – Jan 29, 2013

Subsystems (2)
lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/2 regmap: Add asynchronous I/O support

Some use cases like firmware download can transfer a lot of data in quick succession. With high speed buses these use cases can benefit from having multiple transfers scheduled at once since this allows the bus to minimise the delay between transfers.

Support this by adding regmap_raw_write_async(), allowing raw transfers to be scheduled, and regmap_async_complete() to wait for them to finish.

---
 drivers/base/regmap/internal.h |   15 ++++
 drivers/base/regmap/regmap.c   |  183 +++++++++++++++++++++++++++++++++++++---
 include/linux/regmap.h         |   26 ++++++
 3 files changed, 214 insertions(+), 10 deletions(-)
drivers/base/regmap/internal.h +15 -0
index ba6532c..855eb4f 100644
@@ -16,6 +16,7 @@
 #include <linux/regmap.h>
 #include <linux/fs.h>
 #include <linux/list.h>
+#include <linux/wait.h>
 
 struct regmap;
 struct regcache_ops;
@@ -39,6 +40,13 @@ struct regmap_format {
 	unsigned int (*parse_val)(void *buf);
 };
 
+struct regmap_async {
+	struct list_head list;
+	struct work_struct cleanup;
+	struct regmap *map;
+	void *work_buf;
+};
+
 struct regmap {
 	struct mutex mutex;
 	spinlock_t spinlock;
@@ -53,6 +61,11 @@ struct regmap {
 	void *bus_context;
 	const char *name;
 
+	spinlock_t async_lock;
+	wait_queue_head_t async_waitq;
+	struct list_head async_list;
+	int async_ret;
+
 #ifdef CONFIG_DEBUG_FS
 	struct dentry *debugfs;
 	const char *debugfs_name;
@@ -178,6 +191,8 @@ bool regcache_set_val(void *base, unsigned int idx,
 		      unsigned int val, unsigned int word_size);
 int regcache_lookup_reg(struct regmap *map, unsigned int reg);
 
+void regmap_async_complete_cb(struct regmap_async *async, int ret);
+
 extern struct regcache_ops regcache_rbtree_ops;
 extern struct regcache_ops regcache_lzo_ops;
 extern struct regcache_ops regcache_flat_ops;
drivers/base/regmap/regmap.c +173 -10

256 lines not on this page · raw message

include/linux/regmap.h +26 -0
index 5e81abf..c8e9024 100644
@@ -236,11 +236,17 @@ struct regmap_range_cfg {
 	unsigned int window_len;
 };
 
+struct regmap_async;
+
 typedef int (*regmap_hw_write)(void *context, const void *data,
 			       size_t count);
 typedef int (*regmap_hw_gather_write)(void *context,
 				      const void *reg, size_t reg_len,
 				      const void *val, size_t val_len);
+typedef int (*regmap_hw_async_write)(void *context,
+				     const void *reg, size_t reg_len,
+				     const void *val, size_t val_len,
+				     struct regmap_async *async);
 typedef int (*regmap_hw_read)(void *context,
 			      const void *reg_buf, size_t reg_size,
 			      void *val_buf, size_t val_size);
@@ -256,6 +262,8 @@ typedef void (*regmap_hw_free_context)(void *context);
  * @write: Write operation.
  * @gather_write: Write operation with split register/value, return -ENOTSUPP
  *                if not implemented  on a given device.
+ * @async_write: Write operation which completes asynchronously, optional and
+ *               must serialise with respect to non-async I/O.
  * @read: Read operation.  Data is returned in the buffer used to transmit
  *         data.
  * @read_flag_mask: Mask to be set in the top byte of the register when doing
@@ -266,16 +274,19 @@ typedef void (*regmap_hw_free_context)(void *context);
  * @val_format_endian_default: Default endianness for formatted register
  *     values. Used when the regmap_config specifies DEFAULT. If this is
  *     DEFAULT, BIG is assumed.
+ * @async_size: Size of struct used for async work.
  */
 struct regmap_bus {
 	bool fast_io;
 	regmap_hw_write write;
 	regmap_hw_gather_write gather_write;
+	regmap_hw_async_write async_write;
 	regmap_hw_read read;
 	regmap_hw_free_context free_context;
 	u8 read_flag_mask;
 	enum regmap_endian reg_format_endian_default;
 	enum regmap_endian val_format_endian_default;
+	size_t async_size;
 };
 
 struct regmap *regmap_init(struct device *dev,
@@ -311,6 +322,8 @@ 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_raw_write_async(struct regmap *map, unsigned int reg,
+			   const void *val, size_t val_len);
 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);
@@ -322,6 +335,7 @@ int regmap_update_bits_check(struct regmap *map, unsigned int reg,
 			     unsigned int mask, unsigned int val,
 			     bool *change);
 int regmap_get_val_bytes(struct regmap *map);
+int regmap_async_complete(struct regmap *map);
 
 int regcache_sync(struct regmap *map);
 int regcache_sync_region(struct regmap *map, unsigned int min,
@@ -424,6 +438,13 @@ static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
 	return -EINVAL;
 }
 
+static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
+					 const void *val, size_t val_len)
+{
+	WARN_ONCE(1, "regmap API is disabled");
+	return -EINVAL;
+}
+
 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
 				    const void *val, size_t val_count)
 {
@@ -502,6 +523,11 @@ static inline void regcache_mark_dirty(struct regmap *map)
 	WARN_ONCE(1, "regmap API is disabled");
 }
 
+static inline void regmap_async_complete(struct regmap *map)
+{
+	WARN_ONCE(1, "regmap API is disabled");
+}
+
 static inline int regmap_register_patch(struct regmap *map,
 					const struct reg_default *regs,
 					int num_regs)
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH 2/2 regmap: spi: Support asynchronous I/O for SPI

---
 drivers/base/regmap/regmap-spi.c |   41 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
drivers/base/regmap/regmap-spi.c +41 -0
index ffa46a9..bb19592 100644
@@ -15,6 +15,21 @@
 #include <linux/init.h>
 #include <linux/module.h>
 
+#include "internal.h"
+
+struct regmap_async_spi {
+	struct regmap_async core;
+	struct spi_message m;
+	struct spi_transfer t[2];
+};
+
+static void regmap_spi_complete(void *data)
+{
+	struct regmap_async_spi *async = data;
+
+	regmap_async_complete_cb(&async->core, async->m.status);
+}
+
 static int regmap_spi_write(void *context, const void *data, size_t count)
 {
 	struct device *dev = context;
@@ -40,6 +55,30 @@ static int regmap_spi_gather_write(void *context,
 	return spi_sync(spi, &m);
 }
 
+static int regmap_spi_async_write(void *context,
+				  const void *reg, size_t reg_len,
+				  const void *val, size_t val_len,
+				  struct regmap_async *a)
+{
+	struct regmap_async_spi *async = (void *)a;
+	struct device *dev = context;
+	struct spi_device *spi = to_spi_device(dev);
+
+	async->t[0].tx_buf = reg;
+	async->t[0].len = reg_len;
+	async->t[1].tx_buf = val;
+	async->t[1].len = val_len;
+
+	spi_message_init(&async->m);
+	spi_message_add_tail(&async->t[0], &async->m);
+	spi_message_add_tail(&async->t[1], &async->m);
+
+	async->m.complete = regmap_spi_complete;
+	async->m.context = async;
+
+	return spi_async(spi, &async->m);
+}
+
 static int regmap_spi_read(void *context,
 			   const void *reg, size_t reg_size,
 			   void *val, size_t val_size)
@@ -53,8 +92,10 @@ static int regmap_spi_read(void *context,
 static struct regmap_bus regmap_spi = {
 	.write = regmap_spi_write,
 	.gather_write = regmap_spi_gather_write,
+	.async_write = regmap_spi_async_write,
 	.read = regmap_spi_read,
 	.read_flag_mask = 0x80,
+	.async_size = sizeof(struct regmap_async_spi),
 };
 
 /**
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH 2/2] regmap: spi: Support asynchronous I/O for SPI

Quoted 27 lines · Mark Brown
drivers/base/regmap/regmap-spi.c +15 -0
index ffa46a9..bb19592 100644
@@ -15,6 +15,21 @@
 #include <linux/init.h>
 #include <linux/module.h>
 
+#include "internal.h"
+
+struct regmap_async_spi {
+	struct regmap_async core;
+	struct spi_message m;
+	struct spi_transfer t[2];
+};
+
+static void regmap_spi_complete(void *data)
+{
+	struct regmap_async_spi *async = data;
+
+	regmap_async_complete_cb(&async->core, async->m.status);
+}
+
 static int regmap_spi_write(void *context, const void *data, size_t count)
 {
 	struct device *dev = context;

drivers/base/regmap/regmap-spi.c static int regmap_spi_gather_write(void *context, · line 63

Quoted 9 lines
drivers/base/regmap/regmap-spi.c +6 -0
@@ -40,6 +55,30 @@ static int regmap_spi_gather_write(void *context,
 	return spi_sync(spi, &m);
 }
 
+static int regmap_spi_async_write(void *context,
+				  const void *reg, size_t reg_len,
+				  const void *val, size_t val_len,
+				  struct regmap_async *a)
+{
+	struct regmap_async_spi *async = (void *)a;

Can you use container_of() here?

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation
author
#3 lore raw

Re: [PATCH 2/2] regmap: spi: Support asynchronous I/O for SPI

Quoted 10 lines · Stephen Boyd

drivers/base/regmap/regmap-spi.c static int regmap_spi_gather_write(void *context, · line 63

Quoted 6 lines · Mark Brown
drivers/base/regmap/regmap-spi.c +6 -0
+static int regmap_spi_async_write(void *context,
+				  const void *reg, size_t reg_len,
+				  const void *val, size_t val_len,
+				  struct regmap_async *a)
+{
+	struct regmap_async_spi *async = (void *)a;

Can you use container_of() here?

We could but I tend not to if the interface strictly requires that the pointers be type punnable.

#4 lore raw

Re: [PATCH 2/2] regmap: spi: Support asynchronous I/O for SPI

Quoted 11 lines · Mark Brown
Quoted 8 lines · Stephen Boyd

drivers/base/regmap/regmap-spi.c static int regmap_spi_gather_write(void *context, · line 63

Quoted 6 lines · Mark Brown
drivers/base/regmap/regmap-spi.c +6 -0
+static int regmap_spi_async_write(void *context,
+				  const void *reg, size_t reg_len,
+				  const void *val, size_t val_len,
+				  struct regmap_async *a)
+{
+	struct regmap_async_spi *async = (void *)a;

Can you use container_of() here?

We could but I tend not to if the interface strictly requires that the pointers be type punnable.

What is the interface requiring strict type punning here? async_write?

I just hope we don't move the fields around in the regmap_async_spi struct and then this code silently breaks. I hope the compiler is smart enough to skip doing any math if we used container_of() with the current struct layout.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation
author
#5 lore raw

Re: [PATCH 2/2] regmap: spi: Support asynchronous I/O for SPI

Quoted 6 lines · Stephen Boyd
Quoted 2 lines · Mark Brown

We could but I tend not to if the interface strictly requires that the pointers be type punnable.

What is the interface requiring strict type punning here? async_write?

Yes, the async data must always be the first member of the bus specific struct. This is to avoid having to do an additional allocation for the bus specific private data.

Quoted 4 lines

I just hope we don't move the fields around in the regmap_async_spi struct and then this code silently breaks. I hope the compiler is smart enough to skip doing any math if we used container_of() with the current struct layout.

It would break anyway, like I say the interface relies on it currently. The other option is to do a callback into the bus code and return the contained structure but it seemed more involved and this is fairly idiomatic.

author
#6 lore raw

Re: [PATCH 2/2] regmap: spi: Support asynchronous I/O for SPI

Quoted 4 lines · Mark Brown

It would break anyway, like I say the interface relies on it currently. The other option is to do a callback into the bus code and return the contained structure but it seemed more involved and this is fairly idiomatic.

Actually I'll just do that, we have to provide the size to the core anyway. Thanks.