regmap: introduce explicit bus_context for bus callbacks
[linux-block.git] / drivers / base / regmap / regmap.c
CommitLineData
b83a313b
MB
1/*
2 * Register map access API
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
f5d6eba7 13#include <linux/device.h>
b83a313b 14#include <linux/slab.h>
19694b5e 15#include <linux/export.h>
b83a313b
MB
16#include <linux/mutex.h>
17#include <linux/err.h>
18
fb2736bb
MB
19#define CREATE_TRACE_POINTS
20#include <trace/events/regmap.h>
21
93de9124 22#include "internal.h"
b83a313b 23
8de2f081
MB
24bool regmap_writeable(struct regmap *map, unsigned int reg)
25{
26 if (map->max_register && reg > map->max_register)
27 return false;
28
29 if (map->writeable_reg)
30 return map->writeable_reg(map->dev, reg);
31
32 return true;
33}
34
35bool regmap_readable(struct regmap *map, unsigned int reg)
36{
37 if (map->max_register && reg > map->max_register)
38 return false;
39
4191f197
WS
40 if (map->format.format_write)
41 return false;
42
8de2f081
MB
43 if (map->readable_reg)
44 return map->readable_reg(map->dev, reg);
45
46 return true;
47}
48
49bool regmap_volatile(struct regmap *map, unsigned int reg)
50{
4191f197 51 if (!regmap_readable(map, reg))
8de2f081
MB
52 return false;
53
54 if (map->volatile_reg)
55 return map->volatile_reg(map->dev, reg);
56
57 return true;
58}
59
60bool regmap_precious(struct regmap *map, unsigned int reg)
61{
4191f197 62 if (!regmap_readable(map, reg))
8de2f081
MB
63 return false;
64
65 if (map->precious_reg)
66 return map->precious_reg(map->dev, reg);
67
68 return false;
69}
70
82cd9965
LPC
71static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
72 unsigned int num)
73{
74 unsigned int i;
75
76 for (i = 0; i < num; i++)
77 if (!regmap_volatile(map, reg + i))
78 return false;
79
80 return true;
81}
82
9aa50750
WS
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
b83a313b
MB
91static void regmap_format_4_12_write(struct regmap *map,
92 unsigned int reg, unsigned int val)
93{
94 __be16 *out = map->work_buf;
95 *out = cpu_to_be16((reg << 12) | val);
96}
97
98static void regmap_format_7_9_write(struct regmap *map,
99 unsigned int reg, unsigned int val)
100{
101 __be16 *out = map->work_buf;
102 *out = cpu_to_be16((reg << 9) | val);
103}
104
7e5ec63e
LPC
105static void regmap_format_10_14_write(struct regmap *map,
106 unsigned int reg, unsigned int val)
107{
108 u8 *out = map->work_buf;
109
110 out[2] = val;
111 out[1] = (val >> 8) | (reg << 6);
112 out[0] = reg >> 2;
113}
114
b83a313b
MB
115static void regmap_format_8(void *buf, unsigned int val)
116{
117 u8 *b = buf;
118
119 b[0] = val;
120}
121
122static void regmap_format_16(void *buf, unsigned int val)
123{
124 __be16 *b = buf;
125
126 b[0] = cpu_to_be16(val);
127}
128
7d5e525b
MB
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
b83a313b
MB
136static unsigned int regmap_parse_8(void *buf)
137{
138 u8 *b = buf;
139
140 return b[0];
141}
142
143static unsigned int regmap_parse_16(void *buf)
144{
145 __be16 *b = buf;
146
147 b[0] = be16_to_cpu(b[0]);
148
149 return b[0];
150}
151
7d5e525b
MB
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
b83a313b
MB
161/**
162 * regmap_init(): Initialise register map
163 *
164 * @dev: Device that will be interacted with
165 * @bus: Bus-specific callbacks to use with device
0135bbcc 166 * @bus_context: Data passed to bus-specific callbacks
b83a313b
MB
167 * @config: Configuration for register map
168 *
169 * The return value will be an ERR_PTR() on error or a valid pointer to
170 * a struct regmap. This function should generally not be called
171 * directly, it should be called by bus-specific init functions.
172 */
173struct regmap *regmap_init(struct device *dev,
174 const struct regmap_bus *bus,
0135bbcc 175 void *bus_context,
b83a313b
MB
176 const struct regmap_config *config)
177{
178 struct regmap *map;
179 int ret = -EINVAL;
180
181 if (!bus || !config)
abbb18fb 182 goto err;
b83a313b
MB
183
184 map = kzalloc(sizeof(*map), GFP_KERNEL);
185 if (map == NULL) {
186 ret = -ENOMEM;
187 goto err;
188 }
189
190 mutex_init(&map->lock);
191 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
c212accc 192 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
82159ba8 193 map->format.pad_bytes = config->pad_bits / 8;
c212accc 194 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
82159ba8 195 map->format.buf_size += map->format.pad_bytes;
b83a313b
MB
196 map->dev = dev;
197 map->bus = bus;
0135bbcc 198 map->bus_context = bus_context;
2e2ae66d
MB
199 map->max_register = config->max_register;
200 map->writeable_reg = config->writeable_reg;
201 map->readable_reg = config->readable_reg;
202 map->volatile_reg = config->volatile_reg;
2efe1642 203 map->precious_reg = config->precious_reg;
5d1729e7 204 map->cache_type = config->cache_type;
b83a313b 205
6f306441
LPC
206 if (config->read_flag_mask || config->write_flag_mask) {
207 map->read_flag_mask = config->read_flag_mask;
208 map->write_flag_mask = config->write_flag_mask;
209 } else {
210 map->read_flag_mask = bus->read_flag_mask;
211 }
212
b83a313b 213 switch (config->reg_bits) {
9aa50750
WS
214 case 2:
215 switch (config->val_bits) {
216 case 6:
217 map->format.format_write = regmap_format_2_6_write;
218 break;
219 default:
220 goto err_map;
221 }
222 break;
223
b83a313b
MB
224 case 4:
225 switch (config->val_bits) {
226 case 12:
227 map->format.format_write = regmap_format_4_12_write;
228 break;
229 default:
230 goto err_map;
231 }
232 break;
233
234 case 7:
235 switch (config->val_bits) {
236 case 9:
237 map->format.format_write = regmap_format_7_9_write;
238 break;
239 default:
240 goto err_map;
241 }
242 break;
243
7e5ec63e
LPC
244 case 10:
245 switch (config->val_bits) {
246 case 14:
247 map->format.format_write = regmap_format_10_14_write;
248 break;
249 default:
250 goto err_map;
251 }
252 break;
253
b83a313b
MB
254 case 8:
255 map->format.format_reg = regmap_format_8;
256 break;
257
258 case 16:
259 map->format.format_reg = regmap_format_16;
260 break;
261
7d5e525b
MB
262 case 32:
263 map->format.format_reg = regmap_format_32;
264 break;
265
b83a313b
MB
266 default:
267 goto err_map;
268 }
269
270 switch (config->val_bits) {
271 case 8:
272 map->format.format_val = regmap_format_8;
273 map->format.parse_val = regmap_parse_8;
274 break;
275 case 16:
276 map->format.format_val = regmap_format_16;
277 map->format.parse_val = regmap_parse_16;
278 break;
7d5e525b
MB
279 case 32:
280 map->format.format_val = regmap_format_32;
281 map->format.parse_val = regmap_parse_32;
282 break;
b83a313b
MB
283 }
284
285 if (!map->format.format_write &&
286 !(map->format.format_reg && map->format.format_val))
287 goto err_map;
288
82159ba8 289 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
b83a313b
MB
290 if (map->work_buf == NULL) {
291 ret = -ENOMEM;
5204f5e3 292 goto err_map;
b83a313b
MB
293 }
294
052d2cd1
MB
295 regmap_debugfs_init(map);
296
e5e3b8ab 297 ret = regcache_init(map, config);
5d1729e7 298 if (ret < 0)
58072cbf 299 goto err_free_workbuf;
5d1729e7 300
b83a313b
MB
301 return map;
302
58072cbf
LPC
303err_free_workbuf:
304 kfree(map->work_buf);
b83a313b
MB
305err_map:
306 kfree(map);
307err:
308 return ERR_PTR(ret);
309}
310EXPORT_SYMBOL_GPL(regmap_init);
311
c0eb4676
MB
312static void devm_regmap_release(struct device *dev, void *res)
313{
314 regmap_exit(*(struct regmap **)res);
315}
316
317/**
318 * devm_regmap_init(): Initialise managed register map
319 *
320 * @dev: Device that will be interacted with
321 * @bus: Bus-specific callbacks to use with device
0135bbcc 322 * @bus_context: Data passed to bus-specific callbacks
c0eb4676
MB
323 * @config: Configuration for register map
324 *
325 * The return value will be an ERR_PTR() on error or a valid pointer
326 * to a struct regmap. This function should generally not be called
327 * directly, it should be called by bus-specific init functions. The
328 * map will be automatically freed by the device management code.
329 */
330struct regmap *devm_regmap_init(struct device *dev,
331 const struct regmap_bus *bus,
0135bbcc 332 void *bus_context,
c0eb4676
MB
333 const struct regmap_config *config)
334{
335 struct regmap **ptr, *regmap;
336
337 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
338 if (!ptr)
339 return ERR_PTR(-ENOMEM);
340
0135bbcc 341 regmap = regmap_init(dev, bus, bus_context, config);
c0eb4676
MB
342 if (!IS_ERR(regmap)) {
343 *ptr = regmap;
344 devres_add(dev, ptr);
345 } else {
346 devres_free(ptr);
347 }
348
349 return regmap;
350}
351EXPORT_SYMBOL_GPL(devm_regmap_init);
352
bf315173
MB
353/**
354 * regmap_reinit_cache(): Reinitialise the current register cache
355 *
356 * @map: Register map to operate on.
357 * @config: New configuration. Only the cache data will be used.
358 *
359 * Discard any existing register cache for the map and initialize a
360 * new cache. This can be used to restore the cache to defaults or to
361 * update the cache configuration to reflect runtime discovery of the
362 * hardware.
363 */
364int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
365{
366 int ret;
367
368 mutex_lock(&map->lock);
369
370 regcache_exit(map);
a24f64a6 371 regmap_debugfs_exit(map);
bf315173
MB
372
373 map->max_register = config->max_register;
374 map->writeable_reg = config->writeable_reg;
375 map->readable_reg = config->readable_reg;
376 map->volatile_reg = config->volatile_reg;
377 map->precious_reg = config->precious_reg;
378 map->cache_type = config->cache_type;
379
a24f64a6
MB
380 regmap_debugfs_init(map);
381
421e8d2d
MB
382 map->cache_bypass = false;
383 map->cache_only = false;
384
bf315173
MB
385 ret = regcache_init(map, config);
386
387 mutex_unlock(&map->lock);
388
389 return ret;
390}
391
b83a313b
MB
392/**
393 * regmap_exit(): Free a previously allocated register map
394 */
395void regmap_exit(struct regmap *map)
396{
5d1729e7 397 regcache_exit(map);
31244e39 398 regmap_debugfs_exit(map);
0135bbcc
SW
399 if (map->bus->free_context)
400 map->bus->free_context(map->bus_context);
b83a313b 401 kfree(map->work_buf);
b83a313b
MB
402 kfree(map);
403}
404EXPORT_SYMBOL_GPL(regmap_exit);
405
406static int _regmap_raw_write(struct regmap *map, unsigned int reg,
407 const void *val, size_t val_len)
408{
6f306441 409 u8 *u8 = map->work_buf;
b83a313b
MB
410 void *buf;
411 int ret = -ENOTSUPP;
412 size_t len;
73304781
MB
413 int i;
414
415 /* Check for unwritable registers before we start */
416 if (map->writeable_reg)
417 for (i = 0; i < val_len / map->format.val_bytes; i++)
418 if (!map->writeable_reg(map->dev, reg + i))
419 return -EINVAL;
b83a313b 420
c9157198
LD
421 if (!map->cache_bypass && map->format.parse_val) {
422 unsigned int ival;
423 int val_bytes = map->format.val_bytes;
424 for (i = 0; i < val_len / val_bytes; i++) {
425 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
426 ival = map->format.parse_val(map->work_buf);
427 ret = regcache_write(map, reg + i, ival);
428 if (ret) {
429 dev_err(map->dev,
430 "Error in caching of register: %u ret: %d\n",
431 reg + i, ret);
432 return ret;
433 }
434 }
435 if (map->cache_only) {
436 map->cache_dirty = true;
437 return 0;
438 }
439 }
440
b83a313b
MB
441 map->format.format_reg(map->work_buf, reg);
442
6f306441
LPC
443 u8[0] |= map->write_flag_mask;
444
fb2736bb
MB
445 trace_regmap_hw_write_start(map->dev, reg,
446 val_len / map->format.val_bytes);
447
2547e201
MB
448 /* If we're doing a single register write we can probably just
449 * send the work_buf directly, otherwise try to do a gather
450 * write.
451 */
82159ba8
MB
452 if (val == (map->work_buf + map->format.pad_bytes +
453 map->format.reg_bytes))
0135bbcc 454 ret = map->bus->write(map->bus_context, map->work_buf,
82159ba8
MB
455 map->format.reg_bytes +
456 map->format.pad_bytes +
457 val_len);
2547e201 458 else if (map->bus->gather_write)
0135bbcc 459 ret = map->bus->gather_write(map->bus_context, map->work_buf,
82159ba8
MB
460 map->format.reg_bytes +
461 map->format.pad_bytes,
b83a313b
MB
462 val, val_len);
463
2547e201 464 /* If that didn't work fall back on linearising by hand. */
b83a313b 465 if (ret == -ENOTSUPP) {
82159ba8
MB
466 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
467 buf = kzalloc(len, GFP_KERNEL);
b83a313b
MB
468 if (!buf)
469 return -ENOMEM;
470
471 memcpy(buf, map->work_buf, map->format.reg_bytes);
82159ba8
MB
472 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
473 val, val_len);
0135bbcc 474 ret = map->bus->write(map->bus_context, buf, len);
b83a313b
MB
475
476 kfree(buf);
477 }
478
fb2736bb
MB
479 trace_regmap_hw_write_done(map->dev, reg,
480 val_len / map->format.val_bytes);
481
b83a313b
MB
482 return ret;
483}
484
4d2dc095
DP
485int _regmap_write(struct regmap *map, unsigned int reg,
486 unsigned int val)
b83a313b 487{
fb2736bb 488 int ret;
b83a313b
MB
489 BUG_ON(!map->format.format_write && !map->format.format_val);
490
c9157198 491 if (!map->cache_bypass && map->format.format_write) {
5d1729e7
DP
492 ret = regcache_write(map, reg, val);
493 if (ret != 0)
494 return ret;
8ae0d7e8
MB
495 if (map->cache_only) {
496 map->cache_dirty = true;
5d1729e7 497 return 0;
8ae0d7e8 498 }
5d1729e7
DP
499 }
500
fb2736bb
MB
501 trace_regmap_reg_write(map->dev, reg, val);
502
b83a313b
MB
503 if (map->format.format_write) {
504 map->format.format_write(map, reg, val);
505
fb2736bb
MB
506 trace_regmap_hw_write_start(map->dev, reg, 1);
507
0135bbcc 508 ret = map->bus->write(map->bus_context, map->work_buf,
fb2736bb
MB
509 map->format.buf_size);
510
511 trace_regmap_hw_write_done(map->dev, reg, 1);
512
513 return ret;
b83a313b 514 } else {
82159ba8
MB
515 map->format.format_val(map->work_buf + map->format.reg_bytes
516 + map->format.pad_bytes, val);
b83a313b 517 return _regmap_raw_write(map, reg,
82159ba8
MB
518 map->work_buf +
519 map->format.reg_bytes +
520 map->format.pad_bytes,
b83a313b
MB
521 map->format.val_bytes);
522 }
523}
524
525/**
526 * regmap_write(): Write a value to a single register
527 *
528 * @map: Register map to write to
529 * @reg: Register to write to
530 * @val: Value to be written
531 *
532 * A value of zero will be returned on success, a negative errno will
533 * be returned in error cases.
534 */
535int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
536{
537 int ret;
538
539 mutex_lock(&map->lock);
540
541 ret = _regmap_write(map, reg, val);
542
543 mutex_unlock(&map->lock);
544
545 return ret;
546}
547EXPORT_SYMBOL_GPL(regmap_write);
548
549/**
550 * regmap_raw_write(): Write raw values to one or more registers
551 *
552 * @map: Register map to write to
553 * @reg: Initial register to write to
554 * @val: Block of data to be written, laid out for direct transmission to the
555 * device
556 * @val_len: Length of data pointed to by val.
557 *
558 * This function is intended to be used for things like firmware
559 * download where a large block of data needs to be transferred to the
560 * device. No formatting will be done on the data provided.
561 *
562 * A value of zero will be returned on success, a negative errno will
563 * be returned in error cases.
564 */
565int regmap_raw_write(struct regmap *map, unsigned int reg,
566 const void *val, size_t val_len)
567{
568 int ret;
569
570 mutex_lock(&map->lock);
571
572 ret = _regmap_raw_write(map, reg, val, val_len);
573
574 mutex_unlock(&map->lock);
575
576 return ret;
577}
578EXPORT_SYMBOL_GPL(regmap_raw_write);
579
8eaeb219
LD
580/*
581 * regmap_bulk_write(): Write multiple registers to the device
582 *
583 * @map: Register map to write to
584 * @reg: First register to be write from
585 * @val: Block of data to be written, in native register size for device
586 * @val_count: Number of registers to write
587 *
588 * This function is intended to be used for writing a large block of
589 * data to be device either in single transfer or multiple transfer.
590 *
591 * A value of zero will be returned on success, a negative errno will
592 * be returned in error cases.
593 */
594int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
595 size_t val_count)
596{
597 int ret = 0, i;
598 size_t val_bytes = map->format.val_bytes;
599 void *wval;
600
601 if (!map->format.parse_val)
602 return -EINVAL;
603
604 mutex_lock(&map->lock);
605
606 /* No formatting is require if val_byte is 1 */
607 if (val_bytes == 1) {
608 wval = (void *)val;
609 } else {
610 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
611 if (!wval) {
612 ret = -ENOMEM;
613 dev_err(map->dev, "Error in memory allocation\n");
614 goto out;
615 }
616 for (i = 0; i < val_count * val_bytes; i += val_bytes)
617 map->format.parse_val(wval + i);
618 }
619 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
620
621 if (val_bytes != 1)
622 kfree(wval);
623
624out:
625 mutex_unlock(&map->lock);
626 return ret;
627}
628EXPORT_SYMBOL_GPL(regmap_bulk_write);
629
b83a313b
MB
630static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
631 unsigned int val_len)
632{
633 u8 *u8 = map->work_buf;
634 int ret;
635
636 map->format.format_reg(map->work_buf, reg);
637
638 /*
6f306441 639 * Some buses or devices flag reads by setting the high bits in the
b83a313b
MB
640 * register addresss; since it's always the high bits for all
641 * current formats we can do this here rather than in
642 * formatting. This may break if we get interesting formats.
643 */
6f306441 644 u8[0] |= map->read_flag_mask;
b83a313b 645
fb2736bb
MB
646 trace_regmap_hw_read_start(map->dev, reg,
647 val_len / map->format.val_bytes);
648
0135bbcc 649 ret = map->bus->read(map->bus_context, map->work_buf,
82159ba8 650 map->format.reg_bytes + map->format.pad_bytes,
40c5cc26 651 val, val_len);
b83a313b 652
fb2736bb
MB
653 trace_regmap_hw_read_done(map->dev, reg,
654 val_len / map->format.val_bytes);
655
656 return ret;
b83a313b
MB
657}
658
659static int _regmap_read(struct regmap *map, unsigned int reg,
660 unsigned int *val)
661{
662 int ret;
663
5d1729e7
DP
664 if (!map->cache_bypass) {
665 ret = regcache_read(map, reg, val);
666 if (ret == 0)
667 return 0;
668 }
669
19254411
LPC
670 if (!map->format.parse_val)
671 return -EINVAL;
672
5d1729e7
DP
673 if (map->cache_only)
674 return -EBUSY;
675
b83a313b 676 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
fb2736bb 677 if (ret == 0) {
b83a313b 678 *val = map->format.parse_val(map->work_buf);
fb2736bb
MB
679 trace_regmap_reg_read(map->dev, reg, *val);
680 }
b83a313b
MB
681
682 return ret;
683}
684
685/**
686 * regmap_read(): Read a value from a single register
687 *
688 * @map: Register map to write to
689 * @reg: Register to be read from
690 * @val: Pointer to store read value
691 *
692 * A value of zero will be returned on success, a negative errno will
693 * be returned in error cases.
694 */
695int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
696{
697 int ret;
698
699 mutex_lock(&map->lock);
700
701 ret = _regmap_read(map, reg, val);
702
703 mutex_unlock(&map->lock);
704
705 return ret;
706}
707EXPORT_SYMBOL_GPL(regmap_read);
708
709/**
710 * regmap_raw_read(): Read raw data from the device
711 *
712 * @map: Register map to write to
713 * @reg: First register to be read from
714 * @val: Pointer to store read value
715 * @val_len: Size of data to read
716 *
717 * A value of zero will be returned on success, a negative errno will
718 * be returned in error cases.
719 */
720int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
721 size_t val_len)
722{
b8fb5ab1
MB
723 size_t val_bytes = map->format.val_bytes;
724 size_t val_count = val_len / val_bytes;
725 unsigned int v;
726 int ret, i;
04e016ad 727
b83a313b
MB
728 mutex_lock(&map->lock);
729
b8fb5ab1
MB
730 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
731 map->cache_type == REGCACHE_NONE) {
732 /* Physical block read if there's no cache involved */
733 ret = _regmap_raw_read(map, reg, val, val_len);
734
735 } else {
736 /* Otherwise go word by word for the cache; should be low
737 * cost as we expect to hit the cache.
738 */
739 for (i = 0; i < val_count; i++) {
740 ret = _regmap_read(map, reg + i, &v);
741 if (ret != 0)
742 goto out;
743
744 map->format.format_val(val + (i * val_bytes), v);
745 }
746 }
b83a313b 747
b8fb5ab1 748 out:
b83a313b
MB
749 mutex_unlock(&map->lock);
750
751 return ret;
752}
753EXPORT_SYMBOL_GPL(regmap_raw_read);
754
755/**
756 * regmap_bulk_read(): Read multiple registers from the device
757 *
758 * @map: Register map to write to
759 * @reg: First register to be read from
760 * @val: Pointer to store read value, in native register size for device
761 * @val_count: Number of registers to read
762 *
763 * A value of zero will be returned on success, a negative errno will
764 * be returned in error cases.
765 */
766int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
767 size_t val_count)
768{
769 int ret, i;
770 size_t val_bytes = map->format.val_bytes;
82cd9965 771 bool vol = regmap_volatile_range(map, reg, val_count);
5d1729e7 772
b83a313b
MB
773 if (!map->format.parse_val)
774 return -EINVAL;
775
de2d808f
MB
776 if (vol || map->cache_type == REGCACHE_NONE) {
777 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
778 if (ret != 0)
779 return ret;
780
781 for (i = 0; i < val_count * val_bytes; i += val_bytes)
782 map->format.parse_val(val + i);
783 } else {
784 for (i = 0; i < val_count; i++) {
785 ret = regmap_read(map, reg + i, val + (i * val_bytes));
786 if (ret != 0)
787 return ret;
788 }
789 }
b83a313b
MB
790
791 return 0;
792}
793EXPORT_SYMBOL_GPL(regmap_bulk_read);
794
018690d3
MB
795static int _regmap_update_bits(struct regmap *map, unsigned int reg,
796 unsigned int mask, unsigned int val,
797 bool *change)
b83a313b
MB
798{
799 int ret;
d91e8db2 800 unsigned int tmp, orig;
b83a313b
MB
801
802 mutex_lock(&map->lock);
803
d91e8db2 804 ret = _regmap_read(map, reg, &orig);
b83a313b
MB
805 if (ret != 0)
806 goto out;
807
d91e8db2 808 tmp = orig & ~mask;
b83a313b
MB
809 tmp |= val & mask;
810
018690d3 811 if (tmp != orig) {
d91e8db2 812 ret = _regmap_write(map, reg, tmp);
018690d3
MB
813 *change = true;
814 } else {
815 *change = false;
816 }
b83a313b
MB
817
818out:
819 mutex_unlock(&map->lock);
820
821 return ret;
822}
018690d3
MB
823
824/**
825 * regmap_update_bits: Perform a read/modify/write cycle on the register map
826 *
827 * @map: Register map to update
828 * @reg: Register to update
829 * @mask: Bitmask to change
830 * @val: New value for bitmask
831 *
832 * Returns zero for success, a negative number on error.
833 */
834int regmap_update_bits(struct regmap *map, unsigned int reg,
835 unsigned int mask, unsigned int val)
836{
837 bool change;
838 return _regmap_update_bits(map, reg, mask, val, &change);
839}
b83a313b 840EXPORT_SYMBOL_GPL(regmap_update_bits);
31244e39 841
018690d3
MB
842/**
843 * regmap_update_bits_check: Perform a read/modify/write cycle on the
844 * register map and report if updated
845 *
846 * @map: Register map to update
847 * @reg: Register to update
848 * @mask: Bitmask to change
849 * @val: New value for bitmask
850 * @change: Boolean indicating if a write was done
851 *
852 * Returns zero for success, a negative number on error.
853 */
854int regmap_update_bits_check(struct regmap *map, unsigned int reg,
855 unsigned int mask, unsigned int val,
856 bool *change)
857{
858 return _regmap_update_bits(map, reg, mask, val, change);
859}
860EXPORT_SYMBOL_GPL(regmap_update_bits_check);
861
22f0d90a
MB
862/**
863 * regmap_register_patch: Register and apply register updates to be applied
864 * on device initialistion
865 *
866 * @map: Register map to apply updates to.
867 * @regs: Values to update.
868 * @num_regs: Number of entries in regs.
869 *
870 * Register a set of register updates to be applied to the device
871 * whenever the device registers are synchronised with the cache and
872 * apply them immediately. Typically this is used to apply
873 * corrections to be applied to the device defaults on startup, such
874 * as the updates some vendors provide to undocumented registers.
875 */
876int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
877 int num_regs)
878{
879 int i, ret;
880 bool bypass;
881
882 /* If needed the implementation can be extended to support this */
883 if (map->patch)
884 return -EBUSY;
885
886 mutex_lock(&map->lock);
887
888 bypass = map->cache_bypass;
889
890 map->cache_bypass = true;
891
892 /* Write out first; it's useful to apply even if we fail later. */
893 for (i = 0; i < num_regs; i++) {
894 ret = _regmap_write(map, regs[i].reg, regs[i].def);
895 if (ret != 0) {
896 dev_err(map->dev, "Failed to write %x = %x: %d\n",
897 regs[i].reg, regs[i].def, ret);
898 goto out;
899 }
900 }
901
2a14d7d9 902 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
22f0d90a
MB
903 if (map->patch != NULL) {
904 memcpy(map->patch, regs,
905 num_regs * sizeof(struct reg_default));
906 map->patch_regs = num_regs;
907 } else {
908 ret = -ENOMEM;
909 }
910
911out:
912 map->cache_bypass = bypass;
913
914 mutex_unlock(&map->lock);
915
916 return ret;
917}
918EXPORT_SYMBOL_GPL(regmap_register_patch);
919
eae4b51b 920/*
a6539c32
MB
921 * regmap_get_val_bytes(): Report the size of a register value
922 *
923 * Report the size of a register value, mainly intended to for use by
924 * generic infrastructure built on top of regmap.
925 */
926int regmap_get_val_bytes(struct regmap *map)
927{
928 if (map->format.format_write)
929 return -EINVAL;
930
931 return map->format.val_bytes;
932}
933EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
934
31244e39
MB
935static int __init regmap_initcall(void)
936{
937 regmap_debugfs_initcall();
938
939 return 0;
940}
941postcore_initcall(regmap_initcall);