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 ®cache_rbtree_ops, 242cbbb579SDimitris Papastamos ®cache_lzo_ops, 252ac902ceSMark Brown ®cache_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 25275617328SDylan Reid for (reg = min; reg <= max; reg += map->reg_stride) { 253d856fce4SMaarten ter Huurne unsigned int val; 254d856fce4SMaarten ter Huurne int ret; 255d856fce4SMaarten ter Huurne 25683f8475cSDylan Reid if (regmap_volatile(map, reg) || 25783f8475cSDylan Reid !regmap_writeable(map, reg)) 258d856fce4SMaarten ter Huurne continue; 259d856fce4SMaarten ter Huurne 260d856fce4SMaarten ter Huurne ret = regcache_read(map, reg, &val); 261d856fce4SMaarten ter Huurne if (ret) 262d856fce4SMaarten ter Huurne return ret; 263d856fce4SMaarten ter Huurne 264d856fce4SMaarten ter Huurne /* Is this the hardware default? If so skip. */ 265d856fce4SMaarten ter Huurne ret = regcache_lookup_reg(map, reg); 266d856fce4SMaarten ter Huurne if (ret >= 0 && val == map->reg_defaults[ret].def) 267d856fce4SMaarten ter Huurne continue; 268d856fce4SMaarten ter Huurne 269d856fce4SMaarten ter Huurne map->cache_bypass = 1; 270d856fce4SMaarten ter Huurne ret = _regmap_write(map, reg, val); 271d856fce4SMaarten ter Huurne map->cache_bypass = 0; 272d856fce4SMaarten ter Huurne if (ret) 273d856fce4SMaarten ter Huurne return ret; 274d856fce4SMaarten ter Huurne dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val); 275d856fce4SMaarten ter Huurne } 276d856fce4SMaarten ter Huurne 277d856fce4SMaarten ter Huurne return 0; 278d856fce4SMaarten ter Huurne } 279d856fce4SMaarten ter Huurne 2809fabe24eSDimitris Papastamos /** 2819fabe24eSDimitris Papastamos * regcache_sync: Sync the register cache with the hardware. 2829fabe24eSDimitris Papastamos * 2839fabe24eSDimitris Papastamos * @map: map to configure. 2849fabe24eSDimitris Papastamos * 2859fabe24eSDimitris Papastamos * Any registers that should not be synced should be marked as 2869fabe24eSDimitris Papastamos * volatile. In general drivers can choose not to use the provided 2879fabe24eSDimitris Papastamos * syncing functionality if they so require. 2889fabe24eSDimitris Papastamos * 2899fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2909fabe24eSDimitris Papastamos */ 2919fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map) 2929fabe24eSDimitris Papastamos { 293954757d7SDimitris Papastamos int ret = 0; 294954757d7SDimitris Papastamos unsigned int i; 29559360089SDimitris Papastamos const char *name; 296beb1a10fSDimitris Papastamos unsigned int bypass; 29759360089SDimitris Papastamos 298d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 2999fabe24eSDimitris Papastamos 30081485f52SLars-Peter Clausen map->lock(map->lock_arg); 301beb1a10fSDimitris Papastamos /* Remember the initial bypass state */ 302beb1a10fSDimitris Papastamos bypass = map->cache_bypass; 3039fabe24eSDimitris Papastamos dev_dbg(map->dev, "Syncing %s cache\n", 3049fabe24eSDimitris Papastamos map->cache_ops->name); 30559360089SDimitris Papastamos name = map->cache_ops->name; 30659360089SDimitris Papastamos trace_regcache_sync(map->dev, name, "start"); 30722f0d90aSMark Brown 3088ae0d7e8SMark Brown if (!map->cache_dirty) 3098ae0d7e8SMark Brown goto out; 310d9db7627SMark Brown 311affbe886SMark Brown map->async = true; 312affbe886SMark Brown 31322f0d90aSMark Brown /* Apply any patch first */ 3148a892d69SMark Brown map->cache_bypass = 1; 31522f0d90aSMark Brown for (i = 0; i < map->patch_regs; i++) { 31622f0d90aSMark Brown ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); 31722f0d90aSMark Brown if (ret != 0) { 31822f0d90aSMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n", 31922f0d90aSMark Brown map->patch[i].reg, map->patch[i].def, ret); 32022f0d90aSMark Brown goto out; 32122f0d90aSMark Brown } 32222f0d90aSMark Brown } 3238a892d69SMark Brown map->cache_bypass = 0; 32422f0d90aSMark Brown 325d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 326ac8d91c8SMark Brown ret = map->cache_ops->sync(map, 0, map->max_register); 327d856fce4SMaarten ter Huurne else 328d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, 0, map->max_register); 329954757d7SDimitris Papastamos 3306ff73738SMark Brown if (ret == 0) 3316ff73738SMark Brown map->cache_dirty = false; 3326ff73738SMark Brown 333954757d7SDimitris Papastamos out: 334beb1a10fSDimitris Papastamos /* Restore the bypass state */ 335affbe886SMark Brown map->async = false; 336beb1a10fSDimitris Papastamos map->cache_bypass = bypass; 33781485f52SLars-Peter Clausen map->unlock(map->lock_arg); 338954757d7SDimitris Papastamos 339affbe886SMark Brown regmap_async_complete(map); 340affbe886SMark Brown 341affbe886SMark Brown trace_regcache_sync(map->dev, name, "stop"); 342affbe886SMark Brown 343954757d7SDimitris Papastamos return ret; 3449fabe24eSDimitris Papastamos } 3459fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync); 3469fabe24eSDimitris Papastamos 34792afb286SMark Brown /** 3484d4cfd16SMark Brown * regcache_sync_region: Sync part of the register cache with the hardware. 3494d4cfd16SMark Brown * 3504d4cfd16SMark Brown * @map: map to sync. 3514d4cfd16SMark Brown * @min: first register to sync 3524d4cfd16SMark Brown * @max: last register to sync 3534d4cfd16SMark Brown * 3544d4cfd16SMark Brown * Write all non-default register values in the specified region to 3554d4cfd16SMark Brown * the hardware. 3564d4cfd16SMark Brown * 3574d4cfd16SMark Brown * Return a negative value on failure, 0 on success. 3584d4cfd16SMark Brown */ 3594d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min, 3604d4cfd16SMark Brown unsigned int max) 3614d4cfd16SMark Brown { 3624d4cfd16SMark Brown int ret = 0; 3634d4cfd16SMark Brown const char *name; 3644d4cfd16SMark Brown unsigned int bypass; 3654d4cfd16SMark Brown 366d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3674d4cfd16SMark Brown 36881485f52SLars-Peter Clausen map->lock(map->lock_arg); 3694d4cfd16SMark Brown 3704d4cfd16SMark Brown /* Remember the initial bypass state */ 3714d4cfd16SMark Brown bypass = map->cache_bypass; 3724d4cfd16SMark Brown 3734d4cfd16SMark Brown name = map->cache_ops->name; 3744d4cfd16SMark Brown dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); 3754d4cfd16SMark Brown 3764d4cfd16SMark Brown trace_regcache_sync(map->dev, name, "start region"); 3774d4cfd16SMark Brown 3784d4cfd16SMark Brown if (!map->cache_dirty) 3794d4cfd16SMark Brown goto out; 3804d4cfd16SMark Brown 381affbe886SMark Brown map->async = true; 382affbe886SMark Brown 383d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 3844d4cfd16SMark Brown ret = map->cache_ops->sync(map, min, max); 385d856fce4SMaarten ter Huurne else 386d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, min, max); 3874d4cfd16SMark Brown 3884d4cfd16SMark Brown out: 3894d4cfd16SMark Brown /* Restore the bypass state */ 3904d4cfd16SMark Brown map->cache_bypass = bypass; 391affbe886SMark Brown map->async = false; 39281485f52SLars-Peter Clausen map->unlock(map->lock_arg); 3934d4cfd16SMark Brown 394affbe886SMark Brown regmap_async_complete(map); 395affbe886SMark Brown 396affbe886SMark Brown trace_regcache_sync(map->dev, name, "stop region"); 397affbe886SMark Brown 3984d4cfd16SMark Brown return ret; 3994d4cfd16SMark Brown } 400e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region); 4014d4cfd16SMark Brown 4024d4cfd16SMark Brown /** 403697e85bcSMark Brown * regcache_drop_region: Discard part of the register cache 404697e85bcSMark Brown * 405697e85bcSMark Brown * @map: map to operate on 406697e85bcSMark Brown * @min: first register to discard 407697e85bcSMark Brown * @max: last register to discard 408697e85bcSMark Brown * 409697e85bcSMark Brown * Discard part of the register cache. 410697e85bcSMark Brown * 411697e85bcSMark Brown * Return a negative value on failure, 0 on success. 412697e85bcSMark Brown */ 413697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min, 414697e85bcSMark Brown unsigned int max) 415697e85bcSMark Brown { 416697e85bcSMark Brown int ret = 0; 417697e85bcSMark Brown 4183f4ff561SLars-Peter Clausen if (!map->cache_ops || !map->cache_ops->drop) 419697e85bcSMark Brown return -EINVAL; 420697e85bcSMark Brown 42181485f52SLars-Peter Clausen map->lock(map->lock_arg); 422697e85bcSMark Brown 423697e85bcSMark Brown trace_regcache_drop_region(map->dev, min, max); 424697e85bcSMark Brown 425697e85bcSMark Brown ret = map->cache_ops->drop(map, min, max); 426697e85bcSMark Brown 42781485f52SLars-Peter Clausen map->unlock(map->lock_arg); 428697e85bcSMark Brown 429697e85bcSMark Brown return ret; 430697e85bcSMark Brown } 431697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region); 432697e85bcSMark Brown 433697e85bcSMark Brown /** 43492afb286SMark Brown * regcache_cache_only: Put a register map into cache only mode 43592afb286SMark Brown * 43692afb286SMark Brown * @map: map to configure 43792afb286SMark Brown * @cache_only: flag if changes should be written to the hardware 43892afb286SMark Brown * 43992afb286SMark Brown * When a register map is marked as cache only writes to the register 44092afb286SMark Brown * map API will only update the register cache, they will not cause 44192afb286SMark Brown * any hardware changes. This is useful for allowing portions of 44292afb286SMark Brown * drivers to act as though the device were functioning as normal when 44392afb286SMark Brown * it is disabled for power saving reasons. 44492afb286SMark Brown */ 44592afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable) 44692afb286SMark Brown { 44781485f52SLars-Peter Clausen map->lock(map->lock_arg); 448ac77a765SDimitris Papastamos WARN_ON(map->cache_bypass && enable); 44992afb286SMark Brown map->cache_only = enable; 4505d5b7d4fSMark Brown trace_regmap_cache_only(map->dev, enable); 45181485f52SLars-Peter Clausen map->unlock(map->lock_arg); 45292afb286SMark Brown } 45392afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only); 45492afb286SMark Brown 4556eb0f5e0SDimitris Papastamos /** 4568ae0d7e8SMark Brown * regcache_mark_dirty: Mark the register cache as dirty 4578ae0d7e8SMark Brown * 4588ae0d7e8SMark Brown * @map: map to mark 4598ae0d7e8SMark Brown * 4608ae0d7e8SMark Brown * Mark the register cache as dirty, for example due to the device 4618ae0d7e8SMark Brown * having been powered down for suspend. If the cache is not marked 4628ae0d7e8SMark Brown * as dirty then the cache sync will be suppressed. 4638ae0d7e8SMark Brown */ 4648ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map) 4658ae0d7e8SMark Brown { 46681485f52SLars-Peter Clausen map->lock(map->lock_arg); 4678ae0d7e8SMark Brown map->cache_dirty = true; 46881485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4698ae0d7e8SMark Brown } 4708ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty); 4718ae0d7e8SMark Brown 4728ae0d7e8SMark Brown /** 4736eb0f5e0SDimitris Papastamos * regcache_cache_bypass: Put a register map into cache bypass mode 4746eb0f5e0SDimitris Papastamos * 4756eb0f5e0SDimitris Papastamos * @map: map to configure 4760eef6b04SDimitris Papastamos * @cache_bypass: flag if changes should not be written to the hardware 4776eb0f5e0SDimitris Papastamos * 4786eb0f5e0SDimitris Papastamos * When a register map is marked with the cache bypass option, writes 4796eb0f5e0SDimitris Papastamos * to the register map API will only update the hardware and not the 4806eb0f5e0SDimitris Papastamos * the cache directly. This is useful when syncing the cache back to 4816eb0f5e0SDimitris Papastamos * the hardware. 4826eb0f5e0SDimitris Papastamos */ 4836eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable) 4846eb0f5e0SDimitris Papastamos { 48581485f52SLars-Peter Clausen map->lock(map->lock_arg); 486ac77a765SDimitris Papastamos WARN_ON(map->cache_only && enable); 4876eb0f5e0SDimitris Papastamos map->cache_bypass = enable; 4885d5b7d4fSMark Brown trace_regmap_cache_bypass(map->dev, enable); 48981485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4906eb0f5e0SDimitris Papastamos } 4916eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass); 4926eb0f5e0SDimitris Papastamos 493879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, 494879082c9SMark Brown unsigned int val) 4959fabe24eSDimitris Papastamos { 496325acab4SMark Brown if (regcache_get_val(map, base, idx) == val) 497325acab4SMark Brown return true; 498325acab4SMark Brown 499eb4cb76fSMark Brown /* Use device native format if possible */ 500eb4cb76fSMark Brown if (map->format.format_val) { 501eb4cb76fSMark Brown map->format.format_val(base + (map->cache_word_size * idx), 502eb4cb76fSMark Brown val, 0); 503eb4cb76fSMark Brown return false; 504eb4cb76fSMark Brown } 505eb4cb76fSMark Brown 506879082c9SMark Brown switch (map->cache_word_size) { 5079fabe24eSDimitris Papastamos case 1: { 5089fabe24eSDimitris Papastamos u8 *cache = base; 5099fabe24eSDimitris Papastamos cache[idx] = val; 5109fabe24eSDimitris Papastamos break; 5119fabe24eSDimitris Papastamos } 5129fabe24eSDimitris Papastamos case 2: { 5139fabe24eSDimitris Papastamos u16 *cache = base; 5149fabe24eSDimitris Papastamos cache[idx] = val; 5159fabe24eSDimitris Papastamos break; 5169fabe24eSDimitris Papastamos } 5177d5e525bSMark Brown case 4: { 5187d5e525bSMark Brown u32 *cache = base; 5197d5e525bSMark Brown cache[idx] = val; 5207d5e525bSMark Brown break; 5217d5e525bSMark Brown } 5229fabe24eSDimitris Papastamos default: 5239fabe24eSDimitris Papastamos BUG(); 5249fabe24eSDimitris Papastamos } 5259fabe24eSDimitris Papastamos return false; 5269fabe24eSDimitris Papastamos } 5279fabe24eSDimitris Papastamos 528879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base, 529879082c9SMark Brown unsigned int idx) 5309fabe24eSDimitris Papastamos { 5319fabe24eSDimitris Papastamos if (!base) 5329fabe24eSDimitris Papastamos return -EINVAL; 5339fabe24eSDimitris Papastamos 534eb4cb76fSMark Brown /* Use device native format if possible */ 535eb4cb76fSMark Brown if (map->format.parse_val) 5368817796bSMark Brown return map->format.parse_val(regcache_get_val_addr(map, base, 5378817796bSMark Brown idx)); 538eb4cb76fSMark Brown 539879082c9SMark Brown switch (map->cache_word_size) { 5409fabe24eSDimitris Papastamos case 1: { 5419fabe24eSDimitris Papastamos const u8 *cache = base; 5429fabe24eSDimitris Papastamos return cache[idx]; 5439fabe24eSDimitris Papastamos } 5449fabe24eSDimitris Papastamos case 2: { 5459fabe24eSDimitris Papastamos const u16 *cache = base; 5469fabe24eSDimitris Papastamos return cache[idx]; 5479fabe24eSDimitris Papastamos } 5487d5e525bSMark Brown case 4: { 5497d5e525bSMark Brown const u32 *cache = base; 5507d5e525bSMark Brown return cache[idx]; 5517d5e525bSMark Brown } 5529fabe24eSDimitris Papastamos default: 5539fabe24eSDimitris Papastamos BUG(); 5549fabe24eSDimitris Papastamos } 5559fabe24eSDimitris Papastamos /* unreachable */ 5569fabe24eSDimitris Papastamos return -1; 5579fabe24eSDimitris Papastamos } 5589fabe24eSDimitris Papastamos 559f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b) 560c08604b8SDimitris Papastamos { 561c08604b8SDimitris Papastamos const struct reg_default *_a = a; 562c08604b8SDimitris Papastamos const struct reg_default *_b = b; 563c08604b8SDimitris Papastamos 564c08604b8SDimitris Papastamos return _a->reg - _b->reg; 565c08604b8SDimitris Papastamos } 566c08604b8SDimitris Papastamos 567f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg) 568f094fea6SMark Brown { 569f094fea6SMark Brown struct reg_default key; 570f094fea6SMark Brown struct reg_default *r; 571f094fea6SMark Brown 572f094fea6SMark Brown key.reg = reg; 573f094fea6SMark Brown key.def = 0; 574f094fea6SMark Brown 575f094fea6SMark Brown r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, 576f094fea6SMark Brown sizeof(struct reg_default), regcache_default_cmp); 577f094fea6SMark Brown 578f094fea6SMark Brown if (r) 579f094fea6SMark Brown return r - map->reg_defaults; 580f094fea6SMark Brown else 5816e6ace00SMark Brown return -ENOENT; 582f094fea6SMark Brown } 583f8bd822cSMark Brown 5843f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx) 5853f4ff561SLars-Peter Clausen { 5863f4ff561SLars-Peter Clausen if (!cache_present) 5873f4ff561SLars-Peter Clausen return true; 5883f4ff561SLars-Peter Clausen 5893f4ff561SLars-Peter Clausen return test_bit(idx, cache_present); 5903f4ff561SLars-Peter Clausen } 5913f4ff561SLars-Peter Clausen 592cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block, 5933f4ff561SLars-Peter Clausen unsigned long *cache_present, 594cfdeb8c3SMark Brown unsigned int block_base, 595cfdeb8c3SMark Brown unsigned int start, unsigned int end) 596cfdeb8c3SMark Brown { 597cfdeb8c3SMark Brown unsigned int i, regtmp, val; 598cfdeb8c3SMark Brown int ret; 599cfdeb8c3SMark Brown 600cfdeb8c3SMark Brown for (i = start; i < end; i++) { 601cfdeb8c3SMark Brown regtmp = block_base + (i * map->reg_stride); 602cfdeb8c3SMark Brown 6033f4ff561SLars-Peter Clausen if (!regcache_reg_present(cache_present, i)) 604cfdeb8c3SMark Brown continue; 605cfdeb8c3SMark Brown 606cfdeb8c3SMark Brown val = regcache_get_val(map, block, i); 607cfdeb8c3SMark Brown 608cfdeb8c3SMark Brown /* Is this the hardware default? If so skip. */ 609cfdeb8c3SMark Brown ret = regcache_lookup_reg(map, regtmp); 610cfdeb8c3SMark Brown if (ret >= 0 && val == map->reg_defaults[ret].def) 611cfdeb8c3SMark Brown continue; 612cfdeb8c3SMark Brown 613cfdeb8c3SMark Brown map->cache_bypass = 1; 614cfdeb8c3SMark Brown 615cfdeb8c3SMark Brown ret = _regmap_write(map, regtmp, val); 616cfdeb8c3SMark Brown 617cfdeb8c3SMark Brown map->cache_bypass = 0; 618cfdeb8c3SMark Brown if (ret != 0) 619cfdeb8c3SMark Brown return ret; 620cfdeb8c3SMark Brown dev_dbg(map->dev, "Synced register %#x, value %#x\n", 621cfdeb8c3SMark Brown regtmp, val); 622cfdeb8c3SMark Brown } 623cfdeb8c3SMark Brown 624cfdeb8c3SMark Brown return 0; 625cfdeb8c3SMark Brown } 626cfdeb8c3SMark Brown 62775a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data, 62875a5f89fSMark Brown unsigned int base, unsigned int cur) 62975a5f89fSMark Brown { 63075a5f89fSMark Brown size_t val_bytes = map->format.val_bytes; 63175a5f89fSMark Brown int ret, count; 63275a5f89fSMark Brown 63375a5f89fSMark Brown if (*data == NULL) 63475a5f89fSMark Brown return 0; 63575a5f89fSMark Brown 63678ba73eeSDylan Reid count = (cur - base) / map->reg_stride; 63775a5f89fSMark Brown 6389659293cSStratos Karafotis dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n", 63978ba73eeSDylan Reid count * val_bytes, count, base, cur - map->reg_stride); 64075a5f89fSMark Brown 64175a5f89fSMark Brown map->cache_bypass = 1; 64275a5f89fSMark Brown 6430a819809SMark Brown ret = _regmap_raw_write(map, base, *data, count * val_bytes); 64475a5f89fSMark Brown 64575a5f89fSMark Brown map->cache_bypass = 0; 64675a5f89fSMark Brown 64775a5f89fSMark Brown *data = NULL; 64875a5f89fSMark Brown 64975a5f89fSMark Brown return ret; 65075a5f89fSMark Brown } 65175a5f89fSMark Brown 652f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block, 6533f4ff561SLars-Peter Clausen unsigned long *cache_present, 654f8bd822cSMark Brown unsigned int block_base, unsigned int start, 655f8bd822cSMark Brown unsigned int end) 656f8bd822cSMark Brown { 65775a5f89fSMark Brown unsigned int i, val; 65875a5f89fSMark Brown unsigned int regtmp = 0; 65975a5f89fSMark Brown unsigned int base = 0; 66075a5f89fSMark Brown const void *data = NULL; 661f8bd822cSMark Brown int ret; 662f8bd822cSMark Brown 663f8bd822cSMark Brown for (i = start; i < end; i++) { 664f8bd822cSMark Brown regtmp = block_base + (i * map->reg_stride); 665f8bd822cSMark Brown 6663f4ff561SLars-Peter Clausen if (!regcache_reg_present(cache_present, i)) { 66775a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 66875a5f89fSMark Brown base, regtmp); 66975a5f89fSMark Brown if (ret != 0) 67075a5f89fSMark Brown return ret; 671f8bd822cSMark Brown continue; 67275a5f89fSMark Brown } 673f8bd822cSMark Brown 674f8bd822cSMark Brown val = regcache_get_val(map, block, i); 675f8bd822cSMark Brown 676f8bd822cSMark Brown /* Is this the hardware default? If so skip. */ 677f8bd822cSMark Brown ret = regcache_lookup_reg(map, regtmp); 67875a5f89fSMark Brown if (ret >= 0 && val == map->reg_defaults[ret].def) { 67975a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 68075a5f89fSMark Brown base, regtmp); 681f8bd822cSMark Brown if (ret != 0) 682f8bd822cSMark Brown return ret; 68375a5f89fSMark Brown continue; 684f8bd822cSMark Brown } 685f8bd822cSMark Brown 68675a5f89fSMark Brown if (!data) { 68775a5f89fSMark Brown data = regcache_get_val_addr(map, block, i); 68875a5f89fSMark Brown base = regtmp; 68975a5f89fSMark Brown } 69075a5f89fSMark Brown } 69175a5f89fSMark Brown 6922d49b598SLars-Peter Clausen return regcache_sync_block_raw_flush(map, &data, base, regtmp + 6932d49b598SLars-Peter Clausen map->reg_stride); 694f8bd822cSMark Brown } 695cfdeb8c3SMark Brown 696cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block, 6973f4ff561SLars-Peter Clausen unsigned long *cache_present, 698cfdeb8c3SMark Brown unsigned int block_base, unsigned int start, 699cfdeb8c3SMark Brown unsigned int end) 700cfdeb8c3SMark Brown { 701*5c1ebe7fSMark Brown if (regmap_can_raw_write(map) && !map->use_single_rw) 7023f4ff561SLars-Peter Clausen return regcache_sync_block_raw(map, block, cache_present, 7033f4ff561SLars-Peter Clausen block_base, start, end); 704cfdeb8c3SMark Brown else 7053f4ff561SLars-Peter Clausen return regcache_sync_block_single(map, block, cache_present, 7063f4ff561SLars-Peter Clausen block_base, start, end); 707cfdeb8c3SMark Brown } 708