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 252d856fce4SMaarten ter Huurne for (reg = min; reg <= max; reg++) { 253d856fce4SMaarten ter Huurne unsigned int val; 254d856fce4SMaarten ter Huurne int ret; 255d856fce4SMaarten ter Huurne 256d856fce4SMaarten ter Huurne if (regmap_volatile(map, reg)) 257d856fce4SMaarten ter Huurne continue; 258d856fce4SMaarten ter Huurne 259d856fce4SMaarten ter Huurne ret = regcache_read(map, reg, &val); 260d856fce4SMaarten ter Huurne if (ret) 261d856fce4SMaarten ter Huurne return ret; 262d856fce4SMaarten ter Huurne 263d856fce4SMaarten ter Huurne /* Is this the hardware default? If so skip. */ 264d856fce4SMaarten ter Huurne ret = regcache_lookup_reg(map, reg); 265d856fce4SMaarten ter Huurne if (ret >= 0 && val == map->reg_defaults[ret].def) 266d856fce4SMaarten ter Huurne continue; 267d856fce4SMaarten ter Huurne 268d856fce4SMaarten ter Huurne map->cache_bypass = 1; 269d856fce4SMaarten ter Huurne ret = _regmap_write(map, reg, val); 270d856fce4SMaarten ter Huurne map->cache_bypass = 0; 271d856fce4SMaarten ter Huurne if (ret) 272d856fce4SMaarten ter Huurne return ret; 273d856fce4SMaarten ter Huurne dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val); 274d856fce4SMaarten ter Huurne } 275d856fce4SMaarten ter Huurne 276d856fce4SMaarten ter Huurne return 0; 277d856fce4SMaarten ter Huurne } 278d856fce4SMaarten ter Huurne 2799fabe24eSDimitris Papastamos /** 2809fabe24eSDimitris Papastamos * regcache_sync: Sync the register cache with the hardware. 2819fabe24eSDimitris Papastamos * 2829fabe24eSDimitris Papastamos * @map: map to configure. 2839fabe24eSDimitris Papastamos * 2849fabe24eSDimitris Papastamos * Any registers that should not be synced should be marked as 2859fabe24eSDimitris Papastamos * volatile. In general drivers can choose not to use the provided 2869fabe24eSDimitris Papastamos * syncing functionality if they so require. 2879fabe24eSDimitris Papastamos * 2889fabe24eSDimitris Papastamos * Return a negative value on failure, 0 on success. 2899fabe24eSDimitris Papastamos */ 2909fabe24eSDimitris Papastamos int regcache_sync(struct regmap *map) 2919fabe24eSDimitris Papastamos { 292954757d7SDimitris Papastamos int ret = 0; 293954757d7SDimitris Papastamos unsigned int i; 29459360089SDimitris Papastamos const char *name; 295beb1a10fSDimitris Papastamos unsigned int bypass; 29659360089SDimitris Papastamos 297d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 2989fabe24eSDimitris Papastamos 29981485f52SLars-Peter Clausen map->lock(map->lock_arg); 300beb1a10fSDimitris Papastamos /* Remember the initial bypass state */ 301beb1a10fSDimitris Papastamos bypass = map->cache_bypass; 3029fabe24eSDimitris Papastamos dev_dbg(map->dev, "Syncing %s cache\n", 3039fabe24eSDimitris Papastamos map->cache_ops->name); 30459360089SDimitris Papastamos name = map->cache_ops->name; 30559360089SDimitris Papastamos trace_regcache_sync(map->dev, name, "start"); 30622f0d90aSMark Brown 3078ae0d7e8SMark Brown if (!map->cache_dirty) 3088ae0d7e8SMark Brown goto out; 309d9db7627SMark Brown 310affbe886SMark Brown map->async = true; 311affbe886SMark Brown 31222f0d90aSMark Brown /* Apply any patch first */ 3138a892d69SMark Brown map->cache_bypass = 1; 31422f0d90aSMark Brown for (i = 0; i < map->patch_regs; i++) { 315f01ee60fSStephen Warren if (map->patch[i].reg % map->reg_stride) { 316f01ee60fSStephen Warren ret = -EINVAL; 317f01ee60fSStephen Warren goto out; 318f01ee60fSStephen Warren } 31922f0d90aSMark Brown ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); 32022f0d90aSMark Brown if (ret != 0) { 32122f0d90aSMark Brown dev_err(map->dev, "Failed to write %x = %x: %d\n", 32222f0d90aSMark Brown map->patch[i].reg, map->patch[i].def, ret); 32322f0d90aSMark Brown goto out; 32422f0d90aSMark Brown } 32522f0d90aSMark Brown } 3268a892d69SMark Brown map->cache_bypass = 0; 32722f0d90aSMark Brown 328d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 329ac8d91c8SMark Brown ret = map->cache_ops->sync(map, 0, map->max_register); 330d856fce4SMaarten ter Huurne else 331d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, 0, map->max_register); 332954757d7SDimitris Papastamos 3336ff73738SMark Brown if (ret == 0) 3346ff73738SMark Brown map->cache_dirty = false; 3356ff73738SMark Brown 336954757d7SDimitris Papastamos out: 337beb1a10fSDimitris Papastamos /* Restore the bypass state */ 338affbe886SMark Brown map->async = false; 339beb1a10fSDimitris Papastamos map->cache_bypass = bypass; 34081485f52SLars-Peter Clausen map->unlock(map->lock_arg); 341954757d7SDimitris Papastamos 342affbe886SMark Brown regmap_async_complete(map); 343affbe886SMark Brown 344affbe886SMark Brown trace_regcache_sync(map->dev, name, "stop"); 345affbe886SMark Brown 346954757d7SDimitris Papastamos return ret; 3479fabe24eSDimitris Papastamos } 3489fabe24eSDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_sync); 3499fabe24eSDimitris Papastamos 35092afb286SMark Brown /** 3514d4cfd16SMark Brown * regcache_sync_region: Sync part of the register cache with the hardware. 3524d4cfd16SMark Brown * 3534d4cfd16SMark Brown * @map: map to sync. 3544d4cfd16SMark Brown * @min: first register to sync 3554d4cfd16SMark Brown * @max: last register to sync 3564d4cfd16SMark Brown * 3574d4cfd16SMark Brown * Write all non-default register values in the specified region to 3584d4cfd16SMark Brown * the hardware. 3594d4cfd16SMark Brown * 3604d4cfd16SMark Brown * Return a negative value on failure, 0 on success. 3614d4cfd16SMark Brown */ 3624d4cfd16SMark Brown int regcache_sync_region(struct regmap *map, unsigned int min, 3634d4cfd16SMark Brown unsigned int max) 3644d4cfd16SMark Brown { 3654d4cfd16SMark Brown int ret = 0; 3664d4cfd16SMark Brown const char *name; 3674d4cfd16SMark Brown unsigned int bypass; 3684d4cfd16SMark Brown 369d856fce4SMaarten ter Huurne BUG_ON(!map->cache_ops); 3704d4cfd16SMark Brown 37181485f52SLars-Peter Clausen map->lock(map->lock_arg); 3724d4cfd16SMark Brown 3734d4cfd16SMark Brown /* Remember the initial bypass state */ 3744d4cfd16SMark Brown bypass = map->cache_bypass; 3754d4cfd16SMark Brown 3764d4cfd16SMark Brown name = map->cache_ops->name; 3774d4cfd16SMark Brown dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); 3784d4cfd16SMark Brown 3794d4cfd16SMark Brown trace_regcache_sync(map->dev, name, "start region"); 3804d4cfd16SMark Brown 3814d4cfd16SMark Brown if (!map->cache_dirty) 3824d4cfd16SMark Brown goto out; 3834d4cfd16SMark Brown 384affbe886SMark Brown map->async = true; 385affbe886SMark Brown 386d856fce4SMaarten ter Huurne if (map->cache_ops->sync) 3874d4cfd16SMark Brown ret = map->cache_ops->sync(map, min, max); 388d856fce4SMaarten ter Huurne else 389d856fce4SMaarten ter Huurne ret = regcache_default_sync(map, min, max); 3904d4cfd16SMark Brown 3914d4cfd16SMark Brown out: 3924d4cfd16SMark Brown /* Restore the bypass state */ 3934d4cfd16SMark Brown map->cache_bypass = bypass; 394affbe886SMark Brown map->async = false; 39581485f52SLars-Peter Clausen map->unlock(map->lock_arg); 3964d4cfd16SMark Brown 397affbe886SMark Brown regmap_async_complete(map); 398affbe886SMark Brown 399affbe886SMark Brown trace_regcache_sync(map->dev, name, "stop region"); 400affbe886SMark Brown 4014d4cfd16SMark Brown return ret; 4024d4cfd16SMark Brown } 403e466de05SMark Brown EXPORT_SYMBOL_GPL(regcache_sync_region); 4044d4cfd16SMark Brown 4054d4cfd16SMark Brown /** 406697e85bcSMark Brown * regcache_drop_region: Discard part of the register cache 407697e85bcSMark Brown * 408697e85bcSMark Brown * @map: map to operate on 409697e85bcSMark Brown * @min: first register to discard 410697e85bcSMark Brown * @max: last register to discard 411697e85bcSMark Brown * 412697e85bcSMark Brown * Discard part of the register cache. 413697e85bcSMark Brown * 414697e85bcSMark Brown * Return a negative value on failure, 0 on success. 415697e85bcSMark Brown */ 416697e85bcSMark Brown int regcache_drop_region(struct regmap *map, unsigned int min, 417697e85bcSMark Brown unsigned int max) 418697e85bcSMark Brown { 419697e85bcSMark Brown int ret = 0; 420697e85bcSMark Brown 4213f4ff561SLars-Peter Clausen if (!map->cache_ops || !map->cache_ops->drop) 422697e85bcSMark Brown return -EINVAL; 423697e85bcSMark Brown 42481485f52SLars-Peter Clausen map->lock(map->lock_arg); 425697e85bcSMark Brown 426697e85bcSMark Brown trace_regcache_drop_region(map->dev, min, max); 427697e85bcSMark Brown 428697e85bcSMark Brown ret = map->cache_ops->drop(map, min, max); 429697e85bcSMark Brown 43081485f52SLars-Peter Clausen map->unlock(map->lock_arg); 431697e85bcSMark Brown 432697e85bcSMark Brown return ret; 433697e85bcSMark Brown } 434697e85bcSMark Brown EXPORT_SYMBOL_GPL(regcache_drop_region); 435697e85bcSMark Brown 436697e85bcSMark Brown /** 43792afb286SMark Brown * regcache_cache_only: Put a register map into cache only mode 43892afb286SMark Brown * 43992afb286SMark Brown * @map: map to configure 44092afb286SMark Brown * @cache_only: flag if changes should be written to the hardware 44192afb286SMark Brown * 44292afb286SMark Brown * When a register map is marked as cache only writes to the register 44392afb286SMark Brown * map API will only update the register cache, they will not cause 44492afb286SMark Brown * any hardware changes. This is useful for allowing portions of 44592afb286SMark Brown * drivers to act as though the device were functioning as normal when 44692afb286SMark Brown * it is disabled for power saving reasons. 44792afb286SMark Brown */ 44892afb286SMark Brown void regcache_cache_only(struct regmap *map, bool enable) 44992afb286SMark Brown { 45081485f52SLars-Peter Clausen map->lock(map->lock_arg); 451ac77a765SDimitris Papastamos WARN_ON(map->cache_bypass && enable); 45292afb286SMark Brown map->cache_only = enable; 4535d5b7d4fSMark Brown trace_regmap_cache_only(map->dev, enable); 45481485f52SLars-Peter Clausen map->unlock(map->lock_arg); 45592afb286SMark Brown } 45692afb286SMark Brown EXPORT_SYMBOL_GPL(regcache_cache_only); 45792afb286SMark Brown 4586eb0f5e0SDimitris Papastamos /** 4598ae0d7e8SMark Brown * regcache_mark_dirty: Mark the register cache as dirty 4608ae0d7e8SMark Brown * 4618ae0d7e8SMark Brown * @map: map to mark 4628ae0d7e8SMark Brown * 4638ae0d7e8SMark Brown * Mark the register cache as dirty, for example due to the device 4648ae0d7e8SMark Brown * having been powered down for suspend. If the cache is not marked 4658ae0d7e8SMark Brown * as dirty then the cache sync will be suppressed. 4668ae0d7e8SMark Brown */ 4678ae0d7e8SMark Brown void regcache_mark_dirty(struct regmap *map) 4688ae0d7e8SMark Brown { 46981485f52SLars-Peter Clausen map->lock(map->lock_arg); 4708ae0d7e8SMark Brown map->cache_dirty = true; 47181485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4728ae0d7e8SMark Brown } 4738ae0d7e8SMark Brown EXPORT_SYMBOL_GPL(regcache_mark_dirty); 4748ae0d7e8SMark Brown 4758ae0d7e8SMark Brown /** 4766eb0f5e0SDimitris Papastamos * regcache_cache_bypass: Put a register map into cache bypass mode 4776eb0f5e0SDimitris Papastamos * 4786eb0f5e0SDimitris Papastamos * @map: map to configure 4790eef6b04SDimitris Papastamos * @cache_bypass: flag if changes should not be written to the hardware 4806eb0f5e0SDimitris Papastamos * 4816eb0f5e0SDimitris Papastamos * When a register map is marked with the cache bypass option, writes 4826eb0f5e0SDimitris Papastamos * to the register map API will only update the hardware and not the 4836eb0f5e0SDimitris Papastamos * the cache directly. This is useful when syncing the cache back to 4846eb0f5e0SDimitris Papastamos * the hardware. 4856eb0f5e0SDimitris Papastamos */ 4866eb0f5e0SDimitris Papastamos void regcache_cache_bypass(struct regmap *map, bool enable) 4876eb0f5e0SDimitris Papastamos { 48881485f52SLars-Peter Clausen map->lock(map->lock_arg); 489ac77a765SDimitris Papastamos WARN_ON(map->cache_only && enable); 4906eb0f5e0SDimitris Papastamos map->cache_bypass = enable; 4915d5b7d4fSMark Brown trace_regmap_cache_bypass(map->dev, enable); 49281485f52SLars-Peter Clausen map->unlock(map->lock_arg); 4936eb0f5e0SDimitris Papastamos } 4946eb0f5e0SDimitris Papastamos EXPORT_SYMBOL_GPL(regcache_cache_bypass); 4956eb0f5e0SDimitris Papastamos 496879082c9SMark Brown bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, 497879082c9SMark Brown unsigned int val) 4989fabe24eSDimitris Papastamos { 499325acab4SMark Brown if (regcache_get_val(map, base, idx) == val) 500325acab4SMark Brown return true; 501325acab4SMark Brown 502eb4cb76fSMark Brown /* Use device native format if possible */ 503eb4cb76fSMark Brown if (map->format.format_val) { 504eb4cb76fSMark Brown map->format.format_val(base + (map->cache_word_size * idx), 505eb4cb76fSMark Brown val, 0); 506eb4cb76fSMark Brown return false; 507eb4cb76fSMark Brown } 508eb4cb76fSMark Brown 509879082c9SMark Brown switch (map->cache_word_size) { 5109fabe24eSDimitris Papastamos case 1: { 5119fabe24eSDimitris Papastamos u8 *cache = base; 5129fabe24eSDimitris Papastamos cache[idx] = val; 5139fabe24eSDimitris Papastamos break; 5149fabe24eSDimitris Papastamos } 5159fabe24eSDimitris Papastamos case 2: { 5169fabe24eSDimitris Papastamos u16 *cache = base; 5179fabe24eSDimitris Papastamos cache[idx] = val; 5189fabe24eSDimitris Papastamos break; 5199fabe24eSDimitris Papastamos } 5207d5e525bSMark Brown case 4: { 5217d5e525bSMark Brown u32 *cache = base; 5227d5e525bSMark Brown cache[idx] = val; 5237d5e525bSMark Brown break; 5247d5e525bSMark Brown } 5259fabe24eSDimitris Papastamos default: 5269fabe24eSDimitris Papastamos BUG(); 5279fabe24eSDimitris Papastamos } 5289fabe24eSDimitris Papastamos return false; 5299fabe24eSDimitris Papastamos } 5309fabe24eSDimitris Papastamos 531879082c9SMark Brown unsigned int regcache_get_val(struct regmap *map, const void *base, 532879082c9SMark Brown unsigned int idx) 5339fabe24eSDimitris Papastamos { 5349fabe24eSDimitris Papastamos if (!base) 5359fabe24eSDimitris Papastamos return -EINVAL; 5369fabe24eSDimitris Papastamos 537eb4cb76fSMark Brown /* Use device native format if possible */ 538eb4cb76fSMark Brown if (map->format.parse_val) 5398817796bSMark Brown return map->format.parse_val(regcache_get_val_addr(map, base, 5408817796bSMark Brown idx)); 541eb4cb76fSMark Brown 542879082c9SMark Brown switch (map->cache_word_size) { 5439fabe24eSDimitris Papastamos case 1: { 5449fabe24eSDimitris Papastamos const u8 *cache = base; 5459fabe24eSDimitris Papastamos return cache[idx]; 5469fabe24eSDimitris Papastamos } 5479fabe24eSDimitris Papastamos case 2: { 5489fabe24eSDimitris Papastamos const u16 *cache = base; 5499fabe24eSDimitris Papastamos return cache[idx]; 5509fabe24eSDimitris Papastamos } 5517d5e525bSMark Brown case 4: { 5527d5e525bSMark Brown const u32 *cache = base; 5537d5e525bSMark Brown return cache[idx]; 5547d5e525bSMark Brown } 5559fabe24eSDimitris Papastamos default: 5569fabe24eSDimitris Papastamos BUG(); 5579fabe24eSDimitris Papastamos } 5589fabe24eSDimitris Papastamos /* unreachable */ 5599fabe24eSDimitris Papastamos return -1; 5609fabe24eSDimitris Papastamos } 5619fabe24eSDimitris Papastamos 562f094fea6SMark Brown static int regcache_default_cmp(const void *a, const void *b) 563c08604b8SDimitris Papastamos { 564c08604b8SDimitris Papastamos const struct reg_default *_a = a; 565c08604b8SDimitris Papastamos const struct reg_default *_b = b; 566c08604b8SDimitris Papastamos 567c08604b8SDimitris Papastamos return _a->reg - _b->reg; 568c08604b8SDimitris Papastamos } 569c08604b8SDimitris Papastamos 570f094fea6SMark Brown int regcache_lookup_reg(struct regmap *map, unsigned int reg) 571f094fea6SMark Brown { 572f094fea6SMark Brown struct reg_default key; 573f094fea6SMark Brown struct reg_default *r; 574f094fea6SMark Brown 575f094fea6SMark Brown key.reg = reg; 576f094fea6SMark Brown key.def = 0; 577f094fea6SMark Brown 578f094fea6SMark Brown r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, 579f094fea6SMark Brown sizeof(struct reg_default), regcache_default_cmp); 580f094fea6SMark Brown 581f094fea6SMark Brown if (r) 582f094fea6SMark Brown return r - map->reg_defaults; 583f094fea6SMark Brown else 5846e6ace00SMark Brown return -ENOENT; 585f094fea6SMark Brown } 586f8bd822cSMark Brown 5873f4ff561SLars-Peter Clausen static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx) 5883f4ff561SLars-Peter Clausen { 5893f4ff561SLars-Peter Clausen if (!cache_present) 5903f4ff561SLars-Peter Clausen return true; 5913f4ff561SLars-Peter Clausen 5923f4ff561SLars-Peter Clausen return test_bit(idx, cache_present); 5933f4ff561SLars-Peter Clausen } 5943f4ff561SLars-Peter Clausen 595cfdeb8c3SMark Brown static int regcache_sync_block_single(struct regmap *map, void *block, 5963f4ff561SLars-Peter Clausen unsigned long *cache_present, 597cfdeb8c3SMark Brown unsigned int block_base, 598cfdeb8c3SMark Brown unsigned int start, unsigned int end) 599cfdeb8c3SMark Brown { 600cfdeb8c3SMark Brown unsigned int i, regtmp, val; 601cfdeb8c3SMark Brown int ret; 602cfdeb8c3SMark Brown 603cfdeb8c3SMark Brown for (i = start; i < end; i++) { 604cfdeb8c3SMark Brown regtmp = block_base + (i * map->reg_stride); 605cfdeb8c3SMark Brown 6063f4ff561SLars-Peter Clausen if (!regcache_reg_present(cache_present, i)) 607cfdeb8c3SMark Brown continue; 608cfdeb8c3SMark Brown 609cfdeb8c3SMark Brown val = regcache_get_val(map, block, i); 610cfdeb8c3SMark Brown 611cfdeb8c3SMark Brown /* Is this the hardware default? If so skip. */ 612cfdeb8c3SMark Brown ret = regcache_lookup_reg(map, regtmp); 613cfdeb8c3SMark Brown if (ret >= 0 && val == map->reg_defaults[ret].def) 614cfdeb8c3SMark Brown continue; 615cfdeb8c3SMark Brown 616cfdeb8c3SMark Brown map->cache_bypass = 1; 617cfdeb8c3SMark Brown 618cfdeb8c3SMark Brown ret = _regmap_write(map, regtmp, val); 619cfdeb8c3SMark Brown 620cfdeb8c3SMark Brown map->cache_bypass = 0; 621cfdeb8c3SMark Brown if (ret != 0) 622cfdeb8c3SMark Brown return ret; 623cfdeb8c3SMark Brown dev_dbg(map->dev, "Synced register %#x, value %#x\n", 624cfdeb8c3SMark Brown regtmp, val); 625cfdeb8c3SMark Brown } 626cfdeb8c3SMark Brown 627cfdeb8c3SMark Brown return 0; 628cfdeb8c3SMark Brown } 629cfdeb8c3SMark Brown 63075a5f89fSMark Brown static int regcache_sync_block_raw_flush(struct regmap *map, const void **data, 63175a5f89fSMark Brown unsigned int base, unsigned int cur) 63275a5f89fSMark Brown { 63375a5f89fSMark Brown size_t val_bytes = map->format.val_bytes; 63475a5f89fSMark Brown int ret, count; 63575a5f89fSMark Brown 63675a5f89fSMark Brown if (*data == NULL) 63775a5f89fSMark Brown return 0; 63875a5f89fSMark Brown 639*78ba73eeSDylan Reid count = (cur - base) / map->reg_stride; 64075a5f89fSMark Brown 6419659293cSStratos Karafotis dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n", 642*78ba73eeSDylan Reid count * val_bytes, count, base, cur - map->reg_stride); 64375a5f89fSMark Brown 64475a5f89fSMark Brown map->cache_bypass = 1; 64575a5f89fSMark Brown 6460a819809SMark Brown ret = _regmap_raw_write(map, base, *data, count * val_bytes); 64775a5f89fSMark Brown 64875a5f89fSMark Brown map->cache_bypass = 0; 64975a5f89fSMark Brown 65075a5f89fSMark Brown *data = NULL; 65175a5f89fSMark Brown 65275a5f89fSMark Brown return ret; 65375a5f89fSMark Brown } 65475a5f89fSMark Brown 655f52687afSSachin Kamat static int regcache_sync_block_raw(struct regmap *map, void *block, 6563f4ff561SLars-Peter Clausen unsigned long *cache_present, 657f8bd822cSMark Brown unsigned int block_base, unsigned int start, 658f8bd822cSMark Brown unsigned int end) 659f8bd822cSMark Brown { 66075a5f89fSMark Brown unsigned int i, val; 66175a5f89fSMark Brown unsigned int regtmp = 0; 66275a5f89fSMark Brown unsigned int base = 0; 66375a5f89fSMark Brown const void *data = NULL; 664f8bd822cSMark Brown int ret; 665f8bd822cSMark Brown 666f8bd822cSMark Brown for (i = start; i < end; i++) { 667f8bd822cSMark Brown regtmp = block_base + (i * map->reg_stride); 668f8bd822cSMark Brown 6693f4ff561SLars-Peter Clausen if (!regcache_reg_present(cache_present, i)) { 67075a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 67175a5f89fSMark Brown base, regtmp); 67275a5f89fSMark Brown if (ret != 0) 67375a5f89fSMark Brown return ret; 674f8bd822cSMark Brown continue; 67575a5f89fSMark Brown } 676f8bd822cSMark Brown 677f8bd822cSMark Brown val = regcache_get_val(map, block, i); 678f8bd822cSMark Brown 679f8bd822cSMark Brown /* Is this the hardware default? If so skip. */ 680f8bd822cSMark Brown ret = regcache_lookup_reg(map, regtmp); 68175a5f89fSMark Brown if (ret >= 0 && val == map->reg_defaults[ret].def) { 68275a5f89fSMark Brown ret = regcache_sync_block_raw_flush(map, &data, 68375a5f89fSMark Brown base, regtmp); 684f8bd822cSMark Brown if (ret != 0) 685f8bd822cSMark Brown return ret; 68675a5f89fSMark Brown continue; 687f8bd822cSMark Brown } 688f8bd822cSMark Brown 68975a5f89fSMark Brown if (!data) { 69075a5f89fSMark Brown data = regcache_get_val_addr(map, block, i); 69175a5f89fSMark Brown base = regtmp; 69275a5f89fSMark Brown } 69375a5f89fSMark Brown } 69475a5f89fSMark Brown 6952d49b598SLars-Peter Clausen return regcache_sync_block_raw_flush(map, &data, base, regtmp + 6962d49b598SLars-Peter Clausen map->reg_stride); 697f8bd822cSMark Brown } 698cfdeb8c3SMark Brown 699cfdeb8c3SMark Brown int regcache_sync_block(struct regmap *map, void *block, 7003f4ff561SLars-Peter Clausen unsigned long *cache_present, 701cfdeb8c3SMark Brown unsigned int block_base, unsigned int start, 702cfdeb8c3SMark Brown unsigned int end) 703cfdeb8c3SMark Brown { 704cfdeb8c3SMark Brown if (regmap_can_raw_write(map)) 7053f4ff561SLars-Peter Clausen return regcache_sync_block_raw(map, block, cache_present, 7063f4ff561SLars-Peter Clausen block_base, start, end); 707cfdeb8c3SMark Brown else 7083f4ff561SLars-Peter Clausen return regcache_sync_block_single(map, block, cache_present, 7093f4ff561SLars-Peter Clausen block_base, start, end); 710cfdeb8c3SMark Brown } 711