xref: /openbmc/linux/drivers/base/regmap/regcache.c (revision 0a8198094da895c8d5db95812fe9de7027d808e4)
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 
139fabe24eSDimitris Papastamos #include <linux/slab.h>
141b6bc32fSPaul Gortmaker #include <linux/export.h>
1551990e82SPaul Gortmaker #include <linux/device.h>
169fabe24eSDimitris Papastamos #include <trace/events/regmap.h>
17f094fea6SMark Brown #include <linux/bsearch.h>
18c08604b8SDimitris Papastamos #include <linux/sort.h>
199fabe24eSDimitris Papastamos 
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 
399fabe24eSDimitris Papastamos 	if (!map->reg_defaults_raw) {
40df00c79fSLaxman Dewangan 		u32 cache_bypass = map->cache_bypass;
419fabe24eSDimitris Papastamos 		dev_warn(map->dev, "No cache defaults, reading back from HW\n");
42df00c79fSLaxman Dewangan 
43df00c79fSLaxman Dewangan 		/* Bypass the cache access till data read from HW*/
44df00c79fSLaxman Dewangan 		map->cache_bypass = 1;
459fabe24eSDimitris Papastamos 		tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
469fabe24eSDimitris Papastamos 		if (!tmp_buf)
479fabe24eSDimitris Papastamos 			return -EINVAL;
48eb4cb76fSMark Brown 		ret = regmap_raw_read(map, 0, tmp_buf,
499fabe24eSDimitris Papastamos 				      map->num_reg_defaults_raw);
50df00c79fSLaxman Dewangan 		map->cache_bypass = cache_bypass;
519fabe24eSDimitris Papastamos 		if (ret < 0) {
529fabe24eSDimitris Papastamos 			kfree(tmp_buf);
539fabe24eSDimitris Papastamos 			return ret;
549fabe24eSDimitris Papastamos 		}
559fabe24eSDimitris Papastamos 		map->reg_defaults_raw = tmp_buf;
569fabe24eSDimitris Papastamos 		map->cache_free = 1;
579fabe24eSDimitris Papastamos 	}
589fabe24eSDimitris Papastamos 
599fabe24eSDimitris Papastamos 	/* calculate the size of reg_defaults */
609fabe24eSDimitris Papastamos 	for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
61879082c9SMark Brown 		val = regcache_get_val(map, map->reg_defaults_raw, i);
62f01ee60fSStephen Warren 		if (regmap_volatile(map, i * map->reg_stride))
639fabe24eSDimitris Papastamos 			continue;
649fabe24eSDimitris Papastamos 		count++;
659fabe24eSDimitris Papastamos 	}
669fabe24eSDimitris Papastamos 
679fabe24eSDimitris Papastamos 	map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
689fabe24eSDimitris Papastamos 				      GFP_KERNEL);
69021cd616SLars-Peter Clausen 	if (!map->reg_defaults) {
70021cd616SLars-Peter Clausen 		ret = -ENOMEM;
71021cd616SLars-Peter Clausen 		goto err_free;
72021cd616SLars-Peter Clausen 	}
739fabe24eSDimitris Papastamos 
749fabe24eSDimitris Papastamos 	/* fill the reg_defaults */
759fabe24eSDimitris Papastamos 	map->num_reg_defaults = count;
769fabe24eSDimitris Papastamos 	for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
77879082c9SMark Brown 		val = regcache_get_val(map, map->reg_defaults_raw, i);
78f01ee60fSStephen Warren 		if (regmap_volatile(map, i * map->reg_stride))
799fabe24eSDimitris Papastamos 			continue;
80f01ee60fSStephen Warren 		map->reg_defaults[j].reg = i * map->reg_stride;
819fabe24eSDimitris Papastamos 		map->reg_defaults[j].def = val;
829fabe24eSDimitris Papastamos 		j++;
839fabe24eSDimitris Papastamos 	}
849fabe24eSDimitris Papastamos 
859fabe24eSDimitris Papastamos 	return 0;
86021cd616SLars-Peter Clausen 
87021cd616SLars-Peter Clausen err_free:
88021cd616SLars-Peter Clausen 	if (map->cache_free)
89021cd616SLars-Peter Clausen 		kfree(map->reg_defaults_raw);
90021cd616SLars-Peter Clausen 
91021cd616SLars-Peter Clausen 	return ret;
929fabe24eSDimitris Papastamos }
939fabe24eSDimitris Papastamos 
94e5e3b8abSLars-Peter Clausen int regcache_init(struct regmap *map, const struct regmap_config *config)
959fabe24eSDimitris Papastamos {
969fabe24eSDimitris Papastamos 	int ret;
979fabe24eSDimitris Papastamos 	int i;
989fabe24eSDimitris Papastamos 	void *tmp_buf;
999fabe24eSDimitris Papastamos 
100f01ee60fSStephen Warren 	for (i = 0; i < config->num_reg_defaults; i++)
101f01ee60fSStephen Warren 		if (config->reg_defaults[i].reg % map->reg_stride)
102f01ee60fSStephen Warren 			return -EINVAL;
103f01ee60fSStephen Warren 
104e7a6db30SMark Brown 	if (map->cache_type == REGCACHE_NONE) {
105e7a6db30SMark Brown 		map->cache_bypass = true;
1069fabe24eSDimitris Papastamos 		return 0;
107e7a6db30SMark Brown 	}
1089fabe24eSDimitris Papastamos 
1099fabe24eSDimitris Papastamos 	for (i = 0; i < ARRAY_SIZE(cache_types); i++)
1109fabe24eSDimitris Papastamos 		if (cache_types[i]->type == map->cache_type)
1119fabe24eSDimitris Papastamos 			break;
1129fabe24eSDimitris Papastamos 
1139fabe24eSDimitris Papastamos 	if (i == ARRAY_SIZE(cache_types)) {
1149fabe24eSDimitris Papastamos 		dev_err(map->dev, "Could not match compress type: %d\n",
1159fabe24eSDimitris Papastamos 			map->cache_type);
1169fabe24eSDimitris Papastamos 		return -EINVAL;
1179fabe24eSDimitris Papastamos 	}
1189fabe24eSDimitris Papastamos 
119e5e3b8abSLars-Peter Clausen 	map->num_reg_defaults = config->num_reg_defaults;
120e5e3b8abSLars-Peter Clausen 	map->num_reg_defaults_raw = config->num_reg_defaults_raw;
121e5e3b8abSLars-Peter Clausen 	map->reg_defaults_raw = config->reg_defaults_raw;
122064d4db1SLars-Peter Clausen 	map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
123064d4db1SLars-Peter Clausen 	map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
124e5e3b8abSLars-Peter Clausen 
1259fabe24eSDimitris Papastamos 	map->cache = NULL;
1269fabe24eSDimitris Papastamos 	map->cache_ops = cache_types[i];
1279fabe24eSDimitris Papastamos 
1289fabe24eSDimitris Papastamos 	if (!map->cache_ops->read ||
1299fabe24eSDimitris Papastamos 	    !map->cache_ops->write ||
1309fabe24eSDimitris Papastamos 	    !map->cache_ops->name)
1319fabe24eSDimitris Papastamos 		return -EINVAL;
1329fabe24eSDimitris Papastamos 
1339fabe24eSDimitris Papastamos 	/* We still need to ensure that the reg_defaults
1349fabe24eSDimitris Papastamos 	 * won't vanish from under us.  We'll need to make
1359fabe24eSDimitris Papastamos 	 * a copy of it.
1369fabe24eSDimitris Papastamos 	 */
137720e4616SLars-Peter Clausen 	if (config->reg_defaults) {
1389fabe24eSDimitris Papastamos 		if (!map->num_reg_defaults)
1399fabe24eSDimitris Papastamos 			return -EINVAL;
140720e4616SLars-Peter Clausen 		tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
1419fabe24eSDimitris Papastamos 				  sizeof(struct reg_default), GFP_KERNEL);
1429fabe24eSDimitris Papastamos 		if (!tmp_buf)
1439fabe24eSDimitris Papastamos 			return -ENOMEM;
1449fabe24eSDimitris Papastamos 		map->reg_defaults = tmp_buf;
1458528bdd4SMark Brown 	} else if (map->num_reg_defaults_raw) {
1465fcd2560SMark Brown 		/* Some devices such as PMICs don't have cache defaults,
1479fabe24eSDimitris Papastamos 		 * we cope with this by reading back the HW registers and
1489fabe24eSDimitris Papastamos 		 * crafting the cache defaults by hand.
1499fabe24eSDimitris Papastamos 		 */
1509fabe24eSDimitris Papastamos 		ret = regcache_hw_init(map);
1519fabe24eSDimitris Papastamos 		if (ret < 0)
1529fabe24eSDimitris Papastamos 			return ret;
1539fabe24eSDimitris Papastamos 	}
1549fabe24eSDimitris Papastamos 
1559fabe24eSDimitris Papastamos 	if (!map->max_register)
1569fabe24eSDimitris Papastamos 		map->max_register = map->num_reg_defaults_raw;
1579fabe24eSDimitris Papastamos 
1589fabe24eSDimitris Papastamos 	if (map->cache_ops->init) {
1599fabe24eSDimitris Papastamos 		dev_dbg(map->dev, "Initializing %s cache\n",
1609fabe24eSDimitris Papastamos 			map->cache_ops->name);
161bd061c78SLars-Peter Clausen 		ret = map->cache_ops->init(map);
162bd061c78SLars-Peter Clausen 		if (ret)
163bd061c78SLars-Peter Clausen 			goto err_free;
1649fabe24eSDimitris Papastamos 	}
1659fabe24eSDimitris Papastamos 	return 0;
166bd061c78SLars-Peter Clausen 
167bd061c78SLars-Peter Clausen err_free:
168bd061c78SLars-Peter Clausen 	kfree(map->reg_defaults);
169bd061c78SLars-Peter Clausen 	if (map->cache_free)
170bd061c78SLars-Peter Clausen 		kfree(map->reg_defaults_raw);
171bd061c78SLars-Peter Clausen 
172bd061c78SLars-Peter Clausen 	return ret;
1739fabe24eSDimitris Papastamos }
1749fabe24eSDimitris Papastamos 
1759fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map)
1769fabe24eSDimitris Papastamos {
1779fabe24eSDimitris Papastamos 	if (map->cache_type == REGCACHE_NONE)
1789fabe24eSDimitris Papastamos 		return;
1799fabe24eSDimitris Papastamos 
1809fabe24eSDimitris Papastamos 	BUG_ON(!map->cache_ops);
1819fabe24eSDimitris Papastamos 
1829fabe24eSDimitris Papastamos 	kfree(map->reg_defaults);
1839fabe24eSDimitris Papastamos 	if (map->cache_free)
1849fabe24eSDimitris Papastamos 		kfree(map->reg_defaults_raw);
1859fabe24eSDimitris Papastamos 
1869fabe24eSDimitris Papastamos 	if (map->cache_ops->exit) {
1879fabe24eSDimitris Papastamos 		dev_dbg(map->dev, "Destroying %s cache\n",
1889fabe24eSDimitris Papastamos 			map->cache_ops->name);
1899fabe24eSDimitris Papastamos 		map->cache_ops->exit(map);
1909fabe24eSDimitris Papastamos 	}
1919fabe24eSDimitris Papastamos }
1929fabe24eSDimitris Papastamos 
1939fabe24eSDimitris Papastamos /**
1949fabe24eSDimitris Papastamos  * regcache_read: Fetch the value of a given register from the cache.
1959fabe24eSDimitris Papastamos  *
1969fabe24eSDimitris Papastamos  * @map: map to configure.
1979fabe24eSDimitris Papastamos  * @reg: The register index.
1989fabe24eSDimitris Papastamos  * @value: The value to be returned.
1999fabe24eSDimitris Papastamos  *
2009fabe24eSDimitris Papastamos  * Return a negative value on failure, 0 on success.
2019fabe24eSDimitris Papastamos  */
2029fabe24eSDimitris Papastamos int regcache_read(struct regmap *map,
2039fabe24eSDimitris Papastamos 		  unsigned int reg, unsigned int *value)
2049fabe24eSDimitris Papastamos {
205bc7ee556SMark Brown 	int ret;
206bc7ee556SMark Brown 
2079fabe24eSDimitris Papastamos 	if (map->cache_type == REGCACHE_NONE)
2089fabe24eSDimitris Papastamos 		return -ENOSYS;
2099fabe24eSDimitris Papastamos 
2109fabe24eSDimitris Papastamos 	BUG_ON(!map->cache_ops);
2119fabe24eSDimitris Papastamos 
212bc7ee556SMark Brown 	if (!regmap_volatile(map, reg)) {
213bc7ee556SMark Brown 		ret = map->cache_ops->read(map, reg, value);
214bc7ee556SMark Brown 
215bc7ee556SMark Brown 		if (ret == 0)
216bc7ee556SMark Brown 			trace_regmap_reg_read_cache(map->dev, reg, *value);
217bc7ee556SMark Brown 
218bc7ee556SMark Brown 		return ret;
219bc7ee556SMark Brown 	}
2209fabe24eSDimitris Papastamos 
2219fabe24eSDimitris Papastamos 	return -EINVAL;
2229fabe24eSDimitris Papastamos }
2239fabe24eSDimitris Papastamos 
2249fabe24eSDimitris Papastamos /**
2259fabe24eSDimitris Papastamos  * regcache_write: Set the value of a given register in the cache.
2269fabe24eSDimitris Papastamos  *
2279fabe24eSDimitris Papastamos  * @map: map to configure.
2289fabe24eSDimitris Papastamos  * @reg: The register index.
2299fabe24eSDimitris Papastamos  * @value: The new register value.
2309fabe24eSDimitris Papastamos  *
2319fabe24eSDimitris Papastamos  * Return a negative value on failure, 0 on success.
2329fabe24eSDimitris Papastamos  */
2339fabe24eSDimitris Papastamos int regcache_write(struct regmap *map,
2349fabe24eSDimitris Papastamos 		   unsigned int reg, unsigned int value)
2359fabe24eSDimitris Papastamos {
2369fabe24eSDimitris Papastamos 	if (map->cache_type == REGCACHE_NONE)
2379fabe24eSDimitris Papastamos 		return 0;
2389fabe24eSDimitris Papastamos 
2399fabe24eSDimitris Papastamos 	BUG_ON(!map->cache_ops);
2409fabe24eSDimitris Papastamos 
2419fabe24eSDimitris Papastamos 	if (!regmap_volatile(map, reg))
2429fabe24eSDimitris Papastamos 		return map->cache_ops->write(map, reg, value);
2439fabe24eSDimitris Papastamos 
2449fabe24eSDimitris Papastamos 	return 0;
2459fabe24eSDimitris Papastamos }
2469fabe24eSDimitris Papastamos 
247d856fce4SMaarten ter Huurne static int regcache_default_sync(struct regmap *map, unsigned int min,
248d856fce4SMaarten ter Huurne 				 unsigned int max)
249d856fce4SMaarten ter Huurne {
250d856fce4SMaarten ter Huurne 	unsigned int reg;
251d856fce4SMaarten ter Huurne 
252d856fce4SMaarten ter Huurne 	for (reg = min; reg <= max; reg++) {
253d856fce4SMaarten ter Huurne 		unsigned int val;
254d856fce4SMaarten ter Huurne 		int ret;
255d856fce4SMaarten ter Huurne 
256d856fce4SMaarten ter Huurne 		if (regmap_volatile(map, reg))
257d856fce4SMaarten ter Huurne 			continue;
258d856fce4SMaarten ter Huurne 
259d856fce4SMaarten ter Huurne 		ret = regcache_read(map, reg, &val);
260d856fce4SMaarten ter Huurne 		if (ret)
261d856fce4SMaarten ter Huurne 			return ret;
262d856fce4SMaarten ter Huurne 
263d856fce4SMaarten ter Huurne 		/* Is this the hardware default?  If so skip. */
264d856fce4SMaarten ter Huurne 		ret = regcache_lookup_reg(map, reg);
265d856fce4SMaarten ter Huurne 		if (ret >= 0 && val == map->reg_defaults[ret].def)
266d856fce4SMaarten ter Huurne 			continue;
267d856fce4SMaarten ter Huurne 
268d856fce4SMaarten ter Huurne 		map->cache_bypass = 1;
269d856fce4SMaarten ter Huurne 		ret = _regmap_write(map, reg, val);
270d856fce4SMaarten ter Huurne 		map->cache_bypass = 0;
271d856fce4SMaarten ter Huurne 		if (ret)
272d856fce4SMaarten ter Huurne 			return ret;
273d856fce4SMaarten ter Huurne 		dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
274d856fce4SMaarten ter Huurne 	}
275d856fce4SMaarten ter Huurne 
276d856fce4SMaarten ter Huurne 	return 0;
277d856fce4SMaarten ter Huurne }
278d856fce4SMaarten ter Huurne 
2799fabe24eSDimitris Papastamos /**
2809fabe24eSDimitris Papastamos  * regcache_sync: Sync the register cache with the hardware.
2819fabe24eSDimitris Papastamos  *
2829fabe24eSDimitris Papastamos  * @map: map to configure.
2839fabe24eSDimitris Papastamos  *
2849fabe24eSDimitris Papastamos  * Any registers that should not be synced should be marked as
2859fabe24eSDimitris Papastamos  * volatile.  In general drivers can choose not to use the provided
2869fabe24eSDimitris Papastamos  * syncing functionality if they so require.
2879fabe24eSDimitris Papastamos  *
2889fabe24eSDimitris Papastamos  * Return a negative value on failure, 0 on success.
2899fabe24eSDimitris Papastamos  */
2909fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map)
2919fabe24eSDimitris Papastamos {
292954757d7SDimitris Papastamos 	int ret = 0;
293954757d7SDimitris Papastamos 	unsigned int i;
29459360089SDimitris Papastamos 	const char *name;
295beb1a10fSDimitris Papastamos 	unsigned int bypass;
29659360089SDimitris Papastamos 
297d856fce4SMaarten ter Huurne 	BUG_ON(!map->cache_ops);
2989fabe24eSDimitris Papastamos 
29981485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
300beb1a10fSDimitris Papastamos 	/* Remember the initial bypass state */
301beb1a10fSDimitris Papastamos 	bypass = map->cache_bypass;
3029fabe24eSDimitris Papastamos 	dev_dbg(map->dev, "Syncing %s cache\n",
3039fabe24eSDimitris Papastamos 		map->cache_ops->name);
30459360089SDimitris Papastamos 	name = map->cache_ops->name;
30559360089SDimitris Papastamos 	trace_regcache_sync(map->dev, name, "start");
30622f0d90aSMark Brown 
3078ae0d7e8SMark Brown 	if (!map->cache_dirty)
3088ae0d7e8SMark Brown 		goto out;
309d9db7627SMark Brown 
31022f0d90aSMark Brown 	/* Apply any patch first */
3118a892d69SMark Brown 	map->cache_bypass = 1;
31222f0d90aSMark Brown 	for (i = 0; i < map->patch_regs; i++) {
313f01ee60fSStephen Warren 		if (map->patch[i].reg % map->reg_stride) {
314f01ee60fSStephen Warren 			ret = -EINVAL;
315f01ee60fSStephen Warren 			goto out;
316f01ee60fSStephen Warren 		}
31722f0d90aSMark Brown 		ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
31822f0d90aSMark Brown 		if (ret != 0) {
31922f0d90aSMark Brown 			dev_err(map->dev, "Failed to write %x = %x: %d\n",
32022f0d90aSMark Brown 				map->patch[i].reg, map->patch[i].def, ret);
32122f0d90aSMark Brown 			goto out;
32222f0d90aSMark Brown 		}
32322f0d90aSMark Brown 	}
3248a892d69SMark Brown 	map->cache_bypass = 0;
32522f0d90aSMark Brown 
326d856fce4SMaarten ter Huurne 	if (map->cache_ops->sync)
327ac8d91c8SMark Brown 		ret = map->cache_ops->sync(map, 0, map->max_register);
328d856fce4SMaarten ter Huurne 	else
329d856fce4SMaarten ter Huurne 		ret = regcache_default_sync(map, 0, map->max_register);
330954757d7SDimitris Papastamos 
3316ff73738SMark Brown 	if (ret == 0)
3326ff73738SMark Brown 		map->cache_dirty = false;
3336ff73738SMark Brown 
334954757d7SDimitris Papastamos out:
335954757d7SDimitris Papastamos 	trace_regcache_sync(map->dev, name, "stop");
336beb1a10fSDimitris Papastamos 	/* Restore the bypass state */
337beb1a10fSDimitris Papastamos 	map->cache_bypass = bypass;
33881485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
339954757d7SDimitris Papastamos 
340954757d7SDimitris Papastamos 	return ret;
3419fabe24eSDimitris Papastamos }
3429fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync);
3439fabe24eSDimitris Papastamos 
34492afb286SMark Brown /**
3454d4cfd16SMark Brown  * regcache_sync_region: Sync part  of the register cache with the hardware.
3464d4cfd16SMark Brown  *
3474d4cfd16SMark Brown  * @map: map to sync.
3484d4cfd16SMark Brown  * @min: first register to sync
3494d4cfd16SMark Brown  * @max: last register to sync
3504d4cfd16SMark Brown  *
3514d4cfd16SMark Brown  * Write all non-default register values in the specified region to
3524d4cfd16SMark Brown  * the hardware.
3534d4cfd16SMark Brown  *
3544d4cfd16SMark Brown  * Return a negative value on failure, 0 on success.
3554d4cfd16SMark Brown  */
3564d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min,
3574d4cfd16SMark Brown 			 unsigned int max)
3584d4cfd16SMark Brown {
3594d4cfd16SMark Brown 	int ret = 0;
3604d4cfd16SMark Brown 	const char *name;
3614d4cfd16SMark Brown 	unsigned int bypass;
3624d4cfd16SMark Brown 
363d856fce4SMaarten ter Huurne 	BUG_ON(!map->cache_ops);
3644d4cfd16SMark Brown 
36581485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
3664d4cfd16SMark Brown 
3674d4cfd16SMark Brown 	/* Remember the initial bypass state */
3684d4cfd16SMark Brown 	bypass = map->cache_bypass;
3694d4cfd16SMark Brown 
3704d4cfd16SMark Brown 	name = map->cache_ops->name;
3714d4cfd16SMark Brown 	dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
3724d4cfd16SMark Brown 
3734d4cfd16SMark Brown 	trace_regcache_sync(map->dev, name, "start region");
3744d4cfd16SMark Brown 
3754d4cfd16SMark Brown 	if (!map->cache_dirty)
3764d4cfd16SMark Brown 		goto out;
3774d4cfd16SMark Brown 
378d856fce4SMaarten ter Huurne 	if (map->cache_ops->sync)
3794d4cfd16SMark Brown 		ret = map->cache_ops->sync(map, min, max);
380d856fce4SMaarten ter Huurne 	else
381d856fce4SMaarten ter Huurne 		ret = regcache_default_sync(map, min, max);
3824d4cfd16SMark Brown 
3834d4cfd16SMark Brown out:
3844d4cfd16SMark Brown 	trace_regcache_sync(map->dev, name, "stop region");
3854d4cfd16SMark Brown 	/* Restore the bypass state */
3864d4cfd16SMark Brown 	map->cache_bypass = bypass;
38781485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
3884d4cfd16SMark Brown 
3894d4cfd16SMark Brown 	return ret;
3904d4cfd16SMark Brown }
391e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region);
3924d4cfd16SMark Brown 
3934d4cfd16SMark Brown /**
394697e85bcSMark Brown  * regcache_drop_region: Discard part of the register cache
395697e85bcSMark Brown  *
396697e85bcSMark Brown  * @map: map to operate on
397697e85bcSMark Brown  * @min: first register to discard
398697e85bcSMark Brown  * @max: last register to discard
399697e85bcSMark Brown  *
400697e85bcSMark Brown  * Discard part of the register cache.
401697e85bcSMark Brown  *
402697e85bcSMark Brown  * Return a negative value on failure, 0 on success.
403697e85bcSMark Brown  */
404697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min,
405697e85bcSMark Brown 			 unsigned int max)
406697e85bcSMark Brown {
407697e85bcSMark Brown 	int ret = 0;
408697e85bcSMark Brown 
4093f4ff561SLars-Peter Clausen 	if (!map->cache_ops || !map->cache_ops->drop)
410697e85bcSMark Brown 		return -EINVAL;
411697e85bcSMark Brown 
41281485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
413697e85bcSMark Brown 
414697e85bcSMark Brown 	trace_regcache_drop_region(map->dev, min, max);
415697e85bcSMark Brown 
416697e85bcSMark Brown 	ret = map->cache_ops->drop(map, min, max);
417697e85bcSMark Brown 
41881485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
419697e85bcSMark Brown 
420697e85bcSMark Brown 	return ret;
421697e85bcSMark Brown }
422697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region);
423697e85bcSMark Brown 
424697e85bcSMark Brown /**
42592afb286SMark Brown  * regcache_cache_only: Put a register map into cache only mode
42692afb286SMark Brown  *
42792afb286SMark Brown  * @map: map to configure
42892afb286SMark Brown  * @cache_only: flag if changes should be written to the hardware
42992afb286SMark Brown  *
43092afb286SMark Brown  * When a register map is marked as cache only writes to the register
43192afb286SMark Brown  * map API will only update the register cache, they will not cause
43292afb286SMark Brown  * any hardware changes.  This is useful for allowing portions of
43392afb286SMark Brown  * drivers to act as though the device were functioning as normal when
43492afb286SMark Brown  * it is disabled for power saving reasons.
43592afb286SMark Brown  */
43692afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable)
43792afb286SMark Brown {
43881485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
439ac77a765SDimitris Papastamos 	WARN_ON(map->cache_bypass && enable);
44092afb286SMark Brown 	map->cache_only = enable;
4415d5b7d4fSMark Brown 	trace_regmap_cache_only(map->dev, enable);
44281485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
44392afb286SMark Brown }
44492afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only);
44592afb286SMark Brown 
4466eb0f5e0SDimitris Papastamos /**
4478ae0d7e8SMark Brown  * regcache_mark_dirty: Mark the register cache as dirty
4488ae0d7e8SMark Brown  *
4498ae0d7e8SMark Brown  * @map: map to mark
4508ae0d7e8SMark Brown  *
4518ae0d7e8SMark Brown  * Mark the register cache as dirty, for example due to the device
4528ae0d7e8SMark Brown  * having been powered down for suspend.  If the cache is not marked
4538ae0d7e8SMark Brown  * as dirty then the cache sync will be suppressed.
4548ae0d7e8SMark Brown  */
4558ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map)
4568ae0d7e8SMark Brown {
45781485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
4588ae0d7e8SMark Brown 	map->cache_dirty = true;
45981485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
4608ae0d7e8SMark Brown }
4618ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty);
4628ae0d7e8SMark Brown 
4638ae0d7e8SMark Brown /**
4646eb0f5e0SDimitris Papastamos  * regcache_cache_bypass: Put a register map into cache bypass mode
4656eb0f5e0SDimitris Papastamos  *
4666eb0f5e0SDimitris Papastamos  * @map: map to configure
4670eef6b04SDimitris Papastamos  * @cache_bypass: flag if changes should not be written to the hardware
4686eb0f5e0SDimitris Papastamos  *
4696eb0f5e0SDimitris Papastamos  * When a register map is marked with the cache bypass option, writes
4706eb0f5e0SDimitris Papastamos  * to the register map API will only update the hardware and not the
4716eb0f5e0SDimitris Papastamos  * the cache directly.  This is useful when syncing the cache back to
4726eb0f5e0SDimitris Papastamos  * the hardware.
4736eb0f5e0SDimitris Papastamos  */
4746eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable)
4756eb0f5e0SDimitris Papastamos {
47681485f52SLars-Peter Clausen 	map->lock(map->lock_arg);
477ac77a765SDimitris Papastamos 	WARN_ON(map->cache_only && enable);
4786eb0f5e0SDimitris Papastamos 	map->cache_bypass = enable;
4795d5b7d4fSMark Brown 	trace_regmap_cache_bypass(map->dev, enable);
48081485f52SLars-Peter Clausen 	map->unlock(map->lock_arg);
4816eb0f5e0SDimitris Papastamos }
4826eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass);
4836eb0f5e0SDimitris Papastamos 
484879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
485879082c9SMark Brown 		      unsigned int val)
4869fabe24eSDimitris Papastamos {
487325acab4SMark Brown 	if (regcache_get_val(map, base, idx) == val)
488325acab4SMark Brown 		return true;
489325acab4SMark Brown 
490eb4cb76fSMark Brown 	/* Use device native format if possible */
491eb4cb76fSMark Brown 	if (map->format.format_val) {
492eb4cb76fSMark Brown 		map->format.format_val(base + (map->cache_word_size * idx),
493eb4cb76fSMark Brown 				       val, 0);
494eb4cb76fSMark Brown 		return false;
495eb4cb76fSMark Brown 	}
496eb4cb76fSMark Brown 
497879082c9SMark Brown 	switch (map->cache_word_size) {
4989fabe24eSDimitris Papastamos 	case 1: {
4999fabe24eSDimitris Papastamos 		u8 *cache = base;
5009fabe24eSDimitris Papastamos 		cache[idx] = val;
5019fabe24eSDimitris Papastamos 		break;
5029fabe24eSDimitris Papastamos 	}
5039fabe24eSDimitris Papastamos 	case 2: {
5049fabe24eSDimitris Papastamos 		u16 *cache = base;
5059fabe24eSDimitris Papastamos 		cache[idx] = val;
5069fabe24eSDimitris Papastamos 		break;
5079fabe24eSDimitris Papastamos 	}
5087d5e525bSMark Brown 	case 4: {
5097d5e525bSMark Brown 		u32 *cache = base;
5107d5e525bSMark Brown 		cache[idx] = val;
5117d5e525bSMark Brown 		break;
5127d5e525bSMark Brown 	}
5139fabe24eSDimitris Papastamos 	default:
5149fabe24eSDimitris Papastamos 		BUG();
5159fabe24eSDimitris Papastamos 	}
5169fabe24eSDimitris Papastamos 	return false;
5179fabe24eSDimitris Papastamos }
5189fabe24eSDimitris Papastamos 
519879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base,
520879082c9SMark Brown 			      unsigned int idx)
5219fabe24eSDimitris Papastamos {
5229fabe24eSDimitris Papastamos 	if (!base)
5239fabe24eSDimitris Papastamos 		return -EINVAL;
5249fabe24eSDimitris Papastamos 
525eb4cb76fSMark Brown 	/* Use device native format if possible */
526eb4cb76fSMark Brown 	if (map->format.parse_val)
5278817796bSMark Brown 		return map->format.parse_val(regcache_get_val_addr(map, base,
5288817796bSMark Brown 								   idx));
529eb4cb76fSMark Brown 
530879082c9SMark Brown 	switch (map->cache_word_size) {
5319fabe24eSDimitris Papastamos 	case 1: {
5329fabe24eSDimitris Papastamos 		const u8 *cache = base;
5339fabe24eSDimitris Papastamos 		return cache[idx];
5349fabe24eSDimitris Papastamos 	}
5359fabe24eSDimitris Papastamos 	case 2: {
5369fabe24eSDimitris Papastamos 		const u16 *cache = base;
5379fabe24eSDimitris Papastamos 		return cache[idx];
5389fabe24eSDimitris Papastamos 	}
5397d5e525bSMark Brown 	case 4: {
5407d5e525bSMark Brown 		const u32 *cache = base;
5417d5e525bSMark Brown 		return cache[idx];
5427d5e525bSMark Brown 	}
5439fabe24eSDimitris Papastamos 	default:
5449fabe24eSDimitris Papastamos 		BUG();
5459fabe24eSDimitris Papastamos 	}
5469fabe24eSDimitris Papastamos 	/* unreachable */
5479fabe24eSDimitris Papastamos 	return -1;
5489fabe24eSDimitris Papastamos }
5499fabe24eSDimitris Papastamos 
550f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b)
551c08604b8SDimitris Papastamos {
552c08604b8SDimitris Papastamos 	const struct reg_default *_a = a;
553c08604b8SDimitris Papastamos 	const struct reg_default *_b = b;
554c08604b8SDimitris Papastamos 
555c08604b8SDimitris Papastamos 	return _a->reg - _b->reg;
556c08604b8SDimitris Papastamos }
557c08604b8SDimitris Papastamos 
558f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg)
559f094fea6SMark Brown {
560f094fea6SMark Brown 	struct reg_default key;
561f094fea6SMark Brown 	struct reg_default *r;
562f094fea6SMark Brown 
563f094fea6SMark Brown 	key.reg = reg;
564f094fea6SMark Brown 	key.def = 0;
565f094fea6SMark Brown 
566f094fea6SMark Brown 	r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
567f094fea6SMark Brown 		    sizeof(struct reg_default), regcache_default_cmp);
568f094fea6SMark Brown 
569f094fea6SMark Brown 	if (r)
570f094fea6SMark Brown 		return r - map->reg_defaults;
571f094fea6SMark Brown 	else
5726e6ace00SMark Brown 		return -ENOENT;
573f094fea6SMark Brown }
574f8bd822cSMark Brown 
5753f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
5763f4ff561SLars-Peter Clausen {
5773f4ff561SLars-Peter Clausen 	if (!cache_present)
5783f4ff561SLars-Peter Clausen 		return true;
5793f4ff561SLars-Peter Clausen 
5803f4ff561SLars-Peter Clausen 	return test_bit(idx, cache_present);
5813f4ff561SLars-Peter Clausen }
5823f4ff561SLars-Peter Clausen 
583cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block,
5843f4ff561SLars-Peter Clausen 				      unsigned long *cache_present,
585cfdeb8c3SMark Brown 				      unsigned int block_base,
586cfdeb8c3SMark Brown 				      unsigned int start, unsigned int end)
587cfdeb8c3SMark Brown {
588cfdeb8c3SMark Brown 	unsigned int i, regtmp, val;
589cfdeb8c3SMark Brown 	int ret;
590cfdeb8c3SMark Brown 
591cfdeb8c3SMark Brown 	for (i = start; i < end; i++) {
592cfdeb8c3SMark Brown 		regtmp = block_base + (i * map->reg_stride);
593cfdeb8c3SMark Brown 
5943f4ff561SLars-Peter Clausen 		if (!regcache_reg_present(cache_present, i))
595cfdeb8c3SMark Brown 			continue;
596cfdeb8c3SMark Brown 
597cfdeb8c3SMark Brown 		val = regcache_get_val(map, block, i);
598cfdeb8c3SMark Brown 
599cfdeb8c3SMark Brown 		/* Is this the hardware default?  If so skip. */
600cfdeb8c3SMark Brown 		ret = regcache_lookup_reg(map, regtmp);
601cfdeb8c3SMark Brown 		if (ret >= 0 && val == map->reg_defaults[ret].def)
602cfdeb8c3SMark Brown 			continue;
603cfdeb8c3SMark Brown 
604cfdeb8c3SMark Brown 		map->cache_bypass = 1;
605cfdeb8c3SMark Brown 
606cfdeb8c3SMark Brown 		ret = _regmap_write(map, regtmp, val);
607cfdeb8c3SMark Brown 
608cfdeb8c3SMark Brown 		map->cache_bypass = 0;
609cfdeb8c3SMark Brown 		if (ret != 0)
610cfdeb8c3SMark Brown 			return ret;
611cfdeb8c3SMark Brown 		dev_dbg(map->dev, "Synced register %#x, value %#x\n",
612cfdeb8c3SMark Brown 			regtmp, val);
613cfdeb8c3SMark Brown 	}
614cfdeb8c3SMark Brown 
615cfdeb8c3SMark Brown 	return 0;
616cfdeb8c3SMark Brown }
617cfdeb8c3SMark Brown 
61875a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
61975a5f89fSMark Brown 					 unsigned int base, unsigned int cur)
62075a5f89fSMark Brown {
62175a5f89fSMark Brown 	size_t val_bytes = map->format.val_bytes;
62275a5f89fSMark Brown 	int ret, count;
62375a5f89fSMark Brown 
62475a5f89fSMark Brown 	if (*data == NULL)
62575a5f89fSMark Brown 		return 0;
62675a5f89fSMark Brown 
62775a5f89fSMark Brown 	count = cur - base;
62875a5f89fSMark Brown 
6299659293cSStratos Karafotis 	dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
63075a5f89fSMark Brown 		count * val_bytes, count, base, cur - 1);
63175a5f89fSMark Brown 
63275a5f89fSMark Brown 	map->cache_bypass = 1;
63375a5f89fSMark Brown 
634*0a819809SMark Brown 	ret = _regmap_raw_write(map, base, *data, count * val_bytes);
63575a5f89fSMark Brown 
63675a5f89fSMark Brown 	map->cache_bypass = 0;
63775a5f89fSMark Brown 
63875a5f89fSMark Brown 	*data = NULL;
63975a5f89fSMark Brown 
64075a5f89fSMark Brown 	return ret;
64175a5f89fSMark Brown }
64275a5f89fSMark Brown 
643f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block,
6443f4ff561SLars-Peter Clausen 			    unsigned long *cache_present,
645f8bd822cSMark Brown 			    unsigned int block_base, unsigned int start,
646f8bd822cSMark Brown 			    unsigned int end)
647f8bd822cSMark Brown {
64875a5f89fSMark Brown 	unsigned int i, val;
64975a5f89fSMark Brown 	unsigned int regtmp = 0;
65075a5f89fSMark Brown 	unsigned int base = 0;
65175a5f89fSMark Brown 	const void *data = NULL;
652f8bd822cSMark Brown 	int ret;
653f8bd822cSMark Brown 
654f8bd822cSMark Brown 	for (i = start; i < end; i++) {
655f8bd822cSMark Brown 		regtmp = block_base + (i * map->reg_stride);
656f8bd822cSMark Brown 
6573f4ff561SLars-Peter Clausen 		if (!regcache_reg_present(cache_present, i)) {
65875a5f89fSMark Brown 			ret = regcache_sync_block_raw_flush(map, &data,
65975a5f89fSMark Brown 							    base, regtmp);
66075a5f89fSMark Brown 			if (ret != 0)
66175a5f89fSMark Brown 				return ret;
662f8bd822cSMark Brown 			continue;
66375a5f89fSMark Brown 		}
664f8bd822cSMark Brown 
665f8bd822cSMark Brown 		val = regcache_get_val(map, block, i);
666f8bd822cSMark Brown 
667f8bd822cSMark Brown 		/* Is this the hardware default?  If so skip. */
668f8bd822cSMark Brown 		ret = regcache_lookup_reg(map, regtmp);
66975a5f89fSMark Brown 		if (ret >= 0 && val == map->reg_defaults[ret].def) {
67075a5f89fSMark Brown 			ret = regcache_sync_block_raw_flush(map, &data,
67175a5f89fSMark Brown 							    base, regtmp);
672f8bd822cSMark Brown 			if (ret != 0)
673f8bd822cSMark Brown 				return ret;
67475a5f89fSMark Brown 			continue;
675f8bd822cSMark Brown 		}
676f8bd822cSMark Brown 
67775a5f89fSMark Brown 		if (!data) {
67875a5f89fSMark Brown 			data = regcache_get_val_addr(map, block, i);
67975a5f89fSMark Brown 			base = regtmp;
68075a5f89fSMark Brown 		}
68175a5f89fSMark Brown 	}
68275a5f89fSMark Brown 
6832d49b598SLars-Peter Clausen 	return regcache_sync_block_raw_flush(map, &data, base, regtmp +
6842d49b598SLars-Peter Clausen 			map->reg_stride);
685f8bd822cSMark Brown }
686cfdeb8c3SMark Brown 
687cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block,
6883f4ff561SLars-Peter Clausen 			unsigned long *cache_present,
689cfdeb8c3SMark Brown 			unsigned int block_base, unsigned int start,
690cfdeb8c3SMark Brown 			unsigned int end)
691cfdeb8c3SMark Brown {
692cfdeb8c3SMark Brown 	if (regmap_can_raw_write(map))
6933f4ff561SLars-Peter Clausen 		return regcache_sync_block_raw(map, block, cache_present,
6943f4ff561SLars-Peter Clausen 					       block_base, start, end);
695cfdeb8c3SMark Brown 	else
6963f4ff561SLars-Peter Clausen 		return regcache_sync_block_single(map, block, cache_present,
6973f4ff561SLars-Peter Clausen 						  block_base, start, end);
698cfdeb8c3SMark Brown }
699