xref: /openbmc/linux/drivers/base/regmap/regcache.c (revision fd883d79e4dcd2417c2b80756f22a2ff03b0f6e0)
137613fa5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
237613fa5SGreg Kroah-Hartman //
337613fa5SGreg Kroah-Hartman // Register cache access API
437613fa5SGreg Kroah-Hartman //
537613fa5SGreg Kroah-Hartman // Copyright 2011 Wolfson Microelectronics plc
637613fa5SGreg Kroah-Hartman //
737613fa5SGreg Kroah-Hartman // Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
89fabe24eSDimitris Papastamos 
9f094fea6SMark Brown #include <linux/bsearch.h>
10e39be3a3SXiubo Li #include <linux/device.h>
11e39be3a3SXiubo Li #include <linux/export.h>
12e39be3a3SXiubo Li #include <linux/slab.h>
13c08604b8SDimitris Papastamos #include <linux/sort.h>
149fabe24eSDimitris Papastamos 
15f58078daSSteven Rostedt #include "trace.h"
169fabe24eSDimitris Papastamos #include "internal.h"
179fabe24eSDimitris Papastamos 
189fabe24eSDimitris Papastamos static const struct regcache_ops *cache_types[] = {
1928644c80SDimitris Papastamos 	&regcache_rbtree_ops,
20f458e610SMark Brown #if IS_ENABLED(CONFIG_REGCACHE_COMPRESSED)
212cbbb579SDimitris Papastamos 	&regcache_lzo_ops,
2234a730aaSJonas Gorski #endif
232ac902ceSMark Brown 	&regcache_flat_ops,
249fabe24eSDimitris Papastamos };
259fabe24eSDimitris Papastamos 
269fabe24eSDimitris Papastamos static int regcache_hw_init(struct regmap *map)
279fabe24eSDimitris Papastamos {
289fabe24eSDimitris Papastamos 	int i, j;
299fabe24eSDimitris Papastamos 	int ret;
309fabe24eSDimitris Papastamos 	int count;
313245d460SMark Brown 	unsigned int reg, val;
329fabe24eSDimitris Papastamos 	void *tmp_buf;
339fabe24eSDimitris Papastamos 
349fabe24eSDimitris Papastamos 	if (!map->num_reg_defaults_raw)
359fabe24eSDimitris Papastamos 		return -EINVAL;
369fabe24eSDimitris Papastamos 
37fb70067eSXiubo Li 	/* calculate the size of reg_defaults */
38fb70067eSXiubo Li 	for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++)
39b2c7f5d9SMaarten ter Huurne 		if (regmap_readable(map, i * map->reg_stride) &&
40b2c7f5d9SMaarten ter Huurne 		    !regmap_volatile(map, i * map->reg_stride))
41fb70067eSXiubo Li 			count++;
42fb70067eSXiubo Li 
43b2c7f5d9SMaarten ter Huurne 	/* all registers are unreadable or volatile, so just bypass */
44fb70067eSXiubo Li 	if (!count) {
45fb70067eSXiubo Li 		map->cache_bypass = true;
46fb70067eSXiubo Li 		return 0;
47fb70067eSXiubo Li 	}
48fb70067eSXiubo Li 
49fb70067eSXiubo Li 	map->num_reg_defaults = count;
50fb70067eSXiubo Li 	map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default),
51fb70067eSXiubo Li 					  GFP_KERNEL);
52fb70067eSXiubo Li 	if (!map->reg_defaults)
53fb70067eSXiubo Li 		return -ENOMEM;
54fb70067eSXiubo Li 
559fabe24eSDimitris Papastamos 	if (!map->reg_defaults_raw) {
56621a5f7aSViresh Kumar 		bool cache_bypass = map->cache_bypass;
579fabe24eSDimitris Papastamos 		dev_warn(map->dev, "No cache defaults, reading back from HW\n");
58df00c79fSLaxman Dewangan 
59df00c79fSLaxman Dewangan 		/* Bypass the cache access till data read from HW */
60621a5f7aSViresh Kumar 		map->cache_bypass = true;
619fabe24eSDimitris Papastamos 		tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
62fb70067eSXiubo Li 		if (!tmp_buf) {
63fb70067eSXiubo Li 			ret = -ENOMEM;
64fb70067eSXiubo Li 			goto err_free;
65fb70067eSXiubo Li 		}
66eb4cb76fSMark Brown 		ret = regmap_raw_read(map, 0, tmp_buf,
67d51fe1f3SMaciej S. Szmigiero 				      map->cache_size_raw);
68df00c79fSLaxman Dewangan 		map->cache_bypass = cache_bypass;
693245d460SMark Brown 		if (ret == 0) {
709fabe24eSDimitris Papastamos 			map->reg_defaults_raw = tmp_buf;
71b67498d6SJiapeng Zhong 			map->cache_free = true;
723245d460SMark Brown 		} else {
733245d460SMark Brown 			kfree(tmp_buf);
743245d460SMark Brown 		}
759fabe24eSDimitris Papastamos 	}
769fabe24eSDimitris Papastamos 
779fabe24eSDimitris Papastamos 	/* fill the reg_defaults */
789fabe24eSDimitris Papastamos 	for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
793245d460SMark Brown 		reg = i * map->reg_stride;
803245d460SMark Brown 
813245d460SMark Brown 		if (!regmap_readable(map, reg))
829fabe24eSDimitris Papastamos 			continue;
833245d460SMark Brown 
843245d460SMark Brown 		if (regmap_volatile(map, reg))
853245d460SMark Brown 			continue;
863245d460SMark Brown 
873245d460SMark Brown 		if (map->reg_defaults_raw) {
88fbba43c5SXiubo Li 			val = regcache_get_val(map, map->reg_defaults_raw, i);
893245d460SMark Brown 		} else {
903245d460SMark Brown 			bool cache_bypass = map->cache_bypass;
913245d460SMark Brown 
923245d460SMark Brown 			map->cache_bypass = true;
933245d460SMark Brown 			ret = regmap_read(map, reg, &val);
943245d460SMark Brown 			map->cache_bypass = cache_bypass;
953245d460SMark Brown 			if (ret != 0) {
963245d460SMark Brown 				dev_err(map->dev, "Failed to read %d: %d\n",
973245d460SMark Brown 					reg, ret);
983245d460SMark Brown 				goto err_free;
993245d460SMark Brown 			}
1003245d460SMark Brown 		}
1013245d460SMark Brown 
1023245d460SMark Brown 		map->reg_defaults[j].reg = reg;
1039fabe24eSDimitris Papastamos 		map->reg_defaults[j].def = val;
1049fabe24eSDimitris Papastamos 		j++;
1059fabe24eSDimitris Papastamos 	}
1069fabe24eSDimitris Papastamos 
1079fabe24eSDimitris Papastamos 	return 0;
108021cd616SLars-Peter Clausen 
109021cd616SLars-Peter Clausen err_free:
110fb70067eSXiubo Li 	kfree(map->reg_defaults);
111021cd616SLars-Peter Clausen 
112021cd616SLars-Peter Clausen 	return ret;
1139fabe24eSDimitris Papastamos }
1149fabe24eSDimitris Papastamos 
115e5e3b8abSLars-Peter Clausen int regcache_init(struct regmap *map, const struct regmap_config *config)
1169fabe24eSDimitris Papastamos {
1179fabe24eSDimitris Papastamos 	int ret;
1189fabe24eSDimitris Papastamos 	int i;
1199fabe24eSDimitris Papastamos 	void *tmp_buf;
1209fabe24eSDimitris Papastamos 
121e7a6db30SMark Brown 	if (map->cache_type == REGCACHE_NONE) {
1228cfe2fd3SXiubo Li 		if (config->reg_defaults || config->num_reg_defaults_raw)
1238cfe2fd3SXiubo Li 			dev_warn(map->dev,
1248cfe2fd3SXiubo Li 				 "No cache used with register defaults set!\n");
1258cfe2fd3SXiubo Li 
126e7a6db30SMark Brown 		map->cache_bypass = true;
1279fabe24eSDimitris Papastamos 		return 0;
128e7a6db30SMark Brown 	}
1299fabe24eSDimitris Papastamos 
130167f7066SXiubo Li 	if (config->reg_defaults && !config->num_reg_defaults) {
131167f7066SXiubo Li 		dev_err(map->dev,
132167f7066SXiubo Li 			 "Register defaults are set without the number!\n");
133167f7066SXiubo Li 		return -EINVAL;
134167f7066SXiubo Li 	}
135167f7066SXiubo Li 
136a5201d42SSchspa Shi 	if (config->num_reg_defaults && !config->reg_defaults) {
137a5201d42SSchspa Shi 		dev_err(map->dev,
138a5201d42SSchspa Shi 			"Register defaults number are set without the reg!\n");
139a5201d42SSchspa Shi 		return -EINVAL;
140a5201d42SSchspa Shi 	}
141a5201d42SSchspa Shi 
1428cfe2fd3SXiubo Li 	for (i = 0; i < config->num_reg_defaults; i++)
1438cfe2fd3SXiubo Li 		if (config->reg_defaults[i].reg % map->reg_stride)
1448cfe2fd3SXiubo Li 			return -EINVAL;
1458cfe2fd3SXiubo Li 
1469fabe24eSDimitris Papastamos 	for (i = 0; i < ARRAY_SIZE(cache_types); i++)
1479fabe24eSDimitris Papastamos 		if (cache_types[i]->type == map->cache_type)
1489fabe24eSDimitris Papastamos 			break;
1499fabe24eSDimitris Papastamos 
1509fabe24eSDimitris Papastamos 	if (i == ARRAY_SIZE(cache_types)) {
1519fabe24eSDimitris Papastamos 		dev_err(map->dev, "Could not match compress type: %d\n",
1529fabe24eSDimitris Papastamos 			map->cache_type);
1539fabe24eSDimitris Papastamos 		return -EINVAL;
1549fabe24eSDimitris Papastamos 	}
1559fabe24eSDimitris Papastamos 
156e5e3b8abSLars-Peter Clausen 	map->num_reg_defaults = config->num_reg_defaults;
157e5e3b8abSLars-Peter Clausen 	map->num_reg_defaults_raw = config->num_reg_defaults_raw;
158e5e3b8abSLars-Peter Clausen 	map->reg_defaults_raw = config->reg_defaults_raw;
159064d4db1SLars-Peter Clausen 	map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
160064d4db1SLars-Peter Clausen 	map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
161e5e3b8abSLars-Peter Clausen 
1629fabe24eSDimitris Papastamos 	map->cache = NULL;
1639fabe24eSDimitris Papastamos 	map->cache_ops = cache_types[i];
1649fabe24eSDimitris Papastamos 
1659fabe24eSDimitris Papastamos 	if (!map->cache_ops->read ||
1669fabe24eSDimitris Papastamos 	    !map->cache_ops->write ||
1679fabe24eSDimitris Papastamos 	    !map->cache_ops->name)
1689fabe24eSDimitris Papastamos 		return -EINVAL;
1699fabe24eSDimitris Papastamos 
1709fabe24eSDimitris Papastamos 	/* We still need to ensure that the reg_defaults
1719fabe24eSDimitris Papastamos 	 * won't vanish from under us.  We'll need to make
1729fabe24eSDimitris Papastamos 	 * a copy of it.
1739fabe24eSDimitris Papastamos 	 */
174720e4616SLars-Peter Clausen 	if (config->reg_defaults) {
175720e4616SLars-Peter Clausen 		tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
1769fabe24eSDimitris Papastamos 				  sizeof(struct reg_default), GFP_KERNEL);
1779fabe24eSDimitris Papastamos 		if (!tmp_buf)
1789fabe24eSDimitris Papastamos 			return -ENOMEM;
1799fabe24eSDimitris Papastamos 		map->reg_defaults = tmp_buf;
1808528bdd4SMark Brown 	} else if (map->num_reg_defaults_raw) {
1815fcd2560SMark Brown 		/* Some devices such as PMICs don't have cache defaults,
1829fabe24eSDimitris Papastamos 		 * we cope with this by reading back the HW registers and
1839fabe24eSDimitris Papastamos 		 * crafting the cache defaults by hand.
1849fabe24eSDimitris Papastamos 		 */
1859fabe24eSDimitris Papastamos 		ret = regcache_hw_init(map);
1869fabe24eSDimitris Papastamos 		if (ret < 0)
1879fabe24eSDimitris Papastamos 			return ret;
188fb70067eSXiubo Li 		if (map->cache_bypass)
189fb70067eSXiubo Li 			return 0;
1909fabe24eSDimitris Papastamos 	}
1919fabe24eSDimitris Papastamos 
192d6409475SJeongtae Park 	if (!map->max_register && map->num_reg_defaults_raw)
193d6409475SJeongtae Park 		map->max_register = (map->num_reg_defaults_raw  - 1) * map->reg_stride;
1949fabe24eSDimitris Papastamos 
1959fabe24eSDimitris Papastamos 	if (map->cache_ops->init) {
1969fabe24eSDimitris Papastamos 		dev_dbg(map->dev, "Initializing %s cache\n",
1979fabe24eSDimitris Papastamos 			map->cache_ops->name);
198bd061c78SLars-Peter Clausen 		ret = map->cache_ops->init(map);
199bd061c78SLars-Peter Clausen 		if (ret)
200bd061c78SLars-Peter Clausen 			goto err_free;
2019fabe24eSDimitris Papastamos 	}
2029fabe24eSDimitris Papastamos 	return 0;
203bd061c78SLars-Peter Clausen 
204bd061c78SLars-Peter Clausen err_free:
205bd061c78SLars-Peter Clausen 	kfree(map->reg_defaults);
206bd061c78SLars-Peter Clausen 	if (map->cache_free)
207bd061c78SLars-Peter Clausen 		kfree(map->reg_defaults_raw);
208bd061c78SLars-Peter Clausen 
209bd061c78SLars-Peter Clausen 	return ret;
2109fabe24eSDimitris Papastamos }
2119fabe24eSDimitris Papastamos 
2129fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map)
2139fabe24eSDimitris Papastamos {
2149fabe24eSDimitris Papastamos 	if (map->cache_type == REGCACHE_NONE)
2159fabe24eSDimitris Papastamos 		return;
2169fabe24eSDimitris Papastamos 
2179fabe24eSDimitris Papastamos 	BUG_ON(!map->cache_ops);
2189fabe24eSDimitris Papastamos 
2199fabe24eSDimitris Papastamos 	kfree(map->reg_defaults);
2209fabe24eSDimitris Papastamos 	if (map->cache_free)
2219fabe24eSDimitris Papastamos 		kfree(map->reg_defaults_raw);
2229fabe24eSDimitris Papastamos 
2239fabe24eSDimitris Papastamos 	if (map->cache_ops->exit) {
2249fabe24eSDimitris Papastamos 		dev_dbg(map->dev, "Destroying %s cache\n",
2259fabe24eSDimitris Papastamos 			map->cache_ops->name);
2269fabe24eSDimitris Papastamos 		map->cache_ops->exit(map);
2279fabe24eSDimitris Papastamos 	}
2289fabe24eSDimitris Papastamos }
2299fabe24eSDimitris Papastamos 
2309fabe24eSDimitris Papastamos /**
2312cf8e2dfSCharles Keepax  * regcache_read - Fetch the value of a given register from the cache.
2329fabe24eSDimitris Papastamos  *
2339fabe24eSDimitris Papastamos  * @map: map to configure.
2349fabe24eSDimitris Papastamos  * @reg: The register index.
2359fabe24eSDimitris Papastamos  * @value: The value to be returned.
2369fabe24eSDimitris Papastamos  *
2379fabe24eSDimitris Papastamos  * Return a negative value on failure, 0 on success.
2389fabe24eSDimitris Papastamos  */
2399fabe24eSDimitris Papastamos int regcache_read(struct regmap *map,
2409fabe24eSDimitris Papastamos 		  unsigned int reg, unsigned int *value)
2419fabe24eSDimitris Papastamos {
242bc7ee556SMark Brown 	int ret;
243bc7ee556SMark Brown 
2449fabe24eSDimitris Papastamos 	if (map->cache_type == REGCACHE_NONE)
2459fabe24eSDimitris Papastamos 		return -ENOSYS;
2469fabe24eSDimitris Papastamos 
2479fabe24eSDimitris Papastamos 	BUG_ON(!map->cache_ops);
2489fabe24eSDimitris Papastamos 
249bc7ee556SMark Brown 	if (!regmap_volatile(map, reg)) {
250bc7ee556SMark Brown 		ret = map->cache_ops->read(map, reg, value);
251bc7ee556SMark Brown 
252bc7ee556SMark Brown 		if (ret == 0)
253c6b570d9SPhilipp Zabel 			trace_regmap_reg_read_cache(map, reg, *value);
254bc7ee556SMark Brown 
255bc7ee556SMark Brown 		return ret;
256bc7ee556SMark Brown 	}
2579fabe24eSDimitris Papastamos 
2589fabe24eSDimitris Papastamos 	return -EINVAL;
2599fabe24eSDimitris Papastamos }
2609fabe24eSDimitris Papastamos 
2619fabe24eSDimitris Papastamos /**
2622cf8e2dfSCharles Keepax  * regcache_write - Set the value of a given register in the cache.
2639fabe24eSDimitris Papastamos  *
2649fabe24eSDimitris Papastamos  * @map: map to configure.
2659fabe24eSDimitris Papastamos  * @reg: The register index.
2669fabe24eSDimitris Papastamos  * @value: The new register value.
2679fabe24eSDimitris Papastamos  *
2689fabe24eSDimitris Papastamos  * Return a negative value on failure, 0 on success.
2699fabe24eSDimitris Papastamos  */
2709fabe24eSDimitris Papastamos int regcache_write(struct regmap *map,
2719fabe24eSDimitris Papastamos 		   unsigned int reg, unsigned int value)
2729fabe24eSDimitris Papastamos {
2739fabe24eSDimitris Papastamos 	if (map->cache_type == REGCACHE_NONE)
2749fabe24eSDimitris Papastamos 		return 0;
2759fabe24eSDimitris Papastamos 
2769fabe24eSDimitris Papastamos 	BUG_ON(!map->cache_ops);
2779fabe24eSDimitris Papastamos 
2789fabe24eSDimitris Papastamos 	if (!regmap_volatile(map, reg))
2799fabe24eSDimitris Papastamos 		return map->cache_ops->write(map, reg, value);
2809fabe24eSDimitris Papastamos 
2819fabe24eSDimitris Papastamos 	return 0;
2829fabe24eSDimitris Papastamos }
2839fabe24eSDimitris Papastamos 
2843969fa08SKevin Cernekee static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
2853969fa08SKevin Cernekee 				    unsigned int val)
2863969fa08SKevin Cernekee {
2873969fa08SKevin Cernekee 	int ret;
2883969fa08SKevin Cernekee 
2891c79771aSKevin Cernekee 	/* If we don't know the chip just got reset, then sync everything. */
2901c79771aSKevin Cernekee 	if (!map->no_sync_defaults)
2911c79771aSKevin Cernekee 		return true;
2921c79771aSKevin Cernekee 
2933969fa08SKevin Cernekee 	/* Is this the hardware default?  If so skip. */
2943969fa08SKevin Cernekee 	ret = regcache_lookup_reg(map, reg);
2953969fa08SKevin Cernekee 	if (ret >= 0 && val == map->reg_defaults[ret].def)
2963969fa08SKevin Cernekee 		return false;
2973969fa08SKevin Cernekee 	return true;
2983969fa08SKevin Cernekee }
2993969fa08SKevin Cernekee 
300d856fce4SMaarten ter Huurne static int regcache_default_sync(struct regmap *map, unsigned int min,
301d856fce4SMaarten ter Huurne 				 unsigned int max)
302d856fce4SMaarten ter Huurne {
303d856fce4SMaarten ter Huurne 	unsigned int reg;
304d856fce4SMaarten ter Huurne 
30575617328SDylan Reid 	for (reg = min; reg <= max; reg += map->reg_stride) {
306d856fce4SMaarten ter Huurne 		unsigned int val;
307d856fce4SMaarten ter Huurne 		int ret;
308d856fce4SMaarten ter Huurne 
30983f8475cSDylan Reid 		if (regmap_volatile(map, reg) ||
31083f8475cSDylan Reid 		    !regmap_writeable(map, reg))
311d856fce4SMaarten ter Huurne 			continue;
312d856fce4SMaarten ter Huurne 
313d856fce4SMaarten ter Huurne 		ret = regcache_read(map, reg, &val);
314d856fce4SMaarten ter Huurne 		if (ret)
315d856fce4SMaarten ter Huurne 			return ret;
316d856fce4SMaarten ter Huurne 
3173969fa08SKevin Cernekee 		if (!regcache_reg_needs_sync(map, reg, val))
318d856fce4SMaarten ter Huurne 			continue;
319d856fce4SMaarten ter Huurne 
320621a5f7aSViresh Kumar 		map->cache_bypass = true;
321d856fce4SMaarten ter Huurne 		ret = _regmap_write(map, reg, val);
322621a5f7aSViresh Kumar 		map->cache_bypass = false;
323f29a4320SJarkko Nikula 		if (ret) {
324f29a4320SJarkko Nikula 			dev_err(map->dev, "Unable to sync register %#x. %d\n",
325f29a4320SJarkko Nikula 				reg, ret);
326d856fce4SMaarten ter Huurne 			return ret;
327f29a4320SJarkko Nikula 		}
328d856fce4SMaarten ter Huurne 		dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
329d856fce4SMaarten ter Huurne 	}
330d856fce4SMaarten ter Huurne 
331d856fce4SMaarten ter Huurne 	return 0;
332d856fce4SMaarten ter Huurne }
333d856fce4SMaarten ter Huurne 
3349fabe24eSDimitris Papastamos /**
3352cf8e2dfSCharles Keepax  * regcache_sync - Sync the register cache with the hardware.
3369fabe24eSDimitris Papastamos  *
3379fabe24eSDimitris Papastamos  * @map: map to configure.
3389fabe24eSDimitris Papastamos  *
3399fabe24eSDimitris Papastamos  * Any registers that should not be synced should be marked as
3409fabe24eSDimitris Papastamos  * volatile.  In general drivers can choose not to use the provided
3419fabe24eSDimitris Papastamos  * syncing functionality if they so require.
3429fabe24eSDimitris Papastamos  *
3439fabe24eSDimitris Papastamos  * Return a negative value on failure, 0 on success.
3449fabe24eSDimitris Papastamos  */
3459fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map)
3469fabe24eSDimitris Papastamos {
347954757d7SDimitris Papastamos 	int ret = 0;
348954757d7SDimitris Papastamos 	unsigned int i;
34959360089SDimitris Papastamos 	const char *name;
350621a5f7aSViresh Kumar 	bool bypass;
35159360089SDimitris Papastamos 
352*fd883d79SAlexander Stein 	if (WARN_ON(map->cache_type == REGCACHE_NONE))
353*fd883d79SAlexander Stein 		return -EINVAL;
354*fd883d79SAlexander Stein 
355d856fce4SMaarten ter Huurne 	BUG_ON(!map->cache_ops);
3569fabe24eSDimitris Papastamos 
35781485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
358beb1a10fSDimitris Papastamos 	/* Remember the initial bypass state */
359beb1a10fSDimitris Papastamos 	bypass = map->cache_bypass;
3609fabe24eSDimitris Papastamos 	dev_dbg(map->dev, "Syncing %s cache\n",
3619fabe24eSDimitris Papastamos 		map->cache_ops->name);
36259360089SDimitris Papastamos 	name = map->cache_ops->name;
363c6b570d9SPhilipp Zabel 	trace_regcache_sync(map, name, "start");
36422f0d90aSMark Brown 
3658ae0d7e8SMark Brown 	if (!map->cache_dirty)
3668ae0d7e8SMark Brown 		goto out;
367d9db7627SMark Brown 
368affbe886SMark Brown 	map->async = true;
369affbe886SMark Brown 
37022f0d90aSMark Brown 	/* Apply any patch first */
371621a5f7aSViresh Kumar 	map->cache_bypass = true;
37222f0d90aSMark Brown 	for (i = 0; i < map->patch_regs; i++) {
37322f0d90aSMark Brown 		ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
37422f0d90aSMark Brown 		if (ret != 0) {
37522f0d90aSMark Brown 			dev_err(map->dev, "Failed to write %x = %x: %d\n",
37622f0d90aSMark Brown 				map->patch[i].reg, map->patch[i].def, ret);
37722f0d90aSMark Brown 			goto out;
37822f0d90aSMark Brown 		}
37922f0d90aSMark Brown 	}
380621a5f7aSViresh Kumar 	map->cache_bypass = false;
38122f0d90aSMark Brown 
382d856fce4SMaarten ter Huurne 	if (map->cache_ops->sync)
383ac8d91c8SMark Brown 		ret = map->cache_ops->sync(map, 0, map->max_register);
384d856fce4SMaarten ter Huurne 	else
385d856fce4SMaarten ter Huurne 		ret = regcache_default_sync(map, 0, map->max_register);
386954757d7SDimitris Papastamos 
3876ff73738SMark Brown 	if (ret == 0)
3886ff73738SMark Brown 		map->cache_dirty = false;
3896ff73738SMark Brown 
390954757d7SDimitris Papastamos out:
391beb1a10fSDimitris Papastamos 	/* Restore the bypass state */
392affbe886SMark Brown 	map->async = false;
393beb1a10fSDimitris Papastamos 	map->cache_bypass = bypass;
3941c79771aSKevin Cernekee 	map->no_sync_defaults = false;
39581485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
396954757d7SDimitris Papastamos 
397affbe886SMark Brown 	regmap_async_complete(map);
398affbe886SMark Brown 
399c6b570d9SPhilipp Zabel 	trace_regcache_sync(map, name, "stop");
400affbe886SMark Brown 
401954757d7SDimitris Papastamos 	return ret;
4029fabe24eSDimitris Papastamos }
4039fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync);
4049fabe24eSDimitris Papastamos 
40592afb286SMark Brown /**
4062cf8e2dfSCharles Keepax  * regcache_sync_region - Sync part  of the register cache with the hardware.
4074d4cfd16SMark Brown  *
4084d4cfd16SMark Brown  * @map: map to sync.
4094d4cfd16SMark Brown  * @min: first register to sync
4104d4cfd16SMark Brown  * @max: last register to sync
4114d4cfd16SMark Brown  *
4124d4cfd16SMark Brown  * Write all non-default register values in the specified region to
4134d4cfd16SMark Brown  * the hardware.
4144d4cfd16SMark Brown  *
4154d4cfd16SMark Brown  * Return a negative value on failure, 0 on success.
4164d4cfd16SMark Brown  */
4174d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min,
4184d4cfd16SMark Brown 			 unsigned int max)
4194d4cfd16SMark Brown {
4204d4cfd16SMark Brown 	int ret = 0;
4214d4cfd16SMark Brown 	const char *name;
422621a5f7aSViresh Kumar 	bool bypass;
4234d4cfd16SMark Brown 
424*fd883d79SAlexander Stein 	if (WARN_ON(map->cache_type == REGCACHE_NONE))
425*fd883d79SAlexander Stein 		return -EINVAL;
426*fd883d79SAlexander Stein 
427d856fce4SMaarten ter Huurne 	BUG_ON(!map->cache_ops);
4284d4cfd16SMark Brown 
42981485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
4304d4cfd16SMark Brown 
4314d4cfd16SMark Brown 	/* Remember the initial bypass state */
4324d4cfd16SMark Brown 	bypass = map->cache_bypass;
4334d4cfd16SMark Brown 
4344d4cfd16SMark Brown 	name = map->cache_ops->name;
4354d4cfd16SMark Brown 	dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
4364d4cfd16SMark Brown 
437c6b570d9SPhilipp Zabel 	trace_regcache_sync(map, name, "start region");
4384d4cfd16SMark Brown 
4394d4cfd16SMark Brown 	if (!map->cache_dirty)
4404d4cfd16SMark Brown 		goto out;
4414d4cfd16SMark Brown 
442affbe886SMark Brown 	map->async = true;
443affbe886SMark Brown 
444d856fce4SMaarten ter Huurne 	if (map->cache_ops->sync)
4454d4cfd16SMark Brown 		ret = map->cache_ops->sync(map, min, max);
446d856fce4SMaarten ter Huurne 	else
447d856fce4SMaarten ter Huurne 		ret = regcache_default_sync(map, min, max);
4484d4cfd16SMark Brown 
4494d4cfd16SMark Brown out:
4504d4cfd16SMark Brown 	/* Restore the bypass state */
4514d4cfd16SMark Brown 	map->cache_bypass = bypass;
452affbe886SMark Brown 	map->async = false;
4531c79771aSKevin Cernekee 	map->no_sync_defaults = false;
45481485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
4554d4cfd16SMark Brown 
456affbe886SMark Brown 	regmap_async_complete(map);
457affbe886SMark Brown 
458c6b570d9SPhilipp Zabel 	trace_regcache_sync(map, name, "stop region");
459affbe886SMark Brown 
4604d4cfd16SMark Brown 	return ret;
4614d4cfd16SMark Brown }
462e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region);
4634d4cfd16SMark Brown 
4644d4cfd16SMark Brown /**
4652cf8e2dfSCharles Keepax  * regcache_drop_region - Discard part of the register cache
466697e85bcSMark Brown  *
467697e85bcSMark Brown  * @map: map to operate on
468697e85bcSMark Brown  * @min: first register to discard
469697e85bcSMark Brown  * @max: last register to discard
470697e85bcSMark Brown  *
471697e85bcSMark Brown  * Discard part of the register cache.
472697e85bcSMark Brown  *
473697e85bcSMark Brown  * Return a negative value on failure, 0 on success.
474697e85bcSMark Brown  */
475697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min,
476697e85bcSMark Brown 			 unsigned int max)
477697e85bcSMark Brown {
478697e85bcSMark Brown 	int ret = 0;
479697e85bcSMark Brown 
4803f4ff561SLars-Peter Clausen 	if (!map->cache_ops || !map->cache_ops->drop)
481697e85bcSMark Brown 		return -EINVAL;
482697e85bcSMark Brown 
48381485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
484697e85bcSMark Brown 
485c6b570d9SPhilipp Zabel 	trace_regcache_drop_region(map, min, max);
486697e85bcSMark Brown 
487697e85bcSMark Brown 	ret = map->cache_ops->drop(map, min, max);
488697e85bcSMark Brown 
48981485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
490697e85bcSMark Brown 
491697e85bcSMark Brown 	return ret;
492697e85bcSMark Brown }
493697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region);
494697e85bcSMark Brown 
495697e85bcSMark Brown /**
4962cf8e2dfSCharles Keepax  * regcache_cache_only - Put a register map into cache only mode
49792afb286SMark Brown  *
49892afb286SMark Brown  * @map: map to configure
4992cf8e2dfSCharles Keepax  * @enable: flag if changes should be written to the hardware
50092afb286SMark Brown  *
50192afb286SMark Brown  * When a register map is marked as cache only writes to the register
50292afb286SMark Brown  * map API will only update the register cache, they will not cause
50392afb286SMark Brown  * any hardware changes.  This is useful for allowing portions of
50492afb286SMark Brown  * drivers to act as though the device were functioning as normal when
50592afb286SMark Brown  * it is disabled for power saving reasons.
50692afb286SMark Brown  */
50792afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable)
50892afb286SMark Brown {
50981485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
5103d0afe9cSMark Brown 	WARN_ON(map->cache_type != REGCACHE_NONE &&
5113d0afe9cSMark Brown 		map->cache_bypass && enable);
51292afb286SMark Brown 	map->cache_only = enable;
513c6b570d9SPhilipp Zabel 	trace_regmap_cache_only(map, enable);
51481485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
51592afb286SMark Brown }
51692afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only);
51792afb286SMark Brown 
5186eb0f5e0SDimitris Papastamos /**
5192cf8e2dfSCharles Keepax  * regcache_mark_dirty - Indicate that HW registers were reset to default values
5208ae0d7e8SMark Brown  *
5218ae0d7e8SMark Brown  * @map: map to mark
5228ae0d7e8SMark Brown  *
5231c79771aSKevin Cernekee  * Inform regcache that the device has been powered down or reset, so that
5241c79771aSKevin Cernekee  * on resume, regcache_sync() knows to write out all non-default values
5251c79771aSKevin Cernekee  * stored in the cache.
5261c79771aSKevin Cernekee  *
5271c79771aSKevin Cernekee  * If this function is not called, regcache_sync() will assume that
5281c79771aSKevin Cernekee  * the hardware state still matches the cache state, modulo any writes that
5291c79771aSKevin Cernekee  * happened when cache_only was true.
5308ae0d7e8SMark Brown  */
5318ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map)
5328ae0d7e8SMark Brown {
53381485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
5348ae0d7e8SMark Brown 	map->cache_dirty = true;
5351c79771aSKevin Cernekee 	map->no_sync_defaults = true;
53681485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
5378ae0d7e8SMark Brown }
5388ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty);
5398ae0d7e8SMark Brown 
5408ae0d7e8SMark Brown /**
5412cf8e2dfSCharles Keepax  * regcache_cache_bypass - Put a register map into cache bypass mode
5426eb0f5e0SDimitris Papastamos  *
5436eb0f5e0SDimitris Papastamos  * @map: map to configure
5442cf8e2dfSCharles Keepax  * @enable: flag if changes should not be written to the cache
5456eb0f5e0SDimitris Papastamos  *
5466eb0f5e0SDimitris Papastamos  * When a register map is marked with the cache bypass option, writes
54772607f37SXiang wangx  * to the register map API will only update the hardware and not
5486eb0f5e0SDimitris Papastamos  * the cache directly.  This is useful when syncing the cache back to
5496eb0f5e0SDimitris Papastamos  * the hardware.
5506eb0f5e0SDimitris Papastamos  */
5516eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable)
5526eb0f5e0SDimitris Papastamos {
55381485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
554ac77a765SDimitris Papastamos 	WARN_ON(map->cache_only && enable);
5556eb0f5e0SDimitris Papastamos 	map->cache_bypass = enable;
556c6b570d9SPhilipp Zabel 	trace_regmap_cache_bypass(map, enable);
55781485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
5586eb0f5e0SDimitris Papastamos }
5596eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass);
5606eb0f5e0SDimitris Papastamos 
561879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
562879082c9SMark Brown 		      unsigned int val)
5639fabe24eSDimitris Papastamos {
564325acab4SMark Brown 	if (regcache_get_val(map, base, idx) == val)
565325acab4SMark Brown 		return true;
566325acab4SMark Brown 
567eb4cb76fSMark Brown 	/* Use device native format if possible */
568eb4cb76fSMark Brown 	if (map->format.format_val) {
569eb4cb76fSMark Brown 		map->format.format_val(base + (map->cache_word_size * idx),
570eb4cb76fSMark Brown 				       val, 0);
571eb4cb76fSMark Brown 		return false;
572eb4cb76fSMark Brown 	}
573eb4cb76fSMark Brown 
574879082c9SMark Brown 	switch (map->cache_word_size) {
5759fabe24eSDimitris Papastamos 	case 1: {
5769fabe24eSDimitris Papastamos 		u8 *cache = base;
5772fd6902eSXiubo Li 
5789fabe24eSDimitris Papastamos 		cache[idx] = val;
5799fabe24eSDimitris Papastamos 		break;
5809fabe24eSDimitris Papastamos 	}
5819fabe24eSDimitris Papastamos 	case 2: {
5829fabe24eSDimitris Papastamos 		u16 *cache = base;
5832fd6902eSXiubo Li 
5849fabe24eSDimitris Papastamos 		cache[idx] = val;
5859fabe24eSDimitris Papastamos 		break;
5869fabe24eSDimitris Papastamos 	}
5877d5e525bSMark Brown 	case 4: {
5887d5e525bSMark Brown 		u32 *cache = base;
5892fd6902eSXiubo Li 
5907d5e525bSMark Brown 		cache[idx] = val;
5917d5e525bSMark Brown 		break;
5927d5e525bSMark Brown 	}
5938b7663deSXiubo Li #ifdef CONFIG_64BIT
5948b7663deSXiubo Li 	case 8: {
5958b7663deSXiubo Li 		u64 *cache = base;
5968b7663deSXiubo Li 
5978b7663deSXiubo Li 		cache[idx] = val;
5988b7663deSXiubo Li 		break;
5998b7663deSXiubo Li 	}
6008b7663deSXiubo Li #endif
6019fabe24eSDimitris Papastamos 	default:
6029fabe24eSDimitris Papastamos 		BUG();
6039fabe24eSDimitris Papastamos 	}
6049fabe24eSDimitris Papastamos 	return false;
6059fabe24eSDimitris Papastamos }
6069fabe24eSDimitris Papastamos 
607879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base,
608879082c9SMark Brown 			      unsigned int idx)
6099fabe24eSDimitris Papastamos {
6109fabe24eSDimitris Papastamos 	if (!base)
6119fabe24eSDimitris Papastamos 		return -EINVAL;
6129fabe24eSDimitris Papastamos 
613eb4cb76fSMark Brown 	/* Use device native format if possible */
614eb4cb76fSMark Brown 	if (map->format.parse_val)
6158817796bSMark Brown 		return map->format.parse_val(regcache_get_val_addr(map, base,
6168817796bSMark Brown 								   idx));
617eb4cb76fSMark Brown 
618879082c9SMark Brown 	switch (map->cache_word_size) {
6199fabe24eSDimitris Papastamos 	case 1: {
6209fabe24eSDimitris Papastamos 		const u8 *cache = base;
6212fd6902eSXiubo Li 
6229fabe24eSDimitris Papastamos 		return cache[idx];
6239fabe24eSDimitris Papastamos 	}
6249fabe24eSDimitris Papastamos 	case 2: {
6259fabe24eSDimitris Papastamos 		const u16 *cache = base;
6262fd6902eSXiubo Li 
6279fabe24eSDimitris Papastamos 		return cache[idx];
6289fabe24eSDimitris Papastamos 	}
6297d5e525bSMark Brown 	case 4: {
6307d5e525bSMark Brown 		const u32 *cache = base;
6312fd6902eSXiubo Li 
6327d5e525bSMark Brown 		return cache[idx];
6337d5e525bSMark Brown 	}
6348b7663deSXiubo Li #ifdef CONFIG_64BIT
6358b7663deSXiubo Li 	case 8: {
6368b7663deSXiubo Li 		const u64 *cache = base;
6378b7663deSXiubo Li 
6388b7663deSXiubo Li 		return cache[idx];
6398b7663deSXiubo Li 	}
6408b7663deSXiubo Li #endif
6419fabe24eSDimitris Papastamos 	default:
6429fabe24eSDimitris Papastamos 		BUG();
6439fabe24eSDimitris Papastamos 	}
6449fabe24eSDimitris Papastamos 	/* unreachable */
6459fabe24eSDimitris Papastamos 	return -1;
6469fabe24eSDimitris Papastamos }
6479fabe24eSDimitris Papastamos 
648f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b)
649c08604b8SDimitris Papastamos {
650c08604b8SDimitris Papastamos 	const struct reg_default *_a = a;
651c08604b8SDimitris Papastamos 	const struct reg_default *_b = b;
652c08604b8SDimitris Papastamos 
653c08604b8SDimitris Papastamos 	return _a->reg - _b->reg;
654c08604b8SDimitris Papastamos }
655c08604b8SDimitris Papastamos 
656f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg)
657f094fea6SMark Brown {
658f094fea6SMark Brown 	struct reg_default key;
659f094fea6SMark Brown 	struct reg_default *r;
660f094fea6SMark Brown 
661f094fea6SMark Brown 	key.reg = reg;
662f094fea6SMark Brown 	key.def = 0;
663f094fea6SMark Brown 
664f094fea6SMark Brown 	r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
665f094fea6SMark Brown 		    sizeof(struct reg_default), regcache_default_cmp);
666f094fea6SMark Brown 
667f094fea6SMark Brown 	if (r)
668f094fea6SMark Brown 		return r - map->reg_defaults;
669f094fea6SMark Brown 	else
6706e6ace00SMark Brown 		return -ENOENT;
671f094fea6SMark Brown }
672f8bd822cSMark Brown 
6733f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
6743f4ff561SLars-Peter Clausen {
6753f4ff561SLars-Peter Clausen 	if (!cache_present)
6763f4ff561SLars-Peter Clausen 		return true;
6773f4ff561SLars-Peter Clausen 
6783f4ff561SLars-Peter Clausen 	return test_bit(idx, cache_present);
6793f4ff561SLars-Peter Clausen }
6803f4ff561SLars-Peter Clausen 
681cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block,
6823f4ff561SLars-Peter Clausen 				      unsigned long *cache_present,
683cfdeb8c3SMark Brown 				      unsigned int block_base,
684cfdeb8c3SMark Brown 				      unsigned int start, unsigned int end)
685cfdeb8c3SMark Brown {
686cfdeb8c3SMark Brown 	unsigned int i, regtmp, val;
687cfdeb8c3SMark Brown 	int ret;
688cfdeb8c3SMark Brown 
689cfdeb8c3SMark Brown 	for (i = start; i < end; i++) {
690cfdeb8c3SMark Brown 		regtmp = block_base + (i * map->reg_stride);
691cfdeb8c3SMark Brown 
6924ceba98dSTakashi Iwai 		if (!regcache_reg_present(cache_present, i) ||
6934ceba98dSTakashi Iwai 		    !regmap_writeable(map, regtmp))
694cfdeb8c3SMark Brown 			continue;
695cfdeb8c3SMark Brown 
696cfdeb8c3SMark Brown 		val = regcache_get_val(map, block, i);
6973969fa08SKevin Cernekee 		if (!regcache_reg_needs_sync(map, regtmp, val))
698cfdeb8c3SMark Brown 			continue;
699cfdeb8c3SMark Brown 
700621a5f7aSViresh Kumar 		map->cache_bypass = true;
701cfdeb8c3SMark Brown 
702cfdeb8c3SMark Brown 		ret = _regmap_write(map, regtmp, val);
703cfdeb8c3SMark Brown 
704621a5f7aSViresh Kumar 		map->cache_bypass = false;
705f29a4320SJarkko Nikula 		if (ret != 0) {
706f29a4320SJarkko Nikula 			dev_err(map->dev, "Unable to sync register %#x. %d\n",
707f29a4320SJarkko Nikula 				regtmp, ret);
708cfdeb8c3SMark Brown 			return ret;
709f29a4320SJarkko Nikula 		}
710cfdeb8c3SMark Brown 		dev_dbg(map->dev, "Synced register %#x, value %#x\n",
711cfdeb8c3SMark Brown 			regtmp, val);
712cfdeb8c3SMark Brown 	}
713cfdeb8c3SMark Brown 
714cfdeb8c3SMark Brown 	return 0;
715cfdeb8c3SMark Brown }
716cfdeb8c3SMark Brown 
71775a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
71875a5f89fSMark Brown 					 unsigned int base, unsigned int cur)
71975a5f89fSMark Brown {
72075a5f89fSMark Brown 	size_t val_bytes = map->format.val_bytes;
72175a5f89fSMark Brown 	int ret, count;
72275a5f89fSMark Brown 
72375a5f89fSMark Brown 	if (*data == NULL)
72475a5f89fSMark Brown 		return 0;
72575a5f89fSMark Brown 
72678ba73eeSDylan Reid 	count = (cur - base) / map->reg_stride;
72775a5f89fSMark Brown 
7289659293cSStratos Karafotis 	dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
72978ba73eeSDylan Reid 		count * val_bytes, count, base, cur - map->reg_stride);
73075a5f89fSMark Brown 
731621a5f7aSViresh Kumar 	map->cache_bypass = true;
73275a5f89fSMark Brown 
73305669b63SDmitry Baryshkov 	ret = _regmap_raw_write(map, base, *data, count * val_bytes, false);
734f29a4320SJarkko Nikula 	if (ret)
735f29a4320SJarkko Nikula 		dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
736f29a4320SJarkko Nikula 			base, cur - map->reg_stride, ret);
73775a5f89fSMark Brown 
738621a5f7aSViresh Kumar 	map->cache_bypass = false;
73975a5f89fSMark Brown 
74075a5f89fSMark Brown 	*data = NULL;
74175a5f89fSMark Brown 
74275a5f89fSMark Brown 	return ret;
74375a5f89fSMark Brown }
74475a5f89fSMark Brown 
745f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block,
7463f4ff561SLars-Peter Clausen 			    unsigned long *cache_present,
747f8bd822cSMark Brown 			    unsigned int block_base, unsigned int start,
748f8bd822cSMark Brown 			    unsigned int end)
749f8bd822cSMark Brown {
75075a5f89fSMark Brown 	unsigned int i, val;
75175a5f89fSMark Brown 	unsigned int regtmp = 0;
75275a5f89fSMark Brown 	unsigned int base = 0;
75375a5f89fSMark Brown 	const void *data = NULL;
754f8bd822cSMark Brown 	int ret;
755f8bd822cSMark Brown 
756f8bd822cSMark Brown 	for (i = start; i < end; i++) {
757f8bd822cSMark Brown 		regtmp = block_base + (i * map->reg_stride);
758f8bd822cSMark Brown 
7594ceba98dSTakashi Iwai 		if (!regcache_reg_present(cache_present, i) ||
7604ceba98dSTakashi Iwai 		    !regmap_writeable(map, regtmp)) {
76175a5f89fSMark Brown 			ret = regcache_sync_block_raw_flush(map, &data,
76275a5f89fSMark Brown 							    base, regtmp);
76375a5f89fSMark Brown 			if (ret != 0)
76475a5f89fSMark Brown 				return ret;
765f8bd822cSMark Brown 			continue;
76675a5f89fSMark Brown 		}
767f8bd822cSMark Brown 
768f8bd822cSMark Brown 		val = regcache_get_val(map, block, i);
7693969fa08SKevin Cernekee 		if (!regcache_reg_needs_sync(map, regtmp, val)) {
77075a5f89fSMark Brown 			ret = regcache_sync_block_raw_flush(map, &data,
77175a5f89fSMark Brown 							    base, regtmp);
772f8bd822cSMark Brown 			if (ret != 0)
773f8bd822cSMark Brown 				return ret;
77475a5f89fSMark Brown 			continue;
775f8bd822cSMark Brown 		}
776f8bd822cSMark Brown 
77775a5f89fSMark Brown 		if (!data) {
77875a5f89fSMark Brown 			data = regcache_get_val_addr(map, block, i);
77975a5f89fSMark Brown 			base = regtmp;
78075a5f89fSMark Brown 		}
78175a5f89fSMark Brown 	}
78275a5f89fSMark Brown 
7832d49b598SLars-Peter Clausen 	return regcache_sync_block_raw_flush(map, &data, base, regtmp +
7842d49b598SLars-Peter Clausen 			map->reg_stride);
785f8bd822cSMark Brown }
786cfdeb8c3SMark Brown 
787cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block,
7883f4ff561SLars-Peter Clausen 			unsigned long *cache_present,
789cfdeb8c3SMark Brown 			unsigned int block_base, unsigned int start,
790cfdeb8c3SMark Brown 			unsigned int end)
791cfdeb8c3SMark Brown {
79267921a1aSMarkus Pargmann 	if (regmap_can_raw_write(map) && !map->use_single_write)
7933f4ff561SLars-Peter Clausen 		return regcache_sync_block_raw(map, block, cache_present,
7943f4ff561SLars-Peter Clausen 					       block_base, start, end);
795cfdeb8c3SMark Brown 	else
7963f4ff561SLars-Peter Clausen 		return regcache_sync_block_single(map, block, cache_present,
7973f4ff561SLars-Peter Clausen 						  block_base, start, end);
798cfdeb8c3SMark Brown }
799