xref: /linux/drivers/base/regmap/regcache.c (revision 85cdaca6970028bf6f544c355c90035586836ddf)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Register cache access API
4 //
5 // Copyright 2011 Wolfson Microelectronics plc
6 //
7 // Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
8 
9 #include <linux/bsearch.h>
10 #include <linux/device.h>
11 #include <linux/export.h>
12 #include <linux/slab.h>
13 #include <linux/sort.h>
14 
15 #include "trace.h"
16 #include "internal.h"
17 
18 static const struct regcache_ops *cache_types[] = {
19 	&regcache_flat_sparse_ops,
20 	&regcache_rbtree_ops,
21 	&regcache_maple_ops,
22 	&regcache_flat_ops,
23 };
24 
25 static int regcache_defaults_cmp(const void *a, const void *b)
26 {
27 	const struct reg_default *x = a;
28 	const struct reg_default *y = b;
29 
30 	if (x->reg > y->reg)
31 		return 1;
32 	else if (x->reg < y->reg)
33 		return -1;
34 	else
35 		return 0;
36 }
37 
38 void regcache_sort_defaults(struct reg_default *defaults, unsigned int ndefaults)
39 {
40 	sort(defaults, ndefaults, sizeof(*defaults),
41 	     regcache_defaults_cmp, NULL);
42 }
43 EXPORT_SYMBOL_GPL(regcache_sort_defaults);
44 
45 static int regcache_count_cacheable_registers(struct regmap *map)
46 {
47 	unsigned int count;
48 
49 	/* calculate the size of reg_defaults */
50 	count = 0;
51 	for (unsigned int i = 0; i < map->num_reg_defaults_raw; i++)
52 		if (regmap_readable(map, i * map->reg_stride) &&
53 		    !regmap_volatile(map, i * map->reg_stride))
54 			count++;
55 
56 	return count;
57 }
58 
59 static int regcache_hw_init(struct regmap *map)
60 {
61 	int ret;
62 	unsigned int reg, val;
63 	void *tmp_buf;
64 
65 	if (!map->reg_defaults_raw) {
66 		bool cache_bypass = map->cache_bypass;
67 		dev_dbg(map->dev, "No cache defaults, reading back from HW\n");
68 
69 		/* Bypass the cache access till data read from HW */
70 		map->cache_bypass = true;
71 		tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
72 		if (!tmp_buf)
73 			return -ENOMEM;
74 		ret = regmap_raw_read(map, 0, tmp_buf,
75 				      map->cache_size_raw);
76 		map->cache_bypass = cache_bypass;
77 		if (ret == 0) {
78 			map->reg_defaults_raw = tmp_buf;
79 			map->cache_free = true;
80 		} else {
81 			kfree(tmp_buf);
82 		}
83 	}
84 
85 	/* fill the reg_defaults */
86 	for (unsigned int i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
87 		reg = i * map->reg_stride;
88 
89 		if (!regmap_readable(map, reg))
90 			continue;
91 
92 		if (regmap_volatile(map, reg))
93 			continue;
94 
95 		if (map->reg_defaults_raw) {
96 			val = regcache_get_val(map, map->reg_defaults_raw, i);
97 		} else {
98 			bool cache_bypass = map->cache_bypass;
99 
100 			map->cache_bypass = true;
101 			ret = regmap_read(map, reg, &val);
102 			map->cache_bypass = cache_bypass;
103 			if (ret != 0) {
104 				dev_err(map->dev, "Failed to read %x: %d\n",
105 					reg, ret);
106 				return ret;
107 			}
108 		}
109 
110 		map->reg_defaults[j].reg = reg;
111 		map->reg_defaults[j].def = val;
112 		j++;
113 	}
114 
115 	return 0;
116 }
117 
118 static void regcache_hw_exit(struct regmap *map)
119 {
120 	if (map->cache_free)
121 		kfree(map->reg_defaults_raw);
122 }
123 
124 int regcache_init(struct regmap *map, const struct regmap_config *config)
125 {
126 	bool sort_defaults = false;
127 	unsigned int reg_prev = 0;
128 	int count = 0;
129 	int ret;
130 	int i;
131 	void *tmp_buf;
132 
133 	if (map->cache_type == REGCACHE_NONE) {
134 		if (config->reg_defaults || config->num_reg_defaults_raw)
135 			dev_warn(map->dev,
136 				 "No cache used with register defaults set!\n");
137 
138 		map->cache_bypass = true;
139 		return 0;
140 	}
141 
142 	if (config->reg_defaults && !config->num_reg_defaults) {
143 		dev_err(map->dev,
144 			 "Register defaults are set without the number!\n");
145 		return -EINVAL;
146 	}
147 
148 	if (config->num_reg_defaults && !config->reg_defaults) {
149 		dev_err(map->dev,
150 			"Register defaults number are set without the reg!\n");
151 		return -EINVAL;
152 	}
153 
154 	for (i = 0; i < config->num_reg_defaults; i++) {
155 		if (config->reg_defaults[i].reg % map->reg_stride)
156 			return -EINVAL;
157 
158 		if (reg_prev > config->reg_defaults[i].reg)
159 			sort_defaults = true;
160 
161 		reg_prev = config->reg_defaults[i].reg;
162 	}
163 
164 	for (i = 0; i < ARRAY_SIZE(cache_types); i++)
165 		if (cache_types[i]->type == map->cache_type)
166 			break;
167 
168 	if (i == ARRAY_SIZE(cache_types)) {
169 		dev_err(map->dev, "Could not match cache type: %d\n",
170 			map->cache_type);
171 		return -EINVAL;
172 	}
173 
174 	map->num_reg_defaults = config->num_reg_defaults;
175 	map->num_reg_defaults_raw = config->num_reg_defaults_raw;
176 	map->reg_defaults_raw = config->reg_defaults_raw;
177 	map->cache_word_size = BITS_TO_BYTES(config->val_bits);
178 	map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
179 
180 	map->cache = NULL;
181 	map->cache_ops = cache_types[i];
182 
183 	if (!map->cache_ops->read ||
184 	    !map->cache_ops->write ||
185 	    !map->cache_ops->name)
186 		return -EINVAL;
187 
188 	/* We still need to ensure that the reg_defaults
189 	 * won't vanish from under us.  We'll need to make
190 	 * a copy of it.
191 	 */
192 	if (config->reg_defaults) {
193 		tmp_buf = kmemdup_array(config->reg_defaults, map->num_reg_defaults,
194 					sizeof(*map->reg_defaults), GFP_KERNEL);
195 		if (!tmp_buf)
196 			return -ENOMEM;
197 
198 		/* regcache_lookup_reg() bsearch()es this array */
199 		if (sort_defaults) {
200 			dev_warn(map->dev,
201 				 "Driver needs fixing: Unsorted reg_defaults, sorting the copy\n");
202 			regcache_sort_defaults(tmp_buf, map->num_reg_defaults);
203 		}
204 		map->reg_defaults = tmp_buf;
205 	} else if (map->num_reg_defaults_raw) {
206 		count = regcache_count_cacheable_registers(map);
207 		if (!count)
208 			map->cache_bypass = true;
209 
210 		/* All registers are unreadable or volatile, so just bypass */
211 		if (map->cache_bypass)
212 			return 0;
213 
214 		map->num_reg_defaults = count;
215 		map->reg_defaults = kmalloc_objs(struct reg_default, count);
216 		if (!map->reg_defaults)
217 			return -ENOMEM;
218 	}
219 
220 	if (!map->max_register_is_set && map->num_reg_defaults_raw) {
221 		map->max_register = (map->num_reg_defaults_raw  - 1) * map->reg_stride;
222 		map->max_register_is_set = true;
223 	}
224 
225 	if (map->cache_ops->init) {
226 		dev_dbg(map->dev, "Initializing %s cache\n",
227 			map->cache_ops->name);
228 		map->lock(map->lock_arg);
229 		ret = map->cache_ops->init(map);
230 		map->unlock(map->lock_arg);
231 		if (ret)
232 			goto err_free_reg_defaults;
233 	}
234 
235 	/*
236 	 * Some devices such as PMICs don't have cache defaults,
237 	 * we cope with this by reading back the HW registers and
238 	 * crafting the cache defaults by hand.
239 	 */
240 	if (count) {
241 		ret = regcache_hw_init(map);
242 		if (ret)
243 			goto err_exit;
244 	}
245 
246 	if (map->cache_ops->populate &&
247 	    (map->num_reg_defaults || map->reg_default_cb)) {
248 		dev_dbg(map->dev, "Populating %s cache\n", map->cache_ops->name);
249 		map->lock(map->lock_arg);
250 		ret = map->cache_ops->populate(map);
251 		map->unlock(map->lock_arg);
252 		if (ret)
253 			goto err_free;
254 	}
255 	return 0;
256 
257 err_free:
258 	regcache_hw_exit(map);
259 err_exit:
260 	if (map->cache_ops->exit) {
261 		dev_dbg(map->dev, "Destroying %s cache\n", map->cache_ops->name);
262 		map->lock(map->lock_arg);
263 		map->cache_ops->exit(map);
264 		map->unlock(map->lock_arg);
265 	}
266 err_free_reg_defaults:
267 	kfree(map->reg_defaults);
268 
269 	return ret;
270 }
271 
272 void regcache_exit(struct regmap *map)
273 {
274 	if (map->cache_type == REGCACHE_NONE)
275 		return;
276 
277 	BUG_ON(!map->cache_ops);
278 
279 	regcache_hw_exit(map);
280 
281 	if (map->cache_ops->exit) {
282 		dev_dbg(map->dev, "Destroying %s cache\n",
283 			map->cache_ops->name);
284 		map->lock(map->lock_arg);
285 		map->cache_ops->exit(map);
286 		map->unlock(map->lock_arg);
287 	}
288 
289 	kfree(map->reg_defaults);
290 }
291 
292 /**
293  * regcache_read - Fetch the value of a given register from the cache.
294  *
295  * @map: map to configure.
296  * @reg: The register index.
297  * @value: The value to be returned.
298  *
299  * Return a negative value on failure, 0 on success.
300  */
301 int regcache_read(struct regmap *map,
302 		  unsigned int reg, unsigned int *value)
303 {
304 	int ret;
305 
306 	if (map->cache_type == REGCACHE_NONE)
307 		return -EINVAL;
308 
309 	BUG_ON(!map->cache_ops);
310 
311 	if (!regmap_volatile(map, reg)) {
312 		ret = map->cache_ops->read(map, reg, value);
313 
314 		if (ret == 0)
315 			trace_regmap_reg_read_cache(map, reg, *value);
316 
317 		return ret;
318 	}
319 
320 	return -EINVAL;
321 }
322 
323 /**
324  * regcache_write - Set the value of a given register in the cache.
325  *
326  * @map: map to configure.
327  * @reg: The register index.
328  * @value: The new register value.
329  *
330  * Return a negative value on failure, 0 on success.
331  */
332 int regcache_write(struct regmap *map,
333 		   unsigned int reg, unsigned int value)
334 {
335 	if (map->cache_type == REGCACHE_NONE)
336 		return 0;
337 
338 	BUG_ON(!map->cache_ops);
339 
340 	if (!regmap_volatile(map, reg))
341 		return map->cache_ops->write(map, reg, value);
342 
343 	return 0;
344 }
345 
346 bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
347 			     unsigned int val)
348 {
349 	int ret;
350 
351 	if (!regmap_writeable(map, reg))
352 		return false;
353 
354 	/* If we don't know the chip just got reset, then sync everything. */
355 	if (!map->no_sync_defaults)
356 		return true;
357 
358 	/* Is this the hardware default?  If so skip. */
359 	ret = regcache_lookup_reg(map, reg);
360 	if (ret >= 0 && val == map->reg_defaults[ret].def)
361 		return false;
362 	return true;
363 }
364 
365 static int regcache_default_sync(struct regmap *map, unsigned int min,
366 				 unsigned int max)
367 {
368 	unsigned int reg;
369 
370 	for (reg = min; reg <= max; reg += map->reg_stride) {
371 		unsigned int val;
372 		int ret;
373 
374 		if (regmap_volatile(map, reg) ||
375 		    !regmap_writeable(map, reg))
376 			continue;
377 
378 		ret = regcache_read(map, reg, &val);
379 		if (ret == -ENOENT)
380 			continue;
381 		if (ret)
382 			return ret;
383 
384 		if (!regcache_reg_needs_sync(map, reg, val))
385 			continue;
386 
387 		map->cache_bypass = true;
388 		ret = _regmap_write(map, reg, val);
389 		map->cache_bypass = false;
390 		if (ret) {
391 			dev_err(map->dev, "Unable to sync register %#x. %d\n",
392 				reg, ret);
393 			return ret;
394 		}
395 		dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
396 	}
397 
398 	return 0;
399 }
400 
401 static int rbtree_all(const void *key, const struct rb_node *node)
402 {
403 	return 0;
404 }
405 
406 /**
407  * regcache_sync - Sync the register cache with the hardware.
408  *
409  * @map: map to configure.
410  *
411  * Any registers that should not be synced should be marked as
412  * volatile.  In general drivers can choose not to use the provided
413  * syncing functionality if they so require.
414  *
415  * Return a negative value on failure, 0 on success.
416  */
417 int regcache_sync(struct regmap *map)
418 {
419 	int ret = 0;
420 	unsigned int i;
421 	const char *name;
422 	bool bypass;
423 	struct rb_node *node;
424 
425 	if (WARN_ON(map->cache_type == REGCACHE_NONE))
426 		return -EINVAL;
427 
428 	BUG_ON(!map->cache_ops);
429 
430 	map->lock(map->lock_arg);
431 	/* Remember the initial bypass state */
432 	bypass = map->cache_bypass;
433 	dev_dbg(map->dev, "Syncing %s cache\n",
434 		map->cache_ops->name);
435 	name = map->cache_ops->name;
436 	trace_regcache_sync(map, name, "start");
437 
438 	if (!map->cache_dirty)
439 		goto out;
440 
441 	/* Apply any patch first */
442 	map->cache_bypass = true;
443 	for (i = 0; i < map->patch_regs; i++) {
444 		ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
445 		if (ret != 0) {
446 			dev_err(map->dev, "Failed to write %x = %x: %d\n",
447 				map->patch[i].reg, map->patch[i].def, ret);
448 			goto out;
449 		}
450 	}
451 	map->cache_bypass = false;
452 
453 	if (map->cache_ops->sync)
454 		ret = map->cache_ops->sync(map, 0, map->max_register);
455 	else
456 		ret = regcache_default_sync(map, 0, map->max_register);
457 
458 	if (ret == 0)
459 		map->cache_dirty = false;
460 
461 out:
462 	/* Restore the bypass state */
463 	map->cache_bypass = bypass;
464 	map->no_sync_defaults = false;
465 
466 	/*
467 	 * If we did any paging with cache bypassed and a cached
468 	 * paging register then the register and cache state might
469 	 * have gone out of sync, force writes of all the paging
470 	 * registers.
471 	 */
472 	rb_for_each(node, NULL, &map->range_tree, rbtree_all) {
473 		struct regmap_range_node *this =
474 			rb_entry(node, struct regmap_range_node, node);
475 
476 		/* If there's nothing in the cache there's nothing to sync */
477 		if (regcache_read(map, this->selector_reg, &i) != 0)
478 			continue;
479 
480 		ret = _regmap_write(map, this->selector_reg, i);
481 		if (ret != 0) {
482 			dev_err(map->dev, "Failed to write %x = %x: %d\n",
483 				this->selector_reg, i, ret);
484 			break;
485 		}
486 	}
487 
488 	map->unlock(map->lock_arg);
489 
490 	regmap_async_complete(map);
491 
492 	trace_regcache_sync(map, name, "stop");
493 
494 	return ret;
495 }
496 EXPORT_SYMBOL_GPL(regcache_sync);
497 
498 /**
499  * regcache_sync_region - Sync part  of the register cache with the hardware.
500  *
501  * @map: map to sync.
502  * @min: first register to sync
503  * @max: last register to sync
504  *
505  * Write all non-default register values in the specified region to
506  * the hardware.
507  *
508  * Return a negative value on failure, 0 on success.
509  */
510 int regcache_sync_region(struct regmap *map, unsigned int min,
511 			 unsigned int max)
512 {
513 	int ret = 0;
514 	const char *name;
515 	bool bypass;
516 
517 	if (WARN_ON(map->cache_type == REGCACHE_NONE))
518 		return -EINVAL;
519 
520 	BUG_ON(!map->cache_ops);
521 
522 	map->lock(map->lock_arg);
523 
524 	/* Remember the initial bypass state */
525 	bypass = map->cache_bypass;
526 
527 	name = map->cache_ops->name;
528 	dev_dbg(map->dev, "Syncing %s cache from %#x-%#x\n", name, min, max);
529 
530 	trace_regcache_sync(map, name, "start region");
531 
532 	if (!map->cache_dirty)
533 		goto out;
534 
535 	map->async = true;
536 
537 	if (map->cache_ops->sync)
538 		ret = map->cache_ops->sync(map, min, max);
539 	else
540 		ret = regcache_default_sync(map, min, max);
541 
542 out:
543 	/* Restore the bypass state */
544 	map->cache_bypass = bypass;
545 	map->async = false;
546 	map->no_sync_defaults = false;
547 	map->unlock(map->lock_arg);
548 
549 	regmap_async_complete(map);
550 
551 	trace_regcache_sync(map, name, "stop region");
552 
553 	return ret;
554 }
555 EXPORT_SYMBOL_GPL(regcache_sync_region);
556 
557 /**
558  * regcache_drop_region - Discard part of the register cache
559  *
560  * @map: map to operate on
561  * @min: first register to discard
562  * @max: last register to discard
563  *
564  * Discard part of the register cache.
565  *
566  * Return a negative value on failure, 0 on success.
567  */
568 int regcache_drop_region(struct regmap *map, unsigned int min,
569 			 unsigned int max)
570 {
571 	int ret = 0;
572 
573 	if (!map->cache_ops || !map->cache_ops->drop)
574 		return -EINVAL;
575 
576 	map->lock(map->lock_arg);
577 
578 	trace_regcache_drop_region(map, min, max);
579 
580 	ret = map->cache_ops->drop(map, min, max);
581 
582 	map->unlock(map->lock_arg);
583 
584 	return ret;
585 }
586 EXPORT_SYMBOL_GPL(regcache_drop_region);
587 
588 /**
589  * regcache_cache_only - Put a register map into cache only mode
590  *
591  * @map: map to configure
592  * @enable: flag if changes should be written to the hardware
593  *
594  * When a register map is marked as cache only writes to the register
595  * map API will only update the register cache, they will not cause
596  * any hardware changes.  This is useful for allowing portions of
597  * drivers to act as though the device were functioning as normal when
598  * it is disabled for power saving reasons.
599  */
600 void regcache_cache_only(struct regmap *map, bool enable)
601 {
602 	map->lock(map->lock_arg);
603 	WARN_ON(map->cache_type != REGCACHE_NONE &&
604 		map->cache_bypass && enable);
605 	map->cache_only = enable;
606 	trace_regmap_cache_only(map, enable);
607 	map->unlock(map->lock_arg);
608 }
609 EXPORT_SYMBOL_GPL(regcache_cache_only);
610 
611 /**
612  * regcache_mark_dirty - Indicate that HW registers were reset to default values
613  *
614  * @map: map to mark
615  *
616  * Inform regcache that the device has been powered down or reset, so that
617  * on resume, regcache_sync() knows to write out all non-default values
618  * stored in the cache.
619  *
620  * If this function is not called, regcache_sync() will assume that
621  * the hardware state still matches the cache state, modulo any writes that
622  * happened when cache_only was true.
623  */
624 void regcache_mark_dirty(struct regmap *map)
625 {
626 	map->lock(map->lock_arg);
627 	map->cache_dirty = true;
628 	map->no_sync_defaults = true;
629 	map->unlock(map->lock_arg);
630 }
631 EXPORT_SYMBOL_GPL(regcache_mark_dirty);
632 
633 /**
634  * regcache_cache_bypass - Put a register map into cache bypass mode
635  *
636  * @map: map to configure
637  * @enable: flag if changes should not be written to the cache
638  *
639  * When a register map is marked with the cache bypass option, writes
640  * to the register map API will only update the hardware and not
641  * the cache directly.  This is useful when syncing the cache back to
642  * the hardware.
643  */
644 void regcache_cache_bypass(struct regmap *map, bool enable)
645 {
646 	map->lock(map->lock_arg);
647 	WARN_ON(map->cache_only && enable);
648 	map->cache_bypass = enable;
649 	trace_regmap_cache_bypass(map, enable);
650 	map->unlock(map->lock_arg);
651 }
652 EXPORT_SYMBOL_GPL(regcache_cache_bypass);
653 
654 /**
655  * regcache_reg_cached - Check if a register is cached
656  *
657  * @map: map to check
658  * @reg: register to check
659  *
660  * Reports if a register is cached.
661  */
662 bool regcache_reg_cached(struct regmap *map, unsigned int reg)
663 {
664 	unsigned int val;
665 	int ret;
666 
667 	map->lock(map->lock_arg);
668 
669 	ret = regcache_read(map, reg, &val);
670 
671 	map->unlock(map->lock_arg);
672 
673 	return ret == 0;
674 }
675 EXPORT_SYMBOL_GPL(regcache_reg_cached);
676 
677 void regcache_set_val(struct regmap *map, void *base, unsigned int idx,
678 		      unsigned int val)
679 {
680 	/* Use device native format if possible */
681 	if (map->format.format_val) {
682 		map->format.format_val(base + (map->cache_word_size * idx),
683 				       val, 0);
684 		return;
685 	}
686 
687 	switch (map->cache_word_size) {
688 	case 1: {
689 		u8 *cache = base;
690 
691 		cache[idx] = val;
692 		break;
693 	}
694 	case 2: {
695 		u16 *cache = base;
696 
697 		cache[idx] = val;
698 		break;
699 	}
700 	case 4: {
701 		u32 *cache = base;
702 
703 		cache[idx] = val;
704 		break;
705 	}
706 	default:
707 		BUG();
708 	}
709 }
710 
711 unsigned int regcache_get_val(struct regmap *map, const void *base,
712 			      unsigned int idx)
713 {
714 	if (!base)
715 		return -EINVAL;
716 
717 	/* Use device native format if possible */
718 	if (map->format.parse_val)
719 		return map->format.parse_val(regcache_get_val_addr(map, base,
720 								   idx));
721 
722 	switch (map->cache_word_size) {
723 	case 1: {
724 		const u8 *cache = base;
725 
726 		return cache[idx];
727 	}
728 	case 2: {
729 		const u16 *cache = base;
730 
731 		return cache[idx];
732 	}
733 	case 4: {
734 		const u32 *cache = base;
735 
736 		return cache[idx];
737 	}
738 	default:
739 		BUG();
740 	}
741 	/* unreachable */
742 	return -1;
743 }
744 
745 int regcache_lookup_reg(struct regmap *map, unsigned int reg)
746 {
747 	struct reg_default key;
748 	struct reg_default *r;
749 
750 	key.reg = reg;
751 	key.def = 0;
752 
753 	r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
754 		    sizeof(struct reg_default), regcache_defaults_cmp);
755 
756 	if (r)
757 		return r - map->reg_defaults;
758 	else
759 		return -ENOENT;
760 }
761 
762 static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
763 {
764 	if (!cache_present)
765 		return true;
766 
767 	return test_bit(idx, cache_present);
768 }
769 
770 int regcache_sync_val(struct regmap *map, unsigned int reg, unsigned int val)
771 {
772 	int ret;
773 
774 	if (!regcache_reg_needs_sync(map, reg, val))
775 		return 0;
776 
777 	map->cache_bypass = true;
778 
779 	ret = _regmap_write(map, reg, val);
780 
781 	map->cache_bypass = false;
782 
783 	if (ret != 0) {
784 		dev_err(map->dev, "Unable to sync register %#x. %d\n",
785 			reg, ret);
786 		return ret;
787 	}
788 	dev_dbg(map->dev, "Synced register %#x, value %#x\n",
789 		reg, val);
790 
791 	return 0;
792 }
793 
794 static int regcache_sync_block_single(struct regmap *map, void *block,
795 				      unsigned long *cache_present,
796 				      unsigned int block_base,
797 				      unsigned int start, unsigned int end)
798 {
799 	unsigned int i, regtmp, val;
800 	int ret;
801 
802 	for (i = start; i < end; i++) {
803 		regtmp = block_base + (i * map->reg_stride);
804 
805 		if (!regcache_reg_present(cache_present, i) ||
806 		    !regmap_writeable(map, regtmp))
807 			continue;
808 
809 		val = regcache_get_val(map, block, i);
810 		ret = regcache_sync_val(map, regtmp, val);
811 		if (ret != 0)
812 			return ret;
813 	}
814 
815 	return 0;
816 }
817 
818 static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
819 					 unsigned int base, unsigned int cur)
820 {
821 	size_t val_bytes = map->format.val_bytes;
822 	int ret, count;
823 
824 	if (*data == NULL)
825 		return 0;
826 
827 	count = (cur - base) / map->reg_stride;
828 
829 	dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
830 		count * val_bytes, count, base, cur - map->reg_stride);
831 
832 	map->cache_bypass = true;
833 
834 	ret = _regmap_raw_write(map, base, *data, count * val_bytes, false);
835 	if (ret)
836 		dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
837 			base, cur - map->reg_stride, ret);
838 
839 	map->cache_bypass = false;
840 
841 	*data = NULL;
842 
843 	return ret;
844 }
845 
846 static int regcache_sync_block_raw(struct regmap *map, void *block,
847 			    unsigned long *cache_present,
848 			    unsigned int block_base, unsigned int start,
849 			    unsigned int end)
850 {
851 	unsigned int regtmp = 0;
852 	unsigned int base = 0;
853 	const void *data = NULL;
854 	unsigned int val;
855 	int ret;
856 
857 	for (unsigned int i = start; i < end; i++) {
858 		regtmp = block_base + (i * map->reg_stride);
859 
860 		if (!regcache_reg_present(cache_present, i) ||
861 		    !regmap_writeable(map, regtmp)) {
862 			ret = regcache_sync_block_raw_flush(map, &data,
863 							    base, regtmp);
864 			if (ret != 0)
865 				return ret;
866 			continue;
867 		}
868 
869 		val = regcache_get_val(map, block, i);
870 		if (!regcache_reg_needs_sync(map, regtmp, val)) {
871 			ret = regcache_sync_block_raw_flush(map, &data,
872 							    base, regtmp);
873 			if (ret != 0)
874 				return ret;
875 			continue;
876 		}
877 
878 		if (!data) {
879 			data = regcache_get_val_addr(map, block, i);
880 			base = regtmp;
881 		}
882 	}
883 
884 	return regcache_sync_block_raw_flush(map, &data, base, regtmp +
885 			map->reg_stride);
886 }
887 
888 int regcache_sync_block(struct regmap *map, void *block,
889 			unsigned long *cache_present,
890 			unsigned int block_base, unsigned int start,
891 			unsigned int end)
892 {
893 	if (regmap_can_raw_write(map) && !map->use_single_write)
894 		return regcache_sync_block_raw(map, block, cache_present,
895 					       block_base, start, end);
896 	else
897 		return regcache_sync_block_single(map, block, cache_present,
898 						  block_base, start, end);
899 }
900