regmap: Apply optional delay in multi_reg_write/register_patch
authorNariman Poushin <nariman@opensource.wolfsonmicro.com>
Thu, 16 Jul 2015 15:36:22 +0000 (16:36 +0100)
committerMark Brown <broonie@kernel.org>
Thu, 16 Jul 2015 21:02:55 +0000 (22:02 +0100)
Add an optional delay_us field in reg_sequence to allow the client to
specify a delay (in microseconds) to be applied after any given write
in a sequence of writes.

We treat a delay in a sequence the same way we treat a page change as
they are logically similar in that you can coalesce all write before
a delay (in the same way you can coalesce all writes before a page
change is needed)

Signed-off-by: Nariman Poushin <nariman@opensource.wolfsonmicro.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/base/regmap/regmap.c
include/linux/regmap.h

index 2cbb4502747d8356417b2dc1faec6b94d04f3dff..b3a5aa5cd580c0d1dd67ffe89df915e9e7ec7f7c 100644 (file)
@@ -18,6 +18,7 @@
 #include <linux/of.h>
 #include <linux/rbtree.h>
 #include <linux/sched.h>
+#include <linux/delay.h>
 
 #define CREATE_TRACE_POINTS
 #include "trace.h"
@@ -1807,10 +1808,12 @@ static int _regmap_range_multi_paged_reg_write(struct regmap *map,
        int i, n;
        struct reg_sequence *base;
        unsigned int this_page = 0;
+       unsigned int page_change = 0;
        /*
         * the set of registers are not neccessarily in order, but
         * since the order of write must be preserved this algorithm
-        * chops the set each time the page changes
+        * chops the set each time the page changes. This also applies
+        * if there is a delay required at any point in the sequence.
         */
        base = regs;
        for (i = 0, n = 0; i < num_regs; i++, n++) {
@@ -1826,16 +1829,48 @@ static int _regmap_range_multi_paged_reg_write(struct regmap *map,
                                this_page = win_page;
                        if (win_page != this_page) {
                                this_page = win_page;
+                               page_change = 1;
+                       }
+               }
+
+               /* If we have both a page change and a delay make sure to
+                * write the regs and apply the delay before we change the
+                * page.
+                */
+
+               if (page_change || regs[i].delay_us) {
+
+                               /* For situations where the first write requires
+                                * a delay we need to make sure we don't call
+                                * raw_multi_reg_write with n=0
+                                * This can't occur with page breaks as we
+                                * never write on the first iteration
+                                */
+                               if (regs[i].delay_us && i == 0)
+                                       n = 1;
+
                                ret = _regmap_raw_multi_reg_write(map, base, n);
                                if (ret != 0)
                                        return ret;
+
+                               if (regs[i].delay_us)
+                                       udelay(regs[i].delay_us);
+
                                base += n;
                                n = 0;
-                       }
-                       ret = _regmap_select_page(map, &base[n].reg, range, 1);
-                       if (ret != 0)
-                               return ret;
+
+                               if (page_change) {
+                                       ret = _regmap_select_page(map,
+                                                                 &base[n].reg,
+                                                                 range, 1);
+                                       if (ret != 0)
+                                               return ret;
+
+                                       page_change = 0;
+                               }
+
                }
+
        }
        if (n > 0)
                return _regmap_raw_multi_reg_write(map, base, n);
@@ -1854,6 +1889,9 @@ static int _regmap_multi_reg_write(struct regmap *map,
                        ret = _regmap_write(map, regs[i].reg, regs[i].def);
                        if (ret != 0)
                                return ret;
+
+                       if (regs[i].delay_us)
+                               udelay(regs[i].delay_us);
                }
                return 0;
        }
@@ -1893,8 +1931,12 @@ static int _regmap_multi_reg_write(struct regmap *map,
        for (i = 0; i < num_regs; i++) {
                unsigned int reg = regs[i].reg;
                struct regmap_range_node *range;
+
+               /* Coalesce all the writes between a page break or a delay
+                * in a sequence
+                */
                range = _regmap_range_lookup(map, reg);
-               if (range) {
+               if (range || regs[i].delay_us) {
                        size_t len = sizeof(struct reg_sequence)*num_regs;
                        struct reg_sequence *base = kmemdup(regs, len,
                                                           GFP_KERNEL);
index c9ef2ec69142e2309b5fc80784657f5abe79d4c7..5a7cf2136c81fd9ec5a97f5c819b705febf96fbf 100644 (file)
@@ -51,14 +51,17 @@ struct reg_default {
 };
 
 /**
- * Register/value pairs for sequences of writes
+ * Register/value pairs for sequences of writes with an optional delay in
+ * microseconds to be applied after each write.
  *
  * @reg: Register address.
  * @def: Register value.
+ * @delay_us: Delay to be applied after the register write in microseconds
  */
 struct reg_sequence {
        unsigned int reg;
        unsigned int def;
+       unsigned int delay_us;
 };
 
 #ifdef CONFIG_REGMAP