aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-03-22 23:33:14 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-03-22 23:33:14 -0400
commit9586c959bfc917695893bef0102433a7d0675691 (patch)
treec8b89e40b7a04c3150e50785e7c48b67df360c83 /drivers/base
parent34699403e9916060af8ae23f5e4705a6c078e79d (diff)
parentaddfd8a09e1f434a73b3d87d36ef050c73511d2b (diff)
Merge tag 'regmap-3.4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap
Pull regmap updates from Mark Brown: "Things are really quieting down with the regmap API, while we're still seeing a trickle of new features coming in they're getting much smaller than they were. It's also nice to have some features which support other subsystems building infrastructure on top of regmap. Highlights include: - Support for padding between the register and the value when interacting with the device, sometimes needed for fast interfaces. - Support for applying register updates to the device when restoring the register state. This is intended to be used to apply updates supplied by manufacturers for tuning the performance of the device (many of which are to undocumented registers which aren't otherwise covered). - Support for multi-register operations on cached registers. - Support for syncing only part of the register cache. - Stubs and parameter query functions intended to make it easier for other subsystems to build infrastructure on top of the regmap API. plus a few driver updates making use of the new features which it was easier to merge via this tree." * tag 'regmap-3.4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap: (41 commits) regmap: Fix future missing prototype of devres_alloc() and friends regmap: Rejig struct declarations for stubbed API regmap: Fix rbtree block base in sync regcache: Make sure we sync register 0 in an rbtree cache regmap: delete unused module.h from drivers/base/regmap files regmap: Add stub for regcache_sync_region() mfd: Improve performance of later WM1811 revisions regmap: Fix x86_64 breakage regmap: Allow drivers to sync only part of the register cache regmap: Supply ranges to the sync operations regmap: Add tracepoints for cache only and cache bypass regmap: Mark the cache as clean after a successful sync regmap: Remove default cache sync implementation regmap: Skip hardware defaults for LZO caches regmap: Expose the driver name in debugfs mfd: wm8400: Convert to devm_regmap_init_i2c() mfd: wm831x: Convert to devm_regmap_init() mfd: wm8994: Convert to devm_regmap_init() mfd/ASoC: Convert WM8994 driver to use regmap patches mfd: Add __devinit and __devexit annotations in wm8994 ...
Diffstat (limited to 'drivers/base')
-rw-r--r--drivers/base/regmap/internal.h11
-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.c97
-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.c236
8 files changed, 441 insertions, 64 deletions
diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h
index d141b80479b5..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,16 @@ 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;
78 79
79 struct reg_default *patch; 80 struct reg_default *patch;
80 int patch_regs; 81 int patch_regs;
@@ -87,7 +88,7 @@ struct regcache_ops {
87 int (*exit)(struct regmap *map); 88 int (*exit)(struct regmap *map);
88 int (*read)(struct regmap *map, unsigned int reg, unsigned int *value); 89 int (*read)(struct regmap *map, unsigned int reg, unsigned int *value);
89 int (*write)(struct regmap *map, unsigned int reg, unsigned int value); 90 int (*write)(struct regmap *map, unsigned int reg, unsigned int value);
90 int (*sync)(struct regmap *map); 91 int (*sync)(struct regmap *map, unsigned int min, unsigned int max);
91}; 92};
92 93
93bool 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 5cd2a37e7688..938cb1d2ea26 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -35,12 +35,17 @@ static int regcache_hw_init(struct regmap *map)
35 return -EINVAL; 35 return -EINVAL;
36 36
37 if (!map->reg_defaults_raw) { 37 if (!map->reg_defaults_raw) {
38 u32 cache_bypass = map->cache_bypass;
38 dev_warn(map->dev, "No cache defaults, reading back from HW\n"); 39 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
40
41 /* Bypass the cache access till data read from HW*/
42 map->cache_bypass = 1;
39 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL); 43 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
40 if (!tmp_buf) 44 if (!tmp_buf)
41 return -EINVAL; 45 return -EINVAL;
42 ret = regmap_bulk_read(map, 0, tmp_buf, 46 ret = regmap_bulk_read(map, 0, tmp_buf,
43 map->num_reg_defaults_raw); 47 map->num_reg_defaults_raw);
48 map->cache_bypass = cache_bypass;
44 if (ret < 0) { 49 if (ret < 0) {
45 kfree(tmp_buf); 50 kfree(tmp_buf);
46 return ret; 51 return ret;
@@ -211,7 +216,6 @@ int regcache_read(struct regmap *map,
211 216
212 return -EINVAL; 217 return -EINVAL;
213} 218}
214EXPORT_SYMBOL_GPL(regcache_read);
215 219
216/** 220/**
217 * 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.
@@ -238,7 +242,6 @@ int regcache_write(struct regmap *map,
238 242
239 return 0; 243 return 0;
240} 244}
241EXPORT_SYMBOL_GPL(regcache_write);
242 245
243/** 246/**
244 * regcache_sync: Sync the register cache with the hardware. 247 * regcache_sync: Sync the register cache with the hardware.
@@ -254,12 +257,11 @@ EXPORT_SYMBOL_GPL(regcache_write);
254int regcache_sync(struct regmap *map) 257int regcache_sync(struct regmap *map)
255{ 258{
256 int ret = 0; 259 int ret = 0;
257 unsigned int val;
258 unsigned int i; 260 unsigned int i;
259 const char *name; 261 const char *name;
260 unsigned int bypass; 262 unsigned int bypass;
261 263
262 BUG_ON(!map->cache_ops); 264 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
263 265
264 mutex_lock(&map->lock); 266 mutex_lock(&map->lock);
265 /* Remember the initial bypass state */ 267 /* Remember the initial bypass state */
@@ -269,7 +271,11 @@ int regcache_sync(struct regmap *map)
269 name = map->cache_ops->name; 271 name = map->cache_ops->name;
270 trace_regcache_sync(map->dev, name, "start"); 272 trace_regcache_sync(map->dev, name, "start");
271 273
274 if (!map->cache_dirty)
275 goto out;
276
272 /* Apply any patch first */ 277 /* Apply any patch first */
278 map->cache_bypass = 1;
273 for (i = 0; i < map->patch_regs; i++) { 279 for (i = 0; i < map->patch_regs; i++) {
274 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);
275 if (ret != 0) { 281 if (ret != 0) {
@@ -278,27 +284,13 @@ int regcache_sync(struct regmap *map)
278 goto out; 284 goto out;
279 } 285 }
280 } 286 }
287 map->cache_bypass = 0;
281 288
282 if (!map->cache_dirty) 289 ret = map->cache_ops->sync(map, 0, map->max_register);
283 goto out; 290
284 if (map->cache_ops->sync) { 291 if (ret == 0)
285 ret = map->cache_ops->sync(map); 292 map->cache_dirty = false;
286 } else {
287 for (i = 0; i < map->num_reg_defaults; i++) {
288 ret = regcache_read(map, i, &val);
289 if (ret < 0)
290 goto out;
291 map->cache_bypass = 1;
292 ret = _regmap_write(map, i, val);
293 map->cache_bypass = 0;
294 if (ret < 0)
295 goto out;
296 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
297 map->reg_defaults[i].reg,
298 map->reg_defaults[i].def);
299 }
300 293
301 }
302out: 294out:
303 trace_regcache_sync(map->dev, name, "stop"); 295 trace_regcache_sync(map->dev, name, "stop");
304 /* Restore the bypass state */ 296 /* Restore the bypass state */
@@ -310,6 +302,51 @@ out:
310EXPORT_SYMBOL_GPL(regcache_sync); 302EXPORT_SYMBOL_GPL(regcache_sync);
311 303
312/** 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/**
313 * regcache_cache_only: Put a register map into cache only mode 350 * regcache_cache_only: Put a register map into cache only mode
314 * 351 *
315 * @map: map to configure 352 * @map: map to configure
@@ -326,6 +363,7 @@ void regcache_cache_only(struct regmap *map, bool enable)
326 mutex_lock(&map->lock); 363 mutex_lock(&map->lock);
327 WARN_ON(map->cache_bypass && enable); 364 WARN_ON(map->cache_bypass && enable);
328 map->cache_only = enable; 365 map->cache_only = enable;
366 trace_regmap_cache_only(map->dev, enable);
329 mutex_unlock(&map->lock); 367 mutex_unlock(&map->lock);
330} 368}
331EXPORT_SYMBOL_GPL(regcache_cache_only); 369EXPORT_SYMBOL_GPL(regcache_cache_only);
@@ -363,6 +401,7 @@ void regcache_cache_bypass(struct regmap *map, bool enable)
363 mutex_lock(&map->lock); 401 mutex_lock(&map->lock);
364 WARN_ON(map->cache_only && enable); 402 WARN_ON(map->cache_only && enable);
365 map->cache_bypass = enable; 403 map->cache_bypass = enable;
404 trace_regmap_cache_bypass(map->dev, enable);
366 mutex_unlock(&map->lock); 405 mutex_unlock(&map->lock);
367} 406}
368EXPORT_SYMBOL_GPL(regcache_cache_bypass); 407EXPORT_SYMBOL_GPL(regcache_cache_bypass);
@@ -385,10 +424,16 @@ bool regcache_set_val(void *base, unsigned int idx,
385 cache[idx] = val; 424 cache[idx] = val;
386 break; 425 break;
387 } 426 }
427 case 4: {
428 u32 *cache = base;
429 if (cache[idx] == val)
430 return true;
431 cache[idx] = val;
432 break;
433 }
388 default: 434 default:
389 BUG(); 435 BUG();
390 } 436 }
391 /* unreachable */
392 return false; 437 return false;
393} 438}
394 439
@@ -407,6 +452,10 @@ unsigned int regcache_get_val(const void *base, unsigned int idx,
407 const u16 *cache = base; 452 const u16 *cache = base;
408 return cache[idx]; 453 return cache[idx];
409 } 454 }
455 case 4: {
456 const u32 *cache = base;
457 return cache[idx];
458 }
410 default: 459 default:
411 BUG(); 460 BUG();
412 } 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 7ac234f0b1c5..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
@@ -321,6 +411,26 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
321 if (!map->writeable_reg(map->dev, reg + i)) 411 if (!map->writeable_reg(map->dev, reg + i))
322 return -EINVAL; 412 return -EINVAL;
323 413
414 if (!map->cache_bypass && map->format.parse_val) {
415 unsigned int ival;
416 int val_bytes = map->format.val_bytes;
417 for (i = 0; i < val_len / val_bytes; i++) {
418 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
419 ival = map->format.parse_val(map->work_buf);
420 ret = regcache_write(map, reg + i, ival);
421 if (ret) {
422 dev_err(map->dev,
423 "Error in caching of register: %u ret: %d\n",
424 reg + i, ret);
425 return ret;
426 }
427 }
428 if (map->cache_only) {
429 map->cache_dirty = true;
430 return 0;
431 }
432 }
433
324 map->format.format_reg(map->work_buf, reg); 434 map->format.format_reg(map->work_buf, reg);
325 435
326 u8[0] |= map->write_flag_mask; 436 u8[0] |= map->write_flag_mask;
@@ -332,23 +442,28 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
332 * send the work_buf directly, otherwise try to do a gather 442 * send the work_buf directly, otherwise try to do a gather
333 * write. 443 * write.
334 */ 444 */
335 if (val == map->work_buf + map->format.reg_bytes) 445 if (val == (map->work_buf + map->format.pad_bytes +
446 map->format.reg_bytes))
336 ret = map->bus->write(map->dev, map->work_buf, 447 ret = map->bus->write(map->dev, map->work_buf,
337 map->format.reg_bytes + val_len); 448 map->format.reg_bytes +
449 map->format.pad_bytes +
450 val_len);
338 else if (map->bus->gather_write) 451 else if (map->bus->gather_write)
339 ret = map->bus->gather_write(map->dev, map->work_buf, 452 ret = map->bus->gather_write(map->dev, map->work_buf,
340 map->format.reg_bytes, 453 map->format.reg_bytes +
454 map->format.pad_bytes,
341 val, val_len); 455 val, val_len);
342 456
343 /* If that didn't work fall back on linearising by hand. */ 457 /* If that didn't work fall back on linearising by hand. */
344 if (ret == -ENOTSUPP) { 458 if (ret == -ENOTSUPP) {
345 len = map->format.reg_bytes + val_len; 459 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
346 buf = kmalloc(len, GFP_KERNEL); 460 buf = kzalloc(len, GFP_KERNEL);
347 if (!buf) 461 if (!buf)
348 return -ENOMEM; 462 return -ENOMEM;
349 463
350 memcpy(buf, map->work_buf, map->format.reg_bytes); 464 memcpy(buf, map->work_buf, map->format.reg_bytes);
351 memcpy(buf + map->format.reg_bytes, val, val_len); 465 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
466 val, val_len);
352 ret = map->bus->write(map->dev, buf, len); 467 ret = map->bus->write(map->dev, buf, len);
353 468
354 kfree(buf); 469 kfree(buf);
@@ -366,7 +481,7 @@ int _regmap_write(struct regmap *map, unsigned int reg,
366 int ret; 481 int ret;
367 BUG_ON(!map->format.format_write && !map->format.format_val); 482 BUG_ON(!map->format.format_write && !map->format.format_val);
368 483
369 if (!map->cache_bypass) { 484 if (!map->cache_bypass && map->format.format_write) {
370 ret = regcache_write(map, reg, val); 485 ret = regcache_write(map, reg, val);
371 if (ret != 0) 486 if (ret != 0)
372 return ret; 487 return ret;
@@ -390,10 +505,12 @@ int _regmap_write(struct regmap *map, unsigned int reg,
390 505
391 return ret; 506 return ret;
392 } else { 507 } else {
393 map->format.format_val(map->work_buf + map->format.reg_bytes, 508 map->format.format_val(map->work_buf + map->format.reg_bytes
394 val); 509 + map->format.pad_bytes, val);
395 return _regmap_raw_write(map, reg, 510 return _regmap_raw_write(map, reg,
396 map->work_buf + map->format.reg_bytes, 511 map->work_buf +
512 map->format.reg_bytes +
513 map->format.pad_bytes,
397 map->format.val_bytes); 514 map->format.val_bytes);
398 } 515 }
399} 516}
@@ -441,12 +558,8 @@ EXPORT_SYMBOL_GPL(regmap_write);
441int regmap_raw_write(struct regmap *map, unsigned int reg, 558int regmap_raw_write(struct regmap *map, unsigned int reg,
442 const void *val, size_t val_len) 559 const void *val, size_t val_len)
443{ 560{
444 size_t val_count = val_len / map->format.val_bytes;
445 int ret; 561 int ret;
446 562
447 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
448 map->cache_type != REGCACHE_NONE);
449
450 mutex_lock(&map->lock); 563 mutex_lock(&map->lock);
451 564
452 ret = _regmap_raw_write(map, reg, val, val_len); 565 ret = _regmap_raw_write(map, reg, val, val_len);
@@ -457,6 +570,56 @@ int regmap_raw_write(struct regmap *map, unsigned int reg,
457} 570}
458EXPORT_SYMBOL_GPL(regmap_raw_write); 571EXPORT_SYMBOL_GPL(regmap_raw_write);
459 572
573/*
574 * regmap_bulk_write(): Write multiple registers to the device
575 *
576 * @map: Register map to write to
577 * @reg: First register to be write from
578 * @val: Block of data to be written, in native register size for device
579 * @val_count: Number of registers to write
580 *
581 * This function is intended to be used for writing a large block of
582 * data to be device either in single transfer or multiple transfer.
583 *
584 * A value of zero will be returned on success, a negative errno will
585 * be returned in error cases.
586 */
587int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
588 size_t val_count)
589{
590 int ret = 0, i;
591 size_t val_bytes = map->format.val_bytes;
592 void *wval;
593
594 if (!map->format.parse_val)
595 return -EINVAL;
596
597 mutex_lock(&map->lock);
598
599 /* No formatting is require if val_byte is 1 */
600 if (val_bytes == 1) {
601 wval = (void *)val;
602 } else {
603 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
604 if (!wval) {
605 ret = -ENOMEM;
606 dev_err(map->dev, "Error in memory allocation\n");
607 goto out;
608 }
609 for (i = 0; i < val_count * val_bytes; i += val_bytes)
610 map->format.parse_val(wval + i);
611 }
612 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
613
614 if (val_bytes != 1)
615 kfree(wval);
616
617out:
618 mutex_unlock(&map->lock);
619 return ret;
620}
621EXPORT_SYMBOL_GPL(regmap_bulk_write);
622
460static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 623static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
461 unsigned int val_len) 624 unsigned int val_len)
462{ 625{
@@ -476,7 +639,8 @@ static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
476 trace_regmap_hw_read_start(map->dev, reg, 639 trace_regmap_hw_read_start(map->dev, reg,
477 val_len / map->format.val_bytes); 640 val_len / map->format.val_bytes);
478 641
479 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,
480 val, val_len); 644 val, val_len);
481 645
482 trace_regmap_hw_read_done(map->dev, reg, 646 trace_regmap_hw_read_done(map->dev, reg,
@@ -549,16 +713,32 @@ EXPORT_SYMBOL_GPL(regmap_read);
549int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, 713int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
550 size_t val_len) 714 size_t val_len)
551{ 715{
552 size_t val_count = val_len / map->format.val_bytes; 716 size_t val_bytes = map->format.val_bytes;
553 int ret; 717 size_t val_count = val_len / val_bytes;
554 718 unsigned int v;
555 WARN_ON(!regmap_volatile_range(map, reg, val_count) && 719 int ret, i;
556 map->cache_type != REGCACHE_NONE);
557 720
558 mutex_lock(&map->lock); 721 mutex_lock(&map->lock);
559 722
560 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 }
561 740
741 out:
562 mutex_unlock(&map->lock); 742 mutex_unlock(&map->lock);
563 743
564 return ret; 744 return ret;
@@ -712,7 +892,7 @@ int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
712 } 892 }
713 } 893 }
714 894
715 map->patch = kcalloc(sizeof(struct reg_default), num_regs, GFP_KERNEL); 895 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
716 if (map->patch != NULL) { 896 if (map->patch != NULL) {
717 memcpy(map->patch, regs, 897 memcpy(map->patch, regs,
718 num_regs * sizeof(struct reg_default)); 898 num_regs * sizeof(struct reg_default));