xref: /linux/drivers/base/regmap/regcache.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
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  * This pushes cached changes made while cache_only (e.g. suspend) down
416  * to hardware. The caller must disable cache_only before calling this
417  * function.
418  *
419  * Return a negative value on failure, 0 on success.
420  */
421 int regcache_sync(struct regmap *map)
422 {
423 	int sync_ret = 0;
424 	int selector_ret = 0;
425 	unsigned int i;
426 	const char *name;
427 	bool bypass;
428 	struct rb_node *node;
429 
430 	if (WARN_ON(map->cache_type == REGCACHE_NONE))
431 		return -EINVAL;
432 
433 	BUG_ON(!map->cache_ops);
434 
435 	map->lock(map->lock_arg);
436 
437 	if (WARN_ON(map->cache_only)) {
438 		map->unlock(map->lock_arg);
439 		return -EINVAL;
440 	}
441 
442 	/* Remember the initial bypass state */
443 	bypass = map->cache_bypass;
444 	dev_dbg(map->dev, "Syncing %s cache\n",
445 		map->cache_ops->name);
446 	name = map->cache_ops->name;
447 	trace_regcache_sync(map, name, "start");
448 
449 	if (!map->cache_dirty)
450 		goto out;
451 
452 	/* Apply any patch first */
453 	map->cache_bypass = true;
454 	for (i = 0; i < map->patch_regs; i++) {
455 		sync_ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
456 		if (sync_ret != 0) {
457 			dev_err(map->dev, "Failed to write %x = %x: %d\n",
458 				map->patch[i].reg, map->patch[i].def, sync_ret);
459 			goto out;
460 		}
461 	}
462 	map->cache_bypass = false;
463 
464 	if (map->cache_ops->sync)
465 		sync_ret = map->cache_ops->sync(map, 0, map->max_register);
466 	else
467 		sync_ret = regcache_default_sync(map, 0, map->max_register);
468 
469 	if (sync_ret == 0)
470 		map->cache_dirty = false;
471 
472 out:
473 	/* Restore the bypass state */
474 	map->cache_bypass = bypass;
475 	map->no_sync_defaults = false;
476 
477 	/*
478 	 * If we did any paging with cache bypassed and a cached
479 	 * paging register then the register and cache state might
480 	 * have gone out of sync, force writes of all the paging
481 	 * registers.
482 	 */
483 	rb_for_each(node, NULL, &map->range_tree, rbtree_all) {
484 		struct regmap_range_node *this =
485 			rb_entry(node, struct regmap_range_node, node);
486 
487 		/* If there's nothing in the cache there's nothing to sync */
488 		if (regcache_read(map, this->selector_reg, &i) != 0)
489 			continue;
490 
491 		selector_ret = _regmap_write(map, this->selector_reg, i);
492 		if (selector_ret != 0) {
493 			map->cache_dirty = true;
494 			dev_err(map->dev, "Failed to write %x = %x: %d\n",
495 				this->selector_reg, i, selector_ret);
496 			break;
497 		}
498 	}
499 
500 	map->unlock(map->lock_arg);
501 
502 	regmap_async_complete(map);
503 
504 	trace_regcache_sync(map, name, "stop");
505 
506 	return sync_ret ? sync_ret : selector_ret;
507 }
508 EXPORT_SYMBOL_GPL(regcache_sync);
509 
510 /**
511  * regcache_sync_region - Sync part  of the register cache with the hardware.
512  *
513  * @map: map to sync.
514  * @min: first register to sync
515  * @max: last register to sync
516  *
517  * Write all non-default register values in the specified region to
518  * the hardware.
519  *
520  * Return a negative value on failure, 0 on success.
521  */
522 int regcache_sync_region(struct regmap *map, unsigned int min,
523 			 unsigned int max)
524 {
525 	int ret = 0;
526 	const char *name;
527 	bool bypass;
528 
529 	if (WARN_ON(map->cache_type == REGCACHE_NONE))
530 		return -EINVAL;
531 
532 	BUG_ON(!map->cache_ops);
533 
534 	map->lock(map->lock_arg);
535 
536 	if (WARN_ON(map->cache_only)) {
537 		map->unlock(map->lock_arg);
538 		return -EINVAL;
539 	}
540 	/* Remember the initial bypass state */
541 	bypass = map->cache_bypass;
542 
543 	name = map->cache_ops->name;
544 	dev_dbg(map->dev, "Syncing %s cache from %#x-%#x\n", name, min, max);
545 
546 	trace_regcache_sync(map, name, "start region");
547 
548 	if (!map->cache_dirty)
549 		goto out;
550 
551 	map->async = true;
552 
553 	if (map->cache_ops->sync)
554 		ret = map->cache_ops->sync(map, min, max);
555 	else
556 		ret = regcache_default_sync(map, min, max);
557 
558 out:
559 	/* Restore the bypass state */
560 	map->cache_bypass = bypass;
561 	map->async = false;
562 	map->no_sync_defaults = false;
563 	map->unlock(map->lock_arg);
564 
565 	regmap_async_complete(map);
566 
567 	trace_regcache_sync(map, name, "stop region");
568 
569 	return ret;
570 }
571 EXPORT_SYMBOL_GPL(regcache_sync_region);
572 
573 /**
574  * regcache_drop_region - Discard part of the register cache
575  *
576  * @map: map to operate on
577  * @min: first register to discard
578  * @max: last register to discard
579  *
580  * Discard part of the register cache.
581  *
582  * Return a negative value on failure, 0 on success.
583  */
584 int regcache_drop_region(struct regmap *map, unsigned int min,
585 			 unsigned int max)
586 {
587 	int ret = 0;
588 
589 	if (!map->cache_ops || !map->cache_ops->drop)
590 		return -EINVAL;
591 
592 	map->lock(map->lock_arg);
593 
594 	trace_regcache_drop_region(map, min, max);
595 
596 	ret = map->cache_ops->drop(map, min, max);
597 
598 	map->unlock(map->lock_arg);
599 
600 	return ret;
601 }
602 EXPORT_SYMBOL_GPL(regcache_drop_region);
603 
604 /**
605  * regcache_cache_only - Put a register map into cache only mode
606  *
607  * @map: map to configure
608  * @enable: flag if changes should be written to the hardware
609  *
610  * When a register map is marked as cache only writes to the register
611  * map API will only update the register cache, they will not cause
612  * any hardware changes.  This is useful for allowing portions of
613  * drivers to act as though the device were functioning as normal when
614  * it is disabled for power saving reasons.
615  */
616 void regcache_cache_only(struct regmap *map, bool enable)
617 {
618 	map->lock(map->lock_arg);
619 	WARN_ON(map->cache_type != REGCACHE_NONE &&
620 		map->cache_bypass && enable);
621 	map->cache_only = enable;
622 	trace_regmap_cache_only(map, enable);
623 	map->unlock(map->lock_arg);
624 }
625 EXPORT_SYMBOL_GPL(regcache_cache_only);
626 
627 /**
628  * regcache_mark_dirty - Indicate that HW registers were reset to default values
629  *
630  * @map: map to mark
631  *
632  * Inform regcache that the device has been powered down or reset, so that
633  * on resume, regcache_sync() knows to write out all non-default values
634  * stored in the cache.
635  *
636  * If this function is not called, regcache_sync() will assume that
637  * the hardware state still matches the cache state, modulo any writes that
638  * happened when cache_only was true.
639  */
640 void regcache_mark_dirty(struct regmap *map)
641 {
642 	map->lock(map->lock_arg);
643 	map->cache_dirty = true;
644 	map->no_sync_defaults = true;
645 	map->unlock(map->lock_arg);
646 }
647 EXPORT_SYMBOL_GPL(regcache_mark_dirty);
648 
649 /**
650  * regcache_cache_bypass - Put a register map into cache bypass mode
651  *
652  * @map: map to configure
653  * @enable: flag if changes should not be written to the cache
654  *
655  * When a register map is marked with the cache bypass option, writes
656  * to the register map API will only update the hardware and not
657  * the cache directly.  This is useful when syncing the cache back to
658  * the hardware.
659  */
660 void regcache_cache_bypass(struct regmap *map, bool enable)
661 {
662 	map->lock(map->lock_arg);
663 	WARN_ON(map->cache_only && enable);
664 	map->cache_bypass = enable;
665 	trace_regmap_cache_bypass(map, enable);
666 	map->unlock(map->lock_arg);
667 }
668 EXPORT_SYMBOL_GPL(regcache_cache_bypass);
669 
670 /**
671  * regcache_reg_cached - Check if a register is cached
672  *
673  * @map: map to check
674  * @reg: register to check
675  *
676  * Reports if a register is cached.
677  */
678 bool regcache_reg_cached(struct regmap *map, unsigned int reg)
679 {
680 	unsigned int val;
681 	int ret;
682 
683 	map->lock(map->lock_arg);
684 
685 	ret = regcache_read(map, reg, &val);
686 
687 	map->unlock(map->lock_arg);
688 
689 	return ret == 0;
690 }
691 EXPORT_SYMBOL_GPL(regcache_reg_cached);
692 
693 void regcache_set_val(struct regmap *map, void *base, unsigned int idx,
694 		      unsigned int val)
695 {
696 	/* Use device native format if possible */
697 	if (map->format.format_val) {
698 		map->format.format_val(base + (map->cache_word_size * idx),
699 				       val, 0);
700 		return;
701 	}
702 
703 	switch (map->cache_word_size) {
704 	case 1: {
705 		u8 *cache = base;
706 
707 		cache[idx] = val;
708 		break;
709 	}
710 	case 2: {
711 		u16 *cache = base;
712 
713 		cache[idx] = val;
714 		break;
715 	}
716 	case 4: {
717 		u32 *cache = base;
718 
719 		cache[idx] = val;
720 		break;
721 	}
722 	default:
723 		BUG();
724 	}
725 }
726 
727 unsigned int regcache_get_val(struct regmap *map, const void *base,
728 			      unsigned int idx)
729 {
730 	if (!base)
731 		return -EINVAL;
732 
733 	/* Use device native format if possible */
734 	if (map->format.parse_val)
735 		return map->format.parse_val(regcache_get_val_addr(map, base,
736 								   idx));
737 
738 	switch (map->cache_word_size) {
739 	case 1: {
740 		const u8 *cache = base;
741 
742 		return cache[idx];
743 	}
744 	case 2: {
745 		const u16 *cache = base;
746 
747 		return cache[idx];
748 	}
749 	case 4: {
750 		const u32 *cache = base;
751 
752 		return cache[idx];
753 	}
754 	default:
755 		BUG();
756 	}
757 	/* unreachable */
758 	return -1;
759 }
760 
761 int regcache_lookup_reg(struct regmap *map, unsigned int reg)
762 {
763 	struct reg_default key;
764 	struct reg_default *r;
765 
766 	key.reg = reg;
767 	key.def = 0;
768 
769 	r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
770 		    sizeof(struct reg_default), regcache_defaults_cmp);
771 
772 	if (r)
773 		return r - map->reg_defaults;
774 	else
775 		return -ENOENT;
776 }
777 
778 static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
779 {
780 	if (!cache_present)
781 		return true;
782 
783 	return test_bit(idx, cache_present);
784 }
785 
786 int regcache_sync_val(struct regmap *map, unsigned int reg, unsigned int val)
787 {
788 	int ret;
789 
790 	if (!regcache_reg_needs_sync(map, reg, val))
791 		return 0;
792 
793 	map->cache_bypass = true;
794 
795 	ret = _regmap_write(map, reg, val);
796 
797 	map->cache_bypass = false;
798 
799 	if (ret != 0) {
800 		dev_err(map->dev, "Unable to sync register %#x. %d\n",
801 			reg, ret);
802 		return ret;
803 	}
804 	dev_dbg(map->dev, "Synced register %#x, value %#x\n",
805 		reg, val);
806 
807 	return 0;
808 }
809 
810 static int regcache_sync_block_single(struct regmap *map, void *block,
811 				      unsigned long *cache_present,
812 				      unsigned int block_base,
813 				      unsigned int start, unsigned int end)
814 {
815 	unsigned int i, regtmp, val;
816 	int ret;
817 
818 	for (i = start; i < end; i++) {
819 		regtmp = block_base + (i * map->reg_stride);
820 
821 		if (!regcache_reg_present(cache_present, i) ||
822 		    !regmap_writeable(map, regtmp))
823 			continue;
824 
825 		val = regcache_get_val(map, block, i);
826 		ret = regcache_sync_val(map, regtmp, val);
827 		if (ret != 0)
828 			return ret;
829 	}
830 
831 	return 0;
832 }
833 
834 static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
835 					 unsigned int base, unsigned int cur)
836 {
837 	size_t val_bytes = map->format.val_bytes;
838 	int ret, count;
839 
840 	if (*data == NULL)
841 		return 0;
842 
843 	count = (cur - base) / map->reg_stride;
844 
845 	dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
846 		count * val_bytes, count, base, cur - map->reg_stride);
847 
848 	map->cache_bypass = true;
849 
850 	ret = _regmap_raw_write(map, base, *data, count * val_bytes, false);
851 	if (ret)
852 		dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
853 			base, cur - map->reg_stride, ret);
854 
855 	map->cache_bypass = false;
856 
857 	*data = NULL;
858 
859 	return ret;
860 }
861 
862 static int regcache_sync_block_raw(struct regmap *map, void *block,
863 			    unsigned long *cache_present,
864 			    unsigned int block_base, unsigned int start,
865 			    unsigned int end)
866 {
867 	unsigned int regtmp = 0;
868 	unsigned int base = 0;
869 	const void *data = NULL;
870 	unsigned int val;
871 	int ret;
872 
873 	for (unsigned int i = start; i < end; i++) {
874 		regtmp = block_base + (i * map->reg_stride);
875 
876 		if (!regcache_reg_present(cache_present, i) ||
877 		    !regmap_writeable(map, regtmp)) {
878 			ret = regcache_sync_block_raw_flush(map, &data,
879 							    base, regtmp);
880 			if (ret != 0)
881 				return ret;
882 			continue;
883 		}
884 
885 		val = regcache_get_val(map, block, i);
886 		if (!regcache_reg_needs_sync(map, regtmp, val)) {
887 			ret = regcache_sync_block_raw_flush(map, &data,
888 							    base, regtmp);
889 			if (ret != 0)
890 				return ret;
891 			continue;
892 		}
893 
894 		if (!data) {
895 			data = regcache_get_val_addr(map, block, i);
896 			base = regtmp;
897 		}
898 	}
899 
900 	return regcache_sync_block_raw_flush(map, &data, base, regtmp +
901 			map->reg_stride);
902 }
903 
904 int regcache_sync_block(struct regmap *map, void *block,
905 			unsigned long *cache_present,
906 			unsigned int block_base, unsigned int start,
907 			unsigned int end)
908 {
909 	if (regmap_can_raw_write(map) && !map->use_single_write)
910 		return regcache_sync_block_raw(map, block, cache_present,
911 					       block_base, start, end);
912 	else
913 		return regcache_sync_block_single(map, block, cache_present,
914 						  block_base, start, end);
915 }
916