aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/regmap
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base/regmap')
-rw-r--r--drivers/base/regmap/internal.h14
-rw-r--r--drivers/base/regmap/regcache-lzo.c16
-rw-r--r--drivers/base/regmap/regcache-rbtree.c27
-rw-r--r--drivers/base/regmap/regcache.c101
-rw-r--r--drivers/base/regmap/regmap-debugfs.c84
-rw-r--r--drivers/base/regmap/regmap-i2c.c17
-rw-r--r--drivers/base/regmap/regmap-spi.c17
-rw-r--r--drivers/base/regmap/regmap.c231
8 files changed, 450 insertions, 57 deletions
diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h
index 1a02b7537c8b..fcafc5b2e651 100644
--- a/drivers/base/regmap/internal.h
+++ b/drivers/base/regmap/internal.h
@@ -22,6 +22,7 @@ struct regcache_ops;
22struct regmap_format { 22struct regmap_format {
23 size_t buf_size; 23 size_t buf_size;
24 size_t reg_bytes; 24 size_t reg_bytes;
25 size_t pad_bytes;
25 size_t val_bytes; 26 size_t val_bytes;
26 void (*format_write)(struct regmap *map, 27 void (*format_write)(struct regmap *map,
27 unsigned int reg, unsigned int val); 28 unsigned int reg, unsigned int val);
@@ -65,16 +66,19 @@ struct regmap {
65 unsigned int num_reg_defaults_raw; 66 unsigned int num_reg_defaults_raw;
66 67
67 /* if set, only the cache is modified not the HW */ 68 /* if set, only the cache is modified not the HW */
68 unsigned int cache_only:1; 69 u32 cache_only;
69 /* if set, only the HW is modified not the cache */ 70 /* if set, only the HW is modified not the cache */
70 unsigned int cache_bypass:1; 71 u32 cache_bypass;
71 /* if set, remember to free reg_defaults_raw */ 72 /* if set, remember to free reg_defaults_raw */
72 unsigned int cache_free:1; 73 bool cache_free;
73 74
74 struct reg_default *reg_defaults; 75 struct reg_default *reg_defaults;
75 const void *reg_defaults_raw; 76 const void *reg_defaults_raw;
76 void *cache; 77 void *cache;
77 bool cache_dirty; 78 u32 cache_dirty;
79
80 struct reg_default *patch;
81 int patch_regs;
78}; 82};
79 83
80struct regcache_ops { 84struct regcache_ops {
@@ -84,7 +88,7 @@ struct regcache_ops {
84 int (*exit)(struct regmap *map); 88 int (*exit)(struct regmap *map);
85 int (*read)(struct regmap *map, unsigned int reg, unsigned int *value); 89 int (*read)(struct regmap *map, unsigned int reg, unsigned int *value);
86 int (*write)(struct regmap *map, unsigned int reg, unsigned int value); 90 int (*write)(struct regmap *map, unsigned int reg, unsigned int value);
87 int (*sync)(struct regmap *map); 91 int (*sync)(struct regmap *map, unsigned int min, unsigned int max);
88}; 92};
89 93
90bool regmap_writeable(struct regmap *map, unsigned int reg); 94bool regmap_writeable(struct regmap *map, unsigned int reg);
diff --git a/drivers/base/regmap/regcache-lzo.c b/drivers/base/regmap/regcache-lzo.c
index b7d16143edeb..8d0061569326 100644
--- a/drivers/base/regmap/regcache-lzo.c
+++ b/drivers/base/regmap/regcache-lzo.c
@@ -331,7 +331,8 @@ out:
331 return ret; 331 return ret;
332} 332}
333 333
334static int regcache_lzo_sync(struct regmap *map) 334static int regcache_lzo_sync(struct regmap *map, unsigned int min,
335 unsigned int max)
335{ 336{
336 struct regcache_lzo_ctx **lzo_blocks; 337 struct regcache_lzo_ctx **lzo_blocks;
337 unsigned int val; 338 unsigned int val;
@@ -339,10 +340,21 @@ static int regcache_lzo_sync(struct regmap *map)
339 int ret; 340 int ret;
340 341
341 lzo_blocks = map->cache; 342 lzo_blocks = map->cache;
342 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) { 343 i = min;
344 for_each_set_bit_cont(i, lzo_blocks[0]->sync_bmp,
345 lzo_blocks[0]->sync_bmp_nbits) {
346 if (i > max)
347 continue;
348
343 ret = regcache_read(map, i, &val); 349 ret = regcache_read(map, i, &val);
344 if (ret) 350 if (ret)
345 return ret; 351 return ret;
352
353 /* Is this the hardware default? If so skip. */
354 ret = regcache_lookup_reg(map, i);
355 if (ret > 0 && val == map->reg_defaults[ret].def)
356 continue;
357
346 map->cache_bypass = 1; 358 map->cache_bypass = 1;
347 ret = _regmap_write(map, i, val); 359 ret = _regmap_write(map, i, val);
348 map->cache_bypass = 0; 360 map->cache_bypass = 0;
diff --git a/drivers/base/regmap/regcache-rbtree.c b/drivers/base/regmap/regcache-rbtree.c
index 32620c4f1683..8d51916a283d 100644
--- a/drivers/base/regmap/regcache-rbtree.c
+++ b/drivers/base/regmap/regcache-rbtree.c
@@ -357,7 +357,8 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
357 return 0; 357 return 0;
358} 358}
359 359
360static int regcache_rbtree_sync(struct regmap *map) 360static int regcache_rbtree_sync(struct regmap *map, unsigned int min,
361 unsigned int max)
361{ 362{
362 struct regcache_rbtree_ctx *rbtree_ctx; 363 struct regcache_rbtree_ctx *rbtree_ctx;
363 struct rb_node *node; 364 struct rb_node *node;
@@ -365,19 +366,37 @@ static int regcache_rbtree_sync(struct regmap *map)
365 unsigned int regtmp; 366 unsigned int regtmp;
366 unsigned int val; 367 unsigned int val;
367 int ret; 368 int ret;
368 int i; 369 int i, base, end;
369 370
370 rbtree_ctx = map->cache; 371 rbtree_ctx = map->cache;
371 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) { 372 for (node = rb_first(&rbtree_ctx->root); node; node = rb_next(node)) {
372 rbnode = rb_entry(node, struct regcache_rbtree_node, node); 373 rbnode = rb_entry(node, struct regcache_rbtree_node, node);
373 for (i = 0; i < rbnode->blklen; i++) { 374
375 if (rbnode->base_reg < min)
376 continue;
377 if (rbnode->base_reg > max)
378 break;
379 if (rbnode->base_reg + rbnode->blklen < min)
380 continue;
381
382 if (min > rbnode->base_reg)
383 base = min - rbnode->base_reg;
384 else
385 base = 0;
386
387 if (max < rbnode->base_reg + rbnode->blklen)
388 end = rbnode->base_reg + rbnode->blklen - max;
389 else
390 end = rbnode->blklen;
391
392 for (i = base; i < end; i++) {
374 regtmp = rbnode->base_reg + i; 393 regtmp = rbnode->base_reg + i;
375 val = regcache_rbtree_get_register(rbnode, i, 394 val = regcache_rbtree_get_register(rbnode, i,
376 map->cache_word_size); 395 map->cache_word_size);
377 396
378 /* Is this the hardware default? If so skip. */ 397 /* Is this the hardware default? If so skip. */
379 ret = regcache_lookup_reg(map, i); 398 ret = regcache_lookup_reg(map, i);
380 if (ret > 0 && val == map->reg_defaults[ret].def) 399 if (ret >= 0 && val == map->reg_defaults[ret].def)
381 continue; 400 continue;
382 401
383 map->cache_bypass = 1; 402 map->cache_bypass = 1;
diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
index 4b903a8e92a2..938cb1d2ea26 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -58,7 +58,7 @@ static int regcache_hw_init(struct regmap *map)
58 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) { 58 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
59 val = regcache_get_val(map->reg_defaults_raw, 59 val = regcache_get_val(map->reg_defaults_raw,
60 i, map->cache_word_size); 60 i, map->cache_word_size);
61 if (!val) 61 if (regmap_volatile(map, i))
62 continue; 62 continue;
63 count++; 63 count++;
64 } 64 }
@@ -75,7 +75,7 @@ static int regcache_hw_init(struct regmap *map)
75 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) { 75 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
76 val = regcache_get_val(map->reg_defaults_raw, 76 val = regcache_get_val(map->reg_defaults_raw,
77 i, map->cache_word_size); 77 i, map->cache_word_size);
78 if (!val) 78 if (regmap_volatile(map, i))
79 continue; 79 continue;
80 map->reg_defaults[j].reg = i; 80 map->reg_defaults[j].reg = i;
81 map->reg_defaults[j].def = val; 81 map->reg_defaults[j].def = val;
@@ -216,7 +216,6 @@ int regcache_read(struct regmap *map,
216 216
217 return -EINVAL; 217 return -EINVAL;
218} 218}
219EXPORT_SYMBOL_GPL(regcache_read);
220 219
221/** 220/**
222 * regcache_write: Set the value of a given register in the cache. 221 * regcache_write: Set the value of a given register in the cache.
@@ -243,7 +242,6 @@ int regcache_write(struct regmap *map,
243 242
244 return 0; 243 return 0;
245} 244}
246EXPORT_SYMBOL_GPL(regcache_write);
247 245
248/** 246/**
249 * regcache_sync: Sync the register cache with the hardware. 247 * regcache_sync: Sync the register cache with the hardware.
@@ -259,12 +257,11 @@ EXPORT_SYMBOL_GPL(regcache_write);
259int regcache_sync(struct regmap *map) 257int regcache_sync(struct regmap *map)
260{ 258{
261 int ret = 0; 259 int ret = 0;
262 unsigned int val;
263 unsigned int i; 260 unsigned int i;
264 const char *name; 261 const char *name;
265 unsigned int bypass; 262 unsigned int bypass;
266 263
267 BUG_ON(!map->cache_ops); 264 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
268 265
269 mutex_lock(&map->lock); 266 mutex_lock(&map->lock);
270 /* Remember the initial bypass state */ 267 /* Remember the initial bypass state */
@@ -273,26 +270,27 @@ int regcache_sync(struct regmap *map)
273 map->cache_ops->name); 270 map->cache_ops->name);
274 name = map->cache_ops->name; 271 name = map->cache_ops->name;
275 trace_regcache_sync(map->dev, name, "start"); 272 trace_regcache_sync(map->dev, name, "start");
273
276 if (!map->cache_dirty) 274 if (!map->cache_dirty)
277 goto out; 275 goto out;
278 if (map->cache_ops->sync) {
279 ret = map->cache_ops->sync(map);
280 } else {
281 for (i = 0; i < map->num_reg_defaults; i++) {
282 ret = regcache_read(map, i, &val);
283 if (ret < 0)
284 goto out;
285 map->cache_bypass = 1;
286 ret = _regmap_write(map, i, val);
287 map->cache_bypass = 0;
288 if (ret < 0)
289 goto out;
290 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
291 map->reg_defaults[i].reg,
292 map->reg_defaults[i].def);
293 }
294 276
277 /* Apply any patch first */
278 map->cache_bypass = 1;
279 for (i = 0; i < map->patch_regs; i++) {
280 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
281 if (ret != 0) {
282 dev_err(map->dev, "Failed to write %x = %x: %d\n",
283 map->patch[i].reg, map->patch[i].def, ret);
284 goto out;
285 }
295 } 286 }
287 map->cache_bypass = 0;
288
289 ret = map->cache_ops->sync(map, 0, map->max_register);
290
291 if (ret == 0)
292 map->cache_dirty = false;
293
296out: 294out:
297 trace_regcache_sync(map->dev, name, "stop"); 295 trace_regcache_sync(map->dev, name, "stop");
298 /* Restore the bypass state */ 296 /* Restore the bypass state */
@@ -304,6 +302,51 @@ out:
304EXPORT_SYMBOL_GPL(regcache_sync); 302EXPORT_SYMBOL_GPL(regcache_sync);
305 303
306/** 304/**
305 * regcache_sync_region: Sync part of the register cache with the hardware.
306 *
307 * @map: map to sync.
308 * @min: first register to sync
309 * @max: last register to sync
310 *
311 * Write all non-default register values in the specified region to
312 * the hardware.
313 *
314 * Return a negative value on failure, 0 on success.
315 */
316int regcache_sync_region(struct regmap *map, unsigned int min,
317 unsigned int max)
318{
319 int ret = 0;
320 const char *name;
321 unsigned int bypass;
322
323 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
324
325 mutex_lock(&map->lock);
326
327 /* Remember the initial bypass state */
328 bypass = map->cache_bypass;
329
330 name = map->cache_ops->name;
331 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
332
333 trace_regcache_sync(map->dev, name, "start region");
334
335 if (!map->cache_dirty)
336 goto out;
337
338 ret = map->cache_ops->sync(map, min, max);
339
340out:
341 trace_regcache_sync(map->dev, name, "stop region");
342 /* Restore the bypass state */
343 map->cache_bypass = bypass;
344 mutex_unlock(&map->lock);
345
346 return ret;
347}
348
349/**
307 * regcache_cache_only: Put a register map into cache only mode 350 * regcache_cache_only: Put a register map into cache only mode
308 * 351 *
309 * @map: map to configure 352 * @map: map to configure
@@ -320,6 +363,7 @@ void regcache_cache_only(struct regmap *map, bool enable)
320 mutex_lock(&map->lock); 363 mutex_lock(&map->lock);
321 WARN_ON(map->cache_bypass && enable); 364 WARN_ON(map->cache_bypass && enable);
322 map->cache_only = enable; 365 map->cache_only = enable;
366 trace_regmap_cache_only(map->dev, enable);
323 mutex_unlock(&map->lock); 367 mutex_unlock(&map->lock);
324} 368}
325EXPORT_SYMBOL_GPL(regcache_cache_only); 369EXPORT_SYMBOL_GPL(regcache_cache_only);
@@ -357,6 +401,7 @@ void regcache_cache_bypass(struct regmap *map, bool enable)
357 mutex_lock(&map->lock); 401 mutex_lock(&map->lock);
358 WARN_ON(map->cache_only && enable); 402 WARN_ON(map->cache_only && enable);
359 map->cache_bypass = enable; 403 map->cache_bypass = enable;
404 trace_regmap_cache_bypass(map->dev, enable);
360 mutex_unlock(&map->lock); 405 mutex_unlock(&map->lock);
361} 406}
362EXPORT_SYMBOL_GPL(regcache_cache_bypass); 407EXPORT_SYMBOL_GPL(regcache_cache_bypass);
@@ -379,10 +424,16 @@ bool regcache_set_val(void *base, unsigned int idx,
379 cache[idx] = val; 424 cache[idx] = val;
380 break; 425 break;
381 } 426 }
427 case 4: {
428 u32 *cache = base;
429 if (cache[idx] == val)
430 return true;
431 cache[idx] = val;
432 break;
433 }
382 default: 434 default:
383 BUG(); 435 BUG();
384 } 436 }
385 /* unreachable */
386 return false; 437 return false;
387} 438}
388 439
@@ -401,6 +452,10 @@ unsigned int regcache_get_val(const void *base, unsigned int idx,
401 const u16 *cache = base; 452 const u16 *cache = base;
402 return cache[idx]; 453 return cache[idx];
403 } 454 }
455 case 4: {
456 const u32 *cache = base;
457 return cache[idx];
458 }
404 default: 459 default:
405 BUG(); 460 BUG();
406 } 461 }
diff --git a/drivers/base/regmap/regmap-debugfs.c b/drivers/base/regmap/regmap-debugfs.c
index 6f397476e27c..372f81a21201 100644
--- a/drivers/base/regmap/regmap-debugfs.c
+++ b/drivers/base/regmap/regmap-debugfs.c
@@ -11,7 +11,6 @@
11 */ 11 */
12 12
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/mutex.h> 14#include <linux/mutex.h>
16#include <linux/debugfs.h> 15#include <linux/debugfs.h>
17#include <linux/uaccess.h> 16#include <linux/uaccess.h>
@@ -33,6 +32,35 @@ static int regmap_open_file(struct inode *inode, struct file *file)
33 return 0; 32 return 0;
34} 33}
35 34
35static ssize_t regmap_name_read_file(struct file *file,
36 char __user *user_buf, size_t count,
37 loff_t *ppos)
38{
39 struct regmap *map = file->private_data;
40 int ret;
41 char *buf;
42
43 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
44 if (!buf)
45 return -ENOMEM;
46
47 ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name);
48 if (ret < 0) {
49 kfree(buf);
50 return ret;
51 }
52
53 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
54 kfree(buf);
55 return ret;
56}
57
58static const struct file_operations regmap_name_fops = {
59 .open = regmap_open_file,
60 .read = regmap_name_read_file,
61 .llseek = default_llseek,
62};
63
36static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf, 64static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
37 size_t count, loff_t *ppos) 65 size_t count, loff_t *ppos)
38{ 66{
@@ -103,9 +131,51 @@ out:
103 return ret; 131 return ret;
104} 132}
105 133
134#undef REGMAP_ALLOW_WRITE_DEBUGFS
135#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
136/*
137 * This can be dangerous especially when we have clients such as
138 * PMICs, therefore don't provide any real compile time configuration option
139 * for this feature, people who want to use this will need to modify
140 * the source code directly.
141 */
142static ssize_t regmap_map_write_file(struct file *file,
143 const char __user *user_buf,
144 size_t count, loff_t *ppos)
145{
146 char buf[32];
147 size_t buf_size;
148 char *start = buf;
149 unsigned long reg, value;
150 struct regmap *map = file->private_data;
151
152 buf_size = min(count, (sizeof(buf)-1));
153 if (copy_from_user(buf, user_buf, buf_size))
154 return -EFAULT;
155 buf[buf_size] = 0;
156
157 while (*start == ' ')
158 start++;
159 reg = simple_strtoul(start, &start, 16);
160 while (*start == ' ')
161 start++;
162 if (strict_strtoul(start, 16, &value))
163 return -EINVAL;
164
165 /* Userspace has been fiddling around behind the kernel's back */
166 add_taint(TAINT_USER);
167
168 regmap_write(map, reg, value);
169 return buf_size;
170}
171#else
172#define regmap_map_write_file NULL
173#endif
174
106static const struct file_operations regmap_map_fops = { 175static const struct file_operations regmap_map_fops = {
107 .open = regmap_open_file, 176 .open = regmap_open_file,
108 .read = regmap_map_read_file, 177 .read = regmap_map_read_file,
178 .write = regmap_map_write_file,
109 .llseek = default_llseek, 179 .llseek = default_llseek,
110}; 180};
111 181
@@ -186,12 +256,24 @@ void regmap_debugfs_init(struct regmap *map)
186 return; 256 return;
187 } 257 }
188 258
259 debugfs_create_file("name", 0400, map->debugfs,
260 map, &regmap_name_fops);
261
189 if (map->max_register) { 262 if (map->max_register) {
190 debugfs_create_file("registers", 0400, map->debugfs, 263 debugfs_create_file("registers", 0400, map->debugfs,
191 map, &regmap_map_fops); 264 map, &regmap_map_fops);
192 debugfs_create_file("access", 0400, map->debugfs, 265 debugfs_create_file("access", 0400, map->debugfs,
193 map, &regmap_access_fops); 266 map, &regmap_access_fops);
194 } 267 }
268
269 if (map->cache_type) {
270 debugfs_create_bool("cache_only", 0400, map->debugfs,
271 &map->cache_only);
272 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
273 &map->cache_dirty);
274 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
275 &map->cache_bypass);
276 }
195} 277}
196 278
197void regmap_debugfs_exit(struct regmap *map) 279void regmap_debugfs_exit(struct regmap *map)
diff --git a/drivers/base/regmap/regmap-i2c.c b/drivers/base/regmap/regmap-i2c.c
index 38621ec87c05..9a3a8c564389 100644
--- a/drivers/base/regmap/regmap-i2c.c
+++ b/drivers/base/regmap/regmap-i2c.c
@@ -111,4 +111,21 @@ struct regmap *regmap_init_i2c(struct i2c_client *i2c,
111} 111}
112EXPORT_SYMBOL_GPL(regmap_init_i2c); 112EXPORT_SYMBOL_GPL(regmap_init_i2c);
113 113
114/**
115 * devm_regmap_init_i2c(): Initialise managed register map
116 *
117 * @i2c: Device that will be interacted with
118 * @config: Configuration for register map
119 *
120 * The return value will be an ERR_PTR() on error or a valid pointer
121 * to a struct regmap. The regmap will be automatically freed by the
122 * device management code.
123 */
124struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
125 const struct regmap_config *config)
126{
127 return devm_regmap_init(&i2c->dev, &regmap_i2c, config);
128}
129EXPORT_SYMBOL_GPL(devm_regmap_init_i2c);
130
114MODULE_LICENSE("GPL"); 131MODULE_LICENSE("GPL");
diff --git a/drivers/base/regmap/regmap-spi.c b/drivers/base/regmap/regmap-spi.c
index 2560658de344..7c0c35a39c33 100644
--- a/drivers/base/regmap/regmap-spi.c
+++ b/drivers/base/regmap/regmap-spi.c
@@ -70,4 +70,21 @@ struct regmap *regmap_init_spi(struct spi_device *spi,
70} 70}
71EXPORT_SYMBOL_GPL(regmap_init_spi); 71EXPORT_SYMBOL_GPL(regmap_init_spi);
72 72
73/**
74 * devm_regmap_init_spi(): Initialise register map
75 *
76 * @spi: Device that will be interacted with
77 * @config: Configuration for register map
78 *
79 * The return value will be an ERR_PTR() on error or a valid pointer
80 * to a struct regmap. The map will be automatically freed by the
81 * device management code.
82 */
83struct regmap *devm_regmap_init_spi(struct spi_device *spi,
84 const struct regmap_config *config)
85{
86 return devm_regmap_init(&spi->dev, &regmap_spi, config);
87}
88EXPORT_SYMBOL_GPL(devm_regmap_init_spi);
89
73MODULE_LICENSE("GPL"); 90MODULE_LICENSE("GPL");
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 2366b6299f07..7a3f535e481c 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;
@@ -258,6 +306,45 @@ err:
258} 306}
259EXPORT_SYMBOL_GPL(regmap_init); 307EXPORT_SYMBOL_GPL(regmap_init);
260 308
309static void devm_regmap_release(struct device *dev, void *res)
310{
311 regmap_exit(*(struct regmap **)res);
312}
313
314/**
315 * devm_regmap_init(): Initialise managed register map
316 *
317 * @dev: Device that will be interacted with
318 * @bus: Bus-specific callbacks to use with device
319 * @config: Configuration for register map
320 *
321 * The return value will be an ERR_PTR() on error or a valid pointer
322 * to a struct regmap. This function should generally not be called
323 * directly, it should be called by bus-specific init functions. The
324 * map will be automatically freed by the device management code.
325 */
326struct regmap *devm_regmap_init(struct device *dev,
327 const struct regmap_bus *bus,
328 const struct regmap_config *config)
329{
330 struct regmap **ptr, *regmap;
331
332 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
333 if (!ptr)
334 return ERR_PTR(-ENOMEM);
335
336 regmap = regmap_init(dev, bus, config);
337 if (!IS_ERR(regmap)) {
338 *ptr = regmap;
339 devres_add(dev, ptr);
340 } else {
341 devres_free(ptr);
342 }
343
344 return regmap;
345}
346EXPORT_SYMBOL_GPL(devm_regmap_init);
347
261/** 348/**
262 * regmap_reinit_cache(): Reinitialise the current register cache 349 * regmap_reinit_cache(): Reinitialise the current register cache
263 * 350 *
@@ -276,6 +363,7 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
276 mutex_lock(&map->lock); 363 mutex_lock(&map->lock);
277 364
278 regcache_exit(map); 365 regcache_exit(map);
366 regmap_debugfs_exit(map);
279 367
280 map->max_register = config->max_register; 368 map->max_register = config->max_register;
281 map->writeable_reg = config->writeable_reg; 369 map->writeable_reg = config->writeable_reg;
@@ -284,6 +372,8 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
284 map->precious_reg = config->precious_reg; 372 map->precious_reg = config->precious_reg;
285 map->cache_type = config->cache_type; 373 map->cache_type = config->cache_type;
286 374
375 regmap_debugfs_init(map);
376
287 map->cache_bypass = false; 377 map->cache_bypass = false;
288 map->cache_only = false; 378 map->cache_only = false;
289 379
@@ -352,23 +442,28 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
352 * send the work_buf directly, otherwise try to do a gather 442 * send the work_buf directly, otherwise try to do a gather
353 * write. 443 * write.
354 */ 444 */
355 if (val == map->work_buf + map->format.reg_bytes) 445 if (val == (map->work_buf + map->format.pad_bytes +
446 map->format.reg_bytes))
356 ret = map->bus->write(map->dev, map->work_buf, 447 ret = map->bus->write(map->dev, map->work_buf,
357 map->format.reg_bytes + val_len); 448 map->format.reg_bytes +
449 map->format.pad_bytes +
450 val_len);
358 else if (map->bus->gather_write) 451 else if (map->bus->gather_write)
359 ret = map->bus->gather_write(map->dev, map->work_buf, 452 ret = map->bus->gather_write(map->dev, map->work_buf,
360 map->format.reg_bytes, 453 map->format.reg_bytes +
454 map->format.pad_bytes,
361 val, val_len); 455 val, val_len);
362 456
363 /* If that didn't work fall back on linearising by hand. */ 457 /* If that didn't work fall back on linearising by hand. */
364 if (ret == -ENOTSUPP) { 458 if (ret == -ENOTSUPP) {
365 len = map->format.reg_bytes + val_len; 459 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
366 buf = kmalloc(len, GFP_KERNEL); 460 buf = kzalloc(len, GFP_KERNEL);
367 if (!buf) 461 if (!buf)
368 return -ENOMEM; 462 return -ENOMEM;
369 463
370 memcpy(buf, map->work_buf, map->format.reg_bytes); 464 memcpy(buf, map->work_buf, map->format.reg_bytes);
371 memcpy(buf + map->format.reg_bytes, val, val_len); 465 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
466 val, val_len);
372 ret = map->bus->write(map->dev, buf, len); 467 ret = map->bus->write(map->dev, buf, len);
373 468
374 kfree(buf); 469 kfree(buf);
@@ -410,10 +505,12 @@ int _regmap_write(struct regmap *map, unsigned int reg,
410 505
411 return ret; 506 return ret;
412 } else { 507 } else {
413 map->format.format_val(map->work_buf + map->format.reg_bytes, 508 map->format.format_val(map->work_buf + map->format.reg_bytes
414 val); 509 + map->format.pad_bytes, val);
415 return _regmap_raw_write(map, reg, 510 return _regmap_raw_write(map, reg,
416 map->work_buf + map->format.reg_bytes, 511 map->work_buf +
512 map->format.reg_bytes +
513 map->format.pad_bytes,
417 map->format.val_bytes); 514 map->format.val_bytes);
418 } 515 }
419} 516}
@@ -542,7 +639,8 @@ static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
542 trace_regmap_hw_read_start(map->dev, reg, 639 trace_regmap_hw_read_start(map->dev, reg,
543 val_len / map->format.val_bytes); 640 val_len / map->format.val_bytes);
544 641
545 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes, 642 ret = map->bus->read(map->dev, map->work_buf,
643 map->format.reg_bytes + map->format.pad_bytes,
546 val, val_len); 644 val, val_len);
547 645
548 trace_regmap_hw_read_done(map->dev, reg, 646 trace_regmap_hw_read_done(map->dev, reg,
@@ -615,16 +713,32 @@ EXPORT_SYMBOL_GPL(regmap_read);
615int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 713int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
616 size_t val_len) 714 size_t val_len)
617{ 715{
618 size_t val_count = val_len / map->format.val_bytes; 716 size_t val_bytes = map->format.val_bytes;
619 int ret; 717 size_t val_count = val_len / val_bytes;
620 718 unsigned int v;
621 WARN_ON(!regmap_volatile_range(map, reg, val_count) && 719 int ret, i;
622 map->cache_type != REGCACHE_NONE);
623 720
624 mutex_lock(&map->lock); 721 mutex_lock(&map->lock);
625 722
626 ret = _regmap_raw_read(map, reg, val, val_len); 723 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
724 map->cache_type == REGCACHE_NONE) {
725 /* Physical block read if there's no cache involved */
726 ret = _regmap_raw_read(map, reg, val, val_len);
727
728 } else {
729 /* Otherwise go word by word for the cache; should be low
730 * cost as we expect to hit the cache.
731 */
732 for (i = 0; i < val_count; i++) {
733 ret = _regmap_read(map, reg + i, &v);
734 if (ret != 0)
735 goto out;
736
737 map->format.format_val(val + (i * val_bytes), v);
738 }
739 }
627 740
741 out:
628 mutex_unlock(&map->lock); 742 mutex_unlock(&map->lock);
629 743
630 return ret; 744 return ret;
@@ -738,6 +852,79 @@ int regmap_update_bits_check(struct regmap *map, unsigned int reg,
738} 852}
739EXPORT_SYMBOL_GPL(regmap_update_bits_check); 853EXPORT_SYMBOL_GPL(regmap_update_bits_check);
740 854
855/**
856 * regmap_register_patch: Register and apply register updates to be applied
857 * on device initialistion
858 *
859 * @map: Register map to apply updates to.
860 * @regs: Values to update.
861 * @num_regs: Number of entries in regs.
862 *
863 * Register a set of register updates to be applied to the device
864 * whenever the device registers are synchronised with the cache and
865 * apply them immediately. Typically this is used to apply
866 * corrections to be applied to the device defaults on startup, such
867 * as the updates some vendors provide to undocumented registers.
868 */
869int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
870 int num_regs)
871{
872 int i, ret;
873 bool bypass;
874
875 /* If needed the implementation can be extended to support this */
876 if (map->patch)
877 return -EBUSY;
878
879 mutex_lock(&map->lock);
880
881 bypass = map->cache_bypass;
882
883 map->cache_bypass = true;
884
885 /* Write out first; it's useful to apply even if we fail later. */
886 for (i = 0; i < num_regs; i++) {
887 ret = _regmap_write(map, regs[i].reg, regs[i].def);
888 if (ret != 0) {
889 dev_err(map->dev, "Failed to write %x = %x: %d\n",
890 regs[i].reg, regs[i].def, ret);
891 goto out;
892 }
893 }
894
895 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
896 if (map->patch != NULL) {
897 memcpy(map->patch, regs,
898 num_regs * sizeof(struct reg_default));
899 map->patch_regs = num_regs;
900 } else {
901 ret = -ENOMEM;
902 }
903
904out:
905 map->cache_bypass = bypass;
906
907 mutex_unlock(&map->lock);
908
909 return ret;
910}
911EXPORT_SYMBOL_GPL(regmap_register_patch);
912
913/*
914 * regmap_get_val_bytes(): Report the size of a register value
915 *
916 * Report the size of a register value, mainly intended to for use by
917 * generic infrastructure built on top of regmap.
918 */
919int regmap_get_val_bytes(struct regmap *map)
920{
921 if (map->format.format_write)
922 return -EINVAL;
923
924 return map->format.val_bytes;
925}
926EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
927
741static int __init regmap_initcall(void) 928static int __init regmap_initcall(void)
742{ 929{
743 regmap_debugfs_initcall(); 930 regmap_debugfs_initcall();