regmap: Devices using format_write don't support bulk operations
[deliverable/linux.git] / drivers / base / regmap / regmap.c
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
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
18
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/regmap.h>
21
22 #include "internal.h"
23
24 bool 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
35 bool regmap_readable(struct regmap *map, unsigned int reg)
36 {
37 if (map->max_register && reg > map->max_register)
38 return false;
39
40 if (map->format.format_write)
41 return false;
42
43 if (map->readable_reg)
44 return map->readable_reg(map->dev, reg);
45
46 return true;
47 }
48
49 bool regmap_volatile(struct regmap *map, unsigned int reg)
50 {
51 if (!regmap_readable(map, reg))
52 return false;
53
54 if (map->volatile_reg)
55 return map->volatile_reg(map->dev, reg);
56
57 return true;
58 }
59
60 bool regmap_precious(struct regmap *map, unsigned int reg)
61 {
62 if (!regmap_readable(map, reg))
63 return false;
64
65 if (map->precious_reg)
66 return map->precious_reg(map->dev, reg);
67
68 return false;
69 }
70
71 static 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
83 static 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
91 static 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
98 static 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
105 static 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
115 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
116 {
117 u8 *b = buf;
118
119 b[0] = val << shift;
120 }
121
122 static void regmap_format_16(void *buf, unsigned int val, unsigned int shift)
123 {
124 __be16 *b = buf;
125
126 b[0] = cpu_to_be16(val << shift);
127 }
128
129 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
130 {
131 u8 *b = buf;
132
133 val <<= shift;
134
135 b[0] = val >> 16;
136 b[1] = val >> 8;
137 b[2] = val;
138 }
139
140 static void regmap_format_32(void *buf, unsigned int val, unsigned int shift)
141 {
142 __be32 *b = buf;
143
144 b[0] = cpu_to_be32(val << shift);
145 }
146
147 static unsigned int regmap_parse_8(void *buf)
148 {
149 u8 *b = buf;
150
151 return b[0];
152 }
153
154 static 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
163 static 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
173 static 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
182 static void regmap_lock_mutex(struct regmap *map)
183 {
184 mutex_lock(&map->mutex);
185 }
186
187 static void regmap_unlock_mutex(struct regmap *map)
188 {
189 mutex_unlock(&map->mutex);
190 }
191
192 static void regmap_lock_spinlock(struct regmap *map)
193 {
194 spin_lock(&map->spinlock);
195 }
196
197 static void regmap_unlock_spinlock(struct regmap *map)
198 {
199 spin_unlock(&map->spinlock);
200 }
201
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
207 * @bus_context: Data passed to bus-specific callbacks
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 */
214 struct regmap *regmap_init(struct device *dev,
215 const struct regmap_bus *bus,
216 void *bus_context,
217 const struct regmap_config *config)
218 {
219 struct regmap *map;
220 int ret = -EINVAL;
221
222 if (!bus || !config)
223 goto err;
224
225 map = kzalloc(sizeof(*map), GFP_KERNEL);
226 if (map == NULL) {
227 ret = -ENOMEM;
228 goto err;
229 }
230
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 }
240 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
241 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
242 map->format.pad_bytes = config->pad_bits / 8;
243 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
244 map->format.buf_size += map->format.pad_bytes;
245 map->reg_shift = config->pad_bits % 8;
246 if (config->reg_stride)
247 map->reg_stride = config->reg_stride;
248 else
249 map->reg_stride = 1;
250 map->use_single_rw = config->use_single_rw;
251 map->dev = dev;
252 map->bus = bus;
253 map->bus_context = bus_context;
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;
258 map->precious_reg = config->precious_reg;
259 map->cache_type = config->cache_type;
260
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
268 switch (config->reg_bits + map->reg_shift) {
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
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
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
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
317 case 32:
318 map->format.format_reg = regmap_format_32;
319 break;
320
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;
334 case 24:
335 map->format.format_val = regmap_format_24;
336 map->format.parse_val = regmap_parse_24;
337 break;
338 case 32:
339 map->format.format_val = regmap_format_32;
340 map->format.parse_val = regmap_parse_32;
341 break;
342 }
343
344 if (map->format.format_write)
345 map->use_single_rw = true;
346
347 if (!map->format.format_write &&
348 !(map->format.format_reg && map->format.format_val))
349 goto err_map;
350
351 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
352 if (map->work_buf == NULL) {
353 ret = -ENOMEM;
354 goto err_map;
355 }
356
357 regmap_debugfs_init(map, config->name);
358
359 ret = regcache_init(map, config);
360 if (ret < 0)
361 goto err_free_workbuf;
362
363 return map;
364
365 err_free_workbuf:
366 kfree(map->work_buf);
367 err_map:
368 kfree(map);
369 err:
370 return ERR_PTR(ret);
371 }
372 EXPORT_SYMBOL_GPL(regmap_init);
373
374 static 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
384 * @bus_context: Data passed to bus-specific callbacks
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 */
392 struct regmap *devm_regmap_init(struct device *dev,
393 const struct regmap_bus *bus,
394 void *bus_context,
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
403 regmap = regmap_init(dev, bus, bus_context, config);
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 }
413 EXPORT_SYMBOL_GPL(devm_regmap_init);
414
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 */
426 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
427 {
428 int ret;
429
430 map->lock(map);
431
432 regcache_exit(map);
433 regmap_debugfs_exit(map);
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
442 regmap_debugfs_init(map, config->name);
443
444 map->cache_bypass = false;
445 map->cache_only = false;
446
447 ret = regcache_init(map, config);
448
449 map->unlock(map);
450
451 return ret;
452 }
453
454 /**
455 * regmap_exit(): Free a previously allocated register map
456 */
457 void regmap_exit(struct regmap *map)
458 {
459 regcache_exit(map);
460 regmap_debugfs_exit(map);
461 if (map->bus->free_context)
462 map->bus->free_context(map->bus_context);
463 kfree(map->work_buf);
464 kfree(map);
465 }
466 EXPORT_SYMBOL_GPL(regmap_exit);
467
468 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
469 const void *val, size_t val_len)
470 {
471 u8 *u8 = map->work_buf;
472 void *buf;
473 int ret = -ENOTSUPP;
474 size_t len;
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++)
480 if (!map->writeable_reg(map->dev,
481 reg + (i * map->reg_stride)))
482 return -EINVAL;
483
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);
490 ret = regcache_write(map, reg + (i * map->reg_stride),
491 ival);
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
505 map->format.format_reg(map->work_buf, reg, map->reg_shift);
506
507 u8[0] |= map->write_flag_mask;
508
509 trace_regmap_hw_write_start(map->dev, reg,
510 val_len / map->format.val_bytes);
511
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 */
516 if (val == (map->work_buf + map->format.pad_bytes +
517 map->format.reg_bytes))
518 ret = map->bus->write(map->bus_context, map->work_buf,
519 map->format.reg_bytes +
520 map->format.pad_bytes +
521 val_len);
522 else if (map->bus->gather_write)
523 ret = map->bus->gather_write(map->bus_context, map->work_buf,
524 map->format.reg_bytes +
525 map->format.pad_bytes,
526 val, val_len);
527
528 /* If that didn't work fall back on linearising by hand. */
529 if (ret == -ENOTSUPP) {
530 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
531 buf = kzalloc(len, GFP_KERNEL);
532 if (!buf)
533 return -ENOMEM;
534
535 memcpy(buf, map->work_buf, map->format.reg_bytes);
536 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
537 val, val_len);
538 ret = map->bus->write(map->bus_context, buf, len);
539
540 kfree(buf);
541 }
542
543 trace_regmap_hw_write_done(map->dev, reg,
544 val_len / map->format.val_bytes);
545
546 return ret;
547 }
548
549 int _regmap_write(struct regmap *map, unsigned int reg,
550 unsigned int val)
551 {
552 int ret;
553 BUG_ON(!map->format.format_write && !map->format.format_val);
554
555 if (!map->cache_bypass && map->format.format_write) {
556 ret = regcache_write(map, reg, val);
557 if (ret != 0)
558 return ret;
559 if (map->cache_only) {
560 map->cache_dirty = true;
561 return 0;
562 }
563 }
564
565 trace_regmap_reg_write(map->dev, reg, val);
566
567 if (map->format.format_write) {
568 map->format.format_write(map, reg, val);
569
570 trace_regmap_hw_write_start(map->dev, reg, 1);
571
572 ret = map->bus->write(map->bus_context, map->work_buf,
573 map->format.buf_size);
574
575 trace_regmap_hw_write_done(map->dev, reg, 1);
576
577 return ret;
578 } else {
579 map->format.format_val(map->work_buf + map->format.reg_bytes
580 + map->format.pad_bytes, val, 0);
581 return _regmap_raw_write(map, reg,
582 map->work_buf +
583 map->format.reg_bytes +
584 map->format.pad_bytes,
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 */
599 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
600 {
601 int ret;
602
603 if (reg % map->reg_stride)
604 return -EINVAL;
605
606 map->lock(map);
607
608 ret = _regmap_write(map, reg, val);
609
610 map->unlock(map);
611
612 return ret;
613 }
614 EXPORT_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 */
632 int regmap_raw_write(struct regmap *map, unsigned int reg,
633 const void *val, size_t val_len)
634 {
635 int ret;
636
637 if (val_len % map->format.val_bytes)
638 return -EINVAL;
639 if (reg % map->reg_stride)
640 return -EINVAL;
641
642 map->lock(map);
643
644 ret = _regmap_raw_write(map, reg, val, val_len);
645
646 map->unlock(map);
647
648 return ret;
649 }
650 EXPORT_SYMBOL_GPL(regmap_raw_write);
651
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 */
666 int 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;
675 if (reg % map->reg_stride)
676 return -EINVAL;
677
678 map->lock(map);
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 }
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 }
709
710 if (val_bytes != 1)
711 kfree(wval);
712
713 out:
714 map->unlock(map);
715 return ret;
716 }
717 EXPORT_SYMBOL_GPL(regmap_bulk_write);
718
719 static 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
725 map->format.format_reg(map->work_buf, reg, map->reg_shift);
726
727 /*
728 * Some buses or devices flag reads by setting the high bits in the
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 */
733 u8[0] |= map->read_flag_mask;
734
735 trace_regmap_hw_read_start(map->dev, reg,
736 val_len / map->format.val_bytes);
737
738 ret = map->bus->read(map->bus_context, map->work_buf,
739 map->format.reg_bytes + map->format.pad_bytes,
740 val, val_len);
741
742 trace_regmap_hw_read_done(map->dev, reg,
743 val_len / map->format.val_bytes);
744
745 return ret;
746 }
747
748 static int _regmap_read(struct regmap *map, unsigned int reg,
749 unsigned int *val)
750 {
751 int ret;
752
753 if (!map->cache_bypass) {
754 ret = regcache_read(map, reg, val);
755 if (ret == 0)
756 return 0;
757 }
758
759 if (!map->format.parse_val)
760 return -EINVAL;
761
762 if (map->cache_only)
763 return -EBUSY;
764
765 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
766 if (ret == 0) {
767 *val = map->format.parse_val(map->work_buf);
768 trace_regmap_reg_read(map->dev, reg, *val);
769 }
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 */
784 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
785 {
786 int ret;
787
788 if (reg % map->reg_stride)
789 return -EINVAL;
790
791 map->lock(map);
792
793 ret = _regmap_read(map, reg, val);
794
795 map->unlock(map);
796
797 return ret;
798 }
799 EXPORT_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 */
812 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
813 size_t val_len)
814 {
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;
819
820 if (val_len % map->format.val_bytes)
821 return -EINVAL;
822 if (reg % map->reg_stride)
823 return -EINVAL;
824
825 map->lock(map);
826
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++) {
837 ret = _regmap_read(map, reg + (i * map->reg_stride),
838 &v);
839 if (ret != 0)
840 goto out;
841
842 map->format.format_val(val + (i * val_bytes), v, 0);
843 }
844 }
845
846 out:
847 map->unlock(map);
848
849 return ret;
850 }
851 EXPORT_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 */
864 int 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;
869 bool vol = regmap_volatile_range(map, reg, val_count);
870
871 if (!map->format.parse_val)
872 return -EINVAL;
873 if (reg % map->reg_stride)
874 return -EINVAL;
875
876 if (vol || map->cache_type == REGCACHE_NONE) {
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 }
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++) {
901 ret = regmap_read(map, reg + (i * map->reg_stride),
902 val + (i * val_bytes));
903 if (ret != 0)
904 return ret;
905 }
906 }
907
908 return 0;
909 }
910 EXPORT_SYMBOL_GPL(regmap_bulk_read);
911
912 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
913 unsigned int mask, unsigned int val,
914 bool *change)
915 {
916 int ret;
917 unsigned int tmp, orig;
918
919 map->lock(map);
920
921 ret = _regmap_read(map, reg, &orig);
922 if (ret != 0)
923 goto out;
924
925 tmp = orig & ~mask;
926 tmp |= val & mask;
927
928 if (tmp != orig) {
929 ret = _regmap_write(map, reg, tmp);
930 *change = true;
931 } else {
932 *change = false;
933 }
934
935 out:
936 map->unlock(map);
937
938 return ret;
939 }
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 */
951 int 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 }
957 EXPORT_SYMBOL_GPL(regmap_update_bits);
958
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 */
971 int 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 }
977 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
978
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 */
993 int 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
1003 map->lock(map);
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
1019 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
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
1028 out:
1029 map->cache_bypass = bypass;
1030
1031 map->unlock(map);
1032
1033 return ret;
1034 }
1035 EXPORT_SYMBOL_GPL(regmap_register_patch);
1036
1037 /*
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 */
1043 int 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 }
1050 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1051
1052 static int __init regmap_initcall(void)
1053 {
1054 regmap_debugfs_initcall();
1055
1056 return 0;
1057 }
1058 postcore_initcall(regmap_initcall);
This page took 0.08084 seconds and 6 git commands to generate.