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 ®cache_rbtree_ops,
20f033c26dSMark Brown ®cache_maple_ops,
212ac902ceSMark Brown ®cache_flat_ops,
229fabe24eSDimitris Papastamos };
239fabe24eSDimitris Papastamos
regcache_hw_init(struct regmap * map)249fabe24eSDimitris Papastamos static int regcache_hw_init(struct regmap *map)
259fabe24eSDimitris Papastamos {
269fabe24eSDimitris Papastamos int i, j;
279fabe24eSDimitris Papastamos int ret;
289fabe24eSDimitris Papastamos int count;
293245d460SMark Brown unsigned int reg, val;
309fabe24eSDimitris Papastamos void *tmp_buf;
319fabe24eSDimitris Papastamos
329fabe24eSDimitris Papastamos if (!map->num_reg_defaults_raw)
339fabe24eSDimitris Papastamos return -EINVAL;
349fabe24eSDimitris Papastamos
35fb70067eSXiubo Li /* calculate the size of reg_defaults */
36fb70067eSXiubo Li for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++)
37b2c7f5d9SMaarten ter Huurne if (regmap_readable(map, i * map->reg_stride) &&
38b2c7f5d9SMaarten ter Huurne !regmap_volatile(map, i * map->reg_stride))
39fb70067eSXiubo Li count++;
40fb70067eSXiubo Li
41b2c7f5d9SMaarten ter Huurne /* all registers are unreadable or volatile, so just bypass */
42fb70067eSXiubo Li if (!count) {
43fb70067eSXiubo Li map->cache_bypass = true;
44fb70067eSXiubo Li return 0;
45fb70067eSXiubo Li }
46fb70067eSXiubo Li
47fb70067eSXiubo Li map->num_reg_defaults = count;
48fb70067eSXiubo Li map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default),
49fb70067eSXiubo Li GFP_KERNEL);
50fb70067eSXiubo Li if (!map->reg_defaults)
51fb70067eSXiubo Li return -ENOMEM;
52fb70067eSXiubo Li
539fabe24eSDimitris Papastamos if (!map->reg_defaults_raw) {
54621a5f7aSViresh Kumar bool cache_bypass = map->cache_bypass;
559fabe24eSDimitris Papastamos dev_warn(map->dev, "No cache defaults, reading back from HW\n");
56df00c79fSLaxman Dewangan
57df00c79fSLaxman Dewangan /* Bypass the cache access till data read from HW */
58621a5f7aSViresh Kumar map->cache_bypass = true;
599fabe24eSDimitris Papastamos tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
60fb70067eSXiubo Li if (!tmp_buf) {
61fb70067eSXiubo Li ret = -ENOMEM;
62fb70067eSXiubo Li goto err_free;
63fb70067eSXiubo Li }
64eb4cb76fSMark Brown ret = regmap_raw_read(map, 0, tmp_buf,
65d51fe1f3SMaciej S. Szmigiero map->cache_size_raw);
66df00c79fSLaxman Dewangan map->cache_bypass = cache_bypass;
673245d460SMark Brown if (ret == 0) {
689fabe24eSDimitris Papastamos map->reg_defaults_raw = tmp_buf;
69b67498d6SJiapeng Zhong map->cache_free = true;
703245d460SMark Brown } else {
713245d460SMark Brown kfree(tmp_buf);
723245d460SMark Brown }
739fabe24eSDimitris Papastamos }
749fabe24eSDimitris Papastamos
759fabe24eSDimitris Papastamos /* fill the reg_defaults */
769fabe24eSDimitris Papastamos for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
773245d460SMark Brown reg = i * map->reg_stride;
783245d460SMark Brown
793245d460SMark Brown if (!regmap_readable(map, reg))
809fabe24eSDimitris Papastamos continue;
813245d460SMark Brown
823245d460SMark Brown if (regmap_volatile(map, reg))
833245d460SMark Brown continue;
843245d460SMark Brown
853245d460SMark Brown if (map->reg_defaults_raw) {
86fbba43c5SXiubo Li val = regcache_get_val(map, map->reg_defaults_raw, i);
873245d460SMark Brown } else {
883245d460SMark Brown bool cache_bypass = map->cache_bypass;
893245d460SMark Brown
903245d460SMark Brown map->cache_bypass = true;
913245d460SMark Brown ret = regmap_read(map, reg, &val);
923245d460SMark Brown map->cache_bypass = cache_bypass;
933245d460SMark Brown if (ret != 0) {
943245d460SMark Brown dev_err(map->dev, "Failed to read %d: %d\n",
953245d460SMark Brown reg, ret);
963245d460SMark Brown goto err_free;
973245d460SMark Brown }
983245d460SMark Brown }
993245d460SMark Brown
1003245d460SMark Brown map->reg_defaults[j].reg = reg;
1019fabe24eSDimitris Papastamos map->reg_defaults[j].def = val;
1029fabe24eSDimitris Papastamos j++;
1039fabe24eSDimitris Papastamos }
1049fabe24eSDimitris Papastamos
1059fabe24eSDimitris Papastamos return 0;
106021cd616SLars-Peter Clausen
107021cd616SLars-Peter Clausen err_free:
108fb70067eSXiubo Li kfree(map->reg_defaults);
109021cd616SLars-Peter Clausen
110021cd616SLars-Peter Clausen return ret;
1119fabe24eSDimitris Papastamos }
1129fabe24eSDimitris Papastamos
regcache_init(struct regmap * map,const struct regmap_config * config)113e5e3b8abSLars-Peter Clausen int regcache_init(struct regmap *map, const struct regmap_config *config)
1149fabe24eSDimitris Papastamos {
1159fabe24eSDimitris Papastamos int ret;
1169fabe24eSDimitris Papastamos int i;
1179fabe24eSDimitris Papastamos void *tmp_buf;
1189fabe24eSDimitris Papastamos
119e7a6db30SMark Brown if (map->cache_type == REGCACHE_NONE) {
1208cfe2fd3SXiubo Li if (config->reg_defaults || config->num_reg_defaults_raw)
1218cfe2fd3SXiubo Li dev_warn(map->dev,
1228cfe2fd3SXiubo Li "No cache used with register defaults set!\n");
1238cfe2fd3SXiubo Li
124e7a6db30SMark Brown map->cache_bypass = true;
1259fabe24eSDimitris Papastamos return 0;
126e7a6db30SMark Brown }
1279fabe24eSDimitris Papastamos
128167f7066SXiubo Li if (config->reg_defaults && !config->num_reg_defaults) {
129167f7066SXiubo Li dev_err(map->dev,
130167f7066SXiubo Li "Register defaults are set without the number!\n");
131167f7066SXiubo Li return -EINVAL;
132167f7066SXiubo Li }
133167f7066SXiubo Li
134a5201d42SSchspa Shi if (config->num_reg_defaults && !config->reg_defaults) {
135a5201d42SSchspa Shi dev_err(map->dev,
136a5201d42SSchspa Shi "Register defaults number are set without the reg!\n");
137a5201d42SSchspa Shi return -EINVAL;
138a5201d42SSchspa Shi }
139a5201d42SSchspa Shi
1408cfe2fd3SXiubo Li for (i = 0; i < config->num_reg_defaults; i++)
1418cfe2fd3SXiubo Li if (config->reg_defaults[i].reg % map->reg_stride)
1428cfe2fd3SXiubo Li return -EINVAL;
1438cfe2fd3SXiubo Li
1449fabe24eSDimitris Papastamos for (i = 0; i < ARRAY_SIZE(cache_types); i++)
1459fabe24eSDimitris Papastamos if (cache_types[i]->type == map->cache_type)
1469fabe24eSDimitris Papastamos break;
1479fabe24eSDimitris Papastamos
1489fabe24eSDimitris Papastamos if (i == ARRAY_SIZE(cache_types)) {
1492d38e861SMark Brown dev_err(map->dev, "Could not match cache type: %d\n",
1509fabe24eSDimitris Papastamos map->cache_type);
1519fabe24eSDimitris Papastamos return -EINVAL;
1529fabe24eSDimitris Papastamos }
1539fabe24eSDimitris Papastamos
154e5e3b8abSLars-Peter Clausen map->num_reg_defaults = config->num_reg_defaults;
155e5e3b8abSLars-Peter Clausen map->num_reg_defaults_raw = config->num_reg_defaults_raw;
156e5e3b8abSLars-Peter Clausen map->reg_defaults_raw = config->reg_defaults_raw;
157064d4db1SLars-Peter Clausen map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
158064d4db1SLars-Peter Clausen map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
159e5e3b8abSLars-Peter Clausen
1609fabe24eSDimitris Papastamos map->cache = NULL;
1619fabe24eSDimitris Papastamos map->cache_ops = cache_types[i];
1629fabe24eSDimitris Papastamos
1639fabe24eSDimitris Papastamos if (!map->cache_ops->read ||
1649fabe24eSDimitris Papastamos !map->cache_ops->write ||
1659fabe24eSDimitris Papastamos !map->cache_ops->name)
1669fabe24eSDimitris Papastamos return -EINVAL;
1679fabe24eSDimitris Papastamos
1689fabe24eSDimitris Papastamos /* We still need to ensure that the reg_defaults
1699fabe24eSDimitris Papastamos * won't vanish from under us. We'll need to make
1709fabe24eSDimitris Papastamos * a copy of it.
1719fabe24eSDimitris Papastamos */
172720e4616SLars-Peter Clausen if (config->reg_defaults) {
173720e4616SLars-Peter Clausen tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
1749fabe24eSDimitris Papastamos sizeof(struct reg_default), GFP_KERNEL);
1759fabe24eSDimitris Papastamos if (!tmp_buf)
1769fabe24eSDimitris Papastamos return -ENOMEM;
1779fabe24eSDimitris Papastamos map->reg_defaults = tmp_buf;
1788528bdd4SMark Brown } else if (map->num_reg_defaults_raw) {
1795fcd2560SMark Brown /* Some devices such as PMICs don't have cache defaults,
1809fabe24eSDimitris Papastamos * we cope with this by reading back the HW registers and
1819fabe24eSDimitris Papastamos * crafting the cache defaults by hand.
1829fabe24eSDimitris Papastamos */
1839fabe24eSDimitris Papastamos ret = regcache_hw_init(map);
1849fabe24eSDimitris Papastamos if (ret < 0)
1859fabe24eSDimitris Papastamos return ret;
186fb70067eSXiubo Li if (map->cache_bypass)
187fb70067eSXiubo Li return 0;
1889fabe24eSDimitris Papastamos }
1899fabe24eSDimitris Papastamos
190d6409475SJeongtae Park if (!map->max_register && map->num_reg_defaults_raw)
191d6409475SJeongtae Park map->max_register = (map->num_reg_defaults_raw - 1) * map->reg_stride;
1929fabe24eSDimitris Papastamos
1939fabe24eSDimitris Papastamos if (map->cache_ops->init) {
1949fabe24eSDimitris Papastamos dev_dbg(map->dev, "Initializing %s cache\n",
1959fabe24eSDimitris Papastamos map->cache_ops->name);
196bd061c78SLars-Peter Clausen ret = map->cache_ops->init(map);
197bd061c78SLars-Peter Clausen if (ret)
198bd061c78SLars-Peter Clausen goto err_free;
1999fabe24eSDimitris Papastamos }
2009fabe24eSDimitris Papastamos return 0;
201bd061c78SLars-Peter Clausen
202bd061c78SLars-Peter Clausen err_free:
203bd061c78SLars-Peter Clausen kfree(map->reg_defaults);
204bd061c78SLars-Peter Clausen if (map->cache_free)
205bd061c78SLars-Peter Clausen kfree(map->reg_defaults_raw);
206bd061c78SLars-Peter Clausen
207bd061c78SLars-Peter Clausen return ret;
2089fabe24eSDimitris Papastamos }
2099fabe24eSDimitris Papastamos
regcache_exit(struct regmap * map)2109fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map)
2119fabe24eSDimitris Papastamos {
2129fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE)
2139fabe24eSDimitris Papastamos return;
2149fabe24eSDimitris Papastamos
2159fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops);
2169fabe24eSDimitris Papastamos
2179fabe24eSDimitris Papastamos kfree(map->reg_defaults);
2189fabe24eSDimitris Papastamos if (map->cache_free)
2199fabe24eSDimitris Papastamos kfree(map->reg_defaults_raw);
2209fabe24eSDimitris Papastamos
2219fabe24eSDimitris Papastamos if (map->cache_ops->exit) {
2229fabe24eSDimitris Papastamos dev_dbg(map->dev, "Destroying %s cache\n",
2239fabe24eSDimitris Papastamos map->cache_ops->name);
2249fabe24eSDimitris Papastamos map->cache_ops->exit(map);
2259fabe24eSDimitris Papastamos }
2269fabe24eSDimitris Papastamos }
2279fabe24eSDimitris Papastamos
2289fabe24eSDimitris Papastamos /**
2292cf8e2dfSCharles Keepax * regcache_read - Fetch the value of a given register from the cache.
2309fabe24eSDimitris Papastamos *
2319fabe24eSDimitris Papastamos * @map: map to configure.
2329fabe24eSDimitris Papastamos * @reg: The register index.
2339fabe24eSDimitris Papastamos * @value: The value to be returned.
2349fabe24eSDimitris Papastamos *
2359fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success.
2369fabe24eSDimitris Papastamos */
regcache_read(struct regmap * map,unsigned int reg,unsigned int * value)2379fabe24eSDimitris Papastamos int regcache_read(struct regmap *map,
2389fabe24eSDimitris Papastamos unsigned int reg, unsigned int *value)
2399fabe24eSDimitris Papastamos {
240bc7ee556SMark Brown int ret;
241bc7ee556SMark Brown
2429fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE)
24324d80fdeSAlexander Stein return -EINVAL;
2449fabe24eSDimitris Papastamos
2459fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops);
2469fabe24eSDimitris Papastamos
247bc7ee556SMark Brown if (!regmap_volatile(map, reg)) {
248bc7ee556SMark Brown ret = map->cache_ops->read(map, reg, value);
249bc7ee556SMark Brown
250bc7ee556SMark Brown if (ret == 0)
251c6b570d9SPhilipp Zabel trace_regmap_reg_read_cache(map, reg, *value);
252bc7ee556SMark Brown
253bc7ee556SMark Brown return ret;
254bc7ee556SMark Brown }
2559fabe24eSDimitris Papastamos
2569fabe24eSDimitris Papastamos return -EINVAL;
2579fabe24eSDimitris Papastamos }
2589fabe24eSDimitris Papastamos
2599fabe24eSDimitris Papastamos /**
2602cf8e2dfSCharles Keepax * regcache_write - Set the value of a given register in the cache.
2619fabe24eSDimitris Papastamos *
2629fabe24eSDimitris Papastamos * @map: map to configure.
2639fabe24eSDimitris Papastamos * @reg: The register index.
2649fabe24eSDimitris Papastamos * @value: The new register value.
2659fabe24eSDimitris Papastamos *
2669fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success.
2679fabe24eSDimitris Papastamos */
regcache_write(struct regmap * map,unsigned int reg,unsigned int value)2689fabe24eSDimitris Papastamos int regcache_write(struct regmap *map,
2699fabe24eSDimitris Papastamos unsigned int reg, unsigned int value)
2709fabe24eSDimitris Papastamos {
2719fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE)
2729fabe24eSDimitris Papastamos return 0;
2739fabe24eSDimitris Papastamos
2749fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops);
2759fabe24eSDimitris Papastamos
2769fabe24eSDimitris Papastamos if (!regmap_volatile(map, reg))
2779fabe24eSDimitris Papastamos return map->cache_ops->write(map, reg, value);
2789fabe24eSDimitris Papastamos
2799fabe24eSDimitris Papastamos return 0;
2809fabe24eSDimitris Papastamos }
2819fabe24eSDimitris Papastamos
regcache_reg_needs_sync(struct regmap * map,unsigned int reg,unsigned int val)282bfa0b38cSMark Brown bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
2833969fa08SKevin Cernekee unsigned int val)
2843969fa08SKevin Cernekee {
2853969fa08SKevin Cernekee int ret;
2863969fa08SKevin Cernekee
28744e46572STakashi Iwai if (!regmap_writeable(map, reg))
28844e46572STakashi Iwai return false;
28944e46572STakashi Iwai
2901c79771aSKevin Cernekee /* If we don't know the chip just got reset, then sync everything. */
2911c79771aSKevin Cernekee if (!map->no_sync_defaults)
2921c79771aSKevin Cernekee return true;
2931c79771aSKevin Cernekee
2943969fa08SKevin Cernekee /* Is this the hardware default? If so skip. */
2953969fa08SKevin Cernekee ret = regcache_lookup_reg(map, reg);
2963969fa08SKevin Cernekee if (ret >= 0 && val == map->reg_defaults[ret].def)
2973969fa08SKevin Cernekee return false;
2983969fa08SKevin Cernekee return true;
2993969fa08SKevin Cernekee }
3003969fa08SKevin Cernekee
regcache_default_sync(struct regmap * map,unsigned int min,unsigned int max)301d856fce4SMaarten ter Huurne static int regcache_default_sync(struct regmap *map, unsigned int min,
302d856fce4SMaarten ter Huurne unsigned int max)
303d856fce4SMaarten ter Huurne {
304d856fce4SMaarten ter Huurne unsigned int reg;
305d856fce4SMaarten ter Huurne
30675617328SDylan Reid for (reg = min; reg <= max; reg += map->reg_stride) {
307d856fce4SMaarten ter Huurne unsigned int val;
308d856fce4SMaarten ter Huurne int ret;
309d856fce4SMaarten ter Huurne
31083f8475cSDylan Reid if (regmap_volatile(map, reg) ||
31183f8475cSDylan Reid !regmap_writeable(map, reg))
312d856fce4SMaarten ter Huurne continue;
313d856fce4SMaarten ter Huurne
314d856fce4SMaarten ter Huurne ret = regcache_read(map, reg, &val);
3152c89db8fSMark Brown if (ret == -ENOENT)
3162c89db8fSMark Brown continue;
317d856fce4SMaarten ter Huurne if (ret)
318d856fce4SMaarten ter Huurne return ret;
319d856fce4SMaarten ter Huurne
3203969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, reg, val))
321d856fce4SMaarten ter Huurne continue;
322d856fce4SMaarten ter Huurne
323621a5f7aSViresh Kumar map->cache_bypass = true;
324d856fce4SMaarten ter Huurne ret = _regmap_write(map, reg, val);
325621a5f7aSViresh Kumar map->cache_bypass = false;
326f29a4320SJarkko Nikula if (ret) {
327f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync register %#x. %d\n",
328f29a4320SJarkko Nikula reg, ret);
329d856fce4SMaarten ter Huurne return ret;
330f29a4320SJarkko Nikula }
331d856fce4SMaarten ter Huurne dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
332d856fce4SMaarten ter Huurne }
333d856fce4SMaarten ter Huurne
334d856fce4SMaarten ter Huurne return 0;
335d856fce4SMaarten ter Huurne }
336d856fce4SMaarten ter Huurne
rbtree_all(const void * key,const struct rb_node * node)3371985fab7SMark Brown static int rbtree_all(const void *key, const struct rb_node *node)
3381985fab7SMark Brown {
3391985fab7SMark Brown return 0;
3401985fab7SMark Brown }
3411985fab7SMark Brown
3429fabe24eSDimitris Papastamos /**
3432cf8e2dfSCharles Keepax * regcache_sync - Sync the register cache with the hardware.
3449fabe24eSDimitris Papastamos *
3459fabe24eSDimitris Papastamos * @map: map to configure.
3469fabe24eSDimitris Papastamos *
3479fabe24eSDimitris Papastamos * Any registers that should not be synced should be marked as
3489fabe24eSDimitris Papastamos * volatile. In general drivers can choose not to use the provided
3499fabe24eSDimitris Papastamos * syncing functionality if they so require.
3509fabe24eSDimitris Papastamos *
3519fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success.
3529fabe24eSDimitris Papastamos */
regcache_sync(struct regmap * map)3539fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map)
3549fabe24eSDimitris Papastamos {
355954757d7SDimitris Papastamos int ret = 0;
356954757d7SDimitris Papastamos unsigned int i;
35759360089SDimitris Papastamos const char *name;
358621a5f7aSViresh Kumar bool bypass;
3591985fab7SMark Brown struct rb_node *node;
36059360089SDimitris Papastamos
361fd883d79SAlexander Stein if (WARN_ON(map->cache_type == REGCACHE_NONE))
362fd883d79SAlexander Stein return -EINVAL;
363fd883d79SAlexander Stein
364d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops);
3659fabe24eSDimitris Papastamos
36681485f52SLars-Peter Clausen map->lock(map->lock_arg);
367beb1a10fSDimitris Papastamos /* Remember the initial bypass state */
368beb1a10fSDimitris Papastamos bypass = map->cache_bypass;
3699fabe24eSDimitris Papastamos dev_dbg(map->dev, "Syncing %s cache\n",
3709fabe24eSDimitris Papastamos map->cache_ops->name);
37159360089SDimitris Papastamos name = map->cache_ops->name;
372c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start");
37322f0d90aSMark Brown
3748ae0d7e8SMark Brown if (!map->cache_dirty)
3758ae0d7e8SMark Brown goto out;
376d9db7627SMark Brown
37722f0d90aSMark Brown /* Apply any patch first */
378621a5f7aSViresh Kumar map->cache_bypass = true;
37922f0d90aSMark Brown for (i = 0; i < map->patch_regs; i++) {
38022f0d90aSMark Brown ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
38122f0d90aSMark Brown if (ret != 0) {
38222f0d90aSMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n",
38322f0d90aSMark Brown map->patch[i].reg, map->patch[i].def, ret);
38422f0d90aSMark Brown goto out;
38522f0d90aSMark Brown }
38622f0d90aSMark Brown }
387621a5f7aSViresh Kumar map->cache_bypass = false;
38822f0d90aSMark Brown
389d856fce4SMaarten ter Huurne if (map->cache_ops->sync)
390ac8d91c8SMark Brown ret = map->cache_ops->sync(map, 0, map->max_register);
391d856fce4SMaarten ter Huurne else
392d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, 0, map->max_register);
393954757d7SDimitris Papastamos
3946ff73738SMark Brown if (ret == 0)
3956ff73738SMark Brown map->cache_dirty = false;
3966ff73738SMark Brown
397954757d7SDimitris Papastamos out:
398beb1a10fSDimitris Papastamos /* Restore the bypass state */
399beb1a10fSDimitris Papastamos map->cache_bypass = bypass;
4001c79771aSKevin Cernekee map->no_sync_defaults = false;
4011985fab7SMark Brown
4021985fab7SMark Brown /*
4031985fab7SMark Brown * If we did any paging with cache bypassed and a cached
4041985fab7SMark Brown * paging register then the register and cache state might
4051985fab7SMark Brown * have gone out of sync, force writes of all the paging
4061985fab7SMark Brown * registers.
4071985fab7SMark Brown */
4081985fab7SMark Brown rb_for_each(node, 0, &map->range_tree, rbtree_all) {
4091985fab7SMark Brown struct regmap_range_node *this =
4101985fab7SMark Brown rb_entry(node, struct regmap_range_node, node);
4111985fab7SMark Brown
4121985fab7SMark Brown /* If there's nothing in the cache there's nothing to sync */
413*78c8fc33SMatthias Reichl if (regcache_read(map, this->selector_reg, &i) != 0)
4141985fab7SMark Brown continue;
4151985fab7SMark Brown
4161985fab7SMark Brown ret = _regmap_write(map, this->selector_reg, i);
4171985fab7SMark Brown if (ret != 0) {
4181985fab7SMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n",
4191985fab7SMark Brown this->selector_reg, i, ret);
4201985fab7SMark Brown break;
4211985fab7SMark Brown }
4221985fab7SMark Brown }
4231985fab7SMark Brown
42481485f52SLars-Peter Clausen map->unlock(map->lock_arg);
425954757d7SDimitris Papastamos
426affbe886SMark Brown regmap_async_complete(map);
427affbe886SMark Brown
428c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop");
429affbe886SMark Brown
430954757d7SDimitris Papastamos return ret;
4319fabe24eSDimitris Papastamos }
4329fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync);
4339fabe24eSDimitris Papastamos
43492afb286SMark Brown /**
4352cf8e2dfSCharles Keepax * regcache_sync_region - Sync part of the register cache with the hardware.
4364d4cfd16SMark Brown *
4374d4cfd16SMark Brown * @map: map to sync.
4384d4cfd16SMark Brown * @min: first register to sync
4394d4cfd16SMark Brown * @max: last register to sync
4404d4cfd16SMark Brown *
4414d4cfd16SMark Brown * Write all non-default register values in the specified region to
4424d4cfd16SMark Brown * the hardware.
4434d4cfd16SMark Brown *
4444d4cfd16SMark Brown * Return a negative value on failure, 0 on success.
4454d4cfd16SMark Brown */
regcache_sync_region(struct regmap * map,unsigned int min,unsigned int max)4464d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min,
4474d4cfd16SMark Brown unsigned int max)
4484d4cfd16SMark Brown {
4494d4cfd16SMark Brown int ret = 0;
4504d4cfd16SMark Brown const char *name;
451621a5f7aSViresh Kumar bool bypass;
4524d4cfd16SMark Brown
453fd883d79SAlexander Stein if (WARN_ON(map->cache_type == REGCACHE_NONE))
454fd883d79SAlexander Stein return -EINVAL;
455fd883d79SAlexander Stein
456d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops);
4574d4cfd16SMark Brown
45881485f52SLars-Peter Clausen map->lock(map->lock_arg);
4594d4cfd16SMark Brown
4604d4cfd16SMark Brown /* Remember the initial bypass state */
4614d4cfd16SMark Brown bypass = map->cache_bypass;
4624d4cfd16SMark Brown
4634d4cfd16SMark Brown name = map->cache_ops->name;
4644d4cfd16SMark Brown dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
4654d4cfd16SMark Brown
466c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "start region");
4674d4cfd16SMark Brown
4684d4cfd16SMark Brown if (!map->cache_dirty)
4694d4cfd16SMark Brown goto out;
4704d4cfd16SMark Brown
471affbe886SMark Brown map->async = true;
472affbe886SMark Brown
473d856fce4SMaarten ter Huurne if (map->cache_ops->sync)
4744d4cfd16SMark Brown ret = map->cache_ops->sync(map, min, max);
475d856fce4SMaarten ter Huurne else
476d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, min, max);
4774d4cfd16SMark Brown
4784d4cfd16SMark Brown out:
4794d4cfd16SMark Brown /* Restore the bypass state */
4804d4cfd16SMark Brown map->cache_bypass = bypass;
481affbe886SMark Brown map->async = false;
4821c79771aSKevin Cernekee map->no_sync_defaults = false;
48381485f52SLars-Peter Clausen map->unlock(map->lock_arg);
4844d4cfd16SMark Brown
485affbe886SMark Brown regmap_async_complete(map);
486affbe886SMark Brown
487c6b570d9SPhilipp Zabel trace_regcache_sync(map, name, "stop region");
488affbe886SMark Brown
4894d4cfd16SMark Brown return ret;
4904d4cfd16SMark Brown }
491e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region);
4924d4cfd16SMark Brown
4934d4cfd16SMark Brown /**
4942cf8e2dfSCharles Keepax * regcache_drop_region - Discard part of the register cache
495697e85bcSMark Brown *
496697e85bcSMark Brown * @map: map to operate on
497697e85bcSMark Brown * @min: first register to discard
498697e85bcSMark Brown * @max: last register to discard
499697e85bcSMark Brown *
500697e85bcSMark Brown * Discard part of the register cache.
501697e85bcSMark Brown *
502697e85bcSMark Brown * Return a negative value on failure, 0 on success.
503697e85bcSMark Brown */
regcache_drop_region(struct regmap * map,unsigned int min,unsigned int max)504697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min,
505697e85bcSMark Brown unsigned int max)
506697e85bcSMark Brown {
507697e85bcSMark Brown int ret = 0;
508697e85bcSMark Brown
5093f4ff561SLars-Peter Clausen if (!map->cache_ops || !map->cache_ops->drop)
510697e85bcSMark Brown return -EINVAL;
511697e85bcSMark Brown
51281485f52SLars-Peter Clausen map->lock(map->lock_arg);
513697e85bcSMark Brown
514c6b570d9SPhilipp Zabel trace_regcache_drop_region(map, min, max);
515697e85bcSMark Brown
516697e85bcSMark Brown ret = map->cache_ops->drop(map, min, max);
517697e85bcSMark Brown
51881485f52SLars-Peter Clausen map->unlock(map->lock_arg);
519697e85bcSMark Brown
520697e85bcSMark Brown return ret;
521697e85bcSMark Brown }
522697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region);
523697e85bcSMark Brown
524697e85bcSMark Brown /**
5252cf8e2dfSCharles Keepax * regcache_cache_only - Put a register map into cache only mode
52692afb286SMark Brown *
52792afb286SMark Brown * @map: map to configure
5282cf8e2dfSCharles Keepax * @enable: flag if changes should be written to the hardware
52992afb286SMark Brown *
53092afb286SMark Brown * When a register map is marked as cache only writes to the register
53192afb286SMark Brown * map API will only update the register cache, they will not cause
53292afb286SMark Brown * any hardware changes. This is useful for allowing portions of
53392afb286SMark Brown * drivers to act as though the device were functioning as normal when
53492afb286SMark Brown * it is disabled for power saving reasons.
53592afb286SMark Brown */
regcache_cache_only(struct regmap * map,bool enable)53692afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable)
53792afb286SMark Brown {
53881485f52SLars-Peter Clausen map->lock(map->lock_arg);
5393d0afe9cSMark Brown WARN_ON(map->cache_type != REGCACHE_NONE &&
5403d0afe9cSMark Brown map->cache_bypass && enable);
54192afb286SMark Brown map->cache_only = enable;
542c6b570d9SPhilipp Zabel trace_regmap_cache_only(map, enable);
54381485f52SLars-Peter Clausen map->unlock(map->lock_arg);
54492afb286SMark Brown }
54592afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only);
54692afb286SMark Brown
5476eb0f5e0SDimitris Papastamos /**
5482cf8e2dfSCharles Keepax * regcache_mark_dirty - Indicate that HW registers were reset to default values
5498ae0d7e8SMark Brown *
5508ae0d7e8SMark Brown * @map: map to mark
5518ae0d7e8SMark Brown *
5521c79771aSKevin Cernekee * Inform regcache that the device has been powered down or reset, so that
5531c79771aSKevin Cernekee * on resume, regcache_sync() knows to write out all non-default values
5541c79771aSKevin Cernekee * stored in the cache.
5551c79771aSKevin Cernekee *
5561c79771aSKevin Cernekee * If this function is not called, regcache_sync() will assume that
5571c79771aSKevin Cernekee * the hardware state still matches the cache state, modulo any writes that
5581c79771aSKevin Cernekee * happened when cache_only was true.
5598ae0d7e8SMark Brown */
regcache_mark_dirty(struct regmap * map)5608ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map)
5618ae0d7e8SMark Brown {
56281485f52SLars-Peter Clausen map->lock(map->lock_arg);
5638ae0d7e8SMark Brown map->cache_dirty = true;
5641c79771aSKevin Cernekee map->no_sync_defaults = true;
56581485f52SLars-Peter Clausen map->unlock(map->lock_arg);
5668ae0d7e8SMark Brown }
5678ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty);
5688ae0d7e8SMark Brown
5698ae0d7e8SMark Brown /**
5702cf8e2dfSCharles Keepax * regcache_cache_bypass - Put a register map into cache bypass mode
5716eb0f5e0SDimitris Papastamos *
5726eb0f5e0SDimitris Papastamos * @map: map to configure
5732cf8e2dfSCharles Keepax * @enable: flag if changes should not be written to the cache
5746eb0f5e0SDimitris Papastamos *
5756eb0f5e0SDimitris Papastamos * When a register map is marked with the cache bypass option, writes
57672607f37SXiang wangx * to the register map API will only update the hardware and not
5776eb0f5e0SDimitris Papastamos * the cache directly. This is useful when syncing the cache back to
5786eb0f5e0SDimitris Papastamos * the hardware.
5796eb0f5e0SDimitris Papastamos */
regcache_cache_bypass(struct regmap * map,bool enable)5806eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable)
5816eb0f5e0SDimitris Papastamos {
58281485f52SLars-Peter Clausen map->lock(map->lock_arg);
583ac77a765SDimitris Papastamos WARN_ON(map->cache_only && enable);
5846eb0f5e0SDimitris Papastamos map->cache_bypass = enable;
585c6b570d9SPhilipp Zabel trace_regmap_cache_bypass(map, enable);
58681485f52SLars-Peter Clausen map->unlock(map->lock_arg);
5876eb0f5e0SDimitris Papastamos }
5886eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass);
5896eb0f5e0SDimitris Papastamos
59078908f45SMark Brown /**
59178908f45SMark Brown * regcache_reg_cached - Check if a register is cached
59278908f45SMark Brown *
59378908f45SMark Brown * @map: map to check
59478908f45SMark Brown * @reg: register to check
59578908f45SMark Brown *
59678908f45SMark Brown * Reports if a register is cached.
59778908f45SMark Brown */
regcache_reg_cached(struct regmap * map,unsigned int reg)59878908f45SMark Brown bool regcache_reg_cached(struct regmap *map, unsigned int reg)
59978908f45SMark Brown {
60078908f45SMark Brown unsigned int val;
60178908f45SMark Brown int ret;
60278908f45SMark Brown
60378908f45SMark Brown map->lock(map->lock_arg);
60478908f45SMark Brown
60578908f45SMark Brown ret = regcache_read(map, reg, &val);
60678908f45SMark Brown
60778908f45SMark Brown map->unlock(map->lock_arg);
60878908f45SMark Brown
60978908f45SMark Brown return ret == 0;
61078908f45SMark Brown }
61178908f45SMark Brown EXPORT_SYMBOL_GPL(regcache_reg_cached);
61278908f45SMark Brown
regcache_set_val(struct regmap * map,void * base,unsigned int idx,unsigned int val)613d32758acSMark Brown void regcache_set_val(struct regmap *map, void *base, unsigned int idx,
614879082c9SMark Brown unsigned int val)
6159fabe24eSDimitris Papastamos {
616eb4cb76fSMark Brown /* Use device native format if possible */
617eb4cb76fSMark Brown if (map->format.format_val) {
618eb4cb76fSMark Brown map->format.format_val(base + (map->cache_word_size * idx),
619eb4cb76fSMark Brown val, 0);
620d32758acSMark Brown return;
621eb4cb76fSMark Brown }
622eb4cb76fSMark Brown
623879082c9SMark Brown switch (map->cache_word_size) {
6249fabe24eSDimitris Papastamos case 1: {
6259fabe24eSDimitris Papastamos u8 *cache = base;
6262fd6902eSXiubo Li
6279fabe24eSDimitris Papastamos cache[idx] = val;
6289fabe24eSDimitris Papastamos break;
6299fabe24eSDimitris Papastamos }
6309fabe24eSDimitris Papastamos case 2: {
6319fabe24eSDimitris Papastamos u16 *cache = base;
6322fd6902eSXiubo Li
6339fabe24eSDimitris Papastamos cache[idx] = val;
6349fabe24eSDimitris Papastamos break;
6359fabe24eSDimitris Papastamos }
6367d5e525bSMark Brown case 4: {
6377d5e525bSMark Brown u32 *cache = base;
6382fd6902eSXiubo Li
6397d5e525bSMark Brown cache[idx] = val;
6407d5e525bSMark Brown break;
6417d5e525bSMark Brown }
6429fabe24eSDimitris Papastamos default:
6439fabe24eSDimitris Papastamos BUG();
6449fabe24eSDimitris Papastamos }
6459fabe24eSDimitris Papastamos }
6469fabe24eSDimitris Papastamos
regcache_get_val(struct regmap * map,const void * base,unsigned int idx)647879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base,
648879082c9SMark Brown unsigned int idx)
6499fabe24eSDimitris Papastamos {
6509fabe24eSDimitris Papastamos if (!base)
6519fabe24eSDimitris Papastamos return -EINVAL;
6529fabe24eSDimitris Papastamos
653eb4cb76fSMark Brown /* Use device native format if possible */
654eb4cb76fSMark Brown if (map->format.parse_val)
6558817796bSMark Brown return map->format.parse_val(regcache_get_val_addr(map, base,
6568817796bSMark Brown idx));
657eb4cb76fSMark Brown
658879082c9SMark Brown switch (map->cache_word_size) {
6599fabe24eSDimitris Papastamos case 1: {
6609fabe24eSDimitris Papastamos const u8 *cache = base;
6612fd6902eSXiubo Li
6629fabe24eSDimitris Papastamos return cache[idx];
6639fabe24eSDimitris Papastamos }
6649fabe24eSDimitris Papastamos case 2: {
6659fabe24eSDimitris Papastamos const u16 *cache = base;
6662fd6902eSXiubo Li
6679fabe24eSDimitris Papastamos return cache[idx];
6689fabe24eSDimitris Papastamos }
6697d5e525bSMark Brown case 4: {
6707d5e525bSMark Brown const u32 *cache = base;
6712fd6902eSXiubo Li
6727d5e525bSMark Brown return cache[idx];
6737d5e525bSMark Brown }
6749fabe24eSDimitris Papastamos default:
6759fabe24eSDimitris Papastamos BUG();
6769fabe24eSDimitris Papastamos }
6779fabe24eSDimitris Papastamos /* unreachable */
6789fabe24eSDimitris Papastamos return -1;
6799fabe24eSDimitris Papastamos }
6809fabe24eSDimitris Papastamos
regcache_default_cmp(const void * a,const void * b)681f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b)
682c08604b8SDimitris Papastamos {
683c08604b8SDimitris Papastamos const struct reg_default *_a = a;
684c08604b8SDimitris Papastamos const struct reg_default *_b = b;
685c08604b8SDimitris Papastamos
686c08604b8SDimitris Papastamos return _a->reg - _b->reg;
687c08604b8SDimitris Papastamos }
688c08604b8SDimitris Papastamos
regcache_lookup_reg(struct regmap * map,unsigned int reg)689f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg)
690f094fea6SMark Brown {
691f094fea6SMark Brown struct reg_default key;
692f094fea6SMark Brown struct reg_default *r;
693f094fea6SMark Brown
694f094fea6SMark Brown key.reg = reg;
695f094fea6SMark Brown key.def = 0;
696f094fea6SMark Brown
697f094fea6SMark Brown r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
698f094fea6SMark Brown sizeof(struct reg_default), regcache_default_cmp);
699f094fea6SMark Brown
700f094fea6SMark Brown if (r)
701f094fea6SMark Brown return r - map->reg_defaults;
702f094fea6SMark Brown else
7036e6ace00SMark Brown return -ENOENT;
704f094fea6SMark Brown }
705f8bd822cSMark Brown
regcache_reg_present(unsigned long * cache_present,unsigned int idx)7063f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
7073f4ff561SLars-Peter Clausen {
7083f4ff561SLars-Peter Clausen if (!cache_present)
7093f4ff561SLars-Peter Clausen return true;
7103f4ff561SLars-Peter Clausen
7113f4ff561SLars-Peter Clausen return test_bit(idx, cache_present);
7123f4ff561SLars-Peter Clausen }
7133f4ff561SLars-Peter Clausen
regcache_sync_val(struct regmap * map,unsigned int reg,unsigned int val)71405933e2dSMark Brown int regcache_sync_val(struct regmap *map, unsigned int reg, unsigned int val)
71505933e2dSMark Brown {
71605933e2dSMark Brown int ret;
71705933e2dSMark Brown
71805933e2dSMark Brown if (!regcache_reg_needs_sync(map, reg, val))
71905933e2dSMark Brown return 0;
72005933e2dSMark Brown
72105933e2dSMark Brown map->cache_bypass = true;
72205933e2dSMark Brown
72305933e2dSMark Brown ret = _regmap_write(map, reg, val);
72405933e2dSMark Brown
72505933e2dSMark Brown map->cache_bypass = false;
72605933e2dSMark Brown
72705933e2dSMark Brown if (ret != 0) {
72805933e2dSMark Brown dev_err(map->dev, "Unable to sync register %#x. %d\n",
72905933e2dSMark Brown reg, ret);
73005933e2dSMark Brown return ret;
73105933e2dSMark Brown }
73205933e2dSMark Brown dev_dbg(map->dev, "Synced register %#x, value %#x\n",
73305933e2dSMark Brown reg, val);
73405933e2dSMark Brown
73505933e2dSMark Brown return 0;
73605933e2dSMark Brown }
73705933e2dSMark Brown
regcache_sync_block_single(struct regmap * map,void * block,unsigned long * cache_present,unsigned int block_base,unsigned int start,unsigned int end)738cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block,
7393f4ff561SLars-Peter Clausen unsigned long *cache_present,
740cfdeb8c3SMark Brown unsigned int block_base,
741cfdeb8c3SMark Brown unsigned int start, unsigned int end)
742cfdeb8c3SMark Brown {
743cfdeb8c3SMark Brown unsigned int i, regtmp, val;
744cfdeb8c3SMark Brown int ret;
745cfdeb8c3SMark Brown
746cfdeb8c3SMark Brown for (i = start; i < end; i++) {
747cfdeb8c3SMark Brown regtmp = block_base + (i * map->reg_stride);
748cfdeb8c3SMark Brown
7494ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) ||
7504ceba98dSTakashi Iwai !regmap_writeable(map, regtmp))
751cfdeb8c3SMark Brown continue;
752cfdeb8c3SMark Brown
753cfdeb8c3SMark Brown val = regcache_get_val(map, block, i);
75405933e2dSMark Brown ret = regcache_sync_val(map, regtmp, val);
75505933e2dSMark Brown if (ret != 0)
756cfdeb8c3SMark Brown return ret;
757f29a4320SJarkko Nikula }
758cfdeb8c3SMark Brown
759cfdeb8c3SMark Brown return 0;
760cfdeb8c3SMark Brown }
761cfdeb8c3SMark Brown
regcache_sync_block_raw_flush(struct regmap * map,const void ** data,unsigned int base,unsigned int cur)76275a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
76375a5f89fSMark Brown unsigned int base, unsigned int cur)
76475a5f89fSMark Brown {
76575a5f89fSMark Brown size_t val_bytes = map->format.val_bytes;
76675a5f89fSMark Brown int ret, count;
76775a5f89fSMark Brown
76875a5f89fSMark Brown if (*data == NULL)
76975a5f89fSMark Brown return 0;
77075a5f89fSMark Brown
77178ba73eeSDylan Reid count = (cur - base) / map->reg_stride;
77275a5f89fSMark Brown
7739659293cSStratos Karafotis dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
77478ba73eeSDylan Reid count * val_bytes, count, base, cur - map->reg_stride);
77575a5f89fSMark Brown
776621a5f7aSViresh Kumar map->cache_bypass = true;
77775a5f89fSMark Brown
77805669b63SDmitry Baryshkov ret = _regmap_raw_write(map, base, *data, count * val_bytes, false);
779f29a4320SJarkko Nikula if (ret)
780f29a4320SJarkko Nikula dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
781f29a4320SJarkko Nikula base, cur - map->reg_stride, ret);
78275a5f89fSMark Brown
783621a5f7aSViresh Kumar map->cache_bypass = false;
78475a5f89fSMark Brown
78575a5f89fSMark Brown *data = NULL;
78675a5f89fSMark Brown
78775a5f89fSMark Brown return ret;
78875a5f89fSMark Brown }
78975a5f89fSMark Brown
regcache_sync_block_raw(struct regmap * map,void * block,unsigned long * cache_present,unsigned int block_base,unsigned int start,unsigned int end)790f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block,
7913f4ff561SLars-Peter Clausen unsigned long *cache_present,
792f8bd822cSMark Brown unsigned int block_base, unsigned int start,
793f8bd822cSMark Brown unsigned int end)
794f8bd822cSMark Brown {
79575a5f89fSMark Brown unsigned int i, val;
79675a5f89fSMark Brown unsigned int regtmp = 0;
79775a5f89fSMark Brown unsigned int base = 0;
79875a5f89fSMark Brown const void *data = NULL;
799f8bd822cSMark Brown int ret;
800f8bd822cSMark Brown
801f8bd822cSMark Brown for (i = start; i < end; i++) {
802f8bd822cSMark Brown regtmp = block_base + (i * map->reg_stride);
803f8bd822cSMark Brown
8044ceba98dSTakashi Iwai if (!regcache_reg_present(cache_present, i) ||
8054ceba98dSTakashi Iwai !regmap_writeable(map, regtmp)) {
80675a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data,
80775a5f89fSMark Brown base, regtmp);
80875a5f89fSMark Brown if (ret != 0)
80975a5f89fSMark Brown return ret;
810f8bd822cSMark Brown continue;
81175a5f89fSMark Brown }
812f8bd822cSMark Brown
813f8bd822cSMark Brown val = regcache_get_val(map, block, i);
8143969fa08SKevin Cernekee if (!regcache_reg_needs_sync(map, regtmp, val)) {
81575a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data,
81675a5f89fSMark Brown base, regtmp);
817f8bd822cSMark Brown if (ret != 0)
818f8bd822cSMark Brown return ret;
81975a5f89fSMark Brown continue;
820f8bd822cSMark Brown }
821f8bd822cSMark Brown
82275a5f89fSMark Brown if (!data) {
82375a5f89fSMark Brown data = regcache_get_val_addr(map, block, i);
82475a5f89fSMark Brown base = regtmp;
82575a5f89fSMark Brown }
82675a5f89fSMark Brown }
82775a5f89fSMark Brown
8282d49b598SLars-Peter Clausen return regcache_sync_block_raw_flush(map, &data, base, regtmp +
8292d49b598SLars-Peter Clausen map->reg_stride);
830f8bd822cSMark Brown }
831cfdeb8c3SMark Brown
regcache_sync_block(struct regmap * map,void * block,unsigned long * cache_present,unsigned int block_base,unsigned int start,unsigned int end)832cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block,
8333f4ff561SLars-Peter Clausen unsigned long *cache_present,
834cfdeb8c3SMark Brown unsigned int block_base, unsigned int start,
835cfdeb8c3SMark Brown unsigned int end)
836cfdeb8c3SMark Brown {
83767921a1aSMarkus Pargmann if (regmap_can_raw_write(map) && !map->use_single_write)
8383f4ff561SLars-Peter Clausen return regcache_sync_block_raw(map, block, cache_present,
8393f4ff561SLars-Peter Clausen block_base, start, end);
840cfdeb8c3SMark Brown else
8413f4ff561SLars-Peter Clausen return regcache_sync_block_single(map, block, cache_present,
8423f4ff561SLars-Peter Clausen block_base, start, end);
843cfdeb8c3SMark Brown }
844