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