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