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