xref: /openbmc/linux/drivers/base/regmap/regcache.c (revision f58078dacaaf6fbe664d5404dca7f76dce576113)
19fabe24eSDimitris Papastamos /*
29fabe24eSDimitris Papastamos  * Register cache access API
39fabe24eSDimitris Papastamos  *
49fabe24eSDimitris Papastamos  * Copyright 2011 Wolfson Microelectronics plc
59fabe24eSDimitris Papastamos  *
69fabe24eSDimitris Papastamos  * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
79fabe24eSDimitris Papastamos  *
89fabe24eSDimitris Papastamos  * This program is free software; you can redistribute it and/or modify
99fabe24eSDimitris Papastamos  * it under the terms of the GNU General Public License version 2 as
109fabe24eSDimitris Papastamos  * published by the Free Software Foundation.
119fabe24eSDimitris Papastamos  */
129fabe24eSDimitris Papastamos 
13f094fea6SMark Brown #include <linux/bsearch.h>
14e39be3a3SXiubo Li #include <linux/device.h>
15e39be3a3SXiubo Li #include <linux/export.h>
16e39be3a3SXiubo Li #include <linux/slab.h>
17c08604b8SDimitris Papastamos #include <linux/sort.h>
189fabe24eSDimitris Papastamos 
19*f58078daSSteven Rostedt #include "trace.h"
209fabe24eSDimitris Papastamos #include "internal.h"
219fabe24eSDimitris Papastamos 
229fabe24eSDimitris Papastamos static const struct regcache_ops *cache_types[] = {
2328644c80SDimitris Papastamos 	&regcache_rbtree_ops,
242cbbb579SDimitris Papastamos 	&regcache_lzo_ops,
252ac902ceSMark Brown 	&regcache_flat_ops,
269fabe24eSDimitris Papastamos };
279fabe24eSDimitris Papastamos 
289fabe24eSDimitris Papastamos static int regcache_hw_init(struct regmap *map)
299fabe24eSDimitris Papastamos {
309fabe24eSDimitris Papastamos 	int i, j;
319fabe24eSDimitris Papastamos 	int ret;
329fabe24eSDimitris Papastamos 	int count;
339fabe24eSDimitris Papastamos 	unsigned int val;
349fabe24eSDimitris Papastamos 	void *tmp_buf;
359fabe24eSDimitris Papastamos 
369fabe24eSDimitris Papastamos 	if (!map->num_reg_defaults_raw)
379fabe24eSDimitris Papastamos 		return -EINVAL;
389fabe24eSDimitris Papastamos 
39fb70067eSXiubo Li 	/* calculate the size of reg_defaults */
40fb70067eSXiubo Li 	for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++)
41fb70067eSXiubo Li 		if (!regmap_volatile(map, i * map->reg_stride))
42fb70067eSXiubo Li 			count++;
43fb70067eSXiubo Li 
44fb70067eSXiubo Li 	/* all registers are volatile, so just bypass */
45fb70067eSXiubo Li 	if (!count) {
46fb70067eSXiubo Li 		map->cache_bypass = true;
47fb70067eSXiubo Li 		return 0;
48fb70067eSXiubo Li 	}
49fb70067eSXiubo Li 
50fb70067eSXiubo Li 	map->num_reg_defaults = count;
51fb70067eSXiubo Li 	map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default),
52fb70067eSXiubo Li 					  GFP_KERNEL);
53fb70067eSXiubo Li 	if (!map->reg_defaults)
54fb70067eSXiubo Li 		return -ENOMEM;
55fb70067eSXiubo Li 
569fabe24eSDimitris Papastamos 	if (!map->reg_defaults_raw) {
57df00c79fSLaxman Dewangan 		u32 cache_bypass = map->cache_bypass;
589fabe24eSDimitris Papastamos 		dev_warn(map->dev, "No cache defaults, reading back from HW\n");
59df00c79fSLaxman Dewangan 
60df00c79fSLaxman Dewangan 		/* Bypass the cache access till data read from HW*/
61df00c79fSLaxman Dewangan 		map->cache_bypass = 1;
629fabe24eSDimitris Papastamos 		tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
63fb70067eSXiubo Li 		if (!tmp_buf) {
64fb70067eSXiubo Li 			ret = -ENOMEM;
65fb70067eSXiubo Li 			goto err_free;
66fb70067eSXiubo Li 		}
67eb4cb76fSMark Brown 		ret = regmap_raw_read(map, 0, tmp_buf,
689fabe24eSDimitris Papastamos 				      map->num_reg_defaults_raw);
69df00c79fSLaxman Dewangan 		map->cache_bypass = cache_bypass;
70fb70067eSXiubo Li 		if (ret < 0)
71fb70067eSXiubo Li 			goto err_cache_free;
72fb70067eSXiubo Li 
739fabe24eSDimitris Papastamos 		map->reg_defaults_raw = tmp_buf;
749fabe24eSDimitris Papastamos 		map->cache_free = 1;
759fabe24eSDimitris Papastamos 	}
769fabe24eSDimitris Papastamos 
779fabe24eSDimitris Papastamos 	/* fill the reg_defaults */
789fabe24eSDimitris Papastamos 	for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
79f01ee60fSStephen Warren 		if (regmap_volatile(map, i * map->reg_stride))
809fabe24eSDimitris Papastamos 			continue;
81fbba43c5SXiubo Li 		val = regcache_get_val(map, map->reg_defaults_raw, i);
82f01ee60fSStephen Warren 		map->reg_defaults[j].reg = i * map->reg_stride;
839fabe24eSDimitris Papastamos 		map->reg_defaults[j].def = val;
849fabe24eSDimitris Papastamos 		j++;
859fabe24eSDimitris Papastamos 	}
869fabe24eSDimitris Papastamos 
879fabe24eSDimitris Papastamos 	return 0;
88021cd616SLars-Peter Clausen 
89fb70067eSXiubo Li err_cache_free:
90fb70067eSXiubo Li 	kfree(tmp_buf);
91021cd616SLars-Peter Clausen err_free:
92fb70067eSXiubo Li 	kfree(map->reg_defaults);
93021cd616SLars-Peter Clausen 
94021cd616SLars-Peter Clausen 	return ret;
959fabe24eSDimitris Papastamos }
969fabe24eSDimitris Papastamos 
97e5e3b8abSLars-Peter Clausen int regcache_init(struct regmap *map, const struct regmap_config *config)
989fabe24eSDimitris Papastamos {
999fabe24eSDimitris Papastamos 	int ret;
1009fabe24eSDimitris Papastamos 	int i;
1019fabe24eSDimitris Papastamos 	void *tmp_buf;
1029fabe24eSDimitris Papastamos 
103f01ee60fSStephen Warren 	for (i = 0; i < config->num_reg_defaults; i++)
104f01ee60fSStephen Warren 		if (config->reg_defaults[i].reg % map->reg_stride)
105f01ee60fSStephen Warren 			return -EINVAL;
106f01ee60fSStephen Warren 
107e7a6db30SMark Brown 	if (map->cache_type == REGCACHE_NONE) {
108e7a6db30SMark Brown 		map->cache_bypass = true;
1099fabe24eSDimitris Papastamos 		return 0;
110e7a6db30SMark Brown 	}
1119fabe24eSDimitris Papastamos 
1129fabe24eSDimitris Papastamos 	for (i = 0; i < ARRAY_SIZE(cache_types); i++)
1139fabe24eSDimitris Papastamos 		if (cache_types[i]->type == map->cache_type)
1149fabe24eSDimitris Papastamos 			break;
1159fabe24eSDimitris Papastamos 
1169fabe24eSDimitris Papastamos 	if (i == ARRAY_SIZE(cache_types)) {
1179fabe24eSDimitris Papastamos 		dev_err(map->dev, "Could not match compress type: %d\n",
1189fabe24eSDimitris Papastamos 			map->cache_type);
1199fabe24eSDimitris Papastamos 		return -EINVAL;
1209fabe24eSDimitris Papastamos 	}
1219fabe24eSDimitris Papastamos 
122e5e3b8abSLars-Peter Clausen 	map->num_reg_defaults = config->num_reg_defaults;
123e5e3b8abSLars-Peter Clausen 	map->num_reg_defaults_raw = config->num_reg_defaults_raw;
124e5e3b8abSLars-Peter Clausen 	map->reg_defaults_raw = config->reg_defaults_raw;
125064d4db1SLars-Peter Clausen 	map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
126064d4db1SLars-Peter Clausen 	map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
127e5e3b8abSLars-Peter Clausen 
1289fabe24eSDimitris Papastamos 	map->cache = NULL;
1299fabe24eSDimitris Papastamos 	map->cache_ops = cache_types[i];
1309fabe24eSDimitris Papastamos 
1319fabe24eSDimitris Papastamos 	if (!map->cache_ops->read ||
1329fabe24eSDimitris Papastamos 	    !map->cache_ops->write ||
1339fabe24eSDimitris Papastamos 	    !map->cache_ops->name)
1349fabe24eSDimitris Papastamos 		return -EINVAL;
1359fabe24eSDimitris Papastamos 
1369fabe24eSDimitris Papastamos 	/* We still need to ensure that the reg_defaults
1379fabe24eSDimitris Papastamos 	 * won't vanish from under us.  We'll need to make
1389fabe24eSDimitris Papastamos 	 * a copy of it.
1399fabe24eSDimitris Papastamos 	 */
140720e4616SLars-Peter Clausen 	if (config->reg_defaults) {
1419fabe24eSDimitris Papastamos 		if (!map->num_reg_defaults)
1429fabe24eSDimitris Papastamos 			return -EINVAL;
143720e4616SLars-Peter Clausen 		tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
1449fabe24eSDimitris Papastamos 				  sizeof(struct reg_default), GFP_KERNEL);
1459fabe24eSDimitris Papastamos 		if (!tmp_buf)
1469fabe24eSDimitris Papastamos 			return -ENOMEM;
1479fabe24eSDimitris Papastamos 		map->reg_defaults = tmp_buf;
1488528bdd4SMark Brown 	} else if (map->num_reg_defaults_raw) {
1495fcd2560SMark Brown 		/* Some devices such as PMICs don't have cache defaults,
1509fabe24eSDimitris Papastamos 		 * we cope with this by reading back the HW registers and
1519fabe24eSDimitris Papastamos 		 * crafting the cache defaults by hand.
1529fabe24eSDimitris Papastamos 		 */
1539fabe24eSDimitris Papastamos 		ret = regcache_hw_init(map);
1549fabe24eSDimitris Papastamos 		if (ret < 0)
1559fabe24eSDimitris Papastamos 			return ret;
156fb70067eSXiubo Li 		if (map->cache_bypass)
157fb70067eSXiubo Li 			return 0;
1589fabe24eSDimitris Papastamos 	}
1599fabe24eSDimitris Papastamos 
1609fabe24eSDimitris Papastamos 	if (!map->max_register)
1619fabe24eSDimitris Papastamos 		map->max_register = map->num_reg_defaults_raw;
1629fabe24eSDimitris Papastamos 
1639fabe24eSDimitris Papastamos 	if (map->cache_ops->init) {
1649fabe24eSDimitris Papastamos 		dev_dbg(map->dev, "Initializing %s cache\n",
1659fabe24eSDimitris Papastamos 			map->cache_ops->name);
166bd061c78SLars-Peter Clausen 		ret = map->cache_ops->init(map);
167bd061c78SLars-Peter Clausen 		if (ret)
168bd061c78SLars-Peter Clausen 			goto err_free;
1699fabe24eSDimitris Papastamos 	}
1709fabe24eSDimitris Papastamos 	return 0;
171bd061c78SLars-Peter Clausen 
172bd061c78SLars-Peter Clausen err_free:
173bd061c78SLars-Peter Clausen 	kfree(map->reg_defaults);
174bd061c78SLars-Peter Clausen 	if (map->cache_free)
175bd061c78SLars-Peter Clausen 		kfree(map->reg_defaults_raw);
176bd061c78SLars-Peter Clausen 
177bd061c78SLars-Peter Clausen 	return ret;
1789fabe24eSDimitris Papastamos }
1799fabe24eSDimitris Papastamos 
1809fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map)
1819fabe24eSDimitris Papastamos {
1829fabe24eSDimitris Papastamos 	if (map->cache_type == REGCACHE_NONE)
1839fabe24eSDimitris Papastamos 		return;
1849fabe24eSDimitris Papastamos 
1859fabe24eSDimitris Papastamos 	BUG_ON(!map->cache_ops);
1869fabe24eSDimitris Papastamos 
1879fabe24eSDimitris Papastamos 	kfree(map->reg_defaults);
1889fabe24eSDimitris Papastamos 	if (map->cache_free)
1899fabe24eSDimitris Papastamos 		kfree(map->reg_defaults_raw);
1909fabe24eSDimitris Papastamos 
1919fabe24eSDimitris Papastamos 	if (map->cache_ops->exit) {
1929fabe24eSDimitris Papastamos 		dev_dbg(map->dev, "Destroying %s cache\n",
1939fabe24eSDimitris Papastamos 			map->cache_ops->name);
1949fabe24eSDimitris Papastamos 		map->cache_ops->exit(map);
1959fabe24eSDimitris Papastamos 	}
1969fabe24eSDimitris Papastamos }
1979fabe24eSDimitris Papastamos 
1989fabe24eSDimitris Papastamos /**
1999fabe24eSDimitris Papastamos  * regcache_read: Fetch the value of a given register from the cache.
2009fabe24eSDimitris Papastamos  *
2019fabe24eSDimitris Papastamos  * @map: map to configure.
2029fabe24eSDimitris Papastamos  * @reg: The register index.
2039fabe24eSDimitris Papastamos  * @value: The value to be returned.
2049fabe24eSDimitris Papastamos  *
2059fabe24eSDimitris Papastamos  * Return a negative value on failure, 0 on success.
2069fabe24eSDimitris Papastamos  */
2079fabe24eSDimitris Papastamos int regcache_read(struct regmap *map,
2089fabe24eSDimitris Papastamos 		  unsigned int reg, unsigned int *value)
2099fabe24eSDimitris Papastamos {
210bc7ee556SMark Brown 	int ret;
211bc7ee556SMark Brown 
2129fabe24eSDimitris Papastamos 	if (map->cache_type == REGCACHE_NONE)
2139fabe24eSDimitris Papastamos 		return -ENOSYS;
2149fabe24eSDimitris Papastamos 
2159fabe24eSDimitris Papastamos 	BUG_ON(!map->cache_ops);
2169fabe24eSDimitris Papastamos 
217bc7ee556SMark Brown 	if (!regmap_volatile(map, reg)) {
218bc7ee556SMark Brown 		ret = map->cache_ops->read(map, reg, value);
219bc7ee556SMark Brown 
220bc7ee556SMark Brown 		if (ret == 0)
221c6b570d9SPhilipp Zabel 			trace_regmap_reg_read_cache(map, reg, *value);
222bc7ee556SMark Brown 
223bc7ee556SMark Brown 		return ret;
224bc7ee556SMark Brown 	}
2259fabe24eSDimitris Papastamos 
2269fabe24eSDimitris Papastamos 	return -EINVAL;
2279fabe24eSDimitris Papastamos }
2289fabe24eSDimitris Papastamos 
2299fabe24eSDimitris Papastamos /**
2309fabe24eSDimitris Papastamos  * regcache_write: Set the value of a given register in the cache.
2319fabe24eSDimitris Papastamos  *
2329fabe24eSDimitris Papastamos  * @map: map to configure.
2339fabe24eSDimitris Papastamos  * @reg: The register index.
2349fabe24eSDimitris Papastamos  * @value: The new register value.
2359fabe24eSDimitris Papastamos  *
2369fabe24eSDimitris Papastamos  * Return a negative value on failure, 0 on success.
2379fabe24eSDimitris Papastamos  */
2389fabe24eSDimitris Papastamos int regcache_write(struct regmap *map,
2399fabe24eSDimitris Papastamos 		   unsigned int reg, unsigned int value)
2409fabe24eSDimitris Papastamos {
2419fabe24eSDimitris Papastamos 	if (map->cache_type == REGCACHE_NONE)
2429fabe24eSDimitris Papastamos 		return 0;
2439fabe24eSDimitris Papastamos 
2449fabe24eSDimitris Papastamos 	BUG_ON(!map->cache_ops);
2459fabe24eSDimitris Papastamos 
2469fabe24eSDimitris Papastamos 	if (!regmap_volatile(map, reg))
2479fabe24eSDimitris Papastamos 		return map->cache_ops->write(map, reg, value);
2489fabe24eSDimitris Papastamos 
2499fabe24eSDimitris Papastamos 	return 0;
2509fabe24eSDimitris Papastamos }
2519fabe24eSDimitris Papastamos 
252d856fce4SMaarten ter Huurne static int regcache_default_sync(struct regmap *map, unsigned int min,
253d856fce4SMaarten ter Huurne 				 unsigned int max)
254d856fce4SMaarten ter Huurne {
255d856fce4SMaarten ter Huurne 	unsigned int reg;
256d856fce4SMaarten ter Huurne 
25775617328SDylan Reid 	for (reg = min; reg <= max; reg += map->reg_stride) {
258d856fce4SMaarten ter Huurne 		unsigned int val;
259d856fce4SMaarten ter Huurne 		int ret;
260d856fce4SMaarten ter Huurne 
26183f8475cSDylan Reid 		if (regmap_volatile(map, reg) ||
26283f8475cSDylan Reid 		    !regmap_writeable(map, reg))
263d856fce4SMaarten ter Huurne 			continue;
264d856fce4SMaarten ter Huurne 
265d856fce4SMaarten ter Huurne 		ret = regcache_read(map, reg, &val);
266d856fce4SMaarten ter Huurne 		if (ret)
267d856fce4SMaarten ter Huurne 			return ret;
268d856fce4SMaarten ter Huurne 
269d856fce4SMaarten ter Huurne 		/* Is this the hardware default?  If so skip. */
270d856fce4SMaarten ter Huurne 		ret = regcache_lookup_reg(map, reg);
271d856fce4SMaarten ter Huurne 		if (ret >= 0 && val == map->reg_defaults[ret].def)
272d856fce4SMaarten ter Huurne 			continue;
273d856fce4SMaarten ter Huurne 
274d856fce4SMaarten ter Huurne 		map->cache_bypass = 1;
275d856fce4SMaarten ter Huurne 		ret = _regmap_write(map, reg, val);
276d856fce4SMaarten ter Huurne 		map->cache_bypass = 0;
277f29a4320SJarkko Nikula 		if (ret) {
278f29a4320SJarkko Nikula 			dev_err(map->dev, "Unable to sync register %#x. %d\n",
279f29a4320SJarkko Nikula 				reg, ret);
280d856fce4SMaarten ter Huurne 			return ret;
281f29a4320SJarkko Nikula 		}
282d856fce4SMaarten ter Huurne 		dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
283d856fce4SMaarten ter Huurne 	}
284d856fce4SMaarten ter Huurne 
285d856fce4SMaarten ter Huurne 	return 0;
286d856fce4SMaarten ter Huurne }
287d856fce4SMaarten ter Huurne 
2889fabe24eSDimitris Papastamos /**
2899fabe24eSDimitris Papastamos  * regcache_sync: Sync the register cache with the hardware.
2909fabe24eSDimitris Papastamos  *
2919fabe24eSDimitris Papastamos  * @map: map to configure.
2929fabe24eSDimitris Papastamos  *
2939fabe24eSDimitris Papastamos  * Any registers that should not be synced should be marked as
2949fabe24eSDimitris Papastamos  * volatile.  In general drivers can choose not to use the provided
2959fabe24eSDimitris Papastamos  * syncing functionality if they so require.
2969fabe24eSDimitris Papastamos  *
2979fabe24eSDimitris Papastamos  * Return a negative value on failure, 0 on success.
2989fabe24eSDimitris Papastamos  */
2999fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map)
3009fabe24eSDimitris Papastamos {
301954757d7SDimitris Papastamos 	int ret = 0;
302954757d7SDimitris Papastamos 	unsigned int i;
30359360089SDimitris Papastamos 	const char *name;
304beb1a10fSDimitris Papastamos 	unsigned int bypass;
30559360089SDimitris Papastamos 
306d856fce4SMaarten ter Huurne 	BUG_ON(!map->cache_ops);
3079fabe24eSDimitris Papastamos 
30881485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
309beb1a10fSDimitris Papastamos 	/* Remember the initial bypass state */
310beb1a10fSDimitris Papastamos 	bypass = map->cache_bypass;
3119fabe24eSDimitris Papastamos 	dev_dbg(map->dev, "Syncing %s cache\n",
3129fabe24eSDimitris Papastamos 		map->cache_ops->name);
31359360089SDimitris Papastamos 	name = map->cache_ops->name;
314c6b570d9SPhilipp Zabel 	trace_regcache_sync(map, name, "start");
31522f0d90aSMark Brown 
3168ae0d7e8SMark Brown 	if (!map->cache_dirty)
3178ae0d7e8SMark Brown 		goto out;
318d9db7627SMark Brown 
319affbe886SMark Brown 	map->async = true;
320affbe886SMark Brown 
32122f0d90aSMark Brown 	/* Apply any patch first */
3228a892d69SMark Brown 	map->cache_bypass = 1;
32322f0d90aSMark Brown 	for (i = 0; i < map->patch_regs; i++) {
32422f0d90aSMark Brown 		ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
32522f0d90aSMark Brown 		if (ret != 0) {
32622f0d90aSMark Brown 			dev_err(map->dev, "Failed to write %x = %x: %d\n",
32722f0d90aSMark Brown 				map->patch[i].reg, map->patch[i].def, ret);
32822f0d90aSMark Brown 			goto out;
32922f0d90aSMark Brown 		}
33022f0d90aSMark Brown 	}
3318a892d69SMark Brown 	map->cache_bypass = 0;
33222f0d90aSMark Brown 
333d856fce4SMaarten ter Huurne 	if (map->cache_ops->sync)
334ac8d91c8SMark Brown 		ret = map->cache_ops->sync(map, 0, map->max_register);
335d856fce4SMaarten ter Huurne 	else
336d856fce4SMaarten ter Huurne 		ret = regcache_default_sync(map, 0, map->max_register);
337954757d7SDimitris Papastamos 
3386ff73738SMark Brown 	if (ret == 0)
3396ff73738SMark Brown 		map->cache_dirty = false;
3406ff73738SMark Brown 
341954757d7SDimitris Papastamos out:
342beb1a10fSDimitris Papastamos 	/* Restore the bypass state */
343affbe886SMark Brown 	map->async = false;
344beb1a10fSDimitris Papastamos 	map->cache_bypass = bypass;
34581485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
346954757d7SDimitris Papastamos 
347affbe886SMark Brown 	regmap_async_complete(map);
348affbe886SMark Brown 
349c6b570d9SPhilipp Zabel 	trace_regcache_sync(map, name, "stop");
350affbe886SMark Brown 
351954757d7SDimitris Papastamos 	return ret;
3529fabe24eSDimitris Papastamos }
3539fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync);
3549fabe24eSDimitris Papastamos 
35592afb286SMark Brown /**
3564d4cfd16SMark Brown  * regcache_sync_region: Sync part  of the register cache with the hardware.
3574d4cfd16SMark Brown  *
3584d4cfd16SMark Brown  * @map: map to sync.
3594d4cfd16SMark Brown  * @min: first register to sync
3604d4cfd16SMark Brown  * @max: last register to sync
3614d4cfd16SMark Brown  *
3624d4cfd16SMark Brown  * Write all non-default register values in the specified region to
3634d4cfd16SMark Brown  * the hardware.
3644d4cfd16SMark Brown  *
3654d4cfd16SMark Brown  * Return a negative value on failure, 0 on success.
3664d4cfd16SMark Brown  */
3674d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min,
3684d4cfd16SMark Brown 			 unsigned int max)
3694d4cfd16SMark Brown {
3704d4cfd16SMark Brown 	int ret = 0;
3714d4cfd16SMark Brown 	const char *name;
3724d4cfd16SMark Brown 	unsigned int bypass;
3734d4cfd16SMark Brown 
374d856fce4SMaarten ter Huurne 	BUG_ON(!map->cache_ops);
3754d4cfd16SMark Brown 
37681485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
3774d4cfd16SMark Brown 
3784d4cfd16SMark Brown 	/* Remember the initial bypass state */
3794d4cfd16SMark Brown 	bypass = map->cache_bypass;
3804d4cfd16SMark Brown 
3814d4cfd16SMark Brown 	name = map->cache_ops->name;
3824d4cfd16SMark Brown 	dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
3834d4cfd16SMark Brown 
384c6b570d9SPhilipp Zabel 	trace_regcache_sync(map, name, "start region");
3854d4cfd16SMark Brown 
3864d4cfd16SMark Brown 	if (!map->cache_dirty)
3874d4cfd16SMark Brown 		goto out;
3884d4cfd16SMark Brown 
389affbe886SMark Brown 	map->async = true;
390affbe886SMark Brown 
391d856fce4SMaarten ter Huurne 	if (map->cache_ops->sync)
3924d4cfd16SMark Brown 		ret = map->cache_ops->sync(map, min, max);
393d856fce4SMaarten ter Huurne 	else
394d856fce4SMaarten ter Huurne 		ret = regcache_default_sync(map, min, max);
3954d4cfd16SMark Brown 
3964d4cfd16SMark Brown out:
3974d4cfd16SMark Brown 	/* Restore the bypass state */
3984d4cfd16SMark Brown 	map->cache_bypass = bypass;
399affbe886SMark Brown 	map->async = false;
40081485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
4014d4cfd16SMark Brown 
402affbe886SMark Brown 	regmap_async_complete(map);
403affbe886SMark Brown 
404c6b570d9SPhilipp Zabel 	trace_regcache_sync(map, name, "stop region");
405affbe886SMark Brown 
4064d4cfd16SMark Brown 	return ret;
4074d4cfd16SMark Brown }
408e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region);
4094d4cfd16SMark Brown 
4104d4cfd16SMark Brown /**
411697e85bcSMark Brown  * regcache_drop_region: Discard part of the register cache
412697e85bcSMark Brown  *
413697e85bcSMark Brown  * @map: map to operate on
414697e85bcSMark Brown  * @min: first register to discard
415697e85bcSMark Brown  * @max: last register to discard
416697e85bcSMark Brown  *
417697e85bcSMark Brown  * Discard part of the register cache.
418697e85bcSMark Brown  *
419697e85bcSMark Brown  * Return a negative value on failure, 0 on success.
420697e85bcSMark Brown  */
421697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min,
422697e85bcSMark Brown 			 unsigned int max)
423697e85bcSMark Brown {
424697e85bcSMark Brown 	int ret = 0;
425697e85bcSMark Brown 
4263f4ff561SLars-Peter Clausen 	if (!map->cache_ops || !map->cache_ops->drop)
427697e85bcSMark Brown 		return -EINVAL;
428697e85bcSMark Brown 
42981485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
430697e85bcSMark Brown 
431c6b570d9SPhilipp Zabel 	trace_regcache_drop_region(map, min, max);
432697e85bcSMark Brown 
433697e85bcSMark Brown 	ret = map->cache_ops->drop(map, min, max);
434697e85bcSMark Brown 
43581485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
436697e85bcSMark Brown 
437697e85bcSMark Brown 	return ret;
438697e85bcSMark Brown }
439697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region);
440697e85bcSMark Brown 
441697e85bcSMark Brown /**
44292afb286SMark Brown  * regcache_cache_only: Put a register map into cache only mode
44392afb286SMark Brown  *
44492afb286SMark Brown  * @map: map to configure
44592afb286SMark Brown  * @cache_only: flag if changes should be written to the hardware
44692afb286SMark Brown  *
44792afb286SMark Brown  * When a register map is marked as cache only writes to the register
44892afb286SMark Brown  * map API will only update the register cache, they will not cause
44992afb286SMark Brown  * any hardware changes.  This is useful for allowing portions of
45092afb286SMark Brown  * drivers to act as though the device were functioning as normal when
45192afb286SMark Brown  * it is disabled for power saving reasons.
45292afb286SMark Brown  */
45392afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable)
45492afb286SMark Brown {
45581485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
456ac77a765SDimitris Papastamos 	WARN_ON(map->cache_bypass && enable);
45792afb286SMark Brown 	map->cache_only = enable;
458c6b570d9SPhilipp Zabel 	trace_regmap_cache_only(map, enable);
45981485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
46092afb286SMark Brown }
46192afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only);
46292afb286SMark Brown 
4636eb0f5e0SDimitris Papastamos /**
4648ae0d7e8SMark Brown  * regcache_mark_dirty: Mark the register cache as dirty
4658ae0d7e8SMark Brown  *
4668ae0d7e8SMark Brown  * @map: map to mark
4678ae0d7e8SMark Brown  *
4688ae0d7e8SMark Brown  * Mark the register cache as dirty, for example due to the device
4698ae0d7e8SMark Brown  * having been powered down for suspend.  If the cache is not marked
4708ae0d7e8SMark Brown  * as dirty then the cache sync will be suppressed.
4718ae0d7e8SMark Brown  */
4728ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map)
4738ae0d7e8SMark Brown {
47481485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
4758ae0d7e8SMark Brown 	map->cache_dirty = true;
47681485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
4778ae0d7e8SMark Brown }
4788ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty);
4798ae0d7e8SMark Brown 
4808ae0d7e8SMark Brown /**
4816eb0f5e0SDimitris Papastamos  * regcache_cache_bypass: Put a register map into cache bypass mode
4826eb0f5e0SDimitris Papastamos  *
4836eb0f5e0SDimitris Papastamos  * @map: map to configure
4840eef6b04SDimitris Papastamos  * @cache_bypass: flag if changes should not be written to the hardware
4856eb0f5e0SDimitris Papastamos  *
4866eb0f5e0SDimitris Papastamos  * When a register map is marked with the cache bypass option, writes
4876eb0f5e0SDimitris Papastamos  * to the register map API will only update the hardware and not the
4886eb0f5e0SDimitris Papastamos  * the cache directly.  This is useful when syncing the cache back to
4896eb0f5e0SDimitris Papastamos  * the hardware.
4906eb0f5e0SDimitris Papastamos  */
4916eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable)
4926eb0f5e0SDimitris Papastamos {
49381485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
494ac77a765SDimitris Papastamos 	WARN_ON(map->cache_only && enable);
4956eb0f5e0SDimitris Papastamos 	map->cache_bypass = enable;
496c6b570d9SPhilipp Zabel 	trace_regmap_cache_bypass(map, enable);
49781485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
4986eb0f5e0SDimitris Papastamos }
4996eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass);
5006eb0f5e0SDimitris Papastamos 
501879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
502879082c9SMark Brown 		      unsigned int val)
5039fabe24eSDimitris Papastamos {
504325acab4SMark Brown 	if (regcache_get_val(map, base, idx) == val)
505325acab4SMark Brown 		return true;
506325acab4SMark Brown 
507eb4cb76fSMark Brown 	/* Use device native format if possible */
508eb4cb76fSMark Brown 	if (map->format.format_val) {
509eb4cb76fSMark Brown 		map->format.format_val(base + (map->cache_word_size * idx),
510eb4cb76fSMark Brown 				       val, 0);
511eb4cb76fSMark Brown 		return false;
512eb4cb76fSMark Brown 	}
513eb4cb76fSMark Brown 
514879082c9SMark Brown 	switch (map->cache_word_size) {
5159fabe24eSDimitris Papastamos 	case 1: {
5169fabe24eSDimitris Papastamos 		u8 *cache = base;
5179fabe24eSDimitris Papastamos 		cache[idx] = val;
5189fabe24eSDimitris Papastamos 		break;
5199fabe24eSDimitris Papastamos 	}
5209fabe24eSDimitris Papastamos 	case 2: {
5219fabe24eSDimitris Papastamos 		u16 *cache = base;
5229fabe24eSDimitris Papastamos 		cache[idx] = val;
5239fabe24eSDimitris Papastamos 		break;
5249fabe24eSDimitris Papastamos 	}
5257d5e525bSMark Brown 	case 4: {
5267d5e525bSMark Brown 		u32 *cache = base;
5277d5e525bSMark Brown 		cache[idx] = val;
5287d5e525bSMark Brown 		break;
5297d5e525bSMark Brown 	}
5309fabe24eSDimitris Papastamos 	default:
5319fabe24eSDimitris Papastamos 		BUG();
5329fabe24eSDimitris Papastamos 	}
5339fabe24eSDimitris Papastamos 	return false;
5349fabe24eSDimitris Papastamos }
5359fabe24eSDimitris Papastamos 
536879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base,
537879082c9SMark Brown 			      unsigned int idx)
5389fabe24eSDimitris Papastamos {
5399fabe24eSDimitris Papastamos 	if (!base)
5409fabe24eSDimitris Papastamos 		return -EINVAL;
5419fabe24eSDimitris Papastamos 
542eb4cb76fSMark Brown 	/* Use device native format if possible */
543eb4cb76fSMark Brown 	if (map->format.parse_val)
5448817796bSMark Brown 		return map->format.parse_val(regcache_get_val_addr(map, base,
5458817796bSMark Brown 								   idx));
546eb4cb76fSMark Brown 
547879082c9SMark Brown 	switch (map->cache_word_size) {
5489fabe24eSDimitris Papastamos 	case 1: {
5499fabe24eSDimitris Papastamos 		const u8 *cache = base;
5509fabe24eSDimitris Papastamos 		return cache[idx];
5519fabe24eSDimitris Papastamos 	}
5529fabe24eSDimitris Papastamos 	case 2: {
5539fabe24eSDimitris Papastamos 		const u16 *cache = base;
5549fabe24eSDimitris Papastamos 		return cache[idx];
5559fabe24eSDimitris Papastamos 	}
5567d5e525bSMark Brown 	case 4: {
5577d5e525bSMark Brown 		const u32 *cache = base;
5587d5e525bSMark Brown 		return cache[idx];
5597d5e525bSMark Brown 	}
5609fabe24eSDimitris Papastamos 	default:
5619fabe24eSDimitris Papastamos 		BUG();
5629fabe24eSDimitris Papastamos 	}
5639fabe24eSDimitris Papastamos 	/* unreachable */
5649fabe24eSDimitris Papastamos 	return -1;
5659fabe24eSDimitris Papastamos }
5669fabe24eSDimitris Papastamos 
567f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b)
568c08604b8SDimitris Papastamos {
569c08604b8SDimitris Papastamos 	const struct reg_default *_a = a;
570c08604b8SDimitris Papastamos 	const struct reg_default *_b = b;
571c08604b8SDimitris Papastamos 
572c08604b8SDimitris Papastamos 	return _a->reg - _b->reg;
573c08604b8SDimitris Papastamos }
574c08604b8SDimitris Papastamos 
575f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg)
576f094fea6SMark Brown {
577f094fea6SMark Brown 	struct reg_default key;
578f094fea6SMark Brown 	struct reg_default *r;
579f094fea6SMark Brown 
580f094fea6SMark Brown 	key.reg = reg;
581f094fea6SMark Brown 	key.def = 0;
582f094fea6SMark Brown 
583f094fea6SMark Brown 	r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
584f094fea6SMark Brown 		    sizeof(struct reg_default), regcache_default_cmp);
585f094fea6SMark Brown 
586f094fea6SMark Brown 	if (r)
587f094fea6SMark Brown 		return r - map->reg_defaults;
588f094fea6SMark Brown 	else
5896e6ace00SMark Brown 		return -ENOENT;
590f094fea6SMark Brown }
591f8bd822cSMark Brown 
5923f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
5933f4ff561SLars-Peter Clausen {
5943f4ff561SLars-Peter Clausen 	if (!cache_present)
5953f4ff561SLars-Peter Clausen 		return true;
5963f4ff561SLars-Peter Clausen 
5973f4ff561SLars-Peter Clausen 	return test_bit(idx, cache_present);
5983f4ff561SLars-Peter Clausen }
5993f4ff561SLars-Peter Clausen 
600cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block,
6013f4ff561SLars-Peter Clausen 				      unsigned long *cache_present,
602cfdeb8c3SMark Brown 				      unsigned int block_base,
603cfdeb8c3SMark Brown 				      unsigned int start, unsigned int end)
604cfdeb8c3SMark Brown {
605cfdeb8c3SMark Brown 	unsigned int i, regtmp, val;
606cfdeb8c3SMark Brown 	int ret;
607cfdeb8c3SMark Brown 
608cfdeb8c3SMark Brown 	for (i = start; i < end; i++) {
609cfdeb8c3SMark Brown 		regtmp = block_base + (i * map->reg_stride);
610cfdeb8c3SMark Brown 
6113f4ff561SLars-Peter Clausen 		if (!regcache_reg_present(cache_present, i))
612cfdeb8c3SMark Brown 			continue;
613cfdeb8c3SMark Brown 
614cfdeb8c3SMark Brown 		val = regcache_get_val(map, block, i);
615cfdeb8c3SMark Brown 
616cfdeb8c3SMark Brown 		/* Is this the hardware default?  If so skip. */
617cfdeb8c3SMark Brown 		ret = regcache_lookup_reg(map, regtmp);
618cfdeb8c3SMark Brown 		if (ret >= 0 && val == map->reg_defaults[ret].def)
619cfdeb8c3SMark Brown 			continue;
620cfdeb8c3SMark Brown 
621cfdeb8c3SMark Brown 		map->cache_bypass = 1;
622cfdeb8c3SMark Brown 
623cfdeb8c3SMark Brown 		ret = _regmap_write(map, regtmp, val);
624cfdeb8c3SMark Brown 
625cfdeb8c3SMark Brown 		map->cache_bypass = 0;
626f29a4320SJarkko Nikula 		if (ret != 0) {
627f29a4320SJarkko Nikula 			dev_err(map->dev, "Unable to sync register %#x. %d\n",
628f29a4320SJarkko Nikula 				regtmp, ret);
629cfdeb8c3SMark Brown 			return ret;
630f29a4320SJarkko Nikula 		}
631cfdeb8c3SMark Brown 		dev_dbg(map->dev, "Synced register %#x, value %#x\n",
632cfdeb8c3SMark Brown 			regtmp, val);
633cfdeb8c3SMark Brown 	}
634cfdeb8c3SMark Brown 
635cfdeb8c3SMark Brown 	return 0;
636cfdeb8c3SMark Brown }
637cfdeb8c3SMark Brown 
63875a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
63975a5f89fSMark Brown 					 unsigned int base, unsigned int cur)
64075a5f89fSMark Brown {
64175a5f89fSMark Brown 	size_t val_bytes = map->format.val_bytes;
64275a5f89fSMark Brown 	int ret, count;
64375a5f89fSMark Brown 
64475a5f89fSMark Brown 	if (*data == NULL)
64575a5f89fSMark Brown 		return 0;
64675a5f89fSMark Brown 
64778ba73eeSDylan Reid 	count = (cur - base) / map->reg_stride;
64875a5f89fSMark Brown 
6499659293cSStratos Karafotis 	dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
65078ba73eeSDylan Reid 		count * val_bytes, count, base, cur - map->reg_stride);
65175a5f89fSMark Brown 
65275a5f89fSMark Brown 	map->cache_bypass = 1;
65375a5f89fSMark Brown 
6540a819809SMark Brown 	ret = _regmap_raw_write(map, base, *data, count * val_bytes);
655f29a4320SJarkko Nikula 	if (ret)
656f29a4320SJarkko Nikula 		dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
657f29a4320SJarkko Nikula 			base, cur - map->reg_stride, ret);
65875a5f89fSMark Brown 
65975a5f89fSMark Brown 	map->cache_bypass = 0;
66075a5f89fSMark Brown 
66175a5f89fSMark Brown 	*data = NULL;
66275a5f89fSMark Brown 
66375a5f89fSMark Brown 	return ret;
66475a5f89fSMark Brown }
66575a5f89fSMark Brown 
666f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block,
6673f4ff561SLars-Peter Clausen 			    unsigned long *cache_present,
668f8bd822cSMark Brown 			    unsigned int block_base, unsigned int start,
669f8bd822cSMark Brown 			    unsigned int end)
670f8bd822cSMark Brown {
67175a5f89fSMark Brown 	unsigned int i, val;
67275a5f89fSMark Brown 	unsigned int regtmp = 0;
67375a5f89fSMark Brown 	unsigned int base = 0;
67475a5f89fSMark Brown 	const void *data = NULL;
675f8bd822cSMark Brown 	int ret;
676f8bd822cSMark Brown 
677f8bd822cSMark Brown 	for (i = start; i < end; i++) {
678f8bd822cSMark Brown 		regtmp = block_base + (i * map->reg_stride);
679f8bd822cSMark Brown 
6803f4ff561SLars-Peter Clausen 		if (!regcache_reg_present(cache_present, i)) {
68175a5f89fSMark Brown 			ret = regcache_sync_block_raw_flush(map, &data,
68275a5f89fSMark Brown 							    base, regtmp);
68375a5f89fSMark Brown 			if (ret != 0)
68475a5f89fSMark Brown 				return ret;
685f8bd822cSMark Brown 			continue;
68675a5f89fSMark Brown 		}
687f8bd822cSMark Brown 
688f8bd822cSMark Brown 		val = regcache_get_val(map, block, i);
689f8bd822cSMark Brown 
690f8bd822cSMark Brown 		/* Is this the hardware default?  If so skip. */
691f8bd822cSMark Brown 		ret = regcache_lookup_reg(map, regtmp);
69275a5f89fSMark Brown 		if (ret >= 0 && val == map->reg_defaults[ret].def) {
69375a5f89fSMark Brown 			ret = regcache_sync_block_raw_flush(map, &data,
69475a5f89fSMark Brown 							    base, regtmp);
695f8bd822cSMark Brown 			if (ret != 0)
696f8bd822cSMark Brown 				return ret;
69775a5f89fSMark Brown 			continue;
698f8bd822cSMark Brown 		}
699f8bd822cSMark Brown 
70075a5f89fSMark Brown 		if (!data) {
70175a5f89fSMark Brown 			data = regcache_get_val_addr(map, block, i);
70275a5f89fSMark Brown 			base = regtmp;
70375a5f89fSMark Brown 		}
70475a5f89fSMark Brown 	}
70575a5f89fSMark Brown 
7062d49b598SLars-Peter Clausen 	return regcache_sync_block_raw_flush(map, &data, base, regtmp +
7072d49b598SLars-Peter Clausen 			map->reg_stride);
708f8bd822cSMark Brown }
709cfdeb8c3SMark Brown 
710cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block,
7113f4ff561SLars-Peter Clausen 			unsigned long *cache_present,
712cfdeb8c3SMark Brown 			unsigned int block_base, unsigned int start,
713cfdeb8c3SMark Brown 			unsigned int end)
714cfdeb8c3SMark Brown {
7155c1ebe7fSMark Brown 	if (regmap_can_raw_write(map) && !map->use_single_rw)
7163f4ff561SLars-Peter Clausen 		return regcache_sync_block_raw(map, block, cache_present,
7173f4ff561SLars-Peter Clausen 					       block_base, start, end);
718cfdeb8c3SMark Brown 	else
7193f4ff561SLars-Peter Clausen 		return regcache_sync_block_single(map, block, cache_present,
7203f4ff561SLars-Peter Clausen 						  block_base, start, end);
721cfdeb8c3SMark Brown }
722