aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/regmap/regmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base/regmap/regmap.c')
-rw-r--r--drivers/base/regmap/regmap.c119
1 files changed, 97 insertions, 22 deletions
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 1752f13ddeb..e3ee9cabccb 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -10,8 +10,9 @@
10 * published by the Free Software Foundation. 10 * published by the Free Software Foundation.
11 */ 11 */
12 12
13#include <linux/device.h>
13#include <linux/slab.h> 14#include <linux/slab.h>
14#include <linux/module.h> 15#include <linux/export.h>
15#include <linux/mutex.h> 16#include <linux/mutex.h>
16#include <linux/err.h> 17#include <linux/err.h>
17 18
@@ -36,6 +37,9 @@ bool regmap_readable(struct regmap *map, unsigned int reg)
36 if (map->max_register && reg > map->max_register) 37 if (map->max_register && reg > map->max_register)
37 return false; 38 return false;
38 39
40 if (map->format.format_write)
41 return false;
42
39 if (map->readable_reg) 43 if (map->readable_reg)
40 return map->readable_reg(map->dev, reg); 44 return map->readable_reg(map->dev, reg);
41 45
@@ -44,7 +48,7 @@ bool regmap_readable(struct regmap *map, unsigned int reg)
44 48
45bool regmap_volatile(struct regmap *map, unsigned int reg) 49bool regmap_volatile(struct regmap *map, unsigned int reg)
46{ 50{
47 if (map->max_register && reg > map->max_register) 51 if (!regmap_readable(map, reg))
48 return false; 52 return false;
49 53
50 if (map->volatile_reg) 54 if (map->volatile_reg)
@@ -55,7 +59,7 @@ bool regmap_volatile(struct regmap *map, unsigned int reg)
55 59
56bool regmap_precious(struct regmap *map, unsigned int reg) 60bool regmap_precious(struct regmap *map, unsigned int reg)
57{ 61{
58 if (map->max_register && reg > map->max_register) 62 if (!regmap_readable(map, reg))
59 return false; 63 return false;
60 64
61 if (map->precious_reg) 65 if (map->precious_reg)
@@ -76,6 +80,14 @@ static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
76 return true; 80 return true;
77} 81}
78 82
83static void regmap_format_2_6_write(struct regmap *map,
84 unsigned int reg, unsigned int val)
85{
86 u8 *out = map->work_buf;
87
88 *out = (reg << 6) | val;
89}
90
79static void regmap_format_4_12_write(struct regmap *map, 91static void regmap_format_4_12_write(struct regmap *map,
80 unsigned int reg, unsigned int val) 92 unsigned int reg, unsigned int val)
81{ 93{
@@ -114,6 +126,13 @@ static void regmap_format_16(void *buf, unsigned int val)
114 b[0] = cpu_to_be16(val); 126 b[0] = cpu_to_be16(val);
115} 127}
116 128
129static void regmap_format_32(void *buf, unsigned int val)
130{
131 __be32 *b = buf;
132
133 b[0] = cpu_to_be32(val);
134}
135
117static unsigned int regmap_parse_8(void *buf) 136static unsigned int regmap_parse_8(void *buf)
118{ 137{
119 u8 *b = buf; 138 u8 *b = buf;
@@ -130,6 +149,15 @@ static unsigned int regmap_parse_16(void *buf)
130 return b[0]; 149 return b[0];
131} 150}
132 151
152static unsigned int regmap_parse_32(void *buf)
153{
154 __be32 *b = buf;
155
156 b[0] = be32_to_cpu(b[0]);
157
158 return b[0];
159}
160
133/** 161/**
134 * regmap_init(): Initialise register map 162 * regmap_init(): Initialise register map
135 * 163 *
@@ -159,8 +187,10 @@ struct regmap *regmap_init(struct device *dev,
159 187
160 mutex_init(&map->lock); 188 mutex_init(&map->lock);
161 map->format.buf_size = (config->reg_bits + config->val_bits) / 8; 189 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
162 map->format.reg_bytes = config->reg_bits / 8; 190 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
163 map->format.val_bytes = config->val_bits / 8; 191 map->format.pad_bytes = config->pad_bits / 8;
192 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
193 map->format.buf_size += map->format.pad_bytes;
164 map->dev = dev; 194 map->dev = dev;
165 map->bus = bus; 195 map->bus = bus;
166 map->max_register = config->max_register; 196 map->max_register = config->max_register;
@@ -178,6 +208,16 @@ struct regmap *regmap_init(struct device *dev,
178 } 208 }
179 209
180 switch (config->reg_bits) { 210 switch (config->reg_bits) {
211 case 2:
212 switch (config->val_bits) {
213 case 6:
214 map->format.format_write = regmap_format_2_6_write;
215 break;
216 default:
217 goto err_map;
218 }
219 break;
220
181 case 4: 221 case 4:
182 switch (config->val_bits) { 222 switch (config->val_bits) {
183 case 12: 223 case 12:
@@ -216,6 +256,10 @@ struct regmap *regmap_init(struct device *dev,
216 map->format.format_reg = regmap_format_16; 256 map->format.format_reg = regmap_format_16;
217 break; 257 break;
218 258
259 case 32:
260 map->format.format_reg = regmap_format_32;
261 break;
262
219 default: 263 default:
220 goto err_map; 264 goto err_map;
221 } 265 }
@@ -229,13 +273,17 @@ struct regmap *regmap_init(struct device *dev,
229 map->format.format_val = regmap_format_16; 273 map->format.format_val = regmap_format_16;
230 map->format.parse_val = regmap_parse_16; 274 map->format.parse_val = regmap_parse_16;
231 break; 275 break;
276 case 32:
277 map->format.format_val = regmap_format_32;
278 map->format.parse_val = regmap_parse_32;
279 break;
232 } 280 }
233 281
234 if (!map->format.format_write && 282 if (!map->format.format_write &&
235 !(map->format.format_reg && map->format.format_val)) 283 !(map->format.format_reg && map->format.format_val))
236 goto err_map; 284 goto err_map;
237 285
238 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL); 286 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
239 if (map->work_buf == NULL) { 287 if (map->work_buf == NULL) {
240 ret = -ENOMEM; 288 ret = -ENOMEM;
241 goto err_map; 289 goto err_map;
@@ -315,6 +363,7 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
315 mutex_lock(&map->lock); 363 mutex_lock(&map->lock);
316 364
317 regcache_exit(map); 365 regcache_exit(map);
366 regmap_debugfs_exit(map);
318 367
319 map->max_register = config->max_register; 368 map->max_register = config->max_register;
320 map->writeable_reg = config->writeable_reg; 369 map->writeable_reg = config->writeable_reg;
@@ -323,6 +372,8 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
323 map->precious_reg = config->precious_reg; 372 map->precious_reg = config->precious_reg;
324 map->cache_type = config->cache_type; 373 map->cache_type = config->cache_type;
325 374
375 regmap_debugfs_init(map);
376
326 map->cache_bypass = false; 377 map->cache_bypass = false;
327 map->cache_only = false; 378 map->cache_only = false;
328 379
@@ -371,23 +422,28 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
371 * send the work_buf directly, otherwise try to do a gather 422 * send the work_buf directly, otherwise try to do a gather
372 * write. 423 * write.
373 */ 424 */
374 if (val == map->work_buf + map->format.reg_bytes) 425 if (val == (map->work_buf + map->format.pad_bytes +
426 map->format.reg_bytes))
375 ret = map->bus->write(map->dev, map->work_buf, 427 ret = map->bus->write(map->dev, map->work_buf,
376 map->format.reg_bytes + val_len); 428 map->format.reg_bytes +
429 map->format.pad_bytes +
430 val_len);
377 else if (map->bus->gather_write) 431 else if (map->bus->gather_write)
378 ret = map->bus->gather_write(map->dev, map->work_buf, 432 ret = map->bus->gather_write(map->dev, map->work_buf,
379 map->format.reg_bytes, 433 map->format.reg_bytes +
434 map->format.pad_bytes,
380 val, val_len); 435 val, val_len);
381 436
382 /* If that didn't work fall back on linearising by hand. */ 437 /* If that didn't work fall back on linearising by hand. */
383 if (ret == -ENOTSUPP) { 438 if (ret == -ENOTSUPP) {
384 len = map->format.reg_bytes + val_len; 439 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
385 buf = kmalloc(len, GFP_KERNEL); 440 buf = kzalloc(len, GFP_KERNEL);
386 if (!buf) 441 if (!buf)
387 return -ENOMEM; 442 return -ENOMEM;
388 443
389 memcpy(buf, map->work_buf, map->format.reg_bytes); 444 memcpy(buf, map->work_buf, map->format.reg_bytes);
390 memcpy(buf + map->format.reg_bytes, val, val_len); 445 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
446 val, val_len);
391 ret = map->bus->write(map->dev, buf, len); 447 ret = map->bus->write(map->dev, buf, len);
392 448
393 kfree(buf); 449 kfree(buf);
@@ -429,10 +485,12 @@ int _regmap_write(struct regmap *map, unsigned int reg,
429 485
430 return ret; 486 return ret;
431 } else { 487 } else {
432 map->format.format_val(map->work_buf + map->format.reg_bytes, 488 map->format.format_val(map->work_buf + map->format.reg_bytes
433 val); 489 + map->format.pad_bytes, val);
434 return _regmap_raw_write(map, reg, 490 return _regmap_raw_write(map, reg,
435 map->work_buf + map->format.reg_bytes, 491 map->work_buf +
492 map->format.reg_bytes +
493 map->format.pad_bytes,
436 map->format.val_bytes); 494 map->format.val_bytes);
437 } 495 }
438} 496}
@@ -515,7 +573,8 @@ static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
515 trace_regmap_hw_read_start(map->dev, reg, 573 trace_regmap_hw_read_start(map->dev, reg,
516 val_len / map->format.val_bytes); 574 val_len / map->format.val_bytes);
517 575
518 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes, 576 ret = map->bus->read(map->dev, map->work_buf,
577 map->format.reg_bytes + map->format.pad_bytes,
519 val, val_len); 578 val, val_len);
520 579
521 trace_regmap_hw_read_done(map->dev, reg, 580 trace_regmap_hw_read_done(map->dev, reg,
@@ -588,16 +647,32 @@ EXPORT_SYMBOL_GPL(regmap_read);
588int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 647int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
589 size_t val_len) 648 size_t val_len)
590{ 649{
591 size_t val_count = val_len / map->format.val_bytes; 650 size_t val_bytes = map->format.val_bytes;
592 int ret; 651 size_t val_count = val_len / val_bytes;
593 652 unsigned int v;
594 WARN_ON(!regmap_volatile_range(map, reg, val_count) && 653 int ret, i;
595 map->cache_type != REGCACHE_NONE);
596 654
597 mutex_lock(&map->lock); 655 mutex_lock(&map->lock);
598 656
599 ret = _regmap_raw_read(map, reg, val, val_len); 657 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
658 map->cache_type == REGCACHE_NONE) {
659 /* Physical block read if there's no cache involved */
660 ret = _regmap_raw_read(map, reg, val, val_len);
661
662 } else {
663 /* Otherwise go word by word for the cache; should be low
664 * cost as we expect to hit the cache.
665 */
666 for (i = 0; i < val_count; i++) {
667 ret = _regmap_read(map, reg + i, &v);
668 if (ret != 0)
669 goto out;
670
671 map->format.format_val(val + (i * val_bytes), v);
672 }
673 }
600 674
675 out:
601 mutex_unlock(&map->lock); 676 mutex_unlock(&map->lock);
602 677
603 return ret; 678 return ret;