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