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