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