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; 12478493f2dSMark Brown map->cache_present = NULL; 12578493f2dSMark Brown map->cache_present_nbits = 0; 126e5e3b8abSLars-Peter Clausen 1279fabe24eSDimitris Papastamos map->cache = NULL; 1289fabe24eSDimitris Papastamos map->cache_ops = cache_types[i]; 1299fabe24eSDimitris Papastamos 1309fabe24eSDimitris Papastamos if (!map->cache_ops->read || 1319fabe24eSDimitris Papastamos !map->cache_ops->write || 1329fabe24eSDimitris Papastamos !map->cache_ops->name) 1339fabe24eSDimitris Papastamos return -EINVAL; 1349fabe24eSDimitris Papastamos 1359fabe24eSDimitris Papastamos /* We still need to ensure that the reg_defaults 1369fabe24eSDimitris Papastamos * won't vanish from under us. We'll need to make 1379fabe24eSDimitris Papastamos * a copy of it. 1389fabe24eSDimitris Papastamos */ 139720e4616SLars-Peter Clausen if (config->reg_defaults) { 1409fabe24eSDimitris Papastamos if (!map->num_reg_defaults) 1419fabe24eSDimitris Papastamos return -EINVAL; 142720e4616SLars-Peter Clausen tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults * 1439fabe24eSDimitris Papastamos sizeof(struct reg_default), GFP_KERNEL); 1449fabe24eSDimitris Papastamos if (!tmp_buf) 1459fabe24eSDimitris Papastamos return -ENOMEM; 1469fabe24eSDimitris Papastamos map->reg_defaults = tmp_buf; 1478528bdd4SMark Brown } else if (map->num_reg_defaults_raw) { 1485fcd2560SMark Brown /* Some devices such as PMICs don't have cache defaults, 1499fabe24eSDimitris Papastamos * we cope with this by reading back the HW registers and 1509fabe24eSDimitris Papastamos * crafting the cache defaults by hand. 1519fabe24eSDimitris Papastamos */ 1529fabe24eSDimitris Papastamos ret = regcache_hw_init(map); 1539fabe24eSDimitris Papastamos if (ret < 0) 1549fabe24eSDimitris Papastamos return ret; 1559fabe24eSDimitris Papastamos } 1569fabe24eSDimitris Papastamos 1579fabe24eSDimitris Papastamos if (!map->max_register) 1589fabe24eSDimitris Papastamos map->max_register = map->num_reg_defaults_raw; 1599fabe24eSDimitris Papastamos 1609fabe24eSDimitris Papastamos if (map->cache_ops->init) { 1619fabe24eSDimitris Papastamos dev_dbg(map->dev, "Initializing %s cache\n", 1629fabe24eSDimitris Papastamos map->cache_ops->name); 163bd061c78SLars-Peter Clausen ret = map->cache_ops->init(map); 164bd061c78SLars-Peter Clausen if (ret) 165bd061c78SLars-Peter Clausen goto err_free; 1669fabe24eSDimitris Papastamos } 1679fabe24eSDimitris Papastamos return 0; 168bd061c78SLars-Peter Clausen 169bd061c78SLars-Peter Clausen err_free: 170bd061c78SLars-Peter Clausen kfree(map->reg_defaults); 171bd061c78SLars-Peter Clausen if (map->cache_free) 172bd061c78SLars-Peter Clausen kfree(map->reg_defaults_raw); 173bd061c78SLars-Peter Clausen 174bd061c78SLars-Peter Clausen return ret; 1759fabe24eSDimitris Papastamos } 1769fabe24eSDimitris Papastamos 1779fabe24eSDimitris Papastamos void regcache_exit(struct regmap *map) 1789fabe24eSDimitris Papastamos { 1799fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 1809fabe24eSDimitris Papastamos return; 1819fabe24eSDimitris Papastamos 1829fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 1839fabe24eSDimitris Papastamos 18478493f2dSMark Brown kfree(map->cache_present); 1859fabe24eSDimitris Papastamos kfree(map->reg_defaults); 1869fabe24eSDimitris Papastamos if (map->cache_free) 1879fabe24eSDimitris Papastamos kfree(map->reg_defaults_raw); 1889fabe24eSDimitris Papastamos 1899fabe24eSDimitris Papastamos if (map->cache_ops->exit) { 1909fabe24eSDimitris Papastamos dev_dbg(map->dev, "Destroying %s cache\n", 1919fabe24eSDimitris Papastamos map->cache_ops->name); 1929fabe24eSDimitris Papastamos map->cache_ops->exit(map); 1939fabe24eSDimitris Papastamos } 1949fabe24eSDimitris Papastamos } 1959fabe24eSDimitris Papastamos 1969fabe24eSDimitris Papastamos /** 1979fabe24eSDimitris Papastamos * regcache_read: Fetch the value of a given register from the cache. 1989fabe24eSDimitris Papastamos * 1999fabe24eSDimitris Papastamos * @map: map to configure. 2009fabe24eSDimitris Papastamos * @reg: The register index. 2019fabe24eSDimitris Papastamos * @value: The value to be returned. 2029fabe24eSDimitris Papastamos * 2039fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2049fabe24eSDimitris Papastamos */ 2059fabe24eSDimitris Papastamos int regcache_read(struct regmap *map, 2069fabe24eSDimitris Papastamos unsigned int reg, unsigned int *value) 2079fabe24eSDimitris Papastamos { 208bc7ee556SMark Brown int ret; 209bc7ee556SMark Brown 2109fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2119fabe24eSDimitris Papastamos return -ENOSYS; 2129fabe24eSDimitris Papastamos 2139fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2149fabe24eSDimitris Papastamos 215bc7ee556SMark Brown if (!regmap_volatile(map, reg)) { 216bc7ee556SMark Brown ret = map->cache_ops->read(map, reg, value); 217bc7ee556SMark Brown 218bc7ee556SMark Brown if (ret == 0) 219bc7ee556SMark Brown trace_regmap_reg_read_cache(map->dev, reg, *value); 220bc7ee556SMark Brown 221bc7ee556SMark Brown return ret; 222bc7ee556SMark Brown } 2239fabe24eSDimitris Papastamos 2249fabe24eSDimitris Papastamos return -EINVAL; 2259fabe24eSDimitris Papastamos } 2269fabe24eSDimitris Papastamos 2279fabe24eSDimitris Papastamos /** 2289fabe24eSDimitris Papastamos * regcache_write: Set the value of a given register in the cache. 2299fabe24eSDimitris Papastamos * 2309fabe24eSDimitris Papastamos * @map: map to configure. 2319fabe24eSDimitris Papastamos * @reg: The register index. 2329fabe24eSDimitris Papastamos * @value: The new register value. 2339fabe24eSDimitris Papastamos * 2349fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2359fabe24eSDimitris Papastamos */ 2369fabe24eSDimitris Papastamos int regcache_write(struct regmap *map, 2379fabe24eSDimitris Papastamos unsigned int reg, unsigned int value) 2389fabe24eSDimitris Papastamos { 2399fabe24eSDimitris Papastamos if (map->cache_type == REGCACHE_NONE) 2409fabe24eSDimitris Papastamos return 0; 2419fabe24eSDimitris Papastamos 2429fabe24eSDimitris Papastamos BUG_ON(!map->cache_ops); 2439fabe24eSDimitris Papastamos 2449fabe24eSDimitris Papastamos if (!regmap_writeable(map, reg)) 2459fabe24eSDimitris Papastamos return -EIO; 2469fabe24eSDimitris Papastamos 2479fabe24eSDimitris Papastamos if (!regmap_volatile(map, reg)) 2489fabe24eSDimitris Papastamos return map->cache_ops->write(map, reg, value); 2499fabe24eSDimitris Papastamos 2509fabe24eSDimitris Papastamos return 0; 2519fabe24eSDimitris Papastamos } 2529fabe24eSDimitris Papastamos 2539fabe24eSDimitris Papastamos /** 2549fabe24eSDimitris Papastamos * regcache_sync: Sync the register cache with the hardware. 2559fabe24eSDimitris Papastamos * 2569fabe24eSDimitris Papastamos * @map: map to configure. 2579fabe24eSDimitris Papastamos * 2589fabe24eSDimitris Papastamos * Any registers that should not be synced should be marked as 2599fabe24eSDimitris Papastamos * volatile. In general drivers can choose not to use the provided 2609fabe24eSDimitris Papastamos * syncing functionality if they so require. 2619fabe24eSDimitris Papastamos * 2629fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2639fabe24eSDimitris Papastamos */ 2649fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map) 2659fabe24eSDimitris Papastamos { 266954757d7SDimitris Papastamos int ret = 0; 267954757d7SDimitris Papastamos unsigned int i; 26859360089SDimitris Papastamos const char *name; 269beb1a10fSDimitris Papastamos unsigned int bypass; 27059360089SDimitris Papastamos 271c3ec2328SMark Brown BUG_ON(!map->cache_ops || !map->cache_ops->sync); 2729fabe24eSDimitris Papastamos 273bacdbe07SStephen Warren map->lock(map); 274beb1a10fSDimitris Papastamos /* Remember the initial bypass state */ 275beb1a10fSDimitris Papastamos bypass = map->cache_bypass; 2769fabe24eSDimitris Papastamos dev_dbg(map->dev, "Syncing %s cache\n", 2779fabe24eSDimitris Papastamos map->cache_ops->name); 27859360089SDimitris Papastamos name = map->cache_ops->name; 27959360089SDimitris Papastamos trace_regcache_sync(map->dev, name, "start"); 28022f0d90aSMark Brown 2818ae0d7e8SMark Brown if (!map->cache_dirty) 2828ae0d7e8SMark Brown goto out; 283d9db7627SMark Brown 28422f0d90aSMark Brown /* Apply any patch first */ 2858a892d69SMark Brown map->cache_bypass = 1; 28622f0d90aSMark Brown for (i = 0; i < map->patch_regs; i++) { 287f01ee60fSStephen Warren if (map->patch[i].reg % map->reg_stride) { 288f01ee60fSStephen Warren ret = -EINVAL; 289f01ee60fSStephen Warren goto out; 290f01ee60fSStephen Warren } 29122f0d90aSMark Brown ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); 29222f0d90aSMark Brown if (ret != 0) { 29322f0d90aSMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n", 29422f0d90aSMark Brown map->patch[i].reg, map->patch[i].def, ret); 29522f0d90aSMark Brown goto out; 29622f0d90aSMark Brown } 29722f0d90aSMark Brown } 2988a892d69SMark Brown map->cache_bypass = 0; 29922f0d90aSMark Brown 300ac8d91c8SMark Brown ret = map->cache_ops->sync(map, 0, map->max_register); 301954757d7SDimitris Papastamos 3026ff73738SMark Brown if (ret == 0) 3036ff73738SMark Brown map->cache_dirty = false; 3046ff73738SMark Brown 305954757d7SDimitris Papastamos out: 306954757d7SDimitris Papastamos trace_regcache_sync(map->dev, name, "stop"); 307beb1a10fSDimitris Papastamos /* Restore the bypass state */ 308beb1a10fSDimitris Papastamos map->cache_bypass = bypass; 309bacdbe07SStephen Warren map->unlock(map); 310954757d7SDimitris Papastamos 311954757d7SDimitris Papastamos return ret; 3129fabe24eSDimitris Papastamos } 3139fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync); 3149fabe24eSDimitris Papastamos 31592afb286SMark Brown /** 3164d4cfd16SMark Brown * regcache_sync_region: Sync part of the register cache with the hardware. 3174d4cfd16SMark Brown * 3184d4cfd16SMark Brown * @map: map to sync. 3194d4cfd16SMark Brown * @min: first register to sync 3204d4cfd16SMark Brown * @max: last register to sync 3214d4cfd16SMark Brown * 3224d4cfd16SMark Brown * Write all non-default register values in the specified region to 3234d4cfd16SMark Brown * the hardware. 3244d4cfd16SMark Brown * 3254d4cfd16SMark Brown * Return a negative value on failure, 0 on success. 3264d4cfd16SMark Brown */ 3274d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min, 3284d4cfd16SMark Brown unsigned int max) 3294d4cfd16SMark Brown { 3304d4cfd16SMark Brown int ret = 0; 3314d4cfd16SMark Brown const char *name; 3324d4cfd16SMark Brown unsigned int bypass; 3334d4cfd16SMark Brown 3344d4cfd16SMark Brown BUG_ON(!map->cache_ops || !map->cache_ops->sync); 3354d4cfd16SMark Brown 336bacdbe07SStephen Warren map->lock(map); 3374d4cfd16SMark Brown 3384d4cfd16SMark Brown /* Remember the initial bypass state */ 3394d4cfd16SMark Brown bypass = map->cache_bypass; 3404d4cfd16SMark Brown 3414d4cfd16SMark Brown name = map->cache_ops->name; 3424d4cfd16SMark Brown dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); 3434d4cfd16SMark Brown 3444d4cfd16SMark Brown trace_regcache_sync(map->dev, name, "start region"); 3454d4cfd16SMark Brown 3464d4cfd16SMark Brown if (!map->cache_dirty) 3474d4cfd16SMark Brown goto out; 3484d4cfd16SMark Brown 3494d4cfd16SMark Brown ret = map->cache_ops->sync(map, min, max); 3504d4cfd16SMark Brown 3514d4cfd16SMark Brown out: 3524d4cfd16SMark Brown trace_regcache_sync(map->dev, name, "stop region"); 3534d4cfd16SMark Brown /* Restore the bypass state */ 3544d4cfd16SMark Brown map->cache_bypass = bypass; 355bacdbe07SStephen Warren map->unlock(map); 3564d4cfd16SMark Brown 3574d4cfd16SMark Brown return ret; 3584d4cfd16SMark Brown } 359e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region); 3604d4cfd16SMark Brown 3614d4cfd16SMark Brown /** 36292afb286SMark Brown * regcache_cache_only: Put a register map into cache only mode 36392afb286SMark Brown * 36492afb286SMark Brown * @map: map to configure 36592afb286SMark Brown * @cache_only: flag if changes should be written to the hardware 36692afb286SMark Brown * 36792afb286SMark Brown * When a register map is marked as cache only writes to the register 36892afb286SMark Brown * map API will only update the register cache, they will not cause 36992afb286SMark Brown * any hardware changes. This is useful for allowing portions of 37092afb286SMark Brown * drivers to act as though the device were functioning as normal when 37192afb286SMark Brown * it is disabled for power saving reasons. 37292afb286SMark Brown */ 37392afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable) 37492afb286SMark Brown { 375bacdbe07SStephen Warren map->lock(map); 376ac77a765SDimitris Papastamos WARN_ON(map->cache_bypass && enable); 37792afb286SMark Brown map->cache_only = enable; 3785d5b7d4fSMark Brown trace_regmap_cache_only(map->dev, enable); 379bacdbe07SStephen Warren map->unlock(map); 38092afb286SMark Brown } 38192afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only); 38292afb286SMark Brown 3836eb0f5e0SDimitris Papastamos /** 3848ae0d7e8SMark Brown * regcache_mark_dirty: Mark the register cache as dirty 3858ae0d7e8SMark Brown * 3868ae0d7e8SMark Brown * @map: map to mark 3878ae0d7e8SMark Brown * 3888ae0d7e8SMark Brown * Mark the register cache as dirty, for example due to the device 3898ae0d7e8SMark Brown * having been powered down for suspend. If the cache is not marked 3908ae0d7e8SMark Brown * as dirty then the cache sync will be suppressed. 3918ae0d7e8SMark Brown */ 3928ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map) 3938ae0d7e8SMark Brown { 394bacdbe07SStephen Warren map->lock(map); 3958ae0d7e8SMark Brown map->cache_dirty = true; 396bacdbe07SStephen Warren map->unlock(map); 3978ae0d7e8SMark Brown } 3988ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty); 3998ae0d7e8SMark Brown 4008ae0d7e8SMark Brown /** 4016eb0f5e0SDimitris Papastamos * regcache_cache_bypass: Put a register map into cache bypass mode 4026eb0f5e0SDimitris Papastamos * 4036eb0f5e0SDimitris Papastamos * @map: map to configure 4040eef6b04SDimitris Papastamos * @cache_bypass: flag if changes should not be written to the hardware 4056eb0f5e0SDimitris Papastamos * 4066eb0f5e0SDimitris Papastamos * When a register map is marked with the cache bypass option, writes 4076eb0f5e0SDimitris Papastamos * to the register map API will only update the hardware and not the 4086eb0f5e0SDimitris Papastamos * the cache directly. This is useful when syncing the cache back to 4096eb0f5e0SDimitris Papastamos * the hardware. 4106eb0f5e0SDimitris Papastamos */ 4116eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable) 4126eb0f5e0SDimitris Papastamos { 413bacdbe07SStephen Warren map->lock(map); 414ac77a765SDimitris Papastamos WARN_ON(map->cache_only && enable); 4156eb0f5e0SDimitris Papastamos map->cache_bypass = enable; 4165d5b7d4fSMark Brown trace_regmap_cache_bypass(map->dev, enable); 417bacdbe07SStephen Warren map->unlock(map); 4186eb0f5e0SDimitris Papastamos } 4196eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass); 4206eb0f5e0SDimitris Papastamos 42178493f2dSMark Brown int regcache_set_reg_present(struct regmap *map, unsigned int reg) 42278493f2dSMark Brown { 42378493f2dSMark Brown unsigned long *cache_present; 42478493f2dSMark Brown unsigned int cache_present_size; 42578493f2dSMark Brown unsigned int nregs; 42678493f2dSMark Brown int i; 42778493f2dSMark Brown 42878493f2dSMark Brown nregs = reg + 1; 42978493f2dSMark Brown cache_present_size = BITS_TO_LONGS(nregs); 43078493f2dSMark Brown cache_present_size *= sizeof(long); 43178493f2dSMark Brown 43278493f2dSMark Brown if (!map->cache_present) { 43378493f2dSMark Brown cache_present = kmalloc(cache_present_size, GFP_KERNEL); 43478493f2dSMark Brown if (!cache_present) 43578493f2dSMark Brown return -ENOMEM; 43678493f2dSMark Brown bitmap_zero(cache_present, nregs); 43778493f2dSMark Brown map->cache_present = cache_present; 43878493f2dSMark Brown map->cache_present_nbits = nregs; 43978493f2dSMark Brown } 44078493f2dSMark Brown 44178493f2dSMark Brown if (nregs > map->cache_present_nbits) { 44278493f2dSMark Brown cache_present = krealloc(map->cache_present, 44378493f2dSMark Brown cache_present_size, GFP_KERNEL); 44478493f2dSMark Brown if (!cache_present) 44578493f2dSMark Brown return -ENOMEM; 44678493f2dSMark Brown for (i = 0; i < nregs; i++) 44778493f2dSMark Brown if (i >= map->cache_present_nbits) 44878493f2dSMark Brown clear_bit(i, cache_present); 44978493f2dSMark Brown map->cache_present = cache_present; 45078493f2dSMark Brown map->cache_present_nbits = nregs; 45178493f2dSMark Brown } 45278493f2dSMark Brown 45378493f2dSMark Brown set_bit(reg, map->cache_present); 45478493f2dSMark Brown return 0; 45578493f2dSMark Brown } 45678493f2dSMark Brown 457879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, 458879082c9SMark Brown unsigned int val) 4599fabe24eSDimitris Papastamos { 460325acab4SMark Brown if (regcache_get_val(map, base, idx) == val) 461325acab4SMark Brown return true; 462325acab4SMark Brown 463eb4cb76fSMark Brown /* Use device native format if possible */ 464eb4cb76fSMark Brown if (map->format.format_val) { 465eb4cb76fSMark Brown map->format.format_val(base + (map->cache_word_size * idx), 466eb4cb76fSMark Brown val, 0); 467eb4cb76fSMark Brown return false; 468eb4cb76fSMark Brown } 469eb4cb76fSMark Brown 470879082c9SMark Brown switch (map->cache_word_size) { 4719fabe24eSDimitris Papastamos case 1: { 4729fabe24eSDimitris Papastamos u8 *cache = base; 4739fabe24eSDimitris Papastamos cache[idx] = val; 4749fabe24eSDimitris Papastamos break; 4759fabe24eSDimitris Papastamos } 4769fabe24eSDimitris Papastamos case 2: { 4779fabe24eSDimitris Papastamos u16 *cache = base; 4789fabe24eSDimitris Papastamos cache[idx] = val; 4799fabe24eSDimitris Papastamos break; 4809fabe24eSDimitris Papastamos } 4817d5e525bSMark Brown case 4: { 4827d5e525bSMark Brown u32 *cache = base; 4837d5e525bSMark Brown cache[idx] = val; 4847d5e525bSMark Brown break; 4857d5e525bSMark Brown } 4869fabe24eSDimitris Papastamos default: 4879fabe24eSDimitris Papastamos BUG(); 4889fabe24eSDimitris Papastamos } 4899fabe24eSDimitris Papastamos return false; 4909fabe24eSDimitris Papastamos } 4919fabe24eSDimitris Papastamos 492879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base, 493879082c9SMark Brown unsigned int idx) 4949fabe24eSDimitris Papastamos { 4959fabe24eSDimitris Papastamos if (!base) 4969fabe24eSDimitris Papastamos return -EINVAL; 4979fabe24eSDimitris Papastamos 498eb4cb76fSMark Brown /* Use device native format if possible */ 499eb4cb76fSMark Brown if (map->format.parse_val) 5008817796bSMark Brown return map->format.parse_val(regcache_get_val_addr(map, base, 5018817796bSMark Brown idx)); 502eb4cb76fSMark Brown 503879082c9SMark Brown switch (map->cache_word_size) { 5049fabe24eSDimitris Papastamos case 1: { 5059fabe24eSDimitris Papastamos const u8 *cache = base; 5069fabe24eSDimitris Papastamos return cache[idx]; 5079fabe24eSDimitris Papastamos } 5089fabe24eSDimitris Papastamos case 2: { 5099fabe24eSDimitris Papastamos const u16 *cache = base; 5109fabe24eSDimitris Papastamos return cache[idx]; 5119fabe24eSDimitris Papastamos } 5127d5e525bSMark Brown case 4: { 5137d5e525bSMark Brown const u32 *cache = base; 5147d5e525bSMark Brown return cache[idx]; 5157d5e525bSMark Brown } 5169fabe24eSDimitris Papastamos default: 5179fabe24eSDimitris Papastamos BUG(); 5189fabe24eSDimitris Papastamos } 5199fabe24eSDimitris Papastamos /* unreachable */ 5209fabe24eSDimitris Papastamos return -1; 5219fabe24eSDimitris Papastamos } 5229fabe24eSDimitris Papastamos 523f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b) 524c08604b8SDimitris Papastamos { 525c08604b8SDimitris Papastamos const struct reg_default *_a = a; 526c08604b8SDimitris Papastamos const struct reg_default *_b = b; 527c08604b8SDimitris Papastamos 528c08604b8SDimitris Papastamos return _a->reg - _b->reg; 529c08604b8SDimitris Papastamos } 530c08604b8SDimitris Papastamos 531f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg) 532f094fea6SMark Brown { 533f094fea6SMark Brown struct reg_default key; 534f094fea6SMark Brown struct reg_default *r; 535f094fea6SMark Brown 536f094fea6SMark Brown key.reg = reg; 537f094fea6SMark Brown key.def = 0; 538f094fea6SMark Brown 539f094fea6SMark Brown r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, 540f094fea6SMark Brown sizeof(struct reg_default), regcache_default_cmp); 541f094fea6SMark Brown 542f094fea6SMark Brown if (r) 543f094fea6SMark Brown return r - map->reg_defaults; 544f094fea6SMark Brown else 5456e6ace00SMark Brown return -ENOENT; 546f094fea6SMark Brown } 547f8bd822cSMark Brown 548cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block, 549cfdeb8c3SMark Brown unsigned int block_base, 550cfdeb8c3SMark Brown unsigned int start, unsigned int end) 551cfdeb8c3SMark Brown { 552cfdeb8c3SMark Brown unsigned int i, regtmp, val; 553cfdeb8c3SMark Brown int ret; 554cfdeb8c3SMark Brown 555cfdeb8c3SMark Brown for (i = start; i < end; i++) { 556cfdeb8c3SMark Brown regtmp = block_base + (i * map->reg_stride); 557cfdeb8c3SMark Brown 558cfdeb8c3SMark Brown if (!regcache_reg_present(map, regtmp)) 559cfdeb8c3SMark Brown continue; 560cfdeb8c3SMark Brown 561cfdeb8c3SMark Brown val = regcache_get_val(map, block, i); 562cfdeb8c3SMark Brown 563cfdeb8c3SMark Brown /* Is this the hardware default? If so skip. */ 564cfdeb8c3SMark Brown ret = regcache_lookup_reg(map, regtmp); 565cfdeb8c3SMark Brown if (ret >= 0 && val == map->reg_defaults[ret].def) 566cfdeb8c3SMark Brown continue; 567cfdeb8c3SMark Brown 568cfdeb8c3SMark Brown map->cache_bypass = 1; 569cfdeb8c3SMark Brown 570cfdeb8c3SMark Brown ret = _regmap_write(map, regtmp, val); 571cfdeb8c3SMark Brown 572cfdeb8c3SMark Brown map->cache_bypass = 0; 573cfdeb8c3SMark Brown if (ret != 0) 574cfdeb8c3SMark Brown return ret; 575cfdeb8c3SMark Brown dev_dbg(map->dev, "Synced register %#x, value %#x\n", 576cfdeb8c3SMark Brown regtmp, val); 577cfdeb8c3SMark Brown } 578cfdeb8c3SMark Brown 579cfdeb8c3SMark Brown return 0; 580cfdeb8c3SMark Brown } 581cfdeb8c3SMark Brown 58275a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data, 58375a5f89fSMark Brown unsigned int base, unsigned int cur) 58475a5f89fSMark Brown { 58575a5f89fSMark Brown size_t val_bytes = map->format.val_bytes; 58675a5f89fSMark Brown int ret, count; 58775a5f89fSMark Brown 58875a5f89fSMark Brown if (*data == NULL) 58975a5f89fSMark Brown return 0; 59075a5f89fSMark Brown 59175a5f89fSMark Brown count = cur - base; 59275a5f89fSMark Brown 59375a5f89fSMark Brown dev_dbg(map->dev, "Writing %d bytes for %d registers from 0x%x-0x%x\n", 59475a5f89fSMark Brown count * val_bytes, count, base, cur - 1); 59575a5f89fSMark Brown 59675a5f89fSMark Brown map->cache_bypass = 1; 59775a5f89fSMark Brown 59875a5f89fSMark Brown ret = _regmap_raw_write(map, base, *data, count * val_bytes, 59975a5f89fSMark Brown false); 60075a5f89fSMark Brown 60175a5f89fSMark Brown map->cache_bypass = 0; 60275a5f89fSMark Brown 60375a5f89fSMark Brown *data = NULL; 60475a5f89fSMark Brown 60575a5f89fSMark Brown return ret; 60675a5f89fSMark Brown } 60775a5f89fSMark Brown 608*f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block, 609f8bd822cSMark Brown unsigned int block_base, unsigned int start, 610f8bd822cSMark Brown unsigned int end) 611f8bd822cSMark Brown { 61275a5f89fSMark Brown unsigned int i, val; 61375a5f89fSMark Brown unsigned int regtmp = 0; 61475a5f89fSMark Brown unsigned int base = 0; 61575a5f89fSMark Brown const void *data = NULL; 616f8bd822cSMark Brown int ret; 617f8bd822cSMark Brown 618f8bd822cSMark Brown for (i = start; i < end; i++) { 619f8bd822cSMark Brown regtmp = block_base + (i * map->reg_stride); 620f8bd822cSMark Brown 62175a5f89fSMark Brown if (!regcache_reg_present(map, regtmp)) { 62275a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 62375a5f89fSMark Brown base, regtmp); 62475a5f89fSMark Brown if (ret != 0) 62575a5f89fSMark Brown return ret; 626f8bd822cSMark Brown continue; 62775a5f89fSMark Brown } 628f8bd822cSMark Brown 629f8bd822cSMark Brown val = regcache_get_val(map, block, i); 630f8bd822cSMark Brown 631f8bd822cSMark Brown /* Is this the hardware default? If so skip. */ 632f8bd822cSMark Brown ret = regcache_lookup_reg(map, regtmp); 63375a5f89fSMark Brown if (ret >= 0 && val == map->reg_defaults[ret].def) { 63475a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 63575a5f89fSMark Brown base, regtmp); 636f8bd822cSMark Brown if (ret != 0) 637f8bd822cSMark Brown return ret; 63875a5f89fSMark Brown continue; 639f8bd822cSMark Brown } 640f8bd822cSMark Brown 64175a5f89fSMark Brown if (!data) { 64275a5f89fSMark Brown data = regcache_get_val_addr(map, block, i); 64375a5f89fSMark Brown base = regtmp; 64475a5f89fSMark Brown } 64575a5f89fSMark Brown } 64675a5f89fSMark Brown 64775a5f89fSMark Brown return regcache_sync_block_raw_flush(map, &data, base, regtmp); 648f8bd822cSMark Brown } 649cfdeb8c3SMark Brown 650cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block, 651cfdeb8c3SMark Brown unsigned int block_base, unsigned int start, 652cfdeb8c3SMark Brown unsigned int end) 653cfdeb8c3SMark Brown { 654cfdeb8c3SMark Brown if (regmap_can_raw_write(map)) 655cfdeb8c3SMark Brown return regcache_sync_block_raw(map, block, block_base, 656cfdeb8c3SMark Brown start, end); 657cfdeb8c3SMark Brown else 658cfdeb8c3SMark Brown return regcache_sync_block_single(map, block, block_base, 659cfdeb8c3SMark Brown start, end); 660cfdeb8c3SMark Brown } 661