aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/regmap
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
commit8dea78da5cee153b8af9c07a2745f6c55057fe12 (patch)
treea8f4d49d63b1ecc92f2fddceba0655b2472c5bd9 /drivers/base/regmap
parent406089d01562f1e2bf9f089fd7637009ebaad589 (diff)
Patched in Tegra support.
Diffstat (limited to 'drivers/base/regmap')
-rw-r--r--drivers/base/regmap/Kconfig6
-rw-r--r--drivers/base/regmap/Makefile1
-rw-r--r--drivers/base/regmap/internal.h63
-rw-r--r--drivers/base/regmap/regcache-lzo.c28
-rw-r--r--drivers/base/regmap/regcache-rbtree.c80
-rw-r--r--drivers/base/regmap/regcache.c115
-rw-r--r--drivers/base/regmap/regmap-debugfs.c271
-rw-r--r--drivers/base/regmap/regmap-i2c.c15
-rw-r--r--drivers/base/regmap/regmap-irq.c321
-rw-r--r--drivers/base/regmap/regmap-mmio.c234
-rw-r--r--drivers/base/regmap/regmap-spi.c13
-rw-r--r--drivers/base/regmap/regmap.c830
12 files changed, 282 insertions, 1695 deletions
diff --git a/drivers/base/regmap/Kconfig b/drivers/base/regmap/Kconfig
index f0d30543fcc..0f6c7fb418e 100644
--- a/drivers/base/regmap/Kconfig
+++ b/drivers/base/regmap/Kconfig
@@ -3,10 +3,9 @@
3# subsystems should select the appropriate symbols. 3# subsystems should select the appropriate symbols.
4 4
5config REGMAP 5config REGMAP
6 default y if (REGMAP_I2C || REGMAP_SPI || REGMAP_MMIO || REGMAP_IRQ) 6 default y if (REGMAP_I2C || REGMAP_SPI)
7 select LZO_COMPRESS 7 select LZO_COMPRESS
8 select LZO_DECOMPRESS 8 select LZO_DECOMPRESS
9 select IRQ_DOMAIN if REGMAP_IRQ
10 bool 9 bool
11 10
12config REGMAP_I2C 11config REGMAP_I2C
@@ -15,8 +14,5 @@ config REGMAP_I2C
15config REGMAP_SPI 14config REGMAP_SPI
16 tristate 15 tristate
17 16
18config REGMAP_MMIO
19 tristate
20
21config REGMAP_IRQ 17config REGMAP_IRQ
22 bool 18 bool
diff --git a/drivers/base/regmap/Makefile b/drivers/base/regmap/Makefile
index 5e75d1b683e..defd57963c8 100644
--- a/drivers/base/regmap/Makefile
+++ b/drivers/base/regmap/Makefile
@@ -3,5 +3,4 @@ obj-$(CONFIG_REGMAP) += regcache-rbtree.o regcache-lzo.o
3obj-$(CONFIG_DEBUG_FS) += regmap-debugfs.o 3obj-$(CONFIG_DEBUG_FS) += regmap-debugfs.o
4obj-$(CONFIG_REGMAP_I2C) += regmap-i2c.o 4obj-$(CONFIG_REGMAP_I2C) += regmap-i2c.o
5obj-$(CONFIG_REGMAP_SPI) += regmap-spi.o 5obj-$(CONFIG_REGMAP_SPI) += regmap-spi.o
6obj-$(CONFIG_REGMAP_MMIO) += regmap-mmio.o
7obj-$(CONFIG_REGMAP_IRQ) += regmap-irq.o 6obj-$(CONFIG_REGMAP_IRQ) += regmap-irq.o
diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h
index 401d1919635..abd76678ed7 100644
--- a/drivers/base/regmap/internal.h
+++ b/drivers/base/regmap/internal.h
@@ -15,18 +15,10 @@
15 15
16#include <linux/regmap.h> 16#include <linux/regmap.h>
17#include <linux/fs.h> 17#include <linux/fs.h>
18#include <linux/list.h>
19 18
20struct regmap; 19struct regmap;
21struct regcache_ops; 20struct regcache_ops;
22 21
23struct regmap_debugfs_off_cache {
24 struct list_head list;
25 off_t min;
26 off_t max;
27 unsigned int base_reg;
28};
29
30struct regmap_format { 22struct regmap_format {
31 size_t buf_size; 23 size_t buf_size;
32 size_t reg_bytes; 24 size_t reg_bytes;
@@ -34,34 +26,21 @@ struct regmap_format {
34 size_t val_bytes; 26 size_t val_bytes;
35 void (*format_write)(struct regmap *map, 27 void (*format_write)(struct regmap *map,
36 unsigned int reg, unsigned int val); 28 unsigned int reg, unsigned int val);
37 void (*format_reg)(void *buf, unsigned int reg, unsigned int shift); 29 void (*format_reg)(void *buf, unsigned int reg);
38 void (*format_val)(void *buf, unsigned int val, unsigned int shift); 30 void (*format_val)(void *buf, unsigned int val);
39 unsigned int (*parse_val)(void *buf); 31 unsigned int (*parse_val)(void *buf);
40}; 32};
41 33
42struct regmap { 34struct regmap {
43 struct mutex mutex; 35 struct mutex lock;
44 spinlock_t spinlock;
45 regmap_lock lock;
46 regmap_unlock unlock;
47 void *lock_arg; /* This is passed to lock/unlock functions */
48 36
49 struct device *dev; /* Device we do I/O on */ 37 struct device *dev; /* Device we do I/O on */
50 void *work_buf; /* Scratch buffer used to format I/O */ 38 void *work_buf; /* Scratch buffer used to format I/O */
51 struct regmap_format format; /* Buffer format */ 39 struct regmap_format format; /* Buffer format */
52 const struct regmap_bus *bus; 40 const struct regmap_bus *bus;
53 void *bus_context;
54 const char *name;
55 41
56#ifdef CONFIG_DEBUG_FS 42#ifdef CONFIG_DEBUG_FS
57 struct dentry *debugfs; 43 struct dentry *debugfs;
58 const char *debugfs_name;
59
60 unsigned int debugfs_reg_len;
61 unsigned int debugfs_val_len;
62 unsigned int debugfs_tot_len;
63
64 struct list_head debugfs_off_cache;
65#endif 44#endif
66 45
67 unsigned int max_register; 46 unsigned int max_register;
@@ -69,18 +48,10 @@ struct regmap {
69 bool (*readable_reg)(struct device *dev, unsigned int reg); 48 bool (*readable_reg)(struct device *dev, unsigned int reg);
70 bool (*volatile_reg)(struct device *dev, unsigned int reg); 49 bool (*volatile_reg)(struct device *dev, unsigned int reg);
71 bool (*precious_reg)(struct device *dev, unsigned int reg); 50 bool (*precious_reg)(struct device *dev, unsigned int reg);
72 const struct regmap_access_table *wr_table;
73 const struct regmap_access_table *rd_table;
74 const struct regmap_access_table *volatile_table;
75 const struct regmap_access_table *precious_table;
76 51
77 u8 read_flag_mask; 52 u8 read_flag_mask;
78 u8 write_flag_mask; 53 u8 write_flag_mask;
79 54
80 /* number of bits to (left) shift the reg value when formatting*/
81 int reg_shift;
82 int reg_stride;
83
84 /* regcache specific members */ 55 /* regcache specific members */
85 const struct regcache_ops *cache_ops; 56 const struct regcache_ops *cache_ops;
86 enum regcache_type cache_type; 57 enum regcache_type cache_type;
@@ -108,12 +79,6 @@ struct regmap {
108 79
109 struct reg_default *patch; 80 struct reg_default *patch;
110 int patch_regs; 81 int patch_regs;
111
112 /* if set, converts bulk rw to single rw */
113 bool use_single_rw;
114
115 struct rb_root range_tree;
116 void *selector_work_buf; /* Scratch buffer used for selector */
117}; 82};
118 83
119struct regcache_ops { 84struct regcache_ops {
@@ -123,7 +88,7 @@ struct regcache_ops {
123 int (*exit)(struct regmap *map); 88 int (*exit)(struct regmap *map);
124 int (*read)(struct regmap *map, unsigned int reg, unsigned int *value); 89 int (*read)(struct regmap *map, unsigned int reg, unsigned int *value);
125 int (*write)(struct regmap *map, unsigned int reg, unsigned int value); 90 int (*write)(struct regmap *map, unsigned int reg, unsigned int value);
126 int (*sync)(struct regmap *map, unsigned int min, unsigned int max); 91 int (*sync)(struct regmap *map);
127}; 92};
128 93
129bool regmap_writeable(struct regmap *map, unsigned int reg); 94bool regmap_writeable(struct regmap *map, unsigned int reg);
@@ -134,29 +99,13 @@ bool regmap_precious(struct regmap *map, unsigned int reg);
134int _regmap_write(struct regmap *map, unsigned int reg, 99int _regmap_write(struct regmap *map, unsigned int reg,
135 unsigned int val); 100 unsigned int val);
136 101
137struct regmap_range_node {
138 struct rb_node node;
139 const char *name;
140 struct regmap *map;
141
142 unsigned int range_min;
143 unsigned int range_max;
144
145 unsigned int selector_reg;
146 unsigned int selector_mask;
147 int selector_shift;
148
149 unsigned int window_start;
150 unsigned int window_len;
151};
152
153#ifdef CONFIG_DEBUG_FS 102#ifdef CONFIG_DEBUG_FS
154extern void regmap_debugfs_initcall(void); 103extern void regmap_debugfs_initcall(void);
155extern void regmap_debugfs_init(struct regmap *map, const char *name); 104extern void regmap_debugfs_init(struct regmap *map);
156extern void regmap_debugfs_exit(struct regmap *map); 105extern void regmap_debugfs_exit(struct regmap *map);
157#else 106#else
158static inline void regmap_debugfs_initcall(void) { } 107static inline void regmap_debugfs_initcall(void) { }
159static inline void regmap_debugfs_init(struct regmap *map, const char *name) { } 108static inline void regmap_debugfs_init(struct regmap *map) { }
160static inline void regmap_debugfs_exit(struct regmap *map) { } 109static inline void regmap_debugfs_exit(struct regmap *map) { }
161#endif 110#endif
162 111
diff --git a/drivers/base/regmap/regcache-lzo.c b/drivers/base/regmap/regcache-lzo.c
index afd6aa91a0d..b7d16143ede 100644
--- a/drivers/base/regmap/regcache-lzo.c
+++ b/drivers/base/regmap/regcache-lzo.c
@@ -11,7 +11,6 @@
11 */ 11 */
12 12
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/device.h>
15#include <linux/lzo.h> 14#include <linux/lzo.h>
16 15
17#include "internal.h" 16#include "internal.h"
@@ -108,7 +107,7 @@ static int regcache_lzo_decompress_cache_block(struct regmap *map,
108static inline int regcache_lzo_get_blkindex(struct regmap *map, 107static inline int regcache_lzo_get_blkindex(struct regmap *map,
109 unsigned int reg) 108 unsigned int reg)
110{ 109{
111 return ((reg / map->reg_stride) * map->cache_word_size) / 110 return (reg * map->cache_word_size) /
112 DIV_ROUND_UP(map->cache_size_raw, 111 DIV_ROUND_UP(map->cache_size_raw,
113 regcache_lzo_block_count(map)); 112 regcache_lzo_block_count(map));
114} 113}
@@ -116,10 +115,9 @@ static inline int regcache_lzo_get_blkindex(struct regmap *map,
116static inline int regcache_lzo_get_blkpos(struct regmap *map, 115static inline int regcache_lzo_get_blkpos(struct regmap *map,
117 unsigned int reg) 116 unsigned int reg)
118{ 117{
119 return (reg / map->reg_stride) % 118 return reg % (DIV_ROUND_UP(map->cache_size_raw,
120 (DIV_ROUND_UP(map->cache_size_raw, 119 regcache_lzo_block_count(map)) /
121 regcache_lzo_block_count(map)) / 120 map->cache_word_size);
122 map->cache_word_size);
123} 121}
124 122
125static inline int regcache_lzo_get_blksize(struct regmap *map) 123static inline int regcache_lzo_get_blksize(struct regmap *map)
@@ -323,7 +321,7 @@ static int regcache_lzo_write(struct regmap *map,
323 } 321 }
324 322
325 /* set the bit so we know we have to sync this register */ 323 /* set the bit so we know we have to sync this register */
326 set_bit(reg / map->reg_stride, lzo_block->sync_bmp); 324 set_bit(reg, lzo_block->sync_bmp);
327 kfree(tmp_dst); 325 kfree(tmp_dst);
328 kfree(lzo_block->src); 326 kfree(lzo_block->src);
329 return 0; 327 return 0;
@@ -333,8 +331,7 @@ out:
333 return ret; 331 return ret;
334} 332}
335 333
336static int regcache_lzo_sync(struct regmap *map, unsigned int min, 334static int regcache_lzo_sync(struct regmap *map)
337 unsigned int max)
338{ 335{
339 struct regcache_lzo_ctx **lzo_blocks; 336 struct regcache_lzo_ctx **lzo_blocks;
340 unsigned int val; 337 unsigned int val;
@@ -342,21 +339,10 @@ static int regcache_lzo_sync(struct regmap *map, unsigned int min,
342 int ret; 339 int ret;
343 340
344 lzo_blocks = map->cache; 341 lzo_blocks = map->cache;
345 i = min; 342 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
346 for_each_set_bit_from(i, lzo_blocks[0]->sync_bmp,
347 lzo_blocks[0]->sync_bmp_nbits) {
348 if (i > max)
349 continue;
350
351 ret = regcache_read(map, i, &val); 343 ret = regcache_read(map, i, &val);
352 if (ret) 344 if (ret)
353 return ret; 345 return ret;
354
355 /* Is this the hardware default? If so skip. */
356 ret = regcache_lookup_reg(map, i);
357 if (ret > 0 && val == map->reg_defaults[ret].def)
358 continue;
359
360 map->cache_bypass = 1; 346 map->cache_bypass = 1;
361 ret = _regmap_write(map, i, val); 347 ret = _regmap_write(map, i, val);
362 map->cache_bypass = 0; 348 map->cache_bypass = 0;
diff --git a/drivers/base/regmap/regcache-rbtree.c b/drivers/base/regmap/regcache-rbtree.c
index e6732cf7c06..32620c4f168 100644
--- a/drivers/base/regmap/regcache-rbtree.c
+++ b/drivers/base/regmap/regcache-rbtree.c
@@ -11,7 +11,6 @@
11 */ 11 */
12 12
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/device.h>
15#include <linux/debugfs.h> 14#include <linux/debugfs.h>
16#include <linux/rbtree.h> 15#include <linux/rbtree.h>
17#include <linux/seq_file.h> 16#include <linux/seq_file.h>
@@ -39,12 +38,11 @@ struct regcache_rbtree_ctx {
39}; 38};
40 39
41static inline void regcache_rbtree_get_base_top_reg( 40static inline void regcache_rbtree_get_base_top_reg(
42 struct regmap *map,
43 struct regcache_rbtree_node *rbnode, 41 struct regcache_rbtree_node *rbnode,
44 unsigned int *base, unsigned int *top) 42 unsigned int *base, unsigned int *top)
45{ 43{
46 *base = rbnode->base_reg; 44 *base = rbnode->base_reg;
47 *top = rbnode->base_reg + ((rbnode->blklen - 1) * map->reg_stride); 45 *top = rbnode->base_reg + rbnode->blklen - 1;
48} 46}
49 47
50static unsigned int regcache_rbtree_get_register( 48static unsigned int regcache_rbtree_get_register(
@@ -71,8 +69,7 @@ static struct regcache_rbtree_node *regcache_rbtree_lookup(struct regmap *map,
71 69
72 rbnode = rbtree_ctx->cached_rbnode; 70 rbnode = rbtree_ctx->cached_rbnode;
73 if (rbnode) { 71 if (rbnode) {
74 regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg, 72 regcache_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
75 &top_reg);
76 if (reg >= base_reg && reg <= top_reg) 73 if (reg >= base_reg && reg <= top_reg)
77 return rbnode; 74 return rbnode;
78 } 75 }
@@ -80,8 +77,7 @@ static struct regcache_rbtree_node *regcache_rbtree_lookup(struct regmap *map,
80 node = rbtree_ctx->root.rb_node; 77 node = rbtree_ctx->root.rb_node;
81 while (node) { 78 while (node) {
82 rbnode = container_of(node, struct regcache_rbtree_node, node); 79 rbnode = container_of(node, struct regcache_rbtree_node, node);
83 regcache_rbtree_get_base_top_reg(map, rbnode, &base_reg, 80 regcache_rbtree_get_base_top_reg(rbnode, &base_reg, &top_reg);
84 &top_reg);
85 if (reg >= base_reg && reg <= top_reg) { 81 if (reg >= base_reg && reg <= top_reg) {
86 rbtree_ctx->cached_rbnode = rbnode; 82 rbtree_ctx->cached_rbnode = rbnode;
87 return rbnode; 83 return rbnode;
@@ -95,7 +91,7 @@ static struct regcache_rbtree_node *regcache_rbtree_lookup(struct regmap *map,
95 return NULL; 91 return NULL;
96} 92}
97 93
98static int regcache_rbtree_insert(struct regmap *map, struct rb_root *root, 94static int regcache_rbtree_insert(struct rb_root *root,
99 struct regcache_rbtree_node *rbnode) 95 struct regcache_rbtree_node *rbnode)
100{ 96{
101 struct rb_node **new, *parent; 97 struct rb_node **new, *parent;
@@ -109,7 +105,7 @@ static int regcache_rbtree_insert(struct regmap *map, struct rb_root *root,
109 rbnode_tmp = container_of(*new, struct regcache_rbtree_node, 105 rbnode_tmp = container_of(*new, struct regcache_rbtree_node,
110 node); 106 node);
111 /* base and top registers of the current rbnode */ 107 /* base and top registers of the current rbnode */
112 regcache_rbtree_get_base_top_reg(map, rbnode_tmp, &base_reg_tmp, 108 regcache_rbtree_get_base_top_reg(rbnode_tmp, &base_reg_tmp,
113 &top_reg_tmp); 109 &top_reg_tmp);
114 /* base register of the rbnode to be added */ 110 /* base register of the rbnode to be added */
115 base_reg = rbnode->base_reg; 111 base_reg = rbnode->base_reg;
@@ -141,31 +137,24 @@ static int rbtree_show(struct seq_file *s, void *ignored)
141 unsigned int base, top; 137 unsigned int base, top;
142 int nodes = 0; 138 int nodes = 0;
143 int registers = 0; 139 int registers = 0;
144 int this_registers, average;
145 140
146 map->lock(map); 141 mutex_lock(&map->lock);
147 142
148 for (node = rb_first(&rbtree_ctx->root); node != NULL; 143 for (node = rb_first(&rbtree_ctx->root); node != NULL;
149 node = rb_next(node)) { 144 node = rb_next(node)) {
150 n = container_of(node, struct regcache_rbtree_node, node); 145 n = container_of(node, struct regcache_rbtree_node, node);
151 146
152 regcache_rbtree_get_base_top_reg(map, n, &base, &top); 147 regcache_rbtree_get_base_top_reg(n, &base, &top);
153 this_registers = ((top - base) / map->reg_stride) + 1; 148 seq_printf(s, "%x-%x (%d)\n", base, top, top - base + 1);
154 seq_printf(s, "%x-%x (%d)\n", base, top, this_registers);
155 149
156 nodes++; 150 nodes++;
157 registers += this_registers; 151 registers += top - base + 1;
158 } 152 }
159 153
160 if (nodes)
161 average = registers / nodes;
162 else
163 average = 0;
164
165 seq_printf(s, "%d nodes, %d registers, average %d registers\n", 154 seq_printf(s, "%d nodes, %d registers, average %d registers\n",
166 nodes, registers, average); 155 nodes, registers, registers / nodes);
167 156
168 map->unlock(map); 157 mutex_unlock(&map->lock);
169 158
170 return 0; 159 return 0;
171} 160}
@@ -259,7 +248,7 @@ static int regcache_rbtree_read(struct regmap *map,
259 248
260 rbnode = regcache_rbtree_lookup(map, reg); 249 rbnode = regcache_rbtree_lookup(map, reg);
261 if (rbnode) { 250 if (rbnode) {
262 reg_tmp = (reg - rbnode->base_reg) / map->reg_stride; 251 reg_tmp = reg - rbnode->base_reg;
263 *value = regcache_rbtree_get_register(rbnode, reg_tmp, 252 *value = regcache_rbtree_get_register(rbnode, reg_tmp,
264 map->cache_word_size); 253 map->cache_word_size);
265 } else { 254 } else {
@@ -314,7 +303,7 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
314 */ 303 */
315 rbnode = regcache_rbtree_lookup(map, reg); 304 rbnode = regcache_rbtree_lookup(map, reg);
316 if (rbnode) { 305 if (rbnode) {
317 reg_tmp = (reg - rbnode->base_reg) / map->reg_stride; 306 reg_tmp = reg - rbnode->base_reg;
318 val = regcache_rbtree_get_register(rbnode, reg_tmp, 307 val = regcache_rbtree_get_register(rbnode, reg_tmp,
319 map->cache_word_size); 308 map->cache_word_size);
320 if (val == value) 309 if (val == value)
@@ -325,15 +314,13 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
325 /* look for an adjacent register to the one we are about to add */ 314 /* look for an adjacent register to the one we are about to add */
326 for (node = rb_first(&rbtree_ctx->root); node; 315 for (node = rb_first(&rbtree_ctx->root); node;
327 node = rb_next(node)) { 316 node = rb_next(node)) {
328 rbnode_tmp = rb_entry(node, struct regcache_rbtree_node, 317 rbnode_tmp = rb_entry(node, struct regcache_rbtree_node, node);
329 node);
330 for (i = 0; i < rbnode_tmp->blklen; i++) { 318 for (i = 0; i < rbnode_tmp->blklen; i++) {
331 reg_tmp = rbnode_tmp->base_reg + 319 reg_tmp = rbnode_tmp->base_reg + i;
332 (i * map->reg_stride); 320 if (abs(reg_tmp - reg) != 1)
333 if (abs(reg_tmp - reg) != map->reg_stride)
334 continue; 321 continue;
335 /* decide where in the block to place our register */ 322 /* decide where in the block to place our register */
336 if (reg_tmp + map->reg_stride == reg) 323 if (reg_tmp + 1 == reg)
337 pos = i + 1; 324 pos = i + 1;
338 else 325 else
339 pos = i; 326 pos = i;
@@ -363,15 +350,14 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
363 return -ENOMEM; 350 return -ENOMEM;
364 } 351 }
365 regcache_rbtree_set_register(rbnode, 0, value, map->cache_word_size); 352 regcache_rbtree_set_register(rbnode, 0, value, map->cache_word_size);
366 regcache_rbtree_insert(map, &rbtree_ctx->root, rbnode); 353 regcache_rbtree_insert(&rbtree_ctx->root, rbnode);
367 rbtree_ctx->cached_rbnode = rbnode; 354 rbtree_ctx->cached_rbnode = rbnode;
368 } 355 }
369 356
370 return 0; 357 return 0;
371} 358}
372 359
373static int regcache_rbtree_sync(struct regmap *map, unsigned int min, 360static int regcache_rbtree_sync(struct regmap *map)
374 unsigned int max)
375{ 361{
376 struct regcache_rbtree_ctx *rbtree_ctx; 362 struct regcache_rbtree_ctx *rbtree_ctx;
377 struct rb_node *node; 363 struct rb_node *node;
@@ -379,37 +365,19 @@ static int regcache_rbtree_sync(struct regmap *map, unsigned int min,
379 unsigned int regtmp; 365 unsigned int regtmp;
380 unsigned int val; 366 unsigned int val;
381 int ret; 367 int ret;
382 int i, base, end; 368 int i;
383 369
384 rbtree_ctx = map->cache; 370 rbtree_ctx = map->cache;
385 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) { 371 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
386 rbnode = rb_entry(node, struct regcache_rbtree_node, node); 372 rbnode = rb_entry(node, struct regcache_rbtree_node, node);
387 373 for (i = 0; i < rbnode->blklen; i++) {
388 if (rbnode->base_reg < min) 374 regtmp = rbnode->base_reg + i;
389 continue;
390 if (rbnode->base_reg > max)
391 break;
392 if (rbnode->base_reg + rbnode->blklen < min)
393 continue;
394
395 if (min > rbnode->base_reg)
396 base = min - rbnode->base_reg;
397 else
398 base = 0;
399
400 if (max < rbnode->base_reg + rbnode->blklen)
401 end = rbnode->base_reg + rbnode->blklen - max;
402 else
403 end = rbnode->blklen;
404
405 for (i = base; i < end; i++) {
406 regtmp = rbnode->base_reg + (i * map->reg_stride);
407 val = regcache_rbtree_get_register(rbnode, i, 375 val = regcache_rbtree_get_register(rbnode, i,
408 map->cache_word_size); 376 map->cache_word_size);
409 377
410 /* Is this the hardware default? If so skip. */ 378 /* Is this the hardware default? If so skip. */
411 ret = regcache_lookup_reg(map, regtmp); 379 ret = regcache_lookup_reg(map, i);
412 if (ret >= 0 && val == map->reg_defaults[ret].def) 380 if (ret > 0 && val == map->reg_defaults[ret].def)
413 continue; 381 continue;
414 382
415 map->cache_bypass = 1; 383 map->cache_bypass = 1;
diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
index 835883bda97..86cc34251ac 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -11,8 +11,6 @@
11 */ 11 */
12 12
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/export.h>
15#include <linux/device.h>
16#include <trace/events/regmap.h> 14#include <trace/events/regmap.h>
17#include <linux/bsearch.h> 15#include <linux/bsearch.h>
18#include <linux/sort.h> 16#include <linux/sort.h>
@@ -59,7 +57,7 @@ static int regcache_hw_init(struct regmap *map)
59 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) { 57 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
60 val = regcache_get_val(map->reg_defaults_raw, 58 val = regcache_get_val(map->reg_defaults_raw,
61 i, map->cache_word_size); 59 i, map->cache_word_size);
62 if (regmap_volatile(map, i * map->reg_stride)) 60 if (regmap_volatile(map, i))
63 continue; 61 continue;
64 count++; 62 count++;
65 } 63 }
@@ -76,9 +74,9 @@ static int regcache_hw_init(struct regmap *map)
76 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) { 74 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
77 val = regcache_get_val(map->reg_defaults_raw, 75 val = regcache_get_val(map->reg_defaults_raw,
78 i, map->cache_word_size); 76 i, map->cache_word_size);
79 if (regmap_volatile(map, i * map->reg_stride)) 77 if (regmap_volatile(map, i))
80 continue; 78 continue;
81 map->reg_defaults[j].reg = i * map->reg_stride; 79 map->reg_defaults[j].reg = i;
82 map->reg_defaults[j].def = val; 80 map->reg_defaults[j].def = val;
83 j++; 81 j++;
84 } 82 }
@@ -98,10 +96,6 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
98 int i; 96 int i;
99 void *tmp_buf; 97 void *tmp_buf;
100 98
101 for (i = 0; i < config->num_reg_defaults; i++)
102 if (config->reg_defaults[i].reg % map->reg_stride)
103 return -EINVAL;
104
105 if (map->cache_type == REGCACHE_NONE) { 99 if (map->cache_type == REGCACHE_NONE) {
106 map->cache_bypass = true; 100 map->cache_bypass = true;
107 return 0; 101 return 0;
@@ -262,13 +256,14 @@ int regcache_write(struct regmap *map,
262int regcache_sync(struct regmap *map) 256int regcache_sync(struct regmap *map)
263{ 257{
264 int ret = 0; 258 int ret = 0;
259 unsigned int val;
265 unsigned int i; 260 unsigned int i;
266 const char *name; 261 const char *name;
267 unsigned int bypass; 262 unsigned int bypass;
268 263
269 BUG_ON(!map->cache_ops || !map->cache_ops->sync); 264 BUG_ON(!map->cache_ops);
270 265
271 map->lock(map); 266 mutex_lock(&map->lock);
272 /* Remember the initial bypass state */ 267 /* Remember the initial bypass state */
273 bypass = map->cache_bypass; 268 bypass = map->cache_bypass;
274 dev_dbg(map->dev, "Syncing %s cache\n", 269 dev_dbg(map->dev, "Syncing %s cache\n",
@@ -282,10 +277,6 @@ int regcache_sync(struct regmap *map)
282 /* Apply any patch first */ 277 /* Apply any patch first */
283 map->cache_bypass = 1; 278 map->cache_bypass = 1;
284 for (i = 0; i < map->patch_regs; i++) { 279 for (i = 0; i < map->patch_regs; i++) {
285 if (map->patch[i].reg % map->reg_stride) {
286 ret = -EINVAL;
287 goto out;
288 }
289 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); 280 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
290 if (ret != 0) { 281 if (ret != 0) {
291 dev_err(map->dev, "Failed to write %x = %x: %d\n", 282 dev_err(map->dev, "Failed to write %x = %x: %d\n",
@@ -295,68 +286,35 @@ int regcache_sync(struct regmap *map)
295 } 286 }
296 map->cache_bypass = 0; 287 map->cache_bypass = 0;
297 288
298 ret = map->cache_ops->sync(map, 0, map->max_register); 289 if (map->cache_ops->sync) {
299 290 ret = map->cache_ops->sync(map);
300 if (ret == 0) 291 } else {
301 map->cache_dirty = false; 292 for (i = 0; i < map->num_reg_defaults; i++) {
293 ret = regcache_read(map, i, &val);
294 if (ret < 0)
295 goto out;
296 map->cache_bypass = 1;
297 ret = _regmap_write(map, i, val);
298 map->cache_bypass = 0;
299 if (ret < 0)
300 goto out;
301 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
302 map->reg_defaults[i].reg,
303 map->reg_defaults[i].def);
304 }
302 305
306 }
303out: 307out:
304 trace_regcache_sync(map->dev, name, "stop"); 308 trace_regcache_sync(map->dev, name, "stop");
305 /* Restore the bypass state */ 309 /* Restore the bypass state */
306 map->cache_bypass = bypass; 310 map->cache_bypass = bypass;
307 map->unlock(map); 311 mutex_unlock(&map->lock);
308 312
309 return ret; 313 return ret;
310} 314}
311EXPORT_SYMBOL_GPL(regcache_sync); 315EXPORT_SYMBOL_GPL(regcache_sync);
312 316
313/** 317/**
314 * regcache_sync_region: Sync part of the register cache with the hardware.
315 *
316 * @map: map to sync.
317 * @min: first register to sync
318 * @max: last register to sync
319 *
320 * Write all non-default register values in the specified region to
321 * the hardware.
322 *
323 * Return a negative value on failure, 0 on success.
324 */
325int regcache_sync_region(struct regmap *map, unsigned int min,
326 unsigned int max)
327{
328 int ret = 0;
329 const char *name;
330 unsigned int bypass;
331
332 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
333
334 map->lock(map);
335
336 /* Remember the initial bypass state */
337 bypass = map->cache_bypass;
338
339 name = map->cache_ops->name;
340 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
341
342 trace_regcache_sync(map->dev, name, "start region");
343
344 if (!map->cache_dirty)
345 goto out;
346
347 ret = map->cache_ops->sync(map, min, max);
348
349out:
350 trace_regcache_sync(map->dev, name, "stop region");
351 /* Restore the bypass state */
352 map->cache_bypass = bypass;
353 map->unlock(map);
354
355 return ret;
356}
357EXPORT_SYMBOL_GPL(regcache_sync_region);
358
359/**
360 * regcache_cache_only: Put a register map into cache only mode 318 * regcache_cache_only: Put a register map into cache only mode
361 * 319 *
362 * @map: map to configure 320 * @map: map to configure
@@ -370,11 +328,10 @@ EXPORT_SYMBOL_GPL(regcache_sync_region);
370 */ 328 */
371void regcache_cache_only(struct regmap *map, bool enable) 329void regcache_cache_only(struct regmap *map, bool enable)
372{ 330{
373 map->lock(map); 331 mutex_lock(&map->lock);
374 WARN_ON(map->cache_bypass && enable); 332 WARN_ON(map->cache_bypass && enable);
375 map->cache_only = enable; 333 map->cache_only = enable;
376 trace_regmap_cache_only(map->dev, enable); 334 mutex_unlock(&map->lock);
377 map->unlock(map);
378} 335}
379EXPORT_SYMBOL_GPL(regcache_cache_only); 336EXPORT_SYMBOL_GPL(regcache_cache_only);
380 337
@@ -389,9 +346,9 @@ EXPORT_SYMBOL_GPL(regcache_cache_only);
389 */ 346 */
390void regcache_mark_dirty(struct regmap *map) 347void regcache_mark_dirty(struct regmap *map)
391{ 348{
392 map->lock(map); 349 mutex_lock(&map->lock);
393 map->cache_dirty = true; 350 map->cache_dirty = true;
394 map->unlock(map); 351 mutex_unlock(&map->lock);
395} 352}
396EXPORT_SYMBOL_GPL(regcache_mark_dirty); 353EXPORT_SYMBOL_GPL(regcache_mark_dirty);
397 354
@@ -408,11 +365,10 @@ EXPORT_SYMBOL_GPL(regcache_mark_dirty);
408 */ 365 */
409void regcache_cache_bypass(struct regmap *map, bool enable) 366void regcache_cache_bypass(struct regmap *map, bool enable)
410{ 367{
411 map->lock(map); 368 mutex_lock(&map->lock);
412 WARN_ON(map->cache_only && enable); 369 WARN_ON(map->cache_only && enable);
413 map->cache_bypass = enable; 370 map->cache_bypass = enable;
414 trace_regmap_cache_bypass(map->dev, enable); 371 mutex_unlock(&map->lock);
415 map->unlock(map);
416} 372}
417EXPORT_SYMBOL_GPL(regcache_cache_bypass); 373EXPORT_SYMBOL_GPL(regcache_cache_bypass);
418 374
@@ -434,13 +390,6 @@ bool regcache_set_val(void *base, unsigned int idx,
434 cache[idx] = val; 390 cache[idx] = val;
435 break; 391 break;
436 } 392 }
437 case 4: {
438 u32 *cache = base;
439 if (cache[idx] == val)
440 return true;
441 cache[idx] = val;
442 break;
443 }
444 default: 393 default:
445 BUG(); 394 BUG();
446 } 395 }
@@ -462,10 +411,6 @@ unsigned int regcache_get_val(const void *base, unsigned int idx,
462 const u16 *cache = base; 411 const u16 *cache = base;
463 return cache[idx]; 412 return cache[idx];
464 } 413 }
465 case 4: {
466 const u32 *cache = base;
467 return cache[idx];
468 }
469 default: 414 default:
470 BUG(); 415 BUG();
471 } 416 }
diff --git a/drivers/base/regmap/regmap-debugfs.c b/drivers/base/regmap/regmap-debugfs.c
index 46a213a596e..b3b4b8f7f40 100644
--- a/drivers/base/regmap/regmap-debugfs.c
+++ b/drivers/base/regmap/regmap-debugfs.c
@@ -11,10 +11,10 @@
11 */ 11 */
12 12
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/module.h>
14#include <linux/mutex.h> 15#include <linux/mutex.h>
15#include <linux/debugfs.h> 16#include <linux/debugfs.h>
16#include <linux/uaccess.h> 17#include <linux/uaccess.h>
17#include <linux/device.h>
18 18
19#include "internal.h" 19#include "internal.h"
20 20
@@ -27,138 +27,23 @@ static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size)
27 return strlen(buf); 27 return strlen(buf);
28} 28}
29 29
30static ssize_t regmap_name_read_file(struct file *file, 30static int regmap_open_file(struct inode *inode, struct file *file)
31 char __user *user_buf, size_t count,
32 loff_t *ppos)
33{ 31{
34 struct regmap *map = file->private_data; 32 file->private_data = inode->i_private;
35 int ret; 33 return 0;
36 char *buf;
37
38 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
39 if (!buf)
40 return -ENOMEM;
41
42 ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name);
43 if (ret < 0) {
44 kfree(buf);
45 return ret;
46 }
47
48 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
49 kfree(buf);
50 return ret;
51} 34}
52 35
53static const struct file_operations regmap_name_fops = { 36static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
54 .open = simple_open, 37 size_t count, loff_t *ppos)
55 .read = regmap_name_read_file,
56 .llseek = default_llseek,
57};
58
59static void regmap_debugfs_free_dump_cache(struct regmap *map)
60{
61 struct regmap_debugfs_off_cache *c;
62
63 while (!list_empty(&map->debugfs_off_cache)) {
64 c = list_first_entry(&map->debugfs_off_cache,
65 struct regmap_debugfs_off_cache,
66 list);
67 list_del(&c->list);
68 kfree(c);
69 }
70}
71
72/*
73 * Work out where the start offset maps into register numbers, bearing
74 * in mind that we suppress hidden registers.
75 */
76static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
77 unsigned int base,
78 loff_t from,
79 loff_t *pos)
80{
81 struct regmap_debugfs_off_cache *c = NULL;
82 loff_t p = 0;
83 unsigned int i, ret;
84
85 /*
86 * If we don't have a cache build one so we don't have to do a
87 * linear scan each time.
88 */
89 if (list_empty(&map->debugfs_off_cache)) {
90 for (i = base; i <= map->max_register; i += map->reg_stride) {
91 /* Skip unprinted registers, closing off cache entry */
92 if (!regmap_readable(map, i) ||
93 regmap_precious(map, i)) {
94 if (c) {
95 c->max = p - 1;
96 list_add_tail(&c->list,
97 &map->debugfs_off_cache);
98 c = NULL;
99 }
100
101 continue;
102 }
103
104 /* No cache entry? Start a new one */
105 if (!c) {
106 c = kzalloc(sizeof(*c), GFP_KERNEL);
107 if (!c) {
108 regmap_debugfs_free_dump_cache(map);
109 return base;
110 }
111 c->min = p;
112 c->base_reg = i;
113 }
114
115 p += map->debugfs_tot_len;
116 }
117 }
118
119 /* Close the last entry off if we didn't scan beyond it */
120 if (c) {
121 c->max = p - 1;
122 list_add_tail(&c->list,
123 &map->debugfs_off_cache);
124 } else {
125 return base;
126 }
127
128 /*
129 * This should never happen; we return above if we fail to
130 * allocate and we should never be in this code if there are
131 * no registers at all.
132 */
133 if (list_empty(&map->debugfs_off_cache)) {
134 WARN_ON(list_empty(&map->debugfs_off_cache));
135 return base;
136 }
137
138 /* Find the relevant block */
139 list_for_each_entry(c, &map->debugfs_off_cache, list) {
140 if (from >= c->min && from <= c->max) {
141 *pos = c->min;
142 return c->base_reg;
143 }
144
145 *pos = c->min;
146 ret = c->base_reg;
147 }
148
149 return ret;
150}
151
152static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
153 unsigned int to, char __user *user_buf,
154 size_t count, loff_t *ppos)
155{ 38{
39 int reg_len, val_len, tot_len;
156 size_t buf_pos = 0; 40 size_t buf_pos = 0;
157 loff_t p = *ppos; 41 loff_t p = 0;
158 ssize_t ret; 42 ssize_t ret;
159 int i; 43 int i;
44 struct regmap *map = file->private_data;
160 char *buf; 45 char *buf;
161 unsigned int val, start_reg; 46 unsigned int val;
162 47
163 if (*ppos < 0 || !count) 48 if (*ppos < 0 || !count)
164 return -EINVAL; 49 return -EINVAL;
@@ -168,18 +53,11 @@ static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
168 return -ENOMEM; 53 return -ENOMEM;
169 54
170 /* Calculate the length of a fixed format */ 55 /* Calculate the length of a fixed format */
171 if (!map->debugfs_tot_len) { 56 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
172 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register, 57 val_len = 2 * map->format.val_bytes;
173 buf, count); 58 tot_len = reg_len + val_len + 3; /* : \n */
174 map->debugfs_val_len = 2 * map->format.val_bytes;
175 map->debugfs_tot_len = map->debugfs_reg_len +
176 map->debugfs_val_len + 3; /* : \n */
177 }
178
179 /* Work out which register we're starting at */
180 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
181 59
182 for (i = start_reg; i <= to; i += map->reg_stride) { 60 for (i = 0; i < map->max_register + 1; i++) {
183 if (!regmap_readable(map, i)) 61 if (!regmap_readable(map, i))
184 continue; 62 continue;
185 63
@@ -189,27 +67,26 @@ static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
189 /* If we're in the region the user is trying to read */ 67 /* If we're in the region the user is trying to read */
190 if (p >= *ppos) { 68 if (p >= *ppos) {
191 /* ...but not beyond it */ 69 /* ...but not beyond it */
192 if (buf_pos + 1 + map->debugfs_tot_len >= count) 70 if (buf_pos >= count - 1 - tot_len)
193 break; 71 break;
194 72
195 /* Format the register */ 73 /* Format the register */
196 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ", 74 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
197 map->debugfs_reg_len, i - from); 75 reg_len, i);
198 buf_pos += map->debugfs_reg_len + 2; 76 buf_pos += reg_len + 2;
199 77
200 /* Format the value, write all X if we can't read */ 78 /* Format the value, write all X if we can't read */
201 ret = regmap_read(map, i, &val); 79 ret = regmap_read(map, i, &val);
202 if (ret == 0) 80 if (ret == 0)
203 snprintf(buf + buf_pos, count - buf_pos, 81 snprintf(buf + buf_pos, count - buf_pos,
204 "%.*x", map->debugfs_val_len, val); 82 "%.*x", val_len, val);
205 else 83 else
206 memset(buf + buf_pos, 'X', 84 memset(buf + buf_pos, 'X', val_len);
207 map->debugfs_val_len);
208 buf_pos += 2 * map->format.val_bytes; 85 buf_pos += 2 * map->format.val_bytes;
209 86
210 buf[buf_pos++] = '\n'; 87 buf[buf_pos++] = '\n';
211 } 88 }
212 p += map->debugfs_tot_len; 89 p += tot_len;
213 } 90 }
214 91
215 ret = buf_pos; 92 ret = buf_pos;
@@ -226,76 +103,9 @@ out:
226 return ret; 103 return ret;
227} 104}
228 105
229static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
230 size_t count, loff_t *ppos)
231{
232 struct regmap *map = file->private_data;
233
234 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
235 count, ppos);
236}
237
238#undef REGMAP_ALLOW_WRITE_DEBUGFS
239#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
240/*
241 * This can be dangerous especially when we have clients such as
242 * PMICs, therefore don't provide any real compile time configuration option
243 * for this feature, people who want to use this will need to modify
244 * the source code directly.
245 */
246static ssize_t regmap_map_write_file(struct file *file,
247 const char __user *user_buf,
248 size_t count, loff_t *ppos)
249{
250 char buf[32];
251 size_t buf_size;
252 char *start = buf;
253 unsigned long reg, value;
254 struct regmap *map = file->private_data;
255
256 buf_size = min(count, (sizeof(buf)-1));
257 if (copy_from_user(buf, user_buf, buf_size))
258 return -EFAULT;
259 buf[buf_size] = 0;
260
261 while (*start == ' ')
262 start++;
263 reg = simple_strtoul(start, &start, 16);
264 while (*start == ' ')
265 start++;
266 if (strict_strtoul(start, 16, &value))
267 return -EINVAL;
268
269 /* Userspace has been fiddling around behind the kernel's back */
270 add_taint(TAINT_USER);
271
272 regmap_write(map, reg, value);
273 return buf_size;
274}
275#else
276#define regmap_map_write_file NULL
277#endif
278
279static const struct file_operations regmap_map_fops = { 106static const struct file_operations regmap_map_fops = {
280 .open = simple_open, 107 .open = regmap_open_file,
281 .read = regmap_map_read_file, 108 .read = regmap_map_read_file,
282 .write = regmap_map_write_file,
283 .llseek = default_llseek,
284};
285
286static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
287 size_t count, loff_t *ppos)
288{
289 struct regmap_range_node *range = file->private_data;
290 struct regmap *map = range->map;
291
292 return regmap_read_debugfs(map, range->range_min, range->range_max,
293 user_buf, count, ppos);
294}
295
296static const struct file_operations regmap_range_fops = {
297 .open = simple_open,
298 .read = regmap_range_read_file,
299 .llseek = default_llseek, 109 .llseek = default_llseek,
300}; 110};
301 111
@@ -322,7 +132,7 @@ static ssize_t regmap_access_read_file(struct file *file,
322 reg_len = regmap_calc_reg_len(map->max_register, buf, count); 132 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
323 tot_len = reg_len + 10; /* ': R W V P\n' */ 133 tot_len = reg_len + 10; /* ': R W V P\n' */
324 134
325 for (i = 0; i <= map->max_register; i += map->reg_stride) { 135 for (i = 0; i < map->max_register + 1; i++) {
326 /* Ignore registers which are neither readable nor writable */ 136 /* Ignore registers which are neither readable nor writable */
327 if (!regmap_readable(map, i) && !regmap_writeable(map, i)) 137 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
328 continue; 138 continue;
@@ -362,35 +172,20 @@ out:
362} 172}
363 173
364static const struct file_operations regmap_access_fops = { 174static const struct file_operations regmap_access_fops = {
365 .open = simple_open, 175 .open = regmap_open_file,
366 .read = regmap_access_read_file, 176 .read = regmap_access_read_file,
367 .llseek = default_llseek, 177 .llseek = default_llseek,
368}; 178};
369 179
370void regmap_debugfs_init(struct regmap *map, const char *name) 180void regmap_debugfs_init(struct regmap *map)
371{ 181{
372 struct rb_node *next; 182 map->debugfs = debugfs_create_dir(dev_name(map->dev),
373 struct regmap_range_node *range_node; 183 regmap_debugfs_root);
374
375 INIT_LIST_HEAD(&map->debugfs_off_cache);
376
377 if (name) {
378 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
379 dev_name(map->dev), name);
380 name = map->debugfs_name;
381 } else {
382 name = dev_name(map->dev);
383 }
384
385 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
386 if (!map->debugfs) { 184 if (!map->debugfs) {
387 dev_warn(map->dev, "Failed to create debugfs directory\n"); 185 dev_warn(map->dev, "Failed to create debugfs directory\n");
388 return; 186 return;
389 } 187 }
390 188
391 debugfs_create_file("name", 0400, map->debugfs,
392 map, &regmap_name_fops);
393
394 if (map->max_register) { 189 if (map->max_register) {
395 debugfs_create_file("registers", 0400, map->debugfs, 190 debugfs_create_file("registers", 0400, map->debugfs,
396 map, &regmap_map_fops); 191 map, &regmap_map_fops);
@@ -406,25 +201,11 @@ void regmap_debugfs_init(struct regmap *map, const char *name)
406 debugfs_create_bool("cache_bypass", 0400, map->debugfs, 201 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
407 &map->cache_bypass); 202 &map->cache_bypass);
408 } 203 }
409
410 next = rb_first(&map->range_tree);
411 while (next) {
412 range_node = rb_entry(next, struct regmap_range_node, node);
413
414 if (range_node->name)
415 debugfs_create_file(range_node->name, 0400,
416 map->debugfs, range_node,
417 &regmap_range_fops);
418
419 next = rb_next(&range_node->node);
420 }
421} 204}
422 205
423void regmap_debugfs_exit(struct regmap *map) 206void regmap_debugfs_exit(struct regmap *map)
424{ 207{
425 debugfs_remove_recursive(map->debugfs); 208 debugfs_remove_recursive(map->debugfs);
426 regmap_debugfs_free_dump_cache(map);
427 kfree(map->debugfs_name);
428} 209}
429 210
430void regmap_debugfs_initcall(void) 211void regmap_debugfs_initcall(void)
diff --git a/drivers/base/regmap/regmap-i2c.c b/drivers/base/regmap/regmap-i2c.c
index fa6bf5279d2..9a3a8c56438 100644
--- a/drivers/base/regmap/regmap-i2c.c
+++ b/drivers/base/regmap/regmap-i2c.c
@@ -15,9 +15,8 @@
15#include <linux/module.h> 15#include <linux/module.h>
16#include <linux/init.h> 16#include <linux/init.h>
17 17
18static int regmap_i2c_write(void *context, const void *data, size_t count) 18static int regmap_i2c_write(struct device *dev, const void *data, size_t count)
19{ 19{
20 struct device *dev = context;
21 struct i2c_client *i2c = to_i2c_client(dev); 20 struct i2c_client *i2c = to_i2c_client(dev);
22 int ret; 21 int ret;
23 22
@@ -30,11 +29,10 @@ static int regmap_i2c_write(void *context, const void *data, size_t count)
30 return -EIO; 29 return -EIO;
31} 30}
32 31
33static int regmap_i2c_gather_write(void *context, 32static int regmap_i2c_gather_write(struct device *dev,
34 const void *reg, size_t reg_size, 33 const void *reg, size_t reg_size,
35 const void *val, size_t val_size) 34 const void *val, size_t val_size)
36{ 35{
37 struct device *dev = context;
38 struct i2c_client *i2c = to_i2c_client(dev); 36 struct i2c_client *i2c = to_i2c_client(dev);
39 struct i2c_msg xfer[2]; 37 struct i2c_msg xfer[2];
40 int ret; 38 int ret;
@@ -42,7 +40,7 @@ static int regmap_i2c_gather_write(void *context,
42 /* If the I2C controller can't do a gather tell the core, it 40 /* If the I2C controller can't do a gather tell the core, it
43 * will substitute in a linear write for us. 41 * will substitute in a linear write for us.
44 */ 42 */
45 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_NOSTART)) 43 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_PROTOCOL_MANGLING))
46 return -ENOTSUPP; 44 return -ENOTSUPP;
47 45
48 xfer[0].addr = i2c->addr; 46 xfer[0].addr = i2c->addr;
@@ -64,11 +62,10 @@ static int regmap_i2c_gather_write(void *context,
64 return -EIO; 62 return -EIO;
65} 63}
66 64
67static int regmap_i2c_read(void *context, 65static int regmap_i2c_read(struct device *dev,
68 const void *reg, size_t reg_size, 66 const void *reg, size_t reg_size,
69 void *val, size_t val_size) 67 void *val, size_t val_size)
70{ 68{
71 struct device *dev = context;
72 struct i2c_client *i2c = to_i2c_client(dev); 69 struct i2c_client *i2c = to_i2c_client(dev);
73 struct i2c_msg xfer[2]; 70 struct i2c_msg xfer[2];
74 int ret; 71 int ret;
@@ -110,7 +107,7 @@ static struct regmap_bus regmap_i2c = {
110struct regmap *regmap_init_i2c(struct i2c_client *i2c, 107struct regmap *regmap_init_i2c(struct i2c_client *i2c,
111 const struct regmap_config *config) 108 const struct regmap_config *config)
112{ 109{
113 return regmap_init(&i2c->dev, &regmap_i2c, &i2c->dev, config); 110 return regmap_init(&i2c->dev, &regmap_i2c, config);
114} 111}
115EXPORT_SYMBOL_GPL(regmap_init_i2c); 112EXPORT_SYMBOL_GPL(regmap_init_i2c);
116 113
@@ -127,7 +124,7 @@ EXPORT_SYMBOL_GPL(regmap_init_i2c);
127struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c, 124struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
128 const struct regmap_config *config) 125 const struct regmap_config *config)
129{ 126{
130 return devm_regmap_init(&i2c->dev, &regmap_i2c, &i2c->dev, config); 127 return devm_regmap_init(&i2c->dev, &regmap_i2c, config);
131} 128}
132EXPORT_SYMBOL_GPL(devm_regmap_init_i2c); 129EXPORT_SYMBOL_GPL(devm_regmap_init_i2c);
133 130
diff --git a/drivers/base/regmap/regmap-irq.c b/drivers/base/regmap/regmap-irq.c
index 5972ad95854..428836fc583 100644
--- a/drivers/base/regmap/regmap-irq.c
+++ b/drivers/base/regmap/regmap-irq.c
@@ -11,42 +11,32 @@
11 */ 11 */
12 12
13#include <linux/export.h> 13#include <linux/export.h>
14#include <linux/device.h>
15#include <linux/regmap.h> 14#include <linux/regmap.h>
16#include <linux/irq.h> 15#include <linux/irq.h>
17#include <linux/interrupt.h> 16#include <linux/interrupt.h>
18#include <linux/irqdomain.h>
19#include <linux/pm_runtime.h>
20#include <linux/slab.h> 17#include <linux/slab.h>
21 18
22#include "internal.h" 19#include "internal.h"
23 20
24struct regmap_irq_chip_data { 21struct regmap_irq_chip_data {
25 struct mutex lock; 22 struct mutex lock;
26 struct irq_chip irq_chip;
27 23
28 struct regmap *map; 24 struct regmap *map;
29 const struct regmap_irq_chip *chip; 25 struct regmap_irq_chip *chip;
30 26
31 int irq_base; 27 int irq_base;
32 struct irq_domain *domain;
33
34 int irq;
35 int wake_count;
36 28
29 void *status_reg_buf;
37 unsigned int *status_buf; 30 unsigned int *status_buf;
38 unsigned int *mask_buf; 31 unsigned int *mask_buf;
39 unsigned int *mask_buf_def; 32 unsigned int *mask_buf_def;
40 unsigned int *wake_buf;
41
42 unsigned int irq_reg_stride;
43}; 33};
44 34
45static inline const 35static inline const
46struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data, 36struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
47 int irq) 37 int irq)
48{ 38{
49 return &data->chip->irqs[irq]; 39 return &data->chip->irqs[irq - data->irq_base];
50} 40}
51 41
52static void regmap_irq_lock(struct irq_data *data) 42static void regmap_irq_lock(struct irq_data *data)
@@ -59,16 +49,7 @@ static void regmap_irq_lock(struct irq_data *data)
59static void regmap_irq_sync_unlock(struct irq_data *data) 49static void regmap_irq_sync_unlock(struct irq_data *data)
60{ 50{
61 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 51 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
62 struct regmap *map = d->map;
63 int i, ret; 52 int i, ret;
64 u32 reg;
65
66 if (d->chip->runtime_pm) {
67 ret = pm_runtime_get_sync(map->dev);
68 if (ret < 0)
69 dev_err(map->dev, "IRQ sync failed to resume: %d\n",
70 ret);
71 }
72 53
73 /* 54 /*
74 * If there's been a change in the mask write it back to the 55 * If there's been a change in the mask write it back to the
@@ -76,99 +57,56 @@ static void regmap_irq_sync_unlock(struct irq_data *data)
76 * suppress pointless writes. 57 * suppress pointless writes.
77 */ 58 */
78 for (i = 0; i < d->chip->num_regs; i++) { 59 for (i = 0; i < d->chip->num_regs; i++) {
79 reg = d->chip->mask_base + 60 ret = regmap_update_bits(d->map, d->chip->mask_base + i,
80 (i * map->reg_stride * d->irq_reg_stride);
81 if (d->chip->mask_invert)
82 ret = regmap_update_bits(d->map, reg,
83 d->mask_buf_def[i], ~d->mask_buf[i]);
84 else
85 ret = regmap_update_bits(d->map, reg,
86 d->mask_buf_def[i], d->mask_buf[i]); 61 d->mask_buf_def[i], d->mask_buf[i]);
87 if (ret != 0) 62 if (ret != 0)
88 dev_err(d->map->dev, "Failed to sync masks in %x\n", 63 dev_err(d->map->dev, "Failed to sync masks in %x\n",
89 reg); 64 d->chip->mask_base + i);
90 } 65 }
91 66
92 if (d->chip->runtime_pm)
93 pm_runtime_put(map->dev);
94
95 /* If we've changed our wakeup count propagate it to the parent */
96 if (d->wake_count < 0)
97 for (i = d->wake_count; i < 0; i++)
98 irq_set_irq_wake(d->irq, 0);
99 else if (d->wake_count > 0)
100 for (i = 0; i < d->wake_count; i++)
101 irq_set_irq_wake(d->irq, 1);
102
103 d->wake_count = 0;
104
105 mutex_unlock(&d->lock); 67 mutex_unlock(&d->lock);
106} 68}
107 69
108static void regmap_irq_enable(struct irq_data *data) 70static void regmap_irq_enable(struct irq_data *data)
109{ 71{
110 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 72 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
111 struct regmap *map = d->map; 73 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);
112 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
113 74
114 d->mask_buf[irq_data->reg_offset / map->reg_stride] &= ~irq_data->mask; 75 d->mask_buf[irq_data->reg_offset] &= ~irq_data->mask;
115} 76}
116 77
117static void regmap_irq_disable(struct irq_data *data) 78static void regmap_irq_disable(struct irq_data *data)
118{ 79{
119 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); 80 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
120 struct regmap *map = d->map; 81 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);
121 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
122 82
123 d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask; 83 d->mask_buf[irq_data->reg_offset] |= irq_data->mask;
124} 84}
125 85
126static int regmap_irq_set_wake(struct irq_data *data, unsigned int on) 86static struct irq_chip regmap_irq_chip = {
127{ 87 .name = "regmap",
128 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
129 struct regmap *map = d->map;
130 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
131
132 if (!d->chip->wake_base)
133 return -EINVAL;
134
135 if (on) {
136 d->wake_buf[irq_data->reg_offset / map->reg_stride]
137 &= ~irq_data->mask;
138 d->wake_count++;
139 } else {
140 d->wake_buf[irq_data->reg_offset / map->reg_stride]
141 |= irq_data->mask;
142 d->wake_count--;
143 }
144
145 return 0;
146}
147
148static const struct irq_chip regmap_irq_chip = {
149 .irq_bus_lock = regmap_irq_lock, 88 .irq_bus_lock = regmap_irq_lock,
150 .irq_bus_sync_unlock = regmap_irq_sync_unlock, 89 .irq_bus_sync_unlock = regmap_irq_sync_unlock,
151 .irq_disable = regmap_irq_disable, 90 .irq_disable = regmap_irq_disable,
152 .irq_enable = regmap_irq_enable, 91 .irq_enable = regmap_irq_enable,
153 .irq_set_wake = regmap_irq_set_wake,
154}; 92};
155 93
156static irqreturn_t regmap_irq_thread(int irq, void *d) 94static irqreturn_t regmap_irq_thread(int irq, void *d)
157{ 95{
158 struct regmap_irq_chip_data *data = d; 96 struct regmap_irq_chip_data *data = d;
159 const struct regmap_irq_chip *chip = data->chip; 97 struct regmap_irq_chip *chip = data->chip;
160 struct regmap *map = data->map; 98 struct regmap *map = data->map;
161 int ret, i; 99 int ret, i;
100 u8 *buf8 = data->status_reg_buf;
101 u16 *buf16 = data->status_reg_buf;
102 u32 *buf32 = data->status_reg_buf;
162 bool handled = false; 103 bool handled = false;
163 u32 reg;
164 104
165 if (chip->runtime_pm) { 105 ret = regmap_bulk_read(map, chip->status_base, data->status_reg_buf,
166 ret = pm_runtime_get_sync(map->dev); 106 chip->num_regs);
167 if (ret < 0) { 107 if (ret != 0) {
168 dev_err(map->dev, "IRQ thread failed to resume: %d\n", 108 dev_err(map->dev, "Failed to read IRQ status: %d\n", ret);
169 ret); 109 return IRQ_NONE;
170 return IRQ_NONE;
171 }
172 } 110 }
173 111
174 /* 112 /*
@@ -179,72 +117,46 @@ static irqreturn_t regmap_irq_thread(int irq, void *d)
179 * doing a write per register. 117 * doing a write per register.
180 */ 118 */
181 for (i = 0; i < data->chip->num_regs; i++) { 119 for (i = 0; i < data->chip->num_regs; i++) {
182 ret = regmap_read(map, chip->status_base + (i * map->reg_stride 120 switch (map->format.val_bytes) {
183 * data->irq_reg_stride), 121 case 1:
184 &data->status_buf[i]); 122 data->status_buf[i] = buf8[i];
185 123 break;
186 if (ret != 0) { 124 case 2:
187 dev_err(map->dev, "Failed to read IRQ status: %d\n", 125 data->status_buf[i] = buf16[i];
188 ret); 126 break;
189 if (chip->runtime_pm) 127 case 4:
190 pm_runtime_put(map->dev); 128 data->status_buf[i] = buf32[i];
129 break;
130 default:
131 BUG();
191 return IRQ_NONE; 132 return IRQ_NONE;
192 } 133 }
193 134
194 data->status_buf[i] &= ~data->mask_buf[i]; 135 data->status_buf[i] &= ~data->mask_buf[i];
195 136
196 if (data->status_buf[i] && chip->ack_base) { 137 if (data->status_buf[i] && chip->ack_base) {
197 reg = chip->ack_base + 138 ret = regmap_write(map, chip->ack_base + i,
198 (i * map->reg_stride * data->irq_reg_stride); 139 data->status_buf[i]);
199 ret = regmap_write(map, reg, data->status_buf[i]);
200 if (ret != 0) 140 if (ret != 0)
201 dev_err(map->dev, "Failed to ack 0x%x: %d\n", 141 dev_err(map->dev, "Failed to ack 0x%x: %d\n",
202 reg, ret); 142 chip->ack_base + i, ret);
203 } 143 }
204 } 144 }
205 145
206 for (i = 0; i < chip->num_irqs; i++) { 146 for (i = 0; i < chip->num_irqs; i++) {
207 if (data->status_buf[chip->irqs[i].reg_offset / 147 if (data->status_buf[chip->irqs[i].reg_offset] &
208 map->reg_stride] & chip->irqs[i].mask) { 148 chip->irqs[i].mask) {
209 handle_nested_irq(irq_find_mapping(data->domain, i)); 149 handle_nested_irq(data->irq_base + i);
210 handled = true; 150 handled = true;
211 } 151 }
212 } 152 }
213 153
214 if (chip->runtime_pm)
215 pm_runtime_put(map->dev);
216
217 if (handled) 154 if (handled)
218 return IRQ_HANDLED; 155 return IRQ_HANDLED;
219 else 156 else
220 return IRQ_NONE; 157 return IRQ_NONE;
221} 158}
222 159
223static int regmap_irq_map(struct irq_domain *h, unsigned int virq,
224 irq_hw_number_t hw)
225{
226 struct regmap_irq_chip_data *data = h->host_data;
227
228 irq_set_chip_data(virq, data);
229 irq_set_chip(virq, &data->irq_chip);
230 irq_set_nested_thread(virq, 1);
231
232 /* ARM needs us to explicitly flag the IRQ as valid
233 * and will set them noprobe when we do so. */
234#ifdef CONFIG_ARM
235 set_irq_flags(virq, IRQF_VALID);
236#else
237 irq_set_noprobe(virq);
238#endif
239
240 return 0;
241}
242
243static struct irq_domain_ops regmap_domain_ops = {
244 .map = regmap_irq_map,
245 .xlate = irq_domain_xlate_twocell,
246};
247
248/** 160/**
249 * regmap_add_irq_chip(): Use standard regmap IRQ controller handling 161 * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
250 * 162 *
@@ -261,42 +173,34 @@ static struct irq_domain_ops regmap_domain_ops = {
261 * register values used by the IRQ controller over suspend and resume. 173 * register values used by the IRQ controller over suspend and resume.
262 */ 174 */
263int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags, 175int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
264 int irq_base, const struct regmap_irq_chip *chip, 176 int irq_base, struct regmap_irq_chip *chip,
265 struct regmap_irq_chip_data **data) 177 struct regmap_irq_chip_data **data)
266{ 178{
267 struct regmap_irq_chip_data *d; 179 struct regmap_irq_chip_data *d;
268 int i; 180 int cur_irq, i;
269 int ret = -ENOMEM; 181 int ret = -ENOMEM;
270 u32 reg;
271 182
272 for (i = 0; i < chip->num_irqs; i++) { 183 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
273 if (chip->irqs[i].reg_offset % map->reg_stride) 184 if (irq_base < 0) {
274 return -EINVAL; 185 dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
275 if (chip->irqs[i].reg_offset / map->reg_stride >= 186 irq_base);
276 chip->num_regs) 187 return irq_base;
277 return -EINVAL;
278 }
279
280 if (irq_base) {
281 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
282 if (irq_base < 0) {
283 dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
284 irq_base);
285 return irq_base;
286 }
287 } 188 }
288 189
289 d = kzalloc(sizeof(*d), GFP_KERNEL); 190 d = kzalloc(sizeof(*d), GFP_KERNEL);
290 if (!d) 191 if (!d)
291 return -ENOMEM; 192 return -ENOMEM;
292 193
293 *data = d;
294
295 d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs, 194 d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
296 GFP_KERNEL); 195 GFP_KERNEL);
297 if (!d->status_buf) 196 if (!d->status_buf)
298 goto err_alloc; 197 goto err_alloc;
299 198
199 d->status_reg_buf = kzalloc(map->format.val_bytes * chip->num_regs,
200 GFP_KERNEL);
201 if (!d->status_reg_buf)
202 goto err_alloc;
203
300 d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs, 204 d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
301 GFP_KERNEL); 205 GFP_KERNEL);
302 if (!d->mask_buf) 206 if (!d->mask_buf)
@@ -307,99 +211,57 @@ int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
307 if (!d->mask_buf_def) 211 if (!d->mask_buf_def)
308 goto err_alloc; 212 goto err_alloc;
309 213
310 if (chip->wake_base) {
311 d->wake_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
312 GFP_KERNEL);
313 if (!d->wake_buf)
314 goto err_alloc;
315 }
316
317 d->irq_chip = regmap_irq_chip;
318 d->irq_chip.name = chip->name;
319 if (!chip->wake_base) {
320 d->irq_chip.irq_set_wake = NULL;
321 d->irq_chip.flags |= IRQCHIP_MASK_ON_SUSPEND |
322 IRQCHIP_SKIP_SET_WAKE;
323 }
324 d->irq = irq;
325 d->map = map; 214 d->map = map;
326 d->chip = chip; 215 d->chip = chip;
327 d->irq_base = irq_base; 216 d->irq_base = irq_base;
328
329 if (chip->irq_reg_stride)
330 d->irq_reg_stride = chip->irq_reg_stride;
331 else
332 d->irq_reg_stride = 1;
333
334 mutex_init(&d->lock); 217 mutex_init(&d->lock);
335 218
336 for (i = 0; i < chip->num_irqs; i++) 219 for (i = 0; i < chip->num_irqs; i++)
337 d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride] 220 d->mask_buf_def[chip->irqs[i].reg_offset]
338 |= chip->irqs[i].mask; 221 |= chip->irqs[i].mask;
339 222
340 /* Mask all the interrupts by default */ 223 /* Mask all the interrupts by default */
341 for (i = 0; i < chip->num_regs; i++) { 224 for (i = 0; i < chip->num_regs; i++) {
342 d->mask_buf[i] = d->mask_buf_def[i]; 225 d->mask_buf[i] = d->mask_buf_def[i];
343 reg = chip->mask_base + 226 ret = regmap_write(map, chip->mask_base + i, d->mask_buf[i]);
344 (i * map->reg_stride * d->irq_reg_stride);
345 if (chip->mask_invert)
346 ret = regmap_update_bits(map, reg,
347 d->mask_buf[i], ~d->mask_buf[i]);
348 else
349 ret = regmap_update_bits(map, reg,
350 d->mask_buf[i], d->mask_buf[i]);
351 if (ret != 0) { 227 if (ret != 0) {
352 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", 228 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
353 reg, ret); 229 chip->mask_base + i, ret);
354 goto err_alloc; 230 goto err_alloc;
355 } 231 }
356 } 232 }
357 233
358 /* Wake is disabled by default */ 234 /* Register them with genirq */
359 if (d->wake_buf) { 235 for (cur_irq = irq_base;
360 for (i = 0; i < chip->num_regs; i++) { 236 cur_irq < chip->num_irqs + irq_base;
361 d->wake_buf[i] = d->mask_buf_def[i]; 237 cur_irq++) {
362 reg = chip->wake_base + 238 irq_set_chip_data(cur_irq, d);
363 (i * map->reg_stride * d->irq_reg_stride); 239 irq_set_chip_and_handler(cur_irq, &regmap_irq_chip,
364 ret = regmap_update_bits(map, reg, d->wake_buf[i], 240 handle_edge_irq);
365 d->wake_buf[i]); 241 irq_set_nested_thread(cur_irq, 1);
366 if (ret != 0) { 242
367 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", 243 /* ARM needs us to explicitly flag the IRQ as valid
368 reg, ret); 244 * and will set them noprobe when we do so. */
369 goto err_alloc; 245#ifdef CONFIG_ARM
370 } 246 set_irq_flags(cur_irq, IRQF_VALID);
371 } 247#else
372 } 248 irq_set_noprobe(cur_irq);
373 249#endif
374 if (irq_base)
375 d->domain = irq_domain_add_legacy(map->dev->of_node,
376 chip->num_irqs, irq_base, 0,
377 &regmap_domain_ops, d);
378 else
379 d->domain = irq_domain_add_linear(map->dev->of_node,
380 chip->num_irqs,
381 &regmap_domain_ops, d);
382 if (!d->domain) {
383 dev_err(map->dev, "Failed to create IRQ domain\n");
384 ret = -ENOMEM;
385 goto err_alloc;
386 } 250 }
387 251
388 ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags, 252 ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
389 chip->name, d); 253 chip->name, d);
390 if (ret != 0) { 254 if (ret != 0) {
391 dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret); 255 dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
392 goto err_domain; 256 goto err_alloc;
393 } 257 }
394 258
395 return 0; 259 return 0;
396 260
397err_domain:
398 /* Should really dispose of the domain but... */
399err_alloc: 261err_alloc:
400 kfree(d->wake_buf);
401 kfree(d->mask_buf_def); 262 kfree(d->mask_buf_def);
402 kfree(d->mask_buf); 263 kfree(d->mask_buf);
264 kfree(d->status_reg_buf);
403 kfree(d->status_buf); 265 kfree(d->status_buf);
404 kfree(d); 266 kfree(d);
405 return ret; 267 return ret;
@@ -418,10 +280,9 @@ void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
418 return; 280 return;
419 281
420 free_irq(irq, d); 282 free_irq(irq, d);
421 /* We should unmap the domain but... */
422 kfree(d->wake_buf);
423 kfree(d->mask_buf_def); 283 kfree(d->mask_buf_def);
424 kfree(d->mask_buf); 284 kfree(d->mask_buf);
285 kfree(d->status_reg_buf);
425 kfree(d->status_buf); 286 kfree(d->status_buf);
426 kfree(d); 287 kfree(d);
427} 288}
@@ -436,44 +297,6 @@ EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
436 */ 297 */
437int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data) 298int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
438{ 299{
439 WARN_ON(!data->irq_base);
440 return data->irq_base; 300 return data->irq_base;
441} 301}
442EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base); 302EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
443
444/**
445 * regmap_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
446 *
447 * Useful for drivers to request their own IRQs.
448 *
449 * @data: regmap_irq controller to operate on.
450 * @irq: index of the interrupt requested in the chip IRQs
451 */
452int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
453{
454 /* Handle holes in the IRQ list */
455 if (!data->chip->irqs[irq].mask)
456 return -EINVAL;
457
458 return irq_create_mapping(data->domain, irq);
459}
460EXPORT_SYMBOL_GPL(regmap_irq_get_virq);
461
462/**
463 * regmap_irq_get_domain(): Retrieve the irq_domain for the chip
464 *
465 * Useful for drivers to request their own IRQs and for integration
466 * with subsystems. For ease of integration NULL is accepted as a
467 * domain, allowing devices to just call this even if no domain is
468 * allocated.
469 *
470 * @data: regmap_irq controller to operate on.
471 */
472struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data)
473{
474 if (data)
475 return data->domain;
476 else
477 return NULL;
478}
479EXPORT_SYMBOL_GPL(regmap_irq_get_domain);
diff --git a/drivers/base/regmap/regmap-mmio.c b/drivers/base/regmap/regmap-mmio.c
deleted file mode 100644
index f05fc74dd84..00000000000
--- a/drivers/base/regmap/regmap-mmio.c
+++ /dev/null
@@ -1,234 +0,0 @@
1/*
2 * Register map access API - MMIO support
3 *
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/io.h>
22#include <linux/module.h>
23#include <linux/regmap.h>
24#include <linux/slab.h>
25
26struct regmap_mmio_context {
27 void __iomem *regs;
28 unsigned val_bytes;
29};
30
31static int regmap_mmio_gather_write(void *context,
32 const void *reg, size_t reg_size,
33 const void *val, size_t val_size)
34{
35 struct regmap_mmio_context *ctx = context;
36 u32 offset;
37
38 BUG_ON(reg_size != 4);
39
40 offset = *(u32 *)reg;
41
42 while (val_size) {
43 switch (ctx->val_bytes) {
44 case 1:
45 writeb(*(u8 *)val, ctx->regs + offset);
46 break;
47 case 2:
48 writew(*(u16 *)val, ctx->regs + offset);
49 break;
50 case 4:
51 writel(*(u32 *)val, ctx->regs + offset);
52 break;
53#ifdef CONFIG_64BIT
54 case 8:
55 writeq(*(u64 *)val, ctx->regs + offset);
56 break;
57#endif
58 default:
59 /* Should be caught by regmap_mmio_check_config */
60 BUG();
61 }
62 val_size -= ctx->val_bytes;
63 val += ctx->val_bytes;
64 offset += ctx->val_bytes;
65 }
66
67 return 0;
68}
69
70static int regmap_mmio_write(void *context, const void *data, size_t count)
71{
72 BUG_ON(count < 4);
73
74 return regmap_mmio_gather_write(context, data, 4, data + 4, count - 4);
75}
76
77static int regmap_mmio_read(void *context,
78 const void *reg, size_t reg_size,
79 void *val, size_t val_size)
80{
81 struct regmap_mmio_context *ctx = context;
82 u32 offset;
83
84 BUG_ON(reg_size != 4);
85
86 offset = *(u32 *)reg;
87
88 while (val_size) {
89 switch (ctx->val_bytes) {
90 case 1:
91 *(u8 *)val = readb(ctx->regs + offset);
92 break;
93 case 2:
94 *(u16 *)val = readw(ctx->regs + offset);
95 break;
96 case 4:
97 *(u32 *)val = readl(ctx->regs + offset);
98 break;
99#ifdef CONFIG_64BIT
100 case 8:
101 *(u64 *)val = readq(ctx->regs + offset);
102 break;
103#endif
104 default:
105 /* Should be caught by regmap_mmio_check_config */
106 BUG();
107 }
108 val_size -= ctx->val_bytes;
109 val += ctx->val_bytes;
110 offset += ctx->val_bytes;
111 }
112
113 return 0;
114}
115
116static void regmap_mmio_free_context(void *context)
117{
118 kfree(context);
119}
120
121static struct regmap_bus regmap_mmio = {
122 .fast_io = true,
123 .write = regmap_mmio_write,
124 .gather_write = regmap_mmio_gather_write,
125 .read = regmap_mmio_read,
126 .free_context = regmap_mmio_free_context,
127 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
128 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
129};
130
131static struct regmap_mmio_context *regmap_mmio_gen_context(void __iomem *regs,
132 const struct regmap_config *config)
133{
134 struct regmap_mmio_context *ctx;
135 int min_stride;
136
137 if (config->reg_bits != 32)
138 return ERR_PTR(-EINVAL);
139
140 if (config->pad_bits)
141 return ERR_PTR(-EINVAL);
142
143 switch (config->val_bits) {
144 case 8:
145 /* The core treats 0 as 1 */
146 min_stride = 0;
147 break;
148 case 16:
149 min_stride = 2;
150 break;
151 case 32:
152 min_stride = 4;
153 break;
154#ifdef CONFIG_64BIT
155 case 64:
156 min_stride = 8;
157 break;
158#endif
159 break;
160 default:
161 return ERR_PTR(-EINVAL);
162 }
163
164 if (config->reg_stride < min_stride)
165 return ERR_PTR(-EINVAL);
166
167 switch (config->reg_format_endian) {
168 case REGMAP_ENDIAN_DEFAULT:
169 case REGMAP_ENDIAN_NATIVE:
170 break;
171 default:
172 return ERR_PTR(-EINVAL);
173 }
174
175 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
176 if (!ctx)
177 return ERR_PTR(-ENOMEM);
178
179 ctx->regs = regs;
180 ctx->val_bytes = config->val_bits / 8;
181
182 return ctx;
183}
184
185/**
186 * regmap_init_mmio(): Initialise register map
187 *
188 * @dev: Device that will be interacted with
189 * @regs: Pointer to memory-mapped IO region
190 * @config: Configuration for register map
191 *
192 * The return value will be an ERR_PTR() on error or a valid pointer to
193 * a struct regmap.
194 */
195struct regmap *regmap_init_mmio(struct device *dev,
196 void __iomem *regs,
197 const struct regmap_config *config)
198{
199 struct regmap_mmio_context *ctx;
200
201 ctx = regmap_mmio_gen_context(regs, config);
202 if (IS_ERR(ctx))
203 return ERR_CAST(ctx);
204
205 return regmap_init(dev, &regmap_mmio, ctx, config);
206}
207EXPORT_SYMBOL_GPL(regmap_init_mmio);
208
209/**
210 * devm_regmap_init_mmio(): Initialise managed register map
211 *
212 * @dev: Device that will be interacted with
213 * @regs: Pointer to memory-mapped IO region
214 * @config: Configuration for register map
215 *
216 * The return value will be an ERR_PTR() on error or a valid pointer
217 * to a struct regmap. The regmap will be automatically freed by the
218 * device management code.
219 */
220struct regmap *devm_regmap_init_mmio(struct device *dev,
221 void __iomem *regs,
222 const struct regmap_config *config)
223{
224 struct regmap_mmio_context *ctx;
225
226 ctx = regmap_mmio_gen_context(regs, config);
227 if (IS_ERR(ctx))
228 return ERR_CAST(ctx);
229
230 return devm_regmap_init(dev, &regmap_mmio, ctx, config);
231}
232EXPORT_SYMBOL_GPL(devm_regmap_init_mmio);
233
234MODULE_LICENSE("GPL v2");
diff --git a/drivers/base/regmap/regmap-spi.c b/drivers/base/regmap/regmap-spi.c
index ffa46a92ad3..7c0c35a39c3 100644
--- a/drivers/base/regmap/regmap-spi.c
+++ b/drivers/base/regmap/regmap-spi.c
@@ -15,19 +15,17 @@
15#include <linux/init.h> 15#include <linux/init.h>
16#include <linux/module.h> 16#include <linux/module.h>
17 17
18static int regmap_spi_write(void *context, const void *data, size_t count) 18static int regmap_spi_write(struct device *dev, const void *data, size_t count)
19{ 19{
20 struct device *dev = context;
21 struct spi_device *spi = to_spi_device(dev); 20 struct spi_device *spi = to_spi_device(dev);
22 21
23 return spi_write(spi, data, count); 22 return spi_write(spi, data, count);
24} 23}
25 24
26static int regmap_spi_gather_write(void *context, 25static int regmap_spi_gather_write(struct device *dev,
27 const void *reg, size_t reg_len, 26 const void *reg, size_t reg_len,
28 const void *val, size_t val_len) 27 const void *val, size_t val_len)
29{ 28{
30 struct device *dev = context;
31 struct spi_device *spi = to_spi_device(dev); 29 struct spi_device *spi = to_spi_device(dev);
32 struct spi_message m; 30 struct spi_message m;
33 struct spi_transfer t[2] = { { .tx_buf = reg, .len = reg_len, }, 31 struct spi_transfer t[2] = { { .tx_buf = reg, .len = reg_len, },
@@ -40,11 +38,10 @@ static int regmap_spi_gather_write(void *context,
40 return spi_sync(spi, &m); 38 return spi_sync(spi, &m);
41} 39}
42 40
43static int regmap_spi_read(void *context, 41static int regmap_spi_read(struct device *dev,
44 const void *reg, size_t reg_size, 42 const void *reg, size_t reg_size,
45 void *val, size_t val_size) 43 void *val, size_t val_size)
46{ 44{
47 struct device *dev = context;
48 struct spi_device *spi = to_spi_device(dev); 45 struct spi_device *spi = to_spi_device(dev);
49 46
50 return spi_write_then_read(spi, reg, reg_size, val, val_size); 47 return spi_write_then_read(spi, reg, reg_size, val, val_size);
@@ -69,7 +66,7 @@ static struct regmap_bus regmap_spi = {
69struct regmap *regmap_init_spi(struct spi_device *spi, 66struct regmap *regmap_init_spi(struct spi_device *spi,
70 const struct regmap_config *config) 67 const struct regmap_config *config)
71{ 68{
72 return regmap_init(&spi->dev, &regmap_spi, &spi->dev, config); 69 return regmap_init(&spi->dev, &regmap_spi, config);
73} 70}
74EXPORT_SYMBOL_GPL(regmap_init_spi); 71EXPORT_SYMBOL_GPL(regmap_init_spi);
75 72
@@ -86,7 +83,7 @@ EXPORT_SYMBOL_GPL(regmap_init_spi);
86struct regmap *devm_regmap_init_spi(struct spi_device *spi, 83struct regmap *devm_regmap_init_spi(struct spi_device *spi,
87 const struct regmap_config *config) 84 const struct regmap_config *config)
88{ 85{
89 return devm_regmap_init(&spi->dev, &regmap_spi, &spi->dev, config); 86 return devm_regmap_init(&spi->dev, &regmap_spi, config);
90} 87}
91EXPORT_SYMBOL_GPL(devm_regmap_init_spi); 88EXPORT_SYMBOL_GPL(devm_regmap_init_spi);
92 89
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 42d5cb0f503..a239ce8e1f0 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -10,60 +10,16 @@
10 * published by the Free Software Foundation. 10 * published by the Free Software Foundation.
11 */ 11 */
12 12
13#include <linux/device.h>
14#include <linux/slab.h> 13#include <linux/slab.h>
15#include <linux/export.h> 14#include <linux/module.h>
16#include <linux/mutex.h> 15#include <linux/mutex.h>
17#include <linux/err.h> 16#include <linux/err.h>
18#include <linux/rbtree.h>
19 17
20#define CREATE_TRACE_POINTS 18#define CREATE_TRACE_POINTS
21#include <trace/events/regmap.h> 19#include <trace/events/regmap.h>
22 20
23#include "internal.h" 21#include "internal.h"
24 22
25/*
26 * Sometimes for failures during very early init the trace
27 * infrastructure isn't available early enough to be used. For this
28 * sort of problem defining LOG_DEVICE will add printks for basic
29 * register I/O on a specific device.
30 */
31#undef LOG_DEVICE
32
33static int _regmap_update_bits(struct regmap *map, unsigned int reg,
34 unsigned int mask, unsigned int val,
35 bool *change);
36
37bool regmap_reg_in_ranges(unsigned int reg,
38 const struct regmap_range *ranges,
39 unsigned int nranges)
40{
41 const struct regmap_range *r;
42 int i;
43
44 for (i = 0, r = ranges; i < nranges; i++, r++)
45 if (regmap_reg_in_range(reg, r))
46 return true;
47 return false;
48}
49EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
50
51static bool _regmap_check_range_table(struct regmap *map,
52 unsigned int reg,
53 const struct regmap_access_table *table)
54{
55 /* Check "no ranges" first */
56 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
57 return false;
58
59 /* In case zero "yes ranges" are supplied, any reg is OK */
60 if (!table->n_yes_ranges)
61 return true;
62
63 return regmap_reg_in_ranges(reg, table->yes_ranges,
64 table->n_yes_ranges);
65}
66
67bool regmap_writeable(struct regmap *map, unsigned int reg) 23bool regmap_writeable(struct regmap *map, unsigned int reg)
68{ 24{
69 if (map->max_register && reg > map->max_register) 25 if (map->max_register && reg > map->max_register)
@@ -72,9 +28,6 @@ bool regmap_writeable(struct regmap *map, unsigned int reg)
72 if (map->writeable_reg) 28 if (map->writeable_reg)
73 return map->writeable_reg(map->dev, reg); 29 return map->writeable_reg(map->dev, reg);
74 30
75 if (map->wr_table)
76 return _regmap_check_range_table(map, reg, map->wr_table);
77
78 return true; 31 return true;
79} 32}
80 33
@@ -89,9 +42,6 @@ bool regmap_readable(struct regmap *map, unsigned int reg)
89 if (map->readable_reg) 42 if (map->readable_reg)
90 return map->readable_reg(map->dev, reg); 43 return map->readable_reg(map->dev, reg);
91 44
92 if (map->rd_table)
93 return _regmap_check_range_table(map, reg, map->rd_table);
94
95 return true; 45 return true;
96} 46}
97 47
@@ -103,9 +53,6 @@ bool regmap_volatile(struct regmap *map, unsigned int reg)
103 if (map->volatile_reg) 53 if (map->volatile_reg)
104 return map->volatile_reg(map->dev, reg); 54 return map->volatile_reg(map->dev, reg);
105 55
106 if (map->volatile_table)
107 return _regmap_check_range_table(map, reg, map->volatile_table);
108
109 return true; 56 return true;
110} 57}
111 58
@@ -117,14 +64,11 @@ bool regmap_precious(struct regmap *map, unsigned int reg)
117 if (map->precious_reg) 64 if (map->precious_reg)
118 return map->precious_reg(map->dev, reg); 65 return map->precious_reg(map->dev, reg);
119 66
120 if (map->precious_table)
121 return _regmap_check_range_table(map, reg, map->precious_table);
122
123 return false; 67 return false;
124} 68}
125 69
126static bool regmap_volatile_range(struct regmap *map, unsigned int reg, 70static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
127 size_t num) 71 unsigned int num)
128{ 72{
129 unsigned int i; 73 unsigned int i;
130 74
@@ -167,48 +111,18 @@ static void regmap_format_10_14_write(struct regmap *map,
167 out[0] = reg >> 2; 111 out[0] = reg >> 2;
168} 112}
169 113
170static void regmap_format_8(void *buf, unsigned int val, unsigned int shift) 114static void regmap_format_8(void *buf, unsigned int val)
171{ 115{
172 u8 *b = buf; 116 u8 *b = buf;
173 117
174 b[0] = val << shift; 118 b[0] = val;
175} 119}
176 120
177static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift) 121static void regmap_format_16(void *buf, unsigned int val)
178{ 122{
179 __be16 *b = buf; 123 __be16 *b = buf;
180 124
181 b[0] = cpu_to_be16(val << shift); 125 b[0] = cpu_to_be16(val);
182}
183
184static void regmap_format_16_native(void *buf, unsigned int val,
185 unsigned int shift)
186{
187 *(u16 *)buf = val << shift;
188}
189
190static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
191{
192 u8 *b = buf;
193
194 val <<= shift;
195
196 b[0] = val >> 16;
197 b[1] = val >> 8;
198 b[2] = val;
199}
200
201static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
202{
203 __be32 *b = buf;
204
205 b[0] = cpu_to_be32(val << shift);
206}
207
208static void regmap_format_32_native(void *buf, unsigned int val,
209 unsigned int shift)
210{
211 *(u32 *)buf = val << shift;
212} 126}
213 127
214static unsigned int regmap_parse_8(void *buf) 128static unsigned int regmap_parse_8(void *buf)
@@ -218,7 +132,7 @@ static unsigned int regmap_parse_8(void *buf)
218 return b[0]; 132 return b[0];
219} 133}
220 134
221static unsigned int regmap_parse_16_be(void *buf) 135static unsigned int regmap_parse_16(void *buf)
222{ 136{
223 __be16 *b = buf; 137 __be16 *b = buf;
224 138
@@ -227,135 +141,11 @@ static unsigned int regmap_parse_16_be(void *buf)
227 return b[0]; 141 return b[0];
228} 142}
229 143
230static unsigned int regmap_parse_16_native(void *buf)
231{
232 return *(u16 *)buf;
233}
234
235static unsigned int regmap_parse_24(void *buf)
236{
237 u8 *b = buf;
238 unsigned int ret = b[2];
239 ret |= ((unsigned int)b[1]) << 8;
240 ret |= ((unsigned int)b[0]) << 16;
241
242 return ret;
243}
244
245static unsigned int regmap_parse_32_be(void *buf)
246{
247 __be32 *b = buf;
248
249 b[0] = be32_to_cpu(b[0]);
250
251 return b[0];
252}
253
254static unsigned int regmap_parse_32_native(void *buf)
255{
256 return *(u32 *)buf;
257}
258
259static void regmap_lock_mutex(void *__map)
260{
261 struct regmap *map = __map;
262 mutex_lock(&map->mutex);
263}
264
265static void regmap_unlock_mutex(void *__map)
266{
267 struct regmap *map = __map;
268 mutex_unlock(&map->mutex);
269}
270
271static void regmap_lock_spinlock(void *__map)
272{
273 struct regmap *map = __map;
274 spin_lock(&map->spinlock);
275}
276
277static void regmap_unlock_spinlock(void *__map)
278{
279 struct regmap *map = __map;
280 spin_unlock(&map->spinlock);
281}
282
283static void dev_get_regmap_release(struct device *dev, void *res)
284{
285 /*
286 * We don't actually have anything to do here; the goal here
287 * is not to manage the regmap but to provide a simple way to
288 * get the regmap back given a struct device.
289 */
290}
291
292static bool _regmap_range_add(struct regmap *map,
293 struct regmap_range_node *data)
294{
295 struct rb_root *root = &map->range_tree;
296 struct rb_node **new = &(root->rb_node), *parent = NULL;
297
298 while (*new) {
299 struct regmap_range_node *this =
300 container_of(*new, struct regmap_range_node, node);
301
302 parent = *new;
303 if (data->range_max < this->range_min)
304 new = &((*new)->rb_left);
305 else if (data->range_min > this->range_max)
306 new = &((*new)->rb_right);
307 else
308 return false;
309 }
310
311 rb_link_node(&data->node, parent, new);
312 rb_insert_color(&data->node, root);
313
314 return true;
315}
316
317static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
318 unsigned int reg)
319{
320 struct rb_node *node = map->range_tree.rb_node;
321
322 while (node) {
323 struct regmap_range_node *this =
324 container_of(node, struct regmap_range_node, node);
325
326 if (reg < this->range_min)
327 node = node->rb_left;
328 else if (reg > this->range_max)
329 node = node->rb_right;
330 else
331 return this;
332 }
333
334 return NULL;
335}
336
337static void regmap_range_exit(struct regmap *map)
338{
339 struct rb_node *next;
340 struct regmap_range_node *range_node;
341
342 next = rb_first(&map->range_tree);
343 while (next) {
344 range_node = rb_entry(next, struct regmap_range_node, node);
345 next = rb_next(&range_node->node);
346 rb_erase(&range_node->node, &map->range_tree);
347 kfree(range_node);
348 }
349
350 kfree(map->selector_work_buf);
351}
352
353/** 144/**
354 * regmap_init(): Initialise register map 145 * regmap_init(): Initialise register map
355 * 146 *
356 * @dev: Device that will be interacted with 147 * @dev: Device that will be interacted with
357 * @bus: Bus-specific callbacks to use with device 148 * @bus: Bus-specific callbacks to use with device
358 * @bus_context: Data passed to bus-specific callbacks
359 * @config: Configuration for register map 149 * @config: Configuration for register map
360 * 150 *
361 * The return value will be an ERR_PTR() on error or a valid pointer to 151 * The return value will be an ERR_PTR() on error or a valid pointer to
@@ -364,13 +154,10 @@ static void regmap_range_exit(struct regmap *map)
364 */ 154 */
365struct regmap *regmap_init(struct device *dev, 155struct regmap *regmap_init(struct device *dev,
366 const struct regmap_bus *bus, 156 const struct regmap_bus *bus,
367 void *bus_context,
368 const struct regmap_config *config) 157 const struct regmap_config *config)
369{ 158{
370 struct regmap *map, **m; 159 struct regmap *map;
371 int ret = -EINVAL; 160 int ret = -EINVAL;
372 enum regmap_endian reg_endian, val_endian;
373 int i, j;
374 161
375 if (!bus || !config) 162 if (!bus || !config)
376 goto err; 163 goto err;
@@ -381,47 +168,20 @@ struct regmap *regmap_init(struct device *dev,
381 goto err; 168 goto err;
382 } 169 }
383 170
384 if (config->lock && config->unlock) { 171 mutex_init(&map->lock);
385 map->lock = config->lock; 172 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
386 map->unlock = config->unlock;
387 map->lock_arg = config->lock_arg;
388 } else {
389 if (bus->fast_io) {
390 spin_lock_init(&map->spinlock);
391 map->lock = regmap_lock_spinlock;
392 map->unlock = regmap_unlock_spinlock;
393 } else {
394 mutex_init(&map->mutex);
395 map->lock = regmap_lock_mutex;
396 map->unlock = regmap_unlock_mutex;
397 }
398 map->lock_arg = map;
399 }
400 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); 173 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
401 map->format.pad_bytes = config->pad_bits / 8; 174 map->format.pad_bytes = config->pad_bits / 8;
402 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); 175 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
403 map->format.buf_size = DIV_ROUND_UP(config->reg_bits + 176 map->format.buf_size += map->format.pad_bytes;
404 config->val_bits + config->pad_bits, 8);
405 map->reg_shift = config->pad_bits % 8;
406 if (config->reg_stride)
407 map->reg_stride = config->reg_stride;
408 else
409 map->reg_stride = 1;
410 map->use_single_rw = config->use_single_rw;
411 map->dev = dev; 177 map->dev = dev;
412 map->bus = bus; 178 map->bus = bus;
413 map->bus_context = bus_context;
414 map->max_register = config->max_register; 179 map->max_register = config->max_register;
415 map->wr_table = config->wr_table;
416 map->rd_table = config->rd_table;
417 map->volatile_table = config->volatile_table;
418 map->precious_table = config->precious_table;
419 map->writeable_reg = config->writeable_reg; 180 map->writeable_reg = config->writeable_reg;
420 map->readable_reg = config->readable_reg; 181 map->readable_reg = config->readable_reg;
421 map->volatile_reg = config->volatile_reg; 182 map->volatile_reg = config->volatile_reg;
422 map->precious_reg = config->precious_reg; 183 map->precious_reg = config->precious_reg;
423 map->cache_type = config->cache_type; 184 map->cache_type = config->cache_type;
424 map->name = config->name;
425 185
426 if (config->read_flag_mask || config->write_flag_mask) { 186 if (config->read_flag_mask || config->write_flag_mask) {
427 map->read_flag_mask = config->read_flag_mask; 187 map->read_flag_mask = config->read_flag_mask;
@@ -430,19 +190,7 @@ struct regmap *regmap_init(struct device *dev,
430 map->read_flag_mask = bus->read_flag_mask; 190 map->read_flag_mask = bus->read_flag_mask;
431 } 191 }
432 192
433 reg_endian = config->reg_format_endian; 193 switch (config->reg_bits) {
434 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
435 reg_endian = bus->reg_format_endian_default;
436 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
437 reg_endian = REGMAP_ENDIAN_BIG;
438
439 val_endian = config->val_format_endian;
440 if (val_endian == REGMAP_ENDIAN_DEFAULT)
441 val_endian = bus->val_format_endian_default;
442 if (val_endian == REGMAP_ENDIAN_DEFAULT)
443 val_endian = REGMAP_ENDIAN_BIG;
444
445 switch (config->reg_bits + map->reg_shift) {
446 case 2: 194 case 2:
447 switch (config->val_bits) { 195 switch (config->val_bits) {
448 case 6: 196 case 6:
@@ -488,29 +236,7 @@ struct regmap *regmap_init(struct device *dev,
488 break; 236 break;
489 237
490 case 16: 238 case 16:
491 switch (reg_endian) { 239 map->format.format_reg = regmap_format_16;
492 case REGMAP_ENDIAN_BIG:
493 map->format.format_reg = regmap_format_16_be;
494 break;
495 case REGMAP_ENDIAN_NATIVE:
496 map->format.format_reg = regmap_format_16_native;
497 break;
498 default:
499 goto err_map;
500 }
501 break;
502
503 case 32:
504 switch (reg_endian) {
505 case REGMAP_ENDIAN_BIG:
506 map->format.format_reg = regmap_format_32_be;
507 break;
508 case REGMAP_ENDIAN_NATIVE:
509 map->format.format_reg = regmap_format_32_native;
510 break;
511 default:
512 goto err_map;
513 }
514 break; 240 break;
515 241
516 default: 242 default:
@@ -523,48 +249,11 @@ struct regmap *regmap_init(struct device *dev,
523 map->format.parse_val = regmap_parse_8; 249 map->format.parse_val = regmap_parse_8;
524 break; 250 break;
525 case 16: 251 case 16:
526 switch (val_endian) { 252 map->format.format_val = regmap_format_16;
527 case REGMAP_ENDIAN_BIG: 253 map->format.parse_val = regmap_parse_16;
528 map->format.format_val = regmap_format_16_be;
529 map->format.parse_val = regmap_parse_16_be;
530 break;
531 case REGMAP_ENDIAN_NATIVE:
532 map->format.format_val = regmap_format_16_native;
533 map->format.parse_val = regmap_parse_16_native;
534 break;
535 default:
536 goto err_map;
537 }
538 break;
539 case 24:
540 if (val_endian != REGMAP_ENDIAN_BIG)
541 goto err_map;
542 map->format.format_val = regmap_format_24;
543 map->format.parse_val = regmap_parse_24;
544 break;
545 case 32:
546 switch (val_endian) {
547 case REGMAP_ENDIAN_BIG:
548 map->format.format_val = regmap_format_32_be;
549 map->format.parse_val = regmap_parse_32_be;
550 break;
551 case REGMAP_ENDIAN_NATIVE:
552 map->format.format_val = regmap_format_32_native;
553 map->format.parse_val = regmap_parse_32_native;
554 break;
555 default:
556 goto err_map;
557 }
558 break; 254 break;
559 } 255 }
560 256
561 if (map->format.format_write) {
562 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
563 (val_endian != REGMAP_ENDIAN_BIG))
564 goto err_map;
565 map->use_single_rw = true;
566 }
567
568 if (!map->format.format_write && 257 if (!map->format.format_write &&
569 !(map->format.format_reg && map->format.format_val)) 258 !(map->format.format_reg && map->format.format_val))
570 goto err_map; 259 goto err_map;
@@ -575,115 +264,15 @@ struct regmap *regmap_init(struct device *dev,
575 goto err_map; 264 goto err_map;
576 } 265 }
577 266
578 map->range_tree = RB_ROOT; 267 regmap_debugfs_init(map);
579 for (i = 0; i < config->num_ranges; i++) {
580 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
581 struct regmap_range_node *new;
582
583 /* Sanity check */
584 if (range_cfg->range_max < range_cfg->range_min) {
585 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
586 range_cfg->range_max, range_cfg->range_min);
587 goto err_range;
588 }
589
590 if (range_cfg->range_max > map->max_register) {
591 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
592 range_cfg->range_max, map->max_register);
593 goto err_range;
594 }
595
596 if (range_cfg->selector_reg > map->max_register) {
597 dev_err(map->dev,
598 "Invalid range %d: selector out of map\n", i);
599 goto err_range;
600 }
601
602 if (range_cfg->window_len == 0) {
603 dev_err(map->dev, "Invalid range %d: window_len 0\n",
604 i);
605 goto err_range;
606 }
607
608 /* Make sure, that this register range has no selector
609 or data window within its boundary */
610 for (j = 0; j < config->num_ranges; j++) {
611 unsigned sel_reg = config->ranges[j].selector_reg;
612 unsigned win_min = config->ranges[j].window_start;
613 unsigned win_max = win_min +
614 config->ranges[j].window_len - 1;
615
616 if (range_cfg->range_min <= sel_reg &&
617 sel_reg <= range_cfg->range_max) {
618 dev_err(map->dev,
619 "Range %d: selector for %d in window\n",
620 i, j);
621 goto err_range;
622 }
623
624 if (!(win_max < range_cfg->range_min ||
625 win_min > range_cfg->range_max)) {
626 dev_err(map->dev,
627 "Range %d: window for %d in window\n",
628 i, j);
629 goto err_range;
630 }
631 }
632
633 new = kzalloc(sizeof(*new), GFP_KERNEL);
634 if (new == NULL) {
635 ret = -ENOMEM;
636 goto err_range;
637 }
638
639 new->map = map;
640 new->name = range_cfg->name;
641 new->range_min = range_cfg->range_min;
642 new->range_max = range_cfg->range_max;
643 new->selector_reg = range_cfg->selector_reg;
644 new->selector_mask = range_cfg->selector_mask;
645 new->selector_shift = range_cfg->selector_shift;
646 new->window_start = range_cfg->window_start;
647 new->window_len = range_cfg->window_len;
648
649 if (_regmap_range_add(map, new) == false) {
650 dev_err(map->dev, "Failed to add range %d\n", i);
651 kfree(new);
652 goto err_range;
653 }
654
655 if (map->selector_work_buf == NULL) {
656 map->selector_work_buf =
657 kzalloc(map->format.buf_size, GFP_KERNEL);
658 if (map->selector_work_buf == NULL) {
659 ret = -ENOMEM;
660 goto err_range;
661 }
662 }
663 }
664 268
665 ret = regcache_init(map, config); 269 ret = regcache_init(map, config);
666 if (ret != 0) 270 if (ret < 0)
667 goto err_range; 271 goto err_free_workbuf;
668
669 regmap_debugfs_init(map, config->name);
670
671 /* Add a devres resource for dev_get_regmap() */
672 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
673 if (!m) {
674 ret = -ENOMEM;
675 goto err_debugfs;
676 }
677 *m = map;
678 devres_add(dev, m);
679 272
680 return map; 273 return map;
681 274
682err_debugfs: 275err_free_workbuf:
683 regmap_debugfs_exit(map);
684 regcache_exit(map);
685err_range:
686 regmap_range_exit(map);
687 kfree(map->work_buf); 276 kfree(map->work_buf);
688err_map: 277err_map:
689 kfree(map); 278 kfree(map);
@@ -702,7 +291,6 @@ static void devm_regmap_release(struct device *dev, void *res)
702 * 291 *
703 * @dev: Device that will be interacted with 292 * @dev: Device that will be interacted with
704 * @bus: Bus-specific callbacks to use with device 293 * @bus: Bus-specific callbacks to use with device
705 * @bus_context: Data passed to bus-specific callbacks
706 * @config: Configuration for register map 294 * @config: Configuration for register map
707 * 295 *
708 * The return value will be an ERR_PTR() on error or a valid pointer 296 * The return value will be an ERR_PTR() on error or a valid pointer
@@ -712,7 +300,6 @@ static void devm_regmap_release(struct device *dev, void *res)
712 */ 300 */
713struct regmap *devm_regmap_init(struct device *dev, 301struct regmap *devm_regmap_init(struct device *dev,
714 const struct regmap_bus *bus, 302 const struct regmap_bus *bus,
715 void *bus_context,
716 const struct regmap_config *config) 303 const struct regmap_config *config)
717{ 304{
718 struct regmap **ptr, *regmap; 305 struct regmap **ptr, *regmap;
@@ -721,7 +308,7 @@ struct regmap *devm_regmap_init(struct device *dev,
721 if (!ptr) 308 if (!ptr)
722 return ERR_PTR(-ENOMEM); 309 return ERR_PTR(-ENOMEM);
723 310
724 regmap = regmap_init(dev, bus, bus_context, config); 311 regmap = regmap_init(dev, bus, config);
725 if (!IS_ERR(regmap)) { 312 if (!IS_ERR(regmap)) {
726 *ptr = regmap; 313 *ptr = regmap;
727 devres_add(dev, ptr); 314 devres_add(dev, ptr);
@@ -743,12 +330,13 @@ EXPORT_SYMBOL_GPL(devm_regmap_init);
743 * new cache. This can be used to restore the cache to defaults or to 330 * new cache. This can be used to restore the cache to defaults or to
744 * update the cache configuration to reflect runtime discovery of the 331 * update the cache configuration to reflect runtime discovery of the
745 * hardware. 332 * hardware.
746 *
747 * No explicit locking is done here, the user needs to ensure that
748 * this function will not race with other calls to regmap.
749 */ 333 */
750int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) 334int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
751{ 335{
336 int ret;
337
338 mutex_lock(&map->lock);
339
752 regcache_exit(map); 340 regcache_exit(map);
753 regmap_debugfs_exit(map); 341 regmap_debugfs_exit(map);
754 342
@@ -759,14 +347,16 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
759 map->precious_reg = config->precious_reg; 347 map->precious_reg = config->precious_reg;
760 map->cache_type = config->cache_type; 348 map->cache_type = config->cache_type;
761 349
762 regmap_debugfs_init(map, config->name);
763
764 map->cache_bypass = false; 350 map->cache_bypass = false;
765 map->cache_only = false; 351 map->cache_only = false;
352 regmap_debugfs_init(map);
766 353
767 return regcache_init(map, config); 354 ret = regcache_init(map, config);
355
356 mutex_unlock(&map->lock);
357
358 return ret;
768} 359}
769EXPORT_SYMBOL_GPL(regmap_reinit_cache);
770 360
771/** 361/**
772 * regmap_exit(): Free a previously allocated register map 362 * regmap_exit(): Free a previously allocated register map
@@ -775,104 +365,14 @@ void regmap_exit(struct regmap *map)
775{ 365{
776 regcache_exit(map); 366 regcache_exit(map);
777 regmap_debugfs_exit(map); 367 regmap_debugfs_exit(map);
778 regmap_range_exit(map);
779 if (map->bus->free_context)
780 map->bus->free_context(map->bus_context);
781 kfree(map->work_buf); 368 kfree(map->work_buf);
782 kfree(map); 369 kfree(map);
783} 370}
784EXPORT_SYMBOL_GPL(regmap_exit); 371EXPORT_SYMBOL_GPL(regmap_exit);
785 372
786static int dev_get_regmap_match(struct device *dev, void *res, void *data)
787{
788 struct regmap **r = res;
789 if (!r || !*r) {
790 WARN_ON(!r || !*r);
791 return 0;
792 }
793
794 /* If the user didn't specify a name match any */
795 if (data)
796 return (*r)->name == data;
797 else
798 return 1;
799}
800
801/**
802 * dev_get_regmap(): Obtain the regmap (if any) for a device
803 *
804 * @dev: Device to retrieve the map for
805 * @name: Optional name for the register map, usually NULL.
806 *
807 * Returns the regmap for the device if one is present, or NULL. If
808 * name is specified then it must match the name specified when
809 * registering the device, if it is NULL then the first regmap found
810 * will be used. Devices with multiple register maps are very rare,
811 * generic code should normally not need to specify a name.
812 */
813struct regmap *dev_get_regmap(struct device *dev, const char *name)
814{
815 struct regmap **r = devres_find(dev, dev_get_regmap_release,
816 dev_get_regmap_match, (void *)name);
817
818 if (!r)
819 return NULL;
820 return *r;
821}
822EXPORT_SYMBOL_GPL(dev_get_regmap);
823
824static int _regmap_select_page(struct regmap *map, unsigned int *reg,
825 struct regmap_range_node *range,
826 unsigned int val_num)
827{
828 void *orig_work_buf;
829 unsigned int win_offset;
830 unsigned int win_page;
831 bool page_chg;
832 int ret;
833
834 win_offset = (*reg - range->range_min) % range->window_len;
835 win_page = (*reg - range->range_min) / range->window_len;
836
837 if (val_num > 1) {
838 /* Bulk write shouldn't cross range boundary */
839 if (*reg + val_num - 1 > range->range_max)
840 return -EINVAL;
841
842 /* ... or single page boundary */
843 if (val_num > range->window_len - win_offset)
844 return -EINVAL;
845 }
846
847 /* It is possible to have selector register inside data window.
848 In that case, selector register is located on every page and
849 it needs no page switching, when accessed alone. */
850 if (val_num > 1 ||
851 range->window_start + win_offset != range->selector_reg) {
852 /* Use separate work_buf during page switching */
853 orig_work_buf = map->work_buf;
854 map->work_buf = map->selector_work_buf;
855
856 ret = _regmap_update_bits(map, range->selector_reg,
857 range->selector_mask,
858 win_page << range->selector_shift,
859 &page_chg);
860
861 map->work_buf = orig_work_buf;
862
863 if (ret != 0)
864 return ret;
865 }
866
867 *reg = range->window_start + win_offset;
868
869 return 0;
870}
871
872static int _regmap_raw_write(struct regmap *map, unsigned int reg, 373static int _regmap_raw_write(struct regmap *map, unsigned int reg,
873 const void *val, size_t val_len) 374 const void *val, size_t val_len)
874{ 375{
875 struct regmap_range_node *range;
876 u8 *u8 = map->work_buf; 376 u8 *u8 = map->work_buf;
877 void *buf; 377 void *buf;
878 int ret = -ENOTSUPP; 378 int ret = -ENOTSUPP;
@@ -882,8 +382,7 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
882 /* Check for unwritable registers before we start */ 382 /* Check for unwritable registers before we start */
883 if (map->writeable_reg) 383 if (map->writeable_reg)
884 for (i = 0; i < val_len / map->format.val_bytes; i++) 384 for (i = 0; i < val_len / map->format.val_bytes; i++)
885 if (!map->writeable_reg(map->dev, 385 if (!map->writeable_reg(map->dev, reg + i))
886 reg + (i * map->reg_stride)))
887 return -EINVAL; 386 return -EINVAL;
888 387
889 if (!map->cache_bypass && map->format.parse_val) { 388 if (!map->cache_bypass && map->format.parse_val) {
@@ -892,11 +391,10 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
892 for (i = 0; i < val_len / val_bytes; i++) { 391 for (i = 0; i < val_len / val_bytes; i++) {
893 memcpy(map->work_buf, val + (i * val_bytes), val_bytes); 392 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
894 ival = map->format.parse_val(map->work_buf); 393 ival = map->format.parse_val(map->work_buf);
895 ret = regcache_write(map, reg + (i * map->reg_stride), 394 ret = regcache_write(map, reg + i, ival);
896 ival);
897 if (ret) { 395 if (ret) {
898 dev_err(map->dev, 396 dev_err(map->dev,
899 "Error in caching of register: %x ret: %d\n", 397 "Error in caching of register: %u ret: %d\n",
900 reg + i, ret); 398 reg + i, ret);
901 return ret; 399 return ret;
902 } 400 }
@@ -907,37 +405,7 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
907 } 405 }
908 } 406 }
909 407
910 range = _regmap_range_lookup(map, reg); 408 map->format.format_reg(map->work_buf, reg);
911 if (range) {
912 int val_num = val_len / map->format.val_bytes;
913 int win_offset = (reg - range->range_min) % range->window_len;
914 int win_residue = range->window_len - win_offset;
915
916 /* If the write goes beyond the end of the window split it */
917 while (val_num > win_residue) {
918 dev_dbg(map->dev, "Writing window %d/%zu\n",
919 win_residue, val_len / map->format.val_bytes);
920 ret = _regmap_raw_write(map, reg, val, win_residue *
921 map->format.val_bytes);
922 if (ret != 0)
923 return ret;
924
925 reg += win_residue;
926 val_num -= win_residue;
927 val += win_residue * map->format.val_bytes;
928 val_len -= win_residue * map->format.val_bytes;
929
930 win_offset = (reg - range->range_min) %
931 range->window_len;
932 win_residue = range->window_len - win_offset;
933 }
934
935 ret = _regmap_select_page(map, &reg, range, val_num);
936 if (ret != 0)
937 return ret;
938 }
939
940 map->format.format_reg(map->work_buf, reg, map->reg_shift);
941 409
942 u8[0] |= map->write_flag_mask; 410 u8[0] |= map->write_flag_mask;
943 411
@@ -950,12 +418,12 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
950 */ 418 */
951 if (val == (map->work_buf + map->format.pad_bytes + 419 if (val == (map->work_buf + map->format.pad_bytes +
952 map->format.reg_bytes)) 420 map->format.reg_bytes))
953 ret = map->bus->write(map->bus_context, map->work_buf, 421 ret = map->bus->write(map->dev, map->work_buf,
954 map->format.reg_bytes + 422 map->format.reg_bytes +
955 map->format.pad_bytes + 423 map->format.pad_bytes +
956 val_len); 424 val_len);
957 else if (map->bus->gather_write) 425 else if (map->bus->gather_write)
958 ret = map->bus->gather_write(map->bus_context, map->work_buf, 426 ret = map->bus->gather_write(map->dev, map->work_buf,
959 map->format.reg_bytes + 427 map->format.reg_bytes +
960 map->format.pad_bytes, 428 map->format.pad_bytes,
961 val, val_len); 429 val, val_len);
@@ -970,7 +438,7 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
970 memcpy(buf, map->work_buf, map->format.reg_bytes); 438 memcpy(buf, map->work_buf, map->format.reg_bytes);
971 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, 439 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
972 val, val_len); 440 val, val_len);
973 ret = map->bus->write(map->bus_context, buf, len); 441 ret = map->bus->write(map->dev, buf, len);
974 442
975 kfree(buf); 443 kfree(buf);
976 } 444 }
@@ -984,7 +452,6 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
984int _regmap_write(struct regmap *map, unsigned int reg, 452int _regmap_write(struct regmap *map, unsigned int reg,
985 unsigned int val) 453 unsigned int val)
986{ 454{
987 struct regmap_range_node *range;
988 int ret; 455 int ret;
989 BUG_ON(!map->format.format_write && !map->format.format_val); 456 BUG_ON(!map->format.format_write && !map->format.format_val);
990 457
@@ -998,26 +465,14 @@ int _regmap_write(struct regmap *map, unsigned int reg,
998 } 465 }
999 } 466 }
1000 467
1001#ifdef LOG_DEVICE
1002 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1003 dev_info(map->dev, "%x <= %x\n", reg, val);
1004#endif
1005
1006 trace_regmap_reg_write(map->dev, reg, val); 468 trace_regmap_reg_write(map->dev, reg, val);
1007 469
1008 if (map->format.format_write) { 470 if (map->format.format_write) {
1009 range = _regmap_range_lookup(map, reg);
1010 if (range) {
1011 ret = _regmap_select_page(map, &reg, range, 1);
1012 if (ret != 0)
1013 return ret;
1014 }
1015
1016 map->format.format_write(map, reg, val); 471 map->format.format_write(map, reg, val);
1017 472
1018 trace_regmap_hw_write_start(map->dev, reg, 1); 473 trace_regmap_hw_write_start(map->dev, reg, 1);
1019 474
1020 ret = map->bus->write(map->bus_context, map->work_buf, 475 ret = map->bus->write(map->dev, map->work_buf,
1021 map->format.buf_size); 476 map->format.buf_size);
1022 477
1023 trace_regmap_hw_write_done(map->dev, reg, 1); 478 trace_regmap_hw_write_done(map->dev, reg, 1);
@@ -1025,7 +480,7 @@ int _regmap_write(struct regmap *map, unsigned int reg,
1025 return ret; 480 return ret;
1026 } else { 481 } else {
1027 map->format.format_val(map->work_buf + map->format.reg_bytes 482 map->format.format_val(map->work_buf + map->format.reg_bytes
1028 + map->format.pad_bytes, val, 0); 483 + map->format.pad_bytes, val);
1029 return _regmap_raw_write(map, reg, 484 return _regmap_raw_write(map, reg,
1030 map->work_buf + 485 map->work_buf +
1031 map->format.reg_bytes + 486 map->format.reg_bytes +
@@ -1048,14 +503,11 @@ int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1048{ 503{
1049 int ret; 504 int ret;
1050 505
1051 if (reg % map->reg_stride) 506 mutex_lock(&map->lock);
1052 return -EINVAL;
1053
1054 map->lock(map->lock_arg);
1055 507
1056 ret = _regmap_write(map, reg, val); 508 ret = _regmap_write(map, reg, val);
1057 509
1058 map->unlock(map->lock_arg); 510 mutex_unlock(&map->lock);
1059 511
1060 return ret; 512 return ret;
1061} 513}
@@ -1082,16 +534,11 @@ int regmap_raw_write(struct regmap *map, unsigned int reg,
1082{ 534{
1083 int ret; 535 int ret;
1084 536
1085 if (val_len % map->format.val_bytes) 537 mutex_lock(&map->lock);
1086 return -EINVAL;
1087 if (reg % map->reg_stride)
1088 return -EINVAL;
1089
1090 map->lock(map->lock_arg);
1091 538
1092 ret = _regmap_raw_write(map, reg, val, val_len); 539 ret = _regmap_raw_write(map, reg, val, val_len);
1093 540
1094 map->unlock(map->lock_arg); 541 mutex_unlock(&map->lock);
1095 542
1096 return ret; 543 return ret;
1097} 544}
@@ -1120,10 +567,8 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1120 567
1121 if (!map->format.parse_val) 568 if (!map->format.parse_val)
1122 return -EINVAL; 569 return -EINVAL;
1123 if (reg % map->reg_stride)
1124 return -EINVAL;
1125 570
1126 map->lock(map->lock_arg); 571 mutex_lock(&map->lock);
1127 572
1128 /* No formatting is require if val_byte is 1 */ 573 /* No formatting is require if val_byte is 1 */
1129 if (val_bytes == 1) { 574 if (val_bytes == 1) {
@@ -1138,28 +583,13 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1138 for (i = 0; i < val_count * val_bytes; i += val_bytes) 583 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1139 map->format.parse_val(wval + i); 584 map->format.parse_val(wval + i);
1140 } 585 }
1141 /* 586 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1142 * Some devices does not support bulk write, for
1143 * them we have a series of single write operations.
1144 */
1145 if (map->use_single_rw) {
1146 for (i = 0; i < val_count; i++) {
1147 ret = regmap_raw_write(map,
1148 reg + (i * map->reg_stride),
1149 val + (i * val_bytes),
1150 val_bytes);
1151 if (ret != 0)
1152 return ret;
1153 }
1154 } else {
1155 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1156 }
1157 587
1158 if (val_bytes != 1) 588 if (val_bytes != 1)
1159 kfree(wval); 589 kfree(wval);
1160 590
1161out: 591out:
1162 map->unlock(map->lock_arg); 592 mutex_unlock(&map->lock);
1163 return ret; 593 return ret;
1164} 594}
1165EXPORT_SYMBOL_GPL(regmap_bulk_write); 595EXPORT_SYMBOL_GPL(regmap_bulk_write);
@@ -1167,19 +597,10 @@ EXPORT_SYMBOL_GPL(regmap_bulk_write);
1167static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 597static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1168 unsigned int val_len) 598 unsigned int val_len)
1169{ 599{
1170 struct regmap_range_node *range;
1171 u8 *u8 = map->work_buf; 600 u8 *u8 = map->work_buf;
1172 int ret; 601 int ret;
1173 602
1174 range = _regmap_range_lookup(map, reg); 603 map->format.format_reg(map->work_buf, reg);
1175 if (range) {
1176 ret = _regmap_select_page(map, &reg, range,
1177 val_len / map->format.val_bytes);
1178 if (ret != 0)
1179 return ret;
1180 }
1181
1182 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1183 604
1184 /* 605 /*
1185 * Some buses or devices flag reads by setting the high bits in the 606 * Some buses or devices flag reads by setting the high bits in the
@@ -1192,7 +613,7 @@ static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1192 trace_regmap_hw_read_start(map->dev, reg, 613 trace_regmap_hw_read_start(map->dev, reg,
1193 val_len / map->format.val_bytes); 614 val_len / map->format.val_bytes);
1194 615
1195 ret = map->bus->read(map->bus_context, map->work_buf, 616 ret = map->bus->read(map->dev, map->work_buf,
1196 map->format.reg_bytes + map->format.pad_bytes, 617 map->format.reg_bytes + map->format.pad_bytes,
1197 val, val_len); 618 val, val_len);
1198 619
@@ -1222,18 +643,9 @@ static int _regmap_read(struct regmap *map, unsigned int reg,
1222 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes); 643 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
1223 if (ret == 0) { 644 if (ret == 0) {
1224 *val = map->format.parse_val(map->work_buf); 645 *val = map->format.parse_val(map->work_buf);
1225
1226#ifdef LOG_DEVICE
1227 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1228 dev_info(map->dev, "%x => %x\n", reg, *val);
1229#endif
1230
1231 trace_regmap_reg_read(map->dev, reg, *val); 646 trace_regmap_reg_read(map->dev, reg, *val);
1232 } 647 }
1233 648
1234 if (ret == 0 && !map->cache_bypass)
1235 regcache_write(map, reg, *val);
1236
1237 return ret; 649 return ret;
1238} 650}
1239 651
@@ -1251,14 +663,11 @@ int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1251{ 663{
1252 int ret; 664 int ret;
1253 665
1254 if (reg % map->reg_stride) 666 mutex_lock(&map->lock);
1255 return -EINVAL;
1256
1257 map->lock(map->lock_arg);
1258 667
1259 ret = _regmap_read(map, reg, val); 668 ret = _regmap_read(map, reg, val);
1260 669
1261 map->unlock(map->lock_arg); 670 mutex_unlock(&map->lock);
1262 671
1263 return ret; 672 return ret;
1264} 673}
@@ -1278,39 +687,17 @@ EXPORT_SYMBOL_GPL(regmap_read);
1278int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 687int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1279 size_t val_len) 688 size_t val_len)
1280{ 689{
1281 size_t val_bytes = map->format.val_bytes; 690 size_t val_count = val_len / map->format.val_bytes;
1282 size_t val_count = val_len / val_bytes; 691 int ret;
1283 unsigned int v;
1284 int ret, i;
1285
1286 if (val_len % map->format.val_bytes)
1287 return -EINVAL;
1288 if (reg % map->reg_stride)
1289 return -EINVAL;
1290 692
1291 map->lock(map->lock_arg); 693 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
694 map->cache_type != REGCACHE_NONE);
1292 695
1293 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || 696 mutex_lock(&map->lock);
1294 map->cache_type == REGCACHE_NONE) {
1295 /* Physical block read if there's no cache involved */
1296 ret = _regmap_raw_read(map, reg, val, val_len);
1297 697
1298 } else { 698 ret = _regmap_raw_read(map, reg, val, val_len);
1299 /* Otherwise go word by word for the cache; should be low
1300 * cost as we expect to hit the cache.
1301 */
1302 for (i = 0; i < val_count; i++) {
1303 ret = _regmap_read(map, reg + (i * map->reg_stride),
1304 &v);
1305 if (ret != 0)
1306 goto out;
1307 699
1308 map->format.format_val(val + (i * val_bytes), v, 0); 700 mutex_unlock(&map->lock);
1309 }
1310 }
1311
1312 out:
1313 map->unlock(map->lock_arg);
1314 701
1315 return ret; 702 return ret;
1316} 703}
@@ -1336,40 +723,19 @@ int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1336 723
1337 if (!map->format.parse_val) 724 if (!map->format.parse_val)
1338 return -EINVAL; 725 return -EINVAL;
1339 if (reg % map->reg_stride)
1340 return -EINVAL;
1341 726
1342 if (vol || map->cache_type == REGCACHE_NONE) { 727 if (vol || map->cache_type == REGCACHE_NONE) {
1343 /* 728 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
1344 * Some devices does not support bulk read, for 729 if (ret != 0)
1345 * them we have a series of single read operations. 730 return ret;
1346 */
1347 if (map->use_single_rw) {
1348 for (i = 0; i < val_count; i++) {
1349 ret = regmap_raw_read(map,
1350 reg + (i * map->reg_stride),
1351 val + (i * val_bytes),
1352 val_bytes);
1353 if (ret != 0)
1354 return ret;
1355 }
1356 } else {
1357 ret = regmap_raw_read(map, reg, val,
1358 val_bytes * val_count);
1359 if (ret != 0)
1360 return ret;
1361 }
1362 731
1363 for (i = 0; i < val_count * val_bytes; i += val_bytes) 732 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1364 map->format.parse_val(val + i); 733 map->format.parse_val(val + i);
1365 } else { 734 } else {
1366 for (i = 0; i < val_count; i++) { 735 for (i = 0; i < val_count; i++) {
1367 unsigned int ival; 736 ret = regmap_read(map, reg + i, val + (i * val_bytes));
1368 ret = regmap_read(map, reg + (i * map->reg_stride),
1369 &ival);
1370 if (ret != 0) 737 if (ret != 0)
1371 return ret; 738 return ret;
1372 memcpy(val + (i * val_bytes), &ival, val_bytes);
1373 } 739 }
1374 } 740 }
1375 741
@@ -1384,9 +750,11 @@ static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1384 int ret; 750 int ret;
1385 unsigned int tmp, orig; 751 unsigned int tmp, orig;
1386 752
753 mutex_lock(&map->lock);
754
1387 ret = _regmap_read(map, reg, &orig); 755 ret = _regmap_read(map, reg, &orig);
1388 if (ret != 0) 756 if (ret != 0)
1389 return ret; 757 goto out;
1390 758
1391 tmp = orig & ~mask; 759 tmp = orig & ~mask;
1392 tmp |= val & mask; 760 tmp |= val & mask;
@@ -1398,6 +766,9 @@ static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1398 *change = false; 766 *change = false;
1399 } 767 }
1400 768
769out:
770 mutex_unlock(&map->lock);
771
1401 return ret; 772 return ret;
1402} 773}
1403 774
@@ -1415,20 +786,49 @@ int regmap_update_bits(struct regmap *map, unsigned int reg,
1415 unsigned int mask, unsigned int val) 786 unsigned int mask, unsigned int val)
1416{ 787{
1417 bool change; 788 bool change;
1418 int ret; 789 return _regmap_update_bits(map, reg, mask, val, &change);
790}
791EXPORT_SYMBOL_GPL(regmap_update_bits);
792
793/**
794 * remap_update_bits_lazy: Perform a read/modify/write cycle on the register
795 * map. Only write new contents if they differ from the previous ones.
796 *
797 * @map: Register map to update
798 * @reg: Register to update
799 * @mask: Bitmask to change
800 * @val: New value for bitmask
801 *
802 * Returns zero for success, a negative number on error.
803 */
804int regmap_update_bits_lazy(struct regmap *map, unsigned int reg,
805 unsigned int mask, unsigned int val)
806{
807 int ret, new;
808 unsigned int tmp;
809
810 mutex_lock(&map->lock);
811
812 ret = _regmap_read(map, reg, &tmp);
813 if (ret != 0)
814 goto out;
1419 815
1420 map->lock(map->lock_arg); 816 new = tmp & ~mask;
1421 ret = _regmap_update_bits(map, reg, mask, val, &change); 817 new |= val & mask;
1422 map->unlock(map->lock_arg); 818 if (new != tmp) {
819 ret = _regmap_write(map, reg, new);
820 }
821
822out:
823 mutex_unlock(&map->lock);
1423 824
1424 return ret; 825 return ret;
1425} 826}
1426EXPORT_SYMBOL_GPL(regmap_update_bits); 827EXPORT_SYMBOL_GPL(regmap_update_bits_lazy);
1427 828
1428/** 829/**
1429 * regmap_update_bits_check: Perform a read/modify/write cycle on the 830 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1430 * register map and report if updated 831 * register map and report if updated
1431 *
1432 * @map: Register map to update 832 * @map: Register map to update
1433 * @reg: Register to update 833 * @reg: Register to update
1434 * @mask: Bitmask to change 834 * @mask: Bitmask to change
@@ -1441,12 +841,7 @@ int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1441 unsigned int mask, unsigned int val, 841 unsigned int mask, unsigned int val,
1442 bool *change) 842 bool *change)
1443{ 843{
1444 int ret; 844 return _regmap_update_bits(map, reg, mask, val, change);
1445
1446 map->lock(map->lock_arg);
1447 ret = _regmap_update_bits(map, reg, mask, val, change);
1448 map->unlock(map->lock_arg);
1449 return ret;
1450} 845}
1451EXPORT_SYMBOL_GPL(regmap_update_bits_check); 846EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1452 847
@@ -1474,7 +869,7 @@ int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1474 if (map->patch) 869 if (map->patch)
1475 return -EBUSY; 870 return -EBUSY;
1476 871
1477 map->lock(map->lock_arg); 872 mutex_lock(&map->lock);
1478 873
1479 bypass = map->cache_bypass; 874 bypass = map->cache_bypass;
1480 875
@@ -1502,27 +897,12 @@ int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1502out: 897out:
1503 map->cache_bypass = bypass; 898 map->cache_bypass = bypass;
1504 899
1505 map->unlock(map->lock_arg); 900 mutex_unlock(&map->lock);
1506 901
1507 return ret; 902 return ret;
1508} 903}
1509EXPORT_SYMBOL_GPL(regmap_register_patch); 904EXPORT_SYMBOL_GPL(regmap_register_patch);
1510 905
1511/*
1512 * regmap_get_val_bytes(): Report the size of a register value
1513 *
1514 * Report the size of a register value, mainly intended to for use by
1515 * generic infrastructure built on top of regmap.
1516 */
1517int regmap_get_val_bytes(struct regmap *map)
1518{
1519 if (map->format.format_write)
1520 return -EINVAL;
1521
1522 return map->format.val_bytes;
1523}
1524EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1525
1526static int __init regmap_initcall(void) 906static int __init regmap_initcall(void)
1527{ 907{
1528 regmap_debugfs_initcall(); 908 regmap_debugfs_initcall();